vendredi 13 novembre 2020

OpenMesh: fast search of common neighbor vertices

I have a function which finds the common neighbors of two vertices v1 and v2, i.e. those vertices that are connected to both v1 and v2:

std::vector<MyMesh::VertexHandle> find_common_neighbors(MyMesh & mesh, MyMesh::VertexHandle & v1, MyMesh::VertexHandle & v2){
    std::vector<MyMesh::VertexHandle> common_neighbors;

    //iterate over neighbors of v1
    for(MyMesh::VertexVertexIter it1 = mesh.vv_iter(v1); it1.is_valid(); ++it1) {
      //neighbors of v2
      for(MyMesh::VertexVertexIter it2 = mesh.vv_iter(v2); it2.is_valid(); ++it2) {
        if ((*it1)==(*it2)){
            common_neighbors.push_back(*it1);     
        }
      }
    }
    return common_neighbors;

The function simply iterates over the neighborhood of v1 and v2 and checks if finds any vertex that appears in both neighborhoods. Unfortunately, this function seems to be the bottleneck of my code, thus my question is whether there is a more optimal way to accomplish this in OpenMesh?

Aucun commentaire:

Enregistrer un commentaire