changeset 190:36968a63efe1

Got the connected_components to finally work using a vecS for the vertex list in the adjacency list. In this case, the component map is simply a vector of ints (which is the type of UndirectedGraph::vextex_descriptor (=graph_traits<FeatureGraph>::vertex_descriptor) and probably UndirectedGraph::vertices_size_type). To use listS, I was told on the Boost mailing list: >> If you truly need listS, you will need to create a vertex index >> map, fill it in before you create the property map, and pass it to the >> vector_property_map constructor (and as a type argument to that class). It may be feasible with a component map like shared_array_property_map< graph_traits<FeatureGraph>::vertex_descriptor, property_map<FeatureGraph, vertex_index_t>::const_type > components(num_vertices(g), get(vertex_index, g));
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 07 Dec 2011 18:51:32 -0500
parents 1116f0a1ff31
children 0e60a306d324
files c/Motion.cpp include/Motion.hpp
diffstat 2 files changed, 13 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/c/Motion.cpp	Fri Dec 02 19:11:53 2011 -0500
+++ b/c/Motion.cpp	Wed Dec 07 18:51:32 2011 -0500
@@ -7,7 +7,6 @@
 #include "opencv2/highgui/highgui.hpp"
 
 #include <boost/graph/connected_components.hpp>
-#include <boost/config.hpp>
 
 #include <iostream>
 #include <vector>
@@ -16,6 +15,7 @@
 
 using namespace std;
 using namespace cv;
+using namespace boost;
 
 /******************** FeatureTrajectory ********************/
 
@@ -75,7 +75,7 @@
 bool FeatureTrajectory::minMaxSimilarity(const FeatureTrajectory& ft, const int& firstInstant, const int& lastInstant, float connectionDistance, float segmentationDistance) {
   float minDistance = norm(positions->getPointAtInstant(firstInstant)-ft.positions->getPointAtInstant(firstInstant));
   float maxDistance = minDistance;
-  bool connected = minDistance <= connectionDistance;
+  bool connected = (minDistance <= connectionDistance);
   int t=firstInstant+1;
   while (t <= lastInstant && connected) {
     float distance = norm(positions->getPointAtInstant(t)-ft.positions->getPointAtInstant(t));
@@ -149,7 +149,7 @@
 void FeatureGraph::addFeature(const FeatureTrajectoryPtr& ft) {
   UndirectedGraph::vertex_descriptor newVertex = add_vertex(graph);
   graph[newVertex].feature = ft;
-  for (boost::graph_traits<UndirectedGraph>::vertex_iterator vi = vertices(graph).first; 
+  for (graph_traits<UndirectedGraph>::vertex_iterator vi = vertices(graph).first; 
        vi!=vertices(graph).second; ++vi) { // vi pointer to vertex_descriptor
     FeatureTrajectoryPtr ft2 = graph[*vi].feature;
     int lastInstant = static_cast<int>(min(ft->getLastInstant(), ft2->getLastInstant()));
@@ -166,15 +166,16 @@
 }
 
 void FeatureGraph::connectedComponents(const int& lastInstant) {
-  typedef boost::graph_traits<UndirectedGraph>::vertices_size_type vertices_size_type;
-  vector<vertices_size_type> components(num_vertices(graph));
-  // int num = connected_components(graph, &components[0]);
-  // // todo change the type of the component map http://www.boost.org/doc/libs/1_48_0/libs/graph/doc/connected_components.html
-  // cout << "Total number of components: " << num << endl;
+  vector<UndirectedGraph::vertex_descriptor> components(num_vertices(graph));
+  //vector_property_map<UndirectedGraph::vertex_descriptor> components(num_vertices(graph));
+  //vector_property_map< graph_traits<UndirectedGraph>::vertex_descriptor, property_map<UndirectedGraph, vertex_index_t>::const_type > components(num_vertices(graph));
 
-  // for (unsigned int i = 0; i < components.size(); ++i)
-  //     cout << "Vertex " << i <<" is in component " << components[i] << endl;
-  // cout << endl;
+  int num = connected_components(graph, &components[0]);
+  cout << "Total number of components: " << num << endl;
+
+  for (unsigned int i = 0; i < num_vertices(graph); ++i)
+    cout << "Vertex " << i <<" is in component " << components[i] << endl;
+  cout << endl;
 }
 
 string FeatureGraph::informationString(void) {
--- a/include/Motion.hpp	Fri Dec 02 19:11:53 2011 -0500
+++ b/include/Motion.hpp	Wed Dec 07 18:51:32 2011 -0500
@@ -116,7 +116,7 @@
     FeatureTrajectoryPtr feature;
   };
 
-  typedef boost::adjacency_list <boost::listS, boost::listS, boost::undirectedS, VertexInformation, FeatureConnection> UndirectedGraph;
+  typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, VertexInformation, FeatureConnection> UndirectedGraph;
 
   float connectionDistance;
   float segmentationDistance;