6
use List::Util qw(first);
8
use Test::More tests => 20;
10
use constant PI => 4 * atan2(1, 1);
19
my $polygon = Slic3r::Polygon->new(@$square);
20
my $cw_polygon = $polygon->clone;
23
ok $polygon->is_valid, 'is_valid';
24
is_deeply $polygon->pp, $square, 'polygon roundtrip';
26
is ref($polygon->arrayref), 'ARRAY', 'polygon arrayref is unblessed';
27
isa_ok $polygon->[0], 'Slic3r::Point::Ref', 'polygon point is blessed';
29
my $lines = $polygon->lines;
30
is_deeply [ map $_->pp, @$lines ], [
31
[ [100, 100], [200, 100] ],
32
[ [200, 100], [200, 200] ],
33
[ [200, 200], [100, 200] ],
34
[ [100, 200], [100, 100] ],
37
is_deeply $polygon->split_at_first_point->pp, [ @$square[0,1,2,3,0] ], 'split_at_first_point';
38
is_deeply $polygon->split_at_index(2)->pp, [ @$square[2,3,0,1,2] ], 'split_at_index';
39
is_deeply $polygon->split_at_vertex(Slic3r::Point->new(@{$square->[2]}))->pp, [ @$square[2,3,0,1,2] ], 'split_at';
40
is $polygon->area, 100*100, 'area';
42
ok $polygon->is_counter_clockwise, 'is_counter_clockwise';
43
ok !$cw_polygon->is_counter_clockwise, 'is_counter_clockwise';
45
my $clone = $polygon->clone;
47
ok !$clone->is_counter_clockwise, 'is_counter_clockwise';
48
$clone->make_counter_clockwise;
49
ok $clone->is_counter_clockwise, 'make_counter_clockwise';
50
$clone->make_counter_clockwise;
51
ok $clone->is_counter_clockwise, 'make_counter_clockwise';
54
ok ref($polygon->first_point) eq 'Slic3r::Point', 'first_point';
56
ok $polygon->contains_point(Slic3r::Point->new(150,150)), 'ccw contains_point';
57
ok $cw_polygon->contains_point(Slic3r::Point->new(150,150)), 'cw contains_point';
60
my @points = (Slic3r::Point->new(100,0));
61
foreach my $i (1..5) {
62
my $point = $points[0]->clone;
63
$point->rotate(PI/3*$i, [0,0]);
66
my $hexagon = Slic3r::Polygon->new(@points);
67
my $triangles = $hexagon->triangulate_convex;
68
is scalar(@$triangles), 4, 'right number of triangles';
69
ok !(defined first { $_->is_clockwise } @$triangles), 'all triangles are ccw';
73
is_deeply $polygon->centroid->pp, [150,150], 'centroid';
76
# this is not a test: this just demonstrates bad usage, where $polygon->clone gets
77
# DESTROY'ed before the derived object ($point), causing bad memory access
81
$point = $polygon->clone->[0];