Guest User

Untitled

a guest
Jan 22nd, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. """
  2. Transforms contain utility functions to manipulate pointclouds
  3.  
  4. date : 2017-20-03
  5. """
  6.  
  7. __author__ = "Mathieu Garon"
  8. __version__ = "0.0.1"
  9.  
  10. from plyfile import PlyData, PlyElement
  11. import os
  12. import numpy as np
  13. from PIL import Image
  14.  
  15.  
  16. class PlyParser:
  17. def __init__(self, path):
  18. self.path = path
  19. self.data = PlyData.read(path)
  20.  
  21. def get_texture(self):
  22. for comment in self.data.comments:
  23. if "TextureFile" in comment:
  24. tex_path = os.path.join(os.path.dirname(self.path), comment.split(" ")[-1])
  25. return np.array(Image.open(tex_path)).astype(np.uint8)
  26. return None
  27.  
  28. def get_vertex(self):
  29. for element in self.data.elements:
  30. if element.name == "vertex":
  31. return PlyParser.recarray_to_array(element.data[["x", "y", "z"]], np.float32)
  32. raise KeyError("No field vertex with x, y, z in ply file.")
  33.  
  34. def get_vertex_color(self):
  35. for element in self.data.elements:
  36. if element.name == "vertex":
  37. try:
  38. return PlyParser.recarray_to_array(element.data[["red", "green", "blue"]], np.uint8)
  39. except ValueError:
  40. break
  41. raise KeyError("No field vertex with red, green, blue in ply file.")
  42.  
  43. def get_vertex_normals(self):
  44. for element in self.data.elements:
  45. if element.name == "vertex":
  46. try:
  47. return PlyParser.recarray_to_array(element.data[["nx", "ny", "nz"]], np.float32)
  48. except ValueError:
  49. break
  50. raise KeyError("No field vertex with normals nx, ny, nz in ply file.")
  51.  
  52. def get_texture_coord(self):
  53. for element in self.data.elements:
  54. if element.name == "vertex":
  55. try:
  56. return PlyParser.recarray_to_array(element.data[["texture_u", "texture_v"]], np.float32)
  57. except ValueError:
  58. break
  59. raise KeyError("No field vertex with texture coord 'texture_u' and 'texture_v' in ply file.")
  60.  
  61. def get_faces(self):
  62. for element in self.data.elements:
  63. if element.name == "face":
  64. try:
  65. faces_object = element.data["vertex_indices"]
  66. except ValueError:
  67. break
  68. # take for granted that all faces are of same lenght
  69. faces = np.ndarray((len(faces_object), len(faces_object[0])), dtype=np.uint32)
  70. for i, face in enumerate(faces_object):
  71. faces[i, :] = face
  72. return faces
  73. raise KeyError("No field face with vertex_indices.")
  74.  
  75. @staticmethod
  76. def recarray_to_array(array, type):
  77. return array.view(type).reshape(array.shape + (-1,))
  78.  
  79. @staticmethod
  80. def save_points(points, path):
  81. vertex = np.zeros(points.shape[0], dtype=([('x', 'f4'), ('y', 'f4'), ('z', 'f4')]))
  82. vertex.fill(255)
  83. vertex['x'] = points[:, 0]
  84. vertex['y'] = points[:, 1]
  85. vertex['z'] = points[:, 2]
  86. el = PlyElement.describe(vertex, 'vertex')
  87. PlyData([el], text=ascii).write(path)
Add Comment
Please, Sign In to add comment