2011-09-05 10:21:27 +00:00
|
|
|
package Slic3r::Fill;
|
2011-09-06 09:50:43 +00:00
|
|
|
use Moo;
|
2011-09-05 10:21:27 +00:00
|
|
|
|
2015-06-09 14:04:26 +00:00
|
|
|
use List::Util qw(max);
|
2014-01-02 16:24:23 +00:00
|
|
|
use Slic3r::ExtrusionPath ':roles';
|
2016-04-11 15:05:58 +00:00
|
|
|
|
2014-01-02 16:24:23 +00:00
|
|
|
use Slic3r::Flow ':roles';
|
2014-04-21 21:21:15 +00:00
|
|
|
use Slic3r::Geometry qw(X Y PI scale chained_path deg2rad);
|
2013-11-23 17:15:59 +00:00
|
|
|
use Slic3r::Geometry::Clipper qw(union union_ex diff diff_ex intersection_ex offset offset2);
|
2012-05-19 14:04:33 +00:00
|
|
|
use Slic3r::Surface ':types';
|
2011-09-05 10:21:27 +00:00
|
|
|
|
2016-04-11 15:05:58 +00:00
|
|
|
use Data::Dumper qw(Dumper);
|
|
|
|
|
2011-10-07 17:07:57 +00:00
|
|
|
|
2013-11-26 17:46:48 +00:00
|
|
|
has 'bounding_box' => (is => 'ro', required => 0);
|
2011-10-06 11:43:32 +00:00
|
|
|
has 'fillers' => (is => 'rw', default => sub { {} });
|
|
|
|
|
2012-04-30 21:50:38 +00:00
|
|
|
sub filler {
|
|
|
|
my $self = shift;
|
|
|
|
my ($filler) = @_;
|
2012-10-30 13:34:41 +00:00
|
|
|
|
2012-10-30 14:38:17 +00:00
|
|
|
if (!ref $self) {
|
2016-04-11 15:05:58 +00:00
|
|
|
return Slic3r::Filler->new_from_type($filler);
|
2012-10-30 14:38:17 +00:00
|
|
|
}
|
2016-04-11 15:05:58 +00:00
|
|
|
|
|
|
|
$self->fillers->{$filler} ||= Slic3r::Filler->new_from_type($filler);
|
|
|
|
$self->fillers->{$filler}->set_bounding_box($self->bounding_box);
|
2012-04-30 21:50:38 +00:00
|
|
|
return $self->fillers->{$filler};
|
2011-10-06 11:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub make_fill {
|
|
|
|
my $self = shift;
|
2013-02-27 09:44:42 +00:00
|
|
|
my ($layerm) = @_;
|
2011-10-06 11:43:32 +00:00
|
|
|
|
2015-10-26 22:23:03 +00:00
|
|
|
Slic3r::debugf "Filling layer %d:\n", $layerm->layer->id;
|
2014-01-02 16:24:23 +00:00
|
|
|
|
2015-10-26 22:23:03 +00:00
|
|
|
my $fill_density = $layerm->region->config->fill_density;
|
2015-03-06 20:35:00 +00:00
|
|
|
my $infill_flow = $layerm->flow(FLOW_ROLE_INFILL);
|
|
|
|
my $solid_infill_flow = $layerm->flow(FLOW_ROLE_SOLID_INFILL);
|
|
|
|
my $top_solid_infill_flow = $layerm->flow(FLOW_ROLE_TOP_SOLID_INFILL);
|
2011-11-13 20:46:32 +00:00
|
|
|
|
2013-03-07 15:24:25 +00:00
|
|
|
my @surfaces = ();
|
|
|
|
|
2013-03-05 19:03:46 +00:00
|
|
|
# merge adjacent surfaces
|
|
|
|
# in case of bridge surfaces, the ones with defined angle will be attached to the ones
|
2013-03-07 14:47:32 +00:00
|
|
|
# without any angle (shouldn't this logic be moved to process_external_surfaces()?)
|
2011-11-23 11:29:27 +00:00
|
|
|
{
|
2014-04-21 21:21:15 +00:00
|
|
|
my @surfaces_with_bridge_angle = grep { $_->bridge_angle >= 0 } @{$layerm->fill_surfaces};
|
2012-01-11 22:15:32 +00:00
|
|
|
|
2014-02-10 12:19:44 +00:00
|
|
|
# group surfaces by distinct properties
|
|
|
|
my @groups = @{$layerm->fill_surfaces->group};
|
|
|
|
|
|
|
|
# merge compatible groups (we can generate continuous infill for them)
|
|
|
|
{
|
|
|
|
# cache flow widths and patterns used for all solid groups
|
|
|
|
# (we'll use them for comparing compatible groups)
|
|
|
|
my @is_solid = my @fw = my @pattern = ();
|
|
|
|
for (my $i = 0; $i <= $#groups; $i++) {
|
|
|
|
# we can only merge solid non-bridge surfaces, so discard
|
|
|
|
# non-solid surfaces
|
2015-10-26 22:23:03 +00:00
|
|
|
if ($groups[$i][0]->is_solid && (!$groups[$i][0]->is_bridge || $layerm->layer->id == 0)) {
|
2014-02-10 12:19:44 +00:00
|
|
|
$is_solid[$i] = 1;
|
|
|
|
$fw[$i] = ($groups[$i][0]->surface_type == S_TYPE_TOP)
|
2015-03-06 20:35:00 +00:00
|
|
|
? $top_solid_infill_flow->width
|
2014-02-10 12:19:44 +00:00
|
|
|
: $solid_infill_flow->width;
|
|
|
|
$pattern[$i] = $groups[$i][0]->is_external
|
2015-10-26 22:23:03 +00:00
|
|
|
? $layerm->region->config->external_fill_pattern
|
2014-02-10 12:19:44 +00:00
|
|
|
: 'rectilinear';
|
|
|
|
} else {
|
|
|
|
$is_solid[$i] = 0;
|
|
|
|
$fw[$i] = 0;
|
|
|
|
$pattern[$i] = 'none';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# loop through solid groups
|
|
|
|
for (my $i = 0; $i <= $#groups; $i++) {
|
|
|
|
next if !$is_solid[$i];
|
|
|
|
|
|
|
|
# find compatible groups and append them to this one
|
|
|
|
for (my $j = $i+1; $j <= $#groups; $j++) {
|
|
|
|
next if !$is_solid[$j];
|
|
|
|
|
|
|
|
if ($fw[$i] == $fw[$j] && $pattern[$i] eq $pattern[$j]) {
|
|
|
|
# groups are compatible, merge them
|
|
|
|
push @{$groups[$i]}, @{$groups[$j]};
|
|
|
|
splice @groups, $j, 1;
|
|
|
|
splice @is_solid, $j, 1;
|
|
|
|
splice @fw, $j, 1;
|
|
|
|
splice @pattern, $j, 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-11 22:15:32 +00:00
|
|
|
# give priority to bridges
|
2014-04-21 21:21:15 +00:00
|
|
|
@groups = sort { ($a->[0]->bridge_angle >= 0) ? -1 : 0 } @groups;
|
2012-01-11 22:15:32 +00:00
|
|
|
|
|
|
|
foreach my $group (@groups) {
|
2013-11-23 17:15:59 +00:00
|
|
|
my $union_p = union([ map $_->p, @$group ], 1);
|
2011-10-06 11:43:32 +00:00
|
|
|
|
2011-11-23 11:29:27 +00:00
|
|
|
# subtract surfaces having a defined bridge_angle from any other
|
2014-04-21 21:21:15 +00:00
|
|
|
if (@surfaces_with_bridge_angle && $group->[0]->bridge_angle < 0) {
|
2013-11-23 17:15:59 +00:00
|
|
|
$union_p = diff(
|
|
|
|
$union_p,
|
2011-11-23 11:29:27 +00:00
|
|
|
[ map $_->p, @surfaces_with_bridge_angle ],
|
2011-12-04 15:24:46 +00:00
|
|
|
1,
|
2011-11-23 11:29:27 +00:00
|
|
|
);
|
2011-10-06 11:43:32 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 10:36:52 +00:00
|
|
|
# subtract any other surface already processed
|
2013-11-23 17:15:59 +00:00
|
|
|
my $union = diff_ex(
|
|
|
|
$union_p,
|
2011-11-29 10:36:52 +00:00
|
|
|
[ map $_->p, @surfaces ],
|
2011-12-04 15:24:46 +00:00
|
|
|
1,
|
2011-11-29 10:36:52 +00:00
|
|
|
);
|
|
|
|
|
2013-03-29 18:18:06 +00:00
|
|
|
push @surfaces, map $group->[0]->clone(expolygon => $_), @$union;
|
2011-11-23 11:29:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-13 00:03:54 +00:00
|
|
|
# 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
|
2011-12-18 12:38:45 +00:00
|
|
|
{
|
2015-06-09 14:04:26 +00:00
|
|
|
my $distance_between_surfaces = max(
|
|
|
|
$infill_flow->scaled_spacing,
|
|
|
|
$solid_infill_flow->scaled_spacing,
|
|
|
|
$top_solid_infill_flow->scaled_spacing,
|
|
|
|
);
|
2013-03-13 13:55:58 +00:00
|
|
|
my $collapsed = diff(
|
2013-03-13 00:03:54 +00:00
|
|
|
[ map @{$_->expolygon}, @surfaces ],
|
2013-09-17 17:39:22 +00:00
|
|
|
offset2([ map @{$_->expolygon}, @surfaces ], -$distance_between_surfaces/2, +$distance_between_surfaces/2),
|
2013-03-13 13:55:58 +00:00
|
|
|
1,
|
2013-03-13 00:03:54 +00:00
|
|
|
);
|
|
|
|
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,
|
2013-03-13 00:03:54 +00:00
|
|
|
)};
|
2011-12-18 12:38:45 +00:00
|
|
|
}
|
|
|
|
|
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 = ();
|
2011-11-23 11:29:27 +00:00
|
|
|
SURFACE: foreach my $surface (@surfaces) {
|
2013-03-13 00:03:54 +00:00
|
|
|
next if $surface->surface_type == S_TYPE_INTERNALVOID;
|
2015-10-26 22:23:03 +00:00
|
|
|
my $filler = $layerm->region->config->fill_pattern;
|
2013-08-25 12:37:50 +00:00
|
|
|
my $density = $fill_density;
|
2014-04-05 08:54:24 +00:00
|
|
|
my $role = ($surface->surface_type == S_TYPE_TOP) ? FLOW_ROLE_TOP_SOLID_INFILL
|
|
|
|
: $surface->is_solid ? FLOW_ROLE_SOLID_INFILL
|
|
|
|
: FLOW_ROLE_INFILL;
|
2015-10-26 22:23:03 +00:00
|
|
|
my $is_bridge = $layerm->layer->id > 0 && $surface->is_bridge;
|
2013-02-23 20:39:13 +00:00
|
|
|
my $is_solid = $surface->is_solid;
|
2011-11-23 11:29:27 +00:00
|
|
|
|
2014-11-26 23:38:05 +00:00
|
|
|
if ($surface->is_solid) {
|
2014-03-22 15:23:33 +00:00
|
|
|
$density = 100;
|
2014-11-26 23:38:05 +00:00
|
|
|
$filler = 'rectilinear';
|
2014-12-28 16:29:21 +00:00
|
|
|
if ($surface->is_external && !$is_bridge) {
|
2015-10-26 22:23:03 +00:00
|
|
|
$filler = $layerm->region->config->external_fill_pattern;
|
2011-12-04 19:29:21 +00:00
|
|
|
}
|
2011-11-23 11:29:27 +00:00
|
|
|
} else {
|
|
|
|
next SURFACE unless $density > 0;
|
2011-10-06 11:43:32 +00:00
|
|
|
}
|
2011-11-23 11:29:27 +00:00
|
|
|
|
2014-12-22 15:47:35 +00:00
|
|
|
# get filler object
|
|
|
|
my $f = $self->filler($filler);
|
|
|
|
|
|
|
|
# calculate the actual flow we'll be using for this infill
|
2015-10-26 22:23:03 +00:00
|
|
|
my $h = $surface->thickness == -1 ? $layerm->layer->height : $surface->thickness;
|
2014-04-05 08:54:24 +00:00
|
|
|
my $flow = $layerm->region->flow(
|
|
|
|
$role,
|
|
|
|
$h,
|
2014-12-22 15:47:35 +00:00
|
|
|
$is_bridge || $f->use_bridge_flow,
|
2015-10-26 22:23:03 +00:00
|
|
|
$layerm->layer->id == 0,
|
2014-08-03 17:17:23 +00:00
|
|
|
-1,
|
2015-10-26 22:23:03 +00:00
|
|
|
$layerm->layer->object,
|
2014-04-05 08:54:24 +00:00
|
|
|
);
|
|
|
|
|
2014-12-22 15:47:35 +00:00
|
|
|
# calculate flow spacing for infill pattern generation
|
|
|
|
my $using_internal_flow = 0;
|
|
|
|
if (!$is_solid && !$is_bridge) {
|
|
|
|
# it's internal infill, so we can calculate a generic flow spacing
|
|
|
|
# for all layers, for avoiding the ugly effect of
|
|
|
|
# misaligned infill on first layer because of different extrusion width and
|
|
|
|
# layer height
|
|
|
|
my $internal_flow = $layerm->region->flow(
|
|
|
|
FLOW_ROLE_INFILL,
|
2015-10-26 22:23:03 +00:00
|
|
|
$layerm->layer->object->config->layer_height, # TODO: handle infill_every_layers?
|
2014-12-22 15:47:35 +00:00
|
|
|
0, # no bridge
|
|
|
|
0, # no first layer
|
|
|
|
-1, # auto width
|
2015-10-26 22:23:03 +00:00
|
|
|
$layerm->layer->object,
|
2014-12-22 15:47:35 +00:00
|
|
|
);
|
2016-04-11 15:05:58 +00:00
|
|
|
$f->set_spacing($internal_flow->spacing);
|
2014-12-22 15:47:35 +00:00
|
|
|
$using_internal_flow = 1;
|
|
|
|
} else {
|
2016-04-11 15:05:58 +00:00
|
|
|
$f->set_spacing($flow->spacing);
|
2014-12-22 15:47:35 +00:00
|
|
|
}
|
|
|
|
|
2016-04-11 15:05:58 +00:00
|
|
|
$f->set_layer_id($layerm->layer->id);
|
|
|
|
$f->set_z($layerm->layer->print_z);
|
|
|
|
$f->set_angle(deg2rad($layerm->region->config->fill_angle));
|
|
|
|
$f->set_loop_clipping(scale($flow->nozzle_diameter) * &Slic3r::LOOP_CLIPPING_LENGTH_OVER_NOZZLE_DIAMETER);
|
2015-06-09 14:04:26 +00:00
|
|
|
|
|
|
|
# apply half spacing using this flow's own spacing and generate infill
|
|
|
|
my @polylines = map $f->fill_surface(
|
|
|
|
$_,
|
2014-04-05 08:54:24 +00:00
|
|
|
density => $density/100,
|
|
|
|
layer_height => $h,
|
2015-06-09 14:04:26 +00:00
|
|
|
), @{ $surface->offset(-scale($f->spacing)/2) };
|
2016-04-11 15:05:58 +00:00
|
|
|
|
2013-07-16 15:13:01 +00:00
|
|
|
next unless @polylines;
|
2016-04-11 15:05:58 +00:00
|
|
|
|
|
|
|
print "Polylines after fill_surface: ", Dumper(\@polylines);
|
2011-11-23 11:29:27 +00:00
|
|
|
|
2014-12-22 15:47:35 +00:00
|
|
|
# calculate actual flow from spacing (which might have been adjusted by the infill
|
|
|
|
# pattern generator)
|
|
|
|
if ($using_internal_flow) {
|
|
|
|
# if we used the internal flow we're not doing a solid infill
|
|
|
|
# so we can safely ignore the slight variation that might have
|
|
|
|
# been applied to $f->flow_spacing
|
|
|
|
} else {
|
|
|
|
$flow = Slic3r::Flow->new_from_spacing(
|
|
|
|
spacing => $f->spacing,
|
|
|
|
nozzle_diameter => $flow->nozzle_diameter,
|
|
|
|
layer_height => $h,
|
|
|
|
bridge => $is_bridge || $f->use_bridge_flow,
|
|
|
|
);
|
|
|
|
}
|
2014-06-11 23:00:13 +00:00
|
|
|
my $mm3_per_mm = $flow->mm3_per_mm;
|
2012-11-18 14:42:59 +00:00
|
|
|
|
2011-11-23 11:29:27 +00:00
|
|
|
# save into layer
|
2014-12-16 23:34:00 +00:00
|
|
|
{
|
|
|
|
my $role = $is_bridge ? EXTR_ROLE_BRIDGE
|
|
|
|
: $is_solid ? (($surface->surface_type == S_TYPE_TOP) ? EXTR_ROLE_TOPSOLIDFILL : EXTR_ROLE_SOLIDFILL)
|
|
|
|
: EXTR_ROLE_FILL;
|
|
|
|
|
2014-12-16 23:52:01 +00:00
|
|
|
push @fills, my $collection = Slic3r::ExtrusionPath::Collection->new;
|
2014-12-22 15:47:35 +00:00
|
|
|
$collection->no_sort($f->no_sort);
|
2016-04-11 15:05:58 +00:00
|
|
|
print "collecton->append\n";
|
2014-12-16 23:34:00 +00:00
|
|
|
$collection->append(
|
|
|
|
map Slic3r::ExtrusionPath->new(
|
|
|
|
polyline => $_,
|
|
|
|
role => $role,
|
|
|
|
mm3_per_mm => $mm3_per_mm,
|
|
|
|
width => $flow->width,
|
2014-12-22 15:47:35 +00:00
|
|
|
height => $flow->height,
|
2016-04-11 15:05:58 +00:00
|
|
|
), map @$_, @polylines,
|
2014-12-16 23:34:00 +00:00
|
|
|
);
|
|
|
|
}
|
2011-10-06 11:43:32 +00:00
|
|
|
}
|
2012-01-12 21:05:35 +00:00
|
|
|
|
|
|
|
# add thin fill regions
|
2014-04-29 13:25:14 +00:00
|
|
|
foreach my $thin_fill (@{$layerm->thin_fills}) {
|
2014-12-16 23:52:01 +00:00
|
|
|
push @fills, Slic3r::ExtrusionPath::Collection->new($thin_fill);
|
2013-07-25 22:03:28 +00:00
|
|
|
}
|
2012-06-30 14:33:01 +00:00
|
|
|
|
2012-01-28 15:27:52 +00:00
|
|
|
return @fills;
|
2011-10-06 11:43:32 +00:00
|
|
|
}
|
|
|
|
|
2011-09-05 10:21:27 +00:00
|
|
|
1;
|