PrusaSlicer-NonPlainar/lib/Slic3r/Perimeter.pm

133 lines
4.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';
2011-09-02 19:10:20 +00:00
use Math::Geometry::Planar;
use XXX;
2011-09-02 19:10:20 +00:00
2011-09-05 18:29:07 +00:00
use constant X => 0;
use constant Y => 1;
2011-09-02 19:10:20 +00:00
sub make_perimeter {
my $self = shift;
my ($layer) = @_;
printf "Making perimeter for layer %d:\n", $layer->id;
2011-09-04 09:06:15 +00:00
# at least one perimeter is required
die "Can't extrude object without any perimeter!\n"
if $Slic3r::perimeter_offsets == 0;
2011-09-02 19:10:20 +00:00
my (%contours, %holes) = ();
2011-09-02 19:10:20 +00:00
foreach my $surface (@{ $layer->surfaces }) {
$contours{$surface} = [];
$holes{$surface} = [];
my @last_offsets = ();
2011-09-02 19:10:20 +00:00
# first perimeter
{
my $polygon = $surface->clipper_polygon;
my ($contour_p, @holes_p) = ($polygon->{outer}, @{$polygon->{holes}});
2011-09-02 19:10:20 +00:00
push @{ $contours{$surface} }, $contour_p;
push @{ $holes{$surface} }, @holes_p;
@last_offsets = ($polygon);
2011-09-02 19:10:20 +00:00
}
# create other offsets
for (my $loop = 1; $loop < $Slic3r::perimeter_offsets; $loop++) {
# offsetting a polygon can result in one or many offset polygons
@last_offsets = map $self->offset_polygon($_), @last_offsets;
2011-09-02 19:10:20 +00:00
foreach my $offset_polygon (@last_offsets) {
my ($contour_p, @holes_p) = ($offset_polygon->{outer}, @{$offset_polygon->{holes}});
2011-09-02 19:10:20 +00:00
push @{ $contours{$surface} }, $contour_p;
push @{ $holes{$surface} }, @holes_p;
}
}
# create one more offset to be used as boundary for fill
2011-09-26 08:52:58 +00:00
push @{ $layer->fill_surfaces }, Slic3r::Surface::Collection->new(
surfaces => [
map Slic3r::Surface->new(
surface_type => $surface->surface_type,
contour => Slic3r::Polyline::Closed->cast($_->{outer}),
holes => [
map Slic3r::Polyline::Closed->cast($_), @{$_->{holes}}
],
), map $self->offset_polygon($_), @last_offsets
2011-09-26 08:52:58 +00:00
],
);
2011-09-02 19:10:20 +00:00
}
# generate paths for holes:
2011-09-02 19:10:20 +00:00
# we start from innermost loops (that is, external ones), do them
# for all holes, than go on with inner loop and do that for all
# holes and so on;
# then we generate paths for contours:
2011-09-02 19:10:20 +00:00
# this time we do something different: we do contour loops for one
# shape (that is, one original surface) at a time: we start from the
# innermost loop (that is, internal one), then without interrupting
# our path we go onto the outer loop and continue; this should ensure
# good surface quality
foreach my $p (map @$_, values %holes, values %contours) {
push @{ $layer->perimeters }, Slic3r::ExtrusionLoop->cast($p);
2011-09-02 19:10:20 +00:00
}
2011-09-05 18:00:59 +00:00
# generate skirt on bottom layer
2011-10-04 15:55:55 +00:00
if ($layer->id == 0 && $Slic3r::skirts > 0 && @{ $layer->surfaces }) {
2011-09-05 18:00:59 +00:00
# find out convex hull
my $points = [ map { @{ $_->mgp_polygon->polygons->[0] } } @{ $layer->surfaces } ];
2011-09-25 18:24:14 +00:00
my $convex_hull = $self->_mgp_from_points_ref($points)->convexhull2; # maybe Math::ConvexHull is faster?
my $convex_hull_points = ref $convex_hull eq 'ARRAY' ? $convex_hull : $convex_hull->points;
2011-09-05 18:00:59 +00:00
# draw outlines from outside to inside
for (my $i = $Slic3r::skirts - 1; $i >= 0; $i--) {
2011-09-25 18:24:14 +00:00
my $distance = ($Slic3r::skirt_distance + ($Slic3r::flow_width * $i)) / $Slic3r::resolution;
2011-10-04 15:55:55 +00:00
my $outline = offset([$convex_hull_points], $distance, $Slic3r::resolution * 100000, JT_ROUND);
push @{ $layer->skirts }, Slic3r::ExtrusionLoop->cast([ @{$outline->[0]} ]);
2011-09-05 18:00:59 +00:00
}
}
2011-09-02 19:10:20 +00:00
}
sub offset_polygon {
my $self = shift;
my ($polygon) = @_;
my $distance = $Slic3r::flow_width / $Slic3r::resolution;
# $polygon holds a Math::Clipper ExPolygon hashref representing
# a polygon and its holes
my ($contour_p, @holes_p) = ($polygon->{outer}, @{$polygon->{holes}});
# generate offsets
2011-10-04 15:55:55 +00:00
my $offsets = offset([ $contour_p, @holes_p ], -$distance, $Slic3r::resolution * 100000, JT_MITER, 2);
# defensive programming
my (@contour_offsets, @hole_offsets) = ();
for (@$offsets) {
if (is_counter_clockwise($_)) {
push @contour_offsets, $_;
} else {
push @hole_offsets, $_;
}
}
# apply all holes to all contours;
# this is improper, but Math::Clipper handles it
return map {{
outer => $_,
holes => [ @hole_offsets ],
}} @contour_offsets;
}
2011-09-02 19:10:20 +00:00
sub _mgp_from_points_ref {
my $self = shift;
my ($points) = @_;
my $p = Math::Geometry::Planar->new;
$p->points($points);
return $p;
}
1;