Removed dependency on Math::Clipper

This commit is contained in:
Alessandro Ranellucci 2013-08-27 01:26:44 +02:00
parent b11b595c97
commit fb763b0187
21 changed files with 82 additions and 76 deletions

View file

@ -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';
}

View file

@ -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';

View file

@ -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(

View file

@ -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';
}

View file

@ -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}++;

View file

@ -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]);