From 1ba3cc76eb9a03649c5fe168b159f4711bc725fa Mon Sep 17 00:00:00 2001 From: Florens Wasserfall Date: Tue, 17 Nov 2020 16:35:49 +0100 Subject: [PATCH] 3D support in distance_to to fix broken Douglas-Peucker algorithm in nonplanar gcode export --- xs/src/libslic3r/MultiPoint.cpp | 7 ------- xs/src/libslic3r/Point.cpp | 8 +++++--- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/xs/src/libslic3r/MultiPoint.cpp b/xs/src/libslic3r/MultiPoint.cpp index 05afd23cec..a9607cd37c 100644 --- a/xs/src/libslic3r/MultiPoint.cpp +++ b/xs/src/libslic3r/MultiPoint.cpp @@ -178,13 +178,6 @@ MultiPoint::_douglas_peucker(const Points &points, const double tolerance) { assert(points.size() >= 2); Points results; - //TODO fix segfault in this function - //Temporary - for (Point p : points) { - results.push_back(p); - } - return results; - ///Temporary double dmax = 0; size_t index = 0; diff --git a/xs/src/libslic3r/Point.cpp b/xs/src/libslic3r/Point.cpp index 2fbcd0e58a..8b9c8b0a59 100644 --- a/xs/src/libslic3r/Point.cpp +++ b/xs/src/libslic3r/Point.cpp @@ -198,19 +198,21 @@ Point::distance_to(const Line &line) const { const double dx = line.b.x - line.a.x; const double dy = line.b.y - line.a.y; + const double dz = line.b.z - line.a.z; - const double l2 = dx*dx + dy*dy; // avoid a sqrt + const double l2 = dx*dx + dy*dy + dz*dz; // avoid a sqrt if (l2 == 0.0) return this->distance_to(line.a); // line.a == line.b case // Consider the line extending the segment, parameterized as line.a + t (line.b - line.a). // We find projection of this point onto the line. // It falls where t = [(this-line.a) . (line.b-line.a)] / |line.b-line.a|^2 - const double t = ((this->x - line.a.x) * dx + (this->y - line.a.y) * dy) / l2; + const double t = ((this->x - line.a.x) * dx + (this->y - line.a.y) * dy + (this->z - line.a.z) * dz) / l2; if (t < 0.0) return this->distance_to(line.a); // beyond the 'a' end of the segment else if (t > 1.0) return this->distance_to(line.b); // beyond the 'b' end of the segment Point projection( line.a.x + t * dx, - line.a.y + t * dy + line.a.y + t * dy, + line.a.z + t * dz ); return this->distance_to(projection); }