diff --git a/lib/Slic3r/Geometry/Clipper.pm b/lib/Slic3r/Geometry/Clipper.pm index 2b090a8bb..164d5fc3e 100644 --- a/lib/Slic3r/Geometry/Clipper.pm +++ b/lib/Slic3r/Geometry/Clipper.pm @@ -45,18 +45,6 @@ sub offset2 { 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 { my ($subject, $clip, $safety_offset) = @_; diff --git a/xs/t/11_clipper.t b/xs/t/11_clipper.t index 1da3de557..3bff3b804 100644 --- a/xs/t/11_clipper.t +++ b/xs/t/11_clipper.t @@ -4,24 +4,24 @@ use strict; use warnings; use Slic3r::XS; -use Test::More tests => 2; +use Test::More tests => 3; my $square = [ # ccw - [100, 100], [200, 100], [200, 200], [100, 200], + [100, 100], ]; my $hole_in_square = [ # cw + [160, 140], [140, 140], [140, 160], [160, 160], - [160, 140], ]; 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, [ [ [205, 95], [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, [ [ [203, 97], [203, 203], @@ -50,4 +50,11 @@ my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square); ] ], '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__ diff --git a/xs/xsp/Clipper.xsp b/xs/xsp/Clipper.xsp index 324da1b53..019639925 100644 --- a/xs/xsp/Clipper.xsp +++ b/xs/xsp/Clipper.xsp @@ -74,4 +74,41 @@ offset2_ex(polygons, delta1, delta2, scale = 100000, joinType = ClipperLib::jtMi OUTPUT: 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 + %}