diff --git a/lib/Slic3r/GCode.pm b/lib/Slic3r/GCode.pm index 38ce4ccb8..934388147 100644 --- a/lib/Slic3r/GCode.pm +++ b/lib/Slic3r/GCode.pm @@ -158,24 +158,40 @@ sub extrude_loop { if ($self->config->spiral_vase) { $loop->split_at($last_pos); } elsif ($self->config->seam_position eq 'nearest' || $self->config->seam_position eq 'aligned') { + # simplify polygon in order to skip false positives in concave/convex detection my $polygon = $loop->polygon; - my @candidates = @{$polygon->concave_points(PI*4/3)}; - @candidates = @{$polygon->convex_points(PI*2/3)} if !@candidates; - @candidates = @{$polygon} if !@candidates; + my @simplified = @{$polygon->simplify(scale $self->extruder->nozzle_diameter/2)}; - my @non_overhang = grep !$loop->has_overhang_point($_), @candidates; - @candidates = @non_overhang if @non_overhang; + # concave vertices have priority + my @candidates = map @{$_->concave_points(PI*4/3)}, @simplified; - if ($self->config->seam_position eq 'nearest') { - $loop->split_at_vertex($last_pos->nearest_point(\@candidates)); - } elsif ($self->config->seam_position eq 'aligned') { - my $obj_ptr = $self->layer->object->ptr; - if (defined $self->layer && defined $self->_seam_position->{$obj_ptr}) { + # if no concave points were found, look for convex vertices + @candidates = map @{$_->convex_points(PI*2/3)}, @simplified if !@candidates; + + # retrieve the last start position for this object + my $obj_ptr; + if (defined $self->layer) { + $obj_ptr = $self->layer->object->ptr; + if (defined $self->_seam_position->{$self->layer->object}) { $last_pos = $self->_seam_position->{$obj_ptr}; } - my $point = $self->_seam_position->{$obj_ptr} = $last_pos->nearest_point(\@candidates); - $loop->split_at_vertex($point); } + + my $point; + if ($self->config->seam_position eq 'nearest') { + @candidates = @$polygon if !@candidates; + $point = $last_pos->nearest_point(\@candidates); + $loop->split_at_vertex($point); + } elsif (@candidates) { + my @non_overhang = grep !$loop->has_overhang_point($_), @candidates; + @candidates = @non_overhang if @non_overhang; + $point = $last_pos->nearest_point(\@candidates); + $loop->split_at_vertex($point); + } else { + $point = $last_pos->projection_onto_polygon($polygon); + $loop->split_at($point); + } + $self->_seam_position->{$obj_ptr} = $point; } elsif ($self->config->seam_position eq 'random') { if ($loop->role == EXTRL_ROLE_CONTOUR_INTERNAL_PERIMETER) { my $polygon = $loop->polygon; diff --git a/lib/Slic3r/Polygon.pm b/lib/Slic3r/Polygon.pm index 01d72dd47..d3d8e6a8d 100644 --- a/lib/Slic3r/Polygon.pm +++ b/lib/Slic3r/Polygon.pm @@ -7,7 +7,7 @@ use parent 'Slic3r::Polyline'; use Slic3r::Geometry qw( polygon_segment_having_point - PI X1 X2 Y1 Y2 epsilon); + PI X1 X2 Y1 Y2 epsilon scaled_epsilon); use Slic3r::Geometry::Clipper qw(intersection_pl); sub wkt { @@ -54,18 +54,18 @@ sub concave_points { # but angle3points measures CCW angle, so we calculate the complementary angle my $ccw_angle = 2*PI-$angle; + my @concave = (); my @points = @$self; my @points_pp = @{$self->pp}; - - my @concave = (); + for my $i (-1 .. ($#points-1)) { - next if $points[$i-1]->coincides_with_epsilon($points[$i]) || $points[$i+1]->coincides_with_epsilon($points[$i]); # angle is measured in ccw orientation my $vertex_angle = Slic3r::Geometry::angle3points(@points_pp[$i, $i-1, $i+1]); if ($vertex_angle <= $ccw_angle) { push @concave, $points[$i]; } } + return [@concave]; } @@ -78,12 +78,11 @@ sub convex_points { # but angle3points measures CCW angle, so we calculate the complementary angle my $ccw_angle = 2*PI-$angle; + my @convex = (); my @points = @$self; my @points_pp = @{$self->pp}; - my @convex = (); for my $i (-1 .. ($#points-1)) { - next if $points[$i-1]->coincides_with_epsilon($points[$i]) || $points[$i+1]->coincides_with_epsilon($points[$i]); # angle is measured in ccw orientation my $vertex_angle = Slic3r::Geometry::angle3points(@points_pp[$i, $i-1, $i+1]); if ($vertex_angle >= $ccw_angle) {