Code optimization

This commit is contained in:
Alessandro Ranellucci 2012-02-25 22:15:34 +01:00
parent 170d29a789
commit c98f6734c7
5 changed files with 13 additions and 20 deletions

View File

@ -121,8 +121,7 @@ sub bounding_box_polygon {
sub clip_line { sub clip_line {
my $self = shift; my $self = shift;
my ($line) = @_; my ($line) = @_; # line must be a Slic3r::Line object
$line = Slic3r::Line->new($line) if ref $line eq 'ARRAY';
my @intersections = grep $_, map $_->intersection($line, 1), map $_->lines, @$self; my @intersections = grep $_, map $_->intersection($line, 1), map $_->lines, @$self;
my @dir = ( my @dir = (

View File

@ -38,7 +38,7 @@ sub fill_surface {
my $x = $bounding_box->[X1]; my $x = $bounding_box->[X1];
my $is_line_pattern = $self->isa('Slic3r::Fill::Line'); my $is_line_pattern = $self->isa('Slic3r::Fill::Line');
for (my $i = 0; $x <= $bounding_box->[X2] + scale epsilon; $i++) { 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) { if ($is_line_pattern && $i % 2) {
$vertical_line->[A][X] -= $line_oscillation; $vertical_line->[A][X] -= $line_oscillation;
$vertical_line->[B][X] += $line_oscillation; $vertical_line->[B][X] += $line_oscillation;

View File

@ -7,15 +7,7 @@ use Slic3r::Geometry qw(A B X Y);
sub new { sub new {
my $class = shift; my $class = shift;
my $self; my $self;
if (@_ == 2) { $self = [ @_ ];
$self = [ @_ ];
} elsif (ref $_[0] eq 'ARRAY') {
$self = [ $_[0][0], $_[0][1] ];
} elsif ($_[0]->isa(__PACKAGE__)) {
return $_[0];
} else {
die "Invalid argument for $class->new";
}
bless $self, $class; bless $self, $class;
bless $_, 'Slic3r::Point' for @$self; bless $_, 'Slic3r::Point' for @$self;
return $self; return $self;

View File

@ -32,7 +32,9 @@ sub clone {
sub lines { sub lines {
my $self = shift; 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 { sub cleanup {

View File

@ -30,7 +30,7 @@ my $square = [ # ccw
[10, 20], [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); my $intersection = Slic3r::Geometry::clip_segment_polygon($line, $square);
is_deeply $intersection, [ [10, 15], [20, 15] ], 'line is clipped to 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, 15]), 1, 'point on hole contour is recognized';
is $expolygon->encloses_point([14, 14]), 1, 'point on hole corner 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, [ is_deeply $intersections, [
[ [15, 18], [15, 16] ], [ [15, 18], [15, 16] ],
], 'line is clipped to square with hole'; ], '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, [ is_deeply $intersections, [
[ [15, 14], [15, 12] ], [ [15, 14], [15, 12] ],
], 'line is clipped to square with hole'; ], '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, [ is_deeply $intersections, [
[ [12,18], [18,18] ], [ [12,18], [18,18] ],
], 'line is clipped to square with hole'; ], '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'; ], '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, [ is_deeply $intersections, [
[ [20, 15], [16, 15] ], [ [20, 15], [16, 15] ],
[ [14, 15], [10, 15] ], [ [14, 15], [10, 15] ],
], 'reverse line is clipped to square with hole'; ], '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, [ is_deeply $intersections, [
[ [10, 18], [20, 18] ], [ [10, 18], [20, 18] ],
], 'tangent line is clipped to square with hole'; ], '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"; is is_counter_clockwise($small_circle), 0, "hole is clockwise";
my $expolygon = Slic3r::ExPolygon->new($large_circle, $small_circle); 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); my $intersections = $expolygon->clip_line($line);
is_deeply $intersections, [ is_deeply $intersections, [