Some refactoring to reuse the same graph for several trackings.
[mtp.git] / mtp_graph.cc
index eb4d3cc..c2a7fa3 100644 (file)
@@ -67,33 +67,64 @@ void MTPGraph::print() {
   }
 }
 
+void MTPGraph::print_dot() {
+  cout << "digraph {" << endl;
+  cout << "  node[shape=circle];" << endl;
+  for(int n = 0; n < _nb_vertices; n++) {
+    int a = vertices[n].id;
+    for(Edge *e = vertices[n].root_edge; e; e = e->next) {
+      int b = e->terminal_vertex->id;
+      if(e->occupied) {
+        cout << "  " << b << " -> " << a << " [style=bold,color=black,label=\"" << -e->length << "\"];" << endl;
+      } else {
+        cout << "  " << a << " -> " << b << " [color=gray,label=\"" << e->length << "\"];" << endl;
+      }
+    }
+  }
+  cout << "}" << endl;
+}
+
+
+void dot_print(int nb_vertices,
+               int nb_edges, int *ea, int *eb, scalar_t *el,
+               int _source, int _sink,
+               int *edge_occupation) {
+  for(int e = 0; e < nb_edges; e++) {
+  }
+  cout << "}" << endl;
+}
 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
                    int *from, int *to,
                    int src, int snk) {
   _nb_vertices = nb_vertices;
   _nb_edges = nb_edges;
 
-  edge_heap = new Edge[_nb_edges];
+  edges = new Edge[_nb_edges];
   vertices = new Vertex[_nb_vertices];
 
-  source = &vertices[src];
-  sink = &vertices[snk];
+  _source = &vertices[src];
+  _sink = &vertices[snk];
 
   for(int v = 0; v < _nb_vertices; v++) {
     vertices[v].id = v;
   }
 
   for(int e = 0; e < nb_edges; e++) {
-    vertices[from[e]].add_edge(&edge_heap[e]);
-    edge_heap[e].occupied = 0;
-    edge_heap[e].id = e;
-    edge_heap[e].terminal_vertex = &vertices[to[e]];
+    vertices[from[e]].add_edge(&edges[e]);
+    edges[e].occupied = 0;
+    edges[e].id = e;
+    edges[e].terminal_vertex = &vertices[to[e]];
   }
+
+  _front = new Vertex *[_nb_vertices];
+  _new_front = new Vertex *[_nb_vertices];
 }
 
 MTPGraph::~MTPGraph() {
   delete[] vertices;
-  delete[] edge_heap;
+  delete[] edges;
+  delete[] _front;
+  delete[] _new_front;
 }
 
 void MTPGraph::initialize_work_lengths() {
@@ -119,7 +150,7 @@ void MTPGraph::update_work_length() {
   }
 }
 
-void MTPGraph::find_shortest_path(Vertex **front, Vertex **new_front) {
+void MTPGraph::find_shortest_path(Vertex **_front, Vertex **_new_front) {
   Vertex **tmp_front;
   int tmp_front_size;
   Vertex *v, *tv;
@@ -151,15 +182,15 @@ void MTPGraph::find_shortest_path(Vertex **front, Vertex **new_front) {
 
   int iteration = 0;
 
-  int front_size = 0, new_front_size;
-  front[front_size++] = source;
-  source->distance_from_source = 0;
+  int _front_size = 0, _new_front_size;
+  _front[_front_size++] = _source;
+  _source->distance_from_source = 0;
 
   do {
-    new_front_size = 0;
+    _new_front_size = 0;
     iteration++;
-    for(int f = 0; f < front_size; f++) {
-      v = front[f];
+    for(int f = 0; f < _front_size; f++) {
+      v = _front[f];
       for(Edge *e = v->root_edge; e; e = e->next) {
         d = v->distance_from_source + e->work_length;
         tv = e->terminal_vertex;
@@ -168,52 +199,49 @@ void MTPGraph::find_shortest_path(Vertex **front, Vertex **new_front) {
           tv->pred_vertex = v;
           tv->pred_edge = e;
           if(tv->iteration < iteration) {
-            new_front[new_front_size++] = tv;
+            _new_front[_new_front_size++] = tv;
             tv->iteration = iteration;
           }
         }
       }
     }
 
-    tmp_front = new_front;
-    new_front = front;
-    front = tmp_front;
+    tmp_front = _new_front;
+    _new_front = _front;
+    _front = tmp_front;
 
-    tmp_front_size = new_front_size;
-    new_front_size = front_size;
-    front_size = tmp_front_size;
-  } while(front_size > 0);
+    tmp_front_size = _new_front_size;
+    _new_front_size = _front_size;
+    _front_size = tmp_front_size;
+  } while(_front_size > 0);
 }
 
 void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
-  Vertex **front = new Vertex *[_nb_vertices];
-  Vertex **new_front = new Vertex *[_nb_vertices];
-
   scalar_t total_length;
 
   for(int e = 0; e < _nb_edges; e++) {
-    edge_heap[e].length = lengths[e];
+    edges[e].length = lengths[e];
   }
 
   initialize_work_lengths();
 
   do {
     total_length = 0.0;
-    find_shortest_path(front, new_front);
+    find_shortest_path(_front, _new_front);
     update_work_length();
 
-    // Do we reach the sink?
-    if(sink->pred_edge) {
+    // Do we reach the _sink?
+    if(_sink->pred_edge) {
 
       // If yes, compute the length of the best path
-      for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
+      for(Vertex *v = _sink; v->pred_edge; v = v->pred_vertex) {
         total_length += v->pred_edge->length;
       }
 
       // If that length is negative
       if(total_length < 0.0) {
         // Invert all the edges along the best path
-        for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
+        for(Vertex *v = _sink; v->pred_edge; v = v->pred_vertex) {
           Edge *e = v->pred_edge;
           e->terminal_vertex = v->pred_vertex;
           e->occupied = 1 - e->occupied;
@@ -226,9 +254,6 @@ void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
     }
   } while(total_length < 0.0);
 
-  delete[] front;
-  delete[] new_front;
-
   for(int n = 0; n < _nb_vertices; n++) {
     Vertex *v = &vertices[n];
     for(Edge *e = v->root_edge; e; e = e->next) {
@@ -236,19 +261,3 @@ void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
     }
   }
 }
-
-void dot_print(int nb_vertices,
-               int nb_edges, int *ea, int *eb, scalar_t *el,
-               int source, int sink,
-               int *edge_occupation) {
-  cout << "digraph {" << endl;
-  cout << "  node[shape=circle];" << endl;
-  for(int e = 0; e < nb_edges; e++) {
-    if(edge_occupation[e]) {
-      cout << "  " << ea[e] << " -> " << eb[e] << " [style=bold,color=black,label=\"" << el[e] << "\"];" << endl;
-    } else {
-      cout << "  " << ea[e] << " -> " << eb[e] << " [color=gray,label=\"" << el[e] << "\"];" << endl;
-    }
-  }
-  cout << "}" << endl;
-}