FIX:Elephant foot compensation simplify expoly leads to contour loss

jira: none
Change-Id: I4df620e1a0c55803499ec979fa3ea22394f32699
This commit is contained in:
zhimin.zeng 2025-02-07 17:34:57 +08:00 committed by lane.wei
parent f2d9902ff3
commit 48368ed472
2 changed files with 34 additions and 20 deletions

View File

@ -29,7 +29,7 @@ struct ResampledPoint {
// Distance calculated using SDF (Shape Diameter Function). // Distance calculated using SDF (Shape Diameter Function).
// The distance is calculated by casting a fan of rays and measuring the intersection distance. // The distance is calculated by casting a fan of rays and measuring the intersection distance.
// Thus the calculation is relatively slow. For the Elephant foot compensation purpose, this distance metric does not avoid // Thus the calculation is relatively slow. For the Elephant foot compensation purpose, this distance metric does not avoid
// pinching off small pieces of a contour, thus this function has been superseded by contour_distance2(). // pinching off small pieces of a contour, thus this function has been superseded by contour_distance2().
std::vector<float> contour_distance(const EdgeGrid::Grid &grid, const size_t idx_contour, const Slic3r::Points &contour, const std::vector<ResampledPoint> &resampled_point_parameters, double search_radius) std::vector<float> contour_distance(const EdgeGrid::Grid &grid, const size_t idx_contour, const Slic3r::Points &contour, const std::vector<ResampledPoint> &resampled_point_parameters, double search_radius)
{ {
@ -38,7 +38,7 @@ std::vector<float> contour_distance(const EdgeGrid::Grid &grid, const size_t idx
std::vector<float> out; std::vector<float> out;
if (contour.size() > 2) if (contour.size() > 2)
{ {
#ifdef CONTOUR_DISTANCE_DEBUG_SVG #ifdef CONTOUR_DISTANCE_DEBUG_SVG
static int iRun = 0; static int iRun = 0;
@ -237,7 +237,7 @@ std::vector<float> contour_distance2(const EdgeGrid::Grid &grid, const size_t id
std::vector<float> out; std::vector<float> out;
if (contour.size() > 2) if (contour.size() > 2)
{ {
#ifdef CONTOUR_DISTANCE_DEBUG_SVG #ifdef CONTOUR_DISTANCE_DEBUG_SVG
static int iRun = 0; static int iRun = 0;
@ -302,11 +302,11 @@ std::vector<float> contour_distance2(const EdgeGrid::Grid &grid, const size_t id
if (dist_along_contour < dist_same_contour_accept) if (dist_along_contour < dist_same_contour_accept)
accept = false; accept = false;
else if (dist < dist_same_contour_reject + SCALED_EPSILON) { else if (dist < dist_same_contour_reject + SCALED_EPSILON) {
// this->point is close to foot. This point will only be accepted if the path along the contour is significantly // this->point is close to foot. This point will only be accepted if the path along the contour is significantly
// longer than the bisector. That is, the path shall not bulge away from the bisector too much. // longer than the bisector. That is, the path shall not bulge away from the bisector too much.
// Bulge is estimated by 0.6 of the circle circumference drawn around the bisector. // Bulge is estimated by 0.6 of the circle circumference drawn around the bisector.
// Test whether the contour is convex or concave. // Test whether the contour is convex or concave.
bool inside = bool inside =
(t == 0.) ? this->inside_corner(contour, ipt, this->point) : (t == 0.) ? this->inside_corner(contour, ipt, this->point) :
(t == 1.) ? this->inside_corner(contour, contour.segment_idx_next(ipt), this->point) : (t == 1.) ? this->inside_corner(contour, contour.segment_idx_next(ipt), this->point) :
this->left_of_segment(contour, ipt, this->point); this->left_of_segment(contour, ipt, this->point);
@ -472,7 +472,7 @@ static inline void smooth_compensation_banded(const Points &contour, float band,
for (size_t iter = 0; iter < num_iterations; ++ iter) { for (size_t iter = 0; iter < num_iterations; ++ iter) {
for (int i = 0; i < int(compensation.size()); ++ i) { for (int i = 0; i < int(compensation.size()); ++ i) {
const Vec2f pthis = contour[i].cast<float>(); const Vec2f pthis = contour[i].cast<float>();
int j = prev_idx_modulo(i, contour); int j = prev_idx_modulo(i, contour);
Vec2f pprev = contour[j].cast<float>(); Vec2f pprev = contour[j].cast<float>();
float prev = compensation[j]; float prev = compensation[j];
@ -535,14 +535,14 @@ static inline void smooth_compensation_banded(const Points &contour, float band,
static bool validate_expoly_orientation(const ExPolygon &expoly) static bool validate_expoly_orientation(const ExPolygon &expoly)
{ {
bool valid = expoly.contour.is_counter_clockwise(); bool valid = expoly.contour.is_counter_clockwise();
for (auto &h : expoly.holes) for (auto &h : expoly.holes)
valid &= h.is_clockwise(); valid &= h.is_clockwise();
return valid; return valid;
} }
#endif /* NDEBUG */ #endif /* NDEBUG */
ExPolygon elephant_foot_compensation(const ExPolygon &input_expoly, double min_contour_width, const double compensation) ExPolygon elephant_foot_compensation(const ExPolygon &input_expoly, double min_contour_width, const double compensation)
{ {
assert(validate_expoly_orientation(input_expoly)); assert(validate_expoly_orientation(input_expoly));
double scaled_compensation = scale_(compensation); double scaled_compensation = scale_(compensation);
@ -564,17 +564,16 @@ ExPolygon elephant_foot_compensation(const ExPolygon &input_expoly, double min_c
else else
{ {
EdgeGrid::Grid grid; EdgeGrid::Grid grid;
ExPolygon simplified = input_expoly.simplify(SCALED_EPSILON).front(); assert(validate_expoly_orientation(input_expoly));
assert(validate_expoly_orientation(simplified)); BoundingBox bbox = get_extents(input_expoly.contour);
BoundingBox bbox = get_extents(simplified.contour);
bbox.offset(SCALED_EPSILON); bbox.offset(SCALED_EPSILON);
grid.set_bbox(bbox); grid.set_bbox(bbox);
grid.create(simplified, coord_t(0.7 * search_radius)); grid.create(input_expoly, coord_t(0.7 * search_radius));
std::vector<std::vector<float>> deltas; std::vector<std::vector<float>> deltas;
deltas.reserve(simplified.holes.size() + 1); deltas.reserve(input_expoly.holes.size() + 1);
ExPolygon resampled(simplified); ExPolygon resampled(input_expoly);
double resample_interval = scale_(0.5); double resample_interval = scale_(0.5);
for (size_t idx_contour = 0; idx_contour <= simplified.holes.size(); ++ idx_contour) { for (size_t idx_contour = 0; idx_contour <= input_expoly.holes.size(); ++ idx_contour) {
Polygon &poly = (idx_contour == 0) ? resampled.contour : resampled.holes[idx_contour - 1]; Polygon &poly = (idx_contour == 0) ? resampled.contour : resampled.holes[idx_contour - 1];
std::vector<ResampledPoint> resampled_point_parameters; std::vector<ResampledPoint> resampled_point_parameters;
poly.points = resample_polygon(poly.points, resample_interval, resampled_point_parameters); poly.points = resample_polygon(poly.points, resample_interval, resampled_point_parameters);
@ -613,7 +612,7 @@ ExPolygon elephant_foot_compensation(const ExPolygon &input_expoly, double min_c
assert(out_vec.size() == 1); assert(out_vec.size() == 1);
} }
} }
assert(validate_expoly_orientation(out)); assert(validate_expoly_orientation(out));
return out; return out;
} }
@ -628,8 +627,9 @@ ExPolygon elephant_foot_compensation(const ExPolygon &input, const Flow &exter
ExPolygons elephant_foot_compensation(const ExPolygons &input, const Flow &external_perimeter_flow, const double compensation) ExPolygons elephant_foot_compensation(const ExPolygons &input, const Flow &external_perimeter_flow, const double compensation)
{ {
ExPolygons out; ExPolygons out;
out.reserve(input.size()); ExPolygons simplified_exps = expolygons_simplify(input, SCALED_EPSILON);
for (const ExPolygon &expoly : input) out.reserve(simplified_exps.size());
for (const ExPolygon &expoly : simplified_exps)
out.emplace_back(elephant_foot_compensation(expoly, external_perimeter_flow, compensation)); out.emplace_back(elephant_foot_compensation(expoly, external_perimeter_flow, compensation));
return out; return out;
} }
@ -637,8 +637,9 @@ ExPolygons elephant_foot_compensation(const ExPolygons &input, const Flow &exter
ExPolygons elephant_foot_compensation(const ExPolygons &input, double min_contour_width, const double compensation) ExPolygons elephant_foot_compensation(const ExPolygons &input, double min_contour_width, const double compensation)
{ {
ExPolygons out; ExPolygons out;
out.reserve(input.size()); ExPolygons simplified_exps = expolygons_simplify(input, SCALED_EPSILON);
for (const ExPolygon &expoly : input) out.reserve(simplified_exps.size());
for (const ExPolygon &expoly : simplified_exps)
out.emplace_back(elephant_foot_compensation(expoly, min_contour_width, compensation)); out.emplace_back(elephant_foot_compensation(expoly, min_contour_width, compensation));
return out; return out;
} }

View File

@ -407,6 +407,19 @@ void PrintObject::make_perimeters(const AutoContourHolesCompensationParams &auto
BOOST_LOG_TRIVIAL(debug) << "Generating extra perimeters for region " << region_id << " in parallel - end"; BOOST_LOG_TRIVIAL(debug) << "Generating extra perimeters for region " << region_id << " in parallel - end";
} }
#if 0
{ // for debug
for (size_t layer_idx = 0; layer_idx < m_layers.size(); ++layer_idx) {
auto regions = m_layers[layer_idx]->regions();
for (size_t region_idx = 0; region_idx < regions.size(); ++region_idx) {
LayerRegion *layer_region = regions[region_idx];
std::string name = "before_make_perimeter_layer-" + std::to_string(layer_idx) + "-region-" + std::to_string(region_idx) + ".svg";
layer_region->slices.export_to_svg(debug_out_path(name.c_str()).c_str(), true);
}
}
}
#endif
BOOST_LOG_TRIVIAL(debug) << "Generating perimeters in parallel - start"; BOOST_LOG_TRIVIAL(debug) << "Generating perimeters in parallel - start";
tbb::parallel_for( tbb::parallel_for(
tbb::blocked_range<size_t>(0, m_layers.size()), tbb::blocked_range<size_t>(0, m_layers.size()),