Rectilinear fill
This commit is contained in:
parent
428006264d
commit
9e111d0a6d
@ -28,20 +28,26 @@ Also, http://xkcd.com/224/
|
|||||||
|
|
||||||
## What's its current status?
|
## What's its current status?
|
||||||
|
|
||||||
Slic3r can now successfully parse and analyze an STL file by slicing it in
|
Slic3r is able to:
|
||||||
layers and representing internally the following features:
|
|
||||||
|
|
||||||
* holes in surfaces;
|
* read binary and ASCII STL files;
|
||||||
* external top/bottom surfaces.
|
* generate multiple perimeters (skins);
|
||||||
|
* generate rectilinear feed (100% solid for external surfaces or with customizable less density for inner surfaces);
|
||||||
|
* use relative or absolute extrusion commands;
|
||||||
|
* center print around bed center point;
|
||||||
|
* output relevant GCODE.
|
||||||
|
|
||||||
This kind of abstraction will allow to implement particular logic and allow the
|
Roadmap include the following goals:
|
||||||
user to specify custom options.
|
|
||||||
|
|
||||||
It is also able to generate perimeters and to produce working GCODE.
|
* set up a command line interface and hide debug messages;
|
||||||
To reach a minimum level of usability, I need to implement an algorithm to generate
|
* output some statistics;
|
||||||
surface fill.
|
* allow the user to customize initial and final GCODE commands;
|
||||||
|
* option for filling multiple solid layers near external surfaces;
|
||||||
Future goals include support material, options to control bridges, skirt, cool.
|
* support material for internal perimeters;
|
||||||
|
* ability to infill in the direction of bridges;
|
||||||
|
* skirt;
|
||||||
|
* cool;
|
||||||
|
* nice packaging for cross-platform deployment.
|
||||||
|
|
||||||
## Is it usable already?
|
## Is it usable already?
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ use strict;
|
|||||||
use warnings;
|
use warnings;
|
||||||
|
|
||||||
use Slic3r::ExtrusionPath;
|
use Slic3r::ExtrusionPath;
|
||||||
|
use Slic3r::Fill;
|
||||||
use Slic3r::Layer;
|
use Slic3r::Layer;
|
||||||
use Slic3r::Line;
|
use Slic3r::Line;
|
||||||
use Slic3r::Perimeter;
|
use Slic3r::Perimeter;
|
||||||
@ -17,6 +18,7 @@ use Slic3r::Surface;
|
|||||||
our $layer_height = 0.4;
|
our $layer_height = 0.4;
|
||||||
our $resolution = 0.1;
|
our $resolution = 0.1;
|
||||||
our $perimeter_offsets = 3;
|
our $perimeter_offsets = 3;
|
||||||
|
our $fill_density = 0.2; # 1 = 100%
|
||||||
our $flow_width = 0.4; # TODO: verify this is a multiple of $resolution
|
our $flow_width = 0.4; # TODO: verify this is a multiple of $resolution
|
||||||
our $temperature = 195;
|
our $temperature = 195;
|
||||||
|
|
||||||
|
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 { [] },
|
default => sub { [] },
|
||||||
);
|
);
|
||||||
|
|
||||||
|
# ordered collection of extrusion paths to fill surfaces
|
||||||
|
has 'fills' => (
|
||||||
|
is => 'rw',
|
||||||
|
isa => 'ArrayRef[Slic3r::ExtrusionPath]',
|
||||||
|
default => sub { [] },
|
||||||
|
);
|
||||||
|
|
||||||
sub z {
|
sub z {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
return $self->id * $Slic3r::layer_height / $Slic3r::resolution;
|
return $self->id * $Slic3r::layer_height / $Slic3r::resolution;
|
||||||
|
@ -45,6 +45,11 @@ sub id {
|
|||||||
return $self->a->id . "-" . $self->b->id;
|
return $self->a->id . "-" . $self->b->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub coordinates {
|
||||||
|
my $self = shift;
|
||||||
|
return ($self->a->coordinates, $self->b->coordinates);
|
||||||
|
}
|
||||||
|
|
||||||
sub coincides_with {
|
sub coincides_with {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my ($line) = @_;
|
my ($line) = @_;
|
||||||
@ -55,7 +60,7 @@ sub coincides_with {
|
|||||||
|
|
||||||
sub has_endpoint {
|
sub has_endpoint {
|
||||||
my $self = shift;
|
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);
|
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
|
# create one more offset to be used as boundary for fill
|
||||||
push @{ $layer->fill_surfaces },
|
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
|
# generate paths for holes
|
||||||
|
@ -31,6 +31,11 @@ sub id {
|
|||||||
return $self->x . "," . $self->y; #;;
|
return $self->x . "," . $self->y; #;;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub coordinates {
|
||||||
|
my $self = shift;
|
||||||
|
return ($self->x, $self->y); #))
|
||||||
|
}
|
||||||
|
|
||||||
sub coincides_with {
|
sub coincides_with {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my ($point) = @_;
|
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 {
|
sub export_gcode {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my ($file) = @_;
|
my ($file) = @_;
|
||||||
@ -107,17 +120,9 @@ sub export_gcode {
|
|||||||
print $fh "\n";
|
print $fh "\n";
|
||||||
};
|
};
|
||||||
|
|
||||||
# write gcode commands layer by layer
|
my $z;
|
||||||
foreach my $layer (@{ $self->layers }) {
|
my $Extrude = sub {
|
||||||
my $z = ($layer->z * $Slic3r::resolution);
|
my ($path, $description) = @_;
|
||||||
|
|
||||||
# go to layer
|
|
||||||
# TODO: retraction
|
|
||||||
printf $fh "G1 Z%.${dec}f F%.${dec}f ; move to next layer\n",
|
|
||||||
$z, $travel_feed_rate;
|
|
||||||
|
|
||||||
# extrude perimeters
|
|
||||||
foreach my $perimeter (@{ $layer->perimeters }) {
|
|
||||||
|
|
||||||
# reset extrusion distance counter
|
# reset extrusion distance counter
|
||||||
my $extrusion_distance = 0;
|
my $extrusion_distance = 0;
|
||||||
@ -126,15 +131,32 @@ sub export_gcode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# go to first point (without extruding)
|
# go to first point (without extruding)
|
||||||
$G1->($perimeter->lines->[0]->a, $z, 0, 'move to first perimeter point');
|
$G1->($path->lines->[0]->a, $z, 0, "move to first $description point");
|
||||||
|
|
||||||
# extrude while going to next points
|
# extrude while going to next points
|
||||||
foreach my $line (@{ $perimeter->lines }) {
|
foreach my $line (@{ $path->lines }) {
|
||||||
$extrusion_distance = 0 if $Slic3r::use_relative_e_distances;
|
$extrusion_distance = 0 if $Slic3r::use_relative_e_distances;
|
||||||
$extrusion_distance += $line->a->distance_to($line->b);
|
$extrusion_distance += $line->a->distance_to($line->b);
|
||||||
$G1->($line->b, $z, $extrusion_distance, 'perimeter');
|
$G1->($line->b, $z, $extrusion_distance, $description);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# TODO: retraction
|
||||||
|
};
|
||||||
|
|
||||||
|
# write gcode commands layer by layer
|
||||||
|
foreach my $layer (@{ $self->layers }) {
|
||||||
|
$z = ($layer->z * $Slic3r::resolution);
|
||||||
|
|
||||||
|
# go to layer
|
||||||
|
# TODO: retraction
|
||||||
|
printf $fh "G1 Z%.${dec}f F%.${dec}f ; move to next layer\n",
|
||||||
|
$z, $travel_feed_rate;
|
||||||
|
|
||||||
|
# extrude perimeters
|
||||||
|
$Extrude->($_, 'perimeter') for @{ $layer->perimeters };
|
||||||
|
|
||||||
|
# extrude fills
|
||||||
|
$Extrude->($_, 'fill') for @{ $layer->fills };
|
||||||
}
|
}
|
||||||
|
|
||||||
# write end commands to file
|
# 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' => (
|
has 'surface_type' => (
|
||||||
is => 'rw',
|
is => 'rw',
|
||||||
isa => enum([qw(internal bottom top)]),
|
isa => enum([qw(internal bottom top)]),
|
||||||
@ -44,7 +46,7 @@ sub BUILD {
|
|||||||
|
|
||||||
sub new_from_mgp {
|
sub new_from_mgp {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my ($polygon) = @_;
|
my ($polygon, %params) = @_;
|
||||||
|
|
||||||
my ($contour_p, @holes_p) = @{ $polygon->polygons };
|
my ($contour_p, @holes_p) = @{ $polygon->polygons };
|
||||||
|
|
||||||
@ -53,6 +55,7 @@ sub new_from_mgp {
|
|||||||
holes => [
|
holes => [
|
||||||
map Slic3r::Polyline::Closed->new_from_points(@$_), @holes_p
|
map Slic3r::Polyline::Closed->new_from_points(@$_), @holes_p
|
||||||
],
|
],
|
||||||
|
%params,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,4 +81,9 @@ sub mgp_polygon {
|
|||||||
return $p;
|
return $p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub lines {
|
||||||
|
my $self = shift;
|
||||||
|
return @{ $self->contour->lines }, map @{ $_->lines }, @{ $self->holes };
|
||||||
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
@ -15,6 +15,7 @@ my $stl_parser = Slic3r::STL->new;
|
|||||||
my $print = $stl_parser->parse_file("testcube20mm.stl");
|
my $print = $stl_parser->parse_file("testcube20mm.stl");
|
||||||
|
|
||||||
$print->extrude_perimeters;
|
$print->extrude_perimeters;
|
||||||
|
$print->extrude_fills;
|
||||||
|
|
||||||
$print->export_gcode("testcube20mm.gcode");
|
$print->export_gcode("testcube20mm.gcode");
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user