6
use List::Util qw(first sum);
8
use Test::More tests => 33;
10
use constant PI => 4 * atan2(1, 1);
18
my $hole_in_square = [ # cw
25
my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square);
27
ok $expolygon->is_valid, 'is_valid';
28
is ref($expolygon->pp), 'ARRAY', 'expolygon pp is unblessed';
29
is_deeply $expolygon->pp, [$square, $hole_in_square], 'expolygon roundtrip';
31
is ref($expolygon->arrayref), 'ARRAY', 'expolygon arrayref is unblessed';
32
isa_ok $expolygon->[0], 'Slic3r::Polygon::Ref', 'expolygon polygon is blessed';
33
isa_ok $expolygon->contour, 'Slic3r::Polygon::Ref', 'expolygon contour is blessed';
34
isa_ok $expolygon->holes->[0], 'Slic3r::Polygon::Ref', 'expolygon hole is blessed';
35
isa_ok $expolygon->[0][0], 'Slic3r::Point::Ref', 'expolygon point is blessed';
38
my $expolygon2 = $expolygon->clone;
39
my $polygon = $expolygon2->[0];
41
is $expolygon2->[0][0][0], $polygon->[0][0], 'polygons are returned by reference';
44
is_deeply $expolygon->clone->pp, [$square, $hole_in_square], 'clone';
46
is $expolygon->area, 100*100-20*20, 'area';
49
my $expolygon2 = $expolygon->clone;
50
$expolygon2->scale(2.5);
51
is_deeply $expolygon2->pp, [
52
[map [ 2.5*$_->[0], 2.5*$_->[1] ], @$square],
53
[map [ 2.5*$_->[0], 2.5*$_->[1] ], @$hole_in_square]
58
my $expolygon2 = $expolygon->clone;
59
$expolygon2->translate(10, -5);
60
is_deeply $expolygon2->pp, [
61
[map [ $_->[0]+10, $_->[1]-5 ], @$square],
62
[map [ $_->[0]+10, $_->[1]-5 ], @$hole_in_square]
67
my $expolygon2 = $expolygon->clone;
68
$expolygon2->rotate(PI/2, Slic3r::Point->new(150,150));
69
is_deeply $expolygon2->pp, [
70
[ @$square[1,2,3,0] ],
71
[ @$hole_in_square[3,0,1,2] ]
72
], 'rotate around Point';
76
my $expolygon2 = $expolygon->clone;
77
$expolygon2->rotate(PI/2, [150,150]);
78
is_deeply $expolygon2->pp, [
79
[ @$square[1,2,3,0] ],
80
[ @$hole_in_square[3,0,1,2] ]
81
], 'rotate around pure-Perl Point';
85
my $expolygon2 = $expolygon->clone;
86
$expolygon2->scale(2);
87
my $collection = Slic3r::ExPolygon::Collection->new($expolygon->pp, $expolygon2->pp);
88
is_deeply $collection->pp, [ $expolygon->pp, $expolygon2->pp ],
89
'expolygon collection (pure Perl) roundtrip';
91
my $collection2 = Slic3r::ExPolygon::Collection->new($expolygon, $expolygon2);
92
is_deeply $collection->pp, $collection2->pp,
93
'expolygon collection (XS) roundtrip';
96
is scalar(@$collection), 0, 'clear collection';
98
$collection->append($expolygon);
99
is scalar(@$collection), 1, 'append to collection';
101
my $exp = $collection->[0];
103
is $collection->[0][0][0][0], $exp->[0][0][0], 'collection items are returned by reference';
105
is_deeply $collection->[0]->clone->pp, $collection->[0]->pp, 'clone collection item';
109
my $expolygon = Slic3r::ExPolygon->new($square);
110
my $polygons = $expolygon->get_trapezoids2(PI/2);
111
is scalar(@$polygons), 1, 'correct number of trapezoids returned';
112
is scalar(@{$polygons->[0]}), 4, 'trapezoid has 4 points';
113
is $polygons->[0]->area, $expolygon->area, 'trapezoid has correct area';
117
my $polygons = $expolygon->get_trapezoids2(PI/2);
118
is scalar(@$polygons), 4, 'correct number of trapezoids returned';
120
# trapezoid polygons might have more than 4 points in case of collinear segments
121
$polygons = [ map @{$_->simplify(1)}, @$polygons ];
122
ok !defined(first { @$_ != 4 } @$polygons), 'all trapezoids have 4 points';
124
is scalar(grep { $_->area == 40*100 } @$polygons), 2, 'trapezoids have expected area';
125
is scalar(grep { $_->area == 20*40 } @$polygons), 2, 'trapezoids have expected area';
129
my $expolygon = Slic3r::ExPolygon->new([ [0,100],[100,0],[200,0],[300,100],[200,200],[100,200] ]);
130
my $polygons = $expolygon->get_trapezoids2(PI/2);
131
is scalar(@$polygons), 3, 'correct number of trapezoids returned';
132
is scalar(grep { $_->area == 100*200/2 } @$polygons), 2, 'trapezoids have expected area';
133
is scalar(grep { $_->area == 100*200 } @$polygons), 1, 'trapezoids have expected area';
137
my $triangles = $expolygon->triangulate_pp;
138
is scalar(@$triangles), 8, 'expected number of triangles';
139
is sum(map $_->area, @$triangles), $expolygon->area, 'sum of triangles area equals original expolygon area';