PrusaSlicer-NonPlainar/lib/Slic3r/Fill.pm

208 lines
7.4 KiB
Perl
Raw Normal View History

2011-09-05 10:21:27 +00:00
package Slic3r::Fill;
use Moo;
2011-09-05 10:21:27 +00:00
2014-01-02 16:24:23 +00:00
use Slic3r::ExtrusionPath ':roles';
use Slic3r::Fill::ArchimedeanChords;
use Slic3r::Fill::Base;
use Slic3r::Fill::Concentric;
use Slic3r::Fill::Flowsnake;
use Slic3r::Fill::HilbertCurve;
2012-04-16 09:55:14 +00:00
use Slic3r::Fill::Honeycomb;
use Slic3r::Fill::Line;
use Slic3r::Fill::OctagramSpiral;
use Slic3r::Fill::PlanePath;
2011-09-05 10:21:27 +00:00
use Slic3r::Fill::Rectilinear;
2014-01-02 16:24:23 +00:00
use Slic3r::Flow ':roles';
use Slic3r::Geometry qw(X Y PI scale chained_path);
2013-11-23 17:15:59 +00:00
use Slic3r::Geometry::Clipper qw(union union_ex diff diff_ex intersection_ex offset offset2);
use Slic3r::Surface ':types';
2011-09-05 10:21:27 +00:00
has 'bounding_box' => (is => 'ro', required => 0);
has 'fillers' => (is => 'rw', default => sub { {} });
our %FillTypes = (
archimedeanchords => 'Slic3r::Fill::ArchimedeanChords',
rectilinear => 'Slic3r::Fill::Rectilinear',
flowsnake => 'Slic3r::Fill::Flowsnake',
octagramspiral => 'Slic3r::Fill::OctagramSpiral',
hilbertcurve => 'Slic3r::Fill::HilbertCurve',
line => 'Slic3r::Fill::Line',
concentric => 'Slic3r::Fill::Concentric',
2012-04-16 09:55:14 +00:00
honeycomb => 'Slic3r::Fill::Honeycomb',
);
sub filler {
my $self = shift;
my ($filler) = @_;
if (!ref $self) {
return $FillTypes{$filler}->new;
}
$self->fillers->{$filler} ||= $FillTypes{$filler}->new(
bounding_box => $self->bounding_box,
);
return $self->fillers->{$filler};
}
sub make_fill {
my $self = shift;
my ($layerm) = @_;
Slic3r::debugf "Filling layer %d:\n", $layerm->id;
2014-01-02 16:24:23 +00:00
my $fill_density = $layerm->config->fill_density;
my $infill_flow = $layerm->flow(FLOW_ROLE_INFILL);
my $solid_infill_flow = $layerm->flow(FLOW_ROLE_SOLID_INFILL);
2013-03-07 15:24:25 +00:00
my @surfaces = ();
# merge adjacent surfaces
# in case of bridge surfaces, the ones with defined angle will be attached to the ones
# without any angle (shouldn't this logic be moved to process_external_surfaces()?)
{
2013-11-23 17:15:59 +00:00
my @surfaces_with_bridge_angle = grep defined $_->bridge_angle, @{$layerm->fill_surfaces};
# give priority to bridges
2013-11-23 17:15:59 +00:00
my @groups = sort { defined $a->[0]->bridge_angle ? -1 : 0 } @{$layerm->fill_surfaces->group(1)};
foreach my $group (@groups) {
2013-11-23 17:15:59 +00:00
my $union_p = union([ map $_->p, @$group ], 1);
# subtract surfaces having a defined bridge_angle from any other
if (@surfaces_with_bridge_angle && !defined $group->[0]->bridge_angle) {
2013-11-23 17:15:59 +00:00
$union_p = diff(
$union_p,
[ map $_->p, @surfaces_with_bridge_angle ],
1,
);
}
# subtract any other surface already processed
2013-11-23 17:15:59 +00:00
my $union = diff_ex(
$union_p,
[ map $_->p, @surfaces ],
1,
);
push @surfaces, map $group->[0]->clone(expolygon => $_), @$union;
}
}
# we need to detect any narrow surfaces that might collapse
# when adding spacing below
# such narrow surfaces are often generated in sloping walls
# by bridge_over_infill() and combine_infill() as a result of the
# subtraction of the combinable area from the layer infill area,
# which leaves small areas near the perimeters
# we are going to grow such regions by overlapping them with the void (if any)
# TODO: detect and investigate whether there could be narrow regions without
# any void neighbors
2014-01-02 16:24:23 +00:00
my $distance_between_surfaces = $infill_flow->scaled_spacing * &Slic3r::INFILL_OVERLAP_OVER_SPACING;
{
2013-03-13 13:55:58 +00:00
my $collapsed = diff(
[ map @{$_->expolygon}, @surfaces ],
offset2([ map @{$_->expolygon}, @surfaces ], -$distance_between_surfaces/2, +$distance_between_surfaces/2),
2013-03-13 13:55:58 +00:00
1,
);
push @surfaces, map Slic3r::Surface->new(
expolygon => $_,
surface_type => S_TYPE_INTERNALSOLID,
), @{intersection_ex(
2013-07-16 22:48:29 +00:00
offset($collapsed, $distance_between_surfaces),
2013-03-13 13:55:58 +00:00
[
(map @{$_->expolygon}, grep $_->surface_type == S_TYPE_INTERNALVOID, @surfaces),
(@$collapsed),
],
1,
)};
}
# add spacing between surfaces
@surfaces = map @{$_->offset(-$distance_between_surfaces / 2)}, @surfaces;
2013-08-13 18:17:17 +00:00
if (0) {
require "Slic3r/SVG.pm";
Slic3r::SVG::output("fill_" . $layerm->print_z . ".svg",
expolygons => [ map $_->expolygon, grep !$_->is_solid, @surfaces ],
red_expolygons => [ map $_->expolygon, grep $_->is_solid, @surfaces ],
);
}
2012-01-28 15:27:52 +00:00
my @fills = ();
my @fills_ordering_points = ();
SURFACE: foreach my $surface (@surfaces) {
next if $surface->surface_type == S_TYPE_INTERNALVOID;
my $filler = $layerm->config->fill_pattern;
my $density = $fill_density;
my $flow = ($surface->surface_type == S_TYPE_TOP)
2014-01-02 16:24:23 +00:00
? $layerm->flow(FLOW_ROLE_TOP_SOLID_INFILL)
: $surface->is_solid
2014-01-02 16:24:23 +00:00
? $solid_infill_flow
: $infill_flow;
my $is_bridge = $layerm->id > 0 && $surface->is_bridge;
my $is_solid = $surface->is_solid;
# force 100% density and rectilinear fill for external surfaces
if ($surface->surface_type != S_TYPE_INTERNAL) {
$density = 1;
$filler = $layerm->config->solid_fill_pattern;
if ($is_bridge) {
$filler = 'rectilinear';
2014-01-02 16:24:23 +00:00
$flow = $layerm->flow(FLOW_ROLE_SOLID_INFILL, 1);
} elsif ($surface->surface_type == S_TYPE_INTERNALSOLID) {
$filler = 'rectilinear';
}
} else {
next SURFACE unless $density > 0;
}
2013-07-16 15:13:01 +00:00
my $f = $self->filler($filler);
$f->layer_id($layerm->id);
$f->angle($layerm->config->fill_angle);
2013-07-16 15:13:01 +00:00
my ($params, @polylines) = $f->fill_surface(
$surface,
density => $density,
flow => $flow,
2013-07-16 15:13:01 +00:00
);
next unless @polylines;
my $h = $surface->thickness;
$h = $layerm->height if $h == -1;
my $mm3_per_mm = $params->{flow}->mm3_per_mm($h);
# save into layer
2013-07-18 17:09:07 +00:00
push @fills, my $collection = Slic3r::ExtrusionPath::Collection->new;
$collection->no_sort($params->{no_sort});
2013-07-18 17:09:07 +00:00
$collection->append(
map Slic3r::ExtrusionPath->new(
polyline => $_,
2013-07-18 17:09:07 +00:00
role => ($surface->surface_type == S_TYPE_INTERNALBRIDGE
? EXTR_ROLE_INTERNALBRIDGE
: $is_bridge
? EXTR_ROLE_BRIDGE
: $is_solid
? (($surface->surface_type == S_TYPE_TOP) ? EXTR_ROLE_TOPSOLIDFILL : EXTR_ROLE_SOLIDFILL)
: EXTR_ROLE_FILL),
mm3_per_mm => $mm3_per_mm,
2013-07-18 17:09:07 +00:00
), @polylines,
);
push @fills_ordering_points, $polylines[0]->first_point;
}
2012-01-12 21:05:35 +00:00
# add thin fill regions
if ($layerm->thin_fills->count > 0) {
push @fills, Slic3r::ExtrusionPath::Collection->new(@{$layerm->thin_fills});
2013-07-25 22:03:28 +00:00
push @fills_ordering_points, $fills[-1]->first_point;
}
# organize infill paths using a nearest-neighbor search
2013-11-23 20:39:05 +00:00
@fills = @fills[ @{chained_path(\@fills_ordering_points)} ];
2012-01-28 15:27:52 +00:00
return @fills;
}
2011-09-05 10:21:27 +00:00
1;