From a950fbe0c2f7384fda38d0e6816a4406dcfc0e71 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Fri, 22 Nov 2013 16:19:15 +0100 Subject: [PATCH] Removed more unused functions and fixed tests --- lib/Slic3r/Geometry.pm | 72 +----------------------------------- lib/Slic3r/Polygon.pm | 17 +-------- lib/Slic3r/Polyline.pm | 12 +----- t/clean_polylines.t | 84 ++++++------------------------------------ t/geometry.t | 31 +--------------- t/polyclip.t | 10 +---- xs/xsp/Polygon.xsp | 2 +- 7 files changed, 20 insertions(+), 208 deletions(-) diff --git a/lib/Slic3r/Geometry.pm b/lib/Slic3r/Geometry.pm index 6c7283492..99a396743 100644 --- a/lib/Slic3r/Geometry.pm +++ b/lib/Slic3r/Geometry.pm @@ -16,9 +16,7 @@ our @EXPORT_OK = qw( dot perp polygon_points_visibility line_intersection bounding_box bounding_box_intersect angle3points three_points_aligned line_direction - polyline_remove_parallel_continuous_edges polyline_remove_acute_vertices - polygon_remove_acute_vertices polygon_remove_parallel_continuous_edges - chained_path collinear scale unscale merge_collinear_lines + chained_path collinear scale unscale rad2deg_dir bounding_box_center line_intersects_any douglas_peucker polyline_remove_short_segments normal triangle_normal polygon_is_convex scaled_epsilon bounding_box_3D size_3D size_2D @@ -370,40 +368,6 @@ sub collinear { return 1; } -sub merge_collinear_lines { - my ($lines) = @_; - my $line_count = @$lines; - - for (my $i = 0; $i <= $#$lines-1; $i++) { - for (my $j = $i+1; $j <= $#$lines; $j++) { - # lines are collinear and overlapping? - next unless collinear($lines->[$i], $lines->[$j], 1); - - # lines have same orientation? - next unless ($lines->[$i][A][X] <=> $lines->[$i][B][X]) == ($lines->[$j][A][X] <=> $lines->[$j][B][X]) - && ($lines->[$i][A][Y] <=> $lines->[$i][B][Y]) == ($lines->[$j][A][Y] <=> $lines->[$j][B][Y]); - - # resulting line - my @x = sort { $a <=> $b } ($lines->[$i][A][X], $lines->[$i][B][X], $lines->[$j][A][X], $lines->[$j][B][X]); - my @y = sort { $a <=> $b } ($lines->[$i][A][Y], $lines->[$i][B][Y], $lines->[$j][A][Y], $lines->[$j][B][Y]); - my $new_line = Slic3r::Line->new([$x[0], $y[0]], [$x[-1], $y[-1]]); - for (X, Y) { - ($new_line->[A][$_], $new_line->[B][$_]) = ($new_line->[B][$_], $new_line->[A][$_]) - if $lines->[$i][A][$_] > $lines->[$i][B][$_]; - } - - # save new line and remove found one - $lines->[$i] = $new_line; - splice @$lines, $j, 1; - $j--; - } - } - - Slic3r::debugf " merging %d lines resulted in %d lines\n", $line_count, scalar(@$lines); - - return $lines; -} - sub _line_intersection { my ( $x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3 ) = @_; @@ -596,40 +560,6 @@ sub angle3points { return $angle <= 0 ? $angle + 2*PI() : $angle; } -sub polyline_remove_parallel_continuous_edges { - my ($points, $isPolygon) = @_; - - for (my $i = $isPolygon ? 0 : 2; $i <= $#$points && @$points >= 3; $i++) { - if (Slic3r::Geometry::lines_parallel([$points->[$i-2], $points->[$i-1]], [$points->[$i-1], $points->[$i]])) { - # we can remove $points->[$i-1] - splice @$points, $i-1, 1; - $i--; - } - } -} - -sub polygon_remove_parallel_continuous_edges { - my ($points) = @_; - return polyline_remove_parallel_continuous_edges($points, 1); -} - -sub polyline_remove_acute_vertices { - my ($points, $isPolygon) = @_; - for (my $i = $isPolygon ? -1 : 1; $i < $#$points; $i++) { - my $angle = angle3points($points->[$i], $points->[$i-1], $points->[$i+1]); - if ($angle < 0.01 || $angle >= 2*PI - 0.01) { - # we can remove $points->[$i] - splice @$points, $i, 1; - $i--; - } - } -} - -sub polygon_remove_acute_vertices { - my ($points) = @_; - return polyline_remove_acute_vertices($points, 1); -} - sub polyline_remove_short_segments { my ($points, $min_length, $isPolygon) = @_; for (my $i = $isPolygon ? 0 : 1; $i < $#$points; $i++) { diff --git a/lib/Slic3r/Polygon.pm b/lib/Slic3r/Polygon.pm index 34ace2427..ce5001ef6 100644 --- a/lib/Slic3r/Polygon.pm +++ b/lib/Slic3r/Polygon.pm @@ -5,8 +5,8 @@ use warnings; # a polygon is a closed polyline. use parent 'Slic3r::Polyline'; -use Slic3r::Geometry qw(polygon_remove_parallel_continuous_edges - polygon_remove_acute_vertices polygon_segment_having_point +use Slic3r::Geometry qw( + polygon_segment_having_point PI X1 X2 Y1 Y2 epsilon); sub wkt { @@ -14,19 +14,6 @@ sub wkt { return sprintf "POLYGON((%s))", join ',', map "$_->[0] $_->[1]", @$self; } -sub merge_continuous_lines { - my $self = shift; - - my $p = $self->pp; - polygon_remove_parallel_continuous_edges($p); - return __PACKAGE__->new(@$p); -} - -sub remove_acute_vertices { - my $self = shift; - polygon_remove_acute_vertices($self); -} - sub grow { my $self = shift; return $self->split_at_first_point->grow(@_); diff --git a/lib/Slic3r/Polyline.pm b/lib/Slic3r/Polyline.pm index 185dee5dc..33234109f 100644 --- a/lib/Slic3r/Polyline.pm +++ b/lib/Slic3r/Polyline.pm @@ -2,7 +2,7 @@ package Slic3r::Polyline; use strict; use warnings; -use Slic3r::Geometry qw(A B X Y X1 X2 Y1 Y2 polyline_remove_parallel_continuous_edges polyline_remove_acute_vertices); +use Slic3r::Geometry qw(A B X Y X1 X2 Y1 Y2); use Slic3r::Geometry::Clipper qw(JT_SQUARE); use Storable qw(); @@ -17,16 +17,6 @@ sub wkt { return sprintf "LINESTRING((%s))", join ',', map "$_->[0] $_->[1]", @$self; } -sub merge_continuous_lines { - my $self = shift; - polyline_remove_parallel_continuous_edges($self); -} - -sub remove_acute_vertices { - my $self = shift; - polyline_remove_acute_vertices($self); -} - sub bounding_box { my $self = shift; return Slic3r::Geometry::BoundingBox->new_from_points($self); diff --git a/t/clean_polylines.t b/t/clean_polylines.t index 4ad905f68..4526bd60c 100644 --- a/t/clean_polylines.t +++ b/t/clean_polylines.t @@ -2,7 +2,7 @@ use Test::More; use strict; use warnings; -plan tests => 10; +plan tests => 6; BEGIN { use FindBin; @@ -11,15 +11,6 @@ BEGIN { use Slic3r; -{ - my $polygon = Slic3r::Polygon->new( - [5,0], [10,0], [15,0], [20,0], [20,10], [20,30], [0,0], - ); - - $polygon = $polygon->merge_continuous_lines; - is scalar(@$polygon), 3, 'merge_continuous_lines'; -} - { my $polyline = Slic3r::Polyline->new( [0,0],[1,0],[2,0],[2,1],[2,2],[1,2],[0,2],[0,1],[0,0], @@ -37,7 +28,7 @@ use Slic3r; } { - my $gear = [ + my $gear = Slic3r::Polygon->new_scale( [144.9694,317.1543], [145.4181,301.5633], [146.3466,296.921], [131.8436,294.1643], [131.7467,294.1464], [121.7238,291.5082], [117.1631,290.2776], [107.9198,308.2068], [100.1735,304.5101], [104.9896,290.3672], [106.6511,286.2133], [93.453,279.2327], [81.0065,271.4171], [67.7886,286.5055], [60.7927,280.1127], @@ -67,76 +58,25 @@ use Slic3r; [234.9061,298.5798], [227.0321,286.2841], [225.2505,281.8301], [211.5387,287.8187], [202.3025,291.0935], [197.307,292.831], [199.808,313.1906], [191.5298,315.0787], [187.3082,299.8172], [186.4201,295.3766], [180.595,296.0487], [161.7854,297.4248], [156.8058,297.6214], [154.3395,317.8592], - ]; - my $polygon = Slic3r::Polygon->new(@$gear); - $polygon = $polygon->merge_continuous_lines; - note sprintf "original points: %d\nnew points: %d", scalar(@$gear), scalar(@$polygon); - ok @$polygon < @$gear, 'gear was simplified using merge_continuous_lines'; - - my $num_points = scalar @$polygon; - my @simplified = $polygon->simplify; - ok @simplified == 1, 'gear simplified to a single polygon'; - note sprintf "original points: %d\nnew points: %d", $num_points, scalar(@{$simplified[0]}); - ok @{$simplified[0]} < $num_points, 'gear was further simplified using Douglas-Peucker'; + ); - my @simplified_ex = Slic3r::ExPolygon->new($polygon)->simplify(10); - is_deeply [ map $_->pp, @simplified_ex ], [ [ map $_->pp, @simplified ] ], 'simplified polygon equals simplified expolygon'; + my $num_points = scalar @$gear; + my $simplified = $gear->simplify(1000); + ok @$simplified == 1, 'gear simplified to a single polygon'; + ###note sprintf "original points: %d\nnew points: %d", $num_points, scalar(@{$simplified->[0]}); + ok @{$simplified->[0]} < $num_points, 'gear was further simplified using Douglas-Peucker'; } { - my $square = Slic3r::Polygon->new( # ccw - [100, 100], - [200, 100], - [200, 200], - [100, 200], - ); + my $hole_in_square = Slic3r::Polygon->new( # cw [140, 140], [140, 160], [160, 160], [160, 140], ); - my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square); - my @simplified = $hole_in_square->simplify; - is scalar(@simplified), 1, 'hole simplification returns one polygon'; - ok $simplified[0]->is_counter_clockwise, 'hole simplification turns cw polygon into ccw polygon'; + my $simplified = $hole_in_square->simplify(2); + is scalar(@$simplified), 1, 'hole simplification returns one polygon'; + ok $simplified->[0]->is_counter_clockwise, 'hole simplification turns cw polygon into ccw polygon'; } -{ - my $circle = [ - [3744.8,8045.8],[3788.1,8061.4],[3940.6,8116.3],[3984.8,8129.2],[4140.6,8174.4],[4185.5,8184.4],[4343.8,8219.9], - [4389.2,8227.1],[4549.4,8252.4],[4595.2,8256.7],[4756.6,8272],[4802.6,8273.4],[4964.7,8278.5],[5010.7,8277.1], - [5172.8,8272],[5218.6,8267.7],[5380,8252.4],[5425.5,8245.2],[5585.6,8219.9],[5630.5,8209.8],[5788.8,8174.4], - [5833,8161.6],[5988.8,8116.3],[6032,8100.7],[6184.6,8045.8],[6226.8,8027.5],[6375.6,7963.2],[6416.6,7942.3], - [6561.1,7868.6],[6600.7,7845.2],[6740.3,7762.7],[6778.4,7736.8],[6912.5,7645.7],[6948.8,7617.5],[7077,7518], - [7111.5,7487.6],[7233.2,7380.4],[7347.9,7265.7],[7380.4,7233.2],[7410.8,7198.7],[7518,7077],[7546.2,7040.6], - [7645.7,6912.5],[7671.5,6874.4],[7762.7,6740.3],[7786.1,6700.7],[7868.6,6561.1],[7889.5,6520.2],[7963.2,6375.6], - [7981.4,6333.4],[8045.8,6184.6],[8061.4,6141.3],[8116.3,5988.8],[8129.2,5944.6],[8174.4,5788.8],[8184.4,5743.9], - [8219.9,5585.6],[8227.1,5540.2],[8252.4,5380],[8256.7,5334.2],[8272,5172.8],[8273.4,5126.8],[8278.5,4964.7], - [8277.1,4918.7],[8272,4756.6],[8267.7,4710.8],[8252.4,4549.4],[8245.2,4503.9],[8219.9,4343.8],[8209.8,4298.9], - [8174.4,4140.6],[8161.6,4096.4],[8116.3,3940.6],[8100.7,3897.4],[8045.8,3744.8],[8027.5,3702.6],[7963.2,3553.8], - [7942.3,3512.8],[7868.6,3368.3],[7845.2,3328.7],[7762.7,3189.1],[7736.8,3151],[7645.7,3016.9],[7617.5,2980.6], - [7518,2852.4],[7487.6,2817.9],[7380.4,2696.2],[7347.9,2663.7],[7233.2,2549],[7198.7,2518.6],[7077,2411.4], - [7040.6,2383.2],[6912.5,2283.7],[6874.4,2257.9],[6740.3,2166.7],[6700.7,2143.3],[6561.1,2060.8],[6520.2,2039.9], - [6375.6,1966.2],[6333.4,1948],[6184.6,1883.6],[6141.3,1868],[5988.8,1813.1],[5944.6,1800.2],[5788.8,1755], - [5743.9,1745],[5585.6,1709.5],[5540.2,1702.3],[5380,1677],[5334.2,1672.7],[5172.8,1657.4],[5126.8,1656], - [4964.7,1650.9],[4918.7,1652.3],[4756.6,1657.4],[4710.8,1661.7],[4549.4,1677],[4503.9,1684.2],[4343.8,1709.5], - [4298.9,1719.6],[4140.6,1755],[4096.4,1767.8],[3940.6,1813.1],[3897.4,1828.7],[3744.8,1883.6],[3702.6,1901.9], - [3553.8,1966.2],[3512.8,1987.1],[3368.3,2060.8],[3328.7,2084.2],[3189.1,2166.7],[3151,2192.6],[3016.9,2283.7], - [2980.6,2311.9],[2852.4,2411.4],[2817.9,2441.8],[2696.2,2549],[2581.5,2663.7],[2549,2696.2],[2518.6,2730.7], - [2411.4,2852.4],[2383.2,2888.8],[2283.7,3016.9],[2257.9,3055],[2166.7,3189.1],[2143.3,3228.7],[2060.8,3368.3], - [2039.9,3409.2],[1966.2,3553.8],[1948,3596],[1883.6,3744.8],[1868,3788.1],[1813.1,3940.6],[1800.2,3984.8], - [1755,4140.6],[1745,4185.5],[1709.5,4343.8],[1702.3,4389.2],[1677,4549.4],[1672.7,4595.2],[1657.4,4756.6], - [1656,4802.6],[1650.9,4964.7],[1652.3,5010.7],[1657.4,5172.8],[1661.7,5218.6],[1677,5380],[1684.2,5425.5], - [1709.5,5585.6],[1719.6,5630.5],[1755,5788.8],[1767.8,5833],[1813.1,5988.8],[1828.7,6032],[1883.6,6184.6], - [1901.9,6226.8],[1966.2,6375.6],[1987.1,6416.6],[2060.8,6561.1],[2084.2,6600.7],[2166.7,6740.3],[2192.6,6778.4], - [2283.7,6912.5],[2311.9,6948.8],[2411.4,7077],[2441.8,7111.5],[2549,7233.2],[2581.5,7265.7],[2696.2,7380.4], - [2730.7,7410.8],[2852.4,7518],[2888.8,7546.2],[3016.9,7645.7],[3055,7671.5],[3189.1,7762.7],[3228.7,7786.1], - [3368.3,7868.6],[3409.2,7889.5],[3553.8,7963.2],[3596,7981.4], - ]; - - my $polygon = Slic3r::Polygon->new(@$circle); - $polygon = $polygon->merge_continuous_lines; - note sprintf "original points: %d\nnew points: %d", scalar(@$circle), scalar(@$polygon); - ok @$polygon >= @$circle/3, 'circle was simplified using merge_continuous_lines'; -} diff --git a/t/geometry.t b/t/geometry.t index 4976f5a09..4edb63dae 100644 --- a/t/geometry.t +++ b/t/geometry.t @@ -2,7 +2,7 @@ use Test::More; use strict; use warnings; -plan tests => 25; +plan tests => 23; BEGIN { use FindBin; @@ -10,9 +10,7 @@ BEGIN { } use Slic3r; -use Slic3r::Geometry qw(PI polyline_remove_parallel_continuous_edges - polyline_remove_acute_vertices polygon_remove_acute_vertices - polygon_remove_parallel_continuous_edges polygon_is_convex +use Slic3r::Geometry qw(PI polygon_is_convex chained_path_points epsilon scale); #========================================================== @@ -115,31 +113,6 @@ my $polygons = [ #========================================================== -{ - my $polygon = [ - [2265447881, 7013509857], [2271869937, 7009802077], [2606221146, 6816764300], [1132221146, 4263721402], - [1098721150, 4205697705], [1228411320, 4130821051], [1557501031, 3940821051], [1737340080, 3836990933], - [1736886253, 3837252951], [1494771522, 3977037948], [2959638603, 6514262167], [3002271522, 6588104548], - [3083252364, 6541350240], [2854283608, 6673545411], [2525193897, 6863545411], - ]; - polygon_remove_parallel_continuous_edges($polygon); - polygon_remove_acute_vertices($polygon); - is scalar(@$polygon), 4, 'polygon_remove_acute_vertices'; -} - -#========================================================== - -{ - my $polygon = [ - [226.5447881,701.3509857], [260.6221146,681.67643], [109.872115,420.5697705], [149.4771522,397.7037948], - [300.2271522,658.8104548], [308.3252364,654.135024], - ]; - polyline_remove_acute_vertices($polygon); - is scalar(@$polygon), 6, 'polyline_remove_acute_vertices'; -} - -#========================================================== - { my $cw_square = [ [0,0], [0,10], [10,10], [10,0] ]; is polygon_is_convex($cw_square), 0, 'cw square is not convex'; diff --git a/t/polyclip.t b/t/polyclip.t index 6ed0028a1..1292d3216 100644 --- a/t/polyclip.t +++ b/t/polyclip.t @@ -2,7 +2,7 @@ use Test::More; use strict; use warnings; -plan tests => 19; +plan tests => 18; BEGIN { use FindBin; @@ -78,14 +78,6 @@ my $square = Slic3r::Polygon->new( # ccw is $intersection->[0]->length, Slic3r::Line->new([100,180], [200,180])->length, 'tangent line is clipped to square with hole'; } - { - my $polyline = Slic3r::Polyline->new([50, 180], [250, 180], [250, 150], [150, 150], [150, 120], [120, 120], [120, 50]); - is_deeply [ map $_->pp, $polyline->clip_with_expolygon($expolygon) ], [ - [ [100, 180], [200, 180] ], - [ [200, 150], [160, 150] ], - [ [150, 140], [150, 120], [120, 120], [120, 100] ], - ], 'polyline is clipped to square with hole'; - } } #========================================================== diff --git a/xs/xsp/Polygon.xsp b/xs/xsp/Polygon.xsp index 363cac38b..7d2a364eb 100644 --- a/xs/xsp/Polygon.xsp +++ b/xs/xsp/Polygon.xsp @@ -34,7 +34,7 @@ Point* first_point() %code{% const char* CLASS = "Slic3r::Point"; RETVAL = THIS->first_point(); %}; bool contains_point(Point* point); - void simplify(double tolerance); + Polygons simplify(double tolerance); %{ Polygon*