From cec7bf815c47354121640f14463941a1b88bb187 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Tue, 30 Oct 2012 15:29:13 +0100 Subject: [PATCH] Get rid of the ExtrusionLoop objects in concentric filler --- lib/Slic3r/ExtrusionLoop.pm | 36 ++++++++--------------------------- lib/Slic3r/ExtrusionPath.pm | 22 +-------------------- lib/Slic3r/Fill/Concentric.pm | 8 ++++---- lib/Slic3r/Polygon.pm | 27 ++++++++++++++++++++++++++ lib/Slic3r/Polyline.pm | 20 +++++++++++++++++++ 5 files changed, 60 insertions(+), 53 deletions(-) diff --git a/lib/Slic3r/ExtrusionLoop.pm b/lib/Slic3r/ExtrusionLoop.pm index 0a950eaf..44a77920 100644 --- a/lib/Slic3r/ExtrusionLoop.pm +++ b/lib/Slic3r/ExtrusionLoop.pm @@ -37,36 +37,22 @@ sub pack { sub split_at_index { my $self = shift; - my ($index) = @_; - - my @new_points = (); - push @new_points, @{$self->polygon}[$index .. $#{$self->polygon}]; - push @new_points, @{$self->polygon}[0 .. $index]; return Slic3r::ExtrusionPath->new( - polyline => Slic3r::Polyline->new(\@new_points), - role => $self->role, - flow_spacing => $self->flow_spacing, + polyline => $self->polygon->split_at_index(@_), + role => $self->role, + flow_spacing => $self->flow_spacing, ); } sub split_at { my $self = shift; - my ($point) = @_; - $point = Slic3r::Point->new($point); - - # find index of point - my $i = -1; - for (my $n = 0; $n <= $#{$self->polygon}; $n++) { - if (same_point($point, $self->polygon->[$n])) { - $i = $n; - last; - } - } - die "Point not found" if $i == -1; - - return $self->split_at_index($i); + return Slic3r::ExtrusionPath->new( + polyline => $self->polygon->split_at(@_), + role => $self->role, + flow_spacing => $self->flow_spacing, + ); } sub split_at_first_point { @@ -82,12 +68,6 @@ sub endpoints { return ($self->polygon->[0], $self->polygon->[-1]); } -# provided for ExtrusionPath::Collection->shortest_path() -sub points { - my $self = shift; - return $self->polygon; -} - package Slic3r::ExtrusionLoop::Packed; sub unpack { my $self = shift; diff --git a/lib/Slic3r/ExtrusionPath.pm b/lib/Slic3r/ExtrusionPath.pm index c55cd02a..bae6e61c 100644 --- a/lib/Slic3r/ExtrusionPath.pm +++ b/lib/Slic3r/ExtrusionPath.pm @@ -15,7 +15,7 @@ use Slic3r::Geometry qw(PI X Y epsilon deg2rad rotate_points); has 'polyline' => ( is => 'rw', required => 1, - handles => [qw(merge_continuous_lines lines length reverse)], + handles => [qw(merge_continuous_lines lines length reverse clip_end)], ); # height is the vertical thickness of the extrusion expressed in mm @@ -58,26 +58,6 @@ sub pack { # no-op, this allows to use both packed and non-packed objects in Collections sub unpack { $_[0] } -sub clip_end { - my $self = shift; - my ($distance) = @_; - - while ($distance > 0) { - my $last_point = pop @{$self->points}; - last if !@{$self->points}; - - my $last_segment_length = $last_point->distance_to($self->points->[-1]); - if ($last_segment_length <= $distance) { - $distance -= $last_segment_length; - next; - } - - my $new_point = Slic3r::Geometry::point_along_segment($last_point, $self->points->[-1], $distance); - push @{$self->points}, Slic3r::Point->new($new_point); - $distance = 0; - } -} - sub clip_with_polygon { my $self = shift; my ($polygon) = @_; diff --git a/lib/Slic3r/Fill/Concentric.pm b/lib/Slic3r/Fill/Concentric.pm index c64548e4..fe508eb9 100644 --- a/lib/Slic3r/Fill/Concentric.pm +++ b/lib/Slic3r/Fill/Concentric.pm @@ -51,13 +51,13 @@ sub fill_surface { ($bounding_box->[X1] + $bounding_box->[X2]) / 2, ($bounding_box->[Y1] + $bounding_box->[Y2]) / 2, ); - foreach my $loop (map Slic3r::ExtrusionLoop->new(polygon => $_, role => EXTR_ROLE_FILL), @loops) { + foreach my $loop (@loops) { # extrude all loops ccw - $loop->polygon->make_counter_clockwise; + $loop->make_counter_clockwise; # find the point of the loop that is closest to the current extruder position my $index = $loop->nearest_point_index_to($cur_pos); - $cur_pos = $loop->polygon->[0]; + $cur_pos = $loop->[0]; # split the loop at the starting point and make a path my $path = $loop->split_at_index($index); @@ -65,7 +65,7 @@ sub fill_surface { # clip the path to avoid the extruder to get exactly on the first point of the loop $path->clip_end(scale $flow_spacing * &Slic3r::LOOP_CLIPPING_LENGTH_OVER_SPACING); - push @paths, $path->points if @{$path->points}; + push @paths, $path if @$path; } return { flow_spacing => $flow_spacing }, @paths; diff --git a/lib/Slic3r/Polygon.pm b/lib/Slic3r/Polygon.pm index ee6f193d..18f840a4 100644 --- a/lib/Slic3r/Polygon.pm +++ b/lib/Slic3r/Polygon.pm @@ -119,4 +119,31 @@ sub is_valid { return @$self >= 3; } +sub split_at_index { + my $self = shift; + my ($index) = @_; + + return (ref $self)->new( + @$self[$index .. $#$self], + @$self[0 .. $index], + ); +} + +sub split_at { + my $self = shift; + my ($point) = @_; + + # find index of point + my $i = -1; + for (my $n = 0; $n <= $#$self; $n++) { + if (Slic3r::Geometry::same_point($point, $self->[$n])) { + $i = $n; + last; + } + } + die "Point not found" if $i == -1; + + return $self->split_at_index($i); +} + 1; \ No newline at end of file diff --git a/lib/Slic3r/Polyline.pm b/lib/Slic3r/Polyline.pm index 3ec90b5d..0813822a 100644 --- a/lib/Slic3r/Polyline.pm +++ b/lib/Slic3r/Polyline.pm @@ -181,6 +181,26 @@ sub scale { return $self; } +sub clip_end { + my $self = shift; + my ($distance) = @_; + + while ($distance > 0) { + my $last_point = pop @$self; + last if !@$self; + + my $last_segment_length = $last_point->distance_to($self->[-1]); + if ($last_segment_length <= $distance) { + $distance -= $last_segment_length; + next; + } + + my $new_point = Slic3r::Geometry::point_along_segment($last_point, $self->[-1], $distance); + push @$self, Slic3r::Point->new($new_point); + $distance = 0; + } +} + package Slic3r::Polyline::Collection; use Moo;