def make_points(vertices, colors=None, texture_coords=None, geom=None): """Make or update existing points set geometry. Arguments: root_path {str} -- path to the group's root node name {str} -- node name within a group vertices {list} -- point coordinates (and other data in a point cloud format) Keyword Arguments: colors {list} -- colors (default: {None}) texture_coords {list} -- texture coordinates (default: {None}) geom {Geom} -- geometry to update (default: {None}) Returns: Geom -- p3d geometry """ if not isinstance(vertices, np.ndarray): vertices = np.asarray(vertices, dtype=np.float32) if colors is not None: if not isinstance(colors, np.ndarray): colors = np.asarray(colors) if colors.dtype != np.uint8: colors = np.uint8(colors * 255) vertices = np.column_stack(( vertices.view(dtype=np.uint32).reshape(-1, 3), colors.view(dtype=np.uint32))) if texture_coords is not None: if not isinstance(texture_coords, np.ndarray): texture_coords = np.asarray(texture_coords) vertices = np.column_stack(( vertices.view(dtype=np.uint32).reshape(-1, 3), texture_coords.view(dtype=np.uint32).reshape(-1, 2))) data = vertices.tostring() if geom is None: if vertices.strides[0] == 12: vformat = GeomVertexFormat.get_v3() elif vertices.strides[0] == 16: vformat = GeomVertexFormat.get_v3c4() elif vertices.strides[0] == 20: vformat = GeomVertexFormat.get_v3t2() else: raise ViewerError('Incompatible point clout format: {},{}'.format( vertices.dtype, vertices.shape)) vdata = GeomVertexData('vdata', vformat, Geom.UHDynamic) vdata.unclean_set_num_rows(len(vertices)) vdata.modify_array_handle(0).set_subdata(0, len(data), data) prim = GeomPoints(Geom.UHDynamic) prim.clear_vertices() prim.add_consecutive_vertices(0, len(vertices)) prim.close_primitive() geom = Geom(vdata) geom.add_primitive(prim) else: vdata = geom.modify_vertex_data() vdata.unclean_set_num_rows(len(vertices)) vdata.modify_array_handle(0).set_subdata(0, len(data), data) prim = geom.modify_primitive(0) prim.clear_vertices() prim.add_consecutive_vertices(0, len(vertices)) prim.close_primitive() return geom
def __make_points(vertices, colours=None, geom: Geom = None) -> Geom: """ This function is largely inspired by panda3d_viewer's implementation. Copyright (c) 2020, Igor Kalevatykh Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ if not isinstance(vertices, np.ndarray): vertices = np.asarray(vertices, dtype=np.float32) if colours is not None: if not isinstance(colours, np.ndarray): colours = np.asarray(colours) if colours.dtype != np.uint8: colours = np.uint8(colours * 255) vertices = np.column_stack((vertices.view(dtype=np.uint32).reshape(-1, 3), colours.view(dtype=np.uint32))) data = vertices.tostring() if geom is None: if vertices.strides[0] == 12: vformat = GeomVertexFormat.get_v3() elif vertices.strides[0] == 16: vformat = GeomVertexFormat.get_v3c4() else: raise ValueError('Incompatible point clout format: {},{}'.format(vertices.dtype, vertices.shape)) vdata = GeomVertexData('vdata', vformat, Geom.UHDynamic) vdata.unclean_set_num_rows(len(vertices)) vdata.modify_array_handle(0).set_subdata(0, len(data), data) prim = GeomPoints(Geom.UHDynamic) prim.clear_vertices() prim.add_consecutive_vertices(0, len(vertices)) prim.close_primitive() geom = Geom(vdata) geom.add_primitive(prim) else: vdata = geom.modify_vertex_data() vdata.unclean_set_num_rows(len(vertices)) vdata.modify_array_handle(0).set_subdata(0, len(data), data) prim = geom.modify_primitive(0) prim.clear_vertices() prim.add_consecutive_vertices(0, len(vertices)) prim.close_primitive() return geom