XS interface completed, including new Line class

This commit is contained in:
Alessandro Ranellucci 2013-07-15 22:57:22 +02:00
parent 9af2a1c007
commit ab6b3d41a7
33 changed files with 435 additions and 257 deletions

View file

@ -4,7 +4,7 @@ use strict;
use warnings;
use Slic3r::XS;
use Test::More tests => 15;
use Test::More tests => 17;
use constant PI => 4 * atan2(1, 1);
@ -21,28 +21,29 @@ my $hole_in_square = [ # cw
[160, 140],
];
my $expolygon = Slic3r::ExPolygon::XS->new($square, $hole_in_square);
is_deeply [ @{$expolygon->arrayref_pp} ], [$square, $hole_in_square], 'expolygon roundtrip';
my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square);
my $arrayref = $expolygon->arrayref;
isa_ok $arrayref, 'Slic3r::ExPolygon', 'Perl expolygon is blessed';
is ref($expolygon->pp), 'ARRAY', 'expolygon pp is unblessed';
is_deeply $expolygon->pp, [$square, $hole_in_square], 'expolygon roundtrip';
my $arrayref_pp = $expolygon->arrayref_pp;
isa_ok $arrayref_pp, 'Slic3r::ExPolygon', 'Perl expolygon is blessed';
isa_ok $arrayref_pp->[0], 'Slic3r::Polygon', 'Perl polygons are blessed';
isnt ref($arrayref_pp->[0][0]), 'Slic3r::Point', 'Perl polygon points are not blessed';
is ref($expolygon->arrayref), 'ARRAY', 'expolygon arrayref is unblessed';
isa_ok $expolygon->[0], 'Slic3r::Polygon', 'expolygon polygon is blessed';
isa_ok $expolygon->[0][0], 'Slic3r::Point', 'expolygon point is blessed';
{
my $clone = $expolygon->clone;
is_deeply [ @{$clone->arrayref_pp} ], [$square, $hole_in_square], 'clone';
# The following tests implicitely check that modifying clones
# does not modify the original one.
my $polygon = $expolygon->[0];
$polygon->scale(2);
isnt $expolygon->[0][0][0], $polygon->[0][0], 'a copy of polygons is returned';
}
is_deeply $expolygon->clone->pp, [$square, $hole_in_square], 'clone';
# The following tests implicitely check that modifying clones
# does not modify the original one.
{
my $expolygon2 = $expolygon->clone;
$expolygon2->scale(2.5);
is_deeply [ @{$expolygon2->arrayref_pp} ], [
is_deeply $expolygon2->pp, [
[map [ 2.5*$_->[0], 2.5*$_->[1] ], @$square],
[map [ 2.5*$_->[0], 2.5*$_->[1] ], @$hole_in_square]
], 'scale';
@ -51,7 +52,7 @@ isnt ref($arrayref_pp->[0][0]), 'Slic3r::Point', 'Perl polygon points are not bl
{
my $expolygon2 = $expolygon->clone;
$expolygon2->translate(10, -5);
is_deeply [ @{$expolygon2->arrayref_pp} ], [
is_deeply $expolygon2->pp, [
[map [ $_->[0]+10, $_->[1]-5 ], @$square],
[map [ $_->[0]+10, $_->[1]-5 ], @$hole_in_square]
], 'translate';
@ -60,7 +61,7 @@ isnt ref($arrayref_pp->[0][0]), 'Slic3r::Point', 'Perl polygon points are not bl
{
my $expolygon2 = $expolygon->clone;
$expolygon2->rotate(PI/2, Slic3r::Point->new(150,150));
is_deeply [ @{$expolygon2->arrayref_pp} ], [
is_deeply $expolygon2->pp, [
[ @$square[1,2,3,0] ],
[ @$hole_in_square[3,0,1,2] ]
], 'rotate around Point';
@ -69,7 +70,7 @@ isnt ref($arrayref_pp->[0][0]), 'Slic3r::Point', 'Perl polygon points are not bl
{
my $expolygon2 = $expolygon->clone;
$expolygon2->rotate(PI/2, [150,150]);
is_deeply [ @{$expolygon2->arrayref_pp} ], [
is_deeply $expolygon2->pp, [
[ @$square[1,2,3,0] ],
[ @$hole_in_square[3,0,1,2] ]
], 'rotate around pure-Perl Point';
@ -78,19 +79,27 @@ isnt ref($arrayref_pp->[0][0]), 'Slic3r::Point', 'Perl polygon points are not bl
{
my $expolygon2 = $expolygon->clone;
$expolygon2->scale(2);
my $collection = Slic3r::ExPolygon::Collection->new($expolygon->arrayref_pp, $expolygon2->arrayref_pp);
is_deeply [ @{$collection->arrayref_pp} ], [ $expolygon->arrayref_pp, $expolygon2->arrayref_pp ],
'expolygon collection';
my $collection = Slic3r::ExPolygon::Collection->new($expolygon->pp, $expolygon2->pp);
is_deeply $collection->pp, [ $expolygon->pp, $expolygon2->pp ],
'expolygon collection (pure Perl) roundtrip';
my $collection2 = Slic3r::ExPolygon::Collection->new($expolygon, $expolygon2);
is_deeply [ @{$collection->arrayref_pp} ], [ @{$collection2->arrayref_pp} ],
'expolygon collection with XS expolygons';
is_deeply $collection->pp, $collection2->pp,
'expolygon collection (XS) roundtrip';
$collection->clear;
is scalar(@$collection), 0, 'clear collection';
$collection->append($expolygon);
is scalar(@$collection), 1, 'append to collection';
is_deeply $collection->[0]->clone->arrayref_pp, $expolygon->arrayref_pp, 'clone collection item';
my $exp = $collection->[0];
$exp->scale(3);
### we store a copy, not the original by reference
###is_deeply $expolygon->pp, $exp->pp, 'input is stored by reference in collection';
is_deeply $collection->[0]->pp, $exp->pp, 'collection items are returned by reference';
is_deeply $collection->[0]->clone->pp, $collection->[0]->pp, 'clone collection item';
}
__END__

View file

@ -19,7 +19,7 @@ my $hole_in_square = [ # cw
[160, 140],
];
my $expolygon = Slic3r::ExPolygon::XS->new($square, $hole_in_square);
my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square);
my $surface = Slic3r::Surface->new(
expolygon => $expolygon,
surface_type => Slic3r::Surface::S_TYPE_INTERNAL,
@ -27,8 +27,8 @@ my $surface = Slic3r::Surface->new(
$surface = $surface->clone;
isa_ok $surface->expolygon, 'Slic3r::ExPolygon::XS', 'expolygon';
is_deeply [ @{$surface->expolygon->arrayref_pp} ], [$square, $hole_in_square], 'expolygon roundtrip';
isa_ok $surface->expolygon, 'Slic3r::ExPolygon', 'expolygon';
is_deeply [ @{$surface->expolygon->pp} ], [$square, $hole_in_square], 'expolygon roundtrip';
is $surface->surface_type, Slic3r::Surface::S_TYPE_INTERNAL, 'surface_type';
$surface->surface_type(Slic3r::Surface::S_TYPE_BOTTOM);
@ -43,7 +43,7 @@ is $surface->extra_perimeters, 2, 'extra_perimeters';
{
my $collection = Slic3r::Surface::Collection->new($surface, $surface->clone);
is scalar(@$collection), 2, 'collection has the right number of items';
is_deeply $collection->[0]->expolygon->arrayref_pp, [$square, $hole_in_square],
is_deeply $collection->[0]->expolygon->pp, [$square, $hole_in_square],
'collection returns a correct surface expolygon';
$collection->clear;
is scalar(@$collection), 0, 'clear collection';

View file

@ -13,11 +13,10 @@ my $square = [ # ccw
[100, 200],
];
my $polygon = Slic3r::Polygon::XS->new(@$square);
is_deeply [ @{$polygon->arrayref_pp} ], [ @$square ], 'polygon roundtrip';
my $polygon = Slic3r::Polygon->new(@$square);
is_deeply [ @{$polygon->pp} ], [ @$square ], 'polygon roundtrip';
my $arrayref = $polygon->arrayref;
isa_ok $arrayref, 'Slic3r::Polygon', 'Perl polygon is blessed';
isa_ok $arrayref->[0], 'Slic3r::Point', 'Perl points are blessed';
is ref($polygon->arrayref), 'ARRAY', 'polygon arrayref is unblessed';
isa_ok $polygon->[0], 'Slic3r::Point', 'polygon point is blessed';
__END__

View file

@ -13,20 +13,20 @@ my $points = [
];
my $path = Slic3r::ExtrusionPath->new(
polyline => Slic3r::Polyline::XS->new(@$points),
polyline => Slic3r::Polyline->new(@$points),
role => Slic3r::ExtrusionPath::EXTR_ROLE_EXTERNAL_PERIMETER,
);
isa_ok $path->as_polyline, 'Slic3r::Polyline::XS', 'path polyline';
is_deeply [ @{ $path->as_polyline->arrayref_pp } ], [ @$points ], 'path points roundtrip';
isa_ok $path->as_polyline, 'Slic3r::Polyline', 'path polyline';
is_deeply $path->as_polyline->pp, $points, 'path points roundtrip';
$path->reverse;
is_deeply [ @{ $path->as_polyline->arrayref_pp } ], [ reverse @$points ], 'reverse path';
is_deeply $path->as_polyline->pp, [ reverse @$points ], 'reverse path';
$path->append([ 150, 150 ]);
is scalar(@{ $path }), 4, 'append to path';
is scalar(@$path), 4, 'append to path';
$path->pop_back;
is scalar(@{ $path }), 3, 'pop_back from path';
is scalar(@$path), 3, 'pop_back from path';
$path = $path->clone;

View file

@ -14,11 +14,11 @@ my $square = [
];
my $loop = Slic3r::ExtrusionLoop->new(
polygon => Slic3r::Polygon::XS->new(@$square),
polygon => Slic3r::Polygon->new(@$square),
role => Slic3r::ExtrusionPath::EXTR_ROLE_EXTERNAL_PERIMETER,
);
isa_ok $loop->as_polygon, 'Slic3r::Polygon::XS', 'loop polygon';
is_deeply [ @{ $loop->as_polygon->arrayref_pp } ], [ @$square ], 'polygon points roundtrip';
isa_ok $loop->as_polygon, 'Slic3r::Polygon', 'loop polygon';
is_deeply $loop->as_polygon->pp, $square, 'polygon points roundtrip';
$loop = $loop->clone;