4d12e6145d38bd702bbabc8a2c805c305f1880ee
[mtp.git] / tracker.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 "tracker.h"
20
21 #include <iostream>
22
23 using namespace std;
24
25 void Tracker::free() {
26   delete[] _edge_lengths;
27   delete _graph;
28   deallocate_array<scalar_t>(detection_scores);
29   deallocate_array<int>(allowed_motion);
30   delete[] exits;
31   delete[] entrances;
32 }
33
34 void Tracker::allocate(int nb_time_steps, int nb_locations) {
35   free();
36
37   _nb_locations = nb_locations;
38   _nb_time_steps = nb_time_steps;
39
40   detection_scores = allocate_array<scalar_t>(_nb_time_steps, _nb_locations);
41   allowed_motion = allocate_array<int>(_nb_locations, _nb_locations);
42
43   entrances = new int[_nb_locations];
44   exits = new int[_nb_locations];
45
46   for(int l = 0; l < _nb_locations; l++) {
47     entrances[l] = 0;
48     exits[l] = 0;
49     for(int m = 0; m < _nb_locations; m++) {
50       allowed_motion[l][m] = 0;
51     }
52   }
53
54   for(int t = 0; t < _nb_time_steps; t++) {
55     for(int l = 0; l < _nb_locations; l++) {
56       detection_scores[t][l] = 0.0;
57     }
58   }
59
60   _edge_lengths = 0;
61   _graph = 0;
62 }
63
64 void Tracker::write(ostream *os) {
65   (*os) << _nb_locations << " " << _nb_time_steps <<endl;
66
67   (*os) << endl;
68
69   for(int l = 0; l < _nb_locations; l++) {
70     for(int m = 0; m < _nb_locations; m++) {
71       (*os) << allowed_motion[l][m];
72       if(m < _nb_locations - 1) (*os) << " "; else (*os) << endl;
73     }
74   }
75
76   (*os) << endl;
77
78   for(int l = 0; l < _nb_locations; l++) {
79     (*os) << entrances[l];
80     if(l < _nb_locations - 1) (*os) << " "; else (*os) << endl;
81   }
82
83   (*os) << endl;
84
85   for(int l = 0; l < _nb_locations; l++) {
86     (*os) << exits[l];
87     if(l < _nb_locations - 1) (*os) << " "; else (*os) << endl;
88   }
89
90   (*os) << endl;
91
92   for(int t = 0; t < _nb_time_steps; t++) {
93     for(int l = 0; l < _nb_locations; l++) {
94       (*os) << detection_scores[t][l];
95       if(l < _nb_locations - 1) (*os) << " "; else (*os) << endl;
96     }
97   }
98 }
99
100 void Tracker::read(istream *is) {
101   int nb_locations, nb_time_steps;
102
103   (*is) >> nb_locations >> nb_time_steps;
104
105   allocate(nb_time_steps, nb_locations);
106
107   for(int l = 0; l < _nb_locations; l++) {
108     for(int m = 0; m < _nb_locations; m++) {
109       (*is) >> allowed_motion[l][m];
110     }
111   }
112
113   for(int l = 0; l < _nb_locations; l++) {
114     (*is) >> entrances[l];
115   }
116
117   for(int l = 0; l < _nb_locations; l++) {
118     (*is) >> exits[l];
119   }
120
121   for(int t = 0; t < _nb_time_steps; t++) {
122     for(int l = 0; l < _nb_locations; l++) {
123       (*is) >> detection_scores[t][l];
124     }
125   }
126 }
127
128 void Tracker::write_trajectories(ostream *os) {
129   for(int t = 0; t < nb_trajectories(); t++) {
130     (*os) << t
131          << " " << trajectory_entrance_time(t)
132          << " " << trajectory_duration(t)
133          << " " << trajectory_score(t);
134     for(int u = 0; u < trajectory_duration(t); u++) {
135       (*os) << " " << trajectory_location(t, u);
136     }
137     (*os) << endl;
138   }
139 }
140
141 Tracker::Tracker() {
142   _nb_locations = 0;
143   _nb_time_steps = 0;
144
145   detection_scores = 0;
146   allowed_motion = 0;
147
148   entrances = 0;
149   exits = 0;
150
151   _edge_lengths = 0;
152   _graph = 0;
153 }
154
155 Tracker::~Tracker() {
156   delete[] _edge_lengths;
157   delete _graph;
158   deallocate_array<scalar_t>(detection_scores);
159   deallocate_array<int>(allowed_motion);
160   delete[] exits;
161   delete[] entrances;
162 }
163
164 int Tracker::early_pair_node(int t, int l) {
165   return 1 + (2 * (t + 0) + 0) * _nb_locations + l;
166 }
167
168 int Tracker::late_pair_node(int t, int l) {
169   return 1 + (2 * (t + 0) + 1) * _nb_locations + l;
170 }
171
172 void Tracker::build_graph() {
173   // Delete the existing graph if there was one
174   delete[] _edge_lengths;
175   delete _graph;
176
177   int nb_motions = 0, nb_exits = 0, nb_entrances = 0;
178
179   for(int l = 0; l < _nb_locations; l++) {
180     if(exits[l]) nb_exits++;
181     if(entrances[l]) nb_entrances++;
182     for(int m = 0; m < _nb_locations; m++) {
183       if(allowed_motion[l][m]) nb_motions++;
184     }
185   }
186
187   int nb_vertices = 2 + 2 * _nb_time_steps * _nb_locations;
188
189   int nb_edges =
190     // The edges from the source to the first frame, and from the last
191     // frame to the sink
192     _nb_locations * 2 +
193     // The edges from the source to the entrances and from the exists
194     // to the sink (in every time frames but the first for the
195     // entrances, and last for the exits)
196     (_nb_time_steps - 1) * (nb_exits + nb_entrances) +
197     // The edges for the motions, between every successive frames
198     (_nb_time_steps - 1) * nb_motions +
199     // The edges inside the duplicated nodes
200     _nb_locations * _nb_time_steps;
201
202   int *node_from = new int[nb_edges];
203   int *node_to = new int[nb_edges];
204
205   int source = 0, sink = nb_vertices - 1;
206   int e = 0;
207
208   _edge_lengths = new scalar_t[nb_edges];
209
210   // We put the in-node edges first, since these are the ones whose
211   // lengths we will have to change before tracking, according to the
212   // detection scores
213
214   for(int t = 0; t < _nb_time_steps; t++) {
215     for(int l = 0; l < _nb_locations; l++) {
216       node_from[e] = early_pair_node(t, l);
217       node_to[e] = late_pair_node(t, l);
218       e++;
219     }
220   }
221
222   for(int l = 0; l < _nb_locations; l++) {
223     node_from[e] = source;
224     node_to[e] = 1 + l + 0 * _nb_locations;
225     _edge_lengths[e] = 0.0;
226     e++;
227   }
228
229   for(int t = 0; t < _nb_time_steps; t++) {
230     for(int l = 0; l < _nb_locations; l++) {
231       if(t == _nb_time_steps - 1) {
232         node_from[e] = late_pair_node(t, l);
233         node_to[e] = sink;
234         _edge_lengths[e] = 0.0;
235         e++;
236       } else {
237         for(int k = 0; k < _nb_locations; k++) {
238           if(allowed_motion[l][k]) {
239             node_from[e] = late_pair_node(t, l);
240             node_to[e] = early_pair_node(t+1, k);
241             _edge_lengths[e] = 0.0;
242             e++;
243           }
244         }
245       }
246     }
247   }
248
249   for(int t = 0; t < _nb_time_steps; t++) {
250     for(int l = 0; l < _nb_locations; l++) {
251       if(t > 0 && entrances[l]) {
252         node_from[e] = source;
253         node_to[e] = early_pair_node(t, l);
254         _edge_lengths[e] = 0.0;
255         e++;
256       }
257       if(t < _nb_time_steps - 1 && exits[l]) {
258         node_from[e] = late_pair_node(t, l);
259         node_to[e] = sink;
260         _edge_lengths[e] = 0.0;
261         e++;
262       }
263     }
264   }
265
266   _graph = new MTPGraph(nb_vertices, nb_edges,
267                         node_from, node_to,
268                         source, sink);
269
270   delete[] node_from;
271   delete[] node_to;
272 }
273
274 void Tracker::print_graph_dot(ostream *os) {
275   int e = 0;
276   for(int t = 0; t < _nb_time_steps; t++) {
277     for(int l = 0; l < _nb_locations; l++) {
278       _edge_lengths[e++] = - detection_scores[t][l];
279     }
280   }
281   _graph->print_dot(os);
282 }
283
284 void Tracker::track() {
285   ASSERT(_graph);
286
287   int e = 0;
288   for(int t = 0; t < _nb_time_steps; t++) {
289     for(int l = 0; l < _nb_locations; l++) {
290       _edge_lengths[e++] = - detection_scores[t][l];
291     }
292   }
293
294   _graph->find_best_paths(_edge_lengths);
295   _graph->retrieve_disjoint_paths();
296
297 #ifdef VERBOSE
298   for(int p = 0; p < _graph->nb_paths; p++) {
299     Path *path = _graph->paths[p];
300     cout << "PATH " << p << " [length " << path->nb_nodes << "] " << path->nodes[0];
301     for(int n = 1; n < path->nb_nodes; n++) {
302       cout << " -> " << path->nodes[n];
303     }
304     cout << endl;
305   }
306 #endif
307 }
308
309 int Tracker::nb_trajectories() {
310   return _graph->nb_paths;
311 }
312
313 scalar_t Tracker::trajectory_score(int k) {
314   return -_graph->paths[k]->length;
315 }
316
317 int Tracker::trajectory_entrance_time(int k) {
318   return (_graph->paths[k]->nodes[1] - 1) / (2 * _nb_locations);
319 }
320
321 int Tracker::trajectory_duration(int k) {
322   return (_graph->paths[k]->nb_nodes - 2) / 2;
323 }
324
325 int Tracker::trajectory_location(int k, int time_from_entry) {
326   return (_graph->paths[k]->nodes[2 * time_from_entry + 1] - 1) % _nb_locations;
327 }