Method: STL::Parser#parse_binary

Defined in:
lib/stl/parser.rb

#parse_binary(io) ⇒ Array

Parse a binary STL file, assuming that the header has already been read

Parameters:

  • io (IO)

    the stream to parse

Returns:

  • (Array)

    An array of [Normal, Triangle] pairs

Raises:

  • (StandardError)


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/stl/parser.rb', line 45

def parse_binary(io)
    count = io.read(4).unpack('V').first

    faces = []
    while not io.eof?
  normal, *vertices = io.read(50).unpack('F3F3F3F3x2').each_slice(3).to_a
  faces.push [Vector[*normal], Geometry::Triangle.new(*vertices)]
    end
    raise StandardError, "Unexpected end of file after #{faces.length} triangles" if faces.length != count

    faces
end