PrusaSlicer-NonPlainar/lib/Slic3r/Perimeter.pm

101 lines
3.7 KiB
Perl
Raw Normal View History

2011-09-02 19:10:20 +00:00
package Slic3r::Perimeter;
use Moo;
2011-09-02 19:10:20 +00:00
use Math::Clipper ':all';
use Slic3r::Geometry qw(X Y shortest_path scale);
2012-01-12 21:05:35 +00:00
use Slic3r::Geometry::Clipper qw(diff_ex);
use XXX;
2011-09-02 19:10:20 +00:00
sub make_perimeter {
my $self = shift;
my ($layer) = @_;
2011-11-19 15:08:00 +00:00
printf "Making perimeter for layer %d\n", $layer->id;
2011-09-02 19:10:20 +00:00
2011-09-04 09:06:15 +00:00
# at least one perimeter is required
die "Can't slice object with no perimeters!\n"
if $Slic3r::perimeters == 0;
2011-09-02 19:10:20 +00:00
2012-01-21 11:00:12 +00:00
# this array will hold one arrayref per original surface (island);
# each item of this arrayref is an arrayref representing a depth (from outer
# perimeters to inner); each item of this arrayref is an ExPolygon:
# @perimeters = (
2012-01-21 11:00:12 +00:00
# [ # first island
# [ Slic3r::ExPolygon, Slic3r::ExPolygon... ], #depth 0: outer loop
# [ Slic3r::ExPolygon, Slic3r::ExPolygon... ], #depth 1: inner loop
# ],
2012-01-21 11:00:12 +00:00
# [ # second island
# ...
# ]
# )
my @perimeters = (); # one item per depth; each item
2012-01-21 11:00:12 +00:00
# organize islands using a shortest path search
my @surfaces = @{shortest_path([
map [ $_->contour->[0], $_ ], @{$layer->slices},
])};
2012-01-21 11:00:12 +00:00
# for each island:
foreach my $surface (@surfaces) {
my @last_offsets = ($surface->expolygon);
2011-12-04 17:37:37 +00:00
my $distance = 0;
2011-09-02 19:10:20 +00:00
# create other offsets
push @perimeters, [];
for (my $loop = 0; $loop < $Slic3r::perimeters; $loop++) {
# offsetting a polygon can result in one or many offset polygons
2011-12-04 17:37:37 +00:00
@last_offsets = map $_->offset_ex(-$distance), @last_offsets if $distance;
2012-01-12 21:05:35 +00:00
last if !@last_offsets;
push @{ $perimeters[-1] }, [@last_offsets];
2011-09-02 19:10:20 +00:00
# offset distance for inner loops
$distance = scale $Slic3r::flow_spacing;
2011-09-02 19:10:20 +00:00
}
# create one more offset to be used as boundary for fill
{
my @fill_boundaries = map $_->offset_ex(-$distance), @last_offsets;
2012-01-12 21:05:35 +00:00
push @{ $layer->fill_boundaries }, @fill_boundaries;
2012-01-12 21:05:35 +00:00
# detect the small gaps that we need to treat like thin polygons,
# thus generating the skeleton and using it to fill them
2012-01-12 21:05:35 +00:00
my $small_gaps = diff_ex(
[ map @$_, map $_->offset_ex(-$distance/2), map @$_, @{$perimeters[-1]} ],
[ map @$_, map $_->offset_ex(+$distance/2), @fill_boundaries ],
);
push @{ $layer->thin_fills },
grep $_,
map $_->medial_axis(scale $Slic3r::flow_width),
@$small_gaps if 0;
}
}
# process one island (original surface) at time
foreach my $island (@perimeters) {
# do holes starting from innermost one
foreach my $hole (map $_->holes, map @$_, @$island) {
push @{ $layer->perimeters }, Slic3r::ExtrusionLoop->new(polygon => $hole, role => 'perimeter');
}
# do contours starting from innermost one
foreach my $contour (map $_->contour, map @$_, reverse @$island) {
push @{ $layer->perimeters }, Slic3r::ExtrusionLoop->new(polygon => $contour, role => 'perimeter');
}
2011-09-02 19:10:20 +00:00
}
# detect small perimeters by checking their area
for (@{ $layer->perimeters }) {
$_->role('small-perimeter') if $_->polygon->area < $Slic3r::small_perimeter_area;
}
2011-12-30 16:17:37 +00:00
# add thin walls as perimeters
for (@{ $layer->thin_walls }) {
if ($_->isa('Slic3r::Polygon')) {
push @{ $layer->perimeters }, Slic3r::ExtrusionLoop->new(polygon => $_, role => 'perimeter');
2011-12-30 16:17:37 +00:00
} else {
push @{ $layer->perimeters }, Slic3r::ExtrusionPath->new(polyline => $_, role => 'perimeter');
2011-12-30 16:17:37 +00:00
}
}
2011-09-02 19:10:20 +00:00
}
1;