2011-11-27 10:40:03 +00:00
|
|
|
package Slic3r::TriangleMesh;
|
|
|
|
use Moo;
|
|
|
|
|
2012-03-11 15:02:17 +00:00
|
|
|
use Slic3r::Geometry qw(X Y Z A B unscale same_point);
|
2012-11-23 23:13:04 +00:00
|
|
|
use Slic3r::Geometry::Clipper qw(union_ex);
|
2011-11-27 10:40:03 +00:00
|
|
|
|
2012-02-18 19:36:14 +00:00
|
|
|
# public
|
2012-04-30 12:56:01 +00:00
|
|
|
has 'vertices' => (is => 'ro', required => 1); # id => [$x,$y,$z]
|
2012-05-20 09:05:16 +00:00
|
|
|
has 'facets' => (is => 'ro', required => 1); # id => [ $v1_id, $v2_id, $v3_id ]
|
2012-02-18 19:36:14 +00:00
|
|
|
|
|
|
|
# private
|
|
|
|
has 'edges' => (is => 'ro', default => sub { [] }); # id => [ $v1_id, $v2_id ]
|
|
|
|
has 'facets_edges' => (is => 'ro', default => sub { [] }); # id => [ $e1_id, $e2_id, $e3_id ]
|
|
|
|
has 'edges_facets' => (is => 'ro', default => sub { [] }); # id => [ $f1_id, $f2_id, (...) ]
|
2011-11-27 10:40:03 +00:00
|
|
|
|
|
|
|
use constant MIN => 0;
|
|
|
|
use constant MAX => 1;
|
|
|
|
|
2012-06-06 16:57:46 +00:00
|
|
|
use constant I_FMT => 'ffllLllc';
|
2012-05-01 08:53:52 +00:00
|
|
|
use constant I_B => 0;
|
|
|
|
use constant I_A_ID => 1;
|
|
|
|
use constant I_B_ID => 2;
|
|
|
|
use constant I_FACET_INDEX => 3;
|
|
|
|
use constant I_PREV_FACET_INDEX => 4;
|
|
|
|
use constant I_NEXT_FACET_INDEX => 5;
|
|
|
|
use constant I_FACET_EDGE => 6;
|
|
|
|
|
2012-05-10 18:07:20 +00:00
|
|
|
use constant FE_TOP => 0;
|
|
|
|
use constant FE_BOTTOM => 1;
|
2012-05-01 08:53:52 +00:00
|
|
|
|
2012-02-19 09:48:58 +00:00
|
|
|
# always make sure BUILD is idempotent
|
2012-02-18 19:36:14 +00:00
|
|
|
sub BUILD {
|
2011-11-27 10:40:03 +00:00
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
@{$self->edges} = ();
|
2012-02-18 19:36:14 +00:00
|
|
|
@{$self->facets_edges} = ();
|
|
|
|
@{$self->edges_facets} = ();
|
|
|
|
my %table = (); # edge_coordinates => edge_id
|
|
|
|
|
|
|
|
for (my $facet_id = 0; $facet_id <= $#{$self->facets}; $facet_id++) {
|
2012-05-20 09:08:44 +00:00
|
|
|
my $facet = $self->facets->[$facet_id];
|
2012-02-18 19:36:14 +00:00
|
|
|
$self->facets_edges->[$facet_id] = [];
|
|
|
|
|
|
|
|
# reorder vertices so that the first one is the one with lowest Z
|
|
|
|
# this is needed to get all intersection lines in a consistent order
|
|
|
|
# (external on the right of the line)
|
|
|
|
{
|
2012-05-20 09:05:16 +00:00
|
|
|
my @z_order = sort { $self->vertices->[$facet->[$a]][Z] <=> $self->vertices->[$facet->[$b]][Z] } -3..-1;
|
|
|
|
@$facet[-3..-1] = (@$facet[$z_order[0]..-1], @$facet[-3..($z_order[0]-1)]);
|
2012-02-18 19:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# ignore the normal if provided
|
|
|
|
my @vertices = @$facet[-3..-1];
|
|
|
|
|
|
|
|
foreach my $edge ($self->_facet_edges($facet_id)) {
|
|
|
|
my $edge_coordinates = join ';', sort @$edge;
|
|
|
|
my $edge_id = $table{$edge_coordinates};
|
|
|
|
if (!defined $edge_id) {
|
|
|
|
# Note that the order of vertices in $self->edges is *casual* because it is only
|
|
|
|
# good for one of the two adjacent facets. For this reason, it must not be used
|
|
|
|
# when dealing with single facets.
|
2011-11-27 10:40:03 +00:00
|
|
|
push @{$self->edges}, $edge;
|
2012-02-18 19:36:14 +00:00
|
|
|
$edge_id = $#{$self->edges};
|
|
|
|
$table{$edge_coordinates} = $edge_id;
|
|
|
|
$self->edges_facets->[$edge_id] = [];
|
2011-11-27 10:40:03 +00:00
|
|
|
}
|
2012-02-18 19:36:14 +00:00
|
|
|
|
|
|
|
push @{$self->facets_edges->[$facet_id]}, $edge_id;
|
|
|
|
push @{$self->edges_facets->[$edge_id]}, $facet_id;
|
2011-11-27 10:40:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-12 14:30:44 +00:00
|
|
|
sub merge {
|
|
|
|
my $class = shift;
|
|
|
|
my @meshes = @_;
|
|
|
|
|
|
|
|
my $vertices = [];
|
|
|
|
my $facets = [];
|
|
|
|
|
|
|
|
foreach my $mesh (@meshes) {
|
|
|
|
my $v_offset = @$vertices;
|
|
|
|
push @$vertices, @{$mesh->vertices};
|
|
|
|
push @$facets, map {
|
|
|
|
my $f = [@$_];
|
|
|
|
$f->[$_] += $v_offset for -3..-1;
|
|
|
|
$f;
|
|
|
|
} @{$mesh->facets};
|
|
|
|
}
|
|
|
|
|
|
|
|
return $class->new(vertices => $vertices, facets => $facets);
|
|
|
|
}
|
|
|
|
|
2012-04-30 15:10:54 +00:00
|
|
|
sub clone {
|
|
|
|
my $self = shift;
|
|
|
|
return (ref $self)->new(
|
|
|
|
vertices => [ map [ @$_ ], @{$self->vertices} ],
|
|
|
|
facets => [ map [ @$_ ], @{$self->facets} ],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-02-18 19:36:14 +00:00
|
|
|
sub _facet_edges {
|
2011-11-27 10:40:03 +00:00
|
|
|
my $self = shift;
|
2012-02-18 19:36:14 +00:00
|
|
|
my ($facet_id) = @_;
|
2011-11-27 10:40:03 +00:00
|
|
|
|
2012-02-18 19:36:14 +00:00
|
|
|
my $facet = $self->facets->[$facet_id];
|
|
|
|
return (
|
2012-05-20 09:05:16 +00:00
|
|
|
[ $facet->[-3], $facet->[-2] ],
|
|
|
|
[ $facet->[-2], $facet->[-1] ],
|
|
|
|
[ $facet->[-1], $facet->[-3] ],
|
2012-02-18 19:36:14 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
# 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;
|
|
|
|
}
|
2011-11-27 10:40:03 +00:00
|
|
|
|
2012-02-18 19:36:14 +00:00
|
|
|
# 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;
|
|
|
|
|
2012-05-31 17:15:40 +00:00
|
|
|
# look for any edges not connected to exactly two facets
|
|
|
|
my ($first_bad_edge_id) =
|
|
|
|
grep { @{ $self->edges_facets->[$_] } != 2 } 0..$#{$self->edges_facets};
|
|
|
|
if (defined $first_bad_edge_id) {
|
2013-01-25 17:02:01 +00:00
|
|
|
warn sprintf "Warning: The input file contains a hole near edge %f,%f,%f-%f,%f,%f (not manifold). "
|
2012-07-21 10:07:02 +00:00
|
|
|
. "You might want to repair it and retry, or to check the resulting G-code before printing anyway.\n",
|
2013-01-25 17:02:01 +00:00
|
|
|
map @{$self->vertices->[$_]}, @{$self->edges->[$first_bad_edge_id]};
|
2012-10-24 20:44:08 +00:00
|
|
|
return 0;
|
2011-11-27 10:40:03 +00:00
|
|
|
}
|
2012-10-24 20:44:08 +00:00
|
|
|
return 1;
|
2011-11-27 10:40:03 +00:00
|
|
|
}
|
|
|
|
|
2012-05-29 12:52:39 +00:00
|
|
|
sub unpack_line {
|
|
|
|
my ($packed) = @_;
|
|
|
|
|
|
|
|
my @data = unpack I_FMT, $packed;
|
|
|
|
splice @data, 0, 2, [ @data[0,1] ];
|
2012-06-06 16:57:46 +00:00
|
|
|
$data[$_] = undef for grep $data[$_] == -1, I_A_ID, I_B_ID, I_FACET_EDGE, I_PREV_FACET_INDEX, I_NEXT_FACET_INDEX;
|
2012-05-29 12:52:39 +00:00
|
|
|
return [@data];
|
|
|
|
}
|
|
|
|
|
2011-11-27 10:40:03 +00:00
|
|
|
sub make_loops {
|
2012-09-22 17:04:36 +00:00
|
|
|
my ($lines) = @_;
|
|
|
|
my @lines = map unpack_line($_), @$lines;
|
2011-11-27 10:40:03 +00:00
|
|
|
|
|
|
|
# remove tangent edges
|
|
|
|
{
|
|
|
|
for (my $i = 0; $i <= $#lines; $i++) {
|
2012-05-10 18:07:20 +00:00
|
|
|
next unless defined $lines[$i] && defined $lines[$i][I_FACET_EDGE];
|
2011-11-27 10:40:03 +00:00
|
|
|
# if the line is a facet edge, find another facet edge
|
|
|
|
# having the same endpoints but in reverse order
|
|
|
|
for (my $j = $i+1; $j <= $#lines; $j++) {
|
2012-05-10 18:07:20 +00:00
|
|
|
next unless defined $lines[$j] && defined $lines[$j][I_FACET_EDGE];
|
2012-02-18 19:36:14 +00:00
|
|
|
|
|
|
|
# are these facets adjacent? (sharing a common edge on this layer)
|
2012-05-02 13:12:24 +00:00
|
|
|
if ($lines[$i][I_A_ID] == $lines[$j][I_B_ID] && $lines[$i][I_B_ID] == $lines[$j][I_A_ID]) {
|
2012-02-18 19:36:14 +00:00
|
|
|
|
|
|
|
# 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
|
2012-05-10 18:07:20 +00:00
|
|
|
if ($lines[$j][I_FACET_EDGE] == $lines[$i][I_FACET_EDGE]) {
|
2012-02-18 19:36:14 +00:00
|
|
|
$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)
|
2012-05-02 13:12:24 +00:00
|
|
|
if ($lines[$i][I_FACET_EDGE] == FE_TOP && $lines[$j][I_FACET_EDGE] == FE_BOTTOM) {
|
2012-02-18 19:36:14 +00:00
|
|
|
$lines[$j] = undef;
|
|
|
|
last;
|
|
|
|
}
|
2011-11-27 10:40:03 +00:00
|
|
|
}
|
2012-02-18 19:36:14 +00:00
|
|
|
|
2011-11-27 10:40:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-18 19:36:14 +00:00
|
|
|
@lines = grep $_, @lines;
|
2011-11-27 10:40:03 +00:00
|
|
|
|
2012-02-18 19:36:14 +00:00
|
|
|
# count relationships
|
|
|
|
my %prev_count = (); # how many lines have the same prev_facet_index
|
|
|
|
my %a_count = (); # how many lines have the same a_id
|
|
|
|
foreach my $line (@lines) {
|
2012-05-01 08:53:52 +00:00
|
|
|
if (defined $line->[I_PREV_FACET_INDEX]) {
|
|
|
|
$prev_count{$line->[I_PREV_FACET_INDEX]}++;
|
2012-02-18 19:36:14 +00:00
|
|
|
}
|
2012-05-01 08:53:52 +00:00
|
|
|
if (defined $line->[I_A_ID]) {
|
|
|
|
$a_count{$line->[I_A_ID]}++;
|
2012-02-18 19:36:14 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-27 10:40:03 +00:00
|
|
|
|
2012-02-18 19:36:14 +00:00
|
|
|
foreach my $point_id (grep $a_count{$_} > 1, keys %a_count) {
|
2012-05-10 21:23:24 +00:00
|
|
|
my @lines_starting_here = grep defined $_->[I_A_ID] && $_->[I_A_ID] == $point_id, @lines;
|
2012-02-18 19:36:14 +00:00
|
|
|
Slic3r::debugf "%d lines start at point %d\n", scalar(@lines_starting_here), $point_id;
|
2011-11-27 10:40:03 +00:00
|
|
|
|
2012-02-18 19:36:14 +00:00
|
|
|
# if two lines start at this point, one being a 'top' facet edge and the other being a 'bottom' one,
|
|
|
|
# then remove the top one and those following it (removing the top or the bottom one is an arbitrary
|
|
|
|
# choice)
|
2012-05-10 21:23:24 +00:00
|
|
|
# The "// ''" on the next line avoids uninitialized value errors mentioned in issue #357 but these
|
|
|
|
# errors occur on fixed models so the root cause still needs to be found
|
2012-05-20 09:05:16 +00:00
|
|
|
if (@lines_starting_here == 2 && join('', sort map $_->[I_FACET_EDGE] // '', @lines_starting_here) eq FE_TOP.FE_BOTTOM) { #/
|
2012-05-02 13:12:24 +00:00
|
|
|
my @to_remove = grep $_->[I_FACET_EDGE] == FE_TOP, @lines_starting_here;
|
2012-05-01 08:53:52 +00:00
|
|
|
while (!grep defined $_->[I_B_ID] && $_->[I_B_ID] == $to_remove[-1]->[I_B_ID] && $_ ne $to_remove[-1], @lines) {
|
|
|
|
push @to_remove, grep defined $_->[I_A_ID] && $_->[I_A_ID] == $to_remove[-1]->[I_B_ID], @lines;
|
2012-02-18 19:36:14 +00:00
|
|
|
}
|
|
|
|
my %to_remove = map {$_ => 1} @to_remove;
|
|
|
|
@lines = grep !$to_remove{$_}, @lines;
|
|
|
|
} else {
|
|
|
|
Slic3r::debugf " this shouldn't happen and should be further investigated\n";
|
|
|
|
if (0) {
|
|
|
|
require "Slic3r/SVG.pm";
|
2012-11-01 10:34:53 +00:00
|
|
|
Slic3r::SVG::output("same_point.svg",
|
2012-05-10 18:07:20 +00:00
|
|
|
lines => [ map $_->line, grep !defined $_->[I_FACET_EDGE], @lines ],
|
|
|
|
red_lines => [ map $_->line, grep defined $_->[I_FACET_EDGE], @lines ],
|
2012-05-28 17:58:27 +00:00
|
|
|
#points => [ $self->vertices->[$point_id] ],
|
2012-02-18 19:36:14 +00:00
|
|
|
no_arrows => 0,
|
|
|
|
);
|
2011-11-27 10:40:03 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-18 19:36:14 +00:00
|
|
|
}
|
|
|
|
|
2012-03-04 12:32:20 +00:00
|
|
|
# optimization: build indexes of lines
|
2012-05-02 13:12:24 +00:00
|
|
|
my %by_facet_index = map { $lines[$_][I_FACET_INDEX] => $_ }
|
|
|
|
grep defined $lines[$_][I_FACET_INDEX],
|
2012-03-04 12:32:20 +00:00
|
|
|
(0..$#lines);
|
2012-05-02 13:12:24 +00:00
|
|
|
my %by_a_id = map { $lines[$_][I_A_ID] => $_ }
|
|
|
|
grep defined $lines[$_][I_A_ID],
|
2012-03-04 12:32:20 +00:00
|
|
|
(0..$#lines);
|
|
|
|
|
2012-06-06 20:28:23 +00:00
|
|
|
my (@polygons, @failed_loops, %visited_lines) = ();
|
2012-09-22 17:04:36 +00:00
|
|
|
my $slicing_errors = 0;
|
2012-02-18 19:36:14 +00:00
|
|
|
CYCLE: for (my $i = 0; $i <= $#lines; $i++) {
|
|
|
|
my $line = $lines[$i];
|
|
|
|
next if $visited_lines{$line};
|
|
|
|
my @points = ();
|
2012-05-01 08:53:52 +00:00
|
|
|
my $first_facet_index = $line->[I_FACET_INDEX];
|
2011-11-27 10:40:03 +00:00
|
|
|
|
2012-02-18 19:36:14 +00:00
|
|
|
do {
|
|
|
|
my $next_line;
|
2012-05-01 08:53:52 +00:00
|
|
|
if (defined $line->[I_NEXT_FACET_INDEX] && exists $by_facet_index{$line->[I_NEXT_FACET_INDEX]}) {
|
|
|
|
$next_line = $lines[$by_facet_index{$line->[I_NEXT_FACET_INDEX]}];
|
|
|
|
} elsif (defined $line->[I_B_ID] && exists $by_a_id{$line->[I_B_ID]}) {
|
|
|
|
$next_line = $lines[$by_a_id{$line->[I_B_ID]}];
|
2012-02-18 19:36:14 +00:00
|
|
|
} else {
|
|
|
|
Slic3r::debugf " line has no next_facet_index or b_id\n";
|
2012-09-22 17:04:36 +00:00
|
|
|
$slicing_errors = 1;
|
2012-06-06 20:28:23 +00:00
|
|
|
push @failed_loops, [@points] if @points;
|
2012-02-18 19:36:14 +00:00
|
|
|
next CYCLE;
|
2011-12-04 18:10:43 +00:00
|
|
|
}
|
|
|
|
|
2012-03-04 12:39:14 +00:00
|
|
|
if (!$next_line || $visited_lines{$next_line}) {
|
2012-02-18 19:36:14 +00:00
|
|
|
Slic3r::debugf " failed to close this loop\n";
|
2012-09-22 17:04:36 +00:00
|
|
|
$slicing_errors = 1;
|
2012-06-06 20:28:23 +00:00
|
|
|
push @failed_loops, [@points] if @points;
|
2012-02-18 19:36:14 +00:00
|
|
|
next CYCLE;
|
2012-05-01 08:53:52 +00:00
|
|
|
} elsif (defined $next_line->[I_PREV_FACET_INDEX] && $next_line->[I_PREV_FACET_INDEX] != $line->[I_FACET_INDEX]) {
|
2012-02-18 19:36:14 +00:00
|
|
|
Slic3r::debugf " wrong prev_facet_index\n";
|
2012-09-22 17:04:36 +00:00
|
|
|
$slicing_errors = 1;
|
2012-06-06 20:28:23 +00:00
|
|
|
push @failed_loops, [@points] if @points;
|
2012-02-18 19:36:14 +00:00
|
|
|
next CYCLE;
|
2012-05-01 13:12:34 +00:00
|
|
|
} elsif (defined $next_line->[I_A_ID] && $next_line->[I_A_ID] != $line->[I_B_ID]) {
|
2012-02-18 19:36:14 +00:00
|
|
|
Slic3r::debugf " wrong a_id\n";
|
2012-09-22 17:04:36 +00:00
|
|
|
$slicing_errors = 1;
|
2012-06-06 20:28:23 +00:00
|
|
|
push @failed_loops, [@points] if @points;
|
2012-02-18 19:36:14 +00:00
|
|
|
next CYCLE;
|
2011-12-04 18:10:43 +00:00
|
|
|
}
|
2012-02-18 19:36:14 +00:00
|
|
|
|
2012-05-01 08:53:52 +00:00
|
|
|
push @points, $next_line->[I_B];
|
2012-02-18 19:36:14 +00:00
|
|
|
$visited_lines{$next_line} = 1;
|
|
|
|
$line = $next_line;
|
2012-05-01 08:53:52 +00:00
|
|
|
} while ($first_facet_index != $line->[I_FACET_INDEX]);
|
2012-02-18 19:36:14 +00:00
|
|
|
|
|
|
|
push @polygons, Slic3r::Polygon->new(@points);
|
2012-04-02 09:40:12 +00:00
|
|
|
Slic3r::debugf " Discovered %s polygon of %d points\n",
|
|
|
|
($polygons[-1]->is_counter_clockwise ? 'ccw' : 'cw'), scalar(@points)
|
|
|
|
if $Slic3r::debug;
|
2011-11-27 10:40:03 +00:00
|
|
|
}
|
|
|
|
|
2012-06-06 20:28:23 +00:00
|
|
|
# TODO: we should try to combine failed loops
|
|
|
|
for (grep @$_ >= 3, @failed_loops) {
|
|
|
|
push @polygons, Slic3r::Polygon->new(@$_);
|
|
|
|
Slic3r::debugf " Discovered failed %s polygon of %d points\n",
|
|
|
|
($polygons[-1]->is_counter_clockwise ? 'ccw' : 'cw'), scalar(@$_)
|
|
|
|
if $Slic3r::debug;
|
|
|
|
}
|
|
|
|
|
2012-09-22 17:04:36 +00:00
|
|
|
return ($slicing_errors, [@polygons]);
|
2011-11-27 10:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub rotate {
|
|
|
|
my $self = shift;
|
|
|
|
my ($deg) = @_;
|
|
|
|
return if $deg == 0;
|
|
|
|
|
|
|
|
my $rad = Slic3r::Geometry::deg2rad($deg);
|
2012-02-17 12:49:33 +00:00
|
|
|
|
|
|
|
# transform vertex coordinates
|
|
|
|
foreach my $vertex (@{$self->vertices}) {
|
|
|
|
@$vertex = (@{ +(Slic3r::Geometry::rotate_points($rad, undef, [ $vertex->[X], $vertex->[Y] ]))[0] }, $vertex->[Z]);
|
2011-11-27 10:40:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub scale {
|
|
|
|
my $self = shift;
|
|
|
|
my ($factor) = @_;
|
|
|
|
return if $factor == 1;
|
|
|
|
|
2012-02-17 12:49:33 +00:00
|
|
|
# transform vertex coordinates
|
|
|
|
foreach my $vertex (@{$self->vertices}) {
|
|
|
|
$vertex->[$_] *= $factor for X,Y,Z;
|
2011-11-27 10:40:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub move {
|
|
|
|
my $self = shift;
|
|
|
|
my (@shift) = @_;
|
|
|
|
|
2012-02-17 12:49:33 +00:00
|
|
|
# transform vertex coordinates
|
|
|
|
foreach my $vertex (@{$self->vertices}) {
|
2012-04-30 15:10:54 +00:00
|
|
|
$vertex->[$_] += $shift[$_] || 0 for X,Y,Z;
|
2011-11-27 10:40:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-20 10:47:21 +00:00
|
|
|
sub align_to_origin {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# calculate the displacements needed to
|
|
|
|
# have lowest value for each axis at coordinate 0
|
2012-09-21 13:35:32 +00:00
|
|
|
my @extents = $self->extents;
|
2012-02-20 10:47:21 +00:00
|
|
|
$self->move(map -$extents[$_][MIN], X,Y,Z);
|
|
|
|
}
|
|
|
|
|
2011-11-27 10:40:03 +00:00
|
|
|
sub duplicate {
|
|
|
|
my $self = shift;
|
2011-12-12 16:32:45 +00:00
|
|
|
my (@shifts) = @_;
|
2011-11-27 10:40:03 +00:00
|
|
|
|
|
|
|
my @new_facets = ();
|
|
|
|
foreach my $facet (@{$self->facets}) {
|
|
|
|
# transform vertex coordinates
|
|
|
|
my ($normal, @vertices) = @$facet;
|
2011-12-12 16:32:45 +00:00
|
|
|
foreach my $shift (@shifts) {
|
|
|
|
push @new_facets, [ $normal ];
|
|
|
|
foreach my $vertex (@vertices) {
|
2012-02-17 12:49:33 +00:00
|
|
|
push @{$self->vertices}, [ map $self->vertices->[$vertex][$_] + ($shift->[$_] || 0), (X,Y,Z) ];
|
|
|
|
push @{$new_facets[-1]}, $#{$self->vertices};
|
2011-12-12 16:32:45 +00:00
|
|
|
}
|
2011-11-27 10:40:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
push @{$self->facets}, @new_facets;
|
2012-02-19 09:48:58 +00:00
|
|
|
$self->BUILD;
|
2011-11-27 10:40:03 +00:00
|
|
|
}
|
|
|
|
|
2012-09-21 13:35:32 +00:00
|
|
|
sub extents {
|
2011-11-27 10:40:03 +00:00
|
|
|
my $self = shift;
|
2012-09-22 14:10:24 +00:00
|
|
|
return Slic3r::Geometry::bounding_box_3D($self->vertices);
|
2011-11-27 10:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub size {
|
|
|
|
my $self = shift;
|
2012-09-22 14:12:54 +00:00
|
|
|
return Slic3r::Geometry::size_3D($self->vertices);
|
2011-11-27 10:40:03 +00:00
|
|
|
}
|
|
|
|
|
2012-02-17 12:49:33 +00:00
|
|
|
sub slice_facet {
|
2011-11-27 10:40:03 +00:00
|
|
|
my $self = shift;
|
2012-04-29 10:51:20 +00:00
|
|
|
my ($print_object, $facet_id) = @_;
|
2012-05-20 09:05:16 +00:00
|
|
|
my @vertices = @{$self->facets->[$facet_id]}[-3..-1];
|
2011-11-27 10:40:03 +00:00
|
|
|
Slic3r::debugf "\n==> FACET %d (%f,%f,%f - %f,%f,%f - %f,%f,%f):\n",
|
2012-02-18 19:36:14 +00:00
|
|
|
$facet_id, map @{$self->vertices->[$_]}, @vertices
|
2011-11-27 10:40:03 +00:00
|
|
|
if $Slic3r::debug;
|
|
|
|
|
|
|
|
# find the vertical extents of the facet
|
|
|
|
my ($min_z, $max_z) = (99999999999, -99999999999);
|
2012-02-18 19:36:14 +00:00
|
|
|
foreach my $vertex (@vertices) {
|
|
|
|
my $vertex_z = $self->vertices->[$vertex][Z];
|
|
|
|
$min_z = $vertex_z if $vertex_z < $min_z;
|
|
|
|
$max_z = $vertex_z if $vertex_z > $max_z;
|
2011-11-27 10:40:03 +00:00
|
|
|
}
|
|
|
|
Slic3r::debugf "z: min = %.0f, max = %.0f\n", $min_z, $max_z;
|
|
|
|
|
2012-03-11 15:02:17 +00:00
|
|
|
if ($max_z == $min_z) {
|
2011-11-27 10:40:03 +00:00
|
|
|
Slic3r::debugf "Facet is horizontal; ignoring\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
# calculate the layer extents
|
2013-01-01 22:28:48 +00:00
|
|
|
my ($min_layer, $max_layer) = $print_object->get_layer_range($min_z, $max_z);
|
2011-11-27 10:40:03 +00:00
|
|
|
Slic3r::debugf "layers: min = %s, max = %s\n", $min_layer, $max_layer;
|
|
|
|
|
2012-04-09 10:29:47 +00:00
|
|
|
my $lines = {}; # layer_id => [ lines ]
|
2011-11-27 10:40:03 +00:00
|
|
|
for (my $layer_id = $min_layer; $layer_id <= $max_layer; $layer_id++) {
|
2013-01-01 22:28:48 +00:00
|
|
|
my $layer = $print_object->layers->[$layer_id];
|
2012-04-09 10:29:47 +00:00
|
|
|
$lines->{$layer_id} ||= [];
|
|
|
|
push @{ $lines->{$layer_id} }, $self->intersect_facet($facet_id, $layer->slice_z);
|
2011-11-27 10:40:03 +00:00
|
|
|
}
|
2012-04-09 10:29:47 +00:00
|
|
|
return $lines;
|
2011-11-27 10:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub intersect_facet {
|
|
|
|
my $self = shift;
|
2012-02-18 19:36:14 +00:00
|
|
|
my ($facet_id, $z) = @_;
|
2011-11-27 10:40:03 +00:00
|
|
|
|
2012-05-20 09:05:16 +00:00
|
|
|
my @vertices_ids = @{$self->facets->[$facet_id]}[-3..-1];
|
2012-02-18 19:36:14 +00:00
|
|
|
my @edge_ids = @{$self->facets_edges->[$facet_id]};
|
|
|
|
my @edge_vertices_ids = $self->_facet_edges($facet_id);
|
2011-11-27 10:40:03 +00:00
|
|
|
|
|
|
|
my (@lines, @points, @intersection_points, @points_on_layer) = ();
|
|
|
|
|
2012-02-18 19:36:14 +00:00
|
|
|
for my $e (0..2) {
|
|
|
|
my $edge_id = $edge_ids[$e];
|
|
|
|
my ($a_id, $b_id) = @{$edge_vertices_ids[$e]};
|
|
|
|
my ($a, $b) = map $self->vertices->[$_], ($a_id, $b_id);
|
2011-11-27 10:40:03 +00:00
|
|
|
#printf "Az = %f, Bz = %f, z = %f\n", $a->[Z], $b->[Z], $z;
|
|
|
|
|
2012-02-18 19:36:14 +00:00
|
|
|
if ($a->[Z] == $b->[Z] && $a->[Z] == $z) {
|
2011-11-27 10:40:03 +00:00
|
|
|
# edge is horizontal and belongs to the current layer
|
2012-05-01 08:53:52 +00:00
|
|
|
my $edge_type = (grep $self->vertices->[$_][Z] < $z, @vertices_ids) ? FE_TOP : FE_BOTTOM;
|
2012-05-02 13:12:24 +00:00
|
|
|
if ($edge_type == FE_TOP) {
|
2012-02-18 19:36:14 +00:00
|
|
|
($a, $b) = ($b, $a);
|
|
|
|
($a_id, $b_id) = ($b_id, $a_id);
|
|
|
|
}
|
2012-05-28 18:29:51 +00:00
|
|
|
push @lines, pack I_FMT, (
|
2012-05-28 17:58:27 +00:00
|
|
|
$b->[X], $b->[Y], # I_B
|
2012-05-01 08:53:52 +00:00
|
|
|
$a_id, # I_A_ID
|
|
|
|
$b_id, # I_B_ID
|
|
|
|
$facet_id, # I_FACET_INDEX
|
2012-05-29 15:12:00 +00:00
|
|
|
-1, # I_PREV_FACET_INDEX
|
|
|
|
-1, # I_NEXT_FACET_INDEX
|
2012-05-01 08:53:52 +00:00
|
|
|
$edge_type, # I_FACET_EDGE
|
|
|
|
|
|
|
|
# Unused data:
|
|
|
|
# a => [$a->[X], $a->[Y]],
|
2012-05-28 17:58:27 +00:00
|
|
|
);
|
2011-11-27 10:40:03 +00:00
|
|
|
#print "Horizontal edge at $z!\n";
|
|
|
|
|
2012-02-18 19:36:14 +00:00
|
|
|
} elsif ($a->[Z] == $z) {
|
2011-11-27 10:40:03 +00:00
|
|
|
#print "A point on plane $z!\n";
|
2012-02-18 19:36:14 +00:00
|
|
|
push @points, [ $a->[X], $a->[Y], $a_id ];
|
2011-11-27 10:40:03 +00:00
|
|
|
push @points_on_layer, $#points;
|
|
|
|
|
2012-02-18 19:36:14 +00:00
|
|
|
} elsif ($b->[Z] == $z) {
|
2011-11-27 10:40:03 +00:00
|
|
|
#print "B point on plane $z!\n";
|
2012-02-18 19:36:14 +00:00
|
|
|
push @points, [ $b->[X], $b->[Y], $b_id ];
|
2011-11-27 10:40:03 +00:00
|
|
|
push @points_on_layer, $#points;
|
|
|
|
|
2012-02-18 19:36:14 +00:00
|
|
|
} elsif (($a->[Z] < $z && $b->[Z] > $z) || ($b->[Z] < $z && $a->[Z] > $z)) {
|
2011-11-27 10:40:03 +00:00
|
|
|
# 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]),
|
2012-02-18 19:36:14 +00:00
|
|
|
undef,
|
2011-11-27 10:40:03 +00:00
|
|
|
$edge_id,
|
|
|
|
];
|
|
|
|
push @intersection_points, $#points;
|
|
|
|
#print "Intersects at $z!\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return @lines if @lines;
|
|
|
|
if (@points_on_layer == 2 && @intersection_points == 1) {
|
|
|
|
$points[ $points_on_layer[1] ] = undef;
|
|
|
|
@points = grep $_, @points;
|
|
|
|
}
|
|
|
|
if (@points_on_layer == 2 && @intersection_points == 0) {
|
|
|
|
if (same_point(map $points[$_], @points_on_layer)) {
|
|
|
|
return ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (@points) {
|
|
|
|
|
|
|
|
# defensive programming:
|
|
|
|
die "Facets must intersect each plane 0 or 2 times" if @points != 2;
|
|
|
|
|
|
|
|
# connect points:
|
2012-02-18 19:36:14 +00:00
|
|
|
my ($prev_facet_index, $next_facet_index) = (undef, undef);
|
|
|
|
$prev_facet_index = +(grep $_ != $facet_id, @{$self->edges_facets->[$points[B][3]]})[0]
|
|
|
|
if defined $points[B][3];
|
|
|
|
$next_facet_index = +(grep $_ != $facet_id, @{$self->edges_facets->[$points[A][3]]})[0]
|
|
|
|
if defined $points[A][3];
|
|
|
|
|
2012-05-28 18:29:51 +00:00
|
|
|
return pack I_FMT, (
|
2012-05-28 17:58:27 +00:00
|
|
|
$points[A][X], $points[A][Y], # I_B
|
2012-06-06 16:57:46 +00:00
|
|
|
$points[B][2] // -1, # I_A_ID /
|
|
|
|
$points[A][2] // -1, # I_B_ID /
|
2012-05-28 17:58:27 +00:00
|
|
|
$facet_id, # I_FACET_INDEX
|
2012-05-29 15:12:00 +00:00
|
|
|
$prev_facet_index // -1, # I_PREV_FACET_INDEX /
|
|
|
|
$next_facet_index // -1, # I_NEXT_FACET_INDEX /
|
2012-05-28 17:58:27 +00:00
|
|
|
-1, # I_FACET_EDGE
|
|
|
|
);
|
2011-11-27 10:40:03 +00:00
|
|
|
#printf " intersection points at z = %f: %f,%f - %f,%f\n", $z, map @$_, @intersection_points;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ();
|
|
|
|
}
|
|
|
|
|
2012-01-28 14:05:42 +00:00
|
|
|
sub get_connected_facets {
|
|
|
|
my $self = shift;
|
|
|
|
my ($facet_id) = @_;
|
|
|
|
|
2012-02-18 19:36:14 +00:00
|
|
|
my %facets = ();
|
|
|
|
foreach my $edge_id (@{$self->facets_edges->[$facet_id]}) {
|
|
|
|
$facets{$_} = 1 for @{$self->edges_facets->[$edge_id]};
|
2012-01-28 14:05:42 +00:00
|
|
|
}
|
2012-02-18 19:36:14 +00:00
|
|
|
delete $facets{$facet_id};
|
|
|
|
return keys %facets;
|
2012-01-28 14:05:42 +00:00
|
|
|
}
|
|
|
|
|
2012-04-30 22:30:46 +00:00
|
|
|
sub split_mesh {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my @meshes = ();
|
|
|
|
|
|
|
|
# loop while we have remaining facets
|
|
|
|
while (1) {
|
|
|
|
# get the first facet
|
|
|
|
my @facet_queue = ();
|
|
|
|
my @facets = ();
|
|
|
|
for (my $i = 0; $i <= $#{$self->facets}; $i++) {
|
|
|
|
if (defined $self->facets->[$i]) {
|
|
|
|
push @facet_queue, $i;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last if !@facet_queue;
|
|
|
|
|
|
|
|
while (defined (my $facet_id = shift @facet_queue)) {
|
|
|
|
next unless defined $self->facets->[$facet_id];
|
|
|
|
push @facets, map [ @$_ ], $self->facets->[$facet_id];
|
|
|
|
push @facet_queue, $self->get_connected_facets($facet_id);
|
|
|
|
$self->facets->[$facet_id] = undef;
|
|
|
|
}
|
|
|
|
|
2012-05-20 09:05:16 +00:00
|
|
|
my %vertices = map { $_ => 1 } map @$_[-3..-1], @facets;
|
2012-04-30 22:30:46 +00:00
|
|
|
my @new_vertices = keys %vertices;
|
|
|
|
my %new_vertices = map { $new_vertices[$_] => $_ } 0..$#new_vertices;
|
|
|
|
foreach my $facet (@facets) {
|
2012-05-20 09:05:16 +00:00
|
|
|
$facet->[$_] = $new_vertices{$facet->[$_]} for -3..-1;
|
2012-04-30 22:30:46 +00:00
|
|
|
}
|
|
|
|
push @meshes, Slic3r::TriangleMesh->new(
|
|
|
|
facets => \@facets,
|
|
|
|
vertices => [ map $self->vertices->[$_], keys %vertices ],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return @meshes;
|
|
|
|
}
|
|
|
|
|
2012-11-23 23:13:04 +00:00
|
|
|
sub horizontal_projection {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my @f = ();
|
|
|
|
foreach my $facet (@{$self->facets}) {
|
2012-11-29 18:13:52 +00:00
|
|
|
push @f, Slic3r::Polygon->new([ map [ @{$self->vertices->[$_]}[X,Y] ], @$facet ]);
|
2012-11-23 23:13:04 +00:00
|
|
|
}
|
|
|
|
|
2012-11-29 18:13:52 +00:00
|
|
|
$_->make_counter_clockwise for @f;
|
2012-11-23 23:13:04 +00:00
|
|
|
my $scale_vector = Math::Clipper::integerize_coordinate_sets({ bits => 32 }, @f);
|
2012-11-29 18:13:52 +00:00
|
|
|
my $union = union_ex([ Slic3r::Geometry::Clipper::offset(\@f, 10000) ]);
|
2012-11-23 23:13:04 +00:00
|
|
|
Math::Clipper::unscale_coordinate_sets($scale_vector, $_) for @$union;
|
|
|
|
return $union;
|
|
|
|
}
|
|
|
|
|
2011-11-27 10:40:03 +00:00
|
|
|
1;
|