Skip to content

Commit dee37e4

Browse files
committed
meshlab vertex colors for ply
1 parent 387b84f commit dee37e4

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

point_cloud_utils/_mesh_io.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,11 @@ def save(self, filename, dtype=np.float32):
250250
# Handle RBG colors by just concatenating alpha=1
251251
vcolors = self.vertex_data.colors
252252
if vcolors.shape[-1] == 3 and len(vcolors.shape) == 2:
253-
vcolors = np.concatenate([np.ascontiguousarray(self.vertex_data.colors),
254-
np.ones([vcolors.shape[0], 1], dtype=vcolors.dtype)], axis=-1)
253+
if vcolors.dtype == np.uint8 or vcolors.dtype == np.int8:
254+
alphas = np.full([vcolors.shape[0], 1], 255, dtype=vcolors.dtype)
255+
else:
256+
alphas = np.ones([vcolors.shape[0], 1], dtype=vcolors.dtype)
257+
vcolors = np.concatenate([np.ascontiguousarray(self.vertex_data.colors), alphas], axis=-1)
255258

256259
fcolors = self.face_data.colors
257260
if fcolors.shape[-1] == 3 and len(fcolors.shape) == 2:
@@ -263,8 +266,11 @@ def save(self, filename, dtype=np.float32):
263266

264267
wcolors = self.face_data.wedge_colors
265268
if wcolors.shape[-1] == 3 and len(wcolors.shape) == 3:
266-
wcolors = np.concatenate([np.ascontiguousarray(wcolors),
267-
np.ones([wcolors.shape[0], wcolors.shape[1], 1], dtype=wcolors.dtype)], axis=-1)
269+
if wcolors.dtype == np.uint8 or wcolors.dtype == np.int8:
270+
alphas = np.full([wcolors.shape[0], wcolors.shape[1], 1], 255, dtype=wcolors.dtype)
271+
else:
272+
alphas = np.ones([wcolors.shape[0], wcolors.shape[1], 1], dtype=wcolors.dtype)
273+
wcolors = np.concatenate([np.ascontiguousarray(wcolors), alphas], axis=-1)
268274

269275
if fcolors.shape[0] > 0:
270276
if fcolors.dtype == np.uint8:
@@ -286,19 +292,19 @@ def save(self, filename, dtype=np.float32):
286292
np.ascontiguousarray(self.vertex_data.positions.astype(dtype)),
287293
np.ascontiguousarray(self.vertex_data.normals.astype(dtype)),
288294
np.ascontiguousarray(self.vertex_data.texcoords.astype(dtype)),
289-
np.ascontiguousarray(vcolors.astype(dtype)),
295+
np.ascontiguousarray((vcolors * 255.0).astype(np.uint8)),
290296
np.ascontiguousarray(self.vertex_data.quality.astype(dtype)),
291297
np.ascontiguousarray(self.vertex_data.radius.astype(dtype)),
292298
np.ascontiguousarray(self.vertex_data.tex_ids.astype(np.int32)),
293299
np.ascontiguousarray(self.vertex_data.flags.astype(np.int32)),
294300

295301
np.ascontiguousarray(self.face_data.vertex_ids.astype(np.int32)),
296302
np.ascontiguousarray(self.face_data.normals.astype(dtype)),
297-
np.ascontiguousarray(fcolors.astype(dtype)),
303+
np.ascontiguousarray((fcolors * 255.0).astype(np.uint8)),
298304
np.ascontiguousarray(self.face_data.quality.astype(dtype)),
299305
np.ascontiguousarray(self.face_data.flags.astype(np.int32)),
300306

301-
np.ascontiguousarray(wcolors.astype(dtype)),
307+
np.ascontiguousarray((wcolors * 255.0).astype(np.uint8)),
302308
np.ascontiguousarray(self.face_data.wedge_normals.astype(dtype)),
303309
np.ascontiguousarray(self.face_data.wedge_texcoords.astype(dtype)),
304310
np.ascontiguousarray(self.face_data.wedge_tex_ids.astype(np.int32)),

src/common/ply_loader.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -373,15 +373,15 @@ void save_mesh_ply(std::string filename,
373373
bool has_v_positions = assert_shape_and_dtype(v_positions, "v_positions", dtype_f, {-num_vertices, 3});
374374
bool has_v_normals = assert_shape_and_dtype(v_normals, "v_normals", dtype_f, {-num_vertices, 3});
375375
bool has_v_texcoords = assert_shape_and_dtype(v_texcoords, "v_texcoords", dtype_f, {-num_vertices, 2});
376-
bool has_v_colors = assert_shape_and_dtype(v_colors, "v_colors", dtype_f, {-num_vertices, 4});
376+
bool has_v_colors = assert_shape_and_dtype(v_colors, "v_colors", pybind11::dtype::of<std::uint8_t>(), {-num_vertices, 4});
377377
bool has_v_quality = assert_shape_and_dtype(v_quality, "v_quality", dtype_f, {-num_vertices});
378378
bool has_v_radius = assert_shape_and_dtype(v_radius, "v_radius", dtype_f, {-num_vertices});
379379
bool has_v_texids = assert_shape_and_dtype(v_texids, "v_texids", dtype_i, {-num_vertices});
380380
bool has_v_flags = assert_shape_and_dtype(v_flags, "v_flags", dtype_i, {-num_vertices});
381381

382382
bool has_f_vertex_ids = assert_shape_and_dtype(f_vertex_ids, "f_vertex_ids", dtype_i, {-num_faces, 3});
383383
bool has_f_normals = assert_shape_and_dtype(f_normals, "f_normals", dtype_f, {-num_faces, 3});
384-
bool has_f_colors = assert_shape_and_dtype(f_colors, "f_colors", dtype_f, {-num_faces, 4});
384+
bool has_f_colors = assert_shape_and_dtype(f_colors, "f_colors", pybind11::dtype::of<std::uint8_t>(), {-num_faces, 4});
385385
bool has_f_quality = assert_shape_and_dtype(f_quality, "f_quality", dtype_f, {-num_faces});
386386
bool has_f_flags = assert_shape_and_dtype(f_flags, "f_flags", dtype_i, {-num_faces});
387387

@@ -417,7 +417,7 @@ void save_mesh_ply(std::string filename,
417417
}
418418
if (has_v_colors) {
419419
plyf.add_properties_to_element(
420-
"vertex", { "red", "green", "blue", "alpha" }, ply_type_f, num_vertices,
420+
"vertex", { "red", "green", "blue", "alpha" }, tinyply::Type::UINT8, num_vertices,
421421
reinterpret_cast<std::uint8_t*>(v_colors.mutable_data()), tinyply::Type::INVALID, 0);
422422
}
423423
if (has_v_quality) {
@@ -485,7 +485,7 @@ void save_mesh_ply(std::string filename,
485485
}
486486
if (has_f_colors) {
487487
plyf.add_properties_to_element(
488-
"face", { "red", "green", "blue", "alpha" }, ply_type_f, num_faces,
488+
"face", { "red", "green", "blue", "alpha" }, tinyply::Type::UINT8, num_faces,
489489
reinterpret_cast<std::uint8_t*>(f_colors.mutable_data()), tinyply::Type::INVALID, 0);
490490
}
491491
if (has_f_quality) {

0 commit comments

Comments
 (0)