Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Converting bytes to floats

by etj (Deacon)
on Apr 18, 2023 at 19:04 UTC ( [id://11151738]=note: print w/replies, xml ) Need Help??


in reply to Converting bytes to floats

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')); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11151738]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found