首页 文章

使用自定义顶点和边来增强图形

提问于
浏览
1

我正在使用自己的节点和边缘属性创建自定义增强图 . 我将图形定义如下:

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方法 . 我真的不知道该怎么做 . 希望你们能帮助我 .

1 回答

  • 2

    从顶部开始:专门化邻接列表并为其添加自己的方法不是一个好主意 .

    相反,您应该创建boost :: adjacency_list类的实例作为新类的成员,然后您可以为其编写方法 .

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

    现在,要通过顶点属性nodeIndex从图中检索顶点,您有两个选项:

    • 在顶点中搜索所需的nodeIndex . 这很简单,但如果图表非常大,则会很慢 .

    • 在添加顶点时从nodeIndex到顶点创建一个贴图,然后在需要从nodeIndex获取顶点时在 Map 上执行查找 . 这需要更多的代码和内存,但对于大型图形来说会更快 .

    作为一个更激进的消化,我建议重新设计东西,以便你使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!

    完成添加所有边后,可以遍历节点,添加属性(可以根据需要添加) .

相关问题