From a1c766cc524fbfacd570d14bf9b6d761258e96b7 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Sat, 19 Nov 2011 16:08:00 +0100 Subject: [PATCH] New bridging logic, more robust. #58 --- lib/Slic3r/ExPolygon.pm | 3 +- lib/Slic3r/Fill.pm | 2 - lib/Slic3r/Fill/Base.pm | 2 +- lib/Slic3r/Layer.pm | 113 +++++++++++++++++----------------------- lib/Slic3r/Perimeter.pm | 2 +- lib/Slic3r/Polygon.pm | 12 +++++ lib/Slic3r/Polyline.pm | 5 ++ lib/Slic3r/Print.pm | 22 ++++++-- 8 files changed, 86 insertions(+), 75 deletions(-) diff --git a/lib/Slic3r/ExPolygon.pm b/lib/Slic3r/ExPolygon.pm index 89b71324ec..bc0433cc79 100644 --- a/lib/Slic3r/ExPolygon.pm +++ b/lib/Slic3r/ExPolygon.pm @@ -4,9 +4,8 @@ use warnings; # 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::Clipper qw(union_ex); +use Slic3r::Geometry::Clipper qw(union_ex JT_MITER); # the constructor accepts an array of polygons # or a Math::Clipper ExPolygon (hashref) diff --git a/lib/Slic3r/Fill.pm b/lib/Slic3r/Fill.pm index 271d1bb7fe..0e2a508837 100644 --- a/lib/Slic3r/Fill.pm +++ b/lib/Slic3r/Fill.pm @@ -58,8 +58,6 @@ sub make_fill { ])}; SURFACE: foreach my $surface (@$surfaces) { - Slic3r::debugf " Processing surface %s:\n", $surface->id; - my $filler = $Slic3r::fill_pattern; my $density = $Slic3r::fill_density; my $flow_width = $Slic3r::flow_width; diff --git a/lib/Slic3r/Fill/Base.pm b/lib/Slic3r/Fill/Base.pm index 45a8e49cca..85e2fbea53 100644 --- a/lib/Slic3r/Fill/Base.pm +++ b/lib/Slic3r/Fill/Base.pm @@ -24,7 +24,7 @@ sub infill_direction { } # 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; $rotate[0] = Slic3r::Geometry::deg2rad($surface->bridge_angle); } diff --git a/lib/Slic3r/Layer.pm b/lib/Slic3r/Layer.pm index 91d279452d..57f351e3ef 100644 --- a/lib/Slic3r/Layer.pm +++ b/lib/Slic3r/Layer.pm @@ -356,83 +356,68 @@ sub process_bridges { ($_->surface_type eq 'bottom' && $self->id > 0) || $_->surface_type eq 'top' } @{$self->surfaces} or return; - my @supporting_surfaces = grep $_->surface_type =~ /internal/, @{$self->surfaces}; + my @internal_surfaces = grep $_->surface_type =~ /internal/, @{$self->surfaces}; SURFACE: foreach my $surface (@solid_surfaces) { - # ignore holes in bridges; - # offset the surface a bit to avoid approximation issues when doing the - # intersection below (this is to make sure we overlap with supporting - # surfaces, otherwise a little gap will result from intersection) + # ignore holes in bridges my $contour = $surface->expolygon->contour->safety_offset; my $description = $surface->surface_type eq 'bottom' ? 'bridge/overhang' : 'reverse bridge'; + # 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) = $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; #Slic3r::SVG::output(undef, "bridge.svg", # green_polygons => [ map $_->p, @supporting_surfaces ], # red_polygons => [ $contour ], #); - # find all supported edges (as polylines, thus keeping notion of - # consecutive supported edges) - my @supported_polylines = (); - { - my @current_polyline = (); - EDGE: foreach my $edge ($contour->lines) { - for my $supporting_surface (@supporting_surfaces) { - local $Slic3r::Geometry::epsilon = 1E+7; - if (Slic3r::Geometry::polygon_has_subsegment($supporting_surface->contour->p, $edge)) { - push @current_polyline, $edge; - next EDGE; + next SURFACE unless @supporting_surfaces; + Slic3r::debugf " Found $description on layer %d with %d support(s)\n", + $self->id, scalar(@supporting_surfaces); + + my $bridge_angle = undef; + if ($surface->surface_type eq 'bottom') { + # detect optimal bridge angle + + my $bridge_over_hole = 0; + my @edges = (); # edges are POLYLINES + 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}; } } - if (@current_polyline) { - push @supported_polylines, [@current_polyline]; - @current_polyline = (); - } + push @edges, @surface_edges; + } + Slic3r::debugf " Bridge is supported on %d edge(s)\n", scalar(@edges); + Slic3r::debugf " and covers a hole\n" if $bridge_over_hole; + + if (0) { + require "Slic3r/SVG.pm"; + Slic3r::SVG::output(undef, "bridge.svg", + polylines => [ map $_->p, @edges ], + ); + } + + if (@edges == 2) { + my @chords = map Slic3r::Line->new($_->points->[0], $_->points->[-1]), @edges; + 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; } - push @supported_polylines, [@current_polyline] if @current_polyline; - } - - # defensive programming, this shouldn't happen - if (@supported_polylines == 0) { - Slic3r::debugf "Found $description with no supports on layer %d; ignoring\n", $self->id; - next SURFACE; - } - - if (@supported_polylines == 1) { - Slic3r::debugf "Found $description 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; - # @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 @@ -443,7 +428,7 @@ sub process_bridges { # calculate the new bridge my $intersection = intersection_ex( - [ $contour, map $_->p, @supporting_neighbor_surfaces ], + [ $contour, map $_->p, @supporting_surfaces ], [ $bridge_offset ], ); push @{$self->bridges}, map Slic3r::Surface::Bridge->cast_from_expolygon($_, diff --git a/lib/Slic3r/Perimeter.pm b/lib/Slic3r/Perimeter.pm index d14b0bff90..02c3a858b5 100644 --- a/lib/Slic3r/Perimeter.pm +++ b/lib/Slic3r/Perimeter.pm @@ -8,7 +8,7 @@ use XXX; sub make_perimeter { my $self = shift; 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 die "Can't slice object with no perimeters!\n" diff --git a/lib/Slic3r/Polygon.pm b/lib/Slic3r/Polygon.pm index 36096e27a3..375037ffc6 100644 --- a/lib/Slic3r/Polygon.pm +++ b/lib/Slic3r/Polygon.pm @@ -9,6 +9,7 @@ use warnings; use Slic3r::Geometry qw(polygon_lines polygon_remove_parallel_continuous_edges 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 sub new { @@ -75,4 +76,15 @@ sub safety_offset { 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; \ No newline at end of file diff --git a/lib/Slic3r/Polyline.pm b/lib/Slic3r/Polyline.pm index 9804260440..fa037877c5 100644 --- a/lib/Slic3r/Polyline.pm +++ b/lib/Slic3r/Polyline.pm @@ -156,6 +156,11 @@ sub clip_with_expolygon { 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; } diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index 95f00f0f41..a570aa3645 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -109,6 +109,23 @@ sub detect_surfaces_type { my $upper_surfaces = [ grep { $_->expolygon->contour->area > $min_area } @{$upper_layer->surfaces} ]; @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 { # if no upper layer, all surfaces of this one are solid @top = @{$layer->surfaces}; @@ -276,8 +293,6 @@ sub extrude_perimeters { foreach my $layer (@{ $self->layers }) { $layer->detect_perimeter_surfaces; $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 }) { $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; } }