diff --git a/lib/Slic3r/ExtrusionLoop.pm b/lib/Slic3r/ExtrusionLoop.pm index adaa61210..ac79c8d0a 100644 --- a/lib/Slic3r/ExtrusionLoop.pm +++ b/lib/Slic3r/ExtrusionLoop.pm @@ -5,7 +5,7 @@ use Moo; has 'polygon' => ( is => 'rw', required => 1, - handles => [qw(is_printable nearest_point_to reverse)], + handles => [qw(is_printable nearest_point_index_to reverse)], ); has 'flow_spacing' => (is => 'rw'); @@ -29,6 +29,23 @@ sub shortest_path { return $self; } +sub split_at_index { + my $self = shift; + my ($index) = @_; + + $self->deserialize; + + 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, + ); +} + sub split_at { my $self = shift; my ($point) = @_; @@ -47,21 +64,12 @@ sub split_at { } die "Point not found" if $i == -1; - my @new_points = (); - push @new_points, @{$self->polygon}[$i .. $#{$self->polygon}]; - push @new_points, @{$self->polygon}[0 .. $i]; - - return Slic3r::ExtrusionPath->new( - polyline => Slic3r::Polyline->new(\@new_points), - role => $self->role, - flow_spacing => $self->flow_spacing, - ); + return $self->split_at_index($i); } sub split_at_first_point { my $self = shift; - $self->deserialize; - return $self->split_at($self->polygon->[0]); + return $self->split_at_index(0); } # although a loop doesn't have endpoints, this method is provided to allow diff --git a/lib/Slic3r/Fill/Concentric.pm b/lib/Slic3r/Fill/Concentric.pm index 920f00ac2..71c19db48 100644 --- a/lib/Slic3r/Fill/Concentric.pm +++ b/lib/Slic3r/Fill/Concentric.pm @@ -57,10 +57,11 @@ sub fill_surface { $loop->polygon->make_counter_clockwise; # find the point of the loop that is closest to the current extruder position - $cur_pos = $loop->nearest_point_to($cur_pos); + my $index = $loop->nearest_point_index_to($cur_pos); + $cur_pos = $loop->polygon->[0]; # split the loop at the starting point and make a path - my $path = $loop->split_at($cur_pos); + my $path = $loop->split_at_index($index); $path->deserialize; # clip the path to avoid the extruder to get exactly on the first point of the loop diff --git a/lib/Slic3r/GCode.pm b/lib/Slic3r/GCode.pm index f8a96e93c..f3afcd985 100644 --- a/lib/Slic3r/GCode.pm +++ b/lib/Slic3r/GCode.pm @@ -103,10 +103,10 @@ sub extrude_loop { $last_pos = Slic3r::Point->new(scale $Slic3r::print_center->[X], scale $Slic3r::bed_size->[Y]); $last_pos->rotate(rand(2*PI), $Slic3r::print_center); } - my $start_at = $loop->nearest_point_to($last_pos); + my $start_index = $loop->nearest_point_index_to($last_pos); # split the loop at the starting point and make a path - my $extrusion_path = $loop->split_at($start_at); + my $extrusion_path = $loop->split_at_index($start_index); $extrusion_path->deserialize; # clip the path to avoid the extruder to get exactly on the first point of the loop; diff --git a/lib/Slic3r/Polyline.pm b/lib/Slic3r/Polyline.pm index caef9beee..e7c2c61c9 100644 --- a/lib/Slic3r/Polyline.pm +++ b/lib/Slic3r/Polyline.pm @@ -97,6 +97,12 @@ sub nearest_point_to { return Slic3r::Point->new($point); } +sub nearest_point_index_to { + my $self = shift; + my ($point) = @_; + return Slic3r::Geometry::nearest_point_index($point, $self); +} + sub has_segment { my $self = shift; my ($line) = @_;