0

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.

jsb
  • 938
  • 6
  • 15
Phil333
  • 19
  • 1
  • 7
  • 1
    Welcome to Stack Overflow! Please take the [tour](http://stackoverflow.com/tour) and read [How to Ask](http://stackoverflow.com/help/how-to-ask) to learn what we expect from questions here. Please be aware that we do not provide from-scratch coding service here. Please show us what you've tried already, how it failed and we might be able to help. – Nitish Mar 23 '16 at 09:36
  • So, from what I understand with OpenMesh, I can cycle trough the Vertexes or a mesh by using a `MyMesh::VertexIter`. To cycle trought he neighbourig Vertexes, I can use a `MyMesh::VertexVertexIter`. But I dont know how to cast a VertexVertexiter to a Vertexiter. So I gues, my actual question is, how can I get an ID out of these Vertexes, so that I can address them without these iteration tools? – Phil333 Mar 23 '16 at 11:50

1 Answers1

0

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;
}
Phil333
  • 19
  • 1
  • 7
  • 1
    Note that the `mesh` argument is passed by value causing your mesh to be copied for each call to `graphTraversal`. This will severely affect performance. You should pass `mesh` by (`const`) reference! – hc_ May 02 '16 at 17:05
  • Ye, that makes sense considering the amount of vertices I am iterating over. thank you for your comment. – Phil333 May 05 '16 at 14:39
  • Please consider editing your answer accordingly so that novice users who don't read the comments don't run into this pitfall. – hc_ May 06 '16 at 09:16