Browse Source

Code optimization

Alessandro Ranellucci 13 years ago
parent
commit
c98f6734c7
5 changed files with 13 additions and 20 deletions
  1. 1 2
      lib/Slic3r/ExPolygon.pm
  2. 1 1
      lib/Slic3r/Fill/Rectilinear.pm
  3. 1 9
      lib/Slic3r/Line.pm
  4. 3 1
      lib/Slic3r/Polygon.pm
  5. 7 7
      t/polyclip.t

+ 1 - 2
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 = (

+ 1 - 1
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;

+ 1 - 9
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;

+ 3 - 1
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 {

+ 7 - 7
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, [