2011-11-27 10:40:03 +00:00
|
|
|
|
package Slic3r::TriangleMesh;
|
|
|
|
|
use Moo;
|
|
|
|
|
|
2013-06-23 22:02:01 +00:00
|
|
|
|
use List::Util qw(reduce min max first);
|
2012-03-11 15:02:17 +00:00
|
|
|
|
use Slic3r::Geometry qw(X Y Z A B unscale same_point);
|
2013-08-05 18:21:08 +00:00
|
|
|
|
use Slic3r::Geometry::Clipper qw(union_ex offset);
|
2013-05-13 20:24:49 +00:00
|
|
|
|
use Storable;
|
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
|
2013-05-18 14:48:26 +00:00
|
|
|
|
has 'edges' => (is => 'rw'); # id => [ $v1_id, $v2_id ]
|
|
|
|
|
has 'facets_edges' => (is => 'rw'); # id => [ $e1_id, $e2_id, $e3_id ]
|
|
|
|
|
has 'edges_facets' => (is => 'rw'); # id => [ $f1_id, $f2_id, (...) ]
|
2011-11-27 10:40:03 +00:00
|
|
|
|
|
|
|
|
|
use constant MIN => 0;
|
|
|
|
|
use constant MAX => 1;
|
|
|
|
|
|
2013-05-14 12:47:00 +00:00
|
|
|
|
sub analyze {
|
2011-11-27 10:40:03 +00:00
|
|
|
|
my $self = shift;
|
|
|
|
|
|
2013-05-18 14:48:26 +00:00
|
|
|
|
return if defined $self->edges;
|
|
|
|
|
$self->edges([]);
|
|
|
|
|
$self->facets_edges([]);
|
|
|
|
|
$self->edges_facets([]);
|
2012-02-18 19:36:14 +00:00
|
|
|
|
my %table = (); # edge_coordinates => edge_id
|
2013-06-22 17:59:27 +00:00
|
|
|
|
my $vertices = $self->vertices; # save method calls
|
2012-02-18 19:36:14 +00:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2013-06-22 17:59:27 +00:00
|
|
|
|
my $lowest_vertex_idx = reduce {
|
|
|
|
|
$vertices->[ $facet->[$a] ][Z] < $vertices->[ $facet->[$b] ][Z] ? $a : $b
|
|
|
|
|
} -3 .. -1;
|
|
|
|
|
@$facet[-3..-1] = (@$facet[$lowest_vertex_idx..-1], @$facet[-3..($lowest_vertex_idx-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 {
|
2013-05-13 20:24:49 +00:00
|
|
|
|
Storable::dclone($_[0])
|
2012-04-30 15:10:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-18 19:36:14 +00:00
|
|
|
|
sub check_manifoldness {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
|
2013-05-14 12:47:00 +00:00
|
|
|
|
$self->analyze;
|
|
|
|
|
|
2013-06-23 22:02:01 +00:00
|
|
|
|
# look for any edges belonging to an odd number of facets
|
|
|
|
|
# we should actually check that each pair of facets belonging to this edge
|
|
|
|
|
# has compatible winding order
|
2012-05-31 17:15:40 +00:00
|
|
|
|
my ($first_bad_edge_id) =
|
2013-06-23 22:02:01 +00:00
|
|
|
|
grep { @{ $self->edges_facets->[$_] } % 2 } 0..$#{$self->edges_facets};
|
2012-05-31 17:15:40 +00:00
|
|
|
|
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
|
|
|
|
}
|
2013-05-14 12:47:00 +00:00
|
|
|
|
|
|
|
|
|
# empty the edges array as we don't really need it anymore
|
|
|
|
|
@{$self->edges} = ();
|
|
|
|
|
|
2012-10-24 20:44:08 +00:00
|
|
|
|
return 1;
|
2011-11-27 10:40:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub rotate {
|
|
|
|
|
my $self = shift;
|
2013-05-31 17:41:31 +00:00
|
|
|
|
my ($deg, $center) = @_;
|
2011-11-27 10:40:03 +00:00
|
|
|
|
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}) {
|
2013-05-31 17:41:31 +00:00
|
|
|
|
@$vertex = (@{ +(Slic3r::Geometry::rotate_points($rad, $center, [ $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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-26 18:17:33 +00:00
|
|
|
|
sub scale_xyz {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
my ($versor) = @_;
|
|
|
|
|
|
|
|
|
|
# transform vertex coordinates
|
|
|
|
|
foreach my $vertex (@{$self->vertices}) {
|
|
|
|
|
$vertex->[$_] *= $versor->[$_] 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
|
2013-06-16 10:21:25 +00:00
|
|
|
|
my $bb = $self->bounding_box;
|
|
|
|
|
$self->move(map -$bb->extents->[$_][MIN], X,Y,Z);
|
2012-02-20 10:47:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-17 12:14:33 +00:00
|
|
|
|
sub center_around_origin {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
|
|
$self->move(map -$_, @{ $self->center });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub center {
|
|
|
|
|
my $self = shift;
|
2013-06-16 10:21:25 +00:00
|
|
|
|
return $self->bounding_box->center;
|
2013-05-17 12:14:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2013-06-02 14:56:08 +00:00
|
|
|
|
sub used_vertices {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
return [ map $self->vertices->[$_], map @$_, @{$self->facets} ];
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-07 21:16:02 +00:00
|
|
|
|
sub bounding_box {
|
|
|
|
|
my $self = shift;
|
2013-06-16 10:21:25 +00:00
|
|
|
|
return Slic3r::Geometry::BoundingBox->new_from_points_3D($self->used_vertices);
|
2013-06-07 21:16:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-27 10:40:03 +00:00
|
|
|
|
sub size {
|
|
|
|
|
my $self = shift;
|
2013-06-16 10:21:25 +00:00
|
|
|
|
return $self->bounding_box->size;
|
2011-11-27 10:40:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-05 18:21:08 +00:00
|
|
|
|
# this will return *scaled* expolygons, so it is expected to be run
|
|
|
|
|
# on unscaled meshes
|
2012-11-23 23:13:04 +00:00
|
|
|
|
sub horizontal_projection {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
|
|
my @f = ();
|
|
|
|
|
foreach my $facet (@{$self->facets}) {
|
2013-08-05 18:21:08 +00:00
|
|
|
|
push @f, Slic3r::Polygon->new(
|
|
|
|
|
map [ map $_ / &Slic3r::SCALING_FACTOR, @{$self->vertices->[$_]}[X,Y] ], @$facet
|
|
|
|
|
);
|
2012-11-23 23:13:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-17 14:39:51 +00:00
|
|
|
|
$_->make_counter_clockwise for @f; # do this after scaling, as winding order might change while doing that
|
2013-08-09 14:34:55 +00:00
|
|
|
|
|
|
|
|
|
# the offset factor was tuned using groovemount.stl
|
2013-08-26 15:58:37 +00:00
|
|
|
|
return union_ex(offset(\@f, Slic3r::Geometry::scale 0.01), 1);
|
2012-11-23 23:13:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-27 10:40:03 +00:00
|
|
|
|
1;
|