diff --git a/xs/src/ExPolygon.hpp b/xs/src/ExPolygon.hpp index 497843dd8..2814cc1af 100644 --- a/xs/src/ExPolygon.hpp +++ b/xs/src/ExPolygon.hpp @@ -19,8 +19,24 @@ class ExPolygon Polygon contour; Polygons holes; SV* arrayref(); + void scale(double factor); }; +#define scale_polygon(poly, factor) \ + for (Polygon::iterator pit = (poly).begin(); pit != (poly).end(); ++pit) { \ + (*pit).x *= factor; \ + (*pit).y *= factor; \ + } + +void +ExPolygon::scale(double factor) +{ + scale_polygon(contour, factor); + for (Polygons::iterator it = holes.begin(); it != holes.end(); ++it) { + scale_polygon(*it, factor); + } +} + void perl2polygon(SV* poly_sv, Polygon& poly) { diff --git a/xs/t/04_expolygon.t b/xs/t/04_expolygon.t index 642d13b5f..0bb50dc57 100644 --- a/xs/t/04_expolygon.t +++ b/xs/t/04_expolygon.t @@ -4,7 +4,7 @@ use strict; use warnings; use Slic3r::XS; -use Test::More tests => 5; +use Test::More tests => 6; my $square = [ # ccw [100, 100], @@ -30,4 +30,10 @@ my $clone = $expolygon->clone; is_deeply [ @$clone ], [$square, $hole_in_square], 'clone'; # TODO: check that modifying the clone doesn't modify the original one +$expolygon->scale(1.5); +is_deeply [ @$expolygon ], [ + [map [ 1.5*$_->[0], 1.5*$_->[1] ], @$square], + [map [ 1.5*$_->[0], 1.5*$_->[1] ], @$hole_in_square] + ], 'scale'; + __END__ diff --git a/xs/xsp/ExPolygon.xsp b/xs/xsp/ExPolygon.xsp index 62c421cce..0af2dc6e4 100644 --- a/xs/xsp/ExPolygon.xsp +++ b/xs/xsp/ExPolygon.xsp @@ -8,6 +8,7 @@ %name{Slic3r::ExPolygon::XS} class ExPolygon { %name{_clone} ExPolygon(ExPolygon& self); ~ExPolygon(); + void scale(double factor); %{ ExPolygon*