From d392858ee35ef993180c7c23dac610e691baf8b4 Mon Sep 17 00:00:00 2001 From: bubnikv Date: Sun, 10 Apr 2016 19:32:39 +0200 Subject: [PATCH] The chaining and leftmost_point methods were rewritten as static methods, so they may be called on Polylines without having to convert to PolylineCollection first. --- xs/src/libslic3r/PolylineCollection.cpp | 139 ++++++++++++++++++------ xs/src/libslic3r/PolylineCollection.hpp | 22 +++- 2 files changed, 123 insertions(+), 38 deletions(-) diff --git a/xs/src/libslic3r/PolylineCollection.cpp b/xs/src/libslic3r/PolylineCollection.cpp index f3077b0e1..136065127 100644 --- a/xs/src/libslic3r/PolylineCollection.cpp +++ b/xs/src/libslic3r/PolylineCollection.cpp @@ -2,50 +2,121 @@ namespace Slic3r { -void -PolylineCollection::chained_path(PolylineCollection* retval, bool no_reverse) const +struct Chaining { - if (this->polylines.empty()) return; - this->chained_path_from(this->polylines.front().first_point(), retval, no_reverse); -} + Point first; + Point last; + size_t idx; +}; -void -PolylineCollection::chained_path_from(Point start_near, PolylineCollection* retval, bool no_reverse) const +#ifndef sqr +template +inline sqr(T x) { return x * x; } +#endif /* sqr */ + +template +inline int nearest_point_index(const std::vector &pairs, const Point &start_near, bool no_reverse) { - Polylines my_paths = this->polylines; - - Points endpoints; - for (Polylines::const_iterator it = my_paths.begin(); it != my_paths.end(); ++it) { - endpoints.push_back(it->first_point()); - if (no_reverse) { - endpoints.push_back(it->first_point()); - } else { - endpoints.push_back(it->last_point()); + T dmin = std::numeric_limits::max(); + int idx = 0; + for (std::vector::const_iterator it = pairs.begin(); it != pairs.end(); ++it) { + T d = sqr(T(start_near.x - it->first.x)); + if (d <= dmin) { + d += sqr(T(start_near.y - it->first.y)); + if (d < dmin) { + idx = (it - pairs.begin()) * 2; + dmin = d; + if (dmin < EPSILON) + break; + } + } + if (! no_reverse) { + d = sqr(T(start_near.x - it->last.x)); + if (d <= dmin) { + d += sqr(T(start_near.y - it->last.y)); + if (d < dmin) { + idx = (it - pairs.begin()) * 2 + 1; + dmin = d; + if (dmin < EPSILON) + break; + } + } } } - - while (!my_paths.empty()) { + + return idx; +} + +Polylines PolylineCollection::chained_path_from( +#if SLIC3R_CPPVER > 11 + Polylines &&src, +#else + const Polylines &src, +#endif + Point start_near, + bool no_reverse) +{ + std::vector endpoints; + endpoints.reserve(src.size()); + for (size_t i = 0; i < src.size(); ++ i) { + Chaining c; + c.first = src[i].first_point(); + if (! no_reverse) + c.last = src[i].last_point(); + c.idx = i; + } + + Polylines retval; + while (! endpoints.empty()) { // find nearest point - int start_index = start_near.nearest_point_index(endpoints); - int path_index = start_index/2; - if (start_index % 2 && !no_reverse) { - my_paths.at(path_index).reverse(); - } - retval->polylines.push_back(my_paths.at(path_index)); - my_paths.erase(my_paths.begin() + path_index); - endpoints.erase(endpoints.begin() + 2*path_index, endpoints.begin() + 2*path_index + 2); - start_near = retval->polylines.back().last_point(); + int endpoint_index = nearest_point_index(endpoints, start_near, no_reverse); +#if SLIC3R_CPPVER > 11 + retval.push_back(std::move(src[endpoints[endpoint_index/2].idx])); +#else + retval.push_back(src[endpoints[endpoint_index/2].idx]); +#endif + if (endpoint_index & 1) + retval.back().reverse(); + endpoints.erase(endpoints.begin() + endpoint_index/2); + start_near = retval.back().last_point(); } } -Point -PolylineCollection::leftmost_point() const +#if SLIC3R_CPPVER > 11 +Polylines PolylineCollection::chained_path(Polylines &&src, bool no_reverse) { - if (this->polylines.empty()) CONFESS("leftmost_point() called on empty PolylineCollection"); - Point p = this->polylines.front().leftmost_point(); - for (Polylines::const_iterator it = this->polylines.begin() + 1; it != this->polylines.end(); ++it) { + return (src.empty() || src.front().empty()) ? + Polylines() : + chained_path_from(std::move(src), src.front().first_point(), no_reverse); +} +Polylines PolylineCollection::chained_path_from(Polylines src, Point start_near, bool no_reverse) +{ + return chained_path_from(std::move(src), start_near, no_reverse); +} +Polylines PolylineCollection::chained_path(Polylines src, bool no_reverse) +{ + return (src.empty() || src.front().empty()) ? + Polylines() : + chained_path_from(std::move(src), src.front().first_point(), no_reverse); +} +#else +Polylines PolylineCollection::chained_path(const Polylines &src, bool no_reverse) +{ + return (src.empty() || src.front().points.empty()) ? + Polylines() : + chained_path_from(src, src.front().first_point(), no_reverse); +} +#endif + +Point PolylineCollection::leftmost_point(const Polylines &polylines) +{ + if (polylines.empty()) CONFESS("leftmost_point() called on empty PolylineCollection"); + Polylines::const_iterator it = polylines.begin(); + Point p = it->leftmost_point(); + for (++ it; it != polylines.end(); ++it) { Point p2 = it->leftmost_point(); - if (p2.x < p.x) p = p2; + if (p2.x < p.x) + p = p2; } return p; } @@ -56,4 +127,4 @@ PolylineCollection::append(const Polylines &pp) this->polylines.insert(this->polylines.end(), pp.begin(), pp.end()); } -} +} // namespace Slic3r diff --git a/xs/src/libslic3r/PolylineCollection.hpp b/xs/src/libslic3r/PolylineCollection.hpp index d41e8c465..492f0318f 100644 --- a/xs/src/libslic3r/PolylineCollection.hpp +++ b/xs/src/libslic3r/PolylineCollection.hpp @@ -8,12 +8,26 @@ namespace Slic3r { class PolylineCollection { - public: +public: Polylines polylines; - void chained_path(PolylineCollection* retval, bool no_reverse = false) const; - void chained_path_from(Point start_near, PolylineCollection* retval, bool no_reverse = false) const; - Point leftmost_point() const; + void chained_path(PolylineCollection* retval, bool no_reverse = false) const + { retval->polylines = chained_path(this->polylines, no_reverse); } + void chained_path_from(Point start_near, PolylineCollection* retval, bool no_reverse = false) const + { retval->polylines = chained_path_from(this->polylines, start_near, no_reverse); } + Point leftmost_point() const + { return leftmost_point(polylines); } void append(const Polylines &polylines); + + static Point leftmost_point(const Polylines &polylines); +#if SLIC3R_CPPVER > 11 + static Polylines chained_path(Polylines &&src, bool no_reverse = false); + static Polylines chained_path_from(Polylines &&src, Point start_near, bool no_reverse = false); + static Polylines chained_path(Polylines src, bool no_reverse = false); + static Polylines chained_path_from(Polylines src, Point start_near, bool no_reverse = false); +#else + static Polylines chained_path(const Polylines &src, bool no_reverse = false); + static Polylines chained_path_from(const Polylines &src, Point start_near, bool no_reverse = false); +#endif }; }