Refactored Perimeter code with new Slic3r::Polygon and Slic3r::ExPolygon objects

Large refactoring. Speed gains. Removed convex hull for bridges.
This commit is contained in:
Alessandro Ranellucci 2011-10-15 11:36:05 +02:00
parent 2d784fac9b
commit 5090ae561c
11 changed files with 174 additions and 100 deletions

View file

@ -14,63 +14,64 @@ sub make_perimeter {
printf "Making perimeter for layer %d:\n", $layer->id;
# at least one perimeter is required
die "Can't extrude object without any perimeter!\n"
die "Can't slice object with no perimeters!\n"
if $Slic3r::perimeter_offsets == 0;
my (%contours, %holes) = ();
# this array will hold one arrayref per original surface;
# each item of this arrayref is an arrayref representing a depth (from inner
# perimeters to outer); each item of this arrayref is an ExPolygon:
# @perimeters = (
# [ # first object (identified by a single surface before offsetting)
# [ Slic3r::ExPolygon, Slic3r::ExPolygon... ], #item 0: outer loop
# [ Slic3r::ExPolygon, Slic3r::ExPolygon... ], #item 1: inner loop
# ],
# [ # second object
# ...
# ]
# )
my @perimeters = (); # one item per depth; each item
foreach my $surface (@{ $layer->perimeter_surfaces }) {
$contours{$surface} = [];
$holes{$surface} = [];
my @last_offsets = ();
# first perimeter
{
my $polygon = $surface->clipper_polygon;
my ($contour_p, @holes_p) = ($polygon->{outer}, @{$polygon->{holes}});
push @{ $contours{$surface} }, $contour_p;
push @{ $holes{$surface} }, @holes_p;
@last_offsets = ($polygon);
}
# the outer loop must be offsetted by half extrusion width inwards
my @last_offsets = ($surface->expolygon);
my $distance = $Slic3r::flow_width / 2 / $Slic3r::resolution;
# create other offsets
for (my $loop = 1; $loop < $Slic3r::perimeter_offsets; $loop++) {
push @perimeters, [];
for (my $loop = 0; $loop < $Slic3r::perimeter_offsets; $loop++) {
# offsetting a polygon can result in one or many offset polygons
@last_offsets = map $self->offset_polygon($_), @last_offsets;
@last_offsets = map $_->offset(-$distance), @last_offsets;
push @{ $perimeters[-1] }, [@last_offsets];
foreach my $offset_polygon (@last_offsets) {
my ($contour_p, @holes_p) = ($offset_polygon->{outer}, @{$offset_polygon->{holes}});
push @{ $contours{$surface} }, $contour_p;
push @{ $holes{$surface} }, @holes_p;
}
# offset distance for inner loops
$distance = $Slic3r::flow_width / $Slic3r::resolution;
}
# create one more offset to be used as boundary for fill
{
my @fill_surfaces = map Slic3r::Surface->cast_from_expolygon(
$_,
surface_type => $surface->surface_type,
), map $self->offset_polygon($_), @last_offsets;
my @fill_surfaces = map Slic3r::Surface->cast_from_expolygon
($_, surface_type => $surface->surface_type),
map $_->offset(-$distance), @last_offsets;
push @{ $layer->fill_surfaces }, Slic3r::Surface::Collection->new(
surfaces => [@fill_surfaces],
) if @fill_surfaces;
push @{ $layer->fill_surfaces }, [@fill_surfaces] if @fill_surfaces;
}
}
# generate paths for holes:
# 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:
# first generate paths for all holes, starting from external (innermost) perimeters
foreach my $i (1..$Slic3r::perimeter_offsets) {
foreach my $hole (map $_->holes, map @{$_->[$i-1]}, @perimeters) {
push @{ $layer->perimeters }, Slic3r::ExtrusionLoop->cast($hole);
}
}
# then generate paths for contours
# 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);
foreach my $contour (map $_->contour, map @$_, map @$_, @perimeters) {
push @{ $layer->perimeters }, Slic3r::ExtrusionLoop->cast($contour);
}
# generate skirt on bottom layer
@ -87,32 +88,4 @@ sub make_perimeter {
}
}
sub offset_polygon {
my $self = shift;
my ($polygon) = @_;
# $polygon holds a Math::Clipper ExPolygon hashref representing
# a polygon and its holes
# generate offsets
my $distance = $Slic3r::flow_width / $Slic3r::resolution;
my $offsets = offset([ $polygon->{outer}, @{$polygon->{holes}} ], -$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 holes to the right contours
my $clipper = Math::Clipper->new;
$clipper->add_subject_polygons($offsets);
my $results = $clipper->ex_execute(CT_UNION, PFT_NONZERO, PFT_NONZERO);
return @$results;
}
1;