Skip to content

Commit

Permalink
Remove unused functions in 3mf reader after refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lvk88 committed Aug 19, 2024
1 parent 3eb2147 commit 3650f7f
Showing 1 changed file with 0 additions and 184 deletions.
184 changes: 0 additions & 184 deletions src/meshlabplugins/io_3mf/io_3mf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,189 +111,6 @@ std::map<std::string, QImage> load_textures(const Lib3MF::PModel& model)
return result;
}

std::map<int, CMeshO> load_meshes(const Lib3MF::PModel& model)
{
std::map<int, CMeshO> result;
if (model == nullptr) {
throw std::runtime_error("Got a null model!");
}

auto meshes = model->GetMeshObjects();
if (meshes == nullptr) {
throw std::runtime_error("Could not access mesh iterators!");
}

while (meshes->MoveNext()) {
auto current_mesh = meshes->GetCurrentMeshObject();
if (current_mesh == nullptr) {
throw std::runtime_error("Error accessing mesh from iterator");
}

auto id = current_mesh->GetUniqueResourceID();
CMeshO cmesh;

auto n_vertices = current_mesh->GetVertexCount();
auto n_triangles = current_mesh->GetTriangleCount();

auto vertex_iterator = vcg::tri::Allocator<decltype(cmesh)>::AddVertices(cmesh, n_vertices);
auto face_iterator = vcg::tri::Allocator<decltype(cmesh)>::AddFaces(cmesh, n_triangles);

for (size_t i = 0; i < n_vertices; ++i) {
const auto& pos = current_mesh->GetVertex(i).m_Coordinates;
(*vertex_iterator).P()[0] = pos[0];
(*vertex_iterator).P()[1] = pos[1];
(*vertex_iterator).P()[2] = pos[2];
++vertex_iterator;
}

for (size_t i = 0; i < n_triangles; ++i) {
const auto& tri = current_mesh->GetTriangle(i).m_Indices;
(*face_iterator).V(0) = &cmesh.vert[tri[0]];
(*face_iterator).V(1) = &cmesh.vert[tri[1]];
(*face_iterator).V(2) = &cmesh.vert[tri[2]];
++face_iterator;
}

result.insert({id, cmesh});
}

return result;
}

void load_mesh_to_meshmodel(
const Lib3MF::PModel& model,
const Lib3MF::PMeshObject& lib3mf_mesh_object,
MeshModel& mesh_model,
const std::string& name_postfix,
const std::map<std::string, QImage>& textures,
bool load_material_data)
{
auto load_or_get_texture_id = [&mesh_model, &model, &textures](const int id) {
const std::string string_id = std::to_string(id);

// If the mesh model doesn't have this texture yet, load it
if (mesh_model.getTexture(string_id).isNull()) {
if (textures.find(string_id) == textures.end()) {
throw std::runtime_error(
"One of the meshes uses a texture that does not exist in the model!");
}
auto texture = textures.at(string_id);
mesh_model.addTexture(string_id, texture);
}

auto texture_id = std::distance(
mesh_model.cm.textures.begin(),
std::find(mesh_model.cm.textures.begin(), mesh_model.cm.textures.end(), string_id));
return texture_id;
};

std::vector<size_t> available_property_ids;

auto base_material_groups = model->GetBaseMaterialGroups();
while (base_material_groups->MoveNext()) {
auto current = base_material_groups->GetCurrentBaseMaterialGroup();
available_property_ids.push_back(current->GetUniqueResourceID());
}

auto color_groups = model->GetColorGroups();
while (color_groups->MoveNext()) {
auto current = color_groups->GetCurrentColorGroup();
available_property_ids.push_back(current->GetUniqueResourceID());
}

auto texture_groups = model->GetTexture2DGroups();
while (texture_groups->MoveNext()) {
auto current = texture_groups->GetCurrentTexture2DGroup();
available_property_ids.push_back(current->GetUniqueResourceID());
}

std::string mesh_name = lib3mf_mesh_object->GetName();
if (mesh_name.empty()) {
mesh_name = "Mesh" + name_postfix;
}
mesh_model.setLabel(QString::fromStdString(mesh_name));
const auto n_vertices = lib3mf_mesh_object->GetVertexCount();
const auto n_triangles = lib3mf_mesh_object->GetTriangleCount();
auto vertex_iterator =
vcg::tri::Allocator<decltype(mesh_model.cm)>::AddVertices(mesh_model.cm, n_vertices);
auto face_iterator =
vcg::tri::Allocator<decltype(mesh_model.cm)>::AddFaces(mesh_model.cm, n_triangles);
for (size_t i = 0; i < n_vertices; ++i) {
const auto& pos = lib3mf_mesh_object->GetVertex(i).m_Coordinates;
(*vertex_iterator).P()[0] = pos[0];
(*vertex_iterator).P()[1] = pos[1];
(*vertex_iterator).P()[2] = pos[2];
++vertex_iterator;
}

// Load the triangles only, but no colors yet
for (size_t i = 0; i < n_triangles; ++i) {
const auto& tri = lib3mf_mesh_object->GetTriangle(i).m_Indices;
(*face_iterator).V(0) = &mesh_model.cm.vert[tri[0]];
(*face_iterator).V(1) = &mesh_model.cm.vert[tri[1]];
(*face_iterator).V(2) = &mesh_model.cm.vert[tri[2]];
++face_iterator;
}

// Load colors or textures, if necessary

if (load_material_data) {
for (size_t i = 0; i < n_triangles; ++i) {
Lib3MF::sTriangleProperties props;
lib3mf_mesh_object->GetTriangleProperties(i, props);

if (std::find_if(
available_property_ids.begin(),
available_property_ids.end(),
[props](auto resourceId) { return resourceId == props.m_ResourceID; }) !=
available_property_ids.end()) {
const auto& tri = lib3mf_mesh_object->GetTriangle(i).m_Indices;
switch (model->GetPropertyTypeByID(props.m_ResourceID)) {
case Lib3MF::ePropertyType::BaseMaterial: {
mesh_model.enable(vcg::tri::io::Mask::IOM_FACECOLOR);
auto baseMaterial = model->GetBaseMaterialGroupByID(props.m_ResourceID);
auto color = baseMaterial->GetDisplayColor(props.m_PropertyIDs[0]);
mesh_model.cm.face[i].C() =
vcg::Color4b {color.m_Red, color.m_Green, color.m_Blue, color.m_Alpha};
break;
}
case Lib3MF::ePropertyType::TexCoord: {
mesh_model.enable(vcg::tri::io::Mask::IOM_WEDGTEXCOORD);
auto group = model->GetTexture2DGroupByID(props.m_ResourceID);
auto texture_id =
load_or_get_texture_id(group->GetTexture2D()->GetUniqueResourceID());
auto coord0 = group->GetTex2Coord(props.m_PropertyIDs[0]);
auto coord1 = group->GetTex2Coord(props.m_PropertyIDs[1]);
auto coord2 = group->GetTex2Coord(props.m_PropertyIDs[2]);

mesh_model.cm.face[i].WT(0).U() = coord0.m_U;
mesh_model.cm.face[i].WT(0).V() = coord0.m_V;
mesh_model.cm.face[i].WT(0).N() = texture_id;

mesh_model.cm.face[i].WT(1).U() = coord1.m_U;
mesh_model.cm.face[i].WT(1).V() = coord1.m_V;
mesh_model.cm.face[i].WT(1).N() = texture_id;

mesh_model.cm.face[i].WT(2).U() = coord2.m_U;
mesh_model.cm.face[i].WT(2).V() = coord2.m_V;
mesh_model.cm.face[i].WT(2).N() = texture_id;
break;
}
case Lib3MF::ePropertyType::Colors: {
mesh_model.enable(vcg::tri::io::Mask::IOM_FACECOLOR);
auto colorGroup = model->GetColorGroupByID(props.m_ResourceID);
auto color0 = colorGroup->GetColor(props.m_PropertyIDs[0]);
mesh_model.cm.face[i].C() =
vcg::Color4b {color0.m_Red, color0.m_Green, color0.m_Blue, color0.m_Alpha};
break;
}
default: break;
};
}
}
}
}

} // namespace

Lib3MFPlugin::Lib3MFPlugin()
Expand Down Expand Up @@ -561,7 +378,6 @@ void Lib3MFPlugin::open(
auto build_item_iterator = get_build_item_iterator(lib3mf_model);
auto mesh_model_iterator = meshModelList.begin();
auto textures = load_textures(lib3mf_model);
auto meshes = load_meshes(lib3mf_model);

auto build_item_count = 0;

Expand Down

0 comments on commit 3650f7f

Please sign in to comment.