mirror of
https://github.com/SoftFever/OrcaSlicer.git
synced 2025-07-22 22:24:01 -06:00
Lots of changes and refactoring after testing with hollow objects
This commit is contained in:
parent
26b05ab155
commit
18c7aef1a7
15 changed files with 716 additions and 494 deletions
|
@ -1,6 +1,8 @@
|
|||
package Slic3r::Layer;
|
||||
use Moo;
|
||||
|
||||
use Math::Clipper ':all';
|
||||
use Math::Geometry::Planar;
|
||||
use XXX;
|
||||
|
||||
# a sequential number of layer, starting at 0
|
||||
|
@ -10,18 +12,8 @@ has 'id' => (
|
|||
required => 1,
|
||||
);
|
||||
|
||||
# index of points generated by slicing the original geometry
|
||||
# keys are stringified coordinates (example: "0,0")
|
||||
# each points connects exactly two segments
|
||||
has 'pointmap' => (
|
||||
traits => ['Hash'],
|
||||
is => 'rw',
|
||||
#isa => 'HashRef[Slic3r::Point]',
|
||||
default => sub { {} },
|
||||
);
|
||||
|
||||
# collection of segments generated by slicing the original geometry
|
||||
# each segment is part of a closed polyline
|
||||
# collection of spare segments generated by slicing the original geometry;
|
||||
# these need to be merged in continuos (closed) polylines
|
||||
has 'lines' => (
|
||||
is => 'rw',
|
||||
#isa => 'ArrayRef[Slic3r::Line]',
|
||||
|
@ -30,7 +22,6 @@ has 'lines' => (
|
|||
|
||||
# collection of surfaces generated by slicing the original geometry
|
||||
has 'surfaces' => (
|
||||
traits => ['Array'],
|
||||
is => 'rw',
|
||||
#isa => 'ArrayRef[Slic3r::Surface]',
|
||||
default => sub { [] },
|
||||
|
@ -53,7 +44,6 @@ has 'skirts' => (
|
|||
# collection of surfaces generated by offsetting the innermost perimeter(s)
|
||||
# they represent boundaries of areas to fill
|
||||
has 'fill_surfaces' => (
|
||||
traits => ['Array'],
|
||||
is => 'rw',
|
||||
#isa => 'ArrayRef[Slic3r::Surface]',
|
||||
default => sub { [] },
|
||||
|
@ -71,24 +61,21 @@ sub z {
|
|||
return $self->id * $Slic3r::layer_height / $Slic3r::resolution;
|
||||
}
|
||||
|
||||
sub points {
|
||||
my $self = shift;
|
||||
return values %{ $self->pointmap };
|
||||
}
|
||||
|
||||
sub add_surface {
|
||||
my $self = shift;
|
||||
my (@vertices) = @_;
|
||||
|
||||
my @points = map $self->add_point($_), @vertices;
|
||||
my $polyline = Slic3r::Polyline::Closed->new_from_points(@points);
|
||||
my @lines = map $self->add_line($_), @{ $polyline->lines };
|
||||
# convert arrayref points to Point objects
|
||||
@vertices = map Slic3r::Point->cast($_), @vertices;
|
||||
|
||||
my $surface = Slic3r::Surface->new(
|
||||
contour => Slic3r::Polyline::Closed->new(lines => \@lines),
|
||||
contour => Slic3r::Polyline::Closed->new(points => \@vertices),
|
||||
);
|
||||
push @{ $self->surfaces }, $surface;
|
||||
|
||||
# make sure our contour has its points in counter-clockwise order
|
||||
$surface->contour->make_counter_clockwise;
|
||||
|
||||
return $surface;
|
||||
}
|
||||
|
||||
|
@ -97,60 +84,12 @@ sub add_line {
|
|||
my ($a, $b) = @_;
|
||||
|
||||
# we accept either a Line object or a couple of points
|
||||
my $line;
|
||||
if ($b) {
|
||||
($a, $b) = map $self->add_point($_), ($a, $b);
|
||||
$line = Slic3r::Line->new(a => $a, b => $b);
|
||||
} elsif (ref $a eq 'Slic3r::Line') {
|
||||
$line = $a;
|
||||
}
|
||||
|
||||
# check whether we already have such a line
|
||||
foreach my $point ($line->a, $line->b) {
|
||||
foreach my $existing_line (grep $_, @{$point->lines}) {
|
||||
return $existing_line
|
||||
if $line->coincides_with($existing_line) && $line ne $existing_line;
|
||||
}
|
||||
}
|
||||
my $line = Slic3r::Line->cast([ $a, $b ]);
|
||||
|
||||
push @{ $self->lines }, $line;
|
||||
return $line;
|
||||
}
|
||||
|
||||
sub add_point {
|
||||
my $self = shift;
|
||||
my ($point) = @_;
|
||||
|
||||
# we accept either a Point object or a pair of coordinates
|
||||
if (ref $point eq 'ARRAY') {
|
||||
$point = Slic3r::Point->new('x' => $point->[0], 'y' => $point->[1]);
|
||||
}
|
||||
|
||||
# check whether we already defined this point
|
||||
if (my $existing_point = $self->pointmap_get($point->x, $point->y)) { #)
|
||||
return $existing_point;
|
||||
}
|
||||
|
||||
# define the new point
|
||||
$self->pointmap->{ $point->id } = $point; #}}
|
||||
|
||||
return $point;
|
||||
}
|
||||
|
||||
sub pointmap_get {
|
||||
my $self = shift;
|
||||
my ($x, $y) = @_;
|
||||
|
||||
return $self->pointmap->{"$x,$y"};
|
||||
}
|
||||
|
||||
sub remove_point {
|
||||
my $self = shift;
|
||||
my ($point) = @_;
|
||||
|
||||
delete $self->pointmap->{ $point->id }; #}}
|
||||
}
|
||||
|
||||
sub remove_line {
|
||||
my $self = shift;
|
||||
my ($line) = @_;
|
||||
|
@ -163,81 +102,62 @@ sub remove_surface {
|
|||
@{ $self->surfaces } = grep $_ ne $surface, @{ $self->surfaces };
|
||||
}
|
||||
|
||||
# merge parallel and continuous lines
|
||||
sub merge_continuous_lines {
|
||||
my $self = shift;
|
||||
|
||||
my $finished = 0;
|
||||
CYCLE: while (!$finished) {
|
||||
foreach my $line (@{ $self->lines }) {
|
||||
# TODO: we shouldn't skip lines already included in polylines
|
||||
next if $line->polyline;
|
||||
my $slope = $line->slope;
|
||||
|
||||
foreach my $point ($line->points) {
|
||||
# skip points connecting more than two lines
|
||||
next if @{ $point->lines } > 2;
|
||||
|
||||
foreach my $neighbor_line (@{ $point->lines }) {
|
||||
next if $neighbor_line eq $line;
|
||||
|
||||
# skip line if it's not parallel to ours
|
||||
my $neighbor_slope = $neighbor_line->slope;
|
||||
next if (!defined $neighbor_slope && defined $slope)
|
||||
|| (defined $neighbor_slope && !defined $slope)
|
||||
|| (defined $neighbor_slope && defined $slope && $neighbor_slope != $slope);
|
||||
|
||||
# create new line
|
||||
my ($a, $b) = grep $_ ne $point, $line->points, $neighbor_line->points;
|
||||
my $new_line = $self->add_line($a, $b);
|
||||
Slic3r::debugf "Merging continuous lines %s and %s into %s\n",
|
||||
$line->id, $neighbor_line->id, $new_line->id if $Slic3r::debug;
|
||||
|
||||
# delete merged lines
|
||||
$self->remove_line($_) for ($line, $neighbor_line);
|
||||
|
||||
# restart cycle
|
||||
next CYCLE;
|
||||
}
|
||||
}
|
||||
}
|
||||
$finished = 1;
|
||||
}
|
||||
}
|
||||
|
||||
# build polylines of lines which do not already belong to a surface
|
||||
sub make_polylines {
|
||||
my $self = shift;
|
||||
|
||||
# defensive programming: let's check that every point
|
||||
# connects at least two lines
|
||||
foreach my $point ($self->points) {
|
||||
if (grep $_, @{ $point->lines } < 2) {
|
||||
warn "Found point connecting less than 2 lines:";
|
||||
XXX $point;
|
||||
# make a cache of line endpoints
|
||||
my %pointmap = ();
|
||||
foreach my $line (@{ $self->lines }) {
|
||||
for my $point (@{ $line->points }) {
|
||||
$pointmap{$point->id} ||= [];
|
||||
push @{ $pointmap{$point->id} }, $line;
|
||||
}
|
||||
}
|
||||
|
||||
# defensive programming
|
||||
die "No point should be endpoint of less or more than 2 lines!"
|
||||
if grep @$_ != 2, values %pointmap;
|
||||
|
||||
# make a subroutine to remove lines from pointmap
|
||||
my $remove_line = sub {
|
||||
my $line = shift;
|
||||
foreach my $lines ($pointmap{$line->a->id}, $pointmap{$line->b->id}) {
|
||||
@$lines = grep $_ ne $line, @$lines;
|
||||
}
|
||||
};
|
||||
|
||||
my $polylines = [];
|
||||
foreach my $line (@{ $self->lines }) {
|
||||
next if $line->polyline;
|
||||
|
||||
# loop while we have spare lines
|
||||
while (my ($first_line) = map @$_, values %pointmap) {
|
||||
# add first line to a new polyline
|
||||
my $points = [ $first_line->a, $first_line->b ];
|
||||
$remove_line->($first_line);
|
||||
my $last_point = $first_line->b;
|
||||
|
||||
my %points = map {$_ => $_} $line->points;
|
||||
my %visited_lines = ();
|
||||
my ($cur_line, $next_line) = ($line, undef);
|
||||
while (!$next_line || $next_line ne $line) {
|
||||
$visited_lines{ $cur_line } = $cur_line;
|
||||
# loop through connected lines until we return to the first point
|
||||
while (my $next_line = $pointmap{$last_point->id}->[0]) {
|
||||
|
||||
$next_line = +(grep !$visited_lines{$_}, $cur_line->neighbors)[0]
|
||||
or last;
|
||||
# get next point
|
||||
($last_point) = grep $_->id ne $last_point->id, @{$next_line->points};
|
||||
|
||||
$points{$_} = $_ for grep $_ ne $cur_line->a && $_ ne $cur_line->b, $next_line->points;
|
||||
$cur_line = $next_line;
|
||||
# add point to polyline
|
||||
push @$points, $last_point;
|
||||
$remove_line->($next_line);
|
||||
}
|
||||
|
||||
Slic3r::debugf "Discovered polyline of %d lines (%s)\n", scalar keys %points,
|
||||
join('-', map $_->id, values %visited_lines) if $Slic3r::debug;
|
||||
push @$polylines, Slic3r::Polyline::Closed->new(lines => [values %visited_lines]);
|
||||
# remove last point as it coincides with first one
|
||||
pop @$points;
|
||||
|
||||
die "Invalid polyline with only 2 points\n" if @$points == 2;
|
||||
|
||||
Slic3r::debugf "Discovered polyline of %d points (%s)\n", scalar @$points,
|
||||
join ' - ', map $_->id, @$points;
|
||||
push @$polylines, Slic3r::Polyline::Closed->new(points => $points);
|
||||
|
||||
# actually this is not needed, as Math::Clipper used in make_surfaces() also cleans contours
|
||||
$polylines->[-1]->merge_continuous_lines;
|
||||
}
|
||||
|
||||
return $polylines;
|
||||
|
@ -255,46 +175,59 @@ sub make_surfaces {
|
|||
foreach my $polyline (@$polylines) {
|
||||
# a polyline encloses another one if any point of it is enclosed
|
||||
# in the other
|
||||
my $point = $polyline->lines->[0]->a;
|
||||
my $point = $polyline->points->[0];
|
||||
my $ordered_id = $polyline->id;
|
||||
|
||||
$enclosing_polylines{$polyline} =
|
||||
[ grep $_ ne $polyline && $_->encloses_point($point), @$polylines ];
|
||||
[ grep $_->id ne $ordered_id && $_->encloses_point($point), @$polylines ];
|
||||
$enclosing_polylines_count{$polyline} = scalar @{ $enclosing_polylines{$polyline} };
|
||||
|
||||
$max_depth = $enclosing_polylines_count{$polyline}
|
||||
if $enclosing_polylines_count{$polyline} > $max_depth;
|
||||
}
|
||||
|
||||
# make a cache for contours and surfaces
|
||||
my %surfaces = (); # contour => surface
|
||||
|
||||
# start looking at most inner polylines
|
||||
for (; $max_depth > -1; $max_depth--) {
|
||||
foreach my $polyline (@$polylines) {
|
||||
next if $polyline->contour_of or $polyline->hole_of;
|
||||
next unless $enclosing_polylines_count{$polyline} == $max_depth;
|
||||
|
||||
my $surface;
|
||||
if ($enclosing_polylines_count{$polyline} % 2 == 0) {
|
||||
# this is a contour
|
||||
$polyline->make_counter_clockwise;
|
||||
$surface = Slic3r::Surface->new(contour => $polyline);
|
||||
} else {
|
||||
# this is a hole
|
||||
$polyline->make_clockwise;
|
||||
|
||||
# find the enclosing polyline having immediately close depth
|
||||
my ($contour) = grep $enclosing_polylines_count{$_} == ($max_depth-1),
|
||||
@{ $enclosing_polylines{$polyline} };
|
||||
|
||||
if ($contour->contour_of) {
|
||||
$surface = $contour->contour_of;
|
||||
if ($surfaces{$contour}) {
|
||||
$surface = $surfaces{$contour};
|
||||
$surface->add_hole($polyline);
|
||||
} else {
|
||||
$surface = Slic3r::Surface->new(
|
||||
contour => $contour,
|
||||
holes => [$polyline],
|
||||
);
|
||||
$surfaces{$contour} = $surface;
|
||||
}
|
||||
}
|
||||
|
||||
# check whether we already have this surface
|
||||
next if grep $_->id eq $surface->id, @{ $self->surfaces };
|
||||
|
||||
$surface->surface_type('internal');
|
||||
push @{ $self->surfaces }, $surface;
|
||||
|
||||
Slic3r::debugf "New surface: %s (holes: %s)\n",
|
||||
$surface->id, join(', ', map $_->id, @{$surface->holes}) || 'none'
|
||||
Slic3r::debugf "New surface: %s (%d holes: %s)\n",
|
||||
$surface->id, scalar @{$surface->holes},
|
||||
join(', ', map $_->id, @{$surface->holes}) || 'none'
|
||||
if $Slic3r::debug;
|
||||
}
|
||||
}
|
||||
|
@ -303,62 +236,71 @@ sub make_surfaces {
|
|||
sub merge_contiguous_surfaces {
|
||||
my $self = shift;
|
||||
|
||||
my $finished = 0;
|
||||
CYCLE: while (!$finished) {
|
||||
foreach my $surface (@{ $self->surfaces }) {
|
||||
# look for a surface sharing one edge with this one
|
||||
foreach my $neighbor_surface (@{ $self->surfaces }) {
|
||||
next if $surface eq $neighbor_surface;
|
||||
|
||||
# find lines shared by the two surfaces (might be 0, 1, 2)
|
||||
my @common_lines = ();
|
||||
foreach my $line (@{ $neighbor_surface->contour->lines }) {
|
||||
next unless grep $_ eq $line, @{ $surface->contour->lines };
|
||||
push @common_lines, $line;
|
||||
}
|
||||
next if !@common_lines;
|
||||
|
||||
# defensive programming
|
||||
if (@common_lines > 2) {
|
||||
Slic3r::debugf "Surfaces %s and %s share %d lines! How's it possible?\n",
|
||||
$surface->id, $neighbor_surface->id, scalar @common_lines if $Slic3r::debug;
|
||||
}
|
||||
|
||||
Slic3r::debugf "Surfaces %s and %s share line/lines %s!\n",
|
||||
$surface->id, $neighbor_surface->id,
|
||||
join(', ', map $_->id, @common_lines) if $Slic3r::debug;
|
||||
|
||||
# defensive programming
|
||||
if ($surface->surface_type ne $neighbor_surface->surface_type) {
|
||||
die "Surfaces %s and %s are of different types: %s, %s!\n",
|
||||
$surface->id, $neighbor_surface->id,
|
||||
$surface->surface_type, $neighbor_surface->surface_type;
|
||||
}
|
||||
|
||||
# build new contour taking all lines of the surfaces' contours
|
||||
# and removing the ones that matched
|
||||
my @new_lines = map @{$_->contour->lines}, $surface, $neighbor_surface;
|
||||
foreach my $line (@common_lines) {
|
||||
@new_lines = grep $_ ne $line, @new_lines;
|
||||
}
|
||||
my $new_contour = Slic3r::Polyline::Closed->new(
|
||||
lines => [ @new_lines ],
|
||||
);
|
||||
|
||||
# build new surface by combining all holes in the two surfaces
|
||||
my $new_surface = Slic3r::Surface->new(
|
||||
contour => $new_contour,
|
||||
holes => [ map @{$_->holes}, $surface, $neighbor_surface ],
|
||||
surface_type => $surface->surface_type,
|
||||
);
|
||||
|
||||
Slic3r::debugf " merging into new surface %s\n", $new_surface->id;
|
||||
push @{ $self->surfaces }, $new_surface;
|
||||
|
||||
$self->remove_surface($_) for ($surface, $neighbor_surface);
|
||||
}
|
||||
if ($Slic3r::debug) {
|
||||
Slic3r::debugf "Initial surfaces (%d):\n", scalar @{ $self->surfaces };
|
||||
Slic3r::debugf " [%s] %s (%s with %d holes)\n", $_->surface_type, $_->id,
|
||||
($_->contour->is_counter_clockwise ? 'ccw' : 'cw'), scalar @{$_->holes} for @{ $self->surfaces };
|
||||
#Slic3r::SVG::output_polygons($main::print, "polygons-before.svg", [ map $_->contour->p, @{$self->surfaces} ]);
|
||||
}
|
||||
|
||||
my %resulting_surfaces = ();
|
||||
|
||||
# only merge surfaces with same type
|
||||
foreach my $type (qw(bottom top internal)) {
|
||||
my $clipper = Math::Clipper->new;
|
||||
my @surfaces = grep $_->surface_type eq $type, @{$self->surfaces}
|
||||
or next;
|
||||
|
||||
#Slic3r::SVG::output_polygons($main::print, "polygons-$type-before.svg", [ map $_->contour->p, @surfaces ]);
|
||||
$clipper->add_subject_polygons([ map $_->contour->p, @surfaces ]);
|
||||
|
||||
my $result = $clipper->ex_execute(CT_UNION, PFT_NONZERO, PFT_NONZERO);
|
||||
$clipper->clear;
|
||||
|
||||
my @extra_holes = map @{$_->{holes}}, @$result;
|
||||
$result = [ map $_->{outer}, @$result ];
|
||||
#Slic3r::SVG::output_polygons($main::print, "polygons-$type-union.svg", $result);
|
||||
|
||||
# subtract bottom or top surfaces from internal
|
||||
if ($type eq 'internal') {
|
||||
$clipper->add_subject_polygons($result);
|
||||
$clipper->add_clip_polygons([ map $_->{outer}, @{$resulting_surfaces{$_}} ])
|
||||
for qw(bottom top);
|
||||
$result = $clipper->execute(CT_DIFFERENCE, PFT_NONZERO, PFT_NONZERO);
|
||||
$clipper->clear;
|
||||
}
|
||||
$finished = 1;
|
||||
|
||||
# apply holes
|
||||
$clipper->add_subject_polygons($result);
|
||||
$result = $clipper->execute(CT_DIFFERENCE, PFT_NONZERO, PFT_NONZERO);
|
||||
$clipper->clear;
|
||||
|
||||
$clipper->add_subject_polygons($result);
|
||||
$clipper->add_clip_polygons([ @extra_holes ]) if @extra_holes;
|
||||
$clipper->add_clip_polygons([ map $_->p, map @{$_->holes}, @surfaces ]);
|
||||
my $result2 = $clipper->ex_execute(CT_DIFFERENCE, PFT_NONZERO, PFT_NONZERO);
|
||||
|
||||
$resulting_surfaces{$type} = $result2;
|
||||
}
|
||||
|
||||
# save surfaces
|
||||
@{ $self->surfaces } = ();
|
||||
foreach my $type (keys %resulting_surfaces) {
|
||||
foreach my $p (@{ $resulting_surfaces{$type} }) {
|
||||
push @{ $self->surfaces }, Slic3r::Surface->new(
|
||||
surface_type => $type,
|
||||
contour => Slic3r::Polyline::Closed->cast($p->{outer}),
|
||||
holes => [
|
||||
map Slic3r::Polyline::Closed->cast($_), @{$p->{holes}}
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ($Slic3r::debug) {
|
||||
Slic3r::debugf "Final surfaces (%d):\n", scalar @{ $self->surfaces };
|
||||
Slic3r::debugf " [%s] %s (%s with %d holes)\n", $_->surface_type, $_->id,
|
||||
($_->contour->is_counter_clockwise ? 'ccw' : 'cw'), scalar @{$_->holes} for @{ $self->surfaces };
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue