From 459577f9a2ee4b6a751ba735cf23a023bcd68ce8 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Sun, 9 Oct 2011 19:47:21 +0200 Subject: [PATCH] Completely rewritten the slicing algorithm It should work with any model now. There are still problems with some bridges. --- lib/Slic3r/Geometry.pm | 2 +- lib/Slic3r/Layer.pm | 188 ++++++----------------------------------- lib/Slic3r/Point.pm | 4 +- lib/Slic3r/Polyline.pm | 10 ++- lib/Slic3r/Print.pm | 95 +++++++++++++++++++-- lib/Slic3r/STL.pm | 69 ++------------- lib/Slic3r/SVG.pm | 8 +- lib/Slic3r/Skein.pm | 7 +- 8 files changed, 137 insertions(+), 246 deletions(-) diff --git a/lib/Slic3r/Geometry.pm b/lib/Slic3r/Geometry.pm index e3819060f..30104e109 100644 --- a/lib/Slic3r/Geometry.pm +++ b/lib/Slic3r/Geometry.pm @@ -17,7 +17,7 @@ our @EXPORT_OK = qw( clip_segment_complex_polygon longest_segment angle3points ); -use Slic3r::Geometry::DouglasPeucker (); +use Slic3r::Geometry::DouglasPeucker qw(Douglas_Peucker); use XXX; use constant PI => 4 * atan2(1, 1); diff --git a/lib/Slic3r/Layer.pm b/lib/Slic3r/Layer.pm index d21060ae1..3c250ca2c 100644 --- a/lib/Slic3r/Layer.pm +++ b/lib/Slic3r/Layer.pm @@ -121,10 +121,17 @@ sub remove_surface { @{ $self->surfaces } = grep $_ ne $surface, @{ $self->surfaces }; } -# build polylines of lines which do not already belong to a surface -sub make_polylines { +# build polylines from lines +sub make_surfaces { my $self = shift; + # this algorithm can be further simplified: + # first remove all facetedges that are not connected to any other edge + # or that are connected to more than one edge: those are the edges + # tangent to our plane, that we don't care about; + # then we would have all points connecting two and only two lines, + # so a simple head-to-tail algorithm would work + my @lines = (); push @lines, map $_->p, @{$self->lines}; @@ -134,7 +141,7 @@ sub make_polylines { # red_lines => [ map $_->p, grep $_->isa('Slic3r::Line::FacetEdge'), @{$self->lines} ], #); - my $get_point_id = sub { sprintf "%d,%d", @{$_[0]} }; + my $get_point_id = sub { sprintf "%.0f,%.0f", @{$_[0]} }; my (%pointmap) = (); foreach my $line (@lines) { @@ -193,166 +200,16 @@ sub make_polylines { # polylines => [ @polylines ], #); - return [ map Slic3r::Polyline::Closed->cast($_), @polylines ]; -} - -sub make_surfaces { - my $self = shift; - my ($polylines) = @_; + #@polylines = map Slic3r::Polyline::Closed->cast($_), @polylines; - #use Slic3r::SVG; - #Slic3r::SVG::output_polygons($main::print, "polylines.svg", [ map $_->p, @$polylines ]); - - # count how many other polylines enclose each polyline - # even = contour; odd = hole - my %enclosing_polylines = (); - my %enclosing_polylines_count = (); - my $max_depth = 0; - foreach my $polyline (@$polylines) { - # a polyline encloses another one if any point of it is enclosed - # in the other - my $point = $polyline->points->[0]; - my $ordered_id = $polyline->id; - - # find polylines contaning $point, and thus $polyline - $enclosing_polylines{$polyline} = - [ grep $_->id ne $ordered_id && $_->encloses_point($point), @$polylines ]; - $enclosing_polylines_count{$polyline} = scalar @{ $enclosing_polylines{$polyline} }; - - $max_depth = $enclosing_polylines_count{$polyline} - if $enclosing_polylines_count{$polyline} > $max_depth; - } - - # make a cache for contours and surfaces - my %surfaces = (); # contour => surface - - # start looking at most inner polylines - for (; $max_depth > -1; $max_depth--) { - foreach my $polyline (@$polylines) { - next unless $enclosing_polylines_count{$polyline} == $max_depth; - - my $surface; - if ($enclosing_polylines_count{$polyline} % 2 == 0) { - # this is a contour - $polyline->make_counter_clockwise; - $surface = Slic3r::Surface->new(contour => $polyline); - } else { - # this is a hole - $polyline->make_clockwise; - - # find the enclosing polyline having immediately close depth - my ($contour) = grep $enclosing_polylines_count{$_} == ($max_depth-1), - @{ $enclosing_polylines{$polyline} }; - - if ($surfaces{$contour}) { - $surface = $surfaces{$contour}; - $surface->add_hole($polyline); - } else { - $surface = Slic3r::Surface->new( - contour => $contour, - holes => [$polyline], - ); - $surfaces{$contour} = $surface; - } - } - - # check whether we already have this surface - next if grep $_->id eq $surface->id, @{ $self->surfaces }; - - $surface->surface_type('internal'); - push @{ $self->surfaces }, $surface; - - Slic3r::debugf "New surface: %s (%d holes: %s)\n", - $surface->id, scalar @{$surface->holes}, - join(', ', map $_->id, @{$surface->holes}) || 'none' - if $Slic3r::debug; - } - } -} - -sub merge_contiguous_surfaces { - my $self = shift; - - if ($Slic3r::debug) { - Slic3r::debugf "Initial surfaces (%d):\n", scalar @{ $self->surfaces }; - Slic3r::debugf " [%s] %s (%s with %d holes)\n", $_->surface_type, $_->id, - ($_->contour->is_counter_clockwise ? 'ccw' : 'cw'), scalar @{$_->holes} for @{ $self->surfaces }; - #Slic3r::SVG::output_polygons(undef, "polygons-before.svg", [ map $_->contour->p, @{$self->surfaces} ]); - } - - my %resulting_surfaces = (); - - # only merge surfaces with same type - foreach my $type (qw(bottom top internal)) { + { my $clipper = Math::Clipper->new; - my @surfaces = grep $_->surface_type eq $type, @{$self->surfaces} - or next; + $clipper->add_subject_polygons([ @polylines ]); + my $expolygons = $clipper->ex_execute(CT_UNION, PFT_NONZERO, PFT_NONZERO); - #Slic3r::SVG::output_polygons($main::print, "polygons-$type-before.svg", [ map $_->contour->p, @surfaces ]); - $clipper->add_subject_polygons([ map $_->contour->p, @surfaces ]); - - my $result = $clipper->ex_execute(CT_UNION, PFT_NONZERO, PFT_NONZERO); - $clipper->clear; - - my @extra_holes = map @{$_->{holes}}, @$result; - $result = [ map $_->{outer}, @$result ]; - #Slic3r::SVG::output_polygons($main::print, "polygons-$type-union.svg", $result); - - # subtract bottom or top surfaces from internal - if ($type eq 'internal') { - $clipper->add_subject_polygons($result); - $clipper->add_clip_polygons([ map $_->{outer}, @{$resulting_surfaces{$_}} ]) - for qw(bottom top); - $result = $clipper->execute(CT_DIFFERENCE, PFT_NONZERO, PFT_NONZERO); - $clipper->clear; - } - - # apply holes - $clipper->add_subject_polygons($result); - $result = $clipper->execute(CT_DIFFERENCE, PFT_NONZERO, PFT_NONZERO); - $clipper->clear; - - $clipper->add_subject_polygons($result); - $clipper->add_clip_polygons([ @extra_holes ]) if @extra_holes; - $clipper->add_clip_polygons([ map $_->p, map @{$_->holes}, @surfaces ]); - my $result2 = $clipper->ex_execute(CT_DIFFERENCE, PFT_NONZERO, PFT_NONZERO); - - $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) { - foreach my $p (@{ $resulting_surfaces{$type} }) { - push @{ $self->surfaces }, Slic3r::Surface->new( - surface_type => $type, - contour => Slic3r::Polyline::Closed->cast($p->{outer}), - holes => [ - map Slic3r::Polyline::Closed->cast($_), @{$p->{holes}} - ], - ); - } - } - - if ($Slic3r::debug) { - Slic3r::debugf "Final surfaces (%d):\n", scalar @{ $self->surfaces }; - Slic3r::debugf " [%s] %s (%s with %d holes)\n", $_->surface_type, $_->id, - ($_->contour->is_counter_clockwise ? 'ccw' : 'cw'), scalar @{$_->holes} for @{ $self->surfaces }; + Slic3r::debugf " %d surface(s) detected from %d polylines\n", + scalar(@$expolygons), scalar(@polylines); + push @{$self->surfaces}, map Slic3r::Surface->cast_from_expolygon($_, surface_type => 'internal'), @$expolygons; } } @@ -360,6 +217,7 @@ sub remove_small_surfaces { my $self = shift; my @good_surfaces = (); + my $surface_count = scalar @{$self->surfaces}; foreach my $surface (@{$self->surfaces}) { next if !$surface->contour->is_printable; @{$surface->holes} = grep $_->is_printable, @{$surface->holes}; @@ -367,12 +225,16 @@ sub remove_small_surfaces { } @{$self->surfaces} = @good_surfaces; + Slic3r::debugf "removed %d small surfaces at layer %d\n", + ($surface_count - @good_surfaces), $self->id + if @good_surfaces != $surface_count; } 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) + Slic3r::debugf "removed %d unprintable perimeters at layer %d\n", + (@{$self->perimeters} - @good_perimeters), $self->id if @good_perimeters != @{$self->perimeters}; @{$self->perimeters} = @good_perimeters; @@ -400,9 +262,9 @@ sub process_bridges { { my @current_polyline = (); EDGE: foreach my $edge (Slic3r::Geometry::polygon_lines($surface_p)) { - for (@supporting_surfaces) { + for my $supporting_surface (@supporting_surfaces) { local $Slic3r::Geometry::epsilon = 1E+7; - if (Slic3r::Geometry::polygon_has_subsegment($_->contour->p, $edge)) { + if (Slic3r::Geometry::polygon_has_subsegment($supporting_surface->contour->p, $edge)) { push @current_polyline, $edge; next EDGE; } diff --git a/lib/Slic3r/Point.pm b/lib/Slic3r/Point.pm index 3b06436a0..cf3075f9d 100644 --- a/lib/Slic3r/Point.pm +++ b/lib/Slic3r/Point.pm @@ -4,13 +4,13 @@ use Moo; has 'x' => ( is => 'ro', required => 1, - coerce => sub { sprintf '%.0f', $_[0] }, + #coerce => sub { sprintf '%.0f', $_[0] }, ); has 'y' => ( is => 'ro', required => 1, - coerce => sub { sprintf '%.0f', $_[0] }, + #coerce => sub { sprintf '%.0f', $_[0] }, ); sub cast { diff --git a/lib/Slic3r/Polyline.pm b/lib/Slic3r/Polyline.pm index 23bfca9a2..3f321cc63 100644 --- a/lib/Slic3r/Polyline.pm +++ b/lib/Slic3r/Polyline.pm @@ -3,6 +3,7 @@ use Moo; use Math::Clipper qw(); use Sub::Quote; +use XXX; # arrayref of ordered points has 'points' => ( @@ -61,9 +62,14 @@ sub merge_continuous_lines { sub cleanup { my $self = shift; - my $tolerance = shift || (1 / $Slic3r::resolution); - @{$self->points} = map Slic3r::Point->cast($_), + my $tolerance = shift || 10; + + my $points = $self->p; + push @$points, $points->[0] if $self->isa('Slic3r::Polyline::Closed'); + my @clean_points = map Slic3r::Point->cast($_), Slic3r::Geometry::Douglas_Peucker($self->p, $tolerance); + pop @clean_points if $self->isa('Slic3r::Polyline::Closed'); + @{$self->points} = @clean_points; } sub reverse_points { diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index eaa2641bf..783b126b8 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -34,16 +34,17 @@ sub new_from_stl { print "\n==> PROCESSING SLICES:\n"; foreach my $layer (@{ $print->layers }) { - printf "\nProcessing layer %d:\n", $layer->id; + printf "Making surfaces for layer %d:\n", $layer->id; - # build polylines of lines which do not already belong to a surface - my $polylines = $layer->make_polylines; + # layer currently has many lines representing intersections of + # model facets with the layer plane. there may also be lines + # that we need to ignore (for example, when two non-horizontal + # facets share a common edge on our plane, we get a single line; + # however that line has no meaning for our layer as it's enclosed + # inside a closed polyline) - # build surfaces of polylines (distinguishing contours from holes) - $layer->make_surfaces($polylines); - - # merge surfaces having a common line - $layer->merge_contiguous_surfaces; + # build surfaces from sparse lines + $layer->make_surfaces; } return $print; @@ -74,6 +75,82 @@ sub layer { return $self->layers->[$layer_id]; } +sub detect_surfaces_type { + my $self = shift; + + my $clipper = Math::Clipper->new; + + # prepare a reusable subroutine to make surface differences + my $surface_difference = sub { + my ($subject_surfaces, $clip_surfaces, $result_type) = @_; + $clipper->clear; + $clipper->add_subject_polygons([ map $_->p, @$subject_surfaces ]); + $clipper->add_clip_polygons([ map { ref $_ eq 'ARRAY' ? $_ : $_->p } @$clip_surfaces ]); + my $expolygons = $clipper->ex_execute(CT_DIFFERENCE, PFT_NONZERO, PFT_NONZERO); + return grep $_->contour->is_printable, + map Slic3r::Surface->cast_from_expolygon($_, surface_type => $result_type), + @$expolygons; + }; + + for (my $i = 0; $i < $self->layer_count; $i++) { + my $layer = $self->layers->[$i]; + my $upper_layer = $self->layers->[$i+1]; + my $lower_layer = $i > 0 ? $self->layers->[$i-1] : undef; + + my (@bottom, @top, @internal) = (); + + # find top surfaces (difference between current surfaces + # of current layer and upper one) + if ($upper_layer) { + # offset upper layer surfaces by extrusion_width * perimeters + my $upper_surfaces = offset( + [ map $_->p, @{$upper_layer->surfaces} ], + ($Slic3r::flow_width / $Slic3r::resolution * $Slic3r::perimeter_offsets), + $Slic3r::resolution * 100, + JT_MITER, 2, + ); + @top = $surface_difference->($layer->surfaces, $upper_surfaces, 'top'); + } else { + # if no upper layer, all surfaces of this one are solid + @top = @{$layer->surfaces}; + $_->surface_type('top') for @top; + } + + # find bottom surfaces (difference between current surfaces + # of current layer and lower one) + if ($lower_layer) { + @bottom = $surface_difference->($layer->surfaces, $lower_layer->surfaces, 'bottom'); + + #Slic3r::SVG::output(undef, "layer_" . $layer->id . "_diff.svg", + # green_polygons => [ map $_->p, @{$layer->surfaces} ], + # red_polygons => [ map $_->p, @{$lower_layer->surfaces} ], + #); + + + } else { + # if no lower layer, all surfaces of this one are solid + @bottom = @{$layer->surfaces}; + $_->surface_type('bottom') for @bottom; + } + + # find internal surfaces (difference between top/bottom surfaces and others) + @internal = $surface_difference->($layer->surfaces, [@top, @bottom], 'internal'); + + # save surfaces to layer + $layer->surfaces([ @bottom, @top, @internal ]); + + #use Slic3r::SVG; + #Slic3r::SVG::output(undef, "layer_" . $layer->id . ".svg", + # white_polygons => [ map $_->p, @internal ], + # green_polygons => [ map $_->p, @bottom ], + # red_polygons => [ map $_->p, @top ], + #); + + Slic3r::debugf " layer %d has %d bottom, %d top and %d internal surfaces\n", + $layer->id, scalar(@bottom), scalar(@top), scalar(@internal); + } +} + sub discover_horizontal_shells { my $self = shift; @@ -97,7 +174,7 @@ sub discover_horizontal_shells { Slic3r::debugf " looking for neighbors on layer %d...\n", $n; foreach my $surf_coll (@{$self->layers->[$n]->fill_surfaces}) { - my $neighbor_polygons = [ map $_->p, grep $_->surface_type eq 'internal', @{$surf_coll->surfaces} ]; + my $neighbor_polygons = [ map $_->p, grep $_->surface_type =~ /internal/, @{$surf_coll->surfaces} ]; # find intersection between @surfaces and current layer's surfaces $clipper->add_subject_polygons([ map $_->p, @surfaces ]); diff --git a/lib/Slic3r/STL.pm b/lib/Slic3r/STL.pm index 44034a3f9..802c27e06 100644 --- a/lib/Slic3r/STL.pm +++ b/lib/Slic3r/STL.pm @@ -80,11 +80,6 @@ sub parse_file { foreach my $vertex (@vertices) { $vertex->[$_] = ($Slic3r::scale * $vertex->[$_] / $Slic3r::resolution) + $shift[$_] for X,Y,Z; - - # round Z coordinates to the nearest multiple of layer height - # XY will be rounded automatically to integers with coercion - $vertex->[Z] = int($vertex->[Z] * $Slic3r::resolution / $Slic3r::layer_height) - * $Slic3r::layer_height / $Slic3r::resolution; } foreach my $copy (@copies) { @@ -113,10 +108,14 @@ sub _facet { } Slic3r::debugf "z: min = %.0f, max = %.0f\n", $min_z, $max_z; + if ($min_z == $max_z) { + Slic3r::debugf "Facet is horizontal; ignoring\n"; + return; + } + # calculate the layer extents my $min_layer = int($min_z * $Slic3r::resolution / $Slic3r::layer_height); - my $max_layer = int(0.99999 + ($max_z * $Slic3r::resolution / $Slic3r::layer_height)); - + my $max_layer = int($max_z * $Slic3r::resolution / $Slic3r::layer_height); Slic3r::debugf "layers: min = %s, max = %s\n", $min_layer, $max_layer; # reorder vertices so that the first one is the one with lowest Z @@ -127,62 +126,6 @@ sub _facet { @vertices = (splice(@vertices, $z_order[0]), splice(@vertices, 0, $z_order[0])); } - # is the facet horizontal? - # (note that we can have $min_z == $max_z && $min_layer != $max_layer - # if $min_z % $layer_height != 0) - if ($min_z == $max_z) { - my $layer = $print->layer($min_layer); - - # if all vertices are aligned, then facet is not horizontal but vertical - # with a height less than layer height: that's why it was squashed on a - # single layer - ##local $Slic3r::Geometry::parallel_degrees_limit = 1; - ##if (three_points_aligned(@vertices)) { - if (0 && abs($normal->[Z]) == 0) { - Slic3r::debugf "Facet is vertical with a height less than layer height\n"; - - my ($p1, $p2, $p3) = @vertices; - $layer->add_line(Slic3r::Line::FacetEdge->cast( - $_, - edge_type => 'bottom', - )) for ([$p1, $p2], [$p2, $p3], [$p1, $p3], [$p2, $p1], [$p3, $p2], [$p3, $p1]); - - return; - } - - Slic3r::debugf "Facet is horizontal\n"; - my $surface = $layer->add_surface(@vertices); - - # to determine whether the surface is a top or bottom let's recompute - # the normal using the right-hand rule - # (this relies on the STL to be well-formed) - # recompute the normal using the right-hand rule - my $vertices_p = [@vertices]; - integerize_coordinate_sets($vertices_p); - my $clockwise = !is_counter_clockwise($vertices_p); - - # defensive programming and/or input check - if (abs($normal->[Z]) == 1) { - # while the vertices may belong to the same layer, it doesn't mean the facet - # was horizontal in the original model; so this check makes sense only - # if the original normal is exactly 1 or -1 - if (($normal->[Z] > 0 && $clockwise) || ($normal->[Z] < 0 && !$clockwise)) { - YYY $normal; - die sprintf "STL normal (%.0f) and right-hand rule computation (%s) differ!\n", - $normal->[Z], $clockwise ? 'clockwise' : 'counter-clockwise'; - } - } - - if ($layer->id == 0 && !$clockwise) { - YYY $normal; - die "Right-hand rule gives bad result for facets on base layer!\n"; - } - - $surface->surface_type($clockwise ? 'bottom' : 'top'); - - return; - } - for (my $layer_id = $min_layer; $layer_id <= $max_layer; $layer_id++) { my $layer = $print->layer($layer_id); $layer->add_line($_) for $self->intersect_facet(\@vertices, $layer->z); diff --git a/lib/Slic3r/SVG.pm b/lib/Slic3r/SVG.pm index 56099e365..ff3faa66b 100644 --- a/lib/Slic3r/SVG.pm +++ b/lib/Slic3r/SVG.pm @@ -39,14 +39,15 @@ sub output { my $svg = svg($print); - foreach my $type (qw(polygons polylines white_polygons red_polygons red_polylines)) { + foreach my $type (qw(polygons polylines white_polygons green_polygons red_polygons red_polylines)) { if ($things{$type}) { my $method = $type =~ /polygons/ ? 'polygon' : 'polyline'; + my ($colour) = $type =~ /^(red|green)_/; my $g = $svg->group( style => { 'stroke-width' => 2, - 'stroke' => $type =~ /red_/ ? 'red' : 'black', - 'fill' => ($type !~ /polygons/ ? 'none' : ($type =~ /red_/ ? 'red' : 'grey')), + 'stroke' => $colour || 'black', + 'fill' => ($type !~ /polygons/ ? 'none' : ($colour || 'grey')), }, ); foreach my $polygon (@{$things{$type}}) { @@ -57,7 +58,6 @@ sub output { ); $g->$method( %$path, - 'marker-end' => "url(#endArrow)", ); } } diff --git a/lib/Slic3r/Skein.pm b/lib/Slic3r/Skein.pm index 244fdaf3a..887260624 100644 --- a/lib/Slic3r/Skein.pm +++ b/lib/Slic3r/Skein.pm @@ -16,10 +16,13 @@ sub go { my $t0 = [gettimeofday]; # skein the STL into layers - # each layer has surfaces with holes; surfaces are distinguished - # in top/bottom/internal + # each layer has surfaces with holes my $print = Slic3r::Print->new_from_stl($self->input_file); + # this will detect the type of each surface (top/bottom/internal) + # by splitting them if necessary + $print->detect_surfaces_type; + # this will remove unprintable surfaces # (those that are too tight for extrusion) $print->remove_small_surfaces;