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

@@ -7,23 +7,19 @@
namespace Slic3r {
template BoundingBoxBase<Point>::BoundingBoxBase(const std::vector<Point> &points);
template BoundingBoxBase<Point, Points>::BoundingBoxBase(const Points &points);
template BoundingBoxBase<Vec2d>::BoundingBoxBase(const std::vector<Vec2d> &points);
template BoundingBox3Base<Vec3d>::BoundingBox3Base(const std::vector<Vec3d> &points);
void BoundingBox::polygon(Polygon* polygon) const
{
polygon->points.clear();
polygon->points.resize(4);
polygon->points[0](0) = this->min(0);
polygon->points[0](1) = this->min(1);
polygon->points[1](0) = this->max(0);
polygon->points[1](1) = this->min(1);
polygon->points[2](0) = this->max(0);
polygon->points[2](1) = this->max(1);
polygon->points[3](0) = this->min(0);
polygon->points[3](1) = this->max(1);
polygon->points = {
this->min,
{ this->max.x(), this->min.y() },
this->max,
{ this->min.x(), this->max.y() }
};
}
Polygon BoundingBox::polygon() const
@@ -38,8 +34,8 @@ BoundingBox BoundingBox::rotated(double angle) const
BoundingBox out;
out.merge(this->min.rotated(angle));
out.merge(this->max.rotated(angle));
out.merge(Point(this->min(0), this->max(1)).rotated(angle));
out.merge(Point(this->max(0), this->min(1)).rotated(angle));
out.merge(Point(this->min.x(), this->max.y()).rotated(angle));
out.merge(Point(this->max.x(), this->min.y()).rotated(angle));
return out;
}
@@ -48,23 +44,23 @@ BoundingBox BoundingBox::rotated(double angle, const Point &center) const
BoundingBox out;
out.merge(this->min.rotated(angle, center));
out.merge(this->max.rotated(angle, center));
out.merge(Point(this->min(0), this->max(1)).rotated(angle, center));
out.merge(Point(this->max(0), this->min(1)).rotated(angle, center));
out.merge(Point(this->min.x(), this->max.y()).rotated(angle, center));
out.merge(Point(this->max.x(), this->min.y()).rotated(angle, center));
return out;
}
template <class PointClass> void
BoundingBoxBase<PointClass>::scale(double factor)
template <class PointType, typename APointsType> void
BoundingBoxBase<PointType, APointsType>::scale(double factor)
{
this->min *= factor;
this->max *= factor;
}
template void BoundingBoxBase<Point>::scale(double factor);
template void BoundingBoxBase<Point, Points>::scale(double factor);
template void BoundingBoxBase<Vec2d>::scale(double factor);
template void BoundingBoxBase<Vec3d>::scale(double factor);
template <class PointClass> void
BoundingBoxBase<PointClass>::merge(const PointClass &point)
template <class PointType, typename APointsType> void
BoundingBoxBase<PointType, APointsType>::merge(const PointType &point)
{
if (this->defined) {
this->min = this->min.cwiseMin(point);
@@ -75,22 +71,22 @@ BoundingBoxBase<PointClass>::merge(const PointClass &point)
this->defined = true;
}
}
template void BoundingBoxBase<Point>::merge(const Point &point);
template void BoundingBoxBase<Point, Points>::merge(const Point &point);
template void BoundingBoxBase<Vec2f>::merge(const Vec2f &point);
template void BoundingBoxBase<Vec2d>::merge(const Vec2d &point);
template <class PointClass> void
BoundingBoxBase<PointClass>::merge(const std::vector<PointClass> &points)
template <class PointType, typename APointsType> void
BoundingBoxBase<PointType, APointsType>::merge(const PointsType &points)
{
this->merge(BoundingBoxBase(points));
}
template void BoundingBoxBase<Point>::merge(const Points &points);
template void BoundingBoxBase<Point, Points>::merge(const Points &points);
template void BoundingBoxBase<Vec2d>::merge(const Pointfs &points);
template <class PointClass> void
BoundingBoxBase<PointClass>::merge(const BoundingBoxBase<PointClass> &bb)
template <class PointType, typename APointsType> void
BoundingBoxBase<PointType, APointsType>::merge(const BoundingBoxBase<PointType, PointsType> &bb)
{
assert(bb.defined || bb.min(0) >= bb.max(0) || bb.min(1) >= bb.max(1));
assert(bb.defined || bb.min.x() >= bb.max.x() || bb.min.y() >= bb.max.y());
if (bb.defined) {
if (this->defined) {
this->min = this->min.cwiseMin(bb.min);
@@ -102,13 +98,13 @@ BoundingBoxBase<PointClass>::merge(const BoundingBoxBase<PointClass> &bb)
}
}
}
template void BoundingBoxBase<Point>::merge(const BoundingBoxBase<Point> &bb);
template void BoundingBoxBase<Point, Points>::merge(const BoundingBoxBase<Point, Points> &bb);
template void BoundingBoxBase<Vec2f>::merge(const BoundingBoxBase<Vec2f> &bb);
template void BoundingBoxBase<Vec2d>::merge(const BoundingBoxBase<Vec2d> &bb);
//BBS
template <class PointClass>
Polygon BoundingBox3Base<PointClass>::polygon(bool is_scaled) const
template <class PointType>
Polygon BoundingBox3Base<PointType>::polygon(bool is_scaled) const
{
Polygon polygon;
polygon.points.clear();
@@ -127,8 +123,8 @@ Polygon BoundingBox3Base<PointClass>::polygon(bool is_scaled) const
template Polygon BoundingBox3Base<Vec3f>::polygon(bool is_scaled) const;
template Polygon BoundingBox3Base<Vec3d>::polygon(bool is_scaled) const;
template <class PointClass> void
BoundingBox3Base<PointClass>::merge(const PointClass &point)
template <class PointType> void
BoundingBox3Base<PointType>::merge(const PointType &point)
{
if (this->defined) {
this->min = this->min.cwiseMin(point);
@@ -142,17 +138,17 @@ BoundingBox3Base<PointClass>::merge(const PointClass &point)
template void BoundingBox3Base<Vec3f>::merge(const Vec3f &point);
template void BoundingBox3Base<Vec3d>::merge(const Vec3d &point);
template <class PointClass> void
BoundingBox3Base<PointClass>::merge(const std::vector<PointClass> &points)
template <class PointType> void
BoundingBox3Base<PointType>::merge(const PointsType &points)
{
this->merge(BoundingBox3Base(points));
}
template void BoundingBox3Base<Vec3d>::merge(const Pointf3s &points);
template <class PointClass> void
BoundingBox3Base<PointClass>::merge(const BoundingBox3Base<PointClass> &bb)
template <class PointType> void
BoundingBox3Base<PointType>::merge(const BoundingBox3Base<PointType> &bb)
{
assert(bb.defined || bb.min(0) >= bb.max(0) || bb.min(1) >= bb.max(1) || bb.min(2) >= bb.max(2));
assert(bb.defined || bb.min.x() >= bb.max.x() || bb.min.y() >= bb.max.y() || bb.min.z() >= bb.max.z());
if (bb.defined) {
if (this->defined) {
this->min = this->min.cwiseMin(bb.min);
@@ -166,83 +162,78 @@ BoundingBox3Base<PointClass>::merge(const BoundingBox3Base<PointClass> &bb)
}
template void BoundingBox3Base<Vec3d>::merge(const BoundingBox3Base<Vec3d> &bb);
template <class PointClass> PointClass
BoundingBoxBase<PointClass>::size() const
template <class PointType, typename APointsType> PointType
BoundingBoxBase<PointType, APointsType>::size() const
{
return PointClass(this->max(0) - this->min(0), this->max(1) - this->min(1));
return this->max - this->min;
}
template Point BoundingBoxBase<Point>::size() const;
template Point BoundingBoxBase<Point, Points>::size() const;
template Vec2f BoundingBoxBase<Vec2f>::size() const;
template Vec2d BoundingBoxBase<Vec2d>::size() const;
template <class PointClass> PointClass
BoundingBox3Base<PointClass>::size() const
template <class PointType> PointType
BoundingBox3Base<PointType>::size() const
{
return PointClass(this->max(0) - this->min(0), this->max(1) - this->min(1), this->max(2) - this->min(2));
return this->max - this->min;
}
template Vec3f BoundingBox3Base<Vec3f>::size() const;
template Vec3d BoundingBox3Base<Vec3d>::size() const;
template <class PointClass> double BoundingBoxBase<PointClass>::radius() const
template <class PointType, typename APointsType> double BoundingBoxBase<PointType, APointsType>::radius() const
{
assert(this->defined);
double x = this->max(0) - this->min(0);
double y = this->max(1) - this->min(1);
return 0.5 * sqrt(x*x+y*y);
return 0.5 * (this->max - this->min).template cast<double>().norm();
}
template double BoundingBoxBase<Point>::radius() const;
template double BoundingBoxBase<Point, Points>::radius() const;
template double BoundingBoxBase<Vec2d>::radius() const;
template <class PointClass> double BoundingBox3Base<PointClass>::radius() const
template <class PointType> double BoundingBox3Base<PointType>::radius() const
{
double x = this->max(0) - this->min(0);
double y = this->max(1) - this->min(1);
double z = this->max(2) - this->min(2);
return 0.5 * sqrt(x*x+y*y+z*z);
return 0.5 * (this->max - this->min).template cast<double>().norm();
}
template double BoundingBox3Base<Vec3d>::radius() const;
template <class PointClass> void
BoundingBoxBase<PointClass>::offset(coordf_t delta)
template <class PointType, typename APointsType> void
BoundingBoxBase<PointType, APointsType>::offset(coordf_t delta)
{
PointClass v(delta, delta);
PointType v(delta, delta);
this->min -= v;
this->max += v;
}
template void BoundingBoxBase<Point>::offset(coordf_t delta);
template void BoundingBoxBase<Point, Points>::offset(coordf_t delta);
template void BoundingBoxBase<Vec2d>::offset(coordf_t delta);
template <class PointClass> void
BoundingBox3Base<PointClass>::offset(coordf_t delta)
template <class PointType> void
BoundingBox3Base<PointType>::offset(coordf_t delta)
{
PointClass v(delta, delta, delta);
PointType v(delta, delta, delta);
this->min -= v;
this->max += v;
}
template void BoundingBox3Base<Vec3d>::offset(coordf_t delta);
template <class PointClass> PointClass
BoundingBoxBase<PointClass>::center() const
template <class PointType, typename APointsType> PointType
BoundingBoxBase<PointType, APointsType>::center() const
{
return (this->min + this->max) / 2;
}
template Point BoundingBoxBase<Point>::center() const;
template Point BoundingBoxBase<Point, Points>::center() const;
template Vec2f BoundingBoxBase<Vec2f>::center() const;
template Vec2d BoundingBoxBase<Vec2d>::center() const;
template <class PointClass> PointClass
BoundingBox3Base<PointClass>::center() const
template <class PointType> PointType
BoundingBox3Base<PointType>::center() const
{
return (this->min + this->max) / 2;
}
template Vec3f BoundingBox3Base<Vec3f>::center() const;
template Vec3d BoundingBox3Base<Vec3d>::center() const;
template <class PointClass> coordf_t
BoundingBox3Base<PointClass>::max_size() const
template <class PointType> coordf_t
BoundingBox3Base<PointType>::max_size() const
{
PointClass s = size();
return std::max(s(0), std::max(s(1), s(2)));
PointType s = size();
return std::max(s.x(), std::max(s.y(), s.z()));
}
template coordf_t BoundingBox3Base<Vec3f>::max_size() const;
template coordf_t BoundingBox3Base<Vec3d>::max_size() const;
@@ -250,8 +241,8 @@ template coordf_t BoundingBox3Base<Vec3d>::max_size() const;
void BoundingBox::align_to_grid(const coord_t cell_size)
{
if (this->defined) {
min(0) = Slic3r::align_to_grid(min(0), cell_size);
min(1) = Slic3r::align_to_grid(min(1), cell_size);
min.x() = Slic3r::align_to_grid(min.x(), cell_size);
min.y() = Slic3r::align_to_grid(min.y(), cell_size);
}
}
@@ -260,14 +251,14 @@ BoundingBoxf3 BoundingBoxf3::transformed(const Transform3d& matrix) const
typedef Eigen::Matrix<double, 3, 8, Eigen::DontAlign> Vertices;
Vertices src_vertices;
src_vertices(0, 0) = min(0); src_vertices(1, 0) = min(1); src_vertices(2, 0) = min(2);
src_vertices(0, 1) = max(0); src_vertices(1, 1) = min(1); src_vertices(2, 1) = min(2);
src_vertices(0, 2) = max(0); src_vertices(1, 2) = max(1); src_vertices(2, 2) = min(2);
src_vertices(0, 3) = min(0); src_vertices(1, 3) = max(1); src_vertices(2, 3) = min(2);
src_vertices(0, 4) = min(0); src_vertices(1, 4) = min(1); src_vertices(2, 4) = max(2);
src_vertices(0, 5) = max(0); src_vertices(1, 5) = min(1); src_vertices(2, 5) = max(2);
src_vertices(0, 6) = max(0); src_vertices(1, 6) = max(1); src_vertices(2, 6) = max(2);
src_vertices(0, 7) = min(0); src_vertices(1, 7) = max(1); src_vertices(2, 7) = max(2);
src_vertices(0, 0) = min.x(); src_vertices(1, 0) = min.y(); src_vertices(2, 0) = min.z();
src_vertices(0, 1) = max.x(); src_vertices(1, 1) = min.y(); src_vertices(2, 1) = min.z();
src_vertices(0, 2) = max.x(); src_vertices(1, 2) = max.y(); src_vertices(2, 2) = min.z();
src_vertices(0, 3) = min.x(); src_vertices(1, 3) = max.y(); src_vertices(2, 3) = min.z();
src_vertices(0, 4) = min.x(); src_vertices(1, 4) = min.y(); src_vertices(2, 4) = max.z();
src_vertices(0, 5) = max.x(); src_vertices(1, 5) = min.y(); src_vertices(2, 5) = max.z();
src_vertices(0, 6) = max.x(); src_vertices(1, 6) = max.y(); src_vertices(2, 6) = max.z();
src_vertices(0, 7) = min.x(); src_vertices(1, 7) = max.y(); src_vertices(2, 7) = max.z();
Vertices dst_vertices = matrix * src_vertices.colwise().homogeneous();