From 1f734807b988e515d05f5ef0e2b259f4e665e40a Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Mon, 26 Aug 2013 22:39:35 +0200 Subject: [PATCH] Ported Polygon->split_at() --- lib/Slic3r/Polygon.pm | 17 ----------------- xs/src/Point.cpp | 2 +- xs/src/Point.hpp | 2 +- xs/src/Polygon.cpp | 12 ++++++++++++ xs/src/Polygon.hpp | 1 + xs/t/06_polygon.t | 3 ++- xs/xsp/Polygon.xsp | 2 ++ 7 files changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/Slic3r/Polygon.pm b/lib/Slic3r/Polygon.pm index b540caf8b..289c2749a 100644 --- a/lib/Slic3r/Polygon.pm +++ b/lib/Slic3r/Polygon.pm @@ -96,23 +96,6 @@ sub is_valid { return @$self >= 3; } -sub split_at { - my $self = shift; - my ($point) = @_; - - # find index of point - my $i = -1; - for (my $n = 0; $n <= $#$self; $n++) { - if (Slic3r::Geometry::same_point($point, $self->[$n])) { - $i = $n; - last; - } - } - die "Point not found" if $i == -1; - - return $self->split_at_index($i); -} - # for cw polygons this will return convex points! sub concave_points { my $self = shift; diff --git a/xs/src/Point.cpp b/xs/src/Point.cpp index 874eeeb1d..15d37177f 100644 --- a/xs/src/Point.cpp +++ b/xs/src/Point.cpp @@ -26,7 +26,7 @@ Point::rotate(double angle, Point* center) } bool -Point::coincides_with(Point* point) +Point::coincides_with(const Point* point) const { return this->x == point->x && this->y == point->y; } diff --git a/xs/src/Point.hpp b/xs/src/Point.hpp index 71b1476e6..bb23b2947 100644 --- a/xs/src/Point.hpp +++ b/xs/src/Point.hpp @@ -19,7 +19,7 @@ class Point void scale(double factor); void translate(double x, double y); void rotate(double angle, Point* center); - bool coincides_with(Point* point); + bool coincides_with(const Point* point) const; }; typedef std::vector Points; diff --git a/xs/src/Polygon.cpp b/xs/src/Polygon.cpp index 8f6d51fd5..dcb07e4ca 100644 --- a/xs/src/Polygon.cpp +++ b/xs/src/Polygon.cpp @@ -23,6 +23,18 @@ Polygon::lines() return lines; } +Polyline* +Polygon::split_at(const Point* point) +{ + // find index of point + for (Points::const_iterator it = this->points.begin(); it != this->points.end(); ++it) { + if ((*it).coincides_with(point)) { + return this->split_at_index(it - this->points.begin()); + } + } + throw "Point not found"; +} + Polyline* Polygon::split_at_index(int index) { diff --git a/xs/src/Polygon.hpp b/xs/src/Polygon.hpp index 37754af08..bb9d2f077 100644 --- a/xs/src/Polygon.hpp +++ b/xs/src/Polygon.hpp @@ -13,6 +13,7 @@ class Polygon : public MultiPoint { public: SV* to_SV_ref(); Lines lines(); + Polyline* split_at(const Point* point); Polyline* split_at_index(int index); Polyline* split_at_first_point(); bool is_counter_clockwise(); diff --git a/xs/t/06_polygon.t b/xs/t/06_polygon.t index 76e4c1554..b93ae24ec 100644 --- a/xs/t/06_polygon.t +++ b/xs/t/06_polygon.t @@ -4,7 +4,7 @@ use strict; use warnings; use Slic3r::XS; -use Test::More tests => 10; +use Test::More tests => 11; my $square = [ # ccw [100, 100], @@ -29,6 +29,7 @@ is_deeply [ map $_->pp, @$lines ], [ is_deeply $polygon->split_at_first_point->pp, [ @$square[0,1,2,3,0] ], 'split_at_first_point'; is_deeply $polygon->split_at_index(2)->pp, [ @$square[2,3,0,1,2] ], 'split_at_index'; +is_deeply $polygon->split_at(Slic3r::Point->new(@{$square->[2]}))->pp, [ @$square[2,3,0,1,2] ], 'split_at'; ok $polygon->is_counter_clockwise, 'is_counter_clockwise'; { diff --git a/xs/xsp/Polygon.xsp b/xs/xsp/Polygon.xsp index 7e96af24d..e425c80a8 100644 --- a/xs/xsp/Polygon.xsp +++ b/xs/xsp/Polygon.xsp @@ -17,6 +17,8 @@ void translate(double x, double y); void reverse(); Lines lines(); + Polyline* split_at(Point* point) + %code{% const char* CLASS = "Slic3r::Polyline"; RETVAL = THIS->split_at(point); %}; Polyline* split_at_index(int index) %code{% const char* CLASS = "Slic3r::Polyline"; RETVAL = THIS->split_at_index(index); %}; Polyline* split_at_first_point()