From c98f6734c7ccf5a7522edef8da90363a9e0b38c2 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Sat, 25 Feb 2012 22:15:34 +0100 Subject: [PATCH] Code optimization --- lib/Slic3r/ExPolygon.pm | 3 +-- lib/Slic3r/Fill/Rectilinear.pm | 2 +- lib/Slic3r/Line.pm | 10 +--------- lib/Slic3r/Polygon.pm | 4 +++- t/polyclip.t | 14 +++++++------- 5 files changed, 13 insertions(+), 20 deletions(-) diff --git a/lib/Slic3r/ExPolygon.pm b/lib/Slic3r/ExPolygon.pm index 69c9b29cc..ae11781a1 100644 --- a/lib/Slic3r/ExPolygon.pm +++ b/lib/Slic3r/ExPolygon.pm @@ -121,8 +121,7 @@ sub bounding_box_polygon { sub clip_line { my $self = shift; - my ($line) = @_; - $line = Slic3r::Line->new($line) if ref $line eq 'ARRAY'; + my ($line) = @_; # line must be a Slic3r::Line object my @intersections = grep $_, map $_->intersection($line, 1), map $_->lines, @$self; my @dir = ( diff --git a/lib/Slic3r/Fill/Rectilinear.pm b/lib/Slic3r/Fill/Rectilinear.pm index d264abb1a..990ff401c 100644 --- a/lib/Slic3r/Fill/Rectilinear.pm +++ b/lib/Slic3r/Fill/Rectilinear.pm @@ -38,7 +38,7 @@ sub fill_surface { my $x = $bounding_box->[X1]; my $is_line_pattern = $self->isa('Slic3r::Fill::Line'); for (my $i = 0; $x <= $bounding_box->[X2] + scale epsilon; $i++) { - my $vertical_line = [ [$x, $bounding_box->[Y2]], [$x, $bounding_box->[Y1]] ]; + my $vertical_line = Slic3r::Line->new([$x, $bounding_box->[Y2]], [$x, $bounding_box->[Y1]]); if ($is_line_pattern && $i % 2) { $vertical_line->[A][X] -= $line_oscillation; $vertical_line->[B][X] += $line_oscillation; diff --git a/lib/Slic3r/Line.pm b/lib/Slic3r/Line.pm index 494eae4d6..d636f4203 100644 --- a/lib/Slic3r/Line.pm +++ b/lib/Slic3r/Line.pm @@ -7,15 +7,7 @@ use Slic3r::Geometry qw(A B X Y); sub new { my $class = shift; my $self; - if (@_ == 2) { - $self = [ @_ ]; - } elsif (ref $_[0] eq 'ARRAY') { - $self = [ $_[0][0], $_[0][1] ]; - } elsif ($_[0]->isa(__PACKAGE__)) { - return $_[0]; - } else { - die "Invalid argument for $class->new"; - } + $self = [ @_ ]; bless $self, $class; bless $_, 'Slic3r::Point' for @$self; return $self; diff --git a/lib/Slic3r/Polygon.pm b/lib/Slic3r/Polygon.pm index d577a2417..2d7f636d1 100644 --- a/lib/Slic3r/Polygon.pm +++ b/lib/Slic3r/Polygon.pm @@ -32,7 +32,9 @@ sub clone { sub lines { my $self = shift; - return map Slic3r::Line->new($_), polygon_lines($self); + my @lines = polygon_lines($self); + bless $_, 'Slic3r::Line' for @lines; + return @lines; } sub cleanup { diff --git a/t/polyclip.t b/t/polyclip.t index 4b3006e1a..12352bf4e 100644 --- a/t/polyclip.t +++ b/t/polyclip.t @@ -30,7 +30,7 @@ my $square = [ # ccw [10, 20], ]; -my $line = [ [5, 15], [30, 15] ]; +my $line = Slic3r::Line->new([5, 15], [30, 15]); my $intersection = Slic3r::Geometry::clip_segment_polygon($line, $square); is_deeply $intersection, [ [10, 15], [20, 15] ], 'line is clipped to square'; @@ -65,19 +65,19 @@ is_deeply $intersection, [ [12, 12], [18, 16] ], 'internal lines are preserved'; is $expolygon->encloses_point([14, 15]), 1, 'point on hole contour is recognized'; is $expolygon->encloses_point([14, 14]), 1, 'point on hole corner is recognized'; { - my $intersections = $expolygon->clip_line([ [15,18], [15,15] ]); + my $intersections = $expolygon->clip_line(Slic3r::Line->new([15,18], [15,15])); is_deeply $intersections, [ [ [15, 18], [15, 16] ], ], 'line is clipped to square with hole'; } { - my $intersections = $expolygon->clip_line([ [15,15], [15,12] ]); + my $intersections = $expolygon->clip_line(Slic3r::Line->new([15,15], [15,12])); is_deeply $intersections, [ [ [15, 14], [15, 12] ], ], 'line is clipped to square with hole'; } { - my $intersections = $expolygon->clip_line([ [12,18], [18,18] ]); + my $intersections = $expolygon->clip_line(Slic3r::Line->new([12,18], [18,18])); is_deeply $intersections, [ [ [12,18], [18,18] ], ], 'line is clipped to square with hole'; @@ -90,14 +90,14 @@ is_deeply $intersection, [ [12, 12], [18, 16] ], 'internal lines are preserved'; ], 'line is clipped to square with hole'; } { - my $intersections = $expolygon->clip_line([ reverse @$line ]); + my $intersections = $expolygon->clip_line(Slic3r::Line->new(reverse @$line)); is_deeply $intersections, [ [ [20, 15], [16, 15] ], [ [14, 15], [10, 15] ], ], 'reverse line is clipped to square with hole'; } { - my $intersections = $expolygon->clip_line([ [10,18], [20,18] ]); + my $intersections = $expolygon->clip_line(Slic3r::Line->new([10,18], [20,18])); is_deeply $intersections, [ [ [10, 18], [20, 18] ], ], 'tangent line is clipped to square with hole'; @@ -140,7 +140,7 @@ is_deeply $intersection, [ [12, 12], [18, 16] ], 'internal lines are preserved'; is is_counter_clockwise($small_circle), 0, "hole is clockwise"; my $expolygon = Slic3r::ExPolygon->new($large_circle, $small_circle); - $line = [ [152.741724,288.086671142818], [152.741724,34.166466971035] ]; + $line = Slic3r::Line->new([152.741724,288.086671142818], [152.741724,34.166466971035]); my $intersections = $expolygon->clip_line($line); is_deeply $intersections, [