ExPolygon::XS->rotate()

This commit is contained in:
Alessandro Ranellucci 2013-07-11 18:55:51 +02:00
parent 1506907212
commit 87a5de193d
7 changed files with 86 additions and 19 deletions

View File

@ -329,10 +329,7 @@ sub scale {
sub rotate { sub rotate {
my $self = shift; my $self = shift;
my ($angle, $center) = @_; $_->rotate(@_) for @{$self->expolygons};
$center = Slic3r::Point::XS->new(@$center) if ref($center) ne 'Slic3r::Point::XS';
$_->rotate($angle, $center) for @{$self->expolygons};
$self; $self;
} }

View File

@ -17,4 +17,13 @@ use overload
sub clone { (ref $_[0])->_clone($_[0]) } sub clone { (ref $_[0])->_clone($_[0]) }
# to handle legacy code
sub rotate {
my $self = shift;
my ($angle, $center) = @_;
$center = Slic3r::Point::XS->new(@$center) if ref($center) ne 'Slic3r::Point::XS';
$self->_rotate($angle, $center);
}
1; 1;

View File

@ -21,6 +21,7 @@ class ExPolygon
SV* arrayref(); SV* arrayref();
void scale(double factor); void scale(double factor);
void translate(double x, double y); void translate(double x, double y);
void _rotate(double angle, Point* center);
}; };
#define scale_polygon(poly, factor) \ #define scale_polygon(poly, factor) \
@ -35,6 +36,14 @@ class ExPolygon
(*pit).y += y; \ (*pit).y += y; \
} }
inline void
rotate_polygon(Polygon* poly, double angle, Point* center)
{
for (Polygon::iterator pit = (*poly).begin(); pit != (*poly).end(); ++pit) { \
(*pit).rotate(angle, center);
}
}
void void
ExPolygon::scale(double factor) ExPolygon::scale(double factor)
{ {
@ -53,6 +62,15 @@ ExPolygon::translate(double x, double y)
} }
} }
void
ExPolygon::_rotate(double angle, Point* center)
{
rotate_polygon(&contour, angle, center);
for (Polygons::iterator it = holes.begin(); it != holes.end(); ++it) {
rotate_polygon(&*it, angle, center);
}
}
void void
perl2polygon(SV* poly_sv, Polygon& poly) perl2polygon(SV* poly_sv, Polygon& poly)
{ {

View File

@ -8,14 +8,26 @@ extern "C" {
#include "ppport.h" #include "ppport.h"
} }
#include <math.h>
class Point class Point
{ {
public: public:
unsigned long x; unsigned long x;
unsigned long y; unsigned long y;
Point(unsigned long _x = 0, unsigned long _y = 0): x(_x), y(_y) {}; Point(unsigned long _x = 0, unsigned long _y = 0): x(_x), y(_y) {};
void rotate(double angle, Point* center);
}; };
void
Point::rotate(double angle, Point* center)
{
double cur_x = (double)x;
double cur_y = (double)y;
x = (unsigned long)( (double)center->x + cos(angle) * (cur_x - (double)center->x) - sin(angle) * (cur_y - (double)center->y) );
y = (unsigned long)( (double)center->y + cos(angle) * (cur_y - (double)center->y) + sin(angle) * (cur_x - (double)center->x) );
}
SV* SV*
point2perl(Point& point) { point2perl(Point& point) {
AV* av = newAV(); AV* av = newAV();

View File

@ -4,7 +4,9 @@ use strict;
use warnings; use warnings;
use Slic3r::XS; use Slic3r::XS;
use Test::More tests => 7; use Test::More tests => 9;
use constant PI => 4 * atan2(1, 1);
my $square = [ # ccw my $square = [ # ccw
[100, 100], [100, 100],
@ -26,21 +28,47 @@ isa_ok $expolygon->arrayref, 'Slic3r::ExPolygon', 'Perl expolygon is blessed';
isa_ok $expolygon->[0], 'Slic3r::Polygon', 'Perl polygons are blessed'; isa_ok $expolygon->[0], 'Slic3r::Polygon', 'Perl polygons are blessed';
isa_ok $expolygon->[0][0], 'Slic3r::Point', 'Perl polygon points are blessed'; isa_ok $expolygon->[0][0], 'Slic3r::Point', 'Perl polygon points are blessed';
my $clone = $expolygon->clone; {
is_deeply [ @$clone ], [$square, $hole_in_square], 'clone'; my $clone = $expolygon->clone;
# TODO: check that modifying the clone doesn't modify the original one is_deeply [ @$clone ], [$square, $hole_in_square], 'clone';
# The following tests implicitely check that modifying clones
# does not modify the original one.
}
$expolygon->scale(2.5); {
is_deeply [ @$expolygon ], [ my $expolygon2 = $expolygon->clone;
[map [ 2.5*$_->[0], 2.5*$_->[1] ], @$square], $expolygon2->scale(2.5);
[map [ 2.5*$_->[0], 2.5*$_->[1] ], @$hole_in_square] is_deeply [ @$expolygon2 ], [
], 'scale'; [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); my $expolygon2 = $expolygon->clone;
is_deeply [ @$expolygon ], [ $expolygon2->translate(10, -5);
[map [ $_->[0]+10, $_->[1]-5 ], @$square], is_deeply [ @$expolygon2 ], [
[map [ $_->[0]+10, $_->[1]-5 ], @$hole_in_square] [map [ $_->[0]+10, $_->[1]-5 ], @$square],
], 'translate'; [map [ $_->[0]+10, $_->[1]-5 ], @$hole_in_square]
], 'translate';
}
{
my $expolygon2 = $expolygon->clone;
$expolygon2->rotate(PI/2, Slic3r::Point::XS->new(150,150));
is_deeply [ @$expolygon2 ], [
[ @$square[1,2,3,0] ],
[ @$hole_in_square[3,0,1,2] ]
], 'rotate around Point::XS';
}
{
my $expolygon2 = $expolygon->clone;
$expolygon2->rotate(PI/2, [150,150]);
is_deeply [ @$expolygon2 ], [
[ @$square[1,2,3,0] ],
[ @$hole_in_square[3,0,1,2] ]
], 'rotate around pure-Perl Point';
}
__END__ __END__

View File

@ -9,6 +9,8 @@
%name{_clone} ExPolygon(ExPolygon& self); %name{_clone} ExPolygon(ExPolygon& self);
~ExPolygon(); ~ExPolygon();
void scale(double factor); void scale(double factor);
void translate(double x, double y);
void _rotate(double angle, Point* center);
%{ %{
ExPolygon* ExPolygon*

View File

@ -2,3 +2,4 @@
%typemap{std::vector<unsigned int>*}; %typemap{std::vector<unsigned int>*};
%typemap{SV*}; %typemap{SV*};
%typemap{AV*}; %typemap{AV*};
%typemap{Point*};