PrusaSlicer-NonPlainar/xs/t/04_expolygon.t

109 lines
3.1 KiB
Perl
Raw Normal View History

2013-07-06 14:33:49 +00:00
#!/usr/bin/perl
use strict;
use warnings;
use Slic3r::XS;
use Test::More tests => 21;
2013-07-11 16:55:51 +00:00
use constant PI => 4 * atan2(1, 1);
2013-07-06 14:33:49 +00:00
my $square = [ # ccw
[100, 100],
[200, 100],
[200, 200],
[100, 200],
];
my $hole_in_square = [ # cw
[140, 140],
[140, 160],
[160, 160],
[160, 140],
];
my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square);
2013-07-06 14:33:49 +00:00
ok $expolygon->is_valid, 'is_valid';
is ref($expolygon->pp), 'ARRAY', 'expolygon pp is unblessed';
is_deeply $expolygon->pp, [$square, $hole_in_square], 'expolygon roundtrip';
2013-07-15 18:31:43 +00:00
is ref($expolygon->arrayref), 'ARRAY', 'expolygon arrayref is unblessed';
isa_ok $expolygon->[0], 'Slic3r::Polygon', 'expolygon polygon is blessed';
isa_ok $expolygon->contour, 'Slic3r::Polygon', 'expolygon contour is blessed';
isa_ok $expolygon->holes->[0], 'Slic3r::Polygon', 'expolygon hole is blessed';
isa_ok $expolygon->[0][0], 'Slic3r::Point', 'expolygon point is blessed';
2013-07-11 16:55:51 +00:00
{
my $polygon = $expolygon->[0];
$polygon->scale(2);
isnt $expolygon->[0][0][0], $polygon->[0][0], 'a copy of polygons is returned';
2013-07-11 16:55:51 +00:00
}
is_deeply $expolygon->clone->pp, [$square, $hole_in_square], 'clone';
# The following tests implicitely check that modifying clones
# does not modify the original one.
2013-08-26 20:50:26 +00:00
is $expolygon->area, 100*100-20*20, 'area';
2013-07-11 16:55:51 +00:00
{
my $expolygon2 = $expolygon->clone;
$expolygon2->scale(2.5);
is_deeply $expolygon2->pp, [
2013-07-11 16:55:51 +00:00
[map [ 2.5*$_->[0], 2.5*$_->[1] ], @$square],
[map [ 2.5*$_->[0], 2.5*$_->[1] ], @$hole_in_square]
], 'scale';
}
{
my $expolygon2 = $expolygon->clone;
$expolygon2->translate(10, -5);
is_deeply $expolygon2->pp, [
2013-07-11 16:55:51 +00:00
[map [ $_->[0]+10, $_->[1]-5 ], @$square],
[map [ $_->[0]+10, $_->[1]-5 ], @$hole_in_square]
], 'translate';
}
{
my $expolygon2 = $expolygon->clone;
2013-07-15 18:31:43 +00:00
$expolygon2->rotate(PI/2, Slic3r::Point->new(150,150));
is_deeply $expolygon2->pp, [
2013-07-11 16:55:51 +00:00
[ @$square[1,2,3,0] ],
[ @$hole_in_square[3,0,1,2] ]
2013-07-15 18:31:43 +00:00
], 'rotate around Point';
2013-07-11 16:55:51 +00:00
}
{
my $expolygon2 = $expolygon->clone;
$expolygon2->rotate(PI/2, [150,150]);
is_deeply $expolygon2->pp, [
2013-07-11 16:55:51 +00:00
[ @$square[1,2,3,0] ],
[ @$hole_in_square[3,0,1,2] ]
], 'rotate around pure-Perl Point';
}
{
my $expolygon2 = $expolygon->clone;
$expolygon2->scale(2);
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->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';
2013-07-15 18:31:43 +00:00
my $exp = $collection->[0];
$exp->scale(3);
isnt $collection->[0][0][0][0], $exp->[0][0][0], 'collection items are not returned by reference';
is_deeply $collection->[0]->clone->pp, $collection->[0]->pp, 'clone collection item';
}
2013-07-06 14:33:49 +00:00
__END__