| 1 | package CAD::Mesh3D::FormatSTL; | ||||||||||||||||||||||||||||
| 2 | 3 | $VERSION = v0.2.1.001; # patched version of CAD::Format::STL v0.2.1 | |||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use CAD::Format::STL::part; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | CAD::Mesh3D::FormatSTL - read/write 3D stereolithography files | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 DON'T USE | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | Please don't use this module. CAD::Mesh3D::FormatSTL exists only for | ||||||||||||||||||||||||||||
| 17 | L<CAD::Mesh3D::STL> to use during testing and to overcome limitations | ||||||||||||||||||||||||||||
| 18 | in L<CAD::Format::STL>. If you think you want to use this directly, | ||||||||||||||||||||||||||||
| 19 | use L<CAD::Format::STL> instead, and encourage the author to implement | ||||||||||||||||||||||||||||
| 20 | and release the known bug-fix that is in the existing issues, possibly | ||||||||||||||||||||||||||||
| 21 | patching it per the instructions in the L<CAD::Mesh3D::STL/"Known Issues">. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | Reading: | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | my $stl = CAD::Mesh3D::FormatSTL->new->load("foo.stl"); | ||||||||||||||||||||||||||||
| 28 | # what about the part/multipart? | ||||||||||||||||||||||||||||
| 29 | my @facets = $stl->part->facets; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | Writing: | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | my $stl = CAD::Mesh3D::FormatSTL->new; | ||||||||||||||||||||||||||||
| 34 | my $part = $stl->add_part("my part"); | ||||||||||||||||||||||||||||
| 35 | $part->add_facets(@faces); | ||||||||||||||||||||||||||||
| 36 | $stl->save("foo.stl"); | ||||||||||||||||||||||||||||
| 37 | # or $stl->save(binary => "foo.stl"); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | Streaming read/write: | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | my $reader = CAD::Mesh3D::FormatSTL->reader("foo.stl"); | ||||||||||||||||||||||||||||
| 42 | my $writer = CAD::Mesh3D::FormatSTL->writer(binary => "bar.stl"); | ||||||||||||||||||||||||||||
| 43 | while(my $part = $reader->next_part) { | ||||||||||||||||||||||||||||
| 44 | my $part_name = $part->name; | ||||||||||||||||||||||||||||
| 45 | $writer->start_solid($part_name); | ||||||||||||||||||||||||||||
| 46 | while(my @data = $part->facet) { | ||||||||||||||||||||||||||||
| 47 | my ($normal, @vertices) = @data; | ||||||||||||||||||||||||||||
| 48 | my @v1 = @{$vertices[0]}; | ||||||||||||||||||||||||||||
| 49 | my @v2 = @{$vertices[0]}; | ||||||||||||||||||||||||||||
| 50 | my @v3 = @{$vertices[0]}; | ||||||||||||||||||||||||||||
| 51 | # that's just for illustration | ||||||||||||||||||||||||||||
| 52 | $writer->facet(\@v1, \@v2, \@v3); | ||||||||||||||||||||||||||||
| 53 | # note the omitted normal | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | $writer->end_solid; | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =begin design | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | The reader auto-detects whether it is binary (but assumes ascii when | ||||||||||||||||||||||||||||
| 61 | seek can't go backwards.) | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | The reader and writer both take 1, 2, or {1,2}+2n arguments. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | This package and/or the reader/writer are subclassable (though getting | ||||||||||||||||||||||||||||
| 66 | $self->reader to instantiate a subclass implies that you have subclassed | ||||||||||||||||||||||||||||
| 67 | $self.) | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | A cached_facet (or raw_facet) method is necessary to ensure uniform | ||||||||||||||||||||||||||||
| 70 | tranformation of shared points (and optimize the computation.) This | ||||||||||||||||||||||||||||
| 71 | would return the normal and points as a list of scalars rather than | ||||||||||||||||||||||||||||
| 72 | arrays, with a later call to unpack_point() or something. The caller | ||||||||||||||||||||||||||||
| 73 | needs to be able to handle the caching (or else there is a callback for | ||||||||||||||||||||||||||||
| 74 | non-cached (or an override for unpack_point().) | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | Maybe $self->set_writer() and set_reader() immutable object methods? | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =end design | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =head1 ABOUT | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | This module provides object-oriented methods to read and write the STL | ||||||||||||||||||||||||||||
| 83 | (Stereo Lithography) file format in both binary and ASCII forms. The | ||||||||||||||||||||||||||||
| 84 | STL format is a simple set of 3D triangles. | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 3 | ▶ | use Class::Accessor::Classy; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 89 | 3 | lo 'parts'; | |||||||||||||||||||||||||||
| 90 | 3 | ▶ | no Class::Accessor::Classy; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head1 Constructor | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head2 new | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | my $stl = CAD::Mesh3D::FormatSTL->new; | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =cut | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub new { | ||||||||||||||||||||||||||||
| 101 | 20 | ▶ | my $package = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | 20 | ▶ | my $class = ref($package) || $package; | ||||||||||||||||||||||||||
|
Condition100%3/3
ref $package || $package
MC/DC100%2/2
ref $package || $package
ref $package
$package
Truth table100%3/3
A || B
A= ref $packageB= $package
| |||||||||||||||||||||||||||||
| 103 | 20 | my $self = {parts => []}; | |||||||||||||||||||||||||||
| 104 | 20 | bless($self, $class); | |||||||||||||||||||||||||||
| 105 | 20 | return($self); | |||||||||||||||||||||||||||
| 106 | } # end subroutine new definition | ||||||||||||||||||||||||||||
| 107 | ######################################################################## | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head2 add_part | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Create a new part in the stl. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | my $part = $stl->add_part("name"); | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | Optionally, add the faces directly: | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | my $part = $stl->add_part("name", @faces); | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =cut | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | sub add_part { | ||||||||||||||||||||||||||||
| 122 | 12 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
add_part: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 123 | 12 | my ($name, @faces) = @_; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 12 | my $part = CAD::Format::STL::part::->new($name, @faces); | |||||||||||||||||||||||||||
| 126 | 12 | push(@{$self->{parts}}, $part); | |||||||||||||||||||||||||||
| 127 | 12 | return($part); | |||||||||||||||||||||||||||
| 128 | } # end subroutine add_part definition | ||||||||||||||||||||||||||||
| 129 | ######################################################################## | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =head2 part | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | Get the part at $index. Negative indices are valid. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | my $part = $stl->part($index); | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | Throws an error if there is no such part. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =cut | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | sub part { | ||||||||||||||||||||||||||||
| 142 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
part: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | 5 | my ($index) = @_; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 5 | ▶ | @{$self->{parts}} or croak("file has no parts"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 4 | ▶ | $index ||= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$index ||= 0
MC/DC100%1/1
$index ||= 0
$index
Truth table100%2/2
A
A= $index
| |||||||||||||||||||||||||||||
| 148 | 4 | ▶ | exists($self->{parts}[$index]) or croak("no part $index"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 2 | return($self->{parts}[$index]); | |||||||||||||||||||||||||||
| 150 | } # end subroutine part definition | ||||||||||||||||||||||||||||
| 151 | ######################################################################## | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head1 I/O Methods | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =head2 load | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | Load an STL file (auto-detects binary/ascii) | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | $stl = $stl->load("filename.stl"); | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | Optionally, explicitly declare binary mode: | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | $stl = $stl->load(binary => "filename.stl"); | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | The $self object is returned to allow e.g. chaining to C<new()>. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | The filename may also be a filehandle. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =cut | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | sub load { | ||||||||||||||||||||||||||||
| 172 | 15 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
load: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||
| 173 | 15 | my ($file, @and) = @_; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 15 | my $mode; | |||||||||||||||||||||||||||
| 176 | 15 | ▶ | if(@and) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 7 | ▶ | (@and > 1) and croak('too many arguments to load()'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 6 | $mode = $file; | |||||||||||||||||||||||||||
| 179 | 6 | ($file) = @and; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | # allow filehandle | ||||||||||||||||||||||||||||
| 183 | 14 | ▶ | unless((ref($file) || '') eq 'GLOB') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
ref $file || ''
MC/DC100%1/1
ref $file || ''
ref $file
Truth table100%2/2
A
A= ref $file
| |||||||||||||||||||||||||||||
| 184 | 12 | ▶ | open(my $fh, '<', $file) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | die "cannot open '$file' for reading $!"; | ||||||||||||||||||||||||||||
| 186 | 11 | $file = $fh; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | # detection | ||||||||||||||||||||||||||||
| 190 | 13 | ▶ | unless($mode) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 7 | ▶ | unless(seek($file, 0,0)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 0 | croak('must have explicit mode for non-seekable filehandle'); | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | # now, detection... | ||||||||||||||||||||||||||||
| 195 | $mode = sub { | ||||||||||||||||||||||||||||
| 196 | 7 | ▶ | my $fh = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 197 | 7 | seek($fh, 80, 0); | |||||||||||||||||||||||||||
| 198 | 7 | my $count = eval { | |||||||||||||||||||||||||||
| 199 | 7 | ▶ | my $buf; read($fh, $buf, 4) or die; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 5 | unpack('L', $buf); | |||||||||||||||||||||||||||
| 201 | }; | ||||||||||||||||||||||||||||
| 202 | 7 | ▶ | $@ and return 'ascii'; # if we hit eof, it can't be binary | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 5 | ▶ | $count or die "detection failed - no facets?"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | 4 | my $size = (stat($fh))[7]; | |||||||||||||||||||||||||||
| 205 | # calculate the expected file size | ||||||||||||||||||||||||||||
| 206 | 4 | my $expect = | |||||||||||||||||||||||||||
| 207 | + 80 # header | ||||||||||||||||||||||||||||
| 208 | + 4 # count | ||||||||||||||||||||||||||||
| 209 | + $count * ( | ||||||||||||||||||||||||||||
| 210 | + 4 # normal, pt,pt,pt (vectors) | ||||||||||||||||||||||||||||
| 211 | * 4 # bytes per value | ||||||||||||||||||||||||||||
| 212 | * 3 # values per vector | ||||||||||||||||||||||||||||
| 213 | + 2 # the trailing 'short' | ||||||||||||||||||||||||||||
| 214 | ); | ||||||||||||||||||||||||||||
| 215 | 4 | ▶ | return ($size == $expect) ? 'binary' : 'ascii'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | 7 | }->($file); | |||||||||||||||||||||||||||
| 217 | 6 | ▶ | seek($file, 0, 0) or die "cannot reset filehandle"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 12 | my $method = '_read_' . lc($mode); | |||||||||||||||||||||||||||
| 221 | 12 | ▶ | $self->can($method) or croak("invalid read mode '$mode'"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 12 | $self->$method($file); | |||||||||||||||||||||||||||
| 224 | 9 | return($self); | |||||||||||||||||||||||||||
| 225 | } # end subroutine load definition | ||||||||||||||||||||||||||||
| 226 | ######################################################################## | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | =head2 _read_ascii | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | $self->_read_ascii($filehandle); | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =cut | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | sub _read_ascii { | ||||||||||||||||||||||||||||
| 235 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_read_ascii: called CC=17 SCAR=29.1
| |||||||||||||||||||||||||||||
| 236 | 8 | my ($fh) = @_; | |||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | my $getline = sub { | ||||||||||||||||||||||||||||
| 239 | 363 | ▶ | while(my $line = <$fh>) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | 357 | $line =~ s/\s*$//; # allow any eol | |||||||||||||||||||||||||||
| 241 | 357 | ▶ | length($line) or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | 357 | return($line); | |||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | 6 | return; | |||||||||||||||||||||||||||
| 245 | 8 | }; | |||||||||||||||||||||||||||
| 246 | 8 | my $p_re = qr/([^ ]+)\s+([^ ]+)\s+([^ ]+)$/; | |||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 8 | my $part; | |||||||||||||||||||||||||||
| 249 | 8 | ▶ | while(my $line = $getline->()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 62 | ▶ | if($line =~ m/^\s*solid (.*)/) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 252 | 6 | $part = $self->add_part($1); | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | elsif($line =~ m/^\s*endsolid (.*)/) { | ||||||||||||||||||||||||||||
| 255 | 5 | my $name = $1; | |||||||||||||||||||||||||||
| 256 | 5 | ▶ | $part or die "invalid 'endsolid' entry with no current part"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | 5 | ▶ | ($name eq $part->name) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | die "end of part '$name' should have been '", | ||||||||||||||||||||||||||||
| 259 | $part->name, "'"; | ||||||||||||||||||||||||||||
| 260 | 5 | $part = undef; | |||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | elsif($part) { | ||||||||||||||||||||||||||||
| 263 | 50 | ▶ | my @n = ($line =~ m/^\s*facet\s+normal\s+$p_re/) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | die "how did that happen? ($line)"; | ||||||||||||||||||||||||||||
| 265 | #warn "got ", join('|', @n); | ||||||||||||||||||||||||||||
| 266 | 50 | my @facet = (\@n); | |||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 50 | my $next = $getline->(); | |||||||||||||||||||||||||||
| 269 | 50 | ▶ | unless($next and ($next =~ m/^\s*outer\s+loop$/)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$next and $next =~ /^\s*outer\s+loop$/
MC/DC50%1/2
$next and $next =~ /^\s*outer\s+loop$/
$next
$next =~ /^\s*outer\s+loop$/
Truth table66%2/3
A and B
A= $nextB= $next =~ /^\s*outer\s+loop$/
| |||||||||||||||||||||||||||||
| 270 | 1 | die "facet doesn't start with 'outer loop' ($next)"; | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | 49 | push(@facet, do { | |||||||||||||||||||||||||||
| 273 | 49 | my @got; | |||||||||||||||||||||||||||
| 274 | 49 | ▶ | while(my $line = $getline->()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 275 | 196 | ▶ | ($line =~ m/^\s*endloop$/) and last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | 147 | ▶ | if($line =~ m/^\s*vertex\s+$p_re/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | 147 | push(@got, [$1, $2, $3]); | |||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | 49 | @got; | |||||||||||||||||||||||||||
| 281 | }); | ||||||||||||||||||||||||||||
| 282 | 49 | ▶ | (scalar(@facet) == 4) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 283 | die "need three vertices per facet (not $#facet)"; | ||||||||||||||||||||||||||||
| 284 | 49 | my $end = $getline->(); | |||||||||||||||||||||||||||
| 285 | 49 | ▶ | ($end and ($end =~ m/^\s*endfacet/)) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$end and $end =~ /^\s*endfacet/
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/
| |||||||||||||||||||||||||||||
| 286 | die "bad endfacet $line"; | ||||||||||||||||||||||||||||
| 287 | 49 | $part->add_facets([@facet]); | |||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | else { | ||||||||||||||||||||||||||||
| 290 | 1 | die "what? ($line)"; | |||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | 6 | ▶ | $part and die "part '", $part->name, "' was left open"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | } # end subroutine _read_ascii definition | ||||||||||||||||||||||||||||
| 295 | ######################################################################## | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | =head2 get_<something> | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | These functions are currently only used internally. | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | =over | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | =item get_triangle | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | =item get_ulong | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | =item get_float32 | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | =item get_short | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | =back | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | =cut | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | sub get_triangle { | ||||||||||||||||||||||||||||
| 316 | 52 | ▶ | my ($fh) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_triangle: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 52 | my ($n, $x, $y, $z) = map({[map({get_float32($fh)} 1..3)]} 1..4); | |||||||||||||||||||||||||||
| 319 | 52 | my $scrap = get_short($fh); | |||||||||||||||||||||||||||
| 320 | 52 | return($n, $x, $y, $z); | |||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | sub get_ulong { | ||||||||||||||||||||||||||||
| 324 | 4 | ▶ | my ($fh) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_ulong: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 4 | my $buf; | |||||||||||||||||||||||||||
| 327 | 4 | ▶ | read($fh, $buf, 4) or warn "EOF?"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 328 | 4 | return(unpack('L', $buf)); | |||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | sub get_float32 { | ||||||||||||||||||||||||||||
| 332 | 624 | ▶ | my ($fh) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_float32: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 624 | my $buf; | |||||||||||||||||||||||||||
| 335 | 624 | ▶ | read($fh, $buf, 4) or warn "EOF?"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 336 | 624 | return(unpack('f', $buf)); | |||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | sub get_short { | ||||||||||||||||||||||||||||
| 340 | 52 | ▶ | my ($fh) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_short: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 52 | my $buf; | |||||||||||||||||||||||||||
| 343 | 52 | ▶ | read($fh, $buf, 2) or warn "EOF?"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 344 | 52 | return(unpack('S', $buf)); | |||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | =head2 _read_binary | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | $self->_read_binary($filehandle); | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | =cut | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | sub _read_binary { | ||||||||||||||||||||||||||||
| 354 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_read_binary: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 355 | 4 | my ($fh) = @_; | |||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 4 | binmode $fh; | |||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 4 | ▶ | $self->parts and die "binary STL files must have only one part"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | 4 | die "bigfloat" unless(length(pack("f", 1)) == 4); | |||||||||||||||||||||||||||
| 362 | # TODO try to read part name from header (up to \0) | ||||||||||||||||||||||||||||
| 363 | 4 | my $name = 'a part'; | |||||||||||||||||||||||||||
| 364 | 4 | seek($fh, 80, 0); | |||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 4 | my $triangles = get_ulong($fh); | |||||||||||||||||||||||||||
| 367 | 4 | my $part = $self->add_part($name); | |||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | 4 | my $count = 0; | |||||||||||||||||||||||||||
| 370 | 4 | while(1) { | |||||||||||||||||||||||||||
| 371 | 52 | my @tr = get_triangle($fh); | |||||||||||||||||||||||||||
| 372 | # TODO check that the unit normal is within a thousandth of a radian | ||||||||||||||||||||||||||||
| 373 | # (0.001 rad is ~0.06deg) | ||||||||||||||||||||||||||||
| 374 | 52 | $part->add_facets([@tr]); | |||||||||||||||||||||||||||
| 375 | 52 | $count++; | |||||||||||||||||||||||||||
| 376 | 52 | ▶ | eof($fh) and last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||
| 378 | 4 | ▶ | ($count == $triangles) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 379 | die "ERROR: got $count facets (expected $triangles)"; | ||||||||||||||||||||||||||||
| 380 | } # end subroutine _read_binary definition | ||||||||||||||||||||||||||||
| 381 | ######################################################################## | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | =head2 save | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | $stl->save("filename.stl"); | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | $stl->save(binary => "filename.stl"); | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | =cut | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | sub save { | ||||||||||||||||||||||||||||
| 392 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
save: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 393 | 2 | my ($file, @and) = @_; | |||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | 2 | my $mode; | |||||||||||||||||||||||||||
| 396 | 2 | ▶ | if(@and) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 397 | 1 | ▶ | (@and > 1) and croak('too many arguments to save()'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 398 | 1 | $mode = $file; | |||||||||||||||||||||||||||
| 399 | 1 | ($file) = @and; | |||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | # allow filehandle | ||||||||||||||||||||||||||||
| 403 | 2 | ▶ | unless((ref($file) || '') eq 'GLOB') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
ref $file || ''
MC/DC100%1/1
ref $file || ''
ref $file
Truth table100%2/2
A
A= ref $file
| |||||||||||||||||||||||||||||
| 404 | 1 | ▶ | open(my $fh, '>', $file) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 405 | die "cannot open '$file' for writing $!"; | ||||||||||||||||||||||||||||
| 406 | 1 | $file = $fh; | |||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | 2 | ▶ | $mode = 'ascii' unless($mode); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | 2 | my $method = '_write_' . lc($mode); | |||||||||||||||||||||||||||
| 412 | 2 | ▶ | $self->can($method) or croak("invalid write mode '$mode'"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | 2 | $self->$method($file); | |||||||||||||||||||||||||||
| 415 | } # end subroutine save definition | ||||||||||||||||||||||||||||
| 416 | ######################################################################## | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | =head2 _write_binary | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | $self->_write_binary($filehandle); | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | =cut | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | sub _write_binary { | ||||||||||||||||||||||||||||
| 425 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_write_binary: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 426 | 1 | my ($fh) = @_; | |||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | 1 | my ($part, @and) = $self->parts; | |||||||||||||||||||||||||||
| 429 | 1 | ▶ | @and and die 'cannot write binary files with multiple parts'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | 1 | binmode $fh; | |||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | 1 | my $name = $part->name; # utf8 is ok | |||||||||||||||||||||||||||
| 434 | 3 | ▶ | print $fh $name, "\0" x (80 - do {use bytes; length($name)}); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 435 | 1 | my @facets = $part->facets; | |||||||||||||||||||||||||||
| 436 | 1 | print $fh pack('L', scalar(@facets)); | |||||||||||||||||||||||||||
| 437 | 1 | foreach my $facet (@facets) { | |||||||||||||||||||||||||||
| 438 | 1 | print $fh map({map({pack('f', $_)} @$_)} @$facet); | |||||||||||||||||||||||||||
| 439 | 1 | print $fh "\0" x 2; | |||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | } # end subroutine _write_binary definition | ||||||||||||||||||||||||||||
| 443 | ######################################################################## | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | =head2 _write_ascii | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | $self->_write_ascii($filehandle); | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | =cut | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | sub _write_ascii { | ||||||||||||||||||||||||||||
| 452 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_write_ascii: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 453 | 1 | my ($fh) = @_; | |||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | 1 | my $spaces = ''; | |||||||||||||||||||||||||||
| 456 | 1 | ▶ | my $print = sub {print $fh $spaces, @_, "\n"}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 457 | 1 | ▶ | my @parts = $self->parts or croak("no parts to write"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 458 | 1 | foreach my $part (@parts) { | |||||||||||||||||||||||||||
| 459 | 1 | $print->('solid ', $part->name); | |||||||||||||||||||||||||||
| 460 | 1 | $spaces = ' 'x2; | |||||||||||||||||||||||||||
| 461 | 1 | foreach my $facet ($part->facets) { | |||||||||||||||||||||||||||
| 462 | 12 | my ($n, @pts) = @$facet; | |||||||||||||||||||||||||||
| 463 | 12 | $print->(join(' ', 'facet normal', @$n)); | |||||||||||||||||||||||||||
| 464 | 12 | $spaces = ' 'x4; | |||||||||||||||||||||||||||
| 465 | 12 | $print->('outer loop'); | |||||||||||||||||||||||||||
| 466 | 12 | $spaces = ' 'x6; | |||||||||||||||||||||||||||
| 467 | 12 | ▶ | (@pts == 3) or die "invalid facet"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 468 | 12 | foreach my $pt (@pts) { | |||||||||||||||||||||||||||
| 469 | 36 | $print->(join(' ', 'vertex', @$pt)); | |||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||
| 471 | 12 | $spaces = ' 'x4; | |||||||||||||||||||||||||||
| 472 | 12 | $print->('endloop'); | |||||||||||||||||||||||||||
| 473 | 12 | $spaces = ' 'x2; | |||||||||||||||||||||||||||
| 474 | 12 | $print->('endfacet'); | |||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||
| 476 | 1 | $spaces = ''; | |||||||||||||||||||||||||||
| 477 | 1 | print $fh 'endsolid ', $part->name, "\n"; | |||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||
| 479 | } # end subroutine _write_ascii definition | ||||||||||||||||||||||||||||
| 480 | ######################################################################## | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | Eric Wilhelm @ <ewilhelm at cpan dot org> | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | http://scratchcomputing.com/ | ||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | CAD::Format::STL Copyright (C) 2007 Eric L. Wilhelm, All Rights Reserved. | ||||||||||||||||||||||||||||
| 491 | CAD::Mesh3D::FormatSTL Copyright (C) 2021 Peter C. Jones, All Rights Reserved. | ||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | =head1 NO WARRANTY | ||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | Absolutely, positively NO WARRANTY, neither express or implied, is | ||||||||||||||||||||||||||||
| 496 | offered with this software. You use this software at your own risk. In | ||||||||||||||||||||||||||||
| 497 | case of loss, no person or entity owes you anything whatsoever. You | ||||||||||||||||||||||||||||
| 498 | have been warned. | ||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 503 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||
| 505 | =head1 PATCHED BY CAD::Mesh3D | ||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||
| 507 | Per the LICENSE following the same terms as Perl, the | ||||||||||||||||||||||||||||
| 508 | L<Artistic License|https://dev.perl.org/licenses/artistic.html> | ||||||||||||||||||||||||||||
| 509 | allows publishing a modified or patched version under the same | ||||||||||||||||||||||||||||
| 510 | name as long as it is made freely available or by allowing the | ||||||||||||||||||||||||||||
| 511 | original copyright holder to include my modifications in the | ||||||||||||||||||||||||||||
| 512 | standard version of the package. As the core modifications | ||||||||||||||||||||||||||||
| 513 | have been in CAD::Format::STL's | ||||||||||||||||||||||||||||
| 514 | L<issue tracker|https://rt.cpan.org/Public/Bug/Display.html?id=83595> | ||||||||||||||||||||||||||||
| 515 | since Feb 2013, the CAD::Mesh3D developer feels justified in providing | ||||||||||||||||||||||||||||
| 516 | the patched version along with CAD::Mesh3D, which requires the patched | ||||||||||||||||||||||||||||
| 517 | version to be used in Windows. However, to avoid offense and confusion, | ||||||||||||||||||||||||||||
| 518 | the file/module that includes the patch has been renamed to | ||||||||||||||||||||||||||||
| 519 | CAD::Mesh3D::FormatSTL in this distribution. | ||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | If the original author of CAD::Format::STL ever publishes a newer version | ||||||||||||||||||||||||||||
| 522 | that doesn't contain the bug, this patched version will not be used by | ||||||||||||||||||||||||||||
| 523 | CAD::Mesh3D, and the official module will be used instead. | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | =cut | ||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | # vi:ts=2:sw=2:et:sta | ||||||||||||||||||||||||||||
| 528 | 3 | 1; | |||||||||||||||||||||||||||