<commit_before>package Slic3r::Fill::Concentric;
use Moo;

extends 'Slic3r::Fill::Base';

use Slic3r::Geometry qw(scale unscale X);
use Slic3r::Geometry::Clipper qw(offset offset2 union_pt traverse_pt);

sub fill_surface {
    my $self = shift;
    my ($surface, %params) = @_;
    
    # no rotation is supported for this infill pattern
    
    my $expolygon = $surface->expolygon;
    my $bounding_box = $expolygon->bounding_box;
    
    my $min_spacing = scale $params{flow_spacing};
    my $distance = $min_spacing / $params{density};
    
    my $flow_spacing = $params{flow_spacing};
    if ($params{density} == 1 && !$params{dont_adjust}) {
        $distance = $self->adjust_solid_spacing(
            width       => $bounding_box->size->[X],
            distance    => $distance,
        );
        $flow_spacing = unscale $distance;
    }
    
    # compensate the overlap which is good for rectilinear but harmful for concentric
    # where the perimeter/infill spacing should be equal to any other loop spacing
    my @loops = my @last = @{offset(\@$expolygon, -&Slic3r::INFILL_OVERLAP_OVER_SPACING * $min_spacing / 2)};
    while (@last) {
        push @loops, @last = @{offset2(\@last, -1.5*$distance,  +0.5*$distance)};
    }
    
    # generate paths from the outermost to the innermost, to avoid 
    # adhesion problems of the first central tiny loops
    @loops = map Slic3r::Polygon->new(@$_),
        reverse traverse_pt( union_pt(\@loops) );
    
    # order paths using a nearest neighbor search
    my @paths = ();
    my $last_pos = Slic3r::Point->new(0,0);
    foreach my $loop (@loops) {
        push @paths, $loop->split_at_index($last_pos->nearest_point_index(\@$loop));
        $last_pos = $paths[-1]->last_point;
    }
    
    # clip the paths to avoid the extruder to get exactly on the first point of the loop
    my $clip_length = scale $flow_spacing * &Slic3r::LOOP_CLIPPING_LENGTH_OVER_SPACING;
    $_->clip_end($clip_length) for @paths;
    
    # TODO: return ExtrusionLoop objects to get better chained paths
    return { flow_spacing => $flow_spacing, no_sort => 1 }, @paths;
}

1;<commit_msg>Bugfix: crashing when concentric infill produced very narrow loops. #1740<commit_after>package Slic3r::Fill::Concentric;
use Moo;

extends 'Slic3r::Fill::Base';

use Slic3r::Geometry qw(scale unscale X);
use Slic3r::Geometry::Clipper qw(offset offset2 union_pt traverse_pt);

sub fill_surface {
    my $self = shift;
    my ($surface, %params) = @_;
    
    # no rotation is supported for this infill pattern
    
    my $expolygon = $surface->expolygon;
    my $bounding_box = $expolygon->bounding_box;
    
    my $min_spacing = scale $params{flow_spacing};
    my $distance = $min_spacing / $params{density};
    
    my $flow_spacing = $params{flow_spacing};
    if ($params{density} == 1 && !$params{dont_adjust}) {
        $distance = $self->adjust_solid_spacing(
            width       => $bounding_box->size->[X],
            distance    => $distance,
        );
        $flow_spacing = unscale $distance;
    }
    
    # compensate the overlap which is good for rectilinear but harmful for concentric
    # where the perimeter/infill spacing should be equal to any other loop spacing
    my @loops = my @last = @{offset(\@$expolygon, -&Slic3r::INFILL_OVERLAP_OVER_SPACING * $min_spacing / 2)};
    while (@last) {
        push @loops, @last = @{offset2(\@last, -1.5*$distance,  +0.5*$distance)};
    }
    
    # generate paths from the outermost to the innermost, to avoid 
    # adhesion problems of the first central tiny loops
    @loops = map Slic3r::Polygon->new(@$_),
        reverse traverse_pt( union_pt(\@loops) );
    
    # order paths using a nearest neighbor search
    my @paths = ();
    my $last_pos = Slic3r::Point->new(0,0);
    foreach my $loop (@loops) {
        push @paths, $loop->split_at_index($last_pos->nearest_point_index(\@$loop));
        $last_pos = $paths[-1]->last_point;
    }
    
    # clip the paths to prevent the extruder from getting exactly on the first point of the loop
    my $clip_length = scale $flow_spacing * &Slic3r::LOOP_CLIPPING_LENGTH_OVER_SPACING;
    $_->clip_end($clip_length) for @paths;
    @paths = grep $_->is_valid, @paths;  # remove empty paths (too short, thus eaten by clipping)
    
    # TODO: return ExtrusionLoop objects to get better chained paths
    return { flow_spacing => $flow_spacing, no_sort => 1 }, @paths;
}

1;
{'fraction_non_alphanumeric': 0.12285843101893598, 'fraction_numerical': 0.010369702434625788, 'mean_word_length': 3.2017045454545454, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4797230', 'n_tokens_mistral': 1520, 'n_tokens_neox': 1449, 'n_words': 399}