Merge branch 'new-bridges'

This commit is contained in:
Alessandro Ranellucci 2011-11-21 12:49:41 +01:00
commit 00beb7e118
9 changed files with 139 additions and 110 deletions

View File

@ -64,7 +64,6 @@ our $flow_width;
# print options # print options
our $perimeters = 3; our $perimeters = 3;
our $solid_layers = 3; our $solid_layers = 3;
our $bridge_overlap = 3; # mm
our $perimeter_infill_overlap_ratio = 0.15; # factor for flow width our $perimeter_infill_overlap_ratio = 0.15; # factor for flow width
our $fill_pattern = 'rectilinear'; our $fill_pattern = 'rectilinear';
our $solid_fill_pattern = 'rectilinear'; our $solid_fill_pattern = 'rectilinear';

View File

@ -4,9 +4,8 @@ use warnings;
# an ExPolygon is a polygon with holes # an ExPolygon is a polygon with holes
use Math::Clipper qw(CT_UNION PFT_NONZERO JT_MITER);
use Slic3r::Geometry qw(point_in_polygon X Y A B); use Slic3r::Geometry qw(point_in_polygon X Y A B);
use Slic3r::Geometry::Clipper qw(union_ex); use Slic3r::Geometry::Clipper qw(union_ex JT_MITER);
# the constructor accepts an array of polygons # the constructor accepts an array of polygons
# or a Math::Clipper ExPolygon (hashref) # or a Math::Clipper ExPolygon (hashref)
@ -54,6 +53,13 @@ sub offset {
return @$offsets; return @$offsets;
} }
sub safety_offset {
my $self = shift;
return (ref $self)->new(
@{ Slic3r::Geometry::Clipper::safety_offset([@$self]) },
);
}
sub offset_ex { sub offset_ex {
my $self = shift; my $self = shift;
my @offsets = $self->offset(@_); my @offsets = $self->offset(@_);

View File

@ -58,15 +58,14 @@ sub make_fill {
])}; ])};
SURFACE: foreach my $surface (@$surfaces) { SURFACE: foreach my $surface (@$surfaces) {
Slic3r::debugf " Processing surface %s:\n", $surface->id;
my $filler = $Slic3r::fill_pattern; my $filler = $Slic3r::fill_pattern;
my $density = $Slic3r::fill_density; my $density = $Slic3r::fill_density;
my $flow_width = $Slic3r::flow_width; my $flow_width = $Slic3r::flow_width;
# force 100% density and rectilinear fill for external surfaces # force 100% density and rectilinear fill for external surfaces
if ($surface->surface_type ne 'internal') { if ($surface->surface_type ne 'internal') {
my $is_bridge = $surface->isa('Slic3r::Surface::Bridge'); my $is_bridge = $surface->isa('Slic3r::Surface::Bridge')
&& $surface->surface_type eq 'bottom';
$density = 1; $density = 1;
$filler = $is_bridge ? 'rectilinear' : $Slic3r::solid_fill_pattern; $filler = $is_bridge ? 'rectilinear' : $Slic3r::solid_fill_pattern;
$flow_width = $Slic3r::nozzle_diameter if $is_bridge; $flow_width = $Slic3r::nozzle_diameter if $is_bridge;

View File

@ -24,7 +24,7 @@ sub infill_direction {
} }
# use bridge angle # use bridge angle
if ($surface->isa('Slic3r::Surface::Bridge')) { if ($surface->isa('Slic3r::Surface::Bridge') && defined $surface->bridge_angle) {
Slic3r::debugf "Filling bridge with angle %d\n", $surface->bridge_angle; Slic3r::debugf "Filling bridge with angle %d\n", $surface->bridge_angle;
$rotate[0] = Slic3r::Geometry::deg2rad($surface->bridge_angle); $rotate[0] = Slic3r::Geometry::deg2rad($surface->bridge_angle);
} }

View File

@ -4,7 +4,7 @@ use Moo;
use Math::Clipper ':all'; use Math::Clipper ':all';
use Slic3r::Geometry qw(polygon_lines points_coincide angle3points polyline_lines nearest_point use Slic3r::Geometry qw(polygon_lines points_coincide angle3points polyline_lines nearest_point
line_length collinear X Y A B PI); line_length collinear X Y A B PI);
use Slic3r::Geometry::Clipper qw(safety_offset union_ex PFT_EVENODD); use Slic3r::Geometry::Clipper qw(union_ex diff_ex intersection_ex PFT_EVENODD);
use XXX; use XXX;
# a sequential number of layer, starting at 0 # a sequential number of layer, starting at 0
@ -347,107 +347,93 @@ sub remove_small_perimeters {
# make bridges printable # make bridges printable
sub process_bridges { sub process_bridges {
my $self = shift; my $self = shift;
return if $self->id == 0;
# a bottom surface on a layer > 0 is either a bridge or a overhang # a bottom surface on a layer > 0 is either a bridge or a overhang
# or a combination of both # or a combination of both; any top surface is a candidate for
# reverse bridge processing
my @bottom_surfaces = grep $_->surface_type eq 'bottom', @{$self->surfaces} or return; my @solid_surfaces = grep {
my @supporting_surfaces = grep $_->surface_type =~ /internal/, @{$self->surfaces}; ($_->surface_type eq 'bottom' && $self->id > 0) || $_->surface_type eq 'top'
} @{$self->surfaces} or return;
SURFACE: foreach my $surface (@bottom_surfaces) { my @internal_surfaces = grep $_->surface_type =~ /internal/, @{$self->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 = $surface->contour->p;
# offset the surface a bit to avoid approximation issues when doing the SURFACE: foreach my $surface (@solid_surfaces) {
# intersection below (this is to make sure we overlap with supporting my $expolygon = $surface->expolygon->safety_offset;
# surfaces, otherwise a little gap will result from intersection) my $description = $surface->surface_type eq 'bottom' ? 'bridge/overhang' : 'reverse bridge';
$surface_p = safety_offset([$surface_p])->[0];
# offset the contour and intersect it with the internal surfaces to discover
# which of them has contact with our bridge
my @supporting_surfaces = ();
my ($contour_offset) = $expolygon->contour->offset($Slic3r::flow_width / $Slic3r::resolution);
foreach my $internal_surface (@internal_surfaces) {
my $intersection = intersection_ex([$contour_offset], [$internal_surface->contour->p]);
if (@$intersection) {
push @supporting_surfaces, $internal_surface;
}
}
#use Slic3r::SVG; #use Slic3r::SVG;
#Slic3r::SVG::output(undef, "bridge.svg", #Slic3r::SVG::output(undef, "bridge.svg",
# green_polygons => [ map $_->p, @supporting_surfaces ], # green_polygons => [ map $_->p, @supporting_surfaces ],
# red_polygons => [ $surface_p ], # red_polygons => [ @$expolygon ],
#); #);
# find all supported edges (as polylines, thus keeping notion of next SURFACE unless @supporting_surfaces;
# consecutive supported edges) Slic3r::debugf " Found $description on layer %d with %d support(s)\n",
my @supported_polylines = (); $self->id, scalar(@supporting_surfaces);
{
my @current_polyline = (); my $bridge_angle = undef;
EDGE: foreach my $edge (Slic3r::Geometry::polygon_lines($surface_p)) { if ($surface->surface_type eq 'bottom') {
for my $supporting_surface (@supporting_surfaces) { # detect optimal bridge angle
local $Slic3r::Geometry::epsilon = 1E+7;
if (Slic3r::Geometry::polygon_has_subsegment($supporting_surface->contour->p, $edge)) { my $bridge_over_hole = 0;
push @current_polyline, $edge; my @edges = (); # edges are POLYLINES
next EDGE; foreach my $supporting_surface (@supporting_surfaces) {
my @surface_edges = $supporting_surface->contour->clip_with_polygon($contour_offset);
if (@surface_edges == 1 && @{$supporting_surface->contour->p} == @{$surface_edges[0]->p}) {
$bridge_over_hole = 1;
} else {
foreach my $edge (@surface_edges) {
shift @{$edge->points};
pop @{$edge->points};
} }
@surface_edges = grep { @{$_->points} } @surface_edges;
} }
if (@current_polyline) { push @edges, @surface_edges;
push @supported_polylines, [@current_polyline];
@current_polyline = ();
}
} }
push @supported_polylines, [@current_polyline] if @current_polyline; Slic3r::debugf " Bridge is supported on %d edge(s)\n", scalar(@edges);
} Slic3r::debugf " and covers a hole\n" if $bridge_over_hole;
# defensive programming, this shouldn't happen if (0) {
if (@supported_polylines == 0) { require "Slic3r/SVG.pm";
Slic3r::debugf "Found bridge/overhang with no supports on layer %d; ignoring\n", $self->id; Slic3r::SVG::output(undef, "bridge.svg",
next SURFACE; polylines => [ map $_->p, @edges ],
} );
}
if (@supported_polylines == 1) { if (@edges == 2) {
Slic3r::debugf "Found bridge/overhang with only one support on layer %d; ignoring\n", $self->id; my @chords = map Slic3r::Line->new($_->points->[0], $_->points->[-1]), @edges;
next SURFACE; my @midpoints = map $_->midpoint, @chords;
} $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;
# now connect the first point to the last of each polyline }
@supported_polylines = map [ $_->[0]->[0], $_->[-1]->[-1] ], @supported_polylines;
# @supported_polylines becomes actually an array of lines
# 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[-2,-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 # now, extend our bridge by taking a portion of supporting surfaces
{ {
# offset the bridge by the specified amount of mm # offset the bridge by the specified amount of mm
my $bridge_offset = ${ offset([$surface_p], $Slic3r::bridge_overlap / $Slic3r::resolution, $Slic3r::resolution * 100, JT_MITER, 2) }[0]; my $bridge_overlap = 2 * $Slic3r::perimeters * $Slic3r::flow_width / $Slic3r::resolution;
my ($bridge_offset) = $expolygon->contour->offset($bridge_overlap, $Slic3r::resolution * 100, JT_MITER, 2);
# calculate the new bridge # calculate the new bridge
my $clipper = Math::Clipper->new; my $intersection = intersection_ex(
$clipper->add_subject_polygon($surface_p); [ @$expolygon, map $_->p, @supporting_surfaces ],
$clipper->add_subject_polygons([ map $_->p, @supporting_neighbor_surfaces ]); [ $bridge_offset ],
$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($_, push @{$self->bridges}, map Slic3r::Surface::Bridge->cast_from_expolygon($_,
surface_type => 'bottom', surface_type => $surface->surface_type,
bridge_angle => $bridge_angle, bridge_angle => $bridge_angle,
), @$intersection; ), @$intersection;
} }
@ -463,18 +449,26 @@ sub detect_perimeter_surfaces {
if (!@{$self->bridges}) { if (!@{$self->bridges}) {
push @{$self->perimeter_surfaces}, @{$self->surfaces}; push @{$self->perimeter_surfaces}, @{$self->surfaces};
} else { } else {
my $clipper = Math::Clipper->new; my $union = union_ex([
$clipper->add_subject_polygons([ map $_->p, grep $_->surface_type =~ /internal/, @{$self->surfaces} ]); (map $_->p, grep $_->surface_type =~ /internal/, @{$self->surfaces}),
$clipper->add_clip_polygons([ map $_->p, @{$self->bridges} ]); (map $_->p, @{$self->bridges}),
my $union = $clipper->ex_execute(CT_UNION, PFT_NONZERO, PFT_NONZERO); ]);
# schedule perimeters for internal surfaces merged with bridges
push @{$self->perimeter_surfaces}, push @{$self->perimeter_surfaces},
map Slic3r::Surface->cast_from_expolygon($_, surface_type => 'internal'), map Slic3r::Surface->cast_from_expolygon($_, surface_type => 'internal'),
@$union; @$union;
push @{$self->perimeter_surfaces}, # schedule perimeters for the remaining surfaces
grep $_->surface_type !~ /internal/ && ($_->surface_type ne 'bottom' || $self->id == 0), foreach my $type (qw(top bottom)) {
@{$self->surfaces}; my $diff = diff_ex(
[ map $_->p, grep $_->surface_type eq $type, @{$self->surfaces} ],
[ map @$_, @$union ],
);
push @{$self->perimeter_surfaces},
map Slic3r::Surface->cast_from_expolygon($_, surface_type => $type),
@$diff;
}
} }
} }
@ -482,29 +476,26 @@ sub detect_perimeter_surfaces {
sub split_bridges_fills { sub split_bridges_fills {
my $self = shift; my $self = shift;
my $clipper = Math::Clipper->new;
foreach my $surfaces (@{$self->fill_surfaces}) { foreach my $surfaces (@{$self->fill_surfaces}) {
my @surfaces = @$surfaces; my @surfaces = @$surfaces;
@$surfaces = (); @$surfaces = ();
# intersect fill_surfaces with bridges to get actual bridges # intersect fill_surfaces with bridges to get actual bridges
foreach my $bridge (@{$self->bridges}) { foreach my $bridge (@{$self->bridges}) {
$clipper->clear; my $intersection = intersection_ex(
$clipper->add_subject_polygons([ map $_->p, @surfaces ]); [ map $_->p, @surfaces ],
$clipper->add_clip_polygon($bridge->contour->p); [ $bridge->p ],
my $intersection = $clipper->ex_execute(CT_INTERSECTION, PFT_NONZERO, PFT_NONZERO); );
push @$surfaces, map Slic3r::Surface::Bridge->cast_from_expolygon($_, push @$surfaces, map Slic3r::Surface::Bridge->cast_from_expolygon($_,
surface_type => 'bottom', surface_type => $bridge->surface_type,
bridge_angle => $bridge->bridge_angle, bridge_angle => $bridge->bridge_angle,
), @$intersection; ), @$intersection;
} }
# difference between fill_surfaces and bridges are the other surfaces # difference between fill_surfaces and bridges are the other surfaces
foreach my $surface (@surfaces) { foreach my $surface (@surfaces) {
$clipper->clear; my $difference = diff_ex([ $surface->p ], [ map $_->p, @{$self->bridges} ]);
$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 @$surfaces, map Slic3r::Surface->cast_from_expolygon($_, push @$surfaces, map Slic3r::Surface->cast_from_expolygon($_,
surface_type => $surface->surface_type), @$difference; surface_type => $surface->surface_type), @$difference;
} }

View File

@ -8,7 +8,7 @@ use XXX;
sub make_perimeter { sub make_perimeter {
my $self = shift; my $self = shift;
my ($layer) = @_; my ($layer) = @_;
printf "Making perimeter for layer %d:\n", $layer->id; printf "Making perimeter for layer %d\n", $layer->id;
# at least one perimeter is required # at least one perimeter is required
die "Can't slice object with no perimeters!\n" die "Can't slice object with no perimeters!\n"

View File

@ -9,6 +9,7 @@ use warnings;
use Slic3r::Geometry qw(polygon_lines polygon_remove_parallel_continuous_edges use Slic3r::Geometry qw(polygon_lines polygon_remove_parallel_continuous_edges
polygon_segment_having_point point_in_polygon move_points rotate_points); polygon_segment_having_point point_in_polygon move_points rotate_points);
use Slic3r::Geometry::Clipper qw(JT_MITER);
# the constructor accepts an array(ref) of points # the constructor accepts an array(ref) of points
sub new { sub new {
@ -70,4 +71,20 @@ sub area {
return Slic3r::Geometry::Clipper::area($self); return Slic3r::Geometry::Clipper::area($self);
} }
sub safety_offset {
my $self = shift;
return (ref $self)->new(Slic3r::Geometry::Clipper::safety_offset([$self])->[0]);
}
sub offset {
my $self = shift;
my ($distance, $scale, $joinType, $miterLimit) = @_;
$scale ||= $Slic3r::resolution * 1000000;
$joinType = JT_MITER if !defined $joinType;
$miterLimit ||= 2;
my $offsets = Math::Clipper::offset([$self], $distance, $scale, $joinType, $miterLimit);
return @$offsets;
}
1; 1;

View File

@ -156,6 +156,11 @@ sub clip_with_expolygon {
push @polylines, $current_polyline; push @polylines, $current_polyline;
} }
if (@polylines > 1 && scalar(@{$polylines[-1]}) == 2 && $polylines[-1][-1] eq $polylines[0][0]) {
unshift @{$polylines[0]}, $polylines[-1][0];
pop @polylines;
}
return map Slic3r::Polyline->cast($_), @polylines; return map Slic3r::Polyline->cast($_), @polylines;
} }

View File

@ -109,6 +109,23 @@ sub detect_surfaces_type {
my $upper_surfaces = [ grep { $_->expolygon->contour->area > $min_area } @{$upper_layer->surfaces} ]; my $upper_surfaces = [ grep { $_->expolygon->contour->area > $min_area } @{$upper_layer->surfaces} ];
@top = $surface_difference->($layer->surfaces, $upper_surfaces, 'top'); @top = $surface_difference->($layer->surfaces, $upper_surfaces, 'top');
# now check whether each resulting top surfaces is large enough to have its
# own perimeters or whether it may be sufficient to use the lower layer's
# perimeters
# offset upper layer's surfaces
my $upper_surfaces_offsetted;
{
my $distance = $Slic3r::flow_width * ($Slic3r::perimeters) / $Slic3r::resolution;
$upper_surfaces_offsetted = offset([ map $_->p, @{$upper_layer->surfaces} ], $distance, 100, JT_MITER, 2);
}
@top = grep {
my $surface = $_;
my $diff = diff_ex([ map $_->p, $surface ], $upper_surfaces_offsetted);
@$diff;
} @top;
} else { } else {
# if no upper layer, all surfaces of this one are solid # if no upper layer, all surfaces of this one are solid
@top = @{$layer->surfaces}; @top = @{$layer->surfaces};
@ -276,8 +293,6 @@ sub extrude_perimeters {
foreach my $layer (@{ $self->layers }) { foreach my $layer (@{ $self->layers }) {
$layer->detect_perimeter_surfaces; $layer->detect_perimeter_surfaces;
$perimeter_extruder->make_perimeter($layer); $perimeter_extruder->make_perimeter($layer);
Slic3r::debugf " generated paths: %s\n",
join ' ', map $_->id, @{ $layer->perimeters } if $Slic3r::debug;
} }
} }
@ -374,9 +389,6 @@ sub extrude_fills {
foreach my $layer (@{ $self->layers }) { foreach my $layer (@{ $self->layers }) {
$fill_extruder->make_fill($layer); $fill_extruder->make_fill($layer);
Slic3r::debugf " generated %d paths: %s\n",
scalar @{ $layer->fills },
join ' ', map $_->id, map @{$_->paths}, @{ $layer->fills } if $Slic3r::debug;
} }
} }