mirror of
https://git.mirrors.martin98.com/https://github.com/prusa3d/PrusaSlicer.git
synced 2025-05-21 20:18:17 +08:00
Further refactoring to PerimeterGenerator: remove the $traverse closure
This commit is contained in:
parent
82ec03fc23
commit
b085710a4b
@ -18,6 +18,12 @@ has 'overhang_flow' => (is => 'ro', required => 1);
|
|||||||
has 'solid_infill_flow' => (is => 'ro', required => 1);
|
has 'solid_infill_flow' => (is => 'ro', required => 1);
|
||||||
has 'config' => (is => 'ro', default => sub { Slic3r::Config::Region->new });
|
has 'config' => (is => 'ro', default => sub { Slic3r::Config::Region->new });
|
||||||
has 'print_config' => (is => 'ro', default => sub { Slic3r::Config::Print->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
|
# generated loops will be put here
|
||||||
has 'loops' => (is => 'ro', default => sub { Slic3r::ExtrusionPath::Collection->new });
|
has 'loops' => (is => 'ro', default => sub { Slic3r::ExtrusionPath::Collection->new });
|
||||||
@ -32,17 +38,17 @@ sub process {
|
|||||||
my ($self) = @_;
|
my ($self) = @_;
|
||||||
|
|
||||||
# other perimeters
|
# 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 $pwidth = $self->perimeter_flow->scaled_width;
|
||||||
my $pspacing = $self->perimeter_flow->scaled_spacing;
|
my $pspacing = $self->perimeter_flow->scaled_spacing;
|
||||||
|
|
||||||
# external perimeters
|
# 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_pwidth = $self->ext_perimeter_flow->scaled_width;
|
||||||
my $ext_pspacing = scale($self->ext_perimeter_flow->spacing_to($self->perimeter_flow));
|
my $ext_pspacing = scale($self->ext_perimeter_flow->spacing_to($self->perimeter_flow));
|
||||||
|
|
||||||
# overhang perimeters
|
# 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
|
# solid infill
|
||||||
my $ispacing = $self->solid_infill_flow->scaled_spacing;
|
my $ispacing = $self->solid_infill_flow->scaled_spacing;
|
||||||
@ -192,7 +198,6 @@ sub process {
|
|||||||
|
|
||||||
|
|
||||||
# process thin walls by collapsing slices to single passes
|
# process thin walls by collapsing slices to single passes
|
||||||
my @thin_wall_polylines = ();
|
|
||||||
if (@thin_walls) {
|
if (@thin_walls) {
|
||||||
# the following offset2 ensures almost nothing in @thin_walls is narrower than $min_width
|
# 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)
|
# (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)};
|
@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
|
# 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;
|
$self->_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;
|
Slic3r::debugf " %d thin walls detected\n", scalar(@{$self->_thin_wall_polylines}) if $Slic3r::debug;
|
||||||
|
|
||||||
if (0) {
|
if (0) {
|
||||||
require "Slic3r/SVG.pm";
|
require "Slic3r/SVG.pm";
|
||||||
@ -210,14 +215,14 @@ sub process {
|
|||||||
no_arrows => 1,
|
no_arrows => 1,
|
||||||
expolygons => \@thin_walls,
|
expolygons => \@thin_walls,
|
||||||
green_polylines => [ map $_->polygon->split_at_first_point, @{$self->perimeters} ],
|
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
|
# find nesting hierarchies separately for contours and holes
|
||||||
my $contours_pt = union_pt(\@contours);
|
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
|
# prepare grown lower layer slices for overhang detection
|
||||||
my $lower_slices = Slic3r::ExPolygon::Collection->new;
|
my $lower_slices = Slic3r::ExPolygon::Collection->new;
|
||||||
@ -229,14 +234,24 @@ sub process {
|
|||||||
$lower_slices->append($_)
|
$lower_slices->append($_)
|
||||||
for @{offset_ex([ map @$_, @{$self->lower_slices} ], scale +$nozzle_diameter/2)};
|
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
|
# order loops from inner to outer (in terms of object slices)
|
||||||
# external contours are root items of $contours_pt
|
my @loops = $self->_traverse_pt($contours_pt, 0, 1);
|
||||||
# internal contours are the ones next to external
|
|
||||||
my $traverse;
|
# if brim will be printed, reverse the order of perimeters so that
|
||||||
$traverse = sub {
|
# we continue inwards after having finished the brim
|
||||||
my ($polynodes, $depth, $is_contour) = @_;
|
# 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
|
# convert all polynodes to ExtrusionLoop objects
|
||||||
my $collection = Slic3r::ExtrusionPath::Collection->new; # temporary collection
|
my $collection = Slic3r::ExtrusionPath::Collection->new; # temporary collection
|
||||||
@ -269,11 +284,11 @@ sub process {
|
|||||||
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 ], $lower_slices_p) }) {
|
foreach my $polyline (@{ intersection_ppl([ $polygon ], $self->_lower_slices_p) }) {
|
||||||
push @paths, Slic3r::ExtrusionPath->new(
|
push @paths, Slic3r::ExtrusionPath->new(
|
||||||
polyline => $polyline,
|
polyline => $polyline,
|
||||||
role => $role,
|
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),
|
width => ($is_external ? $self->ext_perimeter_flow->width : $self->perimeter_flow->width),
|
||||||
height => $self->layer_height,
|
height => $self->layer_height,
|
||||||
);
|
);
|
||||||
@ -282,11 +297,11 @@ sub process {
|
|||||||
# 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 ], $lower_slices_p) }) {
|
foreach my $polyline (@{ diff_ppl([ $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,
|
||||||
mm3_per_mm => $mm3_per_mm_overhang,
|
mm3_per_mm => $self->_mm3_per_mm_overhang,
|
||||||
width => $self->overhang_flow->width,
|
width => $self->overhang_flow->width,
|
||||||
height => $self->layer_height,
|
height => $self->layer_height,
|
||||||
);
|
);
|
||||||
@ -302,7 +317,7 @@ sub process {
|
|||||||
push @paths, Slic3r::ExtrusionPath->new(
|
push @paths, Slic3r::ExtrusionPath->new(
|
||||||
polyline => $polygon->split_at_first_point,
|
polyline => $polygon->split_at_first_point,
|
||||||
role => $role,
|
role => $role,
|
||||||
mm3_per_mm => $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,
|
||||||
);
|
);
|
||||||
@ -330,11 +345,11 @@ sub process {
|
|||||||
# if we're handling the top-level contours, add thin walls as candidates too
|
# if we're handling the top-level contours, add thin walls as candidates too
|
||||||
# in order to include them in the nearest-neighbor search
|
# in order to include them in the nearest-neighbor search
|
||||||
if ($is_contour && $depth == 0) {
|
if ($is_contour && $depth == 0) {
|
||||||
foreach my $polyline (@thin_wall_polylines) {
|
foreach my $polyline (@{$self->_thin_wall_polylines}) {
|
||||||
$collection->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 => $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,
|
||||||
));
|
));
|
||||||
@ -362,9 +377,9 @@ sub process {
|
|||||||
if ($is_contour && $depth == 0) {
|
if ($is_contour && $depth == 0) {
|
||||||
# $loop is the outermost loop of an island
|
# $loop is the outermost loop of an island
|
||||||
my @holes = ();
|
my @holes = ();
|
||||||
for (my $i = 0; $i <= $#$holes_pt; $i++) {
|
for (my $i = 0; $i <= $#{$self->_holes_pt}; $i++) {
|
||||||
if ($loop->polygon->contains_point($holes_pt->[$i]{outer}->first_point)) {
|
if ($loop->polygon->contains_point($self->_holes_pt->[$i]{outer}->first_point)) {
|
||||||
push @holes, splice @$holes_pt, $i, 1; # remove from candidates to reduce complexity
|
push @holes, splice @{$self->_holes_pt}, $i, 1; # remove from candidates to reduce complexity
|
||||||
$i--;
|
$i--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -372,29 +387,15 @@ sub process {
|
|||||||
# order holes efficiently
|
# order holes efficiently
|
||||||
@holes = @holes[@{chained_path([ map {($_->{outer} // $_->{hole})->first_point} @holes ])}];
|
@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
|
# 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;
|
push @loops, $loop->clone;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return @loops;
|
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 {
|
sub _fill_gaps {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user