From 3037b42b4712301a7a7b774d7c20870d6db8fdcf Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Thu, 11 Jul 2013 14:13:30 +0200 Subject: [PATCH] New ->translate() method for ExPolygon::XS --- xs/src/ExPolygon.hpp | 16 ++++++++++++++++ xs/t/04_expolygon.t | 15 +++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/xs/src/ExPolygon.hpp b/xs/src/ExPolygon.hpp index 2814cc1af..c08b322c9 100644 --- a/xs/src/ExPolygon.hpp +++ b/xs/src/ExPolygon.hpp @@ -20,6 +20,7 @@ class ExPolygon Polygons holes; SV* arrayref(); void scale(double factor); + void translate(double x, double y); }; #define scale_polygon(poly, factor) \ @@ -28,6 +29,12 @@ class ExPolygon (*pit).y *= factor; \ } +#define translate_polygon(poly, x, y) \ + for (Polygon::iterator pit = (poly).begin(); pit != (poly).end(); ++pit) { \ + (*pit).x += x; \ + (*pit).y += y; \ + } + void ExPolygon::scale(double factor) { @@ -37,6 +44,15 @@ ExPolygon::scale(double factor) } } +void +ExPolygon::translate(double x, double y) +{ + translate_polygon(contour, x, y); + for (Polygons::iterator it = holes.begin(); it != holes.end(); ++it) { + translate_polygon(*it, x, y); + } +} + void perl2polygon(SV* poly_sv, Polygon& poly) { diff --git a/xs/t/04_expolygon.t b/xs/t/04_expolygon.t index 0bb50dc57..476942c77 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 => 6; +use Test::More tests => 7; my $square = [ # ccw [100, 100], @@ -30,10 +30,17 @@ 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); +$expolygon->scale(2.5); is_deeply [ @$expolygon ], [ - [map [ 1.5*$_->[0], 1.5*$_->[1] ], @$square], - [map [ 1.5*$_->[0], 1.5*$_->[1] ], @$hole_in_square] + [map [ 2.5*$_->[0], 2.5*$_->[1] ], @$square], + [map [ 2.5*$_->[0], 2.5*$_->[1] ], @$hole_in_square] ], 'scale'; +$expolygon->scale(1/2.5); +$expolygon->translate(10, -5); +is_deeply [ @$expolygon ], [ + [map [ $_->[0]+10, $_->[1]-5 ], @$square], + [map [ $_->[0]+10, $_->[1]-5 ], @$hole_in_square] + ], 'translate'; + __END__