Further refactoring to PerimeterGenerator: remove the $traverse closure

This commit is contained in:
Alessandro Ranellucci 2015-01-07 16:16:00 +01:00
parent 82ec03fc23
commit b085710a4b

View File

@ -18,6 +18,12 @@ has 'overhang_flow' => (is => 'ro', required => 1);
has 'solid_infill_flow' => (is => 'ro', required => 1);
has 'config' => (is => 'ro', default => sub { Slic3r::Config::Region->new });
has 'print_config' => (is => 'ro', default => sub { Slic3r::Config::Print->new });
has '_lower_slices_p' => (is => 'rw');
has '_holes_pt' => (is => 'rw');
has '_ext_mm3_per_mm' => (is => 'rw');
has '_mm3_per_mm' => (is => 'rw');
has '_mm3_per_mm_overhang' => (is => 'rw');
has '_thin_wall_polylines' => (is => 'rw', default => sub { [] });
# generated loops will be put here
has 'loops' => (is => 'ro', default => sub { Slic3r::ExtrusionPath::Collection->new });
@ -32,17 +38,17 @@ sub process {
my ($self) = @_;
# other perimeters
my $mm3_per_mm = $self->perimeter_flow->mm3_per_mm;
$self->_mm3_per_mm($self->perimeter_flow->mm3_per_mm);
my $pwidth = $self->perimeter_flow->scaled_width;
my $pspacing = $self->perimeter_flow->scaled_spacing;
# external perimeters
my $ext_mm3_per_mm = $self->ext_perimeter_flow->mm3_per_mm;
$self->_ext_mm3_per_mm($self->ext_perimeter_flow->mm3_per_mm);
my $ext_pwidth = $self->ext_perimeter_flow->scaled_width;
my $ext_pspacing = scale($self->ext_perimeter_flow->spacing_to($self->perimeter_flow));
# overhang perimeters
my $mm3_per_mm_overhang = $self->overhang_flow->mm3_per_mm;
$self->_mm3_per_mm_overhang($self->overhang_flow->mm3_per_mm);
# solid infill
my $ispacing = $self->solid_infill_flow->scaled_spacing;
@ -192,7 +198,6 @@ sub process {
# process thin walls by collapsing slices to single passes
my @thin_wall_polylines = ();
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)
@ -200,8 +205,8 @@ sub process {
@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
@thin_wall_polylines = map @{$_->medial_axis($pwidth + $pspacing, $min_width)}, @thin_walls;
Slic3r::debugf " %d thin walls detected\n", scalar(@thin_wall_polylines) if $Slic3r::debug;
$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";
@ -210,14 +215,14 @@ sub process {
no_arrows => 1,
expolygons => \@thin_walls,
green_polylines => [ map $_->polygon->split_at_first_point, @{$self->perimeters} ],
red_polylines => \@thin_wall_polylines,
red_polylines => $self->_thin_wall_polylines,
);
}
}
# find nesting hierarchies separately for contours and holes
my $contours_pt = union_pt(\@contours);
my $holes_pt = union_pt(\@holes);
$self->_holes_pt(union_pt(\@holes));
# prepare grown lower layer slices for overhang detection
my $lower_slices = Slic3r::ExPolygon::Collection->new;
@ -229,14 +234,24 @@ sub process {
$lower_slices->append($_)
for @{offset_ex([ map @$_, @{$self->lower_slices} ], scale +$nozzle_diameter/2)};
}
my $lower_slices_p = $lower_slices->polygons;
$self->_lower_slices_p($lower_slices->polygons);
# prepare a coderef for traversing the PolyTree object
# external contours are root items of $contours_pt
# internal contours are the ones next to external
my $traverse;
$traverse = sub {
my ($polynodes, $depth, $is_contour) = @_;
# order loops from inner to outer (in terms of object slices)
my @loops = $self->_traverse_pt($contours_pt, 0, 1);
# 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
@loops = reverse @loops
if $self->config->external_perimeters_first
|| ($self->layer_id == 0 && $self->print_config->brim_width > 0);
# append perimeters
$self->loops->append($_) for @loops;
}
sub _traverse_pt {
my ($self, $polynodes, $depth, $is_contour) = @_;
# convert all polynodes to ExtrusionLoop objects
my $collection = Slic3r::ExtrusionPath::Collection->new; # temporary collection
@ -269,11 +284,11 @@ sub process {
my @paths = ();
if ($self->config->overhangs && $self->layer_id > 0) {
# get non-overhang paths by intersecting this loop with the grown lower slices
foreach my $polyline (@{ intersection_ppl([ $polygon ], $lower_slices_p) }) {
foreach my $polyline (@{ intersection_ppl([ $polygon ], $self->_lower_slices_p) }) {
push @paths, Slic3r::ExtrusionPath->new(
polyline => $polyline,
role => $role,
mm3_per_mm => ($is_external ? $ext_mm3_per_mm : $mm3_per_mm),
mm3_per_mm => ($is_external ? $self->_ext_mm3_per_mm : $self->_mm3_per_mm),
width => ($is_external ? $self->ext_perimeter_flow->width : $self->perimeter_flow->width),
height => $self->layer_height,
);
@ -282,11 +297,11 @@ sub process {
# get overhang paths by checking what parts of this loop fall
# outside the grown lower slices (thus where the distance between
# the loop centerline and original lower slices is >= half nozzle diameter
foreach my $polyline (@{ diff_ppl([ $polygon ], $lower_slices_p) }) {
foreach my $polyline (@{ diff_ppl([ $polygon ], $self->_lower_slices_p) }) {
push @paths, Slic3r::ExtrusionPath->new(
polyline => $polyline,
role => EXTR_ROLE_OVERHANG_PERIMETER,
mm3_per_mm => $mm3_per_mm_overhang,
mm3_per_mm => $self->_mm3_per_mm_overhang,
width => $self->overhang_flow->width,
height => $self->layer_height,
);
@ -302,7 +317,7 @@ sub process {
push @paths, Slic3r::ExtrusionPath->new(
polyline => $polygon->split_at_first_point,
role => $role,
mm3_per_mm => $mm3_per_mm,
mm3_per_mm => $self->_mm3_per_mm,
width => $self->perimeter_flow->width,
height => $self->layer_height,
);
@ -330,11 +345,11 @@ sub process {
# if we're handling the top-level contours, add thin walls as candidates too
# in order to include them in the nearest-neighbor search
if ($is_contour && $depth == 0) {
foreach my $polyline (@thin_wall_polylines) {
foreach my $polyline (@{$self->_thin_wall_polylines}) {
$collection->append(Slic3r::ExtrusionPath->new(
polyline => $polyline,
role => EXTR_ROLE_EXTERNAL_PERIMETER,
mm3_per_mm => $mm3_per_mm,
mm3_per_mm => $self->_mm3_per_mm,
width => $self->perimeter_flow->width,
height => $self->layer_height,
));
@ -362,9 +377,9 @@ sub process {
if ($is_contour && $depth == 0) {
# $loop is the outermost loop of an island
my @holes = ();
for (my $i = 0; $i <= $#$holes_pt; $i++) {
if ($loop->polygon->contains_point($holes_pt->[$i]{outer}->first_point)) {
push @holes, splice @$holes_pt, $i, 1; # remove from candidates to reduce complexity
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--;
}
}
@ -372,29 +387,15 @@ sub process {
# order holes efficiently
@holes = @holes[@{chained_path([ map {($_->{outer} // $_->{hole})->first_point} @holes ])}];
push @loops, reverse map $traverse->([$_], 0, 0), @holes;
push @loops, reverse map $self->_traverse_pt([$_], 0, 0), @holes;
}
# traverse children and prepend them to this loop
push @loops, $traverse->($children[$orig_index], $depth+1, $is_contour);
push @loops, $self->_traverse_pt($children[$orig_index], $depth+1, $is_contour);
push @loops, $loop->clone;
}
}
return @loops;
};
# order loops from inner to outer (in terms of object slices)
my @loops = $traverse->($contours_pt, 0, 1);
# 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
@loops = reverse @loops
if $self->config->external_perimeters_first
|| ($self->layer_id == 0 && $self->print_config->brim_width > 0);
# append perimeters
$self->loops->append($_) for @loops;
}
sub _fill_gaps {