Detection of optimal infill direction for bridges. Includes many fixes and improvements.

This commit is contained in:
Alessandro Ranellucci 2011-10-07 19:07:57 +02:00
parent 1cb515a8e5
commit 743f2abcf2
18 changed files with 445 additions and 68 deletions

View File

@ -41,6 +41,8 @@ Slic3r current features are:
* skirt (with rounded corners); * skirt (with rounded corners);
* use relative or absolute extrusion commands; * use relative or absolute extrusion commands;
* high-res perimeters (like the "Skin" plugin for Skeinforge); * high-res perimeters (like the "Skin" plugin for Skeinforge);
* detect optimal infill direction for bridges;
* save configuration profiles;
* center print around bed center point; * center print around bed center point;
* multiple solid layers near horizontal external surfaces; * multiple solid layers near horizontal external surfaces;
* ability to scale, rotate and multiply input object; * ability to scale, rotate and multiply input object;
@ -51,10 +53,8 @@ Roadmap includes the following goals:
* output some statistics; * output some statistics;
* allow the user to customize initial and final GCODE commands; * allow the user to customize initial and final GCODE commands;
* support material for internal perimeters; * support material for internal perimeters;
* detect optimal infill direction for bridges;
* cool; * cool;
* other fill patterns; * other fill patterns.
* nice packaging for cross-platform deployment.
## Is it usable already? ## Is it usable already?

View File

@ -26,6 +26,7 @@ use Slic3r::Print;
use Slic3r::Skein; use Slic3r::Skein;
use Slic3r::STL; use Slic3r::STL;
use Slic3r::Surface; use Slic3r::Surface;
use Slic3r::Surface::Bridge;
use Slic3r::Surface::Collection; use Slic3r::Surface::Collection;
# printer options # printer options

View File

@ -5,6 +5,8 @@ use Slic3r::Fill::Base;
use Slic3r::Fill::Rectilinear; use Slic3r::Fill::Rectilinear;
use Slic3r::Fill::Rectilinear2; use Slic3r::Fill::Rectilinear2;
use XXX;
has 'print' => (is => 'ro', required => 1); has 'print' => (is => 'ro', required => 1);
has 'fillers' => (is => 'rw', default => sub { {} }); has 'fillers' => (is => 'rw', default => sub { {} });

View File

@ -10,7 +10,7 @@ use constant PI => 4 * atan2(1, 1);
sub infill_direction { sub infill_direction {
my $self = shift; my $self = shift;
my ($polygons) = @_; my ($surface) = @_;
# set infill angle # set infill angle
my (@rotate, @shift); my (@rotate, @shift);
@ -23,7 +23,11 @@ sub infill_direction {
$rotate[0] = Slic3r::Geometry::deg2rad($Slic3r::fill_angle) + PI/2; $rotate[0] = Slic3r::Geometry::deg2rad($Slic3r::fill_angle) + PI/2;
} }
# TODO: here we should implement an "infill in direction of bridges" option # use bridge angle
if ($surface->isa('Slic3r::Surface::Bridge')) {
Slic3r::debugf "Filling bridge with angle %d\n", $surface->bridge_angle;
$rotate[0] = Slic3r::Geometry::deg2rad($surface->bridge_angle);
}
@shift = @{ +(Slic3r::Geometry::rotate_points(@rotate, \@shift))[0] }; @shift = @{ +(Slic3r::Geometry::rotate_points(@rotate, \@shift))[0] };
return [\@rotate, \@shift]; return [\@rotate, \@shift];

View File

@ -16,7 +16,7 @@ sub fill_surface {
# rotate polygons so that we can work with vertical lines here # rotate polygons so that we can work with vertical lines here
my $polygons = [ $surface->p ]; my $polygons = [ $surface->p ];
my $rotate_vector = $self->infill_direction($polygons); my $rotate_vector = $self->infill_direction($surface);
$self->rotate_points($polygons, $rotate_vector); $self->rotate_points($polygons, $rotate_vector);
my $bounding_box = [ Slic3r::Geometry::bounding_box(map @$_, $polygons) ]; my $bounding_box = [ Slic3r::Geometry::bounding_box(map @$_, $polygons) ];

View File

@ -21,7 +21,7 @@ sub fill_surface {
my $polygons = [ $surface->p ]; my $polygons = [ $surface->p ];
# rotate polygons so that we can work with vertical lines here # rotate polygons so that we can work with vertical lines here
my $rotate_vector = $self->infill_direction($polygons); my $rotate_vector = $self->infill_direction($surface);
$self->rotate_points($polygons, $rotate_vector); $self->rotate_points($polygons, $rotate_vector);
my $distance_between_lines = $Slic3r::flow_width / $Slic3r::resolution / $params{density}; my $distance_between_lines = $Slic3r::flow_width / $Slic3r::resolution / $params{density};

View File

@ -10,9 +10,11 @@ use constant A => 0;
use constant B => 1; use constant B => 1;
use constant X => 0; use constant X => 0;
use constant Y => 1; use constant Y => 1;
use constant epsilon => 1E-4;
our $parallel_degrees_limit = abs(deg2rad(3)); our $parallel_degrees_limit = abs(deg2rad(3));
our $epsilon = 1E-4;
sub epsilon () { $epsilon }
sub slope { sub slope {
my ($line) = @_; my ($line) = @_;
return undef if abs($line->[B][X] - $line->[A][X]) < epsilon; # line is vertical return undef if abs($line->[B][X] - $line->[A][X]) < epsilon; # line is vertical
@ -60,6 +62,16 @@ sub distance_between_points {
return sqrt((($p1->[X] - $p2->[X])**2) + ($p1->[Y] - $p2->[Y])**2); return sqrt((($p1->[X] - $p2->[X])**2) + ($p1->[Y] - $p2->[Y])**2);
} }
sub line_length {
my ($line) = @_;
return distance_between_points(@$line[A, B]);
}
sub midpoint {
my ($line) = @_;
return [ ($line->[B][X] + $line->[A][X]) / 2, ($line->[B][Y] + $line->[A][Y]) / 2 ];
}
sub point_in_polygon { sub point_in_polygon {
my ($point, $polygon) = @_; my ($point, $polygon) = @_;
@ -118,6 +130,13 @@ sub point_in_segment {
return abs($y3 - $y) < epsilon ? 1 : 0; return abs($y3 - $y) < epsilon ? 1 : 0;
} }
sub segment_in_segment {
my ($needle, $haystack) = @_;
# a segment is contained in another segment if its endpoints are contained
return point_in_segment($needle->[A], $haystack) && point_in_segment($needle->[B], $haystack);
}
sub point_is_on_left_of_segment { sub point_is_on_left_of_segment {
my ($point, $line) = @_; my ($point, $line) = @_;
@ -125,19 +144,24 @@ sub point_is_on_left_of_segment {
- ($line->[B][Y] - $line->[A][Y])*($point->[X] - $line->[A][X])) > 0; - ($line->[B][Y] - $line->[A][Y])*($point->[X] - $line->[A][X])) > 0;
} }
sub polygon_lines { sub polyline_lines {
my ($polygon) = @_; my ($polygon) = @_;
my @lines = (); my @lines = ();
my $last_point = $polygon->[-1]; my $last_point;
foreach my $point (@$polygon) { foreach my $point (@$polygon) {
push @lines, [ $last_point, $point ]; push @lines, [ $last_point, $point ] if $last_point;
$last_point = $point; $last_point = $point;
} }
return @lines; return @lines;
} }
sub polygon_lines {
my ($polygon) = @_;
return polyline_lines([ @$polygon, $polygon->[0] ]);
}
sub nearest_point { sub nearest_point {
my ($point, $points) = @_; my ($point, $points) = @_;
@ -179,6 +203,30 @@ sub polygon_segment_having_point {
return undef; return undef;
} }
# return true if the given segment is contained in any edge of the polygon
sub polygon_has_subsegment {
my ($polygon, $segment) = @_;
foreach my $line (polygon_lines($polygon)) {
return 1 if segment_in_segment($segment, $line);
}
return 0;
}
sub polygon_has_vertex {
my ($polygon, $point) = @_;
foreach my $p (@$polygon) {
return 1 if points_coincide($p, $point);
}
return 0;
}
sub polyline_length {
my ($polyline) = @_;
my $length = 0;
$length += line_length($_) for polygon_lines($polyline);
return $length;
}
sub can_connect_points { sub can_connect_points {
my ($p1, $p2, $polygons) = @_; my ($p1, $p2, $polygons) = @_;
@ -207,6 +255,11 @@ sub deg2rad {
return PI() * $degrees / 180; return PI() * $degrees / 180;
} }
sub rad2deg {
my ($rad) = @_;
return $rad / PI() * 180;
}
sub rotate_points { sub rotate_points {
my ($radians, $center, @points) = @_; my ($radians, $center, @points) = @_;
$center ||= [0,0]; $center ||= [0,0];

View File

@ -2,8 +2,11 @@ package Slic3r::Layer;
use Moo; use Moo;
use Math::Clipper ':all'; use Math::Clipper ':all';
use Math::ConvexHull qw(convex_hull);
use XXX; use XXX;
use constant PI => 4 * atan2(1, 1);
# a sequential number of layer, starting at 0 # a sequential number of layer, starting at 0
has 'id' => ( has 'id' => (
is => 'ro', is => 'ro',
@ -26,17 +29,31 @@ has 'surfaces' => (
default => sub { [] }, default => sub { [] },
); );
# collection of surfaces representing bridges
has 'bridges' => (
is => 'rw',
#isa => 'ArrayRef[Slic3r::Surface::Bridge]',
default => sub { [] },
);
# collection of surfaces to make perimeters for
has 'perimeter_surfaces' => (
is => 'rw',
#isa => 'ArrayRef[Slic3r::Surface]',
default => sub { [] },
);
# ordered collection of extrusion paths to build all perimeters # ordered collection of extrusion paths to build all perimeters
has 'perimeters' => ( has 'perimeters' => (
is => 'rw', is => 'rw',
#isa => 'ArrayRef[Slic3r::ExtrusionPath]', #isa => 'ArrayRef[Slic3r::ExtrusionLoop]',
default => sub { [] }, default => sub { [] },
); );
# ordered collection of extrusion paths to build skirt loops # ordered collection of extrusion paths to build skirt loops
has 'skirts' => ( has 'skirts' => (
is => 'rw', is => 'rw',
#isa => 'ArrayRef[Slic3r::ExtrusionPath]', #isa => 'ArrayRef[Slic3r::ExtrusionLoop]',
default => sub { [] }, default => sub { [] },
); );
@ -44,7 +61,7 @@ has 'skirts' => (
# they represent boundaries of areas to fill # they represent boundaries of areas to fill
has 'fill_surfaces' => ( has 'fill_surfaces' => (
is => 'rw', is => 'rw',
#isa => 'ArrayRef[Slic3r::Surface]', #isa => 'ArrayRef[Slic3r::Surface::Collection]',
default => sub { [] }, default => sub { [] },
); );
@ -101,18 +118,34 @@ sub remove_surface {
} }
# build polylines of lines which do not already belong to a surface # build polylines of lines which do not already belong to a surface
# okay, this code is a mess. will need some refactoring. sorry.
sub make_polylines { sub make_polylines {
my $self = shift; my $self = shift;
# remove line duplicates # remove line duplicates
{ if (0) {
# this removes any couple of coinciding Slic3r::Line::FacetEdge
my %lines_map = ();
foreach my $line (grep $_->isa('Slic3r::Line::FacetEdge'), @{ $self->lines }) {
my $ordered_id = $line->ordered_id;
if (exists $lines_map{$ordered_id}) {
delete $lines_map{$ordered_id};
next;
}
$lines_map{$ordered_id} = $line;
}
@{ $self->lines } = (values(%lines_map), grep !$_->isa('Slic3r::Line::FacetEdge'), @{ $self->lines });
}
if (1) {
# this removes any duplicate, leaving one
my %lines_map = map { join(',', sort map $_->id, @{$_->points} ) => "$_" } @{ $self->lines }; my %lines_map = map { join(',', sort map $_->id, @{$_->points} ) => "$_" } @{ $self->lines };
%lines_map = reverse %lines_map; %lines_map = reverse %lines_map;
@{ $self->lines } = grep $lines_map{"$_"}, @{ $self->lines }; @{ $self->lines } = grep $lines_map{"$_"}, @{ $self->lines };
} }
# now remove lines that are already part of a surface # now remove lines that are already part of a surface
{ if (1) {
my @lines = @{ $self->lines }; my @lines = @{ $self->lines };
@{ $self->lines } = (); @{ $self->lines } = ();
LINE: foreach my $line (@lines) { LINE: foreach my $line (@lines) {
@ -130,26 +163,31 @@ sub make_polylines {
} }
# make a cache of line endpoints # make a cache of line endpoints
my %pointmap = (); my (%pointmap) = ();
foreach my $line (@{ $self->lines }) { foreach my $line (@{ $self->lines }) {
for my $point (@{ $line->points }) { for my $point (@{ $line->points }) {
$pointmap{$point->id} ||= []; $pointmap{$point->id} ||= [];
push @{ $pointmap{$point->id} }, $line; push @{ $pointmap{$point->id} }, $line;
} }
} }
foreach my $point_id (keys %pointmap) {
# defensive programming $pointmap{$point_id} = [
#die "No point should be endpoint of less or more than 2 lines!" sort { $a->isa('Slic3r::Line::FacetEdge') <=> $b->isa('Slic3r::Line::FacetEdge') }
# if grep @$_ != 2, values %pointmap; @{$pointmap{$point_id}} ];
}
if (0) { if (0) {
# defensive programming # defensive programming
for (keys %pointmap) { for (keys %pointmap) {
next if @{$pointmap{$_}} == 2; next if @{$pointmap{$_}} == 2;
#use Slic3r::SVG; use Slic3r::SVG;
#Slic3r::SVG::output_points($main::print, "points.svg", [ map [split /,/], keys %pointmap ], [ [split /,/, $_ ] ]); Slic3r::SVG::output(undef, "lines_and_points.svg",
#Slic3r::SVG::output_lines($main::print, "lines.svg", [ map $_->p, @{$self->lines} ]); lines => [ map $_->p, grep !$_->isa('Slic3r::Line::FacetEdge'), @{$self->lines} ],
red_lines => [ map $_->p, grep $_->isa('Slic3r::Line::FacetEdge'), @{$self->lines} ],
points => [ map [split /,/], keys %pointmap ],
red_points => [ [split /,/, $_ ] ],
);
YYY $pointmap{$_}; YYY $pointmap{$_};
@ -198,6 +236,11 @@ sub make_polylines {
# remove last point as it coincides with first one # remove last point as it coincides with first one
pop @$points; pop @$points;
if (@$points == 1 && $first_line->isa('Slic3r::Line::FacetEdge')) {
Slic3r::debugf "Skipping spare facet edge";
next;
}
die sprintf "Invalid polyline with only %d points\n", scalar(@$points) if @$points < 3; die sprintf "Invalid polyline with only %d points\n", scalar(@$points) if @$points < 3;
Slic3r::debugf "Discovered polyline of %d points (%s)\n", scalar @$points, Slic3r::debugf "Discovered polyline of %d points (%s)\n", scalar @$points,
@ -336,6 +379,21 @@ sub merge_contiguous_surfaces {
$resulting_surfaces{$type} = $result2; $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 # save surfaces
@{ $self->surfaces } = (); @{ $self->surfaces } = ();
foreach my $type (keys %resulting_surfaces) { foreach my $type (keys %resulting_surfaces) {
@ -357,23 +415,181 @@ sub merge_contiguous_surfaces {
} }
} }
sub remove_small_features { sub remove_small_surfaces {
my $self = shift; my $self = shift;
my @good_surfaces = ();
# for each perimeter, try to get an inwards offset foreach my $surface (@{$self->surfaces}) {
# for a distance equal to half of the extrusion width; next if !$surface->contour->is_printable;
# if no offset is possible, then feature is not printable @{$surface->holes} = grep $_->is_printable, @{$surface->holes};
my @good_perimeters = (); push @good_surfaces, $surface;
foreach my $loop (@{$self->perimeters}) {
my $p = $loop->p;
@$p = reverse @$p if !is_counter_clockwise($p);
my $offsets = offset([$p], -($Slic3r::flow_width / 2 / $Slic3r::resolution), $Slic3r::resolution * 100000, JT_MITER, 2);
push @good_perimeters, $loop if @$offsets;
} }
@{$self->surfaces} = @good_surfaces;
}
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\n", (@{$self->perimeters} - @good_perimeters)
if @good_perimeters != @{$self->perimeters}; if @good_perimeters != @{$self->perimeters};
@{$self->perimeters} = @good_perimeters; @{$self->perimeters} = @good_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
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 = convex_hull($surface->contour->p);
# find all supported edges (as polylines, thus keeping notion of
# consecutive supported edges)
my @supported_polylines = ();
{
my @current_polyline = ();
EDGE: foreach my $edge (Slic3r::Geometry::polygon_lines($surface_p)) {
for (@supporting_surfaces) {
local $Slic3r::Geometry::epsilon = 1E+7;
if (Slic3r::Geometry::polygon_has_subsegment($_->contour->p, $edge)) {
push @current_polyline, $edge;
next EDGE;
}
}
if (@current_polyline) {
push @supported_polylines, [@current_polyline];
@current_polyline = ();
}
}
push @supported_polylines, [@current_polyline] if @current_polyline;
}
# 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;
next SURFACE;
}
if (@supported_polylines == 1) {
Slic3r::debugf "Found bridge/overhang 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;
# 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[0,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
{
# offset the bridge by 5mm
my $bridge_offset = ${ offset([$surface_p], 5 / $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',
bridge_angle => $bridge_angle,
), @$intersection;
}
}
}
# generates a set of surfaces that will be used to make perimeters
# thus, we need to merge internal surfaces and bridges
sub detect_perimeter_surfaces {
my $self = shift;
# little optimization: skip the Clipper UNION if we have no bridges
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);
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};
}
}
# splits fill_surfaces in internal and bridge surfaces
sub split_bridges_fills {
my $self = shift;
my $clipper = Math::Clipper->new;
foreach my $surf_coll (@{$self->fill_surfaces}) {
my @surfaces = @{$surf_coll->surfaces};
@{$surf_coll->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);
push @{$surf_coll->surfaces}, map Slic3r::Surface::Bridge->cast_from_expolygon($_,
surface_type => 'bottom',
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);
push @{$surf_coll->surfaces}, map Slic3r::Surface->cast_from_expolygon($_,
surface_type => $surface->surface_type), @$difference;
}
}
}
1; 1;

View File

@ -27,6 +27,11 @@ sub id {
return $self->a->id . "-" . $self->b->id; return $self->a->id . "-" . $self->b->id;
} }
sub ordered_id {
my $self = shift;
return join('-', sort map $_->id, @{$self->points});
}
sub coordinates { sub coordinates {
my $self = shift; my $self = shift;
return ($self->a->coordinates, $self->b->coordinates); return ($self->a->coordinates, $self->b->coordinates);

View File

@ -18,7 +18,7 @@ sub make_perimeter {
if $Slic3r::perimeter_offsets == 0; if $Slic3r::perimeter_offsets == 0;
my (%contours, %holes) = (); my (%contours, %holes) = ();
foreach my $surface (@{ $layer->surfaces }) { foreach my $surface (@{ $layer->perimeter_surfaces }) {
$contours{$surface} = []; $contours{$surface} = [];
$holes{$surface} = []; $holes{$surface} = [];
my @last_offsets = (); my @last_offsets = ();
@ -47,17 +47,16 @@ 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 }, Slic3r::Surface::Collection->new( {
surfaces => [ my @fill_surfaces = map Slic3r::Surface->cast_from_expolygon(
map Slic3r::Surface->new( $_,
surface_type => $surface->surface_type, surface_type => $surface->surface_type,
contour => Slic3r::Polyline::Closed->cast($_->{outer}), ), map $self->offset_polygon($_), @last_offsets;
holes => [
map Slic3r::Polyline::Closed->cast($_), @{$_->{holes}} push @{ $layer->fill_surfaces }, Slic3r::Surface::Collection->new(
], surfaces => [@fill_surfaces],
), map $self->offset_polygon($_), @last_offsets ) if @fill_surfaces;
], }
);
} }
# generate paths for holes: # generate paths for holes:
@ -91,15 +90,13 @@ sub make_perimeter {
sub offset_polygon { sub offset_polygon {
my $self = shift; my $self = shift;
my ($polygon) = @_; my ($polygon) = @_;
my $distance = $Slic3r::flow_width / $Slic3r::resolution;
# $polygon holds a Math::Clipper ExPolygon hashref representing # $polygon holds a Math::Clipper ExPolygon hashref representing
# a polygon and its holes # a polygon and its holes
my ($contour_p, @holes_p) = ($polygon->{outer}, @{$polygon->{holes}});
# generate offsets # generate offsets
my $offsets = offset([ $contour_p, @holes_p ], -$distance, $Slic3r::resolution * 100000, JT_MITER, 2); my $distance = $Slic3r::flow_width / $Slic3r::resolution;
my $offsets = offset([ $polygon->{outer}, @{$polygon->{holes}} ], -$distance,
$Slic3r::resolution * 100000, JT_MITER, 2);
# defensive programming # defensive programming
my (@contour_offsets, @hole_offsets) = (); my (@contour_offsets, @hole_offsets) = ();

View File

@ -3,6 +3,8 @@ use Moo;
extends 'Slic3r::Polyline'; extends 'Slic3r::Polyline';
use Math::Clipper qw(JT_MITER);
sub lines { sub lines {
my $self = shift; my $self = shift;
my @lines = $self->SUPER::lines(@_); my @lines = $self->SUPER::lines(@_);
@ -32,4 +34,17 @@ sub encloses_point {
return Slic3r::Geometry::point_in_polygon($point->p, $self->p); return Slic3r::Geometry::point_in_polygon($point->p, $self->p);
} }
# returns false if the polyline is too tight to be printed
sub is_printable {
my $self = shift;
# try to get an inwards offset
# for a distance equal to half of the extrusion width;
# if no offset is possible, then polyline is not printable
my $p = $self->p;
@$p = reverse @$p if !Math::Clipper::is_counter_clockwise($p);
my $offsets = Math::Clipper::offset([$p], -($Slic3r::flow_width / 2 / $Slic3r::resolution), $Slic3r::resolution * 100000, JT_MITER, 2);
return @$offsets ? 1 : 0;
}
1; 1;

View File

@ -147,11 +147,22 @@ sub discover_horizontal_shells {
} }
} }
# remove perimeters and fill surfaces which are too small to be extruded # remove surfaces which are too small to be extruded
sub remove_small_features { sub remove_small_surfaces {
my $self = shift; my $self = shift;
$_->remove_small_surfaces for @{$self->layers};
}
$_->remove_small_features for @{$self->layers}; # remove perimeters which are too small to be extruded
sub remove_small_perimeters {
my $self = shift;
$_->remove_small_perimeters for @{$self->layers};
}
# make bridges printable
sub process_bridges {
my $self = shift;
$_->process_bridges for @{ $self->layers };
} }
sub extrude_perimeters { sub extrude_perimeters {
@ -160,12 +171,19 @@ sub extrude_perimeters {
my $perimeter_extruder = Slic3r::Perimeter->new; my $perimeter_extruder = Slic3r::Perimeter->new;
foreach my $layer (@{ $self->layers }) { foreach my $layer (@{ $self->layers }) {
$layer->detect_perimeter_surfaces;
$perimeter_extruder->make_perimeter($layer); $perimeter_extruder->make_perimeter($layer);
Slic3r::debugf " generated paths: %s\n", Slic3r::debugf " generated paths: %s\n",
join ' ', map $_->id, @{ $layer->perimeters } if $Slic3r::debug; join ' ', map $_->id, @{ $layer->perimeters } if $Slic3r::debug;
} }
} }
# splits fill_surfaces in internal and bridge surfaces
sub split_bridges_fills {
my $self = shift;
$_->split_bridges_fills for @{$self->layers};
}
sub extrude_fills { sub extrude_fills {
my $self = shift; my $self = shift;

View File

@ -80,8 +80,10 @@ sub parse_file {
$vertex->[$_] = ($Slic3r::scale * $vertex->[$_] / $Slic3r::resolution) + $shift[$_] $vertex->[$_] = ($Slic3r::scale * $vertex->[$_] / $Slic3r::resolution) + $shift[$_]
for X,Y,Z; for X,Y,Z;
# round Z coordinates; XY will be rounded automatically with coercion # round Z coordinates to the nearest multiple of layer height
$vertex->[Z] = sprintf('%.0f', $vertex->[Z]); # XY will be rounded automatically to integers with coercion
$vertex->[Z] = sprintf('%.0f', $vertex->[Z] * $Slic3r::resolution / $Slic3r::layer_height)
* $Slic3r::layer_height / $Slic3r::resolution;
} }
foreach my $copy (@copies) { foreach my $copy (@copies) {

View File

@ -22,14 +22,14 @@ sub output {
my $svg = svg($print); my $svg = svg($print);
foreach my $type (qw(polygons polylines white_polygons red_polylines)) { foreach my $type (qw(polygons polylines white_polygons red_polygons red_polylines)) {
if ($things{$type}) { if ($things{$type}) {
my $method = $type =~ /polygons/ ? 'polygon' : 'polyline'; my $method = $type =~ /polygons/ ? 'polygon' : 'polyline';
my $g = $svg->group( my $g = $svg->group(
style => { style => {
'stroke-width' => 2, 'stroke-width' => 2,
'stroke' => $type =~ /red_/ ? 'red' : 'black', 'stroke' => $type =~ /red_/ ? 'red' : 'black',
'fill' => $type eq 'polygons' ? 'grey' : 'none', 'fill' => ($type !~ /polygons/ ? 'none' : ($type =~ /red_/ ? 'red' : 'grey')),
}, },
); );
foreach my $polygon (@{$things{$type}}) { foreach my $polygon (@{$things{$type}}) {
@ -51,7 +51,7 @@ sub output {
my $g = $svg->group( my $g = $svg->group(
style => { style => {
'stroke-width' => 2, 'stroke-width' => 2,
'stroke' => 'black', 'stroke' => $colour,
'fill' => $colour, 'fill' => $colour,
}, },
); );

View File

@ -14,16 +14,40 @@ sub go {
if $self->input_file !~ /\.stl$/i; if $self->input_file !~ /\.stl$/i;
my $t0 = [gettimeofday]; my $t0 = [gettimeofday];
my $print = Slic3r::Print->new_from_stl($self->input_file);
$print->extrude_perimeters;
$print->remove_small_features;
# detect which surfaces are near external layers # skein the STL into layers
# each layer has surfaces with holes; surfaces are distinguished
# in top/bottom/internal
my $print = Slic3r::Print->new_from_stl($self->input_file);
# this will remove unprintable surfaces
# (those that are too tight for extrusion)
$print->remove_small_surfaces;
# make bridges printable
# this will add a set of bridges to each layer
$print->process_bridges;
# make perimeters
# this will add a set of extrusion loops to each layer
# as well as a set of surfaces to be filled
$print->extrude_perimeters;
# this will remove unprintable perimeter loops
# (those that are too tight for extrusion)
$print->remove_small_perimeters;
# split fill_surfaces in internal and bridge surfaces
$print->split_bridges_fills;
# detect which fill surfaces are near external layers
# they will be split in internal and internal-solid surfaces
$print->discover_horizontal_shells; $print->discover_horizontal_shells;
# this will generate extrusion paths for each layer
$print->extrude_fills; $print->extrude_fills;
# output everything to a GCODE file
if (!$self->output_file) { if (!$self->output_file) {
my $output_file = $self->input_file; my $output_file = $self->input_file;
$output_file =~ s/\.stl$/.gcode/i; $output_file =~ s/\.stl$/.gcode/i;
@ -31,9 +55,12 @@ sub go {
} }
$print->export_gcode($self->output_file); $print->export_gcode($self->output_file);
# output some statistics
my $processing_time = tv_interval($t0); my $processing_time = tv_interval($t0);
printf "Done. Process took %d minutes and %.3f seconds\n", printf "Done. Process took %d minutes and %.3f seconds\n",
int($processing_time/60), $processing_time - int($processing_time/60)*60; int($processing_time/60), $processing_time - int($processing_time/60)*60;
# TODO: more statistics!
} }
1; 1;

View File

@ -19,6 +19,29 @@ has 'surface_type' => (
#isa => enum([qw(internal internal-solid bottom top)]), #isa => enum([qw(internal internal-solid bottom top)]),
); );
sub cast_from_polygon {
my $class = shift;
my ($polygon, %args) = @_;
return $class->new(
contour => Slic3r::Polyline::Closed->cast($polygon),
%args,
);
}
sub cast_from_expolygon {
my $class = shift;
my ($expolygon, %args) = @_;
return $class->new(
contour => Slic3r::Polyline::Closed->cast($expolygon->{outer}),
holes => [
map Slic3r::Polyline::Closed->cast($_), @{$expolygon->{holes}}
],
%args,
);
}
sub add_hole { sub add_hole {
my $self = shift; my $self = shift;
my ($hole) = @_; my ($hole) = @_;

View File

@ -0,0 +1,8 @@
package Slic3r::Surface::Bridge;
use Moo;
extends "Slic3r::Surface";
has 'bridge_angle' => (is => 'ro');
1;

View File

@ -2,7 +2,7 @@ use Test::More;
use strict; use strict;
use warnings; use warnings;
plan tests => 5; plan tests => 6;
BEGIN { BEGIN {
use FindBin; use FindBin;
@ -49,6 +49,12 @@ is Slic3r::Geometry::point_in_segment($point, $line), 0, 'point_in_segment';
#========================================================== #==========================================================
$point = [ 736310778.185108, 5017423926.8924 ];
my $line = [ [627484000, 3695776000], [750000000, 3720147000] ];
is Slic3r::Geometry::point_in_segment($point, $line), 0, 'point_in_segment';
#==========================================================
my $polygons = [ my $polygons = [
[ # contour, ccw [ # contour, ccw
[459190000, 5152739000], [147261000, 4612464000], [147261000, 3487535000], [339887000, 3153898000], [459190000, 5152739000], [147261000, 4612464000], [147261000, 3487535000], [339887000, 3153898000],