Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for to clipper::union without enough offset #6451

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/clipper/clipper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,25 @@ double Area(const Path &poly)
return -a * 0.5;
}
//------------------------------------------------------------------------------
IntPoint Centroid(const Path& poly, double area)
{
double x_temp = 0;
double y_temp = 0;

const int max = poly.size() - 1;
size_t i = 0;
for (; i < max; ++i)
{
size_t j = i + 1;
x_temp += (double)(poly[i].X + poly[j].X) * ((double)poly[i].X * poly[j].Y - (double)poly[j].X * poly[i].Y);
y_temp += (double)(poly[i].Y + poly[j].Y) * ((double)poly[i].X * poly[j].Y - (double)poly[j].X * poly[i].Y);
}
x_temp += (double)(poly[i].X + poly[0].X) * ((double)poly[i].X * poly[0].Y - (double)poly[0].X * poly[i].Y);
y_temp += (double)(poly[i].Y + poly[0].Y) * ((double)poly[i].X * poly[0].Y - (double)poly[0].X * poly[i].Y);

return IntPoint(x_temp / (6 * area), y_temp / (6 * area));
}
//------------------------------------------------------------------------------

double Area(const OutRec &outRec)
{
Expand Down
1 change: 1 addition & 0 deletions src/clipper/clipper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ class PolyTree: public PolyNode
};

double Area(const Path &poly);
IntPoint Centroid(const Path& poly, double area);
inline bool Orientation(const Path &poly) { return Area(poly) >= 0; }
int PointInPolygon(const IntPoint &pt, const Path &path);

Expand Down
34 changes: 34 additions & 0 deletions src/libslic3r/ClipperUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,22 @@ T _clipper_do(const ClipperLib::ClipType clipType,
return retval;
}

bool test_path(const ClipperLib::Path &path) {

double area = std::abs(ClipperLib::Area(path));
// get highest dist, but as it's n² in complexity, i use 2*dist to center wich is 2n in complexity
ClipperLib::cInt max_dist_sqrd = 0;
ClipperLib::IntPoint centroid = ClipperLib::Centroid(path, area);
for (const ClipperLib::IntPoint& pt : path) {
// &0x3FFFFFFF to let (dx * dx + dy * dy) be storable into a int64
ClipperLib::cInt dx = (pt.X - centroid.X) & 0x3FFFFFFF;
ClipperLib::cInt dy = (pt.Y - centroid.Y) & 0x3FFFFFFF;
ClipperLib::cInt dist_sqrd = (dx * dx + dy * dy);
max_dist_sqrd = std::max(max_dist_sqrd, dist_sqrd);
}
return (area < (SCALED_EPSILON + SCALED_EPSILON) * std::sqrt(max_dist_sqrd));
}

// Fix of #117: A large fractal pyramid takes ages to slice
// The Clipper library has difficulties processing overlapping polygons.
// Namely, the function ClipperLib::JoinCommonEdges() has potentially a terrible time complexity if the output
Expand Down Expand Up @@ -609,6 +625,24 @@ inline ClipperLib::PolyTree _clipper_do_polytree2(const ClipperLib::ClipType cli
clipper.AddPaths(input_subject, ClipperLib::ptSubject, true);
ClipperLib::PolyTree retval;
clipper.Execute(ClipperLib::ctUnion, retval, fillType, fillType);

// if safety_offset_, remove too small polygons & holes
if (safety_offset_)
for (int idx_poly = 0; idx_poly < retval.ChildCount(); ++idx_poly) {
ClipperLib::PolyNode* ex_polygon = retval.Childs[idx_poly];
if (test_path(ex_polygon->Contour)) {
retval.Childs.erase(retval.Childs.begin() + idx_poly);
--idx_poly;
} else {
for (int i = 0; i < ex_polygon->ChildCount(); ++i)
{
if (test_path(ex_polygon->Childs[i]->Contour)) {
ex_polygon->Childs.erase(ex_polygon->Childs.begin() + i);
--i;
}
}
}
}
return retval;
}

Expand Down