Faster algorithm for sorting perimeter loops

This commit is contained in:
Alessandro Ranellucci 2015-01-15 17:42:39 +01:00
parent 56853319d1
commit 24e8307e68

View File

@ -5,7 +5,7 @@ use Slic3r::ExtrusionLoop ':roles';
use Slic3r::ExtrusionPath ':roles'; use Slic3r::ExtrusionPath ':roles';
use Slic3r::Geometry qw(scale unscale chained_path); use Slic3r::Geometry qw(scale unscale chained_path);
use Slic3r::Geometry::Clipper qw(union_ex diff diff_ex intersection_ex offset offset2 use Slic3r::Geometry::Clipper qw(union_ex diff diff_ex intersection_ex offset offset2
offset_ex offset2_ex union_pt intersection_ppl diff_ppl); offset_ex offset2_ex intersection_ppl diff_ppl);
use Slic3r::Surface ':types'; use Slic3r::Surface ':types';
has 'slices' => (is => 'ro', required => 1); # SurfaceCollection has 'slices' => (is => 'ro', required => 1); # SurfaceCollection
@ -90,20 +90,23 @@ sub process {
# we need to process each island separately because we might have different # we need to process each island separately because we might have different
# extra perimeters for each one # extra perimeters for each one
foreach my $surface (@{$self->slices}) { foreach my $surface (@{$self->slices}) {
my @contours = (); # array of Polygons with ccw orientation
my @holes = (); # array of Polygons with cw orientation
my @thin_walls = (); # array of ExPolygons
# detect how many perimeters must be generated for this island # detect how many perimeters must be generated for this island
my $loop_number = $self->config->perimeters + ($surface->extra_perimeters || 0); my $loop_number = $self->config->perimeters + ($surface->extra_perimeters || 0);
$loop_number--; # 0-indexed loops
my @gaps = (); # ExPolygons
my @last = @{$surface->expolygon}; my @last = @{$surface->expolygon};
my @gaps = (); # array of ExPolygons if ($loop_number >= 0) { # no loops = -1
if ($loop_number > 0) {
my @contours = (); # depth => [ Polygon, Polygon ... ]
my @holes = (); # depth => [ Polygon, Polygon ... ]
my @thin_walls = (); # Polylines
# we loop one time more than needed in order to find gaps after the last perimeter was applied # we loop one time more than needed in order to find gaps after the last perimeter was applied
for my $i (1 .. ($loop_number+1)) { # outer loop is 1 for my $i (0..($loop_number+1)) { # outer loop is 0
my @offsets = (); my @offsets = ();
if ($i == 1) { if ($i == 0) {
# the minimum thickness of a single loop is: # the minimum thickness of a single loop is:
# ext_width/2 + ext_spacing/2 + spacing/2 + width/2 # ext_width/2 + ext_spacing/2 + spacing/2 + width/2
if ($self->config->thin_walls) { if ($self->config->thin_walls) {
@ -121,15 +124,34 @@ sub process {
# look for thin walls # look for thin walls
if ($self->config->thin_walls) { if ($self->config->thin_walls) {
my $diff = diff_ex( my $diff = diff(
\@last, \@last,
offset(\@offsets, +0.5*$ext_pwidth), offset(\@offsets, +0.5*$ext_pwidth),
1, # medial axis requires non-overlapping geometry 1, # medial axis requires non-overlapping geometry
); );
push @thin_walls, @$diff;
# the following offset2 ensures almost nothing in @thin_walls is narrower than $min_width
# (actually, something larger than that still may exist due to mitering or other causes)
my $min_width = $pwidth / 4;
@thin_walls = @{offset2_ex($diff, -$min_width/2, +$min_width/2)};
# the maximum thickness of our thin wall area is equal to the minimum thickness of a single loop
@thin_walls = map @{$_->medial_axis($pwidth + $pspacing, $min_width)}, @thin_walls;
Slic3r::debugf " %d thin walls detected\n", scalar(@thin_walls) if $Slic3r::debug;
if (0) {
require "Slic3r/SVG.pm";
Slic3r::SVG::output(
"medial_axis.svg",
no_arrows => 1,
expolygons => union_ex($diff),
green_polylines => [ map $_->polygon->split_at_first_point, @{$self->perimeters} ],
red_polylines => $self->_thin_wall_polylines,
);
}
} }
} else { } else {
my $distance = ($i == 2) ? $ext_pspacing : $pspacing; my $distance = ($i == 1) ? $ext_pspacing : $pspacing;
if ($self->config->thin_walls) { if ($self->config->thin_walls) {
@offsets = @{offset2( @offsets = @{offset2(
@ -160,16 +182,92 @@ sub process {
last if !@offsets; last if !@offsets;
last if $i > $loop_number; # we were only looking for gaps this time last if $i > $loop_number; # we were only looking for gaps this time
# clone polygons because these ExPolygons will go out of scope very soon
@last = @offsets; @last = @offsets;
$contours[$i] = [];
$holes[$i] = [];
foreach my $polygon (@offsets) { foreach my $polygon (@offsets) {
if ($polygon->is_counter_clockwise) { my $loop = Slic3r::Layer::PerimeterGenerator::Loop->new(
push @contours, $polygon; polygon => $polygon,
is_contour => $polygon->is_counter_clockwise,
depth => $i,
);
if ($loop->is_contour) {
push @{$contours[$i]}, $loop;
} else { } else {
push @holes, $polygon; push @{$holes[$i]}, $loop;
} }
} }
} }
# nest loops: holes first
for my $d (0..$loop_number) {
# loop through all holes having depth $d
LOOP: for (my $i = 0; $i <= $#{$holes[$d]}; ++$i) {
my $loop = $holes[$d][$i];
# find the hole loop that contains this one, if any
for my $t (($d+1)..$loop_number) {
for (my $j = 0; $j <= $#{$holes[$t]}; ++$j) {
my $candidate_parent = $holes[$t][$j];
if ($candidate_parent->polygon->contains_point($loop->polygon->first_point)) {
$candidate_parent->add_child($loop);
splice @{$holes[$d]}, $i, 1;
--$i;
next LOOP;
}
}
}
# if no hole contains this hole, find the contour loop that contains it
for my $t (reverse 0..$loop_number) {
for (my $j = 0; $j <= $#{$contours[$t]}; ++$j) {
my $candidate_parent = $contours[$t][$j];
if ($candidate_parent->polygon->contains_point($loop->polygon->first_point)) {
$candidate_parent->add_child($loop);
splice @{$holes[$d]}, $i, 1;
--$i;
next LOOP;
}
}
}
}
}
# nest contour loops
for my $d (reverse 1..$loop_number) {
# loop through all contours having depth $d
LOOP: for (my $i = 0; $i <= $#{$contours[$d]}; ++$i) {
my $loop = $contours[$d][$i];
# find the contour loop that contains it
for my $t (reverse 0..($d-1)) {
for (my $j = 0; $j <= $#{$contours[$t]}; ++$j) {
my $candidate_parent = $contours[$t][$j];
if ($candidate_parent->polygon->contains_point($loop->polygon->first_point)) {
$candidate_parent->add_child($loop);
splice @{$contours[$d]}, $i, 1;
--$i;
next LOOP;
}
}
}
}
}
# at this point, all loops should be in $contours[0]
my @entities = $self->_traverse_loops($contours[0], \@thin_walls);
# if brim will be printed, reverse the order of perimeters so that
# we continue inwards after having finished the brim
# TODO: add test for perimeter order
@entities = reverse @entities
if $self->config->external_perimeters_first
|| ($self->layer_id == 0 && $self->print_config->brim_width > 0);
# append perimeters for this slice as a collection
$self->loops->append(Slic3r::ExtrusionPath::Collection->new(@entities))
if @entities;
} }
# fill gaps # fill gaps
@ -219,86 +317,38 @@ sub process {
-($pspacing/2 + $min_perimeter_infill_spacing/2), -($pspacing/2 + $min_perimeter_infill_spacing/2),
+$min_perimeter_infill_spacing/2, +$min_perimeter_infill_spacing/2,
)}; )};
# process thin walls by collapsing slices to single passes
if (@thin_walls) {
# the following offset2 ensures almost nothing in @thin_walls is narrower than $min_width
# (actually, something larger than that still may exist due to mitering or other causes)
my $min_width = $pwidth / 4;
@thin_walls = @{offset2_ex([ map @$_, @thin_walls ], -$min_width/2, +$min_width/2)};
# the maximum thickness of our thin wall area is equal to the minimum thickness of a single loop
$self->_thin_wall_polylines([ map @{$_->medial_axis($pwidth + $pspacing, $min_width)}, @thin_walls ]);
Slic3r::debugf " %d thin walls detected\n", scalar(@{$self->_thin_wall_polylines}) if $Slic3r::debug;
if (0) {
require "Slic3r/SVG.pm";
Slic3r::SVG::output(
"medial_axis.svg",
no_arrows => 1,
expolygons => \@thin_walls,
green_polylines => [ map $_->polygon->split_at_first_point, @{$self->perimeters} ],
red_polylines => $self->_thin_wall_polylines,
);
} }
} }
# find nesting hierarchies separately for contours and holes sub _traverse_loops {
my $contours_pt = union_pt(\@contours); my ($self, $loops, $thin_walls) = @_;
$self->_holes_pt(union_pt(\@holes));
# order loops from inner to outer (in terms of object slices) # loops is an arrayref of ::Loop objects
my @loops = $self->_traverse_pt($contours_pt, 0, 1); # turn each one into an ExtrusionLoop object
my $coll = Slic3r::ExtrusionPath::Collection->new;
foreach my $loop (@$loops) {
my $is_external = $loop->is_external;
# if brim will be printed, reverse the order of perimeters so that my ($role, $loop_role);
# we continue inwards after having finished the brim if ($is_external) {
# TODO: add test for perimeter order $role = EXTR_ROLE_EXTERNAL_PERIMETER;
@loops = reverse @loops } else {
if $self->config->external_perimeters_first $role = EXTR_ROLE_PERIMETER;
|| ($self->layer_id == 0 && $self->print_config->brim_width > 0);
# append perimeters for this slice as a collection
$self->loops->append(Slic3r::ExtrusionPath::Collection->new(@loops))
if @loops;
} }
} if ($loop->is_internal_contour) {
sub _traverse_pt {
my ($self, $polynodes, $depth, $is_contour) = @_;
# convert all polynodes to ExtrusionLoop objects
my $collection = Slic3r::ExtrusionPath::Collection->new; # temporary collection
my @children = ();
foreach my $polynode (@$polynodes) {
my $polygon = ($polynode->{outer} // $polynode->{hole})->clone;
my $role = EXTR_ROLE_PERIMETER;
my $loop_role = EXTRL_ROLE_DEFAULT;
my $root_level = $depth == 0;
my $no_children = !@{ $polynode->{children} };
my $is_external = $is_contour ? $root_level : $no_children;
my $is_internal = $is_contour ? $no_children : $root_level;
if ($is_contour && $is_internal) {
# internal perimeters are root level in case of holes
# and items with no children in case of contours
# Note that we set loop role to ContourInternalPerimeter # Note that we set loop role to ContourInternalPerimeter
# also when loop is both internal and external (i.e. # also when loop is both internal and external (i.e.
# there's only one contour loop). # there's only one contour loop).
$loop_role = EXTRL_ROLE_CONTOUR_INTERNAL_PERIMETER; $loop_role = EXTRL_ROLE_CONTOUR_INTERNAL_PERIMETER;
} } else {
if ($is_external) { $loop_role = EXTR_ROLE_PERIMETER;
# external perimeters are root level in case of contours
# and items with no children in case of holes
$role = EXTR_ROLE_EXTERNAL_PERIMETER;
} }
# detect overhanging/bridging perimeters # detect overhanging/bridging perimeters
my @paths = (); my @paths = ();
if ($self->config->overhangs && $self->layer_id > 0) { if ($self->config->overhangs && $self->layer_id > 0) {
# get non-overhang paths by intersecting this loop with the grown lower slices # get non-overhang paths by intersecting this loop with the grown lower slices
foreach my $polyline (@{ intersection_ppl([ $polygon ], $self->_lower_slices_p) }) { foreach my $polyline (@{ intersection_ppl([ $loop->polygon ], $self->_lower_slices_p) }) {
push @paths, Slic3r::ExtrusionPath->new( push @paths, Slic3r::ExtrusionPath->new(
polyline => $polyline, polyline => $polyline,
role => $role, role => $role,
@ -311,7 +361,7 @@ sub _traverse_pt {
# get overhang paths by checking what parts of this loop fall # get overhang paths by checking what parts of this loop fall
# outside the grown lower slices (thus where the distance between # outside the grown lower slices (thus where the distance between
# the loop centerline and original lower slices is >= half nozzle diameter # the loop centerline and original lower slices is >= half nozzle diameter
foreach my $polyline (@{ diff_ppl([ $polygon ], $self->_lower_slices_p) }) { foreach my $polyline (@{ diff_ppl([ $loop->polygon ], $self->_lower_slices_p) }) {
push @paths, Slic3r::ExtrusionPath->new( push @paths, Slic3r::ExtrusionPath->new(
polyline => $polyline, polyline => $polyline,
role => EXTR_ROLE_OVERHANG_PERIMETER, role => EXTR_ROLE_OVERHANG_PERIMETER,
@ -329,37 +379,22 @@ sub _traverse_pt {
@paths = map $_->clone, @{$collection->chained_path(0)}; @paths = map $_->clone, @{$collection->chained_path(0)};
} else { } else {
push @paths, Slic3r::ExtrusionPath->new( push @paths, Slic3r::ExtrusionPath->new(
polyline => $polygon->split_at_first_point, polyline => $loop->polygon->split_at_first_point,
role => $role, role => $role,
mm3_per_mm => $self->_mm3_per_mm, mm3_per_mm => $self->_mm3_per_mm,
width => $self->perimeter_flow->width, width => $self->perimeter_flow->width,
height => $self->layer_height, height => $self->layer_height,
); );
} }
my $loop = Slic3r::ExtrusionLoop->new_from_paths(@paths); my $eloop = Slic3r::ExtrusionLoop->new_from_paths(@paths);
$loop->role($loop_role); $eloop->role($loop_role);
$coll->append($eloop);
# return ccw contours and cw holes
# GCode.pm will convert all of them to ccw, but it needs to know
# what the holes are in order to compute the correct inwards move
# We do this on the final Loop object because overhang clipping
# does not keep orientation.
if ($is_contour) {
$loop->make_counter_clockwise;
} else {
$loop->make_clockwise;
}
$collection->append($loop);
# save the children
push @children, $polynode->{children};
} }
# if we're handling the top-level contours, add thin walls as candidates too # append thin walls to the nearest-neighbor search (only for first iteration)
# in order to include them in the nearest-neighbor search if (@$thin_walls) {
if ($is_contour && $depth == 0) { foreach my $polyline (@$thin_walls) {
foreach my $polyline (@{$self->_thin_wall_polylines}) { $coll->append(Slic3r::ExtrusionPath->new(
$collection->append(Slic3r::ExtrusionPath->new(
polyline => $polyline, polyline => $polyline,
role => EXTR_ROLE_EXTERNAL_PERIMETER, role => EXTR_ROLE_EXTERNAL_PERIMETER,
mm3_per_mm => $self->_mm3_per_mm, mm3_per_mm => $self->_mm3_per_mm,
@ -367,51 +402,37 @@ sub _traverse_pt {
height => $self->layer_height, height => $self->layer_height,
)); ));
} }
@$thin_walls = ();
} }
# use a nearest neighbor search to order these children # sort entities
# TODO: supply second argument to chained_path() too? my $sorted_coll = $coll->chained_path_indices(0);
# (We used to skip this chained_path() when $is_contour && my @indices = @{$sorted_coll->orig_indices};
# $depth == 0 because slices are ordered at G_code export
# time, but multiple top-level perimeters might belong to
# the same slice actually, so that was a broken optimization.)
# We supply no_reverse = false because we want to permit reversal
# of thin walls, but we rely on the fact that loops will never
# be reversed anyway.
my $sorted_collection = $collection->chained_path_indices(0);
my @orig_indices = @{$sorted_collection->orig_indices};
my @loops = (); # traverse children
foreach my $loop (@$sorted_collection) { my @entities = ();
my $orig_index = shift @orig_indices; for my $i (0..$#indices) {
my $idx = $indices[$i];
if ($loop->isa('Slic3r::ExtrusionPath')) { if ($idx > $#$loops) {
push @loops, $loop->clone; # this is a thin wall
# let's get it from the sorted collection as it might have been reversed
push @entities, $sorted_coll->[$i]->clone;
} else { } else {
# if this is an external contour find all holes belonging to this contour(s) my $loop = $loops->[$idx];
# and prepend them my $eloop = $coll->[$idx]->clone;
if ($is_contour && $depth == 0) {
# $loop is the outermost loop of an island
my @holes = ();
for (my $i = 0; $i <= $#{$self->_holes_pt}; $i++) {
if ($loop->polygon->contains_point($self->_holes_pt->[$i]{outer}->first_point)) {
push @holes, splice @{$self->_holes_pt}, $i, 1; # remove from candidates to reduce complexity
$i--;
}
}
# order holes efficiently my @children = $self->_traverse_loops($loop->children, $thin_walls);
@holes = @holes[@{chained_path([ map {($_->{outer} // $_->{hole})->first_point} @holes ])}]; if ($loop->is_contour) {
$eloop->make_counter_clockwise;
push @loops, reverse map $self->_traverse_pt([$_], 0, 0), @holes; push @entities, @children, $eloop;
} } else {
$eloop->make_clockwise;
# traverse children and prepend them to this loop push @entities, $eloop, @children;
push @loops, $self->_traverse_pt($children[$orig_index], $depth+1, $is_contour);
push @loops, $loop->clone;
} }
} }
return @loops; }
return @entities;
} }
sub _fill_gaps { sub _fill_gaps {
@ -460,4 +481,35 @@ sub _fill_gaps {
return @entities; return @entities;
} }
package Slic3r::Layer::PerimeterGenerator::Loop;
use Moo;
has 'polygon' => (is => 'ro', required => 1);
has 'is_contour' => (is => 'ro', required => 1);
has 'depth' => (is => 'ro', required => 1);
has 'children' => (is => 'ro', default => sub { [] });
use List::Util qw(first);
sub add_child {
my ($self, $child) = @_;
push @{$self->children}, $child;
}
sub is_external {
my ($self) = @_;
return $self->depth == 0;
}
sub is_internal_contour {
my ($self) = @_;
if ($self->is_contour) {
# an internal contour is a contour containing no other contours
return !defined first { $_->is_contour } @{$self->children};
}
return 0;
}
1; 1;