Search code examples
c++geometrycomputational-geometryopenmesh

How to delete faces from mesh using openmesh?


Pretty much the title I am trying to delete a few faces of a mesh using open mesh, like this:


    MyMesh mesh;

    char fname[1024];
    sprintf(fname, "box_%i.obj", 0);
    if (!OpenMesh::IO::read_mesh(mesh, std::string(fname)))
    {
        std::cerr << "read error\n";
        exit(1);
    }

    MyMesh::FaceIter v_it, v_end(mesh.faces_end());
    uint count = 0;
    for (v_it=mesh.faces_begin(); v_it!=v_end; ++v_it)
    {
        mesh.delete_face(*v_it, true);
    }

This is segfaulting on the first call to delete_face.

However, writing this mesh (without trying to delete faces):

    if (!OpenMesh::IO::write_mesh(mesh, std::string("open_mesh.obj")))
    {
        std::cerr << "write error\n";
        exit(1);
    }

Works perfectly fine and blender can open the obj. So the issue very much seems to be with how I am trying to delete the faces.

The docs don;t seem to provide any explanation as to why this is not valid: https://www.graphics.rwth-aachen.de/media/openmesh_static/Documentations/OpenMesh-Doc-Latest/a02618.html#ae20c4e746b52c34ace6a7b805fbd61ac


Solution

  • I think iterator being invalid after removing an element in it.

    for (v_it=mesh.faces_begin(); v_it!=v_end; ++v_it)
    {
        mesh.delete_face(*v_it, true);
        v_it = mesh.faces_begin(); //<- add this to test
    }