简体   繁体   English

在Boost Graph Library中将自定义属性添加到网格的顶点

[英]Adding custom properties to vertex of a grid in Boost Graph Library

I am using Boost Graph Library for map management in my robotics project. 我在我的机器人项目中使用Boost Graph Library进行地图管理。 I intend to use Boost Grid and I am finding the Boost Graph documentation really hard to understand so I need a little help. 我打算使用Boost Grid,我发现Boost Graph文档真的很难理解,所以我需要一些帮助。

This is the way I have created the grid, and printing it : 这是我创建网格并打印它的方式:

  struct sampleVertex {
    int row;
    int col;
    bool occupied;
  };

  boost::array<std::size_t, 2> lengths = { { 3, 2 } };
  boost::grid_graph<2> gridD(lengths);
  boost::write_graphviz(fout, gridD);

Now, I want to add custom properties to the vertices, as defined as a struct - 'sampleVertex'. 现在,我想向顶点添加自定义属性,定义为结构 - 'sampleVertex'。 Please show me some code snippet or example to do this. 请向我展示一些代码片段或示例来执行此操作。 I am aware that, the bundled properties could be added through an adjacency_list and manually creating grid-vertices and connecting edges. 我知道,捆绑的属性可以通过adjacency_list添加并手动创建网格顶点和连接边。 I am wondering, if it could be done directly using boost::grid_graph. 我想知道,如果可以使用boost :: grid_graph直接完成。 Thanks in advance. 提前致谢。

Here's a simple example I can come up with (which also uses the properties in the output): 这是我可以提出的一个简单示例(它也使用输出中的属性):

Live On Coliru 住在Coliru

#include <boost/graph/grid_graph.hpp>
#include <boost/graph/properties.hpp>
#include <boost/graph/graphviz.hpp>
#include <iostream>

struct sampleVertex {
    int row;
    int col;
    bool occupied;
    friend std::ostream& operator<<(std::ostream& os, sampleVertex& sv) {
        return os << "{" << sv.row << "," << sv.col << "," << sv.occupied << "}";
    }
    friend std::istream& operator>>(std::istream& is, sampleVertex& sv) {
        return is >> sv.row >> sv.col >> sv.occupied;
    }
};


int main() {
    boost::array<int, 2> lengths = { { 3, 2 } };
    using Graph  = boost::grid_graph<2, int>;
    using Traits = boost::graph_traits<Graph>;
    using IdMap  = boost::property_map<Graph, boost::vertex_index_t>::const_type;

    Graph gridD(lengths);
    IdMap indexMap(get(boost::vertex_index, gridD));
    // properties
    boost::vector_property_map<sampleVertex, IdMap> props(num_vertices(gridD), indexMap);

    // initialize
    for (int i = 0; i < 3; ++i)
        for (int j = 0; j < 2; ++j)
            put(props, Traits::vertex_descriptor {{i, j}}, sampleVertex{i,j,false});

    // print a property
    boost::dynamic_properties dp;
    dp.property("node_id", props);
    boost::write_graphviz_dp(std::cout, gridD, dp);
}

Output: 输出:

digraph G {
"{0,0,0}";
"{1,0,0}";
"{2,0,0}";
"{0,1,0}";
"{1,1,0}";
"{2,1,0}";
"{0,0,0}"->"{1,0,0}" ;
"{1,0,0}"->"{2,0,0}" ;
"{0,1,0}"->"{1,1,0}" ;
"{1,1,0}"->"{2,1,0}" ;
"{1,0,0}"->"{0,0,0}" ;
"{2,0,0}"->"{1,0,0}" ;
"{1,1,0}"->"{0,1,0}" ;
"{2,1,0}"->"{1,1,0}" ;
"{0,0,0}"->"{0,1,0}" ;
"{1,0,0}"->"{1,1,0}" ;
"{2,0,0}"->"{2,1,0}" ;
"{0,1,0}"->"{0,0,0}" ;
"{1,1,0}"->"{1,0,0}" ;
"{2,1,0}"->"{2,0,0}" ;
}

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

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