Search code examples
c++recursionmeshopenmesh

OpenMesh Recursive Iteration


I recently started using OpenMesh, and I need to make a recursive iteration where I access one vertex, then its neighbouring vertexes and then the neighbours of those. I also need to keep a list of vertexes which I have used already. My main problem with this is that I don't know how to get the ID of a vertex, so that i can access specific vertexes.


Solution

  • Managed to solve my problem: A handle can be selected by its ID number like this:

    MyMesh::VHandle myVertexHandle = mesh.vertex_handle(ID_number);
    

    To return the ID number from a VertexHandle use the following command:

    myVertexHandle.idx();
    

    To cycle recursively over a mesh from a starting vertex, use the following code:

    void graphTraversal(const MyMesh& mesh, MyMesh::VHandle start)
    {
    
        // print starting vertex handle
        std::cout << "Vertex " << start.idx() << std::endl;
    
    
        // retrieve XYZ of initial vertex
        OpenMesh::Vec3f pointA = mesh.point(start);
    
        for (MyMesh::VOHIter vohit = mesh.voh_iter(start); vohit.is_valid(); ++vohit)
        {
    
             MyMesh::VHandle newHandle = mesh.to_vertex_handle(*vohit);
    
             // used to retrive point X Y Z positions
             OpenMesh::Vec3f point = mesh.point(newHandle);
    
             // print out neighbouring vertex x y z position
             std:cout << point[0] << " " << point[1] << " " << point[2] << std::endl;
    
             // call the recursive function from the new vertex
             graphTraversal(mesh, newHandle );
    
        }
    
        return;
    }