From 8a1e8f97a997be58b1c32f680698d4306ac031a4 Mon Sep 17 00:00:00 2001 From: Vojtech Bubnik Date: Wed, 27 Jul 2022 08:53:48 +0200 Subject: [PATCH] Minor refactoring of BoundingBox: change Eigen point accessor from indices to .x(), .y(), .z() Added Polyline vector accessors. Polished Point hash code. --- src/libslic3r/BoundingBox.hpp | 26 ++++++++++++++------------ src/libslic3r/Point.hpp | 10 ++++++---- src/libslic3r/Polyline.hpp | 3 +++ 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/libslic3r/BoundingBox.hpp b/src/libslic3r/BoundingBox.hpp index 6b06f8bab..1d6cd4ef1 100644 --- a/src/libslic3r/BoundingBox.hpp +++ b/src/libslic3r/BoundingBox.hpp @@ -18,7 +18,7 @@ public: BoundingBoxBase() : min(PointClass::Zero()), max(PointClass::Zero()), defined(false) {} BoundingBoxBase(const PointClass &pmin, const PointClass &pmax) : - min(pmin), max(pmax), defined(pmin(0) < pmax(0) && pmin(1) < pmax(1)) {} + min(pmin), max(pmax), defined(pmin.x() < pmax.x() && pmin.y() < pmax.y()) {} BoundingBoxBase(const PointClass &p1, const PointClass &p2, const PointClass &p3) : min(p1), max(p1), defined(false) { merge(p2); merge(p3); } @@ -37,7 +37,7 @@ public: this->min = this->min.cwiseMin(vec); this->max = this->max.cwiseMax(vec); } - this->defined = (this->min(0) < this->max(0)) && (this->min(1) < this->max(1)); + this->defined = (this->min.x() < this->max.x()) && (this->min.y() < this->max.y()); } } @@ -58,15 +58,15 @@ public: BoundingBoxBase inflated(coordf_t delta) const throw() { BoundingBoxBase out(*this); out.offset(delta); return out; } PointClass center() const; bool contains(const PointClass &point) const { - return point(0) >= this->min(0) && point(0) <= this->max(0) - && point(1) >= this->min(1) && point(1) <= this->max(1); + return point.x() >= this->min.x() && point.x() <= this->max.x() + && point.y() >= this->min.y() && point.y() <= this->max.y(); } bool contains(const BoundingBoxBase &other) const { return contains(other.min) && contains(other.max); } bool overlap(const BoundingBoxBase &other) const { - return ! (this->max(0) < other.min(0) || this->min(0) > other.max(0) || - this->max(1) < other.min(1) || this->min(1) > other.max(1)); + return ! (this->max.x() < other.min.x() || this->min.x() > other.max.x() || + this->max.y() < other.min.y() || this->min.y() > other.max.y()); } bool operator==(const BoundingBoxBase &rhs) { return this->min == rhs.min && this->max == rhs.max; } bool operator!=(const BoundingBoxBase &rhs) { return ! (*this == rhs); } @@ -79,7 +79,7 @@ public: BoundingBox3Base() : BoundingBoxBase() {} BoundingBox3Base(const PointClass &pmin, const PointClass &pmax) : BoundingBoxBase(pmin, pmax) - { if (pmin(2) >= pmax(2)) BoundingBoxBase::defined = false; } + { if (pmin.z() >= pmax.z()) BoundingBoxBase::defined = false; } BoundingBox3Base(const PointClass &p1, const PointClass &p2, const PointClass &p3) : BoundingBoxBase(p1, p1) { merge(p2); merge(p3); } @@ -96,7 +96,7 @@ public: this->min = this->min.cwiseMin(vec); this->max = this->max.cwiseMax(vec); } - this->defined = (this->min(0) < this->max(0)) && (this->min(1) < this->max(1)) && (this->min(2) < this->max(2)); + this->defined = (this->min.x() < this->max.x()) && (this->min.y() < this->max.y()) && (this->min.z() < this->max.z()); } BoundingBox3Base(const std::vector &points) @@ -116,15 +116,17 @@ public: coordf_t max_size() const; bool contains(const PointClass &point) const { - return BoundingBoxBase::contains(point) && point(2) >= this->min(2) && point(2) <= this->max(2); + return BoundingBoxBase::contains(point) && point.z() >= this->min.z() && point.z() <= this->max.z(); } bool contains(const BoundingBox3Base& other) const { return contains(other.min) && contains(other.max); } + // Intersects without boundaries. bool intersects(const BoundingBox3Base& other) const { - return (this->min(0) < other.max(0)) && (this->max(0) > other.min(0)) && (this->min(1) < other.max(1)) && (this->max(1) > other.min(1)) && (this->min(2) < other.max(2)) && (this->max(2) > other.min(2)); + return this->min.x() < other.max.x() && this->max.x() > other.min.x() && this->min.y() < other.max.y() && this->max.y() > other.min.y() && + this->min.z() < other.max.z() && this->max.z() > other.min.z(); } }; @@ -212,13 +214,13 @@ public: template inline bool empty(const BoundingBoxBase &bb) { - return ! bb.defined || bb.min(0) >= bb.max(0) || bb.min(1) >= bb.max(1); + return ! bb.defined || bb.min.x() >= bb.max.x() || bb.min.y() >= bb.max.y(); } template inline bool empty(const BoundingBox3Base &bb) { - return ! bb.defined || bb.min(0) >= bb.max(0) || bb.min(1) >= bb.max(1) || bb.min(2) >= bb.max(2); + return ! bb.defined || bb.min.x() >= bb.max.x() || bb.min.y() >= bb.max.y() || bb.min.z() >= bb.max.z(); } inline BoundingBox scaled(const BoundingBoxf &bb) { return {scaled(bb.min), scaled(bb.max)}; } diff --git a/src/libslic3r/Point.hpp b/src/libslic3r/Point.hpp index c6dfb1223..77d99d14a 100644 --- a/src/libslic3r/Point.hpp +++ b/src/libslic3r/Point.hpp @@ -5,6 +5,7 @@ #include #include #include +#include // for hash #include #include #include @@ -280,7 +281,7 @@ namespace int128 { // To be used by std::unordered_map, std::unordered_multimap and friends. struct PointHash { - size_t operator()(const Vec2crd &pt) const { + size_t operator()(const Vec2crd &pt) const noexcept { return coord_t((89 * 31 + int64_t(pt.x())) * 31 + pt.y()); } }; @@ -511,12 +512,13 @@ inline Point align_to_grid(Point coord, Point spacing, Point base) } // namespace Slic3r +/* namespace std { - template <> struct hash { - size_t operator()(const Slic3r::Point &p) const - { return (89 * 31 + p.x()) * 31 + p.y(); } + template<> struct hash { + size_t operator()(const Slic3r::Point& p) const noexcept { return Slic3r::PointHash{}(p); } }; } +*/ // start Boost #include diff --git a/src/libslic3r/Polyline.hpp b/src/libslic3r/Polyline.hpp index 5282f6c77..547664d4f 100644 --- a/src/libslic3r/Polyline.hpp +++ b/src/libslic3r/Polyline.hpp @@ -61,6 +61,9 @@ public: } } + Point& operator[](Points::size_type idx) { return this->points[idx]; } + const Point& operator[](Points::size_type idx) const { return this->points[idx]; } + const Point& last_point() const override { return this->points.back(); } const Point& leftmost_point() const; Lines lines() const override;