具有自定义顶点和边的Boost Graph
Boost Graph with custom vertices and edges
我正在创建一个具有自己的节点和边属性的自定义提升图。我将图形定义如下:
class NavGraph : public boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS, NodeInfo, EdgeInfo > {
public:
typedef boost::graph_traits<NavGraph>::vertex_descriptor Vertex;
typedef boost::graph_traits<NavGraph>::edge_descriptor Edge;
NavGraph(){}
void init(const std::vector<NodeInfo>& nodes, const std::vector<EdgeInfo>& edges);
}
我现在要做的是从节点和边属性列表(在init方法中)初始化这个图。NodeInfo和EdgeInfo是具有基元类型的简单结构。到目前为止,我已经这样做了:
void NavGraph::init(const std::vector<NodeInfo>& nodes, const std::vector<EdgeInfo>& edges){
//Add the Vertices
for (std::vector<NodeInfo>::const_iterator it = nodes.begin() ; it != nodes.end(); ++it){
Vertex u = boost::add_vertex(*this);
(*this)[u].nodeIndex = (*it).nodeIndex;
(*this)[u].x = (*it).x;
(*this)[u].y = (*it).y;
(*this)[u].z = (*it).z;
}
//Add the edges
for (std::vector<EdgeInfo>::const_iterator it = edges.begin() ; it != edges.end(); ++it){
//To be implemented
}
}
因此,我设法添加顶点并设置属性(希望是正确的)。现在,每个EdgeInfo都有一个源id和目标id,用于标识边缘的两个节点。问题是,我需要从图中按Id检索它们(使用之前设置的nodeIndex属性),这样我就可以调用add_edge方法。我真的不知道该怎么做。希望你们能帮助我。
从顶部开始:专门化邻接列表并添加自己的方法不是一个好主意。
相反,您应该创建boost::adjacety_list类的一个实例,作为一个新类的成员,然后可以为其编写方法。
class cNavGraph {
public:
boost::adjacency_list < boost::vecS,
boost::vecS,
boost::undirectedS,
NodeInfo, EdgeInfo > myNavGraph;
...
现在,要通过顶点属性nodeIndex从图中检索顶点,您有两个选项:
在顶点中搜索所需的nodeIndex。这很简单,但如果图形很大,则会很慢。
添加顶点时创建从nodeIndex到顶点的映射,然后在需要从nodeIndex获取顶点时在映射上进行查找。这需要更多的代码和内存,但对于大型图来说会更快。
作为一个更激进的建议,我建议重新设计一些东西,使NodeIndex和顶点描述符相同,比如这个
for (std::vector<EdgeInfo>::const_iterator it = edges.begin() ;
it != edges.end(); ++it)
{
add_edge( it->first_node_index,
it->second_node_index,
myGraph );
请注意,在回复您的评论时,对add_edge的调用将自动添加两个顶点(如果它们还不存在),顶点描述符等于指定的节点索引。不要调用add_vertex!
添加完所有边后,可以在节点中迭代,添加属性(根据需要)。
相关文章:
- Boost Graph Library,修复节点大小
- Boost Graph库,depth_first_search未在MSVC中调用finish_edge
- 如何在Boost Graph库中的Grid_graph的边缘添加自定义属性
- 在Boost Graph库中选择给定顶点的随机进出邻居的有效方法
- 如何使用Boost Graph库使用循环中的循环设置相同的边缘重量
- Boost Graph库示例不编译
- 带标签_Graph的Boost Graph bellman_ford_shortest_path
- Boost Graph库中边缘的随机访问(或其他快速访问)
- 在 Boost.Graph 中向图形添加边
- Boost Graph Library:如何使用depth_first_visit,ColorMap的问题
- 在Boost Graph中使用find()在Boost关联属性图中使用Find()错误
- 在Fortran程序中使用Boost Graph库(BGL)
- 使用Boost :: Graph随机访问顶点
- 在Boost Graph中过滤到现有的Filtered_graph
- 在Boost Graph中定义了图形后,在构造函数中初始化了effled_graph对象
- Boost::Graph中的read_graphviz(),传递给构造函数
- 使用Boost graph库创建图形
- 在执行 DFS 时在 Boost::graph 中维护迭代器
- 从Boost Graph中删除多个顶点
- 用 boost::graph 包装我的自定义图形并计算dijkstra_shortest_paths