Use the XS slicing code
This commit is contained in:
parent
873762491b
commit
1b9d2e2ef8
@ -145,56 +145,21 @@ sub slice {
|
||||
for my $region_id (0 .. $#{$self->meshes}) {
|
||||
my $mesh = $self->meshes->[$region_id] // next; # ignore undef meshes
|
||||
|
||||
my %lines = (); # layer_id => [ lines ]
|
||||
my $apply_lines = sub {
|
||||
my $lines = shift;
|
||||
foreach my $layer_id (keys %$lines) {
|
||||
$lines{$layer_id} ||= [];
|
||||
push @{$lines{$layer_id}}, @{$lines->{$layer_id}};
|
||||
{
|
||||
my $m = Slic3r::TriangleMesh::XS->new;
|
||||
$m->ReadFromPerl($mesh->vertices, $mesh->facets);
|
||||
$m->Repair;
|
||||
my $lines = $m->slice([ map $_->slice_z, @{$self->layers} ]);
|
||||
for my $layer_id (0..$#$lines) {
|
||||
my $layerm = $self->layers->[$layer_id]->regions->[$region_id];
|
||||
$layerm->make_surfaces($lines->[$layer_id]);
|
||||
}
|
||||
};
|
||||
Slic3r::parallelize(
|
||||
disable => ($#{$mesh->facets} < 500), # don't parallelize when too few facets
|
||||
items => [ 0..$#{$mesh->facets} ],
|
||||
thread_cb => sub {
|
||||
my $q = shift;
|
||||
my $result_lines = {};
|
||||
while (defined (my $facet_id = $q->dequeue)) {
|
||||
my $lines = $mesh->slice_facet($self, $facet_id);
|
||||
foreach my $layer_id (keys %$lines) {
|
||||
$result_lines->{$layer_id} ||= [];
|
||||
push @{ $result_lines->{$layer_id} }, @{ $lines->{$layer_id} };
|
||||
}
|
||||
}
|
||||
return $result_lines;
|
||||
},
|
||||
collect_cb => sub {
|
||||
$apply_lines->($_[0]);
|
||||
},
|
||||
no_threads_cb => sub {
|
||||
for (0..$#{$mesh->facets}) {
|
||||
my $lines = $mesh->slice_facet($self, $_);
|
||||
$apply_lines->($lines);
|
||||
}
|
||||
},
|
||||
);
|
||||
# TODO: read slicing_errors
|
||||
}
|
||||
|
||||
# free memory
|
||||
undef $mesh;
|
||||
undef $self->meshes->[$region_id];
|
||||
|
||||
foreach my $layer (@{ $self->layers }) {
|
||||
Slic3r::debugf "Making surfaces for layer %d (slice z = %f):\n",
|
||||
$layer->id, unscale $layer->slice_z if $Slic3r::debug;
|
||||
|
||||
my $layerm = $layer->regions->[$region_id];
|
||||
my ($slicing_errors, $loops) = Slic3r::TriangleMesh::make_loops($lines{$layer->id});
|
||||
$layer->slicing_errors(1) if $slicing_errors;
|
||||
$layerm->make_surfaces($loops);
|
||||
|
||||
# free memory
|
||||
delete $lines{$layer->id};
|
||||
}
|
||||
}
|
||||
|
||||
# free memory
|
||||
|
@ -18,18 +18,6 @@ has 'edges_facets' => (is => 'rw'); # id => [ $f1_id, $f2_id, (...) ]
|
||||
use constant MIN => 0;
|
||||
use constant MAX => 1;
|
||||
|
||||
use constant I_FMT => 'ffffllllc';
|
||||
use constant I_A => 0;
|
||||
use constant I_B => 1;
|
||||
use constant I_A_ID => 2;
|
||||
use constant I_B_ID => 3;
|
||||
use constant I_EDGE_A_ID => 4;
|
||||
use constant I_EDGE_B_ID => 5;
|
||||
use constant I_FACET_EDGE => 6;
|
||||
|
||||
use constant FE_TOP => 0;
|
||||
use constant FE_BOTTOM => 1;
|
||||
|
||||
sub analyze {
|
||||
my $self = shift;
|
||||
|
||||
@ -112,40 +100,6 @@ sub _facet_edges {
|
||||
);
|
||||
}
|
||||
|
||||
# This method is supposed to remove narrow triangles, but it actually doesn't
|
||||
# work much; I'm committing it for future reference but I'm going to remove it later.
|
||||
# Note: a 'clean' method should actually take care of non-manifold facets and remove
|
||||
# them.
|
||||
sub clean {
|
||||
my $self = shift;
|
||||
|
||||
# retrieve all edges shared by more than two facets;
|
||||
my @weird_edges = grep { @{$self->edge_facets->{$_}} != 2 } keys %{$self->edge_facets};
|
||||
|
||||
# usually most of these facets are very narrow triangles whose two edges
|
||||
# are detected as collapsed, and thus added twice to the edge in edge_fasets table
|
||||
# let's identify these triangles
|
||||
my @narrow_facets_indexes = ();
|
||||
foreach my $edge_id (@weird_edges) {
|
||||
my %facet_count = ();
|
||||
$facet_count{$_}++ for @{$self->edge_facets->{$edge_id}};
|
||||
@{$self->edge_facets->{$edge_id}} = grep $facet_count{$_} == 1, keys %facet_count;
|
||||
push @narrow_facets_indexes, grep $facet_count{$_} > 1, keys %facet_count;
|
||||
}
|
||||
|
||||
# remove identified narrow facets
|
||||
foreach my $facet_id (@narrow_facets_indexes) {last;
|
||||
splice @{$self->facets}, $facet_id, 1;
|
||||
splice @{$self->facets_edges}, $facet_id, 1;
|
||||
foreach my $facet_ides (values %{$self->edge_facets}) {
|
||||
@$facet_ides = map { $_ > $facet_id ? ($_-1) : $_ } @$facet_ides;
|
||||
}
|
||||
}
|
||||
|
||||
Slic3r::debugf "%d narrow facets removed\n", scalar(@narrow_facets_indexes)
|
||||
if @narrow_facets_indexes;
|
||||
}
|
||||
|
||||
sub check_manifoldness {
|
||||
my $self = shift;
|
||||
|
||||
@ -169,114 +123,6 @@ sub check_manifoldness {
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub unpack_line {
|
||||
my ($packed) = @_;
|
||||
|
||||
my $data = [ unpack I_FMT, $packed ];
|
||||
splice @$data, 0, 4, [ @$data[0,1] ], [ @$data[2,3] ];
|
||||
$data->[$_] = undef for grep $data->[$_] == -1, I_A_ID, I_B_ID, I_EDGE_A_ID, I_EDGE_B_ID, I_FACET_EDGE;
|
||||
return $data;
|
||||
}
|
||||
|
||||
sub make_loops {
|
||||
my ($lines) = @_;
|
||||
my @lines = map unpack_line($_), @$lines;
|
||||
|
||||
# remove tangent edges
|
||||
for my $i (0 .. $#lines) {
|
||||
next unless defined $lines[$i] && defined $lines[$i][I_FACET_EDGE];
|
||||
# if the line is a facet edge, find another facet edge
|
||||
# having the same endpoints but in reverse order
|
||||
for my $j ($i+1 .. $#lines) {
|
||||
next unless defined $lines[$j] && defined $lines[$j][I_FACET_EDGE];
|
||||
|
||||
# are these facets adjacent? (sharing a common edge on this layer)
|
||||
if ($lines[$i][I_A_ID] == $lines[$j][I_A_ID] && $lines[$i][I_B_ID] == $lines[$j][I_B_ID]) {
|
||||
|
||||
# if they are both oriented upwards or downwards (like a 'V')
|
||||
# then we can remove both edges from this layer since it won't
|
||||
# affect the sliced shape
|
||||
if ($lines[$j][I_FACET_EDGE] == $lines[$i][I_FACET_EDGE]) {
|
||||
$lines[$i] = undef;
|
||||
$lines[$j] = undef;
|
||||
last;
|
||||
}
|
||||
|
||||
# if one of them is oriented upwards and the other is oriented
|
||||
# downwards, let's only keep one of them (it doesn't matter which
|
||||
# one since all 'top' lines were reversed at slicing)
|
||||
if ($lines[$i][I_FACET_EDGE] != $lines[$j][I_FACET_EDGE]) {
|
||||
$lines[$j] = undef;
|
||||
last;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@lines = grep $_, @lines;
|
||||
|
||||
# build a map of lines by EDGE_A_ID and A_ID
|
||||
my %by_edge_a_id = my %by_a_id = ();
|
||||
for (0..$#lines) {
|
||||
if (defined(my $edge_a_id = $lines[$_][I_EDGE_A_ID])) {
|
||||
$by_edge_a_id{$edge_a_id} //= [];
|
||||
push @{ $by_edge_a_id{$edge_a_id} }, $_;
|
||||
}
|
||||
if (defined(my $a_id = $lines[$_][I_A_ID])) {
|
||||
$by_a_id{$a_id} //= [];
|
||||
push @{ $by_a_id{$a_id} }, $_;
|
||||
}
|
||||
}
|
||||
|
||||
my (@polygons, @failed_loops) = ();
|
||||
my %used_lines = ();
|
||||
CYCLE: while (1) {
|
||||
# take first spare line and start a new loop
|
||||
my $first_idx = first { !exists $used_lines{$_} } 0..$#lines;
|
||||
last if !defined $first_idx;
|
||||
$used_lines{$first_idx} = 1;
|
||||
my @loop = ($lines[$first_idx]);
|
||||
|
||||
while (1) {
|
||||
# find a line starting where last one finishes
|
||||
my $line_idx;
|
||||
$line_idx = first { !exists $used_lines{$_} } @{ $by_edge_a_id{$loop[-1][I_EDGE_B_ID]} // [] }
|
||||
if defined $loop[-1][I_EDGE_B_ID];
|
||||
$line_idx //= first { !exists $used_lines{$_} } @{ $by_a_id{$loop[-1][I_B_ID]} // [] }
|
||||
if defined $loop[-1][I_B_ID];
|
||||
|
||||
if (!defined $line_idx) {
|
||||
# check whether we closed this loop
|
||||
if ((defined $loop[0][I_EDGE_A_ID] && defined $loop[-1][I_EDGE_B_ID] && $loop[0][I_EDGE_A_ID] == $loop[-1][I_EDGE_B_ID])
|
||||
|| (defined $loop[0][I_A_ID] && defined $loop[-1][I_B_ID] && $loop[0][I_A_ID] == $loop[-1][I_B_ID])) {
|
||||
# loop is complete!
|
||||
push @polygons, Slic3r::Polygon->new(map $_->[I_A], @loop);
|
||||
Slic3r::debugf " Discovered %s polygon of %d points\n",
|
||||
($polygons[-1]->is_counter_clockwise ? 'ccw' : 'cw'), scalar(@{$polygons[-1]})
|
||||
if $Slic3r::debug;
|
||||
next CYCLE;
|
||||
}
|
||||
|
||||
# we can't close this loop!
|
||||
push @failed_loops, [@loop];
|
||||
next CYCLE;
|
||||
}
|
||||
push @loop, $lines[$line_idx];
|
||||
$used_lines{$line_idx} = 1;
|
||||
}
|
||||
}
|
||||
|
||||
# TODO: we should try to combine failed loops
|
||||
for my $loop (grep @$_ >= 3, @failed_loops) {
|
||||
push @polygons, Slic3r::Polygon->new(map $_->[I_A], @$loop);
|
||||
Slic3r::debugf " Discovered failed %s polygon of %d points\n",
|
||||
($polygons[-1]->is_counter_clockwise ? 'ccw' : 'cw'), scalar(@$loop)
|
||||
if $Slic3r::debug;
|
||||
}
|
||||
|
||||
return (@failed_loops ? 1 : 0, [@polygons]);
|
||||
}
|
||||
|
||||
sub rotate {
|
||||
my $self = shift;
|
||||
my ($deg, $center) = @_;
|
||||
@ -376,129 +222,6 @@ sub size {
|
||||
return $self->bounding_box->size;
|
||||
}
|
||||
|
||||
sub slice_facet {
|
||||
my $self = shift;
|
||||
my ($print_object, $facet_id) = @_;
|
||||
my @vertices = @{$self->facets->[$facet_id]}[-3..-1];
|
||||
Slic3r::debugf "\n==> FACET %d (%f,%f,%f - %f,%f,%f - %f,%f,%f):\n",
|
||||
$facet_id, map @{$self->vertices->[$_]}, @vertices
|
||||
if $Slic3r::debug;
|
||||
|
||||
# find the vertical extents of the facet
|
||||
my @z = map $_->[Z], @{$self->vertices}[@vertices];
|
||||
my $min_z = min(@z);
|
||||
my $max_z = max(@z);
|
||||
Slic3r::debugf "z: min = %.0f, max = %.0f\n", $min_z, $max_z
|
||||
if $Slic3r::debug;
|
||||
|
||||
if ($max_z == $min_z) {
|
||||
Slic3r::debugf "Facet is horizontal; ignoring\n";
|
||||
return;
|
||||
}
|
||||
|
||||
# calculate the layer extents
|
||||
my ($min_layer, $max_layer) = $print_object->get_layer_range($min_z, $max_z);
|
||||
Slic3r::debugf "layers: min = %s, max = %s\n", $min_layer, $max_layer
|
||||
if $Slic3r::debug;
|
||||
|
||||
my $lines = {}; # layer_id => [ lines ]
|
||||
for my $layer_id ($min_layer .. $max_layer) {
|
||||
my $layer = $print_object->layers->[$layer_id];
|
||||
$lines->{$layer_id} ||= [];
|
||||
push @{ $lines->{$layer_id} }, $self->intersect_facet($facet_id, $layer->slice_z);
|
||||
}
|
||||
return $lines;
|
||||
}
|
||||
|
||||
sub intersect_facet {
|
||||
my $self = shift;
|
||||
my ($facet_id, $z) = @_;
|
||||
|
||||
my @edge_ids = @{$self->facets_edges->[$facet_id]};
|
||||
my @edge_vertices_ids = $self->_facet_edges($facet_id);
|
||||
my %vertices;
|
||||
{
|
||||
my @vertices_ids = @{$self->facets->[$facet_id]}[-3..-1];
|
||||
%vertices = map { $_ => $self->vertices->[$_] } @vertices_ids; # cache vertices
|
||||
}
|
||||
|
||||
my (@points, @intersection_points, @points_on_layer) = ();
|
||||
|
||||
for my $e (0..2) {
|
||||
my ($a_id, $b_id) = @{$edge_vertices_ids[$e]};
|
||||
my ($a, $b) = @vertices{$a_id, $b_id};
|
||||
#printf "Az = %f, Bz = %f, z = %f\n", $a->[Z], $b->[Z], $z;
|
||||
|
||||
if ($a->[Z] == $b->[Z] && $a->[Z] == $z) {
|
||||
# edge is horizontal and belongs to the current layer
|
||||
my $edge_type = (grep $_->[Z] < $z, values %vertices) ? FE_TOP : FE_BOTTOM;
|
||||
if ($edge_type == FE_TOP) {
|
||||
($a, $b) = ($b, $a);
|
||||
($a_id, $b_id) = ($b_id, $a_id);
|
||||
}
|
||||
# We assume that this method is never being called for horizontal
|
||||
# facets, so no other edge is going to be on this layer.
|
||||
return pack I_FMT, (
|
||||
$a->[X], $a->[Y], # I_A
|
||||
$b->[X], $b->[Y], # I_B
|
||||
$a_id, # I_A_ID
|
||||
$b_id, # I_B_ID
|
||||
-1, # I_EDGE_A_ID
|
||||
-1, # I_EDGE_B_ID
|
||||
$edge_type, # I_FACET_EDGE
|
||||
);
|
||||
#print "Horizontal edge at $z!\n";
|
||||
|
||||
} elsif ($a->[Z] == $z) {
|
||||
#print "A point on plane $z!\n";
|
||||
push @points, [ $a->[X], $a->[Y], $a_id ];
|
||||
push @points_on_layer, $#points;
|
||||
|
||||
} elsif ($b->[Z] == $z) {
|
||||
#print "B point on plane $z!\n";
|
||||
push @points, [ $b->[X], $b->[Y], $b_id ];
|
||||
push @points_on_layer, $#points;
|
||||
|
||||
} elsif (($a->[Z] < $z && $b->[Z] > $z) || ($b->[Z] < $z && $a->[Z] > $z)) {
|
||||
# edge intersects the current layer; calculate intersection
|
||||
push @points, [
|
||||
$b->[X] + ($a->[X] - $b->[X]) * ($z - $b->[Z]) / ($a->[Z] - $b->[Z]),
|
||||
$b->[Y] + ($a->[Y] - $b->[Y]) * ($z - $b->[Z]) / ($a->[Z] - $b->[Z]),
|
||||
undef,
|
||||
$edge_ids[$e],
|
||||
];
|
||||
push @intersection_points, $#points;
|
||||
#print "Intersects at $z!\n";
|
||||
}
|
||||
}
|
||||
|
||||
if (@points_on_layer == 2) {
|
||||
if (@intersection_points == 1) {
|
||||
splice @points, $points_on_layer[1], 1;
|
||||
} elsif (@intersection_points == 0) {
|
||||
return if same_point(@points[@points_on_layer]);
|
||||
}
|
||||
}
|
||||
|
||||
if (@points) {
|
||||
# defensive programming:
|
||||
die "Facets must intersect each plane 0 or 2 times" if @points != 2;
|
||||
|
||||
return pack I_FMT, (
|
||||
$points[B][X], $points[B][Y], # I_A
|
||||
$points[A][X], $points[A][Y], # I_B
|
||||
$points[B][2] // -1, # I_A_ID /
|
||||
$points[A][2] // -1, # I_B_ID /
|
||||
$points[B][3] // -1, # I_EDGE_A_ID /
|
||||
$points[A][3] // -1, # I_EDGE_B_ID /
|
||||
-1, # I_FACET_EDGE
|
||||
);
|
||||
#printf " intersection points at z = %f: %f,%f - %f,%f\n", $z, map @$_, @intersection_points;
|
||||
}
|
||||
|
||||
return ();
|
||||
}
|
||||
|
||||
sub get_connected_facets {
|
||||
my $self = shift;
|
||||
my ($facet_id) = @_;
|
||||
|
Loading…
Reference in New Issue
Block a user