Guest User

Untitled

a guest
Jul 20th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. import numpy as np
  2.  
  3.  
  4. def write_ngl(path_or_buf, vertices, triangles):
  5. try:
  6. _write_ngl_buffer(path_or_buf)
  7. except AttributeError as e:
  8. if "write" not in str(e):
  9. raise e
  10. with open(path_or_buf, 'wb') as f:
  11. _write_ngl_buffer(path_or_buf, vertices, triangles)
  12.  
  13.  
  14. def _write_ngl_buffer(buf, vertices, triangles):
  15. buf.write(np.uint32(len(vertices)))
  16. buf.write(np.asarray(vertices, dtype=np.float32).tobytes())
  17. buf.write(np.asarray(triangles, dtype=np.uint32).tobytes())
  18.  
  19.  
  20. def read_ngl(path_or_buf):
  21. try:
  22. _read_ngl_buffer(path_or_buf)
  23. except AttributeError as e:
  24. if "read" not in str(e):
  25. raise e
  26. with open(path_or_buf, 'rb') as f:
  27. _read_ngl_buffer(f)
  28.  
  29.  
  30. def _read_ngl_buffer(buf):
  31. n_vertices = int.from_bytes(buf.read(4), byteorder="little")
  32. vertices = np.frombuffer(buf, np.float32, 3 * n_vertices).reshape((n_vertices, 3))
  33. triangles = np.frombuffer(buf, np.uint32)
  34. triangles.reshape([len(triangles) // 3, 3])
  35. return vertices, triangles
Add Comment
Please, Sign In to add comment