diff --git a/xs/src/Polygon.hpp b/xs/src/Polygon.hpp index 24d5bb58e..ffd7fc37f 100644 --- a/xs/src/Polygon.hpp +++ b/xs/src/Polygon.hpp @@ -13,10 +13,24 @@ extern "C" { namespace Slic3r { -class Polygon : public MultiPoint {}; +class Polygon : public MultiPoint { + public: + Lines lines(); +}; typedef std::vector Polygons; +Lines +Polygon::lines() +{ + Lines lines; + for (int i = 0; i < this->points.size()-1; i++) { + lines.push_back(Line(this->points[i], this->points[i+1])); + } + lines.push_back(Line(this->points.back(), this->points.front())); + return lines; +} + } #endif diff --git a/xs/t/06_polygon.t b/xs/t/06_polygon.t index 566120021..f75911ec5 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 => 3; +use Test::More tests => 4; my $square = [ # ccw [100, 100], @@ -19,4 +19,12 @@ is_deeply $polygon->pp, $square, 'polygon roundtrip'; is ref($polygon->arrayref), 'ARRAY', 'polygon arrayref is unblessed'; isa_ok $polygon->[0], 'Slic3r::Point', 'polygon point is blessed'; +my $lines = $polygon->lines; +is_deeply [ map $_->pp, @$lines ], [ + [ [100, 100], [200, 100] ], + [ [200, 100], [200, 200] ], + [ [200, 200], [100, 200] ], + [ [100, 200], [100, 100] ], +], 'polygon lines'; + __END__ diff --git a/xs/xsp/Polygon.xsp b/xs/xsp/Polygon.xsp index a1f56ee29..f16e8da59 100644 --- a/xs/xsp/Polygon.xsp +++ b/xs/xsp/Polygon.xsp @@ -15,6 +15,7 @@ %code{% RETVAL = THIS->to_SV_pureperl(); %}; void scale(double factor); void translate(double x, double y); + Lines lines(); %{ Polygon*