Skip to content

Commit

Permalink
Merge pull request #33 from platsch/nonplanar-master-merge
Browse files Browse the repository at this point in the history
3D support in distance_to to fix broken Douglas-Peucker algorithm
  • Loading branch information
Zip-o-mat authored Nov 17, 2020
2 parents 4b9b58d + 1ba3cc7 commit bcbcf88
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
7 changes: 0 additions & 7 deletions xs/src/libslic3r/MultiPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions xs/src/libslic3r/Point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit bcbcf88

Please sign in to comment.