Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- def write_ngl(path_or_buf, vertices, triangles):
- try:
- _write_ngl_buffer(path_or_buf)
- except AttributeError as e:
- if "write" not in str(e):
- raise e
- with open(path_or_buf, 'wb') as f:
- _write_ngl_buffer(path_or_buf, vertices, triangles)
- def _write_ngl_buffer(buf, vertices, triangles):
- buf.write(np.uint32(len(vertices)))
- buf.write(np.asarray(vertices, dtype=np.float32).tobytes())
- buf.write(np.asarray(triangles, dtype=np.uint32).tobytes())
- def read_ngl(path_or_buf):
- try:
- _read_ngl_buffer(path_or_buf)
- except AttributeError as e:
- if "read" not in str(e):
- raise e
- with open(path_or_buf, 'rb') as f:
- _read_ngl_buffer(f)
- def _read_ngl_buffer(buf):
- n_vertices = int.from_bytes(buf.read(4), byteorder="little")
- vertices = np.frombuffer(buf, np.float32, 3 * n_vertices).reshape((n_vertices, 3))
- triangles = np.frombuffer(buf, np.uint32)
- triangles.reshape([len(triangles) // 3, 3])
- return vertices, triangles
Add Comment
Please, Sign In to add comment