diff --git a/point_cloud_utils/_mesh_io.py b/point_cloud_utils/_mesh_io.py index 648bbfd..92bf082 100644 --- a/point_cloud_utils/_mesh_io.py +++ b/point_cloud_utils/_mesh_io.py @@ -250,8 +250,11 @@ def save(self, filename, dtype=np.float32): # Handle RBG colors by just concatenating alpha=1 vcolors = self.vertex_data.colors if vcolors.shape[-1] == 3 and len(vcolors.shape) == 2: - vcolors = np.concatenate([np.ascontiguousarray(self.vertex_data.colors), - np.ones([vcolors.shape[0], 1], dtype=vcolors.dtype)], axis=-1) + if vcolors.dtype == np.uint8 or vcolors.dtype == np.int8: + alphas = np.full([vcolors.shape[0], 1], 255, dtype=vcolors.dtype) + else: + alphas = np.ones([vcolors.shape[0], 1], dtype=vcolors.dtype) + vcolors = np.concatenate([np.ascontiguousarray(self.vertex_data.colors), alphas], axis=-1) fcolors = self.face_data.colors if fcolors.shape[-1] == 3 and len(fcolors.shape) == 2: @@ -263,8 +266,11 @@ def save(self, filename, dtype=np.float32): wcolors = self.face_data.wedge_colors if wcolors.shape[-1] == 3 and len(wcolors.shape) == 3: - wcolors = np.concatenate([np.ascontiguousarray(wcolors), - np.ones([wcolors.shape[0], wcolors.shape[1], 1], dtype=wcolors.dtype)], axis=-1) + if wcolors.dtype == np.uint8 or wcolors.dtype == np.int8: + alphas = np.full([wcolors.shape[0], wcolors.shape[1], 1], 255, dtype=wcolors.dtype) + else: + alphas = np.ones([wcolors.shape[0], wcolors.shape[1], 1], dtype=wcolors.dtype) + wcolors = np.concatenate([np.ascontiguousarray(wcolors), alphas], axis=-1) if fcolors.shape[0] > 0: if fcolors.dtype == np.uint8: @@ -286,7 +292,7 @@ def save(self, filename, dtype=np.float32): np.ascontiguousarray(self.vertex_data.positions.astype(dtype)), np.ascontiguousarray(self.vertex_data.normals.astype(dtype)), np.ascontiguousarray(self.vertex_data.texcoords.astype(dtype)), - np.ascontiguousarray(vcolors.astype(dtype)), + np.ascontiguousarray((vcolors * 255.0).astype(np.uint8)), np.ascontiguousarray(self.vertex_data.quality.astype(dtype)), np.ascontiguousarray(self.vertex_data.radius.astype(dtype)), np.ascontiguousarray(self.vertex_data.tex_ids.astype(np.int32)), @@ -294,11 +300,11 @@ def save(self, filename, dtype=np.float32): np.ascontiguousarray(self.face_data.vertex_ids.astype(np.int32)), np.ascontiguousarray(self.face_data.normals.astype(dtype)), - np.ascontiguousarray(fcolors.astype(dtype)), + np.ascontiguousarray((fcolors * 255.0).astype(np.uint8)), np.ascontiguousarray(self.face_data.quality.astype(dtype)), np.ascontiguousarray(self.face_data.flags.astype(np.int32)), - np.ascontiguousarray(wcolors.astype(dtype)), + np.ascontiguousarray((wcolors * 255.0).astype(np.uint8)), np.ascontiguousarray(self.face_data.wedge_normals.astype(dtype)), np.ascontiguousarray(self.face_data.wedge_texcoords.astype(dtype)), np.ascontiguousarray(self.face_data.wedge_tex_ids.astype(np.int32)), diff --git a/src/common/ply_loader.h b/src/common/ply_loader.h index a5bc402..765f806 100644 --- a/src/common/ply_loader.h +++ b/src/common/ply_loader.h @@ -373,7 +373,7 @@ void save_mesh_ply(std::string filename, bool has_v_positions = assert_shape_and_dtype(v_positions, "v_positions", dtype_f, {-num_vertices, 3}); bool has_v_normals = assert_shape_and_dtype(v_normals, "v_normals", dtype_f, {-num_vertices, 3}); bool has_v_texcoords = assert_shape_and_dtype(v_texcoords, "v_texcoords", dtype_f, {-num_vertices, 2}); - bool has_v_colors = assert_shape_and_dtype(v_colors, "v_colors", dtype_f, {-num_vertices, 4}); + bool has_v_colors = assert_shape_and_dtype(v_colors, "v_colors", pybind11::dtype::of(), {-num_vertices, 4}); bool has_v_quality = assert_shape_and_dtype(v_quality, "v_quality", dtype_f, {-num_vertices}); bool has_v_radius = assert_shape_and_dtype(v_radius, "v_radius", dtype_f, {-num_vertices}); bool has_v_texids = assert_shape_and_dtype(v_texids, "v_texids", dtype_i, {-num_vertices}); @@ -381,7 +381,7 @@ void save_mesh_ply(std::string filename, bool has_f_vertex_ids = assert_shape_and_dtype(f_vertex_ids, "f_vertex_ids", dtype_i, {-num_faces, 3}); bool has_f_normals = assert_shape_and_dtype(f_normals, "f_normals", dtype_f, {-num_faces, 3}); - bool has_f_colors = assert_shape_and_dtype(f_colors, "f_colors", dtype_f, {-num_faces, 4}); + bool has_f_colors = assert_shape_and_dtype(f_colors, "f_colors", pybind11::dtype::of(), {-num_faces, 4}); bool has_f_quality = assert_shape_and_dtype(f_quality, "f_quality", dtype_f, {-num_faces}); bool has_f_flags = assert_shape_and_dtype(f_flags, "f_flags", dtype_i, {-num_faces}); @@ -417,7 +417,7 @@ void save_mesh_ply(std::string filename, } if (has_v_colors) { plyf.add_properties_to_element( - "vertex", { "red", "green", "blue", "alpha" }, ply_type_f, num_vertices, + "vertex", { "red", "green", "blue", "alpha" }, tinyply::Type::UINT8, num_vertices, reinterpret_cast(v_colors.mutable_data()), tinyply::Type::INVALID, 0); } if (has_v_quality) { @@ -485,7 +485,7 @@ void save_mesh_ply(std::string filename, } if (has_f_colors) { plyf.add_properties_to_element( - "face", { "red", "green", "blue", "alpha" }, ply_type_f, num_faces, + "face", { "red", "green", "blue", "alpha" }, tinyply::Type::UINT8, num_faces, reinterpret_cast(f_colors.mutable_data()), tinyply::Type::INVALID, 0); } if (has_f_quality) {