From 132d170f73524d0e347a924393e4431e163e988e Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Fri, 22 Nov 2013 16:01:50 +0100 Subject: [PATCH] Ported simplify() to XS and removed dependency on Boost::Geometry::Utils --- Build.PL | 1 - lib/Slic3r.pm | 1 - lib/Slic3r/ExPolygon.pm | 1 - xs/src/MultiPoint.cpp | 4 ++-- xs/src/MultiPoint.hpp | 2 +- xs/src/Point.cpp | 4 ++-- xs/src/SurfaceCollection.cpp | 14 +++++++++++--- xs/t/09_polyline.t | 10 +++++++++- 8 files changed, 25 insertions(+), 12 deletions(-) diff --git a/Build.PL b/Build.PL index 23d25db4f..5f2340d34 100644 --- a/Build.PL +++ b/Build.PL @@ -7,7 +7,6 @@ use Config; use File::Spec; my %prereqs = qw( - Boost::Geometry::Utils 0.15 Encode::Locale 0 ExtUtils::MakeMaker 6.80 ExtUtils::ParseXS 3.22 diff --git a/lib/Slic3r.pm b/lib/Slic3r.pm index d317f2e67..d6142f4a6 100644 --- a/lib/Slic3r.pm +++ b/lib/Slic3r.pm @@ -33,7 +33,6 @@ our $var = "$FindBin::Bin/var"; use Encode; use Encode::Locale; -use Boost::Geometry::Utils 0.15; use Moo 1.003001; use Slic3r::XS; # import all symbols (constants etc.) before they get parsed diff --git a/lib/Slic3r/ExPolygon.pm b/lib/Slic3r/ExPolygon.pm index 74d1cfee7..536ea3f94 100644 --- a/lib/Slic3r/ExPolygon.pm +++ b/lib/Slic3r/ExPolygon.pm @@ -4,7 +4,6 @@ use warnings; # an ExPolygon is a polygon with holes -use Boost::Geometry::Utils; use List::Util qw(first); use Math::Geometry::Voronoi; use Slic3r::Geometry qw(X Y A B point_in_polygon epsilon scaled_epsilon); diff --git a/xs/src/MultiPoint.cpp b/xs/src/MultiPoint.cpp index b28912321..c31b23382 100644 --- a/xs/src/MultiPoint.cpp +++ b/xs/src/MultiPoint.cpp @@ -56,11 +56,11 @@ MultiPoint::is_valid() const } Points -MultiPoint::_douglas_peucker(Points &points, double tolerance) +MultiPoint::_douglas_peucker(const Points &points, const double tolerance) { Points results; double dmax = 0; - int index = 0; + size_t index = 0; Line full(points.front(), points.back()); for (Points::const_iterator it = points.begin() + 1; it != points.end(); ++it) { double d = it->distance_to(full); diff --git a/xs/src/MultiPoint.hpp b/xs/src/MultiPoint.hpp index 9abe29e9e..ba06e042a 100644 --- a/xs/src/MultiPoint.hpp +++ b/xs/src/MultiPoint.hpp @@ -21,7 +21,7 @@ class MultiPoint virtual Lines lines() const = 0; double length() const; bool is_valid() const; - static Points _douglas_peucker(Points &points, double tolerance); + static Points _douglas_peucker(const Points &points, const double tolerance); #ifdef SLIC3RXS void from_SV(SV* poly_sv); diff --git a/xs/src/Point.cpp b/xs/src/Point.cpp index cef2bfd90..0c85a309c 100644 --- a/xs/src/Point.cpp +++ b/xs/src/Point.cpp @@ -1,6 +1,6 @@ #include "Point.hpp" #include "Line.hpp" -#include +#include namespace Slic3r { @@ -87,7 +87,7 @@ Point::distance_to(const Line &line) const double n = (line.b.x - line.a.x) * (line.a.y - this->y) - (line.a.x - this->x) * (line.b.y - line.a.y); - return abs(n) / line.length(); + return std::abs(n) / line.length(); } #ifdef SLIC3RXS diff --git a/xs/src/SurfaceCollection.cpp b/xs/src/SurfaceCollection.cpp index ab8661b85..b65c80622 100644 --- a/xs/src/SurfaceCollection.cpp +++ b/xs/src/SurfaceCollection.cpp @@ -3,11 +3,19 @@ namespace Slic3r { void -simplify(double tolerance) +SurfaceCollection::simplify(double tolerance) { - for (Surfaces::iterator it = this->surfaces.begin(); it != this->surfaces.end(); ++it) { - throw "Unimplemented"; + Surfaces ss; + for (Surfaces::const_iterator it_s = this->surfaces.begin(); it_s != this->surfaces.end(); ++it_s) { + ExPolygons expp; + it_s->expolygon.simplify(tolerance, expp); + for (ExPolygons::const_iterator it_e = expp.begin(); it_e != expp.end(); ++it_e) { + Surface s = *it_s; + s.expolygon = *it_e; + ss.push_back(s); + } } + this->surfaces = ss; } } diff --git a/xs/t/09_polyline.t b/xs/t/09_polyline.t index d18f6fbc7..6d39273e9 100644 --- a/xs/t/09_polyline.t +++ b/xs/t/09_polyline.t @@ -4,7 +4,7 @@ use strict; use warnings; use Slic3r::XS; -use Test::More tests => 6; +use Test::More tests => 7; my $points = [ [100, 100], @@ -34,4 +34,12 @@ is_deeply $polyline->pp, [ @$points, @$points ], 'append_polyline'; ok abs($polyline->length - ($len-($len/3))) < 1, 'clip_end'; } +{ + my $polyline = Slic3r::Polyline->new( + [0,0], [50,50], [100,0], [125,-25], [150,50], + ); + $polyline->simplify(25); + is_deeply $polyline->pp, [ [0, 0], [50, 50], [125, -25], [150, 50] ], 'Douglas-Peucker'; +} + __END__