Ported diff_ex() to XS

This commit is contained in:
Alessandro Ranellucci 2013-07-16 20:29:15 +02:00
parent 898007fc36
commit 79db996696
3 changed files with 49 additions and 17 deletions

View File

@ -45,18 +45,6 @@ sub offset2 {
return @$offsets; return @$offsets;
} }
sub diff_ex {
my ($subject, $clip, $safety_offset) = @_;
$clipper->clear;
$clipper->add_subject_polygons(_convert($subject));
$clipper->add_clip_polygons($safety_offset ? _convert(safety_offset($clip)) : _convert($clip));
return [
map Slic3r::ExPolygon->new($_->{outer}, @{$_->{holes}}),
@{ $clipper->ex_execute(CT_DIFFERENCE, PFT_NONZERO, PFT_NONZERO) },
];
}
sub diff { sub diff {
my ($subject, $clip, $safety_offset) = @_; my ($subject, $clip, $safety_offset) = @_;

View File

@ -4,24 +4,24 @@ use strict;
use warnings; use warnings;
use Slic3r::XS; use Slic3r::XS;
use Test::More tests => 2; use Test::More tests => 3;
my $square = [ # ccw my $square = [ # ccw
[100, 100],
[200, 100], [200, 100],
[200, 200], [200, 200],
[100, 200], [100, 200],
[100, 100],
]; ];
my $hole_in_square = [ # cw my $hole_in_square = [ # cw
[160, 140],
[140, 140], [140, 140],
[140, 160], [140, 160],
[160, 160], [160, 160],
[160, 140],
]; ];
my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square); my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square);
{ {
my $result = @{Slic3r::Geometry::Clipper::offset_ex([ @$expolygon ], 5)}; my $result = Slic3r::Geometry::Clipper::offset_ex([ @$expolygon ], 5);
is_deeply $result->[0]->pp, [ [ is_deeply $result->[0]->pp, [ [
[205, 95], [205, 95],
[205, 205], [205, 205],
@ -36,7 +36,7 @@ my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square);
} }
{ {
my $result = @{Slic3r::Geometry::Clipper::offset2_ex([ @$expolygon ], 5, -2)}; my $result = Slic3r::Geometry::Clipper::offset2_ex([ @$expolygon ], 5, -2);
is_deeply $result->[0]->pp, [ [ is_deeply $result->[0]->pp, [ [
[203, 97], [203, 97],
[203, 203], [203, 203],
@ -50,4 +50,11 @@ my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square);
] ], 'offset_ex'; ] ], 'offset_ex';
} }
{
my $polygon1 = Slic3r::Polygon->new(@$square);
my $polygon2 = Slic3r::Polygon->new(reverse @$hole_in_square);
my $result = Slic3r::Geometry::Clipper::diff_ex([$polygon1], [$polygon2]);
is_deeply $result->[0]->pp, $expolygon->pp, 'diff_ex';
}
__END__ __END__

View File

@ -74,4 +74,41 @@ offset2_ex(polygons, delta1, delta2, scale = 100000, joinType = ClipperLib::jtMi
OUTPUT: OUTPUT:
RETVAL RETVAL
ExPolygons
diff_ex(subject, clip, safety_offset = false)
Polygons subject
Polygons clip
bool safety_offset
CODE:
// read input
ClipperLib::Polygons* input_subject = new ClipperLib::Polygons();
ClipperLib::Polygons* input_clip = new ClipperLib::Polygons();
Slic3rPolygons_to_ClipperPolygons(subject, *input_subject);
Slic3rPolygons_to_ClipperPolygons(clip, *input_clip);
// perform safety offset
if (safety_offset) {
// SafetyOffset(*input_clip);
}
// init Clipper
ClipperLib::Clipper clipper;
clipper.Clear();
// add polygons
clipper.AddPolygons(*input_subject, ClipperLib::ptSubject);
delete input_subject;
clipper.AddPolygons(*input_clip, ClipperLib::ptClip);
delete input_clip;
// perform operation
ClipperLib::PolyTree* polytree = new ClipperLib::PolyTree();
clipper.Execute(ClipperLib::ctDifference, *polytree, ClipperLib::pftNonZero, ClipperLib::pftNonZero);
// convert into ExPolygons
PolyTreeToExPolygons(*polytree, RETVAL);
delete polytree;
OUTPUT:
RETVAL
%} %}