diff --git a/src/libslic3r/Geometry.hpp b/src/libslic3r/Geometry.hpp index 2bf9453c4..92333f203 100644 --- a/src/libslic3r/Geometry.hpp +++ b/src/libslic3r/Geometry.hpp @@ -213,16 +213,7 @@ inline bool liang_barsky_line_clipping_interval( double t0 = 0.0; double t1 = 1.0; // Traverse through left, right, bottom, top edges. - for (int edge = 0; edge < 4; ++ edge) - { - double p, q; - switch (edge) { - case 0: p = - v.x(); q = - bbox.min.x() + x0.x(); break; - case 1: p = v.x(); q = bbox.max.x() - x0.x(); break; - case 2: p = - v.y(); q = - bbox.min.y() + x0.y(); break; - default: p = v.y(); q = bbox.max.y() - x0.y(); break; - } - + auto clip_side = [&x0, &v, &bbox, &t0, &t1](double p, double q) -> bool { if (p == 0) { if (q < 0) // Line parallel to the bounding box edge is fully outside of the bounding box. @@ -247,10 +238,18 @@ inline bool liang_barsky_line_clipping_interval( t1 = r; } } + return true; + }; + + if (clip_side(- v.x(), - bbox.min.x() + x0.x()) && + clip_side( v.x(), bbox.max.x() - x0.x()) && + clip_side(- v.y(), - bbox.min.y() + x0.y()) && + clip_side( v.y(), bbox.max.y() - x0.y())) { + out_interval.first = t0; + out_interval.second = t1; + return true; } - out_interval.first = t0; - out_interval.second = t1; - return true; + return false; } template diff --git a/src/libslic3r/Point.cpp b/src/libslic3r/Point.cpp index f3ed41342..555a1cb2d 100644 --- a/src/libslic3r/Point.cpp +++ b/src/libslic3r/Point.cpp @@ -176,6 +176,19 @@ Point Point::projection_onto(const Line &line) const return ((line.a - *this).cast().squaredNorm() < (line.b - *this).cast().squaredNorm()) ? line.a : line.b; } +BoundingBox get_extents(const Points &pts) +{ + return BoundingBox(pts); +} + +BoundingBox get_extents(const std::vector &pts) +{ + BoundingBox bbox; + for (const Points &p : pts) + bbox.merge(get_extents(p)); + return bbox; +} + std::ostream& operator<<(std::ostream &stm, const Vec2d &pointf) { return stm << pointf(0) << "," << pointf(1); diff --git a/src/libslic3r/Point.hpp b/src/libslic3r/Point.hpp index 3b0c54324..a6525af4e 100644 --- a/src/libslic3r/Point.hpp +++ b/src/libslic3r/Point.hpp @@ -13,6 +13,7 @@ namespace Slic3r { +class BoundingBox; class Line; class MultiPoint; class Point; @@ -178,6 +179,9 @@ inline Point lerp(const Point &a, const Point &b, double t) return ((1. - t) * a.cast() + t * b.cast()).cast(); } +extern BoundingBox get_extents(const Points &pts); +extern BoundingBox get_extents(const std::vector &pts); + namespace int128 { // Exact orientation predicate, // returns +1: CCW, 0: collinear, -1: CW. diff --git a/src/libslic3r/Polygon.cpp b/src/libslic3r/Polygon.cpp index 13cdf65b4..f74df1c9a 100644 --- a/src/libslic3r/Polygon.cpp +++ b/src/libslic3r/Polygon.cpp @@ -298,11 +298,6 @@ void Polygon::densify(float min_length, std::vector* lengths_ptr) assert(points.size() == lengths.size() - 1); } -BoundingBox get_extents(const Points &points) -{ - return BoundingBox(points); -} - BoundingBox get_extents(const Polygon &poly) { return poly.bounding_box(); diff --git a/src/libslic3r/Polygon.hpp b/src/libslic3r/Polygon.hpp index f5aa28914..3c44f66f7 100644 --- a/src/libslic3r/Polygon.hpp +++ b/src/libslic3r/Polygon.hpp @@ -74,7 +74,6 @@ public: inline bool operator==(const Polygon &lhs, const Polygon &rhs) { return lhs.points == rhs.points; } inline bool operator!=(const Polygon &lhs, const Polygon &rhs) { return lhs.points != rhs.points; } -extern BoundingBox get_extents(const Points &points); extern BoundingBox get_extents(const Polygon &poly); extern BoundingBox get_extents(const Polygons &polygons); extern BoundingBox get_extents_rotated(const Polygon &poly, double angle);