From 580f42c1deb944f0599b62f2029f4bb525db53fe Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Thu, 17 Nov 2011 10:34:40 +0100 Subject: [PATCH 1/8] Use the same bridging logic for "reverse bridges" (top surfaces) --- lib/Slic3r/Layer.pm | 83 ++++++++++++++++++++++--------------------- lib/Slic3r/Polygon.pm | 5 +++ 2 files changed, 48 insertions(+), 40 deletions(-) diff --git a/lib/Slic3r/Layer.pm b/lib/Slic3r/Layer.pm index 82088ae69..aac37979e 100644 --- a/lib/Slic3r/Layer.pm +++ b/lib/Slic3r/Layer.pm @@ -4,7 +4,7 @@ use Moo; use Math::Clipper ':all'; use Slic3r::Geometry qw(polygon_lines points_coincide angle3points polyline_lines nearest_point 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; # a sequential number of layer, starting at 0 @@ -347,28 +347,29 @@ sub remove_small_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 + # or a combination of both; any top surface is a candidate for + # reverse bridge processing + + my @solid_surfaces = grep { + ($_->surface_type eq 'bottom' && $self->id > 0) || $_->surface_type eq 'top' + } @{$self->surfaces} or return; - 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 = $surface->contour->p; - + 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) - $surface_p = safety_offset([$surface_p])->[0]; + my $contour = $surface->expolygon->contour->safety_offset; + my $description = $surface->surface_type eq 'bottom' ? 'bridge/overhang' : 'reverse bridge'; #use Slic3r::SVG; #Slic3r::SVG::output(undef, "bridge.svg", # green_polygons => [ map $_->p, @supporting_surfaces ], - # red_polygons => [ $surface_p ], + # red_polygons => [ $contour ], #); # find all supported edges (as polylines, thus keeping notion of @@ -376,7 +377,7 @@ sub process_bridges { my @supported_polylines = (); { my @current_polyline = (); - EDGE: foreach my $edge (Slic3r::Geometry::polygon_lines($surface_p)) { + 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)) { @@ -394,12 +395,12 @@ sub process_bridges { # 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; + Slic3r::debugf "Found $description 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; + Slic3r::debugf "Found $description with only one support on layer %d; ignoring\n", $self->id; next SURFACE; } @@ -437,17 +438,15 @@ sub process_bridges { # now, extend our bridge by taking a portion of supporting surfaces { # 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_offset = ${ offset([$contour], $Slic3r::bridge_overlap / $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', + my $intersection = intersection_ex( + [ $contour, map $_->p, @supporting_neighbor_surfaces ], + [ $bridge_offset ], + ); + push @{$self->bridges}, map Slic3r::Surface::Bridge->cast_from_expolygon($_, + surface_type => $surface->surface_type, bridge_angle => $bridge_angle, ), @$intersection; } @@ -463,18 +462,26 @@ sub detect_perimeter_surfaces { 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); + my $union = union_ex([ + (map $_->p, grep $_->surface_type =~ /internal/, @{$self->surfaces}), + (map $_->p, @{$self->bridges}), + ]); + # schedule perimeters for internal surfaces merged with bridges 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}; + # schedule perimeters for the remaining surfaces + foreach my $type (qw(top bottom)) { + 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 +489,25 @@ sub detect_perimeter_surfaces { sub split_bridges_fills { my $self = shift; - my $clipper = Math::Clipper->new; foreach my $surfaces (@{$self->fill_surfaces}) { my @surfaces = @$surfaces; @$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); + my $intersection = intersection_ex( + [ map $_->p, @surfaces ], + [ $bridge->contour->p ], + ); push @$surfaces, map Slic3r::Surface::Bridge->cast_from_expolygon($_, - surface_type => 'bottom', + surface_type => $bridge->surface_type, 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); + my $difference = diff_ex([ $surface->p ], [ map $_->contour->p, @{$self->bridges} ]); push @$surfaces, map Slic3r::Surface->cast_from_expolygon($_, surface_type => $surface->surface_type), @$difference; } diff --git a/lib/Slic3r/Polygon.pm b/lib/Slic3r/Polygon.pm index 3d0224dd5..36096e27a 100644 --- a/lib/Slic3r/Polygon.pm +++ b/lib/Slic3r/Polygon.pm @@ -70,4 +70,9 @@ sub area { return Slic3r::Geometry::Clipper::area($self); } +sub safety_offset { + my $self = shift; + return (ref $self)->new(Slic3r::Geometry::Clipper::safety_offset([$self])->[0]); +} + 1; \ No newline at end of file From c8ce989962794561aa9dc8377d8162f4bbe04537 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Thu, 17 Nov 2011 10:38:23 +0100 Subject: [PATCH 2/8] Renamed perimeter_offsets to perimeters --- lib/Slic3r.pm | 2 +- lib/Slic3r/Config.pm | 5 +++-- lib/Slic3r/GUI/SkeinPanel.pm | 2 +- lib/Slic3r/Perimeter.pm | 4 ++-- slic3r.pl | 4 ++-- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/Slic3r.pm b/lib/Slic3r.pm index 27e3acfbc..39342b10a 100644 --- a/lib/Slic3r.pm +++ b/lib/Slic3r.pm @@ -62,7 +62,7 @@ our $thickness_ratio = 1; our $flow_width; # print options -our $perimeter_offsets = 3; +our $perimeters = 3; our $solid_layers = 3; our $bridge_overlap = 3; # mm our $perimeter_infill_overlap_ratio = 0.15; # factor for flow width diff --git a/lib/Slic3r/Config.pm b/lib/Slic3r/Config.pm index 6d5f081db..8c802d0eb 100644 --- a/lib/Slic3r/Config.pm +++ b/lib/Slic3r/Config.pm @@ -78,9 +78,10 @@ our $Options = { }, # print options - 'perimeter_offsets' => { + 'perimeters' => { label => 'Perimeters', type => 'i', + aliases => [qw(perimeter_offsets)], }, 'solid_layers' => { label => 'Solid layers', @@ -287,7 +288,7 @@ sub validate { # --perimeters die "Invalid value for --perimeters\n" - if $Slic3r::perimeter_offsets < 1; + if $Slic3r::perimeters < 1; # --solid-layers die "Invalid value for --solid-layers\n" diff --git a/lib/Slic3r/GUI/SkeinPanel.pm b/lib/Slic3r/GUI/SkeinPanel.pm index ea521be1a..55459642d 100644 --- a/lib/Slic3r/GUI/SkeinPanel.pm +++ b/lib/Slic3r/GUI/SkeinPanel.pm @@ -33,7 +33,7 @@ sub new { }, print => { title => 'Print settings', - options => [qw(perimeter_offsets solid_layers fill_density fill_angle fill_pattern solid_fill_pattern)], + options => [qw(perimeters solid_layers fill_density fill_angle fill_pattern solid_fill_pattern)], }, retract => { title => 'Retraction', diff --git a/lib/Slic3r/Perimeter.pm b/lib/Slic3r/Perimeter.pm index 5698ae22c..d14b0bff9 100644 --- a/lib/Slic3r/Perimeter.pm +++ b/lib/Slic3r/Perimeter.pm @@ -12,7 +12,7 @@ sub make_perimeter { # at least one perimeter is required die "Can't slice object with no perimeters!\n" - if $Slic3r::perimeter_offsets == 0; + if $Slic3r::perimeters == 0; # this array will hold one arrayref per original surface; # each item of this arrayref is an arrayref representing a depth (from inner @@ -40,7 +40,7 @@ sub make_perimeter { # create other offsets push @perimeters, []; - for (my $loop = 0; $loop < $Slic3r::perimeter_offsets; $loop++) { + for (my $loop = 0; $loop < $Slic3r::perimeters; $loop++) { # offsetting a polygon can result in one or many offset polygons @last_offsets = map $_->offset_ex(-$distance), @last_offsets; push @{ $perimeters[-1] }, [@last_offsets]; diff --git a/slic3r.pl b/slic3r.pl index 001bbb023..0d9e311d1 100755 --- a/slic3r.pl +++ b/slic3r.pl @@ -49,7 +49,7 @@ GetOptions( 'infill-every-layers=i' => \$Slic3r::infill_every_layers, # print options - 'perimeters=i' => \$Slic3r::perimeter_offsets, + 'perimeters=i' => \$Slic3r::perimeters, 'solid-layers=i' => \$Slic3r::solid_layers, 'fill-pattern=s' => \$Slic3r::fill_pattern, 'solid-fill-pattern=s' => \$Slic3r::solid_fill_pattern, @@ -168,7 +168,7 @@ Usage: slic3r.pl [ OPTIONS ] file.stl Print options: --perimeters Number of perimeters/horizontal skins (range: 1+, - default: $Slic3r::perimeter_offsets) + default: $Slic3r::perimeters) --solid-layers Number of solid layers to do for top/bottom surfaces (range: 1+, default: $Slic3r::solid_layers) --fill-density Infill density (range: 0-1, default: $Slic3r::fill_density) From 1a066caeccc111d3002d6579bdf195c5636ab0dc Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Thu, 17 Nov 2011 10:41:20 +0100 Subject: [PATCH 3/8] Compute bridge overlap as twice the total thickness of perimeters --- lib/Slic3r.pm | 1 - lib/Slic3r/Layer.pm | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Slic3r.pm b/lib/Slic3r.pm index 39342b10a..e0ca7326a 100644 --- a/lib/Slic3r.pm +++ b/lib/Slic3r.pm @@ -64,7 +64,6 @@ our $flow_width; # print options our $perimeters = 3; our $solid_layers = 3; -our $bridge_overlap = 3; # mm our $perimeter_infill_overlap_ratio = 0.15; # factor for flow width our $fill_pattern = 'rectilinear'; our $solid_fill_pattern = 'rectilinear'; diff --git a/lib/Slic3r/Layer.pm b/lib/Slic3r/Layer.pm index aac37979e..91d279452 100644 --- a/lib/Slic3r/Layer.pm +++ b/lib/Slic3r/Layer.pm @@ -438,7 +438,8 @@ sub process_bridges { # now, extend our bridge by taking a portion of supporting surfaces { # offset the bridge by the specified amount of mm - my $bridge_offset = ${ offset([$contour], $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 = ${ offset([$contour], $bridge_overlap, $Slic3r::resolution * 100, JT_MITER, 2) }[0]; # calculate the new bridge my $intersection = intersection_ex( From 1ef4d006a0e29ce51174a1c18189ec6ca46ca6ba Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Fri, 18 Nov 2011 09:54:28 +0100 Subject: [PATCH 4/8] Treating "reverse bridges" as bridges should not change the way they're infilled or the chosen flow width --- lib/Slic3r/Fill.pm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/Slic3r/Fill.pm b/lib/Slic3r/Fill.pm index 8c13fcb65..271d1bb7f 100644 --- a/lib/Slic3r/Fill.pm +++ b/lib/Slic3r/Fill.pm @@ -66,7 +66,8 @@ sub make_fill { # force 100% density and rectilinear fill for external surfaces 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; $filler = $is_bridge ? 'rectilinear' : $Slic3r::solid_fill_pattern; $flow_width = $Slic3r::nozzle_diameter if $is_bridge; From a1c766cc524fbfacd570d14bf9b6d761258e96b7 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Sat, 19 Nov 2011 16:08:00 +0100 Subject: [PATCH 5/8] 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 89b71324e..bc0433cc7 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 271d1bb7f..0e2a50883 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 45a8e49cc..85e2fbea5 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 91d279452..57f351e3e 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 d14b0bff9..02c3a858b 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 36096e27a..375037ffc 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 980426044..fa037877c 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 95f00f0f4..a570aa364 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; } } From 4540f2377dff51c0d7678b47d06388cbca331063 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Sat, 19 Nov 2011 17:31:00 +0100 Subject: [PATCH 6/8] Some minor fixes for the new bridging logic #58 --- lib/Slic3r/Layer.pm | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Slic3r/Layer.pm b/lib/Slic3r/Layer.pm index 57f351e3e..8f20885c6 100644 --- a/lib/Slic3r/Layer.pm +++ b/lib/Slic3r/Layer.pm @@ -399,6 +399,7 @@ sub process_bridges { shift @{$edge->points}; pop @{$edge->points}; } + @surface_edges = grep { @{$_->points} } @surface_edges; } push @edges, @surface_edges; } From 4a9fc942cb7bd186d73b7e00c4d677a91b7bcc06 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Sat, 19 Nov 2011 17:32:37 +0100 Subject: [PATCH 7/8] Add a M109 for temperature control --- lib/Slic3r/Print.pm | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index a570aa364..3565f5e5e 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -405,6 +405,7 @@ sub export_gcode { # write start commands to file printf $fh "M104 S%d ; set temperature\n", $Slic3r::temperature unless $Slic3r::temperature; print $fh "$Slic3r::start_gcode\n"; + printf $fh "M109 S%d ; wait for temperature to be reached\n", $Slic3r::temperature unless $Slic3r::temperature; print $fh "G90 ; use absolute coordinates\n"; print $fh "G21 ; set units to millimeters\n"; print $fh "G92 E0 ; reset extrusion distance\n" if !$Slic3r::no_extrusion; From bde3c11e824cbb2df6d246fa138a7ae0feef40fc Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Sun, 20 Nov 2011 22:09:59 +0100 Subject: [PATCH 8/8] Fixes to the new bridging algorithm. It appears to be working fine now --- lib/Slic3r/ExPolygon.pm | 7 +++++++ lib/Slic3r/Layer.pm | 19 ++++++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/Slic3r/ExPolygon.pm b/lib/Slic3r/ExPolygon.pm index bc0433cc7..ba46f7095 100644 --- a/lib/Slic3r/ExPolygon.pm +++ b/lib/Slic3r/ExPolygon.pm @@ -53,6 +53,13 @@ sub offset { return @$offsets; } +sub safety_offset { + my $self = shift; + return (ref $self)->new( + @{ Slic3r::Geometry::Clipper::safety_offset([@$self]) }, + ); +} + sub offset_ex { my $self = shift; my @offsets = $self->offset(@_); diff --git a/lib/Slic3r/Layer.pm b/lib/Slic3r/Layer.pm index 8f20885c6..6712fb3d4 100644 --- a/lib/Slic3r/Layer.pm +++ b/lib/Slic3r/Layer.pm @@ -359,14 +359,13 @@ sub process_bridges { my @internal_surfaces = grep $_->surface_type =~ /internal/, @{$self->surfaces}; SURFACE: foreach my $surface (@solid_surfaces) { - # ignore holes in bridges - my $contour = $surface->expolygon->contour->safety_offset; + my $expolygon = $surface->expolygon->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); + 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) { @@ -377,7 +376,7 @@ sub process_bridges { #use Slic3r::SVG; #Slic3r::SVG::output(undef, "bridge.svg", # green_polygons => [ map $_->p, @supporting_surfaces ], - # red_polygons => [ $contour ], + # red_polygons => [ @$expolygon ], #); next SURFACE unless @supporting_surfaces; @@ -425,13 +424,14 @@ sub process_bridges { { # offset the bridge by the specified amount of mm my $bridge_overlap = 2 * $Slic3r::perimeters * $Slic3r::flow_width / $Slic3r::resolution; - my $bridge_offset = ${ offset([$contour], $bridge_overlap, $Slic3r::resolution * 100, JT_MITER, 2) }[0]; + my ($bridge_offset) = $expolygon->contour->offset($bridge_overlap, $Slic3r::resolution * 100, JT_MITER, 2); # calculate the new bridge my $intersection = intersection_ex( - [ $contour, map $_->p, @supporting_surfaces ], + [ @$expolygon, map $_->p, @supporting_surfaces ], [ $bridge_offset ], - ); + ); + push @{$self->bridges}, map Slic3r::Surface::Bridge->cast_from_expolygon($_, surface_type => $surface->surface_type, bridge_angle => $bridge_angle, @@ -484,8 +484,9 @@ sub split_bridges_fills { foreach my $bridge (@{$self->bridges}) { my $intersection = intersection_ex( [ map $_->p, @surfaces ], - [ $bridge->contour->p ], + [ $bridge->p ], ); + push @$surfaces, map Slic3r::Surface::Bridge->cast_from_expolygon($_, surface_type => $bridge->surface_type, bridge_angle => $bridge->bridge_angle, @@ -494,7 +495,7 @@ sub split_bridges_fills { # difference between fill_surfaces and bridges are the other surfaces foreach my $surface (@surfaces) { - my $difference = diff_ex([ $surface->p ], [ map $_->contour->p, @{$self->bridges} ]); + my $difference = diff_ex([ $surface->p ], [ map $_->p, @{$self->bridges} ]); push @$surfaces, map Slic3r::Surface->cast_from_expolygon($_, surface_type => $surface->surface_type), @$difference; }