Skip to content

Commit

Permalink
meshlab vertex colors for ply
Browse files Browse the repository at this point in the history
  • Loading branch information
fwilliams committed Aug 26, 2024
1 parent 387b84f commit dee37e4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
20 changes: 13 additions & 7 deletions point_cloud_utils/_mesh_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -286,19 +292,19 @@ 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)),
np.ascontiguousarray(self.vertex_data.flags.astype(np.int32)),

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)),
Expand Down
8 changes: 4 additions & 4 deletions src/common/ply_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,15 +373,15 @@ 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<std::uint8_t>(), {-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});
bool has_v_flags = assert_shape_and_dtype(v_flags, "v_flags", dtype_i, {-num_vertices});

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<std::uint8_t>(), {-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});

Expand Down Expand Up @@ -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<std::uint8_t*>(v_colors.mutable_data()), tinyply::Type::INVALID, 0);
}
if (has_v_quality) {
Expand Down Expand Up @@ -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<std::uint8_t*>(f_colors.mutable_data()), tinyply::Type::INVALID, 0);
}
if (has_f_quality) {
Expand Down

0 comments on commit dee37e4

Please sign in to comment.