diff --git a/Build.PL b/Build.PL index 8aa8f69d5..302777a05 100644 --- a/Build.PL +++ b/Build.PL @@ -12,7 +12,6 @@ my %prereqs = qw( File::Basename 0 File::Spec 0 Getopt::Long 0 - Math::Clipper 1.22 Math::ConvexHull::MonotoneChain 0.01 Math::Geometry::Voronoi 1.3 Math::PlanePath 53 diff --git a/lib/Slic3r/ExPolygon.pm b/lib/Slic3r/ExPolygon.pm index 9bda96205..e4dd15266 100644 --- a/lib/Slic3r/ExPolygon.pm +++ b/lib/Slic3r/ExPolygon.pm @@ -8,7 +8,7 @@ use Boost::Geometry::Utils; use List::Util qw(first); use Math::Geometry::Voronoi; use Slic3r::Geometry qw(X Y A B point_in_polygon same_line epsilon); -use Slic3r::Geometry::Clipper qw(union_ex JT_MITER); +use Slic3r::Geometry::Clipper qw(union_ex); sub wkt { my $self = shift; diff --git a/lib/Slic3r/Fill/Concentric.pm b/lib/Slic3r/Fill/Concentric.pm index d176fb051..a7d1c0062 100644 --- a/lib/Slic3r/Fill/Concentric.pm +++ b/lib/Slic3r/Fill/Concentric.pm @@ -4,7 +4,7 @@ use Moo; extends 'Slic3r::Fill::Base'; use Slic3r::Geometry qw(scale unscale X); -use Slic3r::Geometry::Clipper qw(offset offset2 union_pt traverse_pt PFT_EVENODD); +use Slic3r::Geometry::Clipper qw(offset offset2 union_pt traverse_pt); sub fill_surface { my $self = shift; diff --git a/lib/Slic3r/GCode/MotionPlanner.pm b/lib/Slic3r/GCode/MotionPlanner.pm index dae500ea8..e4f8e14bd 100644 --- a/lib/Slic3r/GCode/MotionPlanner.pm +++ b/lib/Slic3r/GCode/MotionPlanner.pm @@ -14,7 +14,7 @@ has '_tolerance' => (is => 'lazy'); use List::Util qw(first); use Slic3r::Geometry qw(A B scale epsilon); -use Slic3r::Geometry::Clipper qw(diff_ex offset JT_MITER); +use Slic3r::Geometry::Clipper qw(diff_ex offset); # clearance (in mm) from the perimeters has '_inner_margin' => (is => 'ro', default => sub { scale 0.5 }); diff --git a/lib/Slic3r/GUI/Plater.pm b/lib/Slic3r/GUI/Plater.pm index 345fdf01a..23f05d2ae 100644 --- a/lib/Slic3r/GUI/Plater.pm +++ b/lib/Slic3r/GUI/Plater.pm @@ -5,7 +5,7 @@ use utf8; use File::Basename qw(basename dirname); use List::Util qw(max sum first); -use Math::Clipper qw(offset JT_ROUND); +use Slic3r::Geometry::Clipper qw(offset JT_ROUND); use Math::ConvexHull::MonotoneChain qw(convex_hull); use Slic3r::Geometry qw(X Y Z MIN MAX); use threads::shared qw(shared_clone); @@ -681,7 +681,6 @@ sub export_gcode { sub export_gcode2 { my $self = shift; my ($print, $output_file, %params) = @_; - $Slic3r::Geometry::Clipper::clipper = Math::Clipper->new; local $SIG{'KILL'} = sub { Slic3r::debugf "Exporting cancelled; exiting thread...\n"; threads->exit(); @@ -822,7 +821,6 @@ sub make_thumbnail { my $object = $self->{objects}[$obj_idx]; $object->thumbnail_scaling_factor($self->{scaling_factor}); my $cb = sub { - $Slic3r::Geometry::Clipper::clipper = Math::Clipper->new if $Slic3r::have_threads; my $thumbnail = $object->make_thumbnail; if ($Slic3r::have_threads) { diff --git a/lib/Slic3r/Geometry.pm b/lib/Slic3r/Geometry.pm index f3d7368f2..93b0924d8 100644 --- a/lib/Slic3r/Geometry.pm +++ b/lib/Slic3r/Geometry.pm @@ -792,14 +792,13 @@ sub chained_path { my %indices = map { $points[$_] => $_ } 0 .. $#points; my @result = (); - my $last_point; - if (!$start_near) { + if (!$start_near && @points) { $start_near = shift @points; - push @result, $indices{$start_near} if $start_near; + push @result, $indices{$start_near}; } while (@points) { - $start_near = $start_near->nearest_point(\@points); - @points = grep $_ ne $start_near, @points; + my $idx = $start_near->nearest_point_index(\@points); + my ($start_near) = splice @points, $idx, 1; push @result, $indices{$start_near}; } diff --git a/lib/Slic3r/Geometry/Clipper.pm b/lib/Slic3r/Geometry/Clipper.pm index 362569ff7..609701e58 100644 --- a/lib/Slic3r/Geometry/Clipper.pm +++ b/lib/Slic3r/Geometry/Clipper.pm @@ -5,13 +5,11 @@ use warnings; require Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = qw(offset offset_ex - diff_ex diff union_ex intersection_ex xor_ex PFT_EVENODD JT_MITER JT_ROUND + diff_ex diff union_ex intersection_ex xor_ex JT_ROUND JT_SQUARE is_counter_clockwise union_pt offset2 offset2_ex traverse_pt intersection union); -use Math::Clipper 1.22 qw(:cliptypes :polyfilltypes :jointypes is_counter_clockwise area); use Slic3r::Geometry qw(scale); -our $clipper = Math::Clipper->new; sub traverse_pt { my ($polynodes) = @_; diff --git a/lib/Slic3r/Layer/Region.pm b/lib/Slic3r/Layer/Region.pm index 30a6c342c..c4df3caf9 100644 --- a/lib/Slic3r/Layer/Region.pm +++ b/lib/Slic3r/Layer/Region.pm @@ -5,7 +5,7 @@ use List::Util qw(sum first); use Slic3r::ExtrusionPath ':roles'; use Slic3r::Geometry qw(PI A B scale chained_path_items points_coincide); use Slic3r::Geometry::Clipper qw(union_ex diff_ex intersection_ex - offset offset2 offset2_ex PFT_EVENODD union_pt traverse_pt diff intersection + offset offset2 offset2_ex union_pt traverse_pt diff intersection union diff); use Slic3r::Surface ':types'; diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index 148166138..6850eaba9 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -8,7 +8,7 @@ use Math::ConvexHull::MonotoneChain qw(convex_hull); use Slic3r::ExtrusionPath ':roles'; use Slic3r::Geometry qw(X Y Z X1 Y1 X2 Y2 MIN MAX PI scale unscale move_points chained_path); use Slic3r::Geometry::Clipper qw(diff_ex union_ex union_pt intersection_ex offset - offset2 traverse_pt JT_ROUND JT_SQUARE PFT_EVENODD); + offset2 traverse_pt JT_ROUND JT_SQUARE); use Time::HiRes qw(gettimeofday tv_interval); has 'config' => (is => 'rw', default => sub { Slic3r::Config->new_from_defaults }, trigger => 1); @@ -403,7 +403,6 @@ sub export_gcode { }, thread_cb => sub { my $q = shift; - $Slic3r::Geometry::Clipper::clipper = Math::Clipper->new; while (defined (my $obj_layer = $q->dequeue)) { my ($obj_idx, $layer_id, $region_id) = @$obj_layer; my $object = $self->objects->[$obj_idx]; diff --git a/lib/Slic3r/Print/Object.pm b/lib/Slic3r/Print/Object.pm index bfbf0d420..a7ac8bd0e 100644 --- a/lib/Slic3r/Print/Object.pm +++ b/lib/Slic3r/Print/Object.pm @@ -330,7 +330,6 @@ sub make_perimeters { items => sub { 0 .. ($self->layer_count-1) }, thread_cb => sub { my $q = shift; - $Slic3r::Geometry::Clipper::clipper = Math::Clipper->new; while (defined (my $layer_id = $q->dequeue)) { $self->layers->[$layer_id]->make_perimeters; } @@ -1235,7 +1234,6 @@ sub generate_support_material { items => [ 0 .. $#{$self->support_layers} ], thread_cb => sub { my $q = shift; - $Slic3r::Geometry::Clipper::clipper = Math::Clipper->new; my $result = {}; while (defined (my $layer_id = $q->dequeue)) { $result->{$layer_id} = $process_layer->($layer_id); diff --git a/t/clipper.t b/t/clipper.t index fda0c9270..fbf581063 100644 --- a/t/clipper.t +++ b/t/clipper.t @@ -4,8 +4,13 @@ use warnings; plan tests => 3; -use Math::Clipper ':all'; +BEGIN { + use FindBin; + use lib "$FindBin::Bin/../lib"; +} +use Slic3r; +use Slic3r::Geometry::Clipper qw(intersection_ex union_ex diff_ex); { my $square = [ # ccw @@ -26,29 +31,22 @@ use Math::Clipper ':all'; [25, 18], [5, 18], ]; - my $clipper = Math::Clipper->new; - $clipper->add_subject_polygons([ $square, $hole_in_square ]); - $clipper->add_clip_polygons([ $square2 ]); - my $intersection = $clipper->ex_execute(CT_INTERSECTION, PFT_NONZERO, PFT_NONZERO); + my $intersection = intersection_ex([ $square, $hole_in_square ], [ $square2 ]); - is_deeply $intersection, [ - { - holes => [ - [ - [14, 14], - [14, 16], - [16, 16], - [16, 14], - ], - ], - outer => [ - [20, 12], - [20, 18], - [10, 18], - [10, 12], - ], - }, - ], 'hole is preserved after intersection'; + is_deeply [ map $_->pp, @$intersection ], [[ + [ + [20, 12], + [20, 18], + [10, 18], + [10, 12], + ], + [ + [14, 14], + [14, 16], + [16, 16], + [16, 14], + ], + ]], 'hole is preserved after intersection'; } #========================================================== @@ -58,18 +56,13 @@ use Math::Clipper ':all'; my $contour2 = [ [10,10], [30,10], [30,30], [10,30] ]; # ccw my $hole = [ [15,15], [15,25], [25,25], [25,15] ]; # cw - my $clipper = Math::Clipper->new; - $clipper->add_subject_polygons([ $contour1, $contour2, $hole ]); - my $union = $clipper->ex_execute(CT_UNION, PFT_NONZERO, PFT_NONZERO); + my $union = union_ex([ $contour1, $contour2, $hole ]); - is_deeply $union, [{ holes => [], outer => [ [40,0], [40,40], [0,40], [0,0] ] }], + is_deeply [ map $_->pp, @$union ], [[ [ [40,0], [40,40], [0,40], [0,0] ] ]], 'union of two ccw and one cw is a contour with no holes'; - $clipper->clear; - $clipper->add_subject_polygons([ $contour1, $contour2 ]); - $clipper->add_clip_polygons([ $hole ]); - my $diff = $clipper->ex_execute(CT_DIFFERENCE, PFT_NONZERO, PFT_NONZERO); - is_deeply $diff, [{ holes => [[ [15,15], [15,25], [25,25], [25,15] ]], outer => [ [40,0], [40,40], [0,40], [0,0] ] }], + my $diff = diff_ex([ $contour1, $contour2 ], [ $hole ]); + is_deeply [ map $_->pp, @$diff ], [[ [ [40,0], [40,40], [0,40], [0,0] ], [ [15,15], [15,25], [25,25], [25,15] ] ]], 'difference of a cw from two ccw is a contour with one hole'; } diff --git a/t/collinear.t b/t/collinear.t index 5ca55fe78..9dee77700 100644 --- a/t/collinear.t +++ b/t/collinear.t @@ -16,9 +16,9 @@ use Slic3r::Geometry qw(collinear); { my @lines = ( - [ [0,4], [4,2] ], - [ [2,3], [8,0] ], - [ [6,1], [8,0] ], + Slic3r::Line->new([0,4], [4,2]), + Slic3r::Line->new([2,3], [8,0]), + Slic3r::Line->new([6,1], [8,0]), ); is collinear($lines[0], $lines[1]), 1, 'collinear'; is collinear($lines[1], $lines[2]), 1, 'collinear'; @@ -30,8 +30,8 @@ use Slic3r::Geometry qw(collinear); { # horizontal my @lines = ( - [ [0,1], [5,1] ], - [ [2,1], [8,1] ], + Slic3r::Line->new([0,1], [5,1]), + Slic3r::Line->new([2,1], [8,1]), ); is collinear($lines[0], $lines[1]), 1, 'collinear'; } @@ -41,8 +41,8 @@ use Slic3r::Geometry qw(collinear); { # vertical my @lines = ( - [ [1,0], [1,5] ], - [ [1,2], [1,8] ], + Slic3r::Line->new([1,0], [1,5]), + Slic3r::Line->new([1,2], [1,8]), ); is collinear($lines[0], $lines[1]), 1, 'collinear'; } @@ -52,8 +52,8 @@ use Slic3r::Geometry qw(collinear); { # non overlapping my @lines = ( - [ [0,1], [5,1] ], - [ [7,1], [10,1] ], + Slic3r::Line->new([0,1], [5,1]), + Slic3r::Line->new([7,1], [10,1]), ); is collinear($lines[0], $lines[1], 1), 0, 'non overlapping'; is collinear($lines[0], $lines[1], 0), 1, 'overlapping'; @@ -64,8 +64,8 @@ use Slic3r::Geometry qw(collinear); { # with one common point my @lines = ( - [ [0,4], [4,2] ], - [ [4,2], [8,0] ], + Slic3r::Line->new([0,4], [4,2]), + Slic3r::Line->new([4,2], [8,0]), ); is collinear($lines[0], $lines[1], 1), 1, 'one common point'; is collinear($lines[0], $lines[1], 0), 1, 'one common point'; @@ -76,8 +76,8 @@ use Slic3r::Geometry qw(collinear); { # not collinear my @lines = ( - [ [290000000,690525600], [285163380,684761540] ], - [ [285163380,684761540], [193267599,575244400] ], + Slic3r::Line->new([290000000,690525600], [285163380,684761540]), + Slic3r::Line->new([285163380,684761540], [193267599,575244400]), ); is collinear($lines[0], $lines[1], 0), 0, 'not collinear'; is collinear($lines[0], $lines[1], 1), 0, 'not collinear'; diff --git a/t/fill.t b/t/fill.t index 84678c301..d8d82b495 100644 --- a/t/fill.t +++ b/t/fill.t @@ -22,7 +22,7 @@ sub scale_points (@) { map [scale $_->[X], scale $_->[Y]], @_ } $print->init_extruders; my $filler = Slic3r::Fill::Rectilinear->new( print => $print, - bounding_box => Slic3r::Geometry::BoundingBox->new_from_points([ [0, 0], [10, 10] ]), + bounding_box => Slic3r::Geometry::BoundingBox->new_from_points([ Slic3r::Point->new(0, 0), Slic3r::Point->new(10, 10) ]), ); my $surface_width = 250; my $distance = $filler->adjust_solid_spacing( diff --git a/t/geometry.t b/t/geometry.t index 2421a057b..0c4550f42 100644 --- a/t/geometry.t +++ b/t/geometry.t @@ -177,7 +177,7 @@ is Slic3r::Geometry::can_connect_points(@$points, $polygons), 0, 'can_connect_po #========================================================== { - my $bb = Slic3r::Geometry::BoundingBox->new_from_points([ [0, 1], [10, 2], [20, 2] ]); + my $bb = Slic3r::Geometry::BoundingBox->new_from_points([ map Slic3r::Point->new(@$_), [0, 1], [10, 2], [20, 2] ]); $bb->scale(2); is_deeply $bb->extents, [ [0,40], [2,4] ], 'bounding box is scaled correctly'; } diff --git a/t/perimeters.t b/t/perimeters.t index 9ff53f01a..7fa0f3e34 100644 --- a/t/perimeters.t +++ b/t/perimeters.t @@ -33,7 +33,7 @@ use Slic3r::Test; push @$cur_loop, [ @$info{qw(new_X new_Y)} ]; } else { if ($cur_loop) { - $has_cw_loops = 1 if !Slic3r::Geometry::Clipper::is_counter_clockwise($cur_loop); + $has_cw_loops = 1 if Slic3r::Polygon->new(@$cur_loop)->is_clockwise; $cur_loop = undef; } } @@ -55,7 +55,7 @@ use Slic3r::Test; push @$cur_loop, [ @$info{qw(new_X new_Y)} ]; } else { if ($cur_loop) { - $has_cw_loops = 1 if !Slic3r::Geometry::Clipper::is_counter_clockwise($cur_loop); + $has_cw_loops = 1 if Slic3r::Polygon->new(@$cur_loop)->is_clockwise; if ($self->F == $config->external_perimeter_speed*60) { my $move_dest = Slic3r::Point->new_scale(@$info{qw(new_X new_Y)}); $external_loops{$self->Z}++; diff --git a/t/polyclip.t b/t/polyclip.t index 2cfe6951f..1711abfd1 100644 --- a/t/polyclip.t +++ b/t/polyclip.t @@ -9,7 +9,6 @@ BEGIN { use lib "$FindBin::Bin/../lib"; } -use Math::Clipper qw(is_counter_clockwise); use Slic3r; #========================================================== @@ -115,7 +114,7 @@ is_deeply $intersection, [ [120, 120], [180, 160] ], 'internal lines are preserv #========================================================== { - my $large_circle = [ # ccw + my $large_circle = Slic3r::Polygon->new( # ccw [151.8639,288.1192], [133.2778,284.6011], [115.0091,279.6997], [98.2859,270.8606], [82.2734,260.7933], [68.8974,247.4181], [56.5622,233.0777], [47.7228,216.3558], [40.1617,199.0172], [36.6431,180.4328], [34.932,165.2312], [37.5567,165.1101], [41.0547,142.9903], [36.9056,141.4295], [40.199,124.1277], @@ -125,10 +124,10 @@ is_deeply $intersection, [ [120, 120], [180, 160] ], 'internal lines are preserv [275.6832,106.6636], [281.9225,124.52], [286.8064,142.795], [287.5061,161.696], [286.7874,180.5972], [281.8856,198.8664], [275.6283,216.7169], [265.5604,232.7294], [254.3211,247.942], [239.9802,260.2776], [224.757,271.5022], [207.4179,279.0635], [189.5605,285.3035], [170.7649,287.4188], - ]; - is is_counter_clockwise($large_circle), 1, "contour is counter-clockwise"; + ); + ok $large_circle->is_counter_clockwise, "contour is counter-clockwise"; - my $small_circle = [ # cw + my $small_circle = Slic3r::Polygon->new( # cw [158.227,215.9007], [164.5136,215.9007], [175.15,214.5007], [184.5576,210.6044], [190.2268,207.8743], [199.1462,201.0306], [209.0146,188.346], [213.5135,177.4829], [214.6979,168.4866], [216.1025,162.3325], [214.6463,151.2703], [213.2471,145.1399], [209.0146,134.9203], [199.1462,122.2357], [189.8944,115.1366], @@ -136,8 +135,8 @@ is_deeply $intersection, [ [120, 120], [180, 160] ], 'internal lines are preserv [138.183,112.6616], [132.5135,115.3919], [123.5943,122.2357], [113.7259,134.92], [109.2269,145.7834], [108.0426,154.7799], [106.638,160.9339], [108.0941,171.9957], [109.4933,178.1264], [113.7259,188.3463], [123.5943,201.0306], [132.8461,208.1296], [141.4901,211.7094], [147.172,214.4458], - ]; - is is_counter_clockwise($small_circle), 0, "hole is clockwise"; + ); + ok $small_circle->is_clockwise, "hole is clockwise"; my $expolygon = Slic3r::ExPolygon->new($large_circle, $small_circle); $line = Slic3r::Line->new([152.742,288.086671142818], [152.742,34.166466971035]); diff --git a/xs/src/ClipperUtils.hpp b/xs/src/ClipperUtils.hpp index c907bbcd7..d49e526d6 100644 --- a/xs/src/ClipperUtils.hpp +++ b/xs/src/ClipperUtils.hpp @@ -6,6 +6,11 @@ #include "ExPolygon.hpp" #include "Polygon.hpp" +// import these wherever we're included +using ClipperLib::jtMiter; +using ClipperLib::jtRound; +using ClipperLib::jtSquare; + namespace Slic3r { #define CLIPPER_OFFSET_SCALE 100000.0 diff --git a/xs/src/Polygon.cpp b/xs/src/Polygon.cpp index 78b4b63b3..ddb3b5130 100644 --- a/xs/src/Polygon.cpp +++ b/xs/src/Polygon.cpp @@ -72,6 +72,12 @@ Polygon::is_counter_clockwise() const return orientation; } +bool +Polygon::is_clockwise() const +{ + return !this->is_counter_clockwise(); +} + bool Polygon::make_counter_clockwise() { diff --git a/xs/src/Polygon.hpp b/xs/src/Polygon.hpp index da913df19..004b655c5 100644 --- a/xs/src/Polygon.hpp +++ b/xs/src/Polygon.hpp @@ -18,6 +18,7 @@ class Polygon : public MultiPoint { Polyline* split_at_first_point(); double area() const; bool is_counter_clockwise() const; + bool is_clockwise() const; bool make_counter_clockwise(); bool make_clockwise(); bool is_valid() const; diff --git a/xs/xsp/Clipper.xsp b/xs/xsp/Clipper.xsp index e69873e07..67d6d6b20 100644 --- a/xs/xsp/Clipper.xsp +++ b/xs/xsp/Clipper.xsp @@ -10,6 +10,16 @@ %{ +IV +_constant() + ALIAS: + JT_MITER = jtMiter + JT_ROUND = jtRound + JT_SQUARE = jtSquare + CODE: + RETVAL = ix; + OUTPUT: RETVAL + Polygons offset(polygons, delta, scale = CLIPPER_OFFSET_SCALE, joinType = ClipperLib::jtMiter, miterLimit = 3) Polygons polygons diff --git a/xs/xsp/Polygon.xsp b/xs/xsp/Polygon.xsp index d2d09d08a..12adef184 100644 --- a/xs/xsp/Polygon.xsp +++ b/xs/xsp/Polygon.xsp @@ -25,6 +25,7 @@ %code{% const char* CLASS = "Slic3r::Polyline"; RETVAL = THIS->split_at_first_point(); %}; double area(); bool is_counter_clockwise(); + bool is_clockwise(); bool make_counter_clockwise(); bool make_clockwise(); bool is_valid();