2012-02-25 16:35:25 +00:00
|
|
|
package Slic3r::Format::STL;
|
2011-09-06 09:50:43 +00:00
|
|
|
use Moo;
|
2011-09-01 19:06:28 +00:00
|
|
|
|
2012-02-19 14:45:27 +00:00
|
|
|
use Slic3r::Geometry qw(X Y Z triangle_normal);
|
2011-09-01 19:06:28 +00:00
|
|
|
|
2011-10-12 08:47:26 +00:00
|
|
|
sub read_file {
|
|
|
|
my $self = shift;
|
|
|
|
my ($file) = @_;
|
|
|
|
|
|
|
|
open my $fh, '<', $file or die "Failed to open $file\n";
|
|
|
|
|
|
|
|
# let's detect whether file is ASCII or binary
|
|
|
|
my $mode;
|
|
|
|
{
|
|
|
|
my $size = +(stat $fh)[7];
|
|
|
|
$mode = 'ascii' if $size < 80 + 4;
|
|
|
|
|
|
|
|
# skip binary header
|
|
|
|
seek $fh, 80, 0;
|
|
|
|
read $fh, my $buf, 4;
|
|
|
|
my $triangle_count = unpack 'L', $buf;
|
2011-12-18 09:50:55 +00:00
|
|
|
die "STL file seems invalid, could not read facet count\n" if !defined $triangle_count;
|
2011-10-12 08:47:26 +00:00
|
|
|
my $expected_size =
|
|
|
|
+ 80 # header
|
|
|
|
+ 4 # count
|
|
|
|
+ $triangle_count * (
|
|
|
|
+ 4 # normal, pt,pt,pt (vectors)
|
|
|
|
* 4 # bytes per value
|
|
|
|
* 3 # values per vector
|
|
|
|
+ 2 # the trailing 'short'
|
|
|
|
);
|
|
|
|
$mode = ($size == $expected_size) ? 'binary' : 'ascii';
|
|
|
|
}
|
|
|
|
|
2012-02-17 12:49:33 +00:00
|
|
|
my $facets = [];
|
2011-10-12 08:47:26 +00:00
|
|
|
$mode eq 'ascii'
|
|
|
|
? _read_ascii($fh, $facets)
|
|
|
|
: _read_binary($fh, $facets);
|
|
|
|
close $fh;
|
2012-02-17 12:49:33 +00:00
|
|
|
|
|
|
|
my $vertices = [];
|
|
|
|
{
|
2012-02-18 19:36:14 +00:00
|
|
|
my %vertices_map = (); # given a vertex's coordinates, what's its index?
|
|
|
|
my @vertices_facets = (); # given a vertex index, what are the indexes of its tangent facets?
|
2012-02-17 13:58:13 +00:00
|
|
|
for (my $f = 0; $f <= $#$facets; $f++) {
|
2012-05-20 09:05:16 +00:00
|
|
|
for (-3..-1) {
|
2012-02-17 13:58:13 +00:00
|
|
|
my $point_id = join ',', @{$facets->[$f][$_]};
|
|
|
|
if (exists $vertices_map{$point_id}) {
|
|
|
|
$facets->[$f][$_] = $vertices_map{$point_id};
|
2012-02-18 19:36:14 +00:00
|
|
|
push @{$vertices_facets[$facets->[$f][$_]]}, $f;
|
2012-02-17 12:49:33 +00:00
|
|
|
} else {
|
2012-02-17 13:58:13 +00:00
|
|
|
push @$vertices, $facets->[$f][$_];
|
|
|
|
$facets->[$f][$_] = $vertices_map{$point_id} = $#$vertices;
|
2012-02-18 19:36:14 +00:00
|
|
|
$vertices_facets[$#$vertices] = [$f];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# The following loop checks that @vertices_facets only groups facets that
|
|
|
|
# are really connected together (i.e. neighbors or sharing neighbors);
|
|
|
|
# in other words it takes care of multiple vertices occupying the same
|
|
|
|
# point in space. It enforces topological correctness which is needed by
|
|
|
|
# the slicing algorithm.
|
|
|
|
# I'm keeping it disabled until I find a good test case.
|
|
|
|
if (0) {
|
|
|
|
my $vertices_count = $#$vertices; # store it to avoid processing newly created vertices
|
|
|
|
for (my $v = 0; $v <= $vertices_count; $v++) {
|
|
|
|
my $more_than_one_vertex_in_this_point = 0;
|
|
|
|
while (@{$vertices_facets[$v]}) {
|
|
|
|
my @facets_indexes = @{$vertices_facets[$v]};
|
|
|
|
@{$vertices_facets[$v]} = ();
|
|
|
|
|
|
|
|
my @this_f = shift @facets_indexes;
|
|
|
|
CYCLE: while (@facets_indexes && @this_f) {
|
|
|
|
|
|
|
|
# look for a facet that is connected to $this_f[-1] and whose common line contains $v
|
2012-05-20 09:05:16 +00:00
|
|
|
my @other_vertices_indexes = grep $_ != $v, @{$facets->[$this_f[-1]]}[-3..-1];
|
2012-02-18 19:36:14 +00:00
|
|
|
|
|
|
|
OTHER: for my $other_f (@facets_indexes) {
|
|
|
|
# facet is connected if it shares one more point
|
2012-05-20 09:05:16 +00:00
|
|
|
for (grep $_ != $v, @{$facets->[$other_f]}[-3..-1]) {
|
2012-02-18 19:36:14 +00:00
|
|
|
if ($_ ~~ @other_vertices_indexes) {
|
|
|
|
#printf "facet %d is connected to $other_f (sharing vertices $v and $_)\n", $this_f[-1];
|
|
|
|
|
|
|
|
# TODO: we should ensure that the common edge has a different orientation
|
|
|
|
# for each of the two adjacent facets
|
|
|
|
|
|
|
|
push @this_f, $other_f;
|
|
|
|
@facets_indexes = grep $_ != $other_f, @facets_indexes;
|
|
|
|
next CYCLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# if we're here, then we couldn't find any facet connected to $this_f[-1]
|
|
|
|
# so we should move this one to a different cluster (that is, a new vertex)
|
|
|
|
# (or ignore it if it turns to be a non-manifold facet)
|
|
|
|
if (@this_f > 1) {
|
|
|
|
push @{$vertices_facets[$v]}, $this_f[-1];
|
|
|
|
pop @this_f;
|
|
|
|
$more_than_one_vertex_in_this_point++;
|
|
|
|
} else {
|
|
|
|
last CYCLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($more_than_one_vertex_in_this_point) {
|
|
|
|
Slic3r::debugf " more than one vertex in the same point\n";
|
|
|
|
push @$vertices, $vertices->[$v];
|
|
|
|
for my $f (@this_f) {
|
2012-05-20 09:05:16 +00:00
|
|
|
$facets->[$f][$_] = $#$vertices for grep $facets->[$f][$_] == $v, -3..-1;
|
2012-02-18 19:36:14 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-17 12:49:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Slic3r::TriangleMesh->new(vertices => $vertices, facets => $facets);
|
2011-10-12 08:47:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub _read_ascii {
|
|
|
|
my ($fh, $facets) = @_;
|
|
|
|
|
2012-04-30 19:49:44 +00:00
|
|
|
my $point_re = qr/([^ ]+)\s+([^ ]+)\s+([^ ]+)/;
|
2011-10-12 08:47:26 +00:00
|
|
|
|
|
|
|
my $facet;
|
|
|
|
seek $fh, 0, 0;
|
2012-02-20 15:43:45 +00:00
|
|
|
while (my $_ = <$fh>) {
|
2011-10-12 08:47:26 +00:00
|
|
|
if (!$facet) {
|
|
|
|
/^\s*facet\s+normal\s+$point_re/ or next;
|
2012-05-20 09:05:16 +00:00
|
|
|
$facet = []; # ignore normal: [$1, $2, $3]
|
2011-10-12 08:47:26 +00:00
|
|
|
} else {
|
|
|
|
if (/^\s*endfacet/) {
|
|
|
|
push @$facets, $facet;
|
|
|
|
undef $facet;
|
|
|
|
} else {
|
2012-04-30 19:49:44 +00:00
|
|
|
/^\s*vertex\s+$point_re/o or next;
|
2012-02-18 19:36:14 +00:00
|
|
|
push @$facet, [map $_ * 1, $1, $2, $3];
|
2011-10-12 08:47:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-10-12 12:54:49 +00:00
|
|
|
if ($facet) {
|
|
|
|
die "STL file seems invalid\n";
|
|
|
|
}
|
2011-10-12 08:47:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub _read_binary {
|
|
|
|
my ($fh, $facets) = @_;
|
|
|
|
|
|
|
|
die "bigfloat" unless length(pack "f", 1) == 4;
|
|
|
|
|
2011-11-12 10:57:22 +00:00
|
|
|
binmode $fh;
|
2011-10-12 08:47:26 +00:00
|
|
|
seek $fh, 80 + 4, 0;
|
2012-02-20 15:43:45 +00:00
|
|
|
while (read $fh, my $_, 4*4*3+2) {
|
2011-11-12 10:57:22 +00:00
|
|
|
my @v = unpack '(f<3)4';
|
2012-05-20 09:05:16 +00:00
|
|
|
push @$facets, [ [@v[3..5]], [@v[6..8]], [@v[9..11]] ]; # ignore normal: [@v[0..2]]
|
2011-10-12 08:47:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-28 14:05:42 +00:00
|
|
|
sub write_file {
|
|
|
|
my $self = shift;
|
|
|
|
my ($file, $mesh, $binary) = @_;
|
|
|
|
|
|
|
|
open my $fh, '>', $file;
|
|
|
|
|
|
|
|
$binary
|
2012-02-18 19:36:14 +00:00
|
|
|
? _write_binary($fh, $mesh)
|
|
|
|
: _write_ascii($fh, $mesh);
|
2012-01-28 14:05:42 +00:00
|
|
|
|
|
|
|
close $fh;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _write_binary {
|
2012-02-18 19:36:14 +00:00
|
|
|
my ($fh, $mesh) = @_;
|
2012-01-28 14:05:42 +00:00
|
|
|
|
|
|
|
die "bigfloat" unless length(pack "f", 1) == 4;
|
|
|
|
|
|
|
|
binmode $fh;
|
|
|
|
print $fh pack 'x80';
|
2012-02-18 19:36:14 +00:00
|
|
|
print $fh pack 'L', scalar(@{$mesh->facets});
|
|
|
|
foreach my $facet (@{$mesh->facets}) {
|
2012-02-19 14:45:27 +00:00
|
|
|
print $fh pack '(f<3)4S',
|
|
|
|
@{_facet_normal($mesh, $facet)},
|
|
|
|
(map @{$mesh->vertices->[$_]}, @$facet[1,2,3]),
|
|
|
|
0;
|
2012-02-18 19:36:14 +00:00
|
|
|
}
|
2012-01-28 14:05:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub _write_ascii {
|
2012-02-18 19:36:14 +00:00
|
|
|
my ($fh, $mesh) = @_;
|
2012-01-28 14:05:42 +00:00
|
|
|
|
|
|
|
printf $fh "solid\n";
|
2012-02-18 19:36:14 +00:00
|
|
|
foreach my $facet (@{$mesh->facets}) {
|
2012-02-19 14:45:27 +00:00
|
|
|
printf $fh " facet normal %f %f %f\n", @{_facet_normal($mesh, $facet)};
|
2012-01-28 14:05:42 +00:00
|
|
|
printf $fh " outer loop\n";
|
2012-02-18 19:36:14 +00:00
|
|
|
printf $fh " vertex %f %f %f\n", @{$mesh->vertices->[$_]} for @$facet[1,2,3];
|
2012-01-28 14:05:42 +00:00
|
|
|
printf $fh " endloop\n";
|
|
|
|
printf $fh " endfacet\n";
|
|
|
|
}
|
|
|
|
printf $fh "endsolid\n";
|
|
|
|
}
|
|
|
|
|
2012-02-19 14:45:27 +00:00
|
|
|
sub _facet_normal {
|
|
|
|
my ($mesh, $facet) = @_;
|
|
|
|
return triangle_normal(map $mesh->vertices->[$_], @$facet[1,2,3]);
|
|
|
|
}
|
|
|
|
|
2011-09-01 19:06:28 +00:00
|
|
|
1;
|