Upgrade clipper & improve multi-thread performance (#7177)

* Clipper: Verify range of int32 coordinates on input.

Cherry-picked from prusa3d/PrusaSlicer@fa7debf49d

Co-authored-by: Vojtech Bubnik <bubnikv@gmail.com>

* ClipperLib: Optimized PointInPolygon() to calculate cross products
 with int64s instead of doubles.

Cherry-picked from prusa3d/PrusaSlicer@9dca8403fe

Co-authored-by: Vojtech Bubnik <bubnikv@gmail.com>

* Reworked the ClipperLib / Polygon types to use
the tbb::scallable_allocator to better scale on multiple threads.

Cherry-picked from prusa3d/PrusaSlicer@9cde96993e

Co-authored-by: Vojtech Bubnik <bubnikv@gmail.com>

* use tbb::scallable_allocator for Polygons and ExPolygon::holes
to better scale on multiple threads

Cherry-picked from prusa3d/PrusaSlicer@b67ad6434d

Co-authored-by: Vojtech Bubnik <bubnikv@gmail.com>

* Fixed compilation on GCC and CLang

Cherry-picked from prusa3d/PrusaSlicer@b3b44681a9

Co-authored-by: Vojtech Bubnik <bubnikv@gmail.com>

* Remove clipper2 which is not used

* Removed shiny profiler from clipperlib

Cherry-picked from prusa3d/PrusaSlicer@7e77048593

Co-authored-by: Vojtech Bubnik <bubnikv@gmail.com>

* ClipperLib: Further optimization of memory allocation using scalable_allocator.
ClipperLib: SimplifyPolygon() - changed default winding number to positive,
      added strictly_simple parameter.
ClipperUtlis simplify_polygons() - removed "remove_collinear" parameter

Cherry-picked from prusa3d/PrusaSlicer@a7e17df25f

Co-authored-by: Vojtech Bubnik <bubnikv@gmail.com>

* ClipperLib: emplace_back() instead of push_back().

Cherry-picked from prusa3d/PrusaSlicer@2e150795b1

Co-authored-by: Vojtech Bubnik <bubnikv@gmail.com>

* Fixed issue in a 32bit clipper, where IntersectPoint() checked for
the Y coordinate of the calculated intersection point for validity,
but the Y coordinate was already rounded to 32bits, thus an overflow
may have in rare cases masked invalidity of the result.

Cherry-picked from prusa3d/PrusaSlicer@b39c33414f

Co-authored-by: Vojtech Bubnik <bubnikv@gmail.com>

* Fixed Vojtech's out of boundary assert in Clipper library.

Cherry-picked from prusa3d/PrusaSlicer@0a202dcff3

Co-authored-by: Vojtech Bubnik <bubnikv@gmail.com>

* Update clipper to 6.4.2.

Cherry-picked from prusa3d/PrusaSlicer@b8b3cccb40

Co-authored-by: Lukáš Hejl <hejl.lukas@gmail.com>

* Try fix cmake opencv

---------

Co-authored-by: Vojtech Bubnik <bubnikv@gmail.com>
Co-authored-by: Lukáš Hejl <hejl.lukas@gmail.com>
This commit is contained in:
Noisyfox
2025-06-18 17:50:44 +08:00
committed by GitHub
parent 95b4334bf7
commit fe4a72ec94
46 changed files with 649 additions and 8481 deletions

View File

@@ -61,8 +61,10 @@ Points SinglePathProvider::s_end;
// Clip source polygon to be used as a clipping polygon with a bouding box around the source (to be clipped) polygon.
// Useful as an optimization for expensive ClipperLib operations, for example when clipping source polygons one by one
// with a set of polygons covering the whole layer below.
template<typename PointType> inline void clip_clipper_polygon_with_subject_bbox_templ(const std::vector<PointType> &src, const BoundingBox &bbox, std::vector<PointType> &out, const bool get_entire_polygons=false)
template<typename PointsType> inline void clip_clipper_polygon_with_subject_bbox_templ(const PointsType &src, const BoundingBox &bbox, PointsType &out, const bool get_entire_polygons=false)
{
using PointType = typename PointsType::value_type;
out.clear();
const size_t cnt = src.size();
if (cnt < 3) return;
@@ -112,9 +114,9 @@ template<typename PointType> inline void clip_clipper_polygon_with_subject_bbox_
void clip_clipper_polygon_with_subject_bbox(const Points &src, const BoundingBox &bbox, Points &out, const bool get_entire_polygons) { clip_clipper_polygon_with_subject_bbox_templ(src, bbox, out, get_entire_polygons); }
void clip_clipper_polygon_with_subject_bbox(const ZPoints &src, const BoundingBox &bbox, ZPoints &out) { clip_clipper_polygon_with_subject_bbox_templ(src, bbox, out); }
template<typename PointType> [[nodiscard]] std::vector<PointType> clip_clipper_polygon_with_subject_bbox_templ(const std::vector<PointType> &src, const BoundingBox &bbox)
template<typename PointsType> [[nodiscard]] PointsType clip_clipper_polygon_with_subject_bbox_templ(const PointsType &src, const BoundingBox &bbox)
{
std::vector<PointType> out;
PointsType out;
clip_clipper_polygon_with_subject_bbox(src, bbox, out);
return out;
}
@@ -1001,31 +1003,26 @@ Polygons union_pt_chained_outside_in(const Polygons &subject)
return retval;
}
Polygons simplify_polygons(const Polygons &subject, bool preserve_collinear)
Polygons simplify_polygons(const Polygons &subject)
{
ClipperLib::Paths output;
if (preserve_collinear) {
ClipperLib::Clipper c;
c.PreserveCollinear(true);
c.StrictlySimple(true);
c.AddPaths(ClipperUtils::PolygonsProvider(subject), ClipperLib::ptSubject, true);
c.Execute(ClipperLib::ctUnion, output, ClipperLib::pftNonZero, ClipperLib::pftNonZero);
} else {
output = ClipperLib::SimplifyPolygons(ClipperUtils::PolygonsProvider(subject), ClipperLib::pftNonZero);
}
ClipperLib::Clipper c;
// c.PreserveCollinear(true);
//FIXME StrictlySimple is very expensive! Is it needed?
c.StrictlySimple(true);
c.AddPaths(ClipperUtils::PolygonsProvider(subject), ClipperLib::ptSubject, true);
c.Execute(ClipperLib::ctUnion, output, ClipperLib::pftNonZero, ClipperLib::pftNonZero);
// convert into Slic3r polygons
return to_polygons(std::move(output));
}
ExPolygons simplify_polygons_ex(const Polygons &subject, bool preserve_collinear)
ExPolygons simplify_polygons_ex(const Polygons &subject)
{
if (! preserve_collinear)
return union_ex(simplify_polygons(subject, false));
ClipperLib::PolyTree polytree;
ClipperLib::PolyTree polytree;
ClipperLib::Clipper c;
c.PreserveCollinear(true);
// c.PreserveCollinear(true);
//FIXME StrictlySimple is very expensive! Is it needed?
c.StrictlySimple(true);
c.AddPaths(ClipperUtils::PolygonsProvider(subject), ClipperLib::ptSubject, true);
c.Execute(ClipperLib::ctUnion, polytree, ClipperLib::pftNonZero, ClipperLib::pftNonZero);