Refactored Line infill implementation

This commit is contained in:
Alessandro Ranellucci 2014-12-22 17:25:52 +01:00
parent 64061267c8
commit e49cf2c55f
3 changed files with 71 additions and 47 deletions

View File

@ -6,7 +6,6 @@ use Slic3r::Fill::3DHoneycomb;
use Slic3r::Fill::Base; use Slic3r::Fill::Base;
use Slic3r::Fill::Concentric; use Slic3r::Fill::Concentric;
use Slic3r::Fill::Honeycomb; use Slic3r::Fill::Honeycomb;
use Slic3r::Fill::Line;
use Slic3r::Fill::PlanePath; use Slic3r::Fill::PlanePath;
use Slic3r::Fill::Rectilinear; use Slic3r::Fill::Rectilinear;
use Slic3r::Flow ':roles'; use Slic3r::Flow ':roles';

View File

@ -1,8 +0,0 @@
package Slic3r::Fill::Line;
use Moo;
extends 'Slic3r::Fill::Rectilinear';
# Sorry for breaking OOP, but Line is implemented inside Rectilinear.
1;

View File

@ -4,10 +4,13 @@ use Moo;
extends 'Slic3r::Fill::Base'; extends 'Slic3r::Fill::Base';
with qw(Slic3r::Fill::WithDirection); with qw(Slic3r::Fill::WithDirection);
has 'cache' => (is => 'rw', default => sub {{}}); has '_min_spacing' => (is => 'rw');
has '_line_spacing' => (is => 'rw');
has '_diagonal_distance' => (is => 'rw');
has '_line_oscillation' => (is => 'rw');
use Slic3r::Geometry qw(A B X Y MIN scale unscale scaled_epsilon); use Slic3r::Geometry qw(scale unscale scaled_epsilon);
use Slic3r::Geometry::Clipper qw(intersection_pl offset); use Slic3r::Geometry::Clipper qw(intersection_pl);
sub fill_surface { sub fill_surface {
my $self = shift; my $self = shift;
@ -18,41 +21,32 @@ sub fill_surface {
my $rotate_vector = $self->infill_direction($surface); my $rotate_vector = $self->infill_direction($surface);
$self->rotate_points($expolygon, $rotate_vector); $self->rotate_points($expolygon, $rotate_vector);
my $min_spacing = scale($self->spacing); $self->_min_spacing(scale $self->spacing);
my $line_spacing = $min_spacing / $params{density}; $self->_line_spacing($self->_min_spacing / $params{density});
my $line_oscillation = $line_spacing - $min_spacing; $self->_diagonal_distance($self->_line_spacing * 2);
my $is_line_pattern = $self->isa('Slic3r::Fill::Line'); $self->_line_oscillation($self->_line_spacing - $self->_min_spacing); # only for Line infill
my $bounding_box = $expolygon->bounding_box; my $bounding_box = $expolygon->bounding_box;
# define flow spacing according to requested density # define flow spacing according to requested density
if ($params{density} == 1 && !$params{dont_adjust}) { if ($params{density} == 1 && !$params{dont_adjust}) {
$line_spacing = $self->adjust_solid_spacing( $self->_line_spacing($self->adjust_solid_spacing(
width => $bounding_box->size->[X], width => $bounding_box->size->x,
distance => $line_spacing, distance => $self->_line_spacing,
); ));
$self->spacing(unscale $line_spacing); $self->spacing(unscale $self->_line_spacing);
} else { } else {
# extend bounding box so that our pattern will be aligned with other layers # extend bounding box so that our pattern will be aligned with other layers
$bounding_box->merge_point(Slic3r::Point->new( $bounding_box->merge_point(Slic3r::Point->new(
$bounding_box->x_min - ($bounding_box->x_min % $line_spacing), $bounding_box->x_min - ($bounding_box->x_min % $self->_line_spacing),
$bounding_box->y_min - ($bounding_box->y_min % $line_spacing), $bounding_box->y_min - ($bounding_box->y_min % $self->_line_spacing),
)); ));
} }
# generate the basic pattern # generate the basic pattern
my $i = 0;
my $x = $bounding_box->x_min;
my $x_max = $bounding_box->x_max + scaled_epsilon; my $x_max = $bounding_box->x_max + scaled_epsilon;
my @vertical_lines = (); my @vertical_lines = ();
while ($x <= $x_max) { for (my $x = $bounding_box->x_min; $x <= $x_max; $x += $self->_line_spacing) {
my $vertical_line = [ [$x, $bounding_box->y_max], [$x, $bounding_box->y_min] ]; push @vertical_lines, $self->_line($#vertical_lines, $x, $bounding_box->y_min, $bounding_box->y_max);
if ($is_line_pattern && $i % 2) {
$vertical_line->[A][X] += $line_oscillation;
$vertical_line->[B][X] -= $line_oscillation;
}
push @vertical_lines, Slic3r::Polyline->new(@$vertical_line);
$i++;
$x += $line_spacing;
} }
# clip paths against a slightly larger expolygon, so that the first and last paths # clip paths against a slightly larger expolygon, so that the first and last paths
@ -64,19 +58,10 @@ sub fill_surface {
# connect lines # connect lines
unless ($params{dont_connect} || !@polylines) { # prevent calling leftmost_point() on empty collections unless ($params{dont_connect} || !@polylines) { # prevent calling leftmost_point() on empty collections
my ($expolygon_off) = @{$expolygon->offset_ex($min_spacing/2)}; my ($expolygon_off) = @{$expolygon->offset_ex($self->_min_spacing/2)};
my $collection = Slic3r::Polyline::Collection->new(@polylines); my $collection = Slic3r::Polyline::Collection->new(@polylines);
@polylines = (); @polylines = ();
my $tolerance = 10 * scaled_epsilon;
my $diagonal_distance = $line_spacing * 2;
my $can_connect = $is_line_pattern
? sub {
($_[X] >= ($line_spacing - $line_oscillation) - $tolerance) && ($_[X] <= ($line_spacing + $line_oscillation) + $tolerance)
&& $_[Y] <= $diagonal_distance
}
: sub { $_[X] <= $diagonal_distance && $_[Y] <= $diagonal_distance };
foreach my $polyline (@{$collection->chained_path_from($collection->leftmost_point, 0)}) { foreach my $polyline (@{$collection->chained_path_from($collection->leftmost_point, 0)}) {
if (@polylines) { if (@polylines) {
my $first_point = $polyline->first_point; my $first_point = $polyline->first_point;
@ -85,7 +70,7 @@ sub fill_surface {
# TODO: we should also check that both points are on a fill_boundary to avoid # TODO: we should also check that both points are on a fill_boundary to avoid
# connecting paths on the boundaries of internal regions # connecting paths on the boundaries of internal regions
if ($can_connect->(@distance) && $expolygon_off->contains_line(Slic3r::Line->new($last_point, $first_point))) { if ($self->_can_connect(@distance) && $expolygon_off->contains_line(Slic3r::Line->new($last_point, $first_point))) {
$polylines[-1]->append_polyline($polyline); $polylines[-1]->append_polyline($polyline);
next; next;
} }
@ -102,4 +87,52 @@ sub fill_surface {
return @polylines; return @polylines;
} }
sub _line {
my ($self, $i, $x, $y_min, $y_max) = @_;
return Slic3r::Polyline->new(
[$x, $y_min],
[$x, $y_max],
);
}
sub _can_connect {
my ($self, $dist_X, $dist_Y) = @_;
return $dist_X <= $self->_diagonal_distance
&& $dist_Y <= $self->_diagonal_distance;
}
package Slic3r::Fill::Line;
use Moo;
extends 'Slic3r::Fill::Rectilinear';
use Slic3r::Geometry qw(scaled_epsilon);
sub _line {
my ($self, $i, $x, $y_min, $y_max) = @_;
if ($i % 2) {
return Slic3r::Polyline->new(
[$x - $self->_line_oscillation, $y_min],
[$x + $self->_line_oscillation, $y_max],
);
} else {
return Slic3r::Polyline->new(
[$x, $y_min],
[$x, $y_max],
);
}
}
sub _can_connect {
my ($self, $dist_X, $dist_Y) = @_;
my $TOLERANCE = 10 * scaled_epsilon;
return ($dist_X >= ($self->_line_spacing - $self->_line_oscillation) - $TOLERANCE)
&& ($dist_X <= ($self->_line_spacing + $self->_line_oscillation) + $TOLERANCE)
&& $dist_Y <= $self->_diagonal_distance;
}
1; 1;