Detection of optimal infill direction for bridges. Includes many fixes and improvements.

This commit is contained in:
Alessandro Ranellucci 2011-10-07 19:07:57 +02:00
parent 1cb515a8e5
commit 743f2abcf2
18 changed files with 445 additions and 68 deletions

View file

@ -2,8 +2,11 @@ package Slic3r::Layer;
use Moo;
use Math::Clipper ':all';
use Math::ConvexHull qw(convex_hull);
use XXX;
use constant PI => 4 * atan2(1, 1);
# a sequential number of layer, starting at 0
has 'id' => (
is => 'ro',
@ -26,17 +29,31 @@ has 'surfaces' => (
default => sub { [] },
);
# collection of surfaces representing bridges
has 'bridges' => (
is => 'rw',
#isa => 'ArrayRef[Slic3r::Surface::Bridge]',
default => sub { [] },
);
# collection of surfaces to make perimeters for
has 'perimeter_surfaces' => (
is => 'rw',
#isa => 'ArrayRef[Slic3r::Surface]',
default => sub { [] },
);
# ordered collection of extrusion paths to build all perimeters
has 'perimeters' => (
is => 'rw',
#isa => 'ArrayRef[Slic3r::ExtrusionPath]',
#isa => 'ArrayRef[Slic3r::ExtrusionLoop]',
default => sub { [] },
);
# ordered collection of extrusion paths to build skirt loops
has 'skirts' => (
is => 'rw',
#isa => 'ArrayRef[Slic3r::ExtrusionPath]',
#isa => 'ArrayRef[Slic3r::ExtrusionLoop]',
default => sub { [] },
);
@ -44,7 +61,7 @@ has 'skirts' => (
# they represent boundaries of areas to fill
has 'fill_surfaces' => (
is => 'rw',
#isa => 'ArrayRef[Slic3r::Surface]',
#isa => 'ArrayRef[Slic3r::Surface::Collection]',
default => sub { [] },
);
@ -101,18 +118,34 @@ sub remove_surface {
}
# build polylines of lines which do not already belong to a surface
# okay, this code is a mess. will need some refactoring. sorry.
sub make_polylines {
my $self = shift;
# remove line duplicates
{
if (0) {
# this removes any couple of coinciding Slic3r::Line::FacetEdge
my %lines_map = ();
foreach my $line (grep $_->isa('Slic3r::Line::FacetEdge'), @{ $self->lines }) {
my $ordered_id = $line->ordered_id;
if (exists $lines_map{$ordered_id}) {
delete $lines_map{$ordered_id};
next;
}
$lines_map{$ordered_id} = $line;
}
@{ $self->lines } = (values(%lines_map), grep !$_->isa('Slic3r::Line::FacetEdge'), @{ $self->lines });
}
if (1) {
# this removes any duplicate, leaving one
my %lines_map = map { join(',', sort map $_->id, @{$_->points} ) => "$_" } @{ $self->lines };
%lines_map = reverse %lines_map;
@{ $self->lines } = grep $lines_map{"$_"}, @{ $self->lines };
}
# now remove lines that are already part of a surface
{
if (1) {
my @lines = @{ $self->lines };
@{ $self->lines } = ();
LINE: foreach my $line (@lines) {
@ -130,26 +163,31 @@ sub make_polylines {
}
# make a cache of line endpoints
my %pointmap = ();
my (%pointmap) = ();
foreach my $line (@{ $self->lines }) {
for my $point (@{ $line->points }) {
$pointmap{$point->id} ||= [];
push @{ $pointmap{$point->id} }, $line;
}
}
# defensive programming
#die "No point should be endpoint of less or more than 2 lines!"
# if grep @$_ != 2, values %pointmap;
foreach my $point_id (keys %pointmap) {
$pointmap{$point_id} = [
sort { $a->isa('Slic3r::Line::FacetEdge') <=> $b->isa('Slic3r::Line::FacetEdge') }
@{$pointmap{$point_id}} ];
}
if (0) {
# defensive programming
for (keys %pointmap) {
next if @{$pointmap{$_}} == 2;
#use Slic3r::SVG;
#Slic3r::SVG::output_points($main::print, "points.svg", [ map [split /,/], keys %pointmap ], [ [split /,/, $_ ] ]);
#Slic3r::SVG::output_lines($main::print, "lines.svg", [ map $_->p, @{$self->lines} ]);
use Slic3r::SVG;
Slic3r::SVG::output(undef, "lines_and_points.svg",
lines => [ map $_->p, grep !$_->isa('Slic3r::Line::FacetEdge'), @{$self->lines} ],
red_lines => [ map $_->p, grep $_->isa('Slic3r::Line::FacetEdge'), @{$self->lines} ],
points => [ map [split /,/], keys %pointmap ],
red_points => [ [split /,/, $_ ] ],
);
YYY $pointmap{$_};
@ -198,6 +236,11 @@ sub make_polylines {
# remove last point as it coincides with first one
pop @$points;
if (@$points == 1 && $first_line->isa('Slic3r::Line::FacetEdge')) {
Slic3r::debugf "Skipping spare facet edge";
next;
}
die sprintf "Invalid polyline with only %d points\n", scalar(@$points) if @$points < 3;
Slic3r::debugf "Discovered polyline of %d points (%s)\n", scalar @$points,
@ -336,6 +379,21 @@ sub merge_contiguous_surfaces {
$resulting_surfaces{$type} = $result2;
}
# remove overlapping surfaces
# (remove anything that is not internal from areas covered by internal surfaces)
# this may happen because of rounding of Z coordinates: the model could have
# features smaller than our layer height, so we'd get more things on a single
# layer
if (0) { # not proven to be necessary until now
my $clipper = Math::Clipper->new;
foreach my $type (qw(bottom top)) {
$clipper->clear;
$clipper->add_subject_polygons([ map { $_->{outer}, @{$_->{holes}} } @{$resulting_surfaces{$type}} ]);
$clipper->add_clip_polygons([ map { $_->{outer}, @{$_->{holes}} } @{$resulting_surfaces{internal}} ]);
$resulting_surfaces{$type} = $clipper->ex_execute(CT_DIFFERENCE, PFT_NONZERO, PFT_NONZERO);
}
}
# save surfaces
@{ $self->surfaces } = ();
foreach my $type (keys %resulting_surfaces) {
@ -357,23 +415,181 @@ sub merge_contiguous_surfaces {
}
}
sub remove_small_features {
sub remove_small_surfaces {
my $self = shift;
my @good_surfaces = ();
# for each perimeter, try to get an inwards offset
# for a distance equal to half of the extrusion width;
# if no offset is possible, then feature is not printable
my @good_perimeters = ();
foreach my $loop (@{$self->perimeters}) {
my $p = $loop->p;
@$p = reverse @$p if !is_counter_clockwise($p);
my $offsets = offset([$p], -($Slic3r::flow_width / 2 / $Slic3r::resolution), $Slic3r::resolution * 100000, JT_MITER, 2);
push @good_perimeters, $loop if @$offsets;
foreach my $surface (@{$self->surfaces}) {
next if !$surface->contour->is_printable;
@{$surface->holes} = grep $_->is_printable, @{$surface->holes};
push @good_surfaces, $surface;
}
@{$self->surfaces} = @good_surfaces;
}
sub remove_small_perimeters {
my $self = shift;
my @good_perimeters = grep $_->is_printable, @{$self->perimeters};
Slic3r::debugf "removed %d unprintable perimeters\n", (@{$self->perimeters} - @good_perimeters)
if @good_perimeters != @{$self->perimeters};
@{$self->perimeters} = @good_perimeters;
}
# make bridges printable
sub process_bridges {
my $self = shift;
return if $self->id == 0;
# a bottom surface on a layer > 0 is either a bridge or a overhang
# or a combination of both
my @bottom_surfaces = grep $_->surface_type eq 'bottom', @{$self->surfaces} or return;
my @supporting_surfaces = grep $_->surface_type =~ /internal/, @{$self->surfaces};
SURFACE: foreach my $surface (@bottom_surfaces) {
# since we can't print concave bridges, we transform the surface
# in a convex polygon; this will print thin membranes eventually
my $surface_p = convex_hull($surface->contour->p);
# find all supported edges (as polylines, thus keeping notion of
# consecutive supported edges)
my @supported_polylines = ();
{
my @current_polyline = ();
EDGE: foreach my $edge (Slic3r::Geometry::polygon_lines($surface_p)) {
for (@supporting_surfaces) {
local $Slic3r::Geometry::epsilon = 1E+7;
if (Slic3r::Geometry::polygon_has_subsegment($_->contour->p, $edge)) {
push @current_polyline, $edge;
next EDGE;
}
}
if (@current_polyline) {
push @supported_polylines, [@current_polyline];
@current_polyline = ();
}
}
push @supported_polylines, [@current_polyline] if @current_polyline;
}
# defensive programming, this shouldn't happen
if (@supported_polylines == 0) {
Slic3r::debugf "Found bridge/overhang with no supports on layer %d; ignoring\n", $self->id;
next SURFACE;
}
if (@supported_polylines == 1) {
Slic3r::debugf "Found bridge/overhang with only one support on layer %d; ignoring\n", $self->id;
next SURFACE;
}
# now connect the first point to the last of each polyline
@supported_polylines = map [ $_->[0]->[0], $_->[-1]->[-1] ], @supported_polylines;
# if we got more than two supports, get the longest two
if (@supported_polylines > 2) {
my %lengths = map { "$_" => Slic3r::Geometry::line_length($_) }, @supported_polylines;
@supported_polylines = sort { $lengths{"$a"} <=> $lengths{"$b"} } @supported_polylines;
@supported_polylines = @supported_polylines[0,1];
}
# connect the midpoints, that will give the the optimal infill direction
my @midpoints = map Slic3r::Geometry::midpoint($_), @supported_polylines;
my $bridge_angle = -Slic3r::Geometry::rad2deg(Slic3r::Geometry::line_atan(\@midpoints) + PI/2);
Slic3r::debugf "Optimal infill angle of bridge on layer %d is %d degrees\n", $self->id, $bridge_angle;
# detect which neighbor surfaces are now supporting our bridge
my @supporting_neighbor_surfaces = ();
foreach my $supporting_surface (@supporting_surfaces) {
local $Slic3r::Geometry::epsilon = 1E+7;
push @supporting_neighbor_surfaces, $supporting_surface
if grep Slic3r::Geometry::polygon_has_vertex($supporting_surface->contour->p, $_),
map $_->[0], @supported_polylines;
}
# defensive programming, this shouldn't happen
if (@supporting_neighbor_surfaces == 0) {
Slic3r::debugf "Couldn't find supporting surfaces on layer %d; ignoring\n", $self->id;
next SURFACE;
}
# now, extend our bridge by taking a portion of supporting surfaces
{
# offset the bridge by 5mm
my $bridge_offset = ${ offset([$surface_p], 5 / $Slic3r::resolution, $Slic3r::resolution * 100, JT_MITER, 2) }[0];
# calculate the new bridge
my $clipper = Math::Clipper->new;
$clipper->add_subject_polygon($surface_p);
$clipper->add_subject_polygons([ map $_->p, @supporting_neighbor_surfaces ]);
$clipper->add_clip_polygon($bridge_offset);
my $intersection = $clipper->execute(CT_INTERSECTION, PFT_NONZERO, PFT_NONZERO);
push @{$self->bridges}, map Slic3r::Surface::Bridge->cast_from_polygon($_,
surface_type => 'bottom',
bridge_angle => $bridge_angle,
), @$intersection;
}
}
}
# generates a set of surfaces that will be used to make perimeters
# thus, we need to merge internal surfaces and bridges
sub detect_perimeter_surfaces {
my $self = shift;
# little optimization: skip the Clipper UNION if we have no bridges
if (!@{$self->bridges}) {
push @{$self->perimeter_surfaces}, @{$self->surfaces};
} else {
my $clipper = Math::Clipper->new;
$clipper->add_subject_polygons([ map $_->p, grep $_->surface_type =~ /internal/, @{$self->surfaces} ]);
$clipper->add_clip_polygons([ map $_->p, @{$self->bridges} ]);
my $union = $clipper->ex_execute(CT_UNION, PFT_NONZERO, PFT_NONZERO);
push @{$self->perimeter_surfaces},
map Slic3r::Surface->cast_from_expolygon($_, surface_type => 'internal'),
@$union;
push @{$self->perimeter_surfaces},
grep $_->surface_type !~ /internal/ && ($_->surface_type ne 'bottom' || $self->id == 0),
@{$self->surfaces};
}
}
# splits fill_surfaces in internal and bridge surfaces
sub split_bridges_fills {
my $self = shift;
my $clipper = Math::Clipper->new;
foreach my $surf_coll (@{$self->fill_surfaces}) {
my @surfaces = @{$surf_coll->surfaces};
@{$surf_coll->surfaces} = ();
# intersect fill_surfaces with bridges to get actual bridges
foreach my $bridge (@{$self->bridges}) {
$clipper->clear;
$clipper->add_subject_polygons([ map $_->p, @surfaces ]);
$clipper->add_clip_polygon($bridge->contour->p);
my $intersection = $clipper->ex_execute(CT_INTERSECTION, PFT_NONZERO, PFT_NONZERO);
push @{$surf_coll->surfaces}, map Slic3r::Surface::Bridge->cast_from_expolygon($_,
surface_type => 'bottom',
bridge_angle => $bridge->bridge_angle,
), @$intersection;
}
# difference between fill_surfaces and bridges are the other surfaces
foreach my $surface (@surfaces) {
$clipper->clear;
$clipper->add_subject_polygons([ $surface->p ]);
$clipper->add_clip_polygons([ map $_->contour->p, @{$self->bridges} ]);
my $difference = $clipper->ex_execute(CT_DIFFERENCE, PFT_NONZERO, PFT_NONZERO);
push @{$surf_coll->surfaces}, map Slic3r::Surface->cast_from_expolygon($_,
surface_type => $surface->surface_type), @$difference;
}
}
}
1;