2011-10-04 20:27:45 +00:00
|
|
|
use Test::More;
|
2011-10-10 09:00:19 +00:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
2011-10-04 20:27:45 +00:00
|
|
|
|
2014-04-08 13:26:31 +00:00
|
|
|
plan tests => 6;
|
2011-10-04 20:27:45 +00:00
|
|
|
|
2013-08-26 23:26:44 +00:00
|
|
|
BEGIN {
|
|
|
|
use FindBin;
|
|
|
|
use lib "$FindBin::Bin/../lib";
|
2017-08-18 07:58:50 +00:00
|
|
|
use local::lib "$FindBin::Bin/../local-lib";
|
2013-08-26 23:26:44 +00:00
|
|
|
}
|
2011-10-04 20:27:45 +00:00
|
|
|
|
2013-12-24 11:40:46 +00:00
|
|
|
use List::Util qw(sum);
|
2013-08-26 23:26:44 +00:00
|
|
|
use Slic3r;
|
2014-04-08 13:26:31 +00:00
|
|
|
use Slic3r::Geometry::Clipper qw(intersection_ex union_ex diff_ex diff_pl);
|
2011-10-04 20:27:45 +00:00
|
|
|
|
2012-12-27 13:16:50 +00:00
|
|
|
{
|
|
|
|
my $square = [ # ccw
|
|
|
|
[10, 10],
|
|
|
|
[20, 10],
|
|
|
|
[20, 20],
|
|
|
|
[10, 20],
|
|
|
|
];
|
|
|
|
my $hole_in_square = [ # cw
|
|
|
|
[14, 14],
|
|
|
|
[14, 16],
|
|
|
|
[16, 16],
|
|
|
|
[16, 14],
|
|
|
|
];
|
|
|
|
my $square2 = [ # ccw
|
|
|
|
[5, 12],
|
|
|
|
[25, 12],
|
|
|
|
[25, 18],
|
|
|
|
[5, 18],
|
|
|
|
];
|
2013-08-26 23:26:44 +00:00
|
|
|
my $intersection = intersection_ex([ $square, $hole_in_square ], [ $square2 ]);
|
2013-05-11 07:19:23 +00:00
|
|
|
|
2013-12-24 11:40:46 +00:00
|
|
|
is sum(map $_->area, @$intersection), Slic3r::ExPolygon->new(
|
2013-08-26 23:26:44 +00:00
|
|
|
[
|
|
|
|
[20, 18],
|
|
|
|
[10, 18],
|
|
|
|
[10, 12],
|
2013-11-20 10:35:58 +00:00
|
|
|
[20, 12],
|
2013-08-26 23:26:44 +00:00
|
|
|
],
|
|
|
|
[
|
|
|
|
[14, 16],
|
|
|
|
[16, 16],
|
|
|
|
[16, 14],
|
2013-11-20 10:35:58 +00:00
|
|
|
[14, 14],
|
2013-08-26 23:26:44 +00:00
|
|
|
],
|
2013-12-24 11:40:46 +00:00
|
|
|
)->area, 'hole is preserved after intersection';
|
2012-12-27 13:16:50 +00:00
|
|
|
}
|
2011-10-04 20:27:45 +00:00
|
|
|
|
2012-12-27 13:16:50 +00:00
|
|
|
#==========================================================
|
2011-10-04 20:27:45 +00:00
|
|
|
|
2012-12-27 13:16:50 +00:00
|
|
|
{
|
|
|
|
my $contour1 = [ [0,0], [40,0], [40,40], [0,40] ]; # ccw
|
|
|
|
my $contour2 = [ [10,10], [30,10], [30,30], [10,30] ]; # ccw
|
|
|
|
my $hole = [ [15,15], [15,25], [25,25], [25,15] ]; # cw
|
|
|
|
|
2013-08-26 23:26:44 +00:00
|
|
|
my $union = union_ex([ $contour1, $contour2, $hole ]);
|
2013-05-11 07:19:23 +00:00
|
|
|
|
2013-11-20 10:35:58 +00:00
|
|
|
is_deeply [ map $_->pp, @$union ], [[ [ [40,40], [0,40], [0,0], [40,0] ] ]],
|
2012-12-27 13:16:50 +00:00
|
|
|
'union of two ccw and one cw is a contour with no holes';
|
|
|
|
|
2013-08-26 23:26:44 +00:00
|
|
|
my $diff = diff_ex([ $contour1, $contour2 ], [ $hole ]);
|
2013-12-24 11:40:46 +00:00
|
|
|
is sum(map $_->area, @$diff),
|
|
|
|
Slic3r::ExPolygon->new([ [40,40], [0,40], [0,0], [40,0] ], [ [15,25], [25,25], [25,15], [15,15] ])->area,
|
2012-12-27 13:16:50 +00:00
|
|
|
'difference of a cw from two ccw is a contour with one hole';
|
|
|
|
}
|
2011-10-04 20:27:45 +00:00
|
|
|
|
2014-04-08 13:26:31 +00:00
|
|
|
#==========================================================
|
|
|
|
|
|
|
|
{
|
|
|
|
my $square = Slic3r::Polygon->new_scale( # ccw
|
|
|
|
[10, 10],
|
|
|
|
[20, 10],
|
|
|
|
[20, 20],
|
|
|
|
[10, 20],
|
|
|
|
);
|
|
|
|
my $square_pl = $square->split_at_first_point;
|
|
|
|
|
|
|
|
my $res = diff_pl([$square_pl], []);
|
|
|
|
is scalar(@$res), 1, 'no-op diff_pl returns the right number of polylines';
|
|
|
|
isa_ok $res->[0], 'Slic3r::Polyline', 'no-op diff_pl result';
|
2014-05-21 13:45:16 +00:00
|
|
|
is scalar(@{$res->[0]}), scalar(@$square_pl), 'no-op diff_pl returns the unmodified input polyline';
|
2014-04-08 13:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__END__
|