mirror of
				https://github.com/SoftFever/OrcaSlicer.git
				synced 2025-10-31 04:31:15 -06:00 
			
		
		
		
	Rectilinear fill
This commit is contained in:
		
							parent
							
								
									428006264d
								
							
						
					
					
						commit
						9e111d0a6d
					
				
					 11 changed files with 317 additions and 33 deletions
				
			
		
							
								
								
									
										6
									
								
								lib/Slic3r/Fill.pm
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								lib/Slic3r/Fill.pm
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,6 @@ | |||
| package Slic3r::Fill; | ||||
| use Moose; | ||||
| 
 | ||||
| use Slic3r::Fill::Rectilinear; | ||||
| 
 | ||||
| 1; | ||||
							
								
								
									
										221
									
								
								lib/Slic3r/Fill/Rectilinear.pm
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										221
									
								
								lib/Slic3r/Fill/Rectilinear.pm
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,221 @@ | |||
| package Slic3r::Fill::Rectilinear; | ||||
| use Moose; | ||||
| 
 | ||||
| use constant epsilon => 1E-10; | ||||
| use constant PI => 4 * atan2(1, 1); | ||||
| use constant X1 => 0; | ||||
| use constant Y1 => 1; | ||||
| use constant X2 => 2; | ||||
| use constant Y2 => 3; | ||||
| 
 | ||||
| use Math::Geometry::Planar; | ||||
| use XXX; | ||||
| 
 | ||||
| sub make_fill { | ||||
|     my $self = shift; | ||||
|     my ($print, $layer) = @_; | ||||
|     printf "Filling layer %d:\n", $layer->id; | ||||
|      | ||||
|     # let's alternate fill direction | ||||
|     my @axes = $layer->id % 2 == 0 ? (0,1) : (1,0); | ||||
|     printf "  primary axis: %d\n", $axes[0]; | ||||
|      | ||||
|     foreach my $surface (@{ $layer->fill_surfaces }) { | ||||
|         printf " Processing surface %s:\n", $surface->id; | ||||
|         my $polygon = $surface->mgp_polygon; | ||||
|          | ||||
|         # rotate surface as needed | ||||
|         if ($axes[0] == 1) { | ||||
|             $polygon = $polygon->rotate(PI/2)->move($print->x_length, $print->y_length); | ||||
|         } | ||||
|          | ||||
|         # force 100% density for external surfaces | ||||
|         my $density = $surface->surface_type eq 'internal' ? $Slic3r::fill_density : 1; | ||||
|         my $distance_between_lines = $Slic3r::flow_width / $Slic3r::resolution / $density; | ||||
|         my $number_of_lines = ($axes[0] == 0 ? $print->x_length : $print->y_length) / $distance_between_lines; | ||||
|          | ||||
|         #printf "distance_between_lines = %f\n", $distance_between_lines; | ||||
|         #printf "number_of_lines = %d\n", $number_of_lines; | ||||
|         #printf "axes = %d, %d\n", @axes; | ||||
|          | ||||
|         # this arrayref will hold intersection points of the fill grid with surface segments | ||||
|         my $points = [ map [], 0..$number_of_lines-1 ]; | ||||
|         foreach my $line (map $self->_lines_from_mgp_points($_), @{ $polygon->polygons }) { | ||||
|          | ||||
|             # for a possible implementation of "infill in direction of bridges" | ||||
|             # we should rotate $line so that primary axis is in detected direction; | ||||
|             # then, generated extrusion paths should be rotated back to the original | ||||
|             # coordinate system | ||||
|              | ||||
|             # find out the coordinates | ||||
|             my @coordinates = map @$_, @$line; | ||||
|             printf "Segment %d,%d - %d,%d\n", @coordinates; | ||||
|              | ||||
|             # get the extents of the segment along the primary axis | ||||
|             my @line_c = sort ($coordinates[X1], $coordinates[X2]); | ||||
|              | ||||
|             for (my $c = $line_c[0]; $c <= $line_c[1]; $c += $distance_between_lines) { | ||||
|                 my $i = sprintf('%.0f', $c / $distance_between_lines) - 1; | ||||
|                  | ||||
|                 # if the segment is parallel to our ray, there will be two intersection points | ||||
|                 if ($line_c[0] == $line_c[1]) { | ||||
|                     printf "  Segment is parallel!\n"; | ||||
|                     push @{ $points->[$i] }, $coordinates[Y1], $coordinates[Y2]; | ||||
|                     printf "   intersections at %f (%d) = %f, %f\n", $c, $i, $points->[$i][-2], $points->[$i][-1]; | ||||
|                 } else { | ||||
|                     printf "  Segment NOT parallel!\n"; | ||||
|                     # one point of intersection | ||||
|                     push @{ $points->[$i] }, $coordinates[Y1] + ($coordinates[Y2] - $coordinates[Y1]) | ||||
|                         * ($c - $coordinates[X1]) / ($coordinates[X2] - $coordinates[X1]); | ||||
|                     printf "   intersection at %f (%d) = %f\n", $c, $i, $points->[$i][-1]; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|          | ||||
|         # sort and remove duplicates | ||||
|         $points = [ | ||||
|             map { | ||||
|                 my %h = map { sprintf("%.0f", $_) => 1 } @$_; | ||||
|                 [ sort keys %h ]; | ||||
|             } @$points | ||||
|         ]; | ||||
|          | ||||
|         # generate extrusion paths | ||||
|         my (@paths, @path_points) = (); | ||||
|         my $direction = 0; | ||||
|          | ||||
|         my $stop_path = sub { | ||||
|             # defensive programming | ||||
|             if (@path_points == 1) { | ||||
|                 YYY \@path_points; | ||||
|                 die "There shouldn't be only one point in the current path"; | ||||
|             } | ||||
|                  | ||||
|             # if we were constructing a path, stop it | ||||
|             push @paths, [ @path_points ] if @path_points; | ||||
|             @path_points = (); | ||||
|         }; | ||||
|          | ||||
|         # loop until we have spare points | ||||
|         while (map @$_, @$points) { | ||||
|          | ||||
|             # loop through rows | ||||
|             ROW: for (my $i = 0; $i < $number_of_lines; $i++) { | ||||
|                 my $row = $points->[$i]; | ||||
|                 printf "Processing row %d...\n", $i; | ||||
|                 if (!@$row) { | ||||
|                     printf "  no points\n"; | ||||
|                     $stop_path->(); | ||||
|                     next ROW; | ||||
|                 } | ||||
|                 printf "  points = %s\n", join ', ', @$row; | ||||
|                  | ||||
|                 # coordinate of current row | ||||
|                 my $c = ($i + 1) * $distance_between_lines; | ||||
|                  | ||||
|                 # need to start a path? | ||||
|                 if (!@path_points) { | ||||
|                     push @path_points, [ $c, shift @$row ]; | ||||
|                 } | ||||
|                  | ||||
|                 my @connectable_points = $self->find_connectable_points($polygon, $path_points[-1], $c, $row); | ||||
|                 @connectable_points = reverse @connectable_points if $direction == 1; | ||||
|                 printf "  found %d connectable points = %s\n", scalar(@connectable_points), | ||||
|                     join ', ', @connectable_points; | ||||
|                  | ||||
|                 if (!@connectable_points && @path_points && $path_points[-1][0] != $c) { | ||||
|                     # no connectable in this row | ||||
|                     $stop_path->(); | ||||
|                 } | ||||
|                  | ||||
|                 foreach my $p (@connectable_points) { | ||||
|                     push @path_points, [ $c, $p ]; | ||||
|                     @$row = grep $_ != $p, @$row;  # remove point from row | ||||
|                 } | ||||
|                  | ||||
|                 # invert direction | ||||
|                 $direction = $direction ? 0 : 1; | ||||
|             } | ||||
|             $stop_path->() if @path_points; | ||||
|         } | ||||
|          | ||||
|         # paths must be rotated back | ||||
|         if ($axes[0] == 1) { | ||||
|             @paths = map $self->_mgp_from_points_ref($_)->move(-$print->x_length, -$print->y_length)->rotate(-PI()/2)->points, @paths; | ||||
|         } | ||||
|          | ||||
|         # save into layer | ||||
|         push @{ $layer->fills }, map Slic3r::ExtrusionPath->new_from_points(@$_), @paths; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| # this function will select the first contiguous block of  | ||||
| # points connectable to a given one | ||||
| sub find_connectable_points { | ||||
|     my $self = shift; | ||||
|     my ($polygon, $point, $c, $points) = @_; | ||||
|      | ||||
|     my @connectable_points = (); | ||||
|     foreach my $p (@$points) { | ||||
|         push @connectable_points, $p  | ||||
|             if $self->can_connect($polygon, $point, [ $c, $p ]); | ||||
|     } | ||||
|     return @connectable_points; | ||||
| } | ||||
| 
 | ||||
| # this subroutine tries to determine whether two points in a surface | ||||
| # are connectable without crossing contour or holes | ||||
| sub can_connect { | ||||
|     my $self = shift; | ||||
|     my ($polygon, $p1, $p2) = @_; | ||||
|      | ||||
|     # there's room for optimization here | ||||
|      | ||||
|     # this is not needed since we assume that $p1 and $p2 belong to $polygon | ||||
|     ###for ($p1, $p2) { | ||||
|         ###return 0 unless $polygon->isinside($_); | ||||
|     ###} | ||||
|      | ||||
|     # check whether the $p1-$p2 segment doesn't intersect any segment | ||||
|     # of the contour or of holes | ||||
|     foreach my $points (@{ $polygon->polygons }) { | ||||
|         foreach my $line ($self->_lines_from_mgp_points($points)) { | ||||
|             my $point = SegmentIntersection([$p1, $p2, @$line]); | ||||
|             if ($point && !$self->points_coincide($point, $p1) && !$self->points_coincide($point, $p2)) { | ||||
|                 return 0; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|      | ||||
|     return 1; | ||||
| } | ||||
| 
 | ||||
| sub points_coincide { | ||||
|     my $self = shift; | ||||
|     my ($p1, $p2) = @_; | ||||
|     return 0 if $p2->[0] - $p1->[0] < epsilon && $p2->[1] - $p1->[1] < epsilon; | ||||
|     return 1; | ||||
| } | ||||
| 
 | ||||
| sub _lines_from_mgp_points { | ||||
|     my $self = shift; | ||||
|     my ($points) = @_; | ||||
|      | ||||
|     my @lines = (); | ||||
|     my $last_point = $points->[-1]; | ||||
|     foreach my $point (@$points) { | ||||
|         push @lines, [ $last_point, $point ]; | ||||
|         $last_point = $point; | ||||
|     } | ||||
|     return @lines; | ||||
| } | ||||
| 
 | ||||
| sub _mgp_from_points_ref { | ||||
|     my $self = shift; | ||||
|     my ($points) = @_; | ||||
|     my $p = Math::Geometry::Planar->new; | ||||
|     $p->points($points); | ||||
|     return $p; | ||||
| } | ||||
| 
 | ||||
| 1; | ||||
|  | @ -55,6 +55,13 @@ has 'fill_surfaces' => ( | |||
|     default => sub { [] }, | ||||
| ); | ||||
| 
 | ||||
| # ordered collection of extrusion paths to fill surfaces | ||||
| has 'fills' => ( | ||||
|     is      => 'rw', | ||||
|     isa     => 'ArrayRef[Slic3r::ExtrusionPath]', | ||||
|     default => sub { [] }, | ||||
| ); | ||||
| 
 | ||||
| sub z { | ||||
|     my $self = shift; | ||||
|     return $self->id * $Slic3r::layer_height / $Slic3r::resolution; | ||||
|  |  | |||
|  | @ -45,6 +45,11 @@ sub id { | |||
|     return $self->a->id . "-" . $self->b->id; | ||||
| } | ||||
| 
 | ||||
| sub coordinates { | ||||
|     my $self = shift; | ||||
|     return ($self->a->coordinates, $self->b->coordinates); | ||||
| } | ||||
| 
 | ||||
| sub coincides_with { | ||||
|     my $self = shift; | ||||
|     my ($line) = @_; | ||||
|  | @ -55,7 +60,7 @@ sub coincides_with { | |||
| 
 | ||||
| sub has_endpoint { | ||||
|     my $self = shift; | ||||
|     my ($point) = @_;#printf "    %s has endpoint %s: %s\n", $self->id, $point->id, ($point eq $self->a || $point eq $self->b); | ||||
|     my ($point) = @_; | ||||
|     return $point->coincides_with($self->a) || $point->coincides_with($self->b); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -44,7 +44,8 @@ sub make_perimeter { | |||
|          | ||||
|         # create one more offset to be used as boundary for fill | ||||
|         push @{ $layer->fill_surfaces },  | ||||
|             map Slic3r::Surface->new_from_mgp($_), $self->offset_polygon($perimeters[-1]); | ||||
|             map Slic3r::Surface->new_from_mgp($_, surface_type => $surface->surface_type),  | ||||
|                 $self->offset_polygon($perimeters[-1]); | ||||
|     } | ||||
|      | ||||
|     # generate paths for holes | ||||
|  |  | |||
|  | @ -31,6 +31,11 @@ sub id { | |||
|     return $self->x . "," . $self->y; #;; | ||||
| } | ||||
| 
 | ||||
| sub coordinates { | ||||
|     my $self = shift; | ||||
|     return ($self->x, $self->y); #)) | ||||
| } | ||||
| 
 | ||||
| sub coincides_with { | ||||
|     my $self = shift; | ||||
|     my ($point) = @_; | ||||
|  |  | |||
|  | @ -55,6 +55,19 @@ sub extrude_perimeters { | |||
|     } | ||||
| } | ||||
| 
 | ||||
| sub extrude_fills { | ||||
|     my $self = shift; | ||||
|      | ||||
|     my $fill_extruder = Slic3r::Fill::Rectilinear->new; | ||||
|      | ||||
|     foreach my $layer (@{ $self->layers }) { | ||||
|         $fill_extruder->make_fill($self, $layer); | ||||
|         printf "  generated %d paths: %s\n", | ||||
|             scalar @{ $layer->fills }, | ||||
|             join '  ', map $_->id, @{ $layer->fills }; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| sub export_gcode { | ||||
|     my $self = shift; | ||||
|     my ($file) = @_; | ||||
|  | @ -107,9 +120,32 @@ sub export_gcode { | |||
|         print  $fh "\n"; | ||||
|     }; | ||||
|      | ||||
|     my $z; | ||||
|     my $Extrude = sub { | ||||
|         my ($path, $description) = @_; | ||||
|          | ||||
|         # reset extrusion distance counter | ||||
|         my $extrusion_distance = 0; | ||||
|         if (!$Slic3r::use_relative_e_distances) { | ||||
|             print $fh "G92 E0 ; reset extrusion distance\n"; | ||||
|         } | ||||
|          | ||||
|         # go to first point (without extruding) | ||||
|         $G1->($path->lines->[0]->a, $z, 0, "move to first $description point"); | ||||
|          | ||||
|         # extrude while going to next points | ||||
|         foreach my $line (@{ $path->lines }) { | ||||
|             $extrusion_distance = 0 if $Slic3r::use_relative_e_distances; | ||||
|             $extrusion_distance += $line->a->distance_to($line->b); | ||||
|             $G1->($line->b, $z, $extrusion_distance, $description); | ||||
|         } | ||||
|          | ||||
|         # TODO: retraction | ||||
|     }; | ||||
|      | ||||
|     # write gcode commands layer by layer | ||||
|     foreach my $layer (@{ $self->layers }) { | ||||
|         my $z = ($layer->z * $Slic3r::resolution); | ||||
|         $z = ($layer->z * $Slic3r::resolution); | ||||
|          | ||||
|         # go to layer | ||||
|         # TODO: retraction | ||||
|  | @ -117,24 +153,10 @@ sub export_gcode { | |||
|             $z, $travel_feed_rate; | ||||
|          | ||||
|         # extrude perimeters | ||||
|         foreach my $perimeter (@{ $layer->perimeters }) { | ||||
|              | ||||
|             # reset extrusion distance counter | ||||
|             my $extrusion_distance = 0; | ||||
|             if (!$Slic3r::use_relative_e_distances) { | ||||
|                 print $fh "G92 E0 ; reset extrusion distance\n"; | ||||
|             } | ||||
|              | ||||
|             # go to first point (without extruding) | ||||
|             $G1->($perimeter->lines->[0]->a, $z, 0, 'move to first perimeter point'); | ||||
|              | ||||
|             # extrude while going to next points | ||||
|             foreach my $line (@{ $perimeter->lines }) { | ||||
|                 $extrusion_distance = 0 if $Slic3r::use_relative_e_distances; | ||||
|                 $extrusion_distance += $line->a->distance_to($line->b); | ||||
|                 $G1->($line->b, $z, $extrusion_distance, 'perimeter'); | ||||
|             } | ||||
|         } | ||||
|         $Extrude->($_, 'perimeter') for @{ $layer->perimeters }; | ||||
|          | ||||
|         # extrude fills | ||||
|         $Extrude->($_, 'fill') for @{ $layer->fills }; | ||||
|     } | ||||
|      | ||||
|     # write end commands to file | ||||
|  |  | |||
|  | @ -20,6 +20,8 @@ has 'holes' => ( | |||
|     }, | ||||
| ); | ||||
| 
 | ||||
| # TODO: to allow for multiple solid skins to be filled near external | ||||
| # surfaces, a new type should be defined: internal-solid | ||||
| has 'surface_type' => ( | ||||
|     is      => 'rw', | ||||
|     isa     => enum([qw(internal bottom top)]), | ||||
|  | @ -44,7 +46,7 @@ sub BUILD { | |||
| 
 | ||||
| sub new_from_mgp { | ||||
|     my $self = shift; | ||||
|     my ($polygon) = @_; | ||||
|     my ($polygon, %params) = @_; | ||||
|      | ||||
|     my ($contour_p, @holes_p) = @{ $polygon->polygons }; | ||||
|      | ||||
|  | @ -53,6 +55,7 @@ sub new_from_mgp { | |||
|         holes   => [ | ||||
|             map Slic3r::Polyline::Closed->new_from_points(@$_), @holes_p | ||||
|         ], | ||||
|         %params, | ||||
|     ); | ||||
| } | ||||
| 
 | ||||
|  | @ -78,4 +81,9 @@ sub mgp_polygon { | |||
|     return $p; | ||||
| } | ||||
| 
 | ||||
| sub lines { | ||||
|     my $self = shift; | ||||
|     return @{ $self->contour->lines }, map @{ $_->lines }, @{ $self->holes }; | ||||
| } | ||||
| 
 | ||||
| 1; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Alessandro Ranellucci
						Alessandro Ranellucci