2013-07-15 10:14:22 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2014-04-24 14:59:36 +00:00
|
|
|
use List::Util qw(first);
|
2013-07-15 10:14:22 +00:00
|
|
|
use Slic3r::XS;
|
2014-04-24 14:59:36 +00:00
|
|
|
use Test::More tests => 19;
|
|
|
|
|
|
|
|
use constant PI => 4 * atan2(1, 1);
|
2013-07-15 10:14:22 +00:00
|
|
|
|
|
|
|
my $square = [ # ccw
|
|
|
|
[100, 100],
|
|
|
|
[200, 100],
|
|
|
|
[200, 200],
|
|
|
|
[100, 200],
|
|
|
|
];
|
|
|
|
|
2013-07-15 20:57:22 +00:00
|
|
|
my $polygon = Slic3r::Polygon->new(@$square);
|
2013-11-21 15:21:42 +00:00
|
|
|
my $cw_polygon = $polygon->clone;
|
|
|
|
$cw_polygon->reverse;
|
|
|
|
|
2013-08-26 21:27:51 +00:00
|
|
|
ok $polygon->is_valid, 'is_valid';
|
2013-07-15 21:12:13 +00:00
|
|
|
is_deeply $polygon->pp, $square, 'polygon roundtrip';
|
2013-07-15 10:14:22 +00:00
|
|
|
|
2013-07-15 20:57:22 +00:00
|
|
|
is ref($polygon->arrayref), 'ARRAY', 'polygon arrayref is unblessed';
|
2013-09-02 18:22:20 +00:00
|
|
|
isa_ok $polygon->[0], 'Slic3r::Point::Ref', 'polygon point is blessed';
|
2013-07-15 10:14:22 +00:00
|
|
|
|
2013-07-15 21:28:23 +00:00
|
|
|
my $lines = $polygon->lines;
|
|
|
|
is_deeply [ map $_->pp, @$lines ], [
|
|
|
|
[ [100, 100], [200, 100] ],
|
|
|
|
[ [200, 100], [200, 200] ],
|
|
|
|
[ [200, 200], [100, 200] ],
|
|
|
|
[ [100, 200], [100, 100] ],
|
|
|
|
], 'polygon lines';
|
|
|
|
|
2013-07-16 15:13:01 +00:00
|
|
|
is_deeply $polygon->split_at_first_point->pp, [ @$square[0,1,2,3,0] ], 'split_at_first_point';
|
|
|
|
is_deeply $polygon->split_at_index(2)->pp, [ @$square[2,3,0,1,2] ], 'split_at_index';
|
2014-05-22 10:28:12 +00:00
|
|
|
is_deeply $polygon->split_at_vertex(Slic3r::Point->new(@{$square->[2]}))->pp, [ @$square[2,3,0,1,2] ], 'split_at';
|
2013-08-26 20:44:40 +00:00
|
|
|
is $polygon->area, 100*100, 'area';
|
2013-07-15 21:38:06 +00:00
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
ok $polygon->is_counter_clockwise, 'is_counter_clockwise';
|
2013-11-21 15:21:42 +00:00
|
|
|
ok !$cw_polygon->is_counter_clockwise, 'is_counter_clockwise';
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
|
|
|
my $clone = $polygon->clone;
|
|
|
|
$clone->reverse;
|
|
|
|
ok !$clone->is_counter_clockwise, 'is_counter_clockwise';
|
2013-07-16 19:09:29 +00:00
|
|
|
$clone->make_counter_clockwise;
|
|
|
|
ok $clone->is_counter_clockwise, 'make_counter_clockwise';
|
|
|
|
$clone->make_counter_clockwise;
|
|
|
|
ok $clone->is_counter_clockwise, 'make_counter_clockwise';
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 17:26:58 +00:00
|
|
|
ok ref($polygon->first_point) eq 'Slic3r::Point', 'first_point';
|
2013-09-02 18:22:20 +00:00
|
|
|
|
2013-11-21 15:21:42 +00:00
|
|
|
ok $polygon->contains_point(Slic3r::Point->new(150,150)), 'ccw contains_point';
|
|
|
|
ok $cw_polygon->contains_point(Slic3r::Point->new(150,150)), 'cw contains_point';
|
|
|
|
|
2014-04-24 14:59:36 +00:00
|
|
|
{
|
|
|
|
my @points = (Slic3r::Point->new(100,0));
|
|
|
|
foreach my $i (1..5) {
|
|
|
|
my $point = $points[0]->clone;
|
|
|
|
$point->rotate(PI/3*$i, [0,0]);
|
|
|
|
push @points, $point;
|
|
|
|
}
|
|
|
|
my $hexagon = Slic3r::Polygon->new(@points);
|
|
|
|
my $triangles = $hexagon->triangulate_convex;
|
|
|
|
is scalar(@$triangles), 4, 'right number of triangles';
|
|
|
|
ok !(defined first { $_->is_clockwise } @$triangles), 'all triangles are ccw';
|
|
|
|
}
|
|
|
|
|
2013-09-02 18:22:20 +00:00
|
|
|
# this is not a test: this just demonstrates bad usage, where $polygon->clone gets
|
|
|
|
# DESTROY'ed before the derived object ($point), causing bad memory access
|
|
|
|
if (0) {
|
|
|
|
my $point;
|
|
|
|
{
|
|
|
|
$point = $polygon->clone->[0];
|
|
|
|
}
|
|
|
|
$point->scale(2);
|
|
|
|
}
|
|
|
|
|
2013-07-15 10:14:22 +00:00
|
|
|
__END__
|