Cleanup lines resulting from plane intersection before detecting polygons. This allows for more tolerance with dirty models. Performance impact depends on how many layers are detected as dirty. #16 #28
This commit is contained in:
parent
fec816b065
commit
c5d5e4d244
1
MANIFEST
1
MANIFEST
@ -39,6 +39,7 @@ slic3r.pl
|
|||||||
t/arcs.t
|
t/arcs.t
|
||||||
t/clean_polylines.t
|
t/clean_polylines.t
|
||||||
t/clipper.t
|
t/clipper.t
|
||||||
|
t/collinear.t
|
||||||
t/geometry.t
|
t/geometry.t
|
||||||
t/polyclip.t
|
t/polyclip.t
|
||||||
t/stl.t
|
t/stl.t
|
||||||
|
@ -31,13 +31,9 @@ has 'retract_speed' => (
|
|||||||
default => sub { $Slic3r::retract_speed * 60 }, # mm/min
|
default => sub { $Slic3r::retract_speed * 60 }, # mm/min
|
||||||
);
|
);
|
||||||
|
|
||||||
use Slic3r::Geometry qw(points_coincide);
|
use Slic3r::Geometry qw(points_coincide PI X Y);
|
||||||
use XXX;
|
use XXX;
|
||||||
|
|
||||||
use constant PI => 4 * atan2(1, 1);
|
|
||||||
use constant X => 0;
|
|
||||||
use constant Y => 1;
|
|
||||||
|
|
||||||
sub move_z {
|
sub move_z {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my ($z) = @_;
|
my ($z) = @_;
|
||||||
|
@ -7,10 +7,7 @@ extends 'Slic3r::Polyline';
|
|||||||
# expressed in layers
|
# expressed in layers
|
||||||
has 'depth_layers' => (is => 'ro', default => sub {1});
|
has 'depth_layers' => (is => 'ro', default => sub {1});
|
||||||
|
|
||||||
use constant X => 0;
|
use Slic3r::Geometry qw(PI X Y epsilon deg2rad rotate_points);
|
||||||
use constant Y => 1;
|
|
||||||
|
|
||||||
use Slic3r::Geometry qw(PI epsilon deg2rad rotate_points);
|
|
||||||
use XXX;
|
use XXX;
|
||||||
|
|
||||||
sub clip_end {
|
sub clip_end {
|
||||||
|
@ -5,7 +5,7 @@ use warnings;
|
|||||||
require Exporter;
|
require Exporter;
|
||||||
our @ISA = qw(Exporter);
|
our @ISA = qw(Exporter);
|
||||||
our @EXPORT_OK = qw(
|
our @EXPORT_OK = qw(
|
||||||
PI epsilon slope line_atan lines_parallel three_points_aligned
|
PI X Y Z A B epsilon slope line_atan lines_parallel three_points_aligned
|
||||||
line_point_belongs_to_segment points_coincide distance_between_points
|
line_point_belongs_to_segment points_coincide distance_between_points
|
||||||
line_length midpoint point_in_polygon point_in_segment segment_in_segment
|
line_length midpoint point_in_polygon point_in_segment segment_in_segment
|
||||||
point_is_on_left_of_segment polyline_lines polygon_lines nearest_point
|
point_is_on_left_of_segment polyline_lines polygon_lines nearest_point
|
||||||
@ -17,7 +17,7 @@ our @EXPORT_OK = qw(
|
|||||||
clip_segment_complex_polygon longest_segment angle3points
|
clip_segment_complex_polygon longest_segment angle3points
|
||||||
polyline_remove_parallel_continuous_edges polyline_remove_acute_vertices
|
polyline_remove_parallel_continuous_edges polyline_remove_acute_vertices
|
||||||
polygon_remove_acute_vertices polygon_remove_parallel_continuous_edges
|
polygon_remove_acute_vertices polygon_remove_parallel_continuous_edges
|
||||||
shortest_path
|
shortest_path collinear
|
||||||
);
|
);
|
||||||
|
|
||||||
use Slic3r::Geometry::DouglasPeucker qw(Douglas_Peucker);
|
use Slic3r::Geometry::DouglasPeucker qw(Douglas_Peucker);
|
||||||
@ -28,6 +28,7 @@ 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 Z => 2;
|
||||||
our $parallel_degrees_limit = abs(deg2rad(3));
|
our $parallel_degrees_limit = abs(deg2rad(3));
|
||||||
|
|
||||||
our $epsilon = 1E-4;
|
our $epsilon = 1E-4;
|
||||||
@ -416,6 +417,22 @@ sub line_intersection {
|
|||||||
: undef;
|
: undef;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub collinear {
|
||||||
|
my ($line1, $line2, $require_overlapping) = @_;
|
||||||
|
my $intersection = _line_intersection(map @$_, @$line1, @$line2);
|
||||||
|
return 0 unless !ref($intersection)
|
||||||
|
&& ($intersection eq 'parallel collinear'
|
||||||
|
|| ($intersection eq 'parallel vertical' && abs($line1->[A][X] - $line2->[A][X]) < epsilon));
|
||||||
|
|
||||||
|
if ($require_overlapping) {
|
||||||
|
my @box_a = bounding_box([ $line1->[0], $line1->[1] ]);
|
||||||
|
my @box_b = bounding_box([ $line2->[0], $line2->[1] ]);
|
||||||
|
return 0 unless bounding_box_intersect( 2, @box_a, @box_b );
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
sub _line_intersection {
|
sub _line_intersection {
|
||||||
my ( $x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3 );
|
my ( $x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3 );
|
||||||
|
|
||||||
|
@ -3,14 +3,10 @@ use Moo;
|
|||||||
|
|
||||||
use Math::Clipper ':all';
|
use Math::Clipper ':all';
|
||||||
use Slic3r::Geometry qw(polygon_lines points_coincide angle3points polyline_lines nearest_point
|
use Slic3r::Geometry qw(polygon_lines points_coincide angle3points polyline_lines nearest_point
|
||||||
line_length);
|
line_length collinear X Y A B PI);
|
||||||
use Slic3r::Geometry::Clipper qw(safety_offset union_ex PFT_EVENODD);
|
use Slic3r::Geometry::Clipper qw(safety_offset union_ex);
|
||||||
use XXX;
|
use XXX;
|
||||||
|
|
||||||
use constant PI => 4 * atan2(1, 1);
|
|
||||||
use constant A => 0;
|
|
||||||
use constant B => 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',
|
||||||
@ -116,32 +112,58 @@ sub add_line {
|
|||||||
return $line;
|
return $line;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub remove_line {
|
# merge overlapping lines
|
||||||
|
sub cleanup_lines {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my ($line) = @_;
|
|
||||||
@{ $self->lines } = grep $_ ne $line, @{ $self->lines };
|
my $lines = $self->lines;
|
||||||
|
my $line_count = @$lines;
|
||||||
|
|
||||||
|
for (my $i = 0; $i <= $#$lines-1; $i++) {
|
||||||
|
for (my $j = $i+1; $j <= $#$lines; $j++) {
|
||||||
|
# lines are collinear and overlapping?
|
||||||
|
next unless collinear($lines->[$i], $lines->[$j], 1);
|
||||||
|
|
||||||
|
# lines have same orientation?
|
||||||
|
next unless ($lines->[$i][A][X] <=> $lines->[$i][B][X]) == ($lines->[$j][A][X] <=> $lines->[$j][B][X])
|
||||||
|
&& ($lines->[$i][A][Y] <=> $lines->[$i][B][Y]) == ($lines->[$j][A][Y] <=> $lines->[$j][B][Y]);
|
||||||
|
|
||||||
|
# resulting line
|
||||||
|
my @x = sort { $a <=> $b } ($lines->[$i][A][X], $lines->[$i][B][X], $lines->[$j][A][X], $lines->[$j][B][X]);
|
||||||
|
my @y = sort { $a <=> $b } ($lines->[$i][A][Y], $lines->[$i][B][Y], $lines->[$j][A][Y], $lines->[$j][B][Y]);
|
||||||
|
my $new_line = Slic3r::Line->new([$x[0], $y[0]], [$x[-1], $y[-1]]);
|
||||||
|
for (X, Y) {
|
||||||
|
($new_line->[A][$_], $new_line->[B][$_]) = ($new_line->[B][$_], $new_line->[A][$_])
|
||||||
|
if $lines->[$i][A][$_] > $lines->[$i][B][$_];
|
||||||
}
|
}
|
||||||
|
|
||||||
sub remove_surface {
|
# save new line and remove found one
|
||||||
my $self = shift;
|
$lines->[$i] = $new_line;
|
||||||
my ($surface) = @_;
|
splice @$lines, $j, 1;
|
||||||
@{ $self->surfaces } = grep $_ ne $surface, @{ $self->surfaces };
|
$j--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Slic3r::debugf " merging %d lines resulted in %d lines\n", $line_count, scalar(@$lines);
|
||||||
}
|
}
|
||||||
|
|
||||||
# build polylines from lines
|
# build polylines from lines
|
||||||
sub make_surfaces {
|
sub make_surfaces {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
|
|
||||||
my @lines = ();
|
if (0) {
|
||||||
push @lines, @{$self->lines};
|
require "Slic3r/SVG.pm";
|
||||||
#@lines = grep line_length($_) > xx, @lines;
|
Slic3r::SVG::output(undef, "lines.svg",
|
||||||
|
lines => [ grep !$_->isa('Slic3r::Line::FacetEdge'), @{$self->lines} ],
|
||||||
|
red_lines => [ grep $_->isa('Slic3r::Line::FacetEdge'), @{$self->lines} ],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#use Slic3r::SVG;
|
my (@polygons, %visited_lines, @discarded_lines, @discarded_polylines) = ();
|
||||||
#Slic3r::SVG::output(undef, "lines.svg",
|
|
||||||
# lines => [ map $_->p, grep !$_->isa('Slic3r::Line::FacetEdge'), @{$self->lines} ],
|
|
||||||
# red_lines => [ map $_->p, grep $_->isa('Slic3r::Line::FacetEdge'), @{$self->lines} ],
|
|
||||||
#);
|
|
||||||
|
|
||||||
|
my $detect = sub {
|
||||||
|
my @lines = @{$self->lines};
|
||||||
|
(@polygons, %visited_lines, @discarded_lines, @discarded_polylines) = ();
|
||||||
my $get_point_id = sub { sprintf "%.0f,%.0f", @{$_[0]} };
|
my $get_point_id = sub { sprintf "%.0f,%.0f", @{$_[0]} };
|
||||||
|
|
||||||
my (%pointmap, @pointmap_keys) = ();
|
my (%pointmap, @pointmap_keys) = ();
|
||||||
@ -155,7 +177,6 @@ sub make_surfaces {
|
|||||||
}
|
}
|
||||||
|
|
||||||
my $n = 0;
|
my $n = 0;
|
||||||
my (@polygons, %visited_lines, @discarded_lines, @discarded_polylines) = ();
|
|
||||||
while (my $first_line = shift @lines) {
|
while (my $first_line = shift @lines) {
|
||||||
next if $visited_lines{ $first_line->id };
|
next if $visited_lines{ $first_line->id };
|
||||||
my @points = @$first_line;
|
my @points = @$first_line;
|
||||||
@ -218,9 +239,8 @@ sub make_surfaces {
|
|||||||
|
|
||||||
if (@points < 4 || !points_coincide($points[0], $points[-1])) {
|
if (@points < 4 || !points_coincide($points[0], $points[-1])) {
|
||||||
# discarding polyline
|
# discarding polyline
|
||||||
if (@points == 2) {
|
push @discarded_lines, @seen_lines;
|
||||||
push @discarded_lines, [@points];
|
if (@points > 2) {
|
||||||
} else {
|
|
||||||
push @discarded_polylines, [@points];
|
push @discarded_polylines, [@points];
|
||||||
}
|
}
|
||||||
next;
|
next;
|
||||||
@ -232,6 +252,9 @@ sub make_surfaces {
|
|||||||
push @polygons, Slic3r::Polygon->new(@points);
|
push @polygons, Slic3r::Polygon->new(@points);
|
||||||
$polygons[-1]->cleanup;
|
$polygons[-1]->cleanup;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
$detect->();
|
||||||
|
|
||||||
# Now, if we got a clean and manifold model then @polygons would contain everything
|
# Now, if we got a clean and manifold model then @polygons would contain everything
|
||||||
# we need to draw our layer. In real life, sadly, things are different and it is likely
|
# we need to draw our layer. In real life, sadly, things are different and it is likely
|
||||||
@ -243,10 +266,11 @@ sub make_surfaces {
|
|||||||
# other line.
|
# other line.
|
||||||
|
|
||||||
# So, let's first check what lines were not detected as part of polygons.
|
# So, let's first check what lines were not detected as part of polygons.
|
||||||
if (@discarded_lines || @discarded_polylines) {
|
if (@discarded_lines) {
|
||||||
print " Warning: errors while parsing this layer (dirty or non-manifold model)\n";
|
|
||||||
Slic3r::debugf " %d lines out of %d were discarded and %d polylines were not closed\n",
|
Slic3r::debugf " %d lines out of %d were discarded and %d polylines were not closed\n",
|
||||||
scalar(@discarded_lines), scalar(@{$self->lines}), scalar(@discarded_polylines);
|
scalar(@discarded_lines), scalar(@{$self->lines}), scalar(@discarded_polylines);
|
||||||
|
print " Warning: errors while parsing this layer (dirty or non-manifold model).\n";
|
||||||
|
print " Retrying with slower algorithm.\n";
|
||||||
|
|
||||||
if (0) {
|
if (0) {
|
||||||
require "Slic3r/SVG.pm";
|
require "Slic3r/SVG.pm";
|
||||||
@ -261,10 +285,17 @@ sub make_surfaces {
|
|||||||
);
|
);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$self->cleanup_lines;
|
||||||
|
$detect->();
|
||||||
|
|
||||||
|
if (@discarded_lines) {
|
||||||
|
print " Warning: even slow detection algorithm throwed errors. Review the output before printing.\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
my $expolygons = union_ex([ @polygons ], PFT_EVENODD);
|
my $expolygons = union_ex([ @polygons ]);
|
||||||
Slic3r::debugf " %d surface(s) having %d holes detected from %d polylines\n",
|
Slic3r::debugf " %d surface(s) having %d holes detected from %d polylines\n",
|
||||||
scalar(@$expolygons), scalar(map $_->holes, @$expolygons), scalar(@polygons);
|
scalar(@$expolygons), scalar(map $_->holes, @$expolygons), scalar(@polygons);
|
||||||
|
|
||||||
|
@ -2,10 +2,7 @@ package Slic3r::Line;
|
|||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
|
|
||||||
use constant A => 0;
|
use Slic3r::Geometry qw(A B X Y);
|
||||||
use constant B => 1;
|
|
||||||
use constant X => 0;
|
|
||||||
use constant Y => 1;
|
|
||||||
|
|
||||||
sub new {
|
sub new {
|
||||||
my $class = shift;
|
my $class = shift;
|
||||||
@ -100,4 +97,9 @@ sub midpoint {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub reverse {
|
||||||
|
my $self = shift;
|
||||||
|
@$self = reverse @$self;
|
||||||
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
@ -3,12 +3,9 @@ use Moo;
|
|||||||
|
|
||||||
use Math::Clipper ':all';
|
use Math::Clipper ':all';
|
||||||
use Math::ConvexHull 1.0.4 qw(convex_hull);
|
use Math::ConvexHull 1.0.4 qw(convex_hull);
|
||||||
use Slic3r::Geometry qw(shortest_path);
|
use Slic3r::Geometry qw(X Y shortest_path);
|
||||||
use XXX;
|
use XXX;
|
||||||
|
|
||||||
use constant X => 0;
|
|
||||||
use constant Y => 1;
|
|
||||||
|
|
||||||
sub make_perimeter {
|
sub make_perimeter {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my ($layer) = @_;
|
my ($layer) = @_;
|
||||||
|
@ -2,12 +2,10 @@ package Slic3r::Print;
|
|||||||
use Moo;
|
use Moo;
|
||||||
|
|
||||||
use Math::Clipper ':all';
|
use Math::Clipper ':all';
|
||||||
|
use Slic3r::Geometry qw(X Y);
|
||||||
use Slic3r::Geometry::Clipper qw(explode_expolygons safety_offset diff_ex intersection_ex);
|
use Slic3r::Geometry::Clipper qw(explode_expolygons safety_offset diff_ex intersection_ex);
|
||||||
use XXX;
|
use XXX;
|
||||||
|
|
||||||
use constant X => 0;
|
|
||||||
use constant Y => 1;
|
|
||||||
|
|
||||||
has 'x_length' => (
|
has 'x_length' => (
|
||||||
is => 'ro',
|
is => 'ro',
|
||||||
required => 1,
|
required => 1,
|
||||||
|
@ -2,12 +2,9 @@ package Slic3r::STL;
|
|||||||
use Moo;
|
use Moo;
|
||||||
|
|
||||||
use Math::Clipper qw(integerize_coordinate_sets is_counter_clockwise);
|
use Math::Clipper qw(integerize_coordinate_sets is_counter_clockwise);
|
||||||
use Slic3r::Geometry qw(three_points_aligned longest_segment);
|
use Slic3r::Geometry qw(X Y Z three_points_aligned longest_segment);
|
||||||
use XXX;
|
use XXX;
|
||||||
|
|
||||||
use constant X => 0;
|
|
||||||
use constant Y => 1;
|
|
||||||
use constant Z => 2;
|
|
||||||
use constant MIN => 0;
|
use constant MIN => 0;
|
||||||
use constant MAX => 1;
|
use constant MAX => 1;
|
||||||
|
|
||||||
|
91
t/collinear.t
Normal file
91
t/collinear.t
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
use Test::More;
|
||||||
|
use strict;
|
||||||
|
use warnings;
|
||||||
|
|
||||||
|
plan tests => 11;
|
||||||
|
|
||||||
|
BEGIN {
|
||||||
|
use FindBin;
|
||||||
|
use lib "$FindBin::Bin/../lib";
|
||||||
|
}
|
||||||
|
|
||||||
|
use Slic3r;
|
||||||
|
use Slic3r::Geometry qw(collinear);
|
||||||
|
|
||||||
|
#==========================================================
|
||||||
|
|
||||||
|
{
|
||||||
|
my @lines = (
|
||||||
|
[ [0,4], [4,2] ],
|
||||||
|
[ [2,3], [8,0] ],
|
||||||
|
[ [6,1], [8,0] ],
|
||||||
|
);
|
||||||
|
is collinear($lines[0], $lines[1]), 1, 'collinear';
|
||||||
|
is collinear($lines[1], $lines[2]), 1, 'collinear';
|
||||||
|
is collinear($lines[0], $lines[2]), 1, 'collinear';
|
||||||
|
}
|
||||||
|
|
||||||
|
#==========================================================
|
||||||
|
|
||||||
|
{
|
||||||
|
# horizontal
|
||||||
|
my @lines = (
|
||||||
|
[ [0,1], [5,1] ],
|
||||||
|
[ [2,1], [8,1] ],
|
||||||
|
);
|
||||||
|
is collinear($lines[0], $lines[1]), 1, 'collinear';
|
||||||
|
}
|
||||||
|
|
||||||
|
#==========================================================
|
||||||
|
|
||||||
|
{
|
||||||
|
# vertical
|
||||||
|
my @lines = (
|
||||||
|
[ [1,0], [1,5] ],
|
||||||
|
[ [1,2], [1,8] ],
|
||||||
|
);
|
||||||
|
is collinear($lines[0], $lines[1]), 1, 'collinear';
|
||||||
|
}
|
||||||
|
|
||||||
|
#==========================================================
|
||||||
|
|
||||||
|
{
|
||||||
|
# non overlapping
|
||||||
|
my @lines = (
|
||||||
|
[ [0,1], [5,1] ],
|
||||||
|
[ [7,1], [10,1] ],
|
||||||
|
);
|
||||||
|
is collinear($lines[0], $lines[1], 1), 0, 'non overlapping';
|
||||||
|
is collinear($lines[0], $lines[1], 0), 1, 'overlapping';
|
||||||
|
}
|
||||||
|
|
||||||
|
#==========================================================
|
||||||
|
|
||||||
|
{
|
||||||
|
# with one common point
|
||||||
|
my @lines = (
|
||||||
|
[ [0,4], [4,2] ],
|
||||||
|
[ [4,2], [8,0] ],
|
||||||
|
);
|
||||||
|
is collinear($lines[0], $lines[1], 1), 1, 'one common point';
|
||||||
|
is collinear($lines[0], $lines[1], 0), 1, 'one common point';
|
||||||
|
}
|
||||||
|
|
||||||
|
#==========================================================
|
||||||
|
|
||||||
|
{
|
||||||
|
# not collinear
|
||||||
|
my @lines = (
|
||||||
|
[ [290000000,690525600], [285163380,684761540] ],
|
||||||
|
[ [285163380,684761540], [193267599,575244400] ],
|
||||||
|
);
|
||||||
|
is collinear($lines[0], $lines[1], 0), 0, 'not collinear';
|
||||||
|
is collinear($lines[0], $lines[1], 1), 0, 'not collinear';
|
||||||
|
|
||||||
|
use Slic3r::SVG;
|
||||||
|
Slic3r::SVG::output(undef, "collinear.svg",
|
||||||
|
lines => \@lines,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#==========================================================
|
Loading…
Reference in New Issue
Block a user