在知道连通性的情况下,在boost邻接图中构建相邻顶点

Build neighboring vertices in a boost adjacency graph knowing the connectivity

本文关键字:构建 顶点 boost 连通性 情况下      更新时间:2023-10-16

我对boost图库有问题。

在初始化步骤,我想在这个邻接图中存储一个图像:

typedef boost::adjacency_list<boost::vecS,
                              boost::vecS,
                              boost::undirectedS,
                              boost::property<boost::vertex_bundle_t, unsigned int>,
                              boost::property<boost::edge_bundle_t, float>,
                              boost::no_property> GraphType

我做了第一个循环,为图像的每个像素创建一个顶点(使用OTB库来处理图像,in_iter只是图像像素上的迭代器):

unsigned long int curr_id = 0;
for(in_iter.GoToBegin(); !in_iter.IsAtEnd(); ++in_iter)
{
    boost::add_vertex(curr_id, *_graph);
    curr_id++;
}

现在我想为顶点创建它与邻居的连接(4连接):我试过了,但不起作用:

curr_id = 0;
long int neighbors[4];
auto iterBounds = boost::vertices(*_graph);
for(auto v_iter = iterBounds.first; v_iter != iterBounds.second; v_iter++)
{
  FindNeighboring(curr_id, neighbors, rows, cols);
  for(short j = 0; j<4; j++)
  { 
    if(neighbors[j] > -1)
    {
      boost::add_vertex(*_graph->m_vertices[curr_id], 
                        *_graph->m_vertices[neighbors[j]],
                        std::numeric_limits<float>::max();
                        *_graph);
    }
  }
}

我想访问顶点描述符,知道它的位置。有可能在BGL中做到这一点吗?

谢谢你的帮助!

答案是"是和否"。

如果您对图使用通用adjacency_list,那么,当然,您必须自己维护对应关系<coords> <--> <vertex>。您可以将其保留为boost提供的映射::unordered_map<点,vertex_descriptor>或作为boost::bimap,甚至std::map。填充地图并使其与图形保持一致将是您的责任。

或者,您可以考虑BGL内置类grid_graph。它为您提供了从坐标到顶点描述符的映射:

// Get the vertex associated with vertex_index
Traits::vertex_descriptor
vertex(Traits::vertices_size_type vertex_index,
       const Graph& graph);
// Get the index associated with vertex
Traits::vertices_size_type
get(boost::vertex_index_t,
    const Graph& graph,
    Traits::vertex_descriptor vertex);

(以及用于边的类似功能)。

在这里,您不需要创建单独的顶点,它们是在您指定图的维度时由图构造函数中的BGL创建的。

您可能希望使用某种自定义特性映射将像素与每个顶点相关联,但这是一个单独的故事。