Made nb_locations and nb_time_steps public.
[mtp.git] / mtp_tracker.cc
1
2 /*
3  *  mtp is the ``Multi Tracked Paths'', an implementation of the
4  *  k-shortest paths algorithm for multi-target tracking.
5  *
6  *  Copyright (c) 2012 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of mtp.
10  *
11  *  mtp is free software: you can redistribute it and/or modify it
12  *  under the terms of the GNU General Public License version 3 as
13  *  published by the Free Software Foundation.
14  *
15  *  mtp is distributed in the hope that it will be useful, but WITHOUT
16  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
18  *  License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with selector.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include "mtp_tracker.h"
26
27 #include <iostream>
28
29 using namespace std;
30
31 void MTPTracker::free() {
32   delete[] _edge_lengths;
33   delete _graph;
34   deallocate_array<scalar_t>(detection_scores);
35   deallocate_array<int>(allowed_motion);
36   delete[] exits;
37   delete[] entrances;
38 }
39
40 void MTPTracker::allocate(int t, int l) {
41   free();
42
43   nb_locations = l;
44   nb_time_steps = t;
45
46   detection_scores = allocate_array<scalar_t>(nb_time_steps, nb_locations);
47   allowed_motion = allocate_array<int>(nb_locations, nb_locations);
48
49   entrances = new int[nb_locations];
50   exits = new int[nb_locations];
51
52   for(int l = 0; l < nb_locations; l++) {
53     entrances[l] = 0;
54     exits[l] = 0;
55     for(int m = 0; m < nb_locations; m++) {
56       allowed_motion[l][m] = 0;
57     }
58   }
59
60   for(int t = 0; t < nb_time_steps; t++) {
61     for(int l = 0; l < nb_locations; l++) {
62       detection_scores[t][l] = 0.0;
63     }
64   }
65
66   _edge_lengths = 0;
67   _graph = 0;
68 }
69
70 void MTPTracker::write(ostream *os) {
71   (*os) << nb_locations << " " << nb_time_steps <<endl;
72
73   (*os) << endl;
74
75   for(int l = 0; l < nb_locations; l++) {
76     for(int m = 0; m < nb_locations; m++) {
77       (*os) << allowed_motion[l][m];
78       if(m < nb_locations - 1) (*os) << " "; else (*os) << endl;
79     }
80   }
81
82   (*os) << endl;
83
84   for(int l = 0; l < nb_locations; l++) {
85     (*os) << entrances[l];
86     if(l < nb_locations - 1) (*os) << " "; else (*os) << endl;
87   }
88
89   (*os) << endl;
90
91   for(int l = 0; l < nb_locations; l++) {
92     (*os) << exits[l];
93     if(l < nb_locations - 1) (*os) << " "; else (*os) << endl;
94   }
95
96   (*os) << endl;
97
98   for(int t = 0; t < nb_time_steps; t++) {
99     for(int l = 0; l < nb_locations; l++) {
100       (*os) << detection_scores[t][l];
101       if(l < nb_locations - 1) (*os) << " "; else (*os) << endl;
102     }
103   }
104 }
105
106 void MTPTracker::read(istream *is) {
107   int nb_locations, nb_time_steps;
108
109   (*is) >> nb_locations >> nb_time_steps;
110
111   allocate(nb_time_steps, nb_locations);
112
113   for(int l = 0; l < nb_locations; l++) {
114     for(int m = 0; m < nb_locations; m++) {
115       (*is) >> allowed_motion[l][m];
116     }
117   }
118
119   for(int l = 0; l < nb_locations; l++) {
120     (*is) >> entrances[l];
121   }
122
123   for(int l = 0; l < nb_locations; l++) {
124     (*is) >> exits[l];
125   }
126
127   for(int t = 0; t < nb_time_steps; t++) {
128     for(int l = 0; l < nb_locations; l++) {
129       (*is) >> detection_scores[t][l];
130     }
131   }
132 }
133
134 void MTPTracker::write_trajectories(ostream *os) {
135   (*os) << nb_trajectories() << endl;
136   for(int t = 0; t < nb_trajectories(); t++) {
137     (*os) << t
138          << " " << trajectory_entrance_time(t)
139          << " " << trajectory_duration(t)
140          << " " << trajectory_score(t);
141     for(int u = 0; u < trajectory_duration(t); u++) {
142       (*os) << " " << trajectory_location(t, u);
143     }
144     (*os) << endl;
145   }
146 }
147
148 MTPTracker::MTPTracker() {
149   nb_locations = 0;
150   nb_time_steps = 0;
151
152   detection_scores = 0;
153   allowed_motion = 0;
154
155   entrances = 0;
156   exits = 0;
157
158   _edge_lengths = 0;
159   _graph = 0;
160 }
161
162 MTPTracker::~MTPTracker() {
163   delete[] _edge_lengths;
164   delete _graph;
165   deallocate_array<scalar_t>(detection_scores);
166   deallocate_array<int>(allowed_motion);
167   delete[] exits;
168   delete[] entrances;
169 }
170
171 int MTPTracker::early_pair_node(int t, int l) {
172   return 1 + (2 * t + 0) * nb_locations + l;
173 }
174
175 int MTPTracker::late_pair_node(int t, int l) {
176   return 1 + (2 * t + 1) * nb_locations + l;
177 }
178
179 void MTPTracker::build_graph() {
180   // Delete the existing graph if there was one
181   delete[] _edge_lengths;
182   delete _graph;
183
184   int nb_motions = 0, nb_exits = 0, nb_entrances = 0;
185
186   for(int l = 0; l < nb_locations; l++) {
187     if(exits[l]) nb_exits++;
188     if(entrances[l]) nb_entrances++;
189     for(int m = 0; m < nb_locations; m++) {
190       if(allowed_motion[l][m]) nb_motions++;
191     }
192   }
193
194   int nb_vertices = 2 + 2 * nb_time_steps * nb_locations;
195
196   int nb_edges =
197     // The edges from the source to the first frame, and from the last
198     // frame to the sink
199     nb_locations * 2 +
200     // The edges from the source to the entrances and from the exits
201     // to the sink (in every time frames but the first for the
202     // entrances, and last for the exits)
203     (nb_time_steps - 1) * (nb_exits + nb_entrances) +
204     // The edges for the motions, between every successive frames
205     (nb_time_steps - 1) * nb_motions +
206     // The edges inside the duplicated nodes
207     nb_locations * nb_time_steps;
208
209   int *node_from = new int[nb_edges];
210   int *node_to = new int[nb_edges];
211
212   int source = 0, sink = nb_vertices - 1;
213   int e = 0;
214
215   _edge_lengths = new scalar_t[nb_edges];
216
217   // We put the in-node edges first, since these are the ones whose
218   // lengths we will have to change before tracking, according to the
219   // detection scores
220
221   for(int t = 0; t < nb_time_steps; t++) {
222     for(int l = 0; l < nb_locations; l++) {
223       node_from[e] = early_pair_node(t, l);
224       node_to[e] = late_pair_node(t, l);
225       e++;
226     }
227   }
228
229   // The edges from the source to the first time frame
230
231   for(int l = 0; l < nb_locations; l++) {
232     node_from[e] = source;
233     node_to[e] = 1 + l + 0 * nb_locations;
234     _edge_lengths[e] = 0.0;
235     e++;
236   }
237
238   // The edges from the last frame to the sink
239
240   for(int l = 0; l < nb_locations; l++) {
241     node_from[e] = late_pair_node(nb_time_steps - 1, l);
242     node_to[e] = sink;
243     _edge_lengths[e] = 0.0;
244     e++;
245   }
246
247   // The edges between frames, corresponding to allowed motions
248
249   for(int t = 0; t < nb_time_steps - 1; t++) {
250     for(int l = 0; l < nb_locations; l++) {
251       for(int k = 0; k < nb_locations; k++) {
252         if(allowed_motion[l][k]) {
253           node_from[e] = late_pair_node(t, l);
254           node_to[e] = early_pair_node(t+1, k);
255           _edge_lengths[e] = 0.0;
256           e++;
257         }
258       }
259     }
260   }
261
262   // The edges from the source to the entrances, and from the exits to
263   // the sink
264
265   for(int t = 0; t < nb_time_steps; t++) {
266     for(int l = 0; l < nb_locations; l++) {
267       if(t > 0 && entrances[l]) {
268         node_from[e] = source;
269         node_to[e] = early_pair_node(t, l);
270         _edge_lengths[e] = 0.0;
271         e++;
272       }
273       if(t < nb_time_steps - 1 && exits[l]) {
274         node_from[e] = late_pair_node(t, l);
275         node_to[e] = sink;
276         _edge_lengths[e] = 0.0;
277         e++;
278       }
279     }
280   }
281
282   // We are done, build the graph
283
284   _graph = new MTPGraph(nb_vertices, nb_edges,
285                         node_from, node_to,
286                         source, sink);
287
288   delete[] node_from;
289   delete[] node_to;
290 }
291
292 void MTPTracker::print_graph_dot(ostream *os) {
293   int e = 0;
294   for(int t = 0; t < nb_time_steps; t++) {
295     for(int l = 0; l < nb_locations; l++) {
296       _edge_lengths[e++] = - detection_scores[t][l];
297     }
298   }
299   _graph->print_dot(os);
300 }
301
302 void MTPTracker::track() {
303   ASSERT(_graph);
304
305   int e = 0;
306   for(int t = 0; t < nb_time_steps; t++) {
307     for(int l = 0; l < nb_locations; l++) {
308       _edge_lengths[e++] = - detection_scores[t][l];
309     }
310   }
311
312   _graph->find_best_paths(_edge_lengths);
313   _graph->retrieve_disjoint_paths();
314
315 #ifdef VERBOSE
316   for(int p = 0; p < _graph->nb_paths; p++) {
317     Path *path = _graph->paths[p];
318     cout << "PATH " << p << " [length " << path->nb_nodes << "] " << path->nodes[0];
319     for(int n = 1; n < path->nb_nodes; n++) {
320       cout << " -> " << path->nodes[n];
321     }
322     cout << endl;
323   }
324 #endif
325 }
326
327 int MTPTracker::nb_trajectories() {
328   return _graph->nb_paths;
329 }
330
331 scalar_t MTPTracker::trajectory_score(int k) {
332   return -_graph->paths[k]->length;
333 }
334
335 int MTPTracker::trajectory_entrance_time(int k) {
336   return (_graph->paths[k]->nodes[1] - 1) / (2 * nb_locations);
337 }
338
339 int MTPTracker::trajectory_duration(int k) {
340   return (_graph->paths[k]->nb_nodes - 2) / 2;
341 }
342
343 int MTPTracker::trajectory_location(int k, int time_from_entry) {
344   return (_graph->paths[k]->nodes[2 * time_from_entry + 1] - 1) % nb_locations;
345 }