简体   繁体   English

带有自定义顶点和边的Boost Graph

[英]Boost Graph with custom vertices and edges

I'm creating a custom boost graph with my own node and edges properties. 我正在使用自己的node和edges属性创建一个自定义增强图。 I defined the graph as follows: 我将图形定义如下:

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);
}   

What I'm trying to do now is to initialize this graph from a list of node and edge properties (in the init method). 我现在想要做的是从节点和边缘属性(在init方法中)列表中初始化此图。 NodeInfo and EdgeInfo are simple structures with primitive types. NodeInfo和EdgeInfo是具有原始类型的简单结构。 So far I've done this: 到目前为止,我已经做到了:

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
    }

 }

So, I manage to add the vertices and set the properties (hope it is right). 因此,我设法添加了顶点并设置了属性(希望它是正确的)。 Now, every EdgeInfo has a source and target id, which identify the two nodes of the edge. 现在,每个EdgeInfo都有一个源ID和目标ID,它们标识边缘的两个节点。 The problem is that I need to retrieve them by Id from the graph (with the nodeIndex property that I set before), so that I can call the add_edge method. 问题是我需要从图中(通过之前设置的nodeIndex属性)通过ID检索它们,以便可以调用add_edge方法。 I don't really know how to do this. 我真的不知道该怎么做。 Hope you guys can help me. 希望你们能帮助我。

Starting at the top: it is NOT a good idea to specialize the adjacency list and add you own methods to it. 从顶部开始:专门设计邻接表并向其中添加您自己的方法不是一个好主意。

Instead you should create an instance of the boost::adjacency_list class as a member of a new class that you can then write methods for. 相反,您应该创建boost :: adjacency_list类的实例作为新类的成员,然后可以为其编写方法。

class cNavGraph {
  public:
  boost::adjacency_list < boost::vecS,
  boost::vecS,
  boost::undirectedS, 
  NodeInfo, EdgeInfo > myNavGraph;

...

Now, to retrieve vertices from the graph by the vertex property nodeIndex you have two options: 现在,要通过顶点属性nodeIndex从图中检索顶点,您有两个选择:

  1. Search the vertices for the nodeIndex you want. 在顶点上搜索所需的nodeIndex。 This is simple but will be slow if the graph is very large. 这很简单,但是如果图形很大,将会很慢。

  2. Create a map from nodeIndex to vertex as you add the vertices, then do a lookup on the map when you need to get the vertex from the nodeIndex. 添加顶点时,创建从nodeIndex到顶点的地图,然后在需要从nodeIndex获取顶点时在地图上进行查找。 This needs more code and memory, but will be much faster for large graphs. 这需要更多的代码和内存,但是对于大型图形将更快。

As a more radical sugestion, I propose redesigning things so that you make the NodeIndex and the vertex descriptor identical, like this 作为更彻底的建议,我建议重新设计事物,以使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 );

Note, in reply to your comment, that the call to add_edge will automatically add the two vertices, if they are not already present, with vertex descriptors equal to the node indices specified. 请注意,作为对您的评论的答复,对add_edge的调用将自动添加两个顶点(如果它们不存在),其顶点描述符等于指定的节点索引。 Do not call add_vertex! 不要调用add_vertex!

When you have completed adding all the edges, you can iterate the through the nodes, adding the properties ( as many as you want ). 完成所有边的添加后,您可以遍历节点,并添加属性(所需数量)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM