mirror of
https://github.com/SoftFever/OrcaSlicer.git
synced 2025-08-09 06:45:25 -06:00
Faster algorithm for rectilinear fill
This commit is contained in:
parent
33d7b8c7cf
commit
1978a99416
10 changed files with 242 additions and 159 deletions
|
@ -62,7 +62,7 @@ our $temperature = 200;
|
|||
our $retract_length = 1; # mm
|
||||
our $retract_restart_extra = 0; # mm
|
||||
our $retract_speed = 40; # mm/sec
|
||||
our $retract_before_travel = 1; # mm
|
||||
our $retract_before_travel = 2; # mm
|
||||
|
||||
# skirt options
|
||||
our $skirts = 1;
|
||||
|
|
|
@ -218,6 +218,10 @@ sub validate {
|
|||
$Slic3r::print_center = [ split /,/, $Slic3r::print_center ]
|
||||
if !ref $Slic3r::print_center;
|
||||
|
||||
# --fill-type
|
||||
die "Invalid value for --fill-type\n"
|
||||
if !exists $Slic3r::Fill::FillTypes{$Slic3r::fill_type};
|
||||
|
||||
# --fill-density
|
||||
die "Invalid value for --fill-density\n"
|
||||
if $Slic3r::fill_density < 0 || $Slic3r::fill_density > 1;
|
||||
|
|
|
@ -69,15 +69,11 @@ sub extrude {
|
|||
|
||||
my $gcode = "";
|
||||
|
||||
# reset extrusion distance counter
|
||||
if (!$Slic3r::use_relative_e_distances) {
|
||||
$self->extrusion_distance(0);
|
||||
$gcode .= "G92 E0 ; reset extrusion distance\n";
|
||||
}
|
||||
|
||||
# retract
|
||||
if (Slic3r::Geometry::distance_between_points($self->last_pos, $path->points->[0]->p) * $Slic3r::resolution
|
||||
>= $Slic3r::retract_before_travel) {
|
||||
# retract if distance from previous position is greater or equal to the one
|
||||
# specified by the user *and* to the maximum distance between infill lines
|
||||
my $distance_from_last_pos = Slic3r::Geometry::distance_between_points($self->last_pos, $path->points->[0]->p) * $Slic3r::resolution;
|
||||
if ($distance_from_last_pos >= $Slic3r::retract_before_travel
|
||||
&& $distance_from_last_pos >= $Slic3r::flow_width / $Slic3r::fill_density) {
|
||||
$gcode .= $self->retract;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,12 +3,14 @@ use Moo;
|
|||
|
||||
use Slic3r::Fill::Base;
|
||||
use Slic3r::Fill::Rectilinear;
|
||||
use Slic3r::Fill::Rectilinear2;
|
||||
|
||||
has 'print' => (is => 'ro', required => 1);
|
||||
has 'fillers' => (is => 'rw', default => sub { {} });
|
||||
|
||||
our %FillTypes = (
|
||||
rectilinear => 'Slic3r::Fill::Rectilinear',
|
||||
rectilinear => 'Slic3r::Fill::Rectilinear',
|
||||
rectilinear2 => 'Slic3r::Fill::Rectilinear2',
|
||||
);
|
||||
|
||||
sub BUILD {
|
||||
|
|
|
@ -7,10 +7,6 @@ use constant X1 => 0;
|
|||
use constant Y1 => 1;
|
||||
use constant X2 => 2;
|
||||
use constant Y2 => 3;
|
||||
use constant A => 0;
|
||||
use constant B => 1;
|
||||
use constant X => 0;
|
||||
use constant Y => 1;
|
||||
|
||||
use XXX;
|
||||
|
||||
|
@ -18,119 +14,23 @@ sub fill_surface {
|
|||
my $self = shift;
|
||||
my ($surface, %params) = @_;
|
||||
|
||||
my $polygons = [ $surface->p ];
|
||||
|
||||
# rotate polygons so that we can work with vertical lines here
|
||||
my $polygons = [ $surface->p ];
|
||||
my $rotate_vector = $self->infill_direction($polygons);
|
||||
$self->rotate_points($polygons, $rotate_vector);
|
||||
|
||||
my $bounding_box = [ Slic3r::Geometry::bounding_box(map @$_, $polygons) ];
|
||||
my $surface_width = $bounding_box->[X2] - $bounding_box->[X1];
|
||||
my $surface_height = $bounding_box->[Y2] - $bounding_box->[Y1];
|
||||
|
||||
my $distance_between_lines = $Slic3r::flow_width / $Slic3r::resolution / $params{density};
|
||||
my $number_of_lines = int(0.99999999 + $self->max_print_dimension / $distance_between_lines); # ceil
|
||||
|
||||
#printf "distance = %f\n", $distance_between_lines;
|
||||
#printf "number_of_lines = %d\n", $number_of_lines;
|
||||
|
||||
# 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 Slic3r::Geometry::polygon_lines($_), @$polygons) {
|
||||
|
||||
# find out the coordinates
|
||||
my @coordinates = map @$_, @$line;
|
||||
|
||||
# get the extents of the segment along the primary axis
|
||||
my @line_c = sort { $a <=> $b } @coordinates[X1, X2];
|
||||
Slic3r::debugf "Segment %d,%d - %d,%d (extents: %f, %f)\n", @coordinates, @line_c;
|
||||
|
||||
for (my $c = int($line_c[0] / $distance_between_lines) * $distance_between_lines;
|
||||
$c <= $line_c[1]; $c += $distance_between_lines) {
|
||||
next if $c < $line_c[0] || $c > $line_c[1];
|
||||
my $i = sprintf('%.0f', $c / $distance_between_lines) - 1;
|
||||
#printf "CURRENT \$i = %d, \$c = %f\n", $i, $c;
|
||||
|
||||
# if the segment is parallel to our ray, there will be two intersection points
|
||||
if ($line_c[0] == $line_c[1]) {
|
||||
Slic3r::debugf " Segment is parallel!\n";
|
||||
push @{ $points->[$i] }, $coordinates[Y1], $coordinates[Y2];
|
||||
Slic3r::debugf " intersections at %f (%d) = %f, %f\n", $c, $i, $points->[$i][-2], $points->[$i][-1];
|
||||
} else {
|
||||
Slic3r::debugf " Segment NOT parallel!\n";
|
||||
# one point of intersection
|
||||
push @{ $points->[$i] }, $coordinates[Y1] + ($coordinates[Y2] - $coordinates[Y1])
|
||||
* ($c - $coordinates[X1]) / ($coordinates[X2] - $coordinates[X1]);
|
||||
Slic3r::debugf " intersection at %f (%d) = %f\n", $c, $i, $points->[$i][-1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# sort and remove duplicates
|
||||
for (my $i = 0; $i <= $#$points; $i++) {
|
||||
my %h = map { sprintf("%.9f", $_) => 1 } @{ $points->[$i] };
|
||||
$points->[$i] = [ sort { $a <=> $b } keys %h ];
|
||||
}
|
||||
|
||||
# generate extrusion paths
|
||||
my (@paths, @path_points) = ();
|
||||
my $direction = 0;
|
||||
|
||||
my $stop_path = sub {
|
||||
# defensive programming
|
||||
if (@path_points == 1) {
|
||||
#warn "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 > 1;
|
||||
@path_points = ();
|
||||
};
|
||||
|
||||
# loop until we have spare points
|
||||
CYCLE: while (scalar map(@$_, @$points) > 1) {
|
||||
# loop through rows
|
||||
ROW: for (my $i = 0; $i <= $#$points; $i++) {
|
||||
my $row = $points->[$i] or next ROW;
|
||||
Slic3r::debugf "\nProcessing row %d (direction: %d)...\n", $i, $direction;
|
||||
if (!@$row) {
|
||||
Slic3r::debugf " no points\n";
|
||||
$stop_path->();
|
||||
next ROW;
|
||||
}
|
||||
Slic3r::debugf " points = %s\n", join ', ', @$row if $Slic3r::debug;
|
||||
|
||||
# coordinate of current row
|
||||
my $c = ($i + 1) * $distance_between_lines;
|
||||
|
||||
# need to start a path?
|
||||
if (!@path_points) {
|
||||
Slic3r::debugf " path starts at %d\n", $row->[0];
|
||||
push @path_points, [ $c, shift @$row ];
|
||||
}
|
||||
|
||||
my @search_points = @$row;
|
||||
@search_points = reverse @search_points if $direction == 1;
|
||||
my @connectable_points = $self->find_connectable_points($polygons, $path_points[-1], $c, [@search_points]);
|
||||
Slic3r::debugf " ==> found %d connectable points = %s\n", scalar(@connectable_points),
|
||||
join ', ', @connectable_points if $Slic3r::debug;
|
||||
|
||||
if (!@connectable_points && @path_points && $path_points[-1][0] != $c) {
|
||||
# no connectable in this row
|
||||
$stop_path->();
|
||||
}
|
||||
|
||||
if (@connectable_points == 1 && $path_points[0][0] != $c
|
||||
&& (($connectable_points[0] == $row->[-1] && $direction == 0)
|
||||
|| ($connectable_points[0] == $row->[0] && $direction == 1))) {
|
||||
$i--; # keep searching on current row in the opposite direction
|
||||
}
|
||||
|
||||
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;
|
||||
my @paths = ();
|
||||
my $x = $bounding_box->[X1];
|
||||
while ($x < $bounding_box->[X2]) {
|
||||
my $vertical_line = [ [$x, $bounding_box->[Y2]], [$x, $bounding_box->[Y1]] ];
|
||||
push @paths, @{ Slic3r::Geometry::clip_segment_complex_polygon($vertical_line, $polygons) };
|
||||
$x += int($distance_between_lines);
|
||||
}
|
||||
|
||||
# paths must be rotated back
|
||||
|
@ -139,21 +39,4 @@ sub fill_surface {
|
|||
return @paths;
|
||||
}
|
||||
|
||||
# this function will select the first contiguous block of
|
||||
# points connectable to a given one
|
||||
sub find_connectable_points {
|
||||
my $self = shift;
|
||||
my ($polygons, $point, $c, $points) = @_;
|
||||
|
||||
my @connectable_points = ();
|
||||
foreach my $p (@$points) {
|
||||
if (!Slic3r::Geometry::can_connect_points($point, [ $c, $p ], $polygons)) {
|
||||
@connectable_points ? last : next;
|
||||
}
|
||||
push @connectable_points, $p;
|
||||
$point = [ $c, $p ] if $point->[0] != $c;
|
||||
}
|
||||
return @connectable_points;
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
159
lib/Slic3r/Fill/Rectilinear2.pm
Normal file
159
lib/Slic3r/Fill/Rectilinear2.pm
Normal file
|
@ -0,0 +1,159 @@
|
|||
package Slic3r::Fill::Rectilinear2;
|
||||
use Moo;
|
||||
|
||||
extends 'Slic3r::Fill::Base';
|
||||
|
||||
use constant X1 => 0;
|
||||
use constant Y1 => 1;
|
||||
use constant X2 => 2;
|
||||
use constant Y2 => 3;
|
||||
use constant A => 0;
|
||||
use constant B => 1;
|
||||
use constant X => 0;
|
||||
use constant Y => 1;
|
||||
|
||||
use XXX;
|
||||
|
||||
sub fill_surface {
|
||||
my $self = shift;
|
||||
my ($surface, %params) = @_;
|
||||
|
||||
my $polygons = [ $surface->p ];
|
||||
|
||||
# rotate polygons so that we can work with vertical lines here
|
||||
my $rotate_vector = $self->infill_direction($polygons);
|
||||
$self->rotate_points($polygons, $rotate_vector);
|
||||
|
||||
my $distance_between_lines = $Slic3r::flow_width / $Slic3r::resolution / $params{density};
|
||||
my $number_of_lines = int(0.99999999 + $self->max_print_dimension / $distance_between_lines); # ceil
|
||||
|
||||
#printf "distance = %f\n", $distance_between_lines;
|
||||
#printf "number_of_lines = %d\n", $number_of_lines;
|
||||
|
||||
# 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 Slic3r::Geometry::polygon_lines($_), @$polygons) {
|
||||
|
||||
# find out the coordinates
|
||||
my @coordinates = map @$_, @$line;
|
||||
|
||||
# get the extents of the segment along the primary axis
|
||||
my @line_c = sort { $a <=> $b } @coordinates[X1, X2];
|
||||
Slic3r::debugf "Segment %d,%d - %d,%d (extents: %f, %f)\n", @coordinates, @line_c;
|
||||
|
||||
for (my $c = int($line_c[0] / $distance_between_lines) * $distance_between_lines;
|
||||
$c <= $line_c[1]; $c += $distance_between_lines) {
|
||||
next if $c < $line_c[0] || $c > $line_c[1];
|
||||
my $i = sprintf('%.0f', $c / $distance_between_lines) - 1;
|
||||
#printf "CURRENT \$i = %d, \$c = %f\n", $i, $c;
|
||||
|
||||
# if the segment is parallel to our ray, there will be two intersection points
|
||||
if ($line_c[0] == $line_c[1]) {
|
||||
Slic3r::debugf " Segment is parallel!\n";
|
||||
push @{ $points->[$i] }, $coordinates[Y1], $coordinates[Y2];
|
||||
Slic3r::debugf " intersections at %f (%d) = %f, %f\n", $c, $i, $points->[$i][-2], $points->[$i][-1];
|
||||
} else {
|
||||
Slic3r::debugf " Segment NOT parallel!\n";
|
||||
# one point of intersection
|
||||
push @{ $points->[$i] }, $coordinates[Y1] + ($coordinates[Y2] - $coordinates[Y1])
|
||||
* ($c - $coordinates[X1]) / ($coordinates[X2] - $coordinates[X1]);
|
||||
Slic3r::debugf " intersection at %f (%d) = %f\n", $c, $i, $points->[$i][-1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# sort and remove duplicates
|
||||
for (my $i = 0; $i <= $#$points; $i++) {
|
||||
my %h = map { sprintf("%.9f", $_) => 1 } @{ $points->[$i] };
|
||||
$points->[$i] = [ sort { $a <=> $b } keys %h ];
|
||||
}
|
||||
|
||||
# generate extrusion paths
|
||||
my (@paths, @path_points) = ();
|
||||
my $direction = 0;
|
||||
|
||||
my $stop_path = sub {
|
||||
# defensive programming
|
||||
if (@path_points == 1) {
|
||||
#warn "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 > 1;
|
||||
@path_points = ();
|
||||
};
|
||||
|
||||
# loop until we have spare points
|
||||
CYCLE: while (scalar map(@$_, @$points) > 1) {
|
||||
# loop through rows
|
||||
ROW: for (my $i = 0; $i <= $#$points; $i++) {
|
||||
my $row = $points->[$i] or next ROW;
|
||||
Slic3r::debugf "\nProcessing row %d (direction: %d)...\n", $i, $direction;
|
||||
if (!@$row) {
|
||||
Slic3r::debugf " no points\n";
|
||||
$stop_path->();
|
||||
next ROW;
|
||||
}
|
||||
Slic3r::debugf " points = %s\n", join ', ', @$row if $Slic3r::debug;
|
||||
|
||||
# coordinate of current row
|
||||
my $c = ($i + 1) * $distance_between_lines;
|
||||
|
||||
# need to start a path?
|
||||
if (!@path_points) {
|
||||
Slic3r::debugf " path starts at %d\n", $row->[0];
|
||||
push @path_points, [ $c, shift @$row ];
|
||||
}
|
||||
|
||||
my @search_points = @$row;
|
||||
@search_points = reverse @search_points if $direction == 1;
|
||||
my @connectable_points = $self->find_connectable_points($polygons, $path_points[-1], $c, [@search_points]);
|
||||
Slic3r::debugf " ==> found %d connectable points = %s\n", scalar(@connectable_points),
|
||||
join ', ', @connectable_points if $Slic3r::debug;
|
||||
|
||||
if (!@connectable_points && @path_points && $path_points[-1][0] != $c) {
|
||||
# no connectable in this row
|
||||
$stop_path->();
|
||||
}
|
||||
|
||||
if (@connectable_points == 1 && $path_points[0][0] != $c
|
||||
&& (($connectable_points[0] == $row->[-1] && $direction == 0)
|
||||
|| ($connectable_points[0] == $row->[0] && $direction == 1))) {
|
||||
$i--; # keep searching on current row in the opposite direction
|
||||
}
|
||||
|
||||
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
|
||||
$self->rotate_points_back(\@paths, $rotate_vector);
|
||||
|
||||
return @paths;
|
||||
}
|
||||
|
||||
# this function will select the first contiguous block of
|
||||
# points connectable to a given one
|
||||
sub find_connectable_points {
|
||||
my $self = shift;
|
||||
my ($polygons, $point, $c, $points) = @_;
|
||||
|
||||
my @connectable_points = ();
|
||||
foreach my $p (@$points) {
|
||||
if (!Slic3r::Geometry::can_connect_points($point, [ $c, $p ], $polygons)) {
|
||||
@connectable_points ? last : next;
|
||||
}
|
||||
push @connectable_points, $p;
|
||||
$point = [ $c, $p ] if $point->[0] != $c;
|
||||
}
|
||||
return @connectable_points;
|
||||
}
|
||||
|
||||
1;
|
|
@ -316,15 +316,10 @@ sub polygon_points_visibility {
|
|||
return 1;
|
||||
}
|
||||
|
||||
my $i = 0;
|
||||
sub line_intersection {
|
||||
my ($line1, $line2, $require_crossing) = @_;
|
||||
$require_crossing ||= 0;
|
||||
|
||||
Slic3r::SVG::output(undef, "line_intersection_" . $i++ . ".svg",
|
||||
lines => [ $line1, $line2 ],
|
||||
) if 0;
|
||||
|
||||
my $intersection = _line_intersection(map @$_, @$line1, @$line2);
|
||||
return (ref $intersection && $intersection->[1] == $require_crossing)
|
||||
? $intersection->[0]
|
||||
|
@ -460,16 +455,23 @@ sub clip_segment_complex_polygon {
|
|||
my ($line, $polygons) = @_;
|
||||
|
||||
my @intersections = grep $_, map line_intersection($line, $_, 1),
|
||||
map polygon_lines($_), @$polygons;
|
||||
map polygon_lines($_), @$polygons or return ();
|
||||
|
||||
@intersections = sort { "$a->[X],$a->[Y]" cmp "$b->[X],$b->[Y]" } @intersections;
|
||||
# this is not very elegant, however it works
|
||||
@intersections = sort { sprintf("%020f,%020f", @$a) cmp sprintf("%020f,%020f", @$b) } @intersections;
|
||||
|
||||
shift(@intersections) if !grep(point_in_polygon($intersections[0], $_), @$polygons)
|
||||
&& !grep(polygon_segment_having_point($_, $intersections[0]), @$polygons);
|
||||
|
||||
# defensive programming
|
||||
die "Invalid intersections" if @intersections % 2 != 0;
|
||||
|
||||
my @lines = ();
|
||||
while (@intersections) {
|
||||
push @lines, [ shift(@intersections), shift(@intersections) ];
|
||||
# skip tangent points
|
||||
my @points = map shift @intersections, 1..2;
|
||||
next if points_coincide(@points);
|
||||
push @lines, [ @points ];
|
||||
}
|
||||
return [@lines];
|
||||
}
|
||||
|
|
|
@ -22,14 +22,14 @@ sub output {
|
|||
|
||||
my $svg = svg($print);
|
||||
|
||||
foreach my $type (qw(polygons polylines)) {
|
||||
foreach my $type (qw(polygons polylines white_polygons red_polylines)) {
|
||||
if ($things{$type}) {
|
||||
my $method = $type eq 'polygons' ? 'polygon' : 'polyline';
|
||||
my $method = $type =~ /polygons/ ? 'polygon' : 'polyline';
|
||||
my $g = $svg->group(
|
||||
style => {
|
||||
'stroke-width' => 2,
|
||||
'stroke' => 'black',
|
||||
'fill' => 'none',
|
||||
'stroke' => $type =~ /red_/ ? 'red' : 'black',
|
||||
'fill' => $type eq 'polygons' ? 'grey' : 'none',
|
||||
},
|
||||
);
|
||||
foreach my $polygon (@{$things{$type}}) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue