blib/lib/PDL/IO/STL.pm

filter: Statementstmt 82.5% 132 / 160 Branchbran 57.6% 45 / 78 Conditioncond 50.0% 8 / 16 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 89.4% 17 / 19 Podpod 50.0% 2 / 4 totaltotal 71.7% 206 / 287 CC 49 SCAR 24.8
CC
49
Cov
73%
_read_ascii
24.1
_write_ascii
20.0
wstl
10.9
CRAP 11.9
« blib/lib/PDL/Demos.pm ↑ Summary lib/PDL/MatrixOps.pm »
1 package PDL::IO::STL;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.001';
7 1 our @EXPORT_OK = qw( rstl wstl );
8 1 our %EXPORT_TAGS = (Func=>[@EXPORT_OK]);
9 1 our @ISA = ('PDL::Exporter');
10  
11 1 ▶use PDL::LiteF;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use PDL::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use PDL::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use PDL::IO::Misc; # for little/big-endian
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 PDL::IO::STL - read/write 3D stereolithography files
19  
20 =head1 SYNOPSIS
21  
22 use PDL;
23 use PDL::IO::STL;
24  
25 ($vertices, $faceidx, $colours) = rstl('owl.stl'); # read an STL file
26 wstl('file.stl', $vertices, $faceidx, $colours); # write an STL file
27  
28 =head1 DESCRIPTION
29  
30 Normal-vector information is currently ignored.
31 The "attribute byte count", used sometimes to store colour information,
32 is currently ignored.
33  
34 This module is based on L<CAD::Format::STL>, but with C<binmode> on
35 opened filehandles and little-endian (i.e. network) order forced on the
36 binary format.
37  
38 =head1 FUNCTIONS
39  
40 =head2 rstl
41  
42 =for ref
43  
44 Read an STL file (ASCII or binary), returning vertices and face-indices.
45  
46 =for example
47  
48 ($vertices, $faceidx, $colours) = rstl('owl.stl'); # read an STL file
49  
50 =cut
51  
52 4 ▶sub rstl { PDL->rstl(@_); }
Subroutine rstl: called CC=1 SCAR=0.0
Pod documented
53 4 ▶sub PDL::rstl {
Subroutine rstl: called CC=9 SCAR=22.7
Pod undocumented
54 4 my $class = shift;
55 4 ▶ barf 'Usage: $x = rstl($file) -or- $x = PDL->rstl($file)' if @_ < 1 || @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 1 or @_ > 2 0 4 4
Condition33%1/3
@_ < 1 or @_ > 2
l!l&&r!l&&!r
004
MC/DC0%0/2
@_ < 1 or @_ > 2
@_ < 1 @_ > 2
Truth table33%1/3
A or B
A= @_ < 1B= @_ > 2
ABresult
1X1
011
000
56 4 my $file = shift;
57 # allow filehandle
58 4 ▶ unless((ref($file) || '') eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
unless ((ref $file || "") eq "GLOB") 3 1 4
Condition100%2/2
ref $file || ''
l!l
13
MC/DC100%1/1
ref $file || ''
ref $file
Truth table100%2/2
A
A= ref $file
Aresult
11
00
59 3 ▶ open(my $fh, '<', $file) or
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or barf("cannot open '${file}' for reading $!") 3 0 3
60 barf "cannot open '$file' for reading $!";
61 3 binmode $fh;
62 3 $file = $fh;
63 }
64 4 ▶ barf('must have seekable filehandle') if !seek($file, 0,0);
Branch: 1 branch
BranchTrueFalseTotal
unless seek $file, 0, 0 0 4 4
65 4 my $mode = _detect($file);
66 4 ▶ seek($file, 0, 0) or barf "cannot reset filehandle";
Branch: 1 branch
BranchTrueFalseTotal
seek $file, 0, 0 or barf("cannot reset filehandle") 4 0 4
67 4 ▶ my $func = $mode eq 'ascii' ? \&_read_ascii : \&_read_binary;
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'ascii' ? : 3 1 4
68 4 $func->($file);
69 }
70  
71 4 ▶sub _detect {
Subroutine _detect: called CC=8 SCAR=21.4
72 4 my $fh = shift;
73 4 my $location = tell $fh;
74 4 ▶ my $buf; read($fh, $buf, 5) or barf $@;
Branch: 1 branch
BranchTrueFalseTotal
read $fh, $buf, 5 or barf($@) 4 0 4
75 4 ▶ seek($fh, $location, 0), return 'ascii' if $buf eq 'solid';
Branch: 1 branch
BranchTrueFalseTotal
if $buf eq "solid" 3 1 4
76 1 seek($fh, $location + 80, 0);
77 1 my $count = eval {
78 1 ▶ my $buf; read($fh, $buf, 4) or barf $@;
Branch: 1 branch
BranchTrueFalseTotal
read $fh, $buf, 4 or barf($@) 1 0 1
79 1 unpack('L<', $buf);
80 };
81 1 ▶ $@ and seek($fh, $location, 0), return 'ascii'; # if we hit eof, not binary
Branch: 1 branch
BranchTrueFalseTotal
$@ and seek($fh, $location, 0), (return "ascii") 0 1 1
82 1 ▶ $count or barf "detection failed - no facets?";
Branch: 1 branch
BranchTrueFalseTotal
$count or barf("detection failed - no facets?") 1 0 1
83 1 my $size = (stat($fh))[7];
84 1 ▶ barf "failed to stat '$fh'" if !defined $size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size 0 1 1
85 # calculate the expected file size
86 1 my $expect =
87 + 80 # header
88 + 4 # count
89 + $count * (
90 + 4 # normal, pt,pt,pt (vectors)
91 * 3 # values per vector
92 * 4 # bytes per value
93 + 2 # the trailing 'short'
94 );
95 1 ▶ return ($size - $location >= $expect) ? 'binary' : 'ascii';
Branch: 1 branch
BranchTrueFalseTotal
$size - $location >= $expect ? : 1 0 1
96 }
97  
98 1 my $p_re = qr/([^ ]+)\s+([^ ]+)\s+([^ ]+)$/;
99 3 ▶sub _read_ascii {
Subroutine _read_ascii: called CC=18 SCAR=31.8
100 3 my ($fh) = @_;
101 132 ▶ my $getline = sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
102 132 ▶ while(my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 132 0 132
103 132 $line =~ s/\s*$//; # allow any eol
104 132 ▶ length($line) or next;
Branch: 1 branch
BranchTrueFalseTotal
length $line or next 132 0 132
105 132 return($line);
106 }
107 0 return;
108 3 };
109 3 my (@tri, $part);
110 3 ▶ while(my $line = $getline->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = &$getline()) 24 0 24
111 24 ▶ if($line =~ m/^\s*solid\s*(.*)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\s*solid\s*(.*)/) { } 3 21 24
elsif ($line =~ /^\s*endsolid\s*(.*)/) { } 3 18 21
112 3 $part = $1;
113 3 next;
114 }
115 elsif($line =~ m/^\s*endsolid\s*(.*)/) {
116 3 my $name = $1;
117 3 ▶ if (length $name) { # only catch if wrong; if absent, fine
Branch: 1 branch
BranchTrueFalseTotal
if (length $name) 1 2 3
118 1 ▶ barf "invalid 'endsolid' entry with no current part" if !defined $part;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $part 0 1 1
119 1 ▶ barf "end of part '$name' should have been '$part'" if $name ne $part;
Branch: 1 branch
BranchTrueFalseTotal
if $name ne $part 0 1 1
120 }
121 3 $part = undef;
122 3 last;
123 }
124 18 ▶ barf "what? ($line)" if !defined $part;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $part 0 18 18
125 18 ▶ my @n = ($line =~ m/^\s*facet\s+normal\s+$p_re/) or
Branch: 1 branch
BranchTrueFalseTotal
my(@n) = $line =~ /^\s*facet\s+normal\s+$p_re/ or barf("how did that happen? ($line)") 18 0 18
126 barf "how did that happen? ($line)";
127 18 my $next = $getline->();
128 18 ▶ unless($next and ($next =~ m/^\s*outer\s+loop$/)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($next and $next =~ /^\s*outer\s+loop$/) 0 18 18
Condition33%1/3
$next and $next =~ /^\s*outer\s+loop$/
!ll&&!rl&&r
0018
MC/DC0%0/2
$next and $next =~ /^\s*outer\s+loop$/
$next $next =~ /^\s*outer\s+loop$/
Truth table33%1/3
A and B
A= $nextB= $next =~ /^\s*outer\s+loop$/
ABresult
0X0
100
111
129 0 barf "facet doesn't start with 'outer loop' ($next)";
130 }
131 18 my @this_tri;
132 18 ▶ while(my $line = $getline->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = &$getline()) 72 0 72
133 72 ▶ ($line =~ m/^\s*endloop$/) and last;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^\s*endloop$/ and last 18 54 72
134 54 ▶ if($line =~ m/^\s*vertex\s+$p_re/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*vertex\s+$p_re/) 54 0 54
135 54 push(@this_tri, [$1, $2, $3]);
136 }
137 }
138 0 ▶ barf "need three vertices per facet (not @{[ 0+@this_tri ]})" if @this_tri != 3;
Branch: 1 branch
BranchTrueFalseTotal
if @this_tri != 3 0 18 18
139 18 my $end = $getline->();
140 18 ▶ ($end and ($end =~ m/^\s*endfacet/)) or
Branch: 1 branch
BranchTrueFalseTotal
$end and $end =~ /^\s*endfacet/ or barf("bad endfacet $line") 18 0 18
Condition33%1/3
$end and $end =~ /^\s*endfacet/
!ll&&!rl&&r
0018
MC/DC0%0/2
$end and $end =~ /^\s*endfacet/
$end $end =~ /^\s*endfacet/
Truth table33%1/3
A and B
A= $endB= $end =~ /^\s*endfacet/
ABresult
0X0
100
111
141 barf "bad endfacet $line";
142 18 push @tri, \@this_tri;
143 }
144 3 ▶ barf "part '$part' was left open" if defined $part;
Branch: 1 branch
BranchTrueFalseTotal
if defined $part 0 3 3
145 3 _as_ndarray(pdl PDL::float(), \@tri);
146 }
147  
148 4 ▶sub _as_ndarray {
Subroutine _as_ndarray: called CC=1 SCAR=0.0
149 4 my ($pdl) = @_;
150 4 my $uniqv = $pdl->uniqvec;
151 4 ($uniqv, $pdl->vsearchvec($uniqv), undef);
152 }
153  
154 1 ▶sub _read_binary {
Subroutine _read_binary: called CC=4 SCAR=13.9
155 1 my ($fh) = @_;
156 1 barf "bigfloat" unless(length(pack("f", 1)) == 4);
157 # TODO try to read part name from header (up to \0)
158 1 seek($fh, 80, 0);
159 1 ▶ my $buf; read($fh, $buf, 4) or warn "EOF?"; my $triangles = unpack('L<', $buf);
Branch: 1 branch
BranchTrueFalseTotal
read $fh, $buf, 4 or warn "EOF?" 1 0 1
160 1 my $bytes = 50 * $triangles; # norm+3vertices * 3float + short with length of extra
161 1 my $bytespdl = zeroes PDL::byte(), 50, $triangles;
162 1 my $bytesread = read($fh, ${$bytespdl->get_dataref}, $bytes);
163 1 ▶ barf "Tried to read $bytes but only got $bytesread" if $bytesread != $bytes;
Branch: 1 branch
BranchTrueFalseTotal
if $bytesread != $bytes 0 1 1
164 1 $bytespdl->upd_data;
165 1 my $floatpdl = zeroes PDL::float(), 3, 4, $triangles;
166 1 ${$floatpdl->get_dataref} = ${$bytespdl->slice('0:47')->get_dataref};
167 1 $floatpdl->upd_data;
168 1 ▶ $floatpdl->type->bswap->($floatpdl) if isbigendian();
Branch: 1 branch
BranchTrueFalseTotal
if isbigendian() 0 1 1
169 # TODO check that the unit normal is within a thousandth of a radian
170 # (0.001 rad is ~0.06deg)
171 1 _as_ndarray($floatpdl->slice(':,1:3'));
172 }
173  
174 =head2 wstl
175  
176 =for ref
177  
178 Simple PDL STL writer
179  
180 =for example
181  
182 wstl 'file.stl', $vertices, $faceidx;
183 wstl 'file.stl', $vertices, $faceidx, \%OPTIONS;
184 wstl $fh, $vertices, $faceidx, \%OPTIONS;
185  
186 Passing a file-handle is supported, so multiple parts can be written to
187 an ASCII file with several calls.
188  
189 C<wstl> accepts several options that may be passed in as a hash ref
190 if desired:
191  
192 =over 3
193  
194 =item mode (default='binary')
195  
196 Whether to write out the file as ASCII or binary.
197  
198 =item name (default='part')
199  
200 The part name to use.
201  
202 =back
203  
204 =cut
205  
206 1 our $wstl_options = PDL::Options->new( { mode=>'binary', name=>'part' } );
207 1 my %valid_mode = map +($_=>1), qw(ascii binary);
208 2 ▶sub wstl { PDL->wstl(@_); }
Subroutine wstl: called CC=1 SCAR=0.0
Pod documented
209 2 ▶sub PDL::wstl {
Subroutine wstl: called CC=8 SCAR=23.9
Pod undocumented
210 2 ▶ barf 'Usage: wstl($file,$vertices,$faceidx,[$colours],[{options}])' if @_<3 || @_>5;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 3 or @_ > 5 1 1 2
Condition66%2/3
@_ < 3 or @_ > 5
l!l&&r!l&&!r
101
MC/DC50%1/2
@_ < 3 or @_ > 5
@_ < 3 @_ > 5
Truth table66%2/3
A or B
A= @_ < 3B= @_ > 5
ABresult
1X1
011
000
211 1 my (undef, $file, $v, $f, $c) = @_;
212 1 my $u_opt = ifhref($_[-1]);
213 1 my $opt = $wstl_options->options($u_opt);
214 1 my $mode = $opt->{mode};
215 1 ▶ barf "invalid write mode '$mode'" if !$valid_mode{$mode};
Branch: 1 branch
BranchTrueFalseTotal
unless $valid_mode{$mode} 0 1 1
216 # allow filehandle
217 1 ▶ unless((ref($file) || '') eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
unless ((ref $file || "") eq "GLOB") 0 1 1
Condition50%1/2
ref $file || ''
l!l
10
MC/DC0%0/1
ref $file || ''
ref $file
Truth table50%1/2
A
A= ref $file
Aresult
11
00
218 0 ▶ open(my $fh, '>', $file) or
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or barf("cannot open '${file}' for writing $!") 0 0 0
219 barf "cannot open '$file' for writing $!";
220 0 binmode $fh;
221 0 $file = $fh;
222 }
223 1 ▶ my $func = $mode eq 'ascii' ? \&_write_ascii : \&_write_binary;
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'ascii' ? : 0 1 1
224 1 $func->($file, $v, $f, $c, $opt->{name});
225 1 1;
226 }
227  
228 1 ▶sub _write_binary {
Subroutine _write_binary: called CC=2 SCAR=6.9
229 1 my ($fh, $v, $f, $c, $name) = @_;
230 1 ▶ print $fh $name, "\0" x (80 - do {use bytes; length($name)});
Subroutine BEGIN: called CC=1 SCAR=0.0
231 1 print $fh pack 'L<', $f->dim(1);
232 1 foreach my $facet (@{ $v->dice_axis(1, $f->flat)->splitdim(1,3)->unpdl }) {
233 12 print $fh map {map pack('f<', $_), @$_} [0,0,0], @$facet;
234 12 print $fh "\0" x 2;
235 }
236 }
237  
238 0 ▶sub _write_ascii {
Subroutine _write_ascii: not called CC=4 SCAR=30.0
239 0 my ($fh, $v, $f, $c, $name) = @_;
240 0 my $spaces = '';
241 0 ▶ my $print = sub {print $fh $spaces . join(' ', @_) . "\n"};
Subroutine __ANON__: not called CC=1 SCAR=6.9
242 0 $print->('solid', $name);
243 0 $spaces = ' 'x2;
244 0 foreach my $facet (@{ $v->dice_axis(1, $f->flat)->splitdim(1,3)->unpdl }) {
245 0 my ($n, @pts) = ([0,0,0], @$facet);
246 0 $print->('facet normal', @$n);
247 0 $spaces = ' 'x4;
248 0 $print->('outer loop');
249 0 $spaces = ' 'x6;
250 0 ▶ (@pts == 3) or barf "invalid facet";
Branch: 1 branch
BranchTrueFalseTotal
@pts == 3 or barf("invalid facet") 0 0 0
251 0 foreach my $pt (@pts) {
252 0 $print->('vertex', @$pt);
253 }
254 0 $spaces = ' 'x4;
255 0 $print->('endloop');
256 0 $spaces = ' 'x2;
257 0 $print->('endfacet');
258 }
259 0 $spaces = '';
260 0 $print->('endsolid', $name);
261 }
262  
263 =head1 AUTHOR
264  
265 Ed J, based on Eric Wilhelm's code in L<CAD::Format::STL>.
266  
267 =cut
268  
269 1 1;
« blib/lib/PDL/Demos.pm ↑ Summary lib/PDL/MatrixOps.pm »