A slightly deranged piece of PDL code I wrote to rapidly decode lots of
floats at scale from binary STL files, at
https://github.com/PDLPorters/pdl/blob/5b6057ffed3ffc1fe2f8018b28299ecf30e36f35/IO/STL/STL.pm#L152-L170:
sub _read_binary {
my ($fh) = @_;
barf "bigfloat" unless(length(pack("f", 1)) == 4);
# TODO try to read part name from header (up to \0)
seek($fh, 80, 0);
my $buf; read($fh, $buf, 4) or warn "EOF?"; my $triangles = unpack('
+L<', $buf);
my $bytes = 50 * $triangles; # norm+3vertices * 3float + short with
+length of extra
my $bytespdl = zeroes PDL::byte(), 50, $triangles;
my $bytesread = read($fh, ${$bytespdl->get_dataref}, $bytes);
barf "Tried to read $bytes but only got $bytesread" if $bytesread !=
+ $bytes;
$bytespdl->upd_data;
my $floatpdl = zeroes PDL::float(), 3, 4, $triangles;
${$floatpdl->get_dataref} = ${$bytespdl->slice('0:47')->get_dataref}
+;
$floatpdl->upd_data;
$floatpdl->type->bswap->($floatpdl) if isbigendian();
# TODO check that the unit normal is within a thousandth of a radian
# (0.001 rad is ~0.06deg)
_as_ndarray($floatpdl->slice(':,1:3'));
}