C++ Repair/Clean mesh

Hi,

I would like to ask two questions:

  1. Is it possible to bake an invalid mesh, because this method would not bake invalid mesh?

         context.m_doc.AddMeshObject(mesh);
         context.m_doc.Redraw();
    
  2. What do I need minimally to create a valid mesh from invalid mesh?
    I was testing multiple commands in C++ Rhino SDK and took methods that seemed to do smth with good to repair the mesh. But I would like to ask how to do it properly?

    if(!mesh.IsValid()){
                 //mesh.Compact();
                 mesh.CombineIdenticalVertices(true,true);
                 mesh.CullUnusedVertices();
                 mesh.Cleanup(true);
                 mesh.CullDegenerateFaces();
                 mesh.CullDegenerates();
                 mesh.CullUnusedVertices();
    
                 ON_Mesh meshRebuilt(mesh.FaceCount(), mesh.VertexCount(), false,  false);
    
                for(int i = 0; i < mesh.VertexCount(); i++)
                     meshRebuilt.SetVertex(i,mesh.Vertex(i) );
    
                for(int i = 0; i < mesh.FaceCount(); i++){
                    auto f = mesh.m_F[i];
    
                    int a = f.vi[0];
                    int b = f.vi[1];
                    int c = f.vi[2];
                    
                    if (a != b && a != c && b != c)  
                          meshRebuilt.SetTriangle(i, a, b, c);
                 }
    
                 context.m_doc.AddMeshObject(meshRebuilt);
                 context.m_doc.Redraw();
    
                 print = (double)meshRebuilt.VertexCount();
                 RhinoApp().Print(L"Cockroach: Number of Vertices = %g\n", print);
                 print = (double)meshRebuilt.FaceCount();
                 RhinoApp().Print(L"Cockroach: Number of Faces = %g\n", print);
                 print = (double)meshRebuilt.IsValid(NULL);
                 RhinoApp().Print(L"Cockroach: ISValid = %g\n", print);
             }
    

Hi @Petras_Vestartas,

No, it is not possible to add a mesh to Rhino that does not pass the ON_Mesh::IsValid test.

Also, rather than shooting at the problem, you might determine why the mesh is invalid and then take action to correct it.

ON_wString str;
ON_TextLog log(str);
if (!mesh.IsValid(&log))
{
  RhinoApp().Print(static_cast<const wchar_t*>(str));
  return CRhinoCommand::failure;
}

You can also use RhinoCheckMesh, which is used by the MeshRepair command. See rhinoSdkMeshUtilities.h for details.

ā€“ Dale

1 Like

Thank you this gives information what is wrong.
I get this info:
ON_Mesh.m_F[6939] has degenerate double precision vertex locations.

When does this issue happen? When points are coincident?

Hi @Petras_Vestartas,

A degenerate face is a face with two vertex indices that are identical, and therefore no geometric area and no normal.

ā€“ Dale

Thank you.
This seems to make clean the mesh.

    mesh.CullUnusedVertices();