diff c/Motion.cpp @ 192:38974d27dd2d

connected_components is working with listS for vertex list
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 12 Dec 2011 02:05:26 -0500
parents 0e60a306d324
children 09c7881073f3
line wrap: on
line diff
--- a/c/Motion.cpp	Thu Dec 08 18:32:35 2011 -0500
+++ b/c/Motion.cpp	Mon Dec 12 02:05:26 2011 -0500
@@ -74,7 +74,7 @@
   return result;
 }
 
-bool FeatureTrajectory::minMaxSimilarity(const FeatureTrajectory& ft, const int& firstInstant, const int& lastInstant, float connectionDistance, float segmentationDistance) {
+bool FeatureTrajectory::minMaxSimilarity(const FeatureTrajectory& ft, const int& firstInstant, const int& lastInstant, const float& connectionDistance, const float& segmentationDistance) {
   float minDistance = norm(positions->getPointAtInstant(firstInstant)-ft.positions->getPointAtInstant(firstInstant));
   float maxDistance = minDistance;
   bool connected = (minDistance <= connectionDistance);
@@ -168,22 +168,23 @@
 }
 
 vector<vector<FeatureGraph::vertex_descriptor> > FeatureGraph::connectedComponents(const int& lastInstant) {
-  int nVertices = num_vertices(graph);
-  vector<vertex_descriptor> components(nVertices);
-  // map<UndirectedGraph::vertex_descriptor, graph_traits<UndirectedGraph>::vertices_size_type> vertex2component;
-  // associative_property_map< map<UndirectedGraph::vertex_descriptor, graph_traits<UndirectedGraph>::vertices_size_type> > components(vertex2component);
+  computeVertexIndex();
+  property_map<UndirectedGraph, int VertexInformation::*>::type components = get(&VertexInformation::index, graph);
 
-  int num = connected_components(graph, &components[0]);
+  int num = connected_components(graph, components, vertex_index_map(get(&VertexInformation::index, graph)));
   cout << "Total number of components: " << num << endl;
 
   vector<unsigned int> lastInstants(num, 0);
   vector<vector<vertex_descriptor> > tmpobjects(num), objects;
 
-  for (int i = 0; i < nVertices; ++i) {
-    cout << "Vertex " << i <<" is in component " << components[i] << endl;// "(last " << graph[i].feature->getLastInstant() << " " << lastInstants[components[i]] << " " << (lastInstants[components[i]] < graph[i].feature->getLastInstant()) << ")" << endl;
-    if (lastInstants[components[i]] < graph[i].feature->getLastInstant())
-      lastInstants[components[i]] = graph[i].feature->getLastInstant();
-    tmpobjects[components[i]].push_back(i);
+  graph_traits<UndirectedGraph>::vertex_iterator vi, vend;
+  for(tie(vi,vend) = vertices(graph); vi != vend; ++vi) {
+    //for (int i = 0; i < nVertices; ++i) {
+    unsigned int id = components[*vi];
+    cout << "Vertex " << *vi << " is in component " << id << endl;// "(last " << graph[i].feature->getLastInstant() << " " << lastInstants[components[i]] << " " << (lastInstants[components[i]] < graph[i].feature->getLastInstant()) << ")" << endl;
+    if (lastInstants[id] < graph[*vi].feature->getLastInstant())
+      lastInstants[id] = graph[*vi].feature->getLastInstant();
+    tmpobjects[id].push_back(*vi);
   }
 
   for (int i = 0; i < num; ++i) {
@@ -221,3 +222,10 @@
   ss << num_vertices(graph) << " vertices, " << num_edges(graph) << " edges";
   return ss.str();
 }
+
+void FeatureGraph::computeVertexIndex(void) {
+  graph_traits<FeatureGraph::UndirectedGraph>::vertex_iterator vi, vend;
+  graph_traits<FeatureGraph::UndirectedGraph>::vertices_size_type cnt = 0;
+  for(tie(vi,vend) = vertices(graph); vi != vend; ++vi)
+    graph[*vi].index = cnt++;
+}