Search code examples
c++boostgraphboost-graph

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. I intend to use Boost Grid and I am finding the Boost Graph documentation really hard to understand so I need a little help.

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'. 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. I am wondering, if it could be done directly using boost::grid_graph. Thanks in advance.


Solution

  • Here's a simple example I can come up with (which also uses the properties in the output):

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

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