From 0d07a2e4e6d52098961f080631c1b851041d1541 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Mon, 15 Jul 2013 23:38:06 +0200 Subject: [PATCH] Implemented split_at_index() and split_at_first_point() for Polygon --- xs/src/Polygon.hpp | 21 +++++++++++++++++++++ xs/t/06_polygon.t | 5 ++++- xs/xsp/Polygon.xsp | 4 ++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/xs/src/Polygon.hpp b/xs/src/Polygon.hpp index ffd7fc37f..bb5a1440f 100644 --- a/xs/src/Polygon.hpp +++ b/xs/src/Polygon.hpp @@ -16,6 +16,8 @@ namespace Slic3r { class Polygon : public MultiPoint { public: Lines lines(); + Polyline* split_at_index(int index); + Polyline* split_at_first_point(); }; typedef std::vector Polygons; @@ -31,6 +33,25 @@ Polygon::lines() return lines; } +Polyline* +Polygon::split_at_index(int index) +{ + Polyline* poly = new Polyline; + for (int i = index; i < this->points.size(); i++) { + poly->points.push_back( this->points[i] ); + } + for (int i = 0; i < index; i++) { + poly->points.push_back( this->points[i] ); + } + return poly; +} + +Polyline* +Polygon::split_at_first_point() +{ + return this->split_at_index(0); +} + } #endif diff --git a/xs/t/06_polygon.t b/xs/t/06_polygon.t index f75911ec5..473eaae0e 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 => 4; +use Test::More tests => 6; my $square = [ # ccw [100, 100], @@ -27,4 +27,7 @@ is_deeply [ map $_->pp, @$lines ], [ [ [100, 200], [100, 100] ], ], 'polygon lines'; +is_deeply $polygon->split_at_first_point->pp, $square, 'split_at_first_point'; +is_deeply $polygon->split_at_index(2)->pp, [ @$square[2,3,0,1] ], 'split_at_index'; + __END__ diff --git a/xs/xsp/Polygon.xsp b/xs/xsp/Polygon.xsp index f16e8da59..69ad45cb9 100644 --- a/xs/xsp/Polygon.xsp +++ b/xs/xsp/Polygon.xsp @@ -16,6 +16,10 @@ void scale(double factor); void translate(double x, double y); Lines lines(); + Polyline* split_at_index(int index) + %code{% const char* CLASS = "Slic3r::Polyline"; RETVAL = THIS->split_at_index(index); %}; + Polyline* split_at_first_point() + %code{% const char* CLASS = "Slic3r::Polyline"; RETVAL = THIS->split_at_first_point(); %}; %{ Polygon*