Skip to content

Commit

Permalink
meshlab is a piece of shit
Browse files Browse the repository at this point in the history
  • Loading branch information
fwilliams committed Dec 8, 2023
1 parent 5250ce4 commit 6ce5ca5
Showing 1 changed file with 61 additions and 60 deletions.
121 changes: 61 additions & 60 deletions src/common/ply_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,66 +400,6 @@ void save_mesh_ply(std::string filename,
ply_type_f = tinyply::Type::FLOAT64;
}

if (has_w_texcoords) {
plyf.add_properties_to_element(
"face", { "texcoords" }, ply_type_f, num_faces,
reinterpret_cast<std::uint8_t*>(w_texcoords.mutable_data()), tinyply::Type::UINT8, 6);
}
if (has_w_texids) {
plyf.add_properties_to_element(
"face", { "texnumber" }, ply_type_i, num_faces,
reinterpret_cast<std::uint8_t*>(w_texcoords.mutable_data()), tinyply::Type::INVALID, 0);
}


if (has_f_vertex_ids) {
plyf.add_properties_to_element(
"face", { "vertex_indices" }, ply_type_i, num_faces,
reinterpret_cast<std::uint8_t*>(f_vertex_ids.mutable_data()), tinyply::Type::UINT8, 3);
}
if (has_f_normals) {
plyf.add_properties_to_element(
"face", { "nx", "ny", "z" }, ply_type_f, num_faces,
reinterpret_cast<std::uint8_t*>(f_normals.mutable_data()), tinyply::Type::INVALID, 0);
}
if (has_f_colors) {
plyf.add_properties_to_element(
"face", { "red", "green", "blue", "alpha" }, ply_type_f, num_faces,
reinterpret_cast<std::uint8_t*>(f_colors.mutable_data()), tinyply::Type::INVALID, 0);
}
if (has_f_quality) {
plyf.add_properties_to_element(
"face", { "quality" }, ply_type_f, num_faces,
reinterpret_cast<std::uint8_t*>(f_quality.mutable_data()), tinyply::Type::INVALID, 0);
}
if (has_f_flags) {
plyf.add_properties_to_element(
"face", { "flags" }, ply_type_i, num_faces,
reinterpret_cast<std::uint8_t*>(f_flags.mutable_data()), tinyply::Type::INVALID, 0);
}
for (auto kv = custom_f_attribs.begin(); kv != custom_f_attribs.end(); kv++) {
pybind11::str key = (pybind11::str) kv->first;
pybind11::array value = pybind11::array::ensure(kv->second);
if (value.shape(0) != num_faces) {
throw pybind11::value_error("Invalid face attribute " + std::string(key) + ". Must have same number of rows as faces.");
}
size_t num_cols = 1;
for (int i = 1; i < value.ndim(); i += 1) {
num_cols *= value.shape(i);
}
if (num_cols == 0) {
throw pybind11::value_error("Invalid face attribute " + std::string(key) + " has zero elements.");
}
try {
tinyply::Type ply_dtype = dtype_to_ply_type(value.dtype());
plyf.add_properties_to_element(
"face", { key }, ply_dtype, num_vertices,
reinterpret_cast<std::uint8_t*>(value.mutable_data()), tinyply::Type::UINT8, num_cols);
} catch (const std::runtime_error& e) {
throw pybind11::value_error("Invalid dtype for custom face attribute "+ std::string(key) + ".");
}
}

if (has_v_positions) {
plyf.add_properties_to_element(
"vertex", { "x", "y", "z" }, ply_type_f, num_vertices,
Expand Down Expand Up @@ -533,6 +473,67 @@ void save_mesh_ply(std::string filename,
}


if (has_f_vertex_ids) {
plyf.add_properties_to_element(
"face", { "vertex_indices" }, ply_type_i, num_faces,
reinterpret_cast<std::uint8_t*>(f_vertex_ids.mutable_data()), tinyply::Type::UINT8, 3);
}
if (has_f_normals) {
plyf.add_properties_to_element(
"face", { "nx", "ny", "z" }, ply_type_f, num_faces,
reinterpret_cast<std::uint8_t*>(f_normals.mutable_data()), tinyply::Type::INVALID, 0);
}
if (has_f_colors) {
plyf.add_properties_to_element(
"face", { "red", "green", "blue", "alpha" }, ply_type_f, num_faces,
reinterpret_cast<std::uint8_t*>(f_colors.mutable_data()), tinyply::Type::INVALID, 0);
}
if (has_f_quality) {
plyf.add_properties_to_element(
"face", { "quality" }, ply_type_f, num_faces,
reinterpret_cast<std::uint8_t*>(f_quality.mutable_data()), tinyply::Type::INVALID, 0);
}
if (has_f_flags) {
plyf.add_properties_to_element(
"face", { "flags" }, ply_type_i, num_faces,
reinterpret_cast<std::uint8_t*>(f_flags.mutable_data()), tinyply::Type::INVALID, 0);
}
for (auto kv = custom_f_attribs.begin(); kv != custom_f_attribs.end(); kv++) {
pybind11::str key = (pybind11::str) kv->first;
pybind11::array value = pybind11::array::ensure(kv->second);
if (value.shape(0) != num_faces) {
throw pybind11::value_error("Invalid face attribute " + std::string(key) + ". Must have same number of rows as faces.");
}
size_t num_cols = 1;
for (int i = 1; i < value.ndim(); i += 1) {
num_cols *= value.shape(i);
}
if (num_cols == 0) {
throw pybind11::value_error("Invalid face attribute " + std::string(key) + " has zero elements.");
}
try {
tinyply::Type ply_dtype = dtype_to_ply_type(value.dtype());
plyf.add_properties_to_element(
"face", { key }, ply_dtype, num_vertices,
reinterpret_cast<std::uint8_t*>(value.mutable_data()), tinyply::Type::UINT8, num_cols);
} catch (const std::runtime_error& e) {
throw pybind11::value_error("Invalid dtype for custom face attribute "+ std::string(key) + ".");
}
}

if (has_w_texcoords) {
plyf.add_properties_to_element(
"face", { "texcoords" }, ply_type_f, num_faces,
reinterpret_cast<std::uint8_t*>(w_texcoords.mutable_data()), tinyply::Type::UINT8, 6);
}
if (has_w_texids) {
plyf.add_properties_to_element(
"face", { "texnumber" }, ply_type_i, num_faces,
reinterpret_cast<std::uint8_t*>(w_texcoords.mutable_data()), tinyply::Type::INVALID, 0);
}



std::filebuf fb_binary;
fb_binary.open(filename, std::ios::out | std::ios::binary);
std::ostream outstream_binary(&fb_binary);
Expand Down

0 comments on commit 6ce5ca5

Please sign in to comment.