From e2b1b526792e2c2525521c9d28ccb77c0d26fbdd Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Fri, 30 Jan 2015 18:33:20 +0100 Subject: [PATCH] Added a new Slic3r::Geometry::simplify_polygons() function --- xs/src/libslic3r/ExPolygon.cpp | 14 ++++++++++---- xs/src/libslic3r/Geometry.cpp | 15 +++++++++++++++ xs/src/libslic3r/Geometry.hpp | 1 + xs/src/libslic3r/Polygon.cpp | 1 + xs/xsp/Geometry.xsp | 9 +++++++++ 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/xs/src/libslic3r/ExPolygon.cpp b/xs/src/libslic3r/ExPolygon.cpp index 9a4215864..a03447bba 100644 --- a/xs/src/libslic3r/ExPolygon.cpp +++ b/xs/src/libslic3r/ExPolygon.cpp @@ -129,14 +129,20 @@ ExPolygon::simplify_p(double tolerance) const pp.reserve(this->holes.size() + 1); // contour - Polygon p = this->contour; - p.points = MultiPoint::_douglas_peucker(p.points, tolerance); - pp.push_back(p); + { + Polygon p = this->contour; + p.points.push_back(p.points.front()); + p.points = MultiPoint::_douglas_peucker(p.points, tolerance); + p.points.pop_back(); + pp.push_back(p); + } // holes for (Polygons::const_iterator it = this->holes.begin(); it != this->holes.end(); ++it) { - p = *it; + Polygon p = *it; + p.points.push_back(p.points.front()); p.points = MultiPoint::_douglas_peucker(p.points, tolerance); + p.points.pop_back(); pp.push_back(p); } simplify_polygons(pp, &pp); diff --git a/xs/src/libslic3r/Geometry.cpp b/xs/src/libslic3r/Geometry.cpp index 8169da5a3..3dc11c0cb 100644 --- a/xs/src/libslic3r/Geometry.cpp +++ b/xs/src/libslic3r/Geometry.cpp @@ -1,4 +1,5 @@ #include "Geometry.hpp" +#include "ClipperUtils.hpp" #include "ExPolygon.hpp" #include "Line.hpp" #include "PolylineCollection.hpp" @@ -146,6 +147,20 @@ deg2rad(double angle) return PI * angle / 180.0; } +void +simplify_polygons(const Polygons &polygons, double tolerance, Polygons* retval) +{ + Polygons pp; + for (Polygons::const_iterator it = polygons.begin(); it != polygons.end(); ++it) { + Polygon p = *it; + p.points.push_back(p.points.front()); + p.points = MultiPoint::_douglas_peucker(p.points, tolerance); + p.points.pop_back(); + pp.push_back(p); + } + Slic3r::simplify_polygons(pp, retval); +} + Line MedialAxis::edge_to_line(const VD::edge_type &edge) const { diff --git a/xs/src/libslic3r/Geometry.hpp b/xs/src/libslic3r/Geometry.hpp index a402e0251..c2cacfa33 100644 --- a/xs/src/libslic3r/Geometry.hpp +++ b/xs/src/libslic3r/Geometry.hpp @@ -21,6 +21,7 @@ template bool contains(const std::vector &vector, const Point &point double rad2deg(double angle); double rad2deg_dir(double angle); double deg2rad(double angle); +void simplify_polygons(const Polygons &polygons, double tolerance, Polygons* retval); class MedialAxis { public: diff --git a/xs/src/libslic3r/Polygon.cpp b/xs/src/libslic3r/Polygon.cpp index 80287e663..c6fb91131 100644 --- a/xs/src/libslic3r/Polygon.cpp +++ b/xs/src/libslic3r/Polygon.cpp @@ -147,6 +147,7 @@ Polygon::contains(const Point &point) const return result; } +// this only works on CCW polygons as CW will be ripped out by Clipper's simplify_polygons() Polygons Polygon::simplify(double tolerance) const { diff --git a/xs/xsp/Geometry.xsp b/xs/xsp/Geometry.xsp index 5a3be4c9e..0df74a4bd 100644 --- a/xs/xsp/Geometry.xsp +++ b/xs/xsp/Geometry.xsp @@ -78,4 +78,13 @@ deg2rad(angle) OUTPUT: RETVAL +Polygons +simplify_polygons(polygons, tolerance) + Polygons polygons + double tolerance + CODE: + Slic3r::Geometry::simplify_polygons(polygons, tolerance, &RETVAL); + OUTPUT: + RETVAL + %}