0

I have a problem with the boost graph library.

At the initialization step, I want to store an image in this adjacency graph:

typedef boost::adjacency_list<boost::vecS,
                              boost::vecS,
                              boost::undirectedS,
                              boost::property<boost::vertex_bundle_t, unsigned int>,
                              boost::property<boost::edge_bundle_t, float>,
                              boost::no_property> GraphType

I did a first loop to create for each pixel of the image a vertex (use OTB library to handle the image, in_iter is just an iterator on the pixel of the image):

unsigned long int curr_id = 0;
for(in_iter.GoToBegin(); !in_iter.IsAtEnd(); ++in_iter)
{
    boost::add_vertex(curr_id, *_graph);
    curr_id++;
}

And now I want to create for a vertex its connections with its neighbors (4 connexity): I tried this but it doesn't work:

curr_id = 0;
long int neighbors[4];
auto iterBounds = boost::vertices(*_graph);
for(auto v_iter = iterBounds.first; v_iter != iterBounds.second; v_iter++)
{
  FindNeighboring(curr_id, neighbors, rows, cols);
  for(short j = 0; j<4; j++)
  { 
    if(neighbors[j] > -1)
    {
      boost::add_vertex(*_graph->m_vertices[curr_id], 
                        *_graph->m_vertices[neighbors[j]],
                        std::numeric_limits<float>::max();
                        *_graph);
    }
  }
}

I want to access to the vertex descriptor knowing its position. Is it possible to do that in BGL ?

Thank you for your help !

1 Answers1

0

The answer is "yes and no".

If you use a generic adjacency_list for your graph, then, of course, you have to maintain the correspondence <coords> <--> <vertex> yourself. You can keep it as a mapping provided by boost::unordered_map< Point, vertex_descriptor> or as a boost::bimap or even std::map. It will be your responsibility to populate the map and keep it in correspondence with the graph.

Alternatively, you can consider BGL built-in class grid_graph. It gives you the mapping from coordinates to vertex descriptors:

// Get the vertex associated with vertex_index
Traits::vertex_descriptor
vertex(Traits::vertices_size_type vertex_index,
       const Graph& graph);

// Get the index associated with vertex
Traits::vertices_size_type
get(boost::vertex_index_t,
    const Graph& graph,
    Traits::vertex_descriptor vertex);

(and similar function for edges).

Here you do not need to create individual vertices, they are created by BGL in the graph constructor the moment you specify the dimensions of your graph.

You might want to associate a pixel with each vertex using some kind of custom property map, but that is a separate story.

Michael Simbirsky
  • 3,045
  • 1
  • 12
  • 24