Some refactoring to reuse the same graph for several trackings.
[mtp.git] / mtp_graph.cc
1
2 ///////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify  //
4 // it under the terms of the version 3 of the GNU General Public License //
5 // as published by the Free Software Foundation.                         //
6 //                                                                       //
7 // This program is distributed in the hope that it will be useful, but   //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of            //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      //
10 // General Public License for more details.                              //
11 //                                                                       //
12 // You should have received a copy of the GNU General Public License     //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>.  //
14 //                                                                       //
15 // Written by and Copyright (C) Francois Fleuret                         //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports        //
17 ///////////////////////////////////////////////////////////////////////////
18
19 #include "mtp_graph.h"
20
21 #include <iostream>
22 #include <float.h>
23
24 using namespace std;
25
26 class Edge {
27 public:
28   int id, occupied;
29   scalar_t length, work_length;
30   Vertex *terminal_vertex;
31   Edge *next, *pred;
32 };
33
34 class Vertex {
35 public:
36   int id, iteration;
37   Edge *root_edge;
38   scalar_t distance_from_source;
39   Vertex *pred_vertex;
40   Edge *pred_edge;
41
42   Vertex() { root_edge = 0; }
43
44   inline void add_edge(Edge *e) {
45     e->next = root_edge;
46     e->pred = 0;
47     if(root_edge) { root_edge->pred = e; }
48     root_edge = e;
49   }
50
51   inline void del_edge(Edge *e) {
52     if(e == root_edge) { root_edge = e->next; }
53     if(e->pred) { e->pred->next = e->next; }
54     if(e->next) { e->next->pred = e->pred; }
55   }
56 };
57
58 void MTPGraph::print() {
59   for(int n = 0; n < _nb_vertices; n++) {
60     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
61       cout << n << " -> " << e->terminal_vertex->id << " " << e->length;
62       if(e->occupied) {
63         cout << " *";
64       }
65       cout << endl;
66     }
67   }
68 }
69
70 void MTPGraph::print_dot() {
71   cout << "digraph {" << endl;
72   cout << "  node[shape=circle];" << endl;
73   for(int n = 0; n < _nb_vertices; n++) {
74     int a = vertices[n].id;
75     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
76       int b = e->terminal_vertex->id;
77       if(e->occupied) {
78         cout << "  " << b << " -> " << a << " [style=bold,color=black,label=\"" << -e->length << "\"];" << endl;
79       } else {
80         cout << "  " << a << " -> " << b << " [color=gray,label=\"" << e->length << "\"];" << endl;
81       }
82     }
83   }
84   cout << "}" << endl;
85 }
86
87
88 void dot_print(int nb_vertices,
89                int nb_edges, int *ea, int *eb, scalar_t *el,
90                int _source, int _sink,
91                int *edge_occupation) {
92   for(int e = 0; e < nb_edges; e++) {
93   }
94   cout << "}" << endl;
95 }
96 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
97                    int *from, int *to,
98                    int src, int snk) {
99   _nb_vertices = nb_vertices;
100   _nb_edges = nb_edges;
101
102   edges = new Edge[_nb_edges];
103   vertices = new Vertex[_nb_vertices];
104
105   _source = &vertices[src];
106   _sink = &vertices[snk];
107
108   for(int v = 0; v < _nb_vertices; v++) {
109     vertices[v].id = v;
110   }
111
112   for(int e = 0; e < nb_edges; e++) {
113     vertices[from[e]].add_edge(&edges[e]);
114     edges[e].occupied = 0;
115     edges[e].id = e;
116     edges[e].terminal_vertex = &vertices[to[e]];
117   }
118
119   _front = new Vertex *[_nb_vertices];
120   _new_front = new Vertex *[_nb_vertices];
121 }
122
123 MTPGraph::~MTPGraph() {
124   delete[] vertices;
125   delete[] edges;
126   delete[] _front;
127   delete[] _new_front;
128 }
129
130 void MTPGraph::initialize_work_lengths() {
131   scalar_t length_min = 0;
132   for(int n = 0; n < _nb_vertices; n++) {
133     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
134       length_min = min(e->length, length_min);
135     }
136   }
137   for(int n = 0; n < _nb_vertices; n++) {
138     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
139       e->work_length = e->length - length_min;
140     }
141   }
142 }
143
144 void MTPGraph::update_work_length() {
145   for(int n = 0; n < _nb_vertices; n++) {
146     scalar_t d = vertices[n].distance_from_source;
147     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
148       e->work_length += d - e->terminal_vertex->distance_from_source;
149     }
150   }
151 }
152
153 void MTPGraph::find_shortest_path(Vertex **_front, Vertex **_new_front) {
154   Vertex **tmp_front;
155   int tmp_front_size;
156   Vertex *v, *tv;
157   scalar_t d;
158
159 #ifdef VERBOSE
160   scalar_t residual_error = 0.0;
161 #endif
162   for(int n = 0; n < _nb_vertices; n++) {
163     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
164       if(e->work_length < 0) {
165 #ifdef VERBOSE
166         residual_error -= e->work_length;
167 #endif
168         e->work_length = 0.0;
169       }
170     }
171   }
172 #ifdef VERBOSE
173   cerr << "residual_error " << residual_error << endl;
174 #endif
175
176   for(int v = 0; v < _nb_vertices; v++) {
177     vertices[v].distance_from_source = FLT_MAX;
178     vertices[v].pred_vertex = 0;
179     vertices[v].pred_edge = 0;
180     vertices[v].iteration = 0;
181   }
182
183   int iteration = 0;
184
185   int _front_size = 0, _new_front_size;
186   _front[_front_size++] = _source;
187   _source->distance_from_source = 0;
188
189   do {
190     _new_front_size = 0;
191     iteration++;
192     for(int f = 0; f < _front_size; f++) {
193       v = _front[f];
194       for(Edge *e = v->root_edge; e; e = e->next) {
195         d = v->distance_from_source + e->work_length;
196         tv = e->terminal_vertex;
197         if(d < tv->distance_from_source) {
198           tv->distance_from_source = d;
199           tv->pred_vertex = v;
200           tv->pred_edge = e;
201           if(tv->iteration < iteration) {
202             _new_front[_new_front_size++] = tv;
203             tv->iteration = iteration;
204           }
205         }
206       }
207     }
208
209     tmp_front = _new_front;
210     _new_front = _front;
211     _front = tmp_front;
212
213     tmp_front_size = _new_front_size;
214     _new_front_size = _front_size;
215     _front_size = tmp_front_size;
216   } while(_front_size > 0);
217 }
218
219 void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
220   scalar_t total_length;
221
222   for(int e = 0; e < _nb_edges; e++) {
223     edges[e].length = lengths[e];
224   }
225
226   initialize_work_lengths();
227
228   do {
229     total_length = 0.0;
230     find_shortest_path(_front, _new_front);
231     update_work_length();
232
233     // Do we reach the _sink?
234     if(_sink->pred_edge) {
235
236       // If yes, compute the length of the best path
237       for(Vertex *v = _sink; v->pred_edge; v = v->pred_vertex) {
238         total_length += v->pred_edge->length;
239       }
240
241       // If that length is negative
242       if(total_length < 0.0) {
243         // Invert all the edges along the best path
244         for(Vertex *v = _sink; v->pred_edge; v = v->pred_vertex) {
245           Edge *e = v->pred_edge;
246           e->terminal_vertex = v->pred_vertex;
247           e->occupied = 1 - e->occupied;
248           e->length = - e->length;
249           e->work_length = - e->work_length;
250           v->pred_vertex->del_edge(e);
251           v->add_edge(e);
252         }
253       }
254     }
255   } while(total_length < 0.0);
256
257   for(int n = 0; n < _nb_vertices; n++) {
258     Vertex *v = &vertices[n];
259     for(Edge *e = v->root_edge; e; e = e->next) {
260       result_edge_occupation[e->id] = e->occupied;
261     }
262   }
263 }