2015-12-07 27 views
3

我在我的机器人项目中使用Boost图库进行地图管理。我打算使用Boost Grid,我发现Boost Graph文档很难理解,所以我需要一些帮助。将自定义属性添加到Boost图库中的网格顶点

这是我所创建的网格的方式,进行打印:

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

现在,我想自定义属性添加到顶点,定义为结构 - “sampleVertex”。请给我看一些代码片段或例子来做到这一点。我知道,捆绑的属性可以通过adjacency_list添加并手动创建网格顶点和连接边。我想知道,如果它可以直接使用boost :: grid_graph完成。提前致谢。

回答

4

下面是一个简单的例子,我可以想出(其也使用在输出属性):

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

输出:

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}" ; 
} 
+0

您好sehe ,我有要求将动态属性写入文件。我确实改变了流,但那给了我错误。任何想法我应该改变,将动态属性写入DOT文件。 – soupso

+0

我的代码已经做到了。 Sooo ...也许你想展示你想要做什么(也许是一个新问题)。 – sehe

相关问题