Adding comments.
[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 Tracker::Tracker(int nb_time_steps, int nb_locations) {
26   _nb_locations = nb_locations;
27   _nb_time_steps = nb_time_steps;
28
29   detection_scores = allocate_array<scalar_t>(_nb_time_steps, _nb_locations);
30   allowed_motion = allocate_array<int>(_nb_locations, _nb_locations);
31
32   entrances = new int[_nb_locations];
33   exits = new int[_nb_locations];
34
35   for(int l = 0; l < nb_locations; l++) {
36     entrances[l] = 0;
37     exits[l] = 0;
38     for(int m = 0; m < nb_locations; m++) {
39       allowed_motion[l][m] = 0;
40     }
41   }
42
43   for(int t = 0; t < _nb_time_steps; t++) {
44     for(int l = 0; l < _nb_locations; l++) {
45       detection_scores[t][l] = 0.0;
46     }
47   }
48
49   _edge_lengths = 0;
50   _graph = 0;
51 }
52
53 Tracker::~Tracker() {
54   delete[] _edge_lengths;
55   delete _graph;
56   deallocate_array<scalar_t>(detection_scores);
57   deallocate_array<int>(allowed_motion);
58   delete[] exits;
59   delete[] entrances;
60 }
61
62 int Tracker::early_pair_node(int t, int l) {
63   return 1 + (2 * (t + 0) + 0) * _nb_locations + l;
64 }
65
66 int Tracker::late_pair_node(int t, int l) {
67   return 1 + (2 * (t + 0) + 1) * _nb_locations + l;
68 }
69
70 void Tracker::build_graph() {
71   // Delete the existing graph if there was one
72   delete[] _edge_lengths;
73   delete _graph;
74
75   int nb_motions = 0, nb_exits = 0, nb_entrances = 0;
76
77   for(int l = 0; l < _nb_locations; l++) {
78     if(exits[l]) nb_exits++;
79     if(entrances[l]) nb_entrances++;
80     for(int m = 0; m < _nb_locations; m++) {
81       if(allowed_motion[l][m]) nb_motions++;
82     }
83   }
84
85   int nb_vertices = 2 + 2 * _nb_time_steps * _nb_locations;
86
87   int nb_edges =
88     // The edges from the source to the first frame, and from the last
89     // frame to the sink
90     _nb_locations * 2 +
91     // The edges from the source to the entrances and from the exists
92     // to the sink (in every time frames but the first for the
93     // entrances, and last for the exits)
94     (_nb_time_steps - 1) * (nb_exits + nb_entrances) +
95     // The edges for the motions, between every successive frames
96     (_nb_time_steps - 1) * nb_motions +
97     // The edges inside the duplicated nodes
98     _nb_locations * _nb_time_steps;
99
100   int *node_from = new int[nb_edges];
101   int *node_to = new int[nb_edges];
102
103   int source = 0, sink = nb_vertices - 1;
104   int e = 0;
105
106   _edge_lengths = new scalar_t[nb_edges];
107
108   // We put the in-node edges first, since these are the ones whose
109   // lengths we will have to change before tracking, according to the
110   // detection scores
111
112   for(int t = 0; t < _nb_time_steps; t++) {
113     for(int l = 0; l < _nb_locations; l++) {
114       node_from[e] = early_pair_node(t, l);
115       node_to[e] = late_pair_node(t, l);
116       e++;
117     }
118   }
119
120   for(int l = 0; l < _nb_locations; l++) {
121     node_from[e] = source;
122     node_to[e] = 1 + l + 0 * _nb_locations;
123     _edge_lengths[e] = 0.0;
124     e++;
125   }
126
127   for(int t = 0; t < _nb_time_steps; t++) {
128     for(int l = 0; l < _nb_locations; l++) {
129       if(t == _nb_time_steps - 1) {
130         node_from[e] = late_pair_node(t, l);
131         node_to[e] = sink;
132         _edge_lengths[e] = 0.0;
133         e++;
134       } else {
135         for(int k = 0; k < _nb_locations; k++) {
136           if(allowed_motion[l][k]) {
137             node_from[e] = late_pair_node(t, l);
138             node_to[e] = early_pair_node(t+1, k);
139             _edge_lengths[e] = 0.0;
140             e++;
141           }
142         }
143       }
144     }
145   }
146
147   for(int t = 0; t < _nb_time_steps; t++) {
148     for(int l = 0; l < _nb_locations; l++) {
149       if(t > 0 && entrances[l]) {
150         node_from[e] = source;
151         node_to[e] = early_pair_node(t, l);
152         _edge_lengths[e] = 0.0;
153         e++;
154       }
155       if(t < _nb_time_steps - 1 && exits[l]) {
156         node_from[e] = late_pair_node(t, l);
157         node_to[e] = sink;
158         _edge_lengths[e] = 0.0;
159         e++;
160       }
161     }
162   }
163
164   _graph = new MTPGraph(nb_vertices, nb_edges,
165                         node_from, node_to,
166                         source, sink);
167
168   delete[] node_from;
169   delete[] node_to;
170 }
171
172 void Tracker::print_graph_dot(ostream *os) {
173   int e = 0;
174   for(int t = 0; t < _nb_time_steps; t++) {
175     for(int l = 0; l < _nb_locations; l++) {
176       _edge_lengths[e++] = - detection_scores[t][l];
177     }
178   }
179   _graph->print_dot(os);
180 }
181
182 void Tracker::track() {
183   int e = 0;
184   for(int t = 0; t < _nb_time_steps; t++) {
185     for(int l = 0; l < _nb_locations; l++) {
186       _edge_lengths[e++] = - detection_scores[t][l];
187     }
188   }
189
190   _graph->find_best_paths(_edge_lengths);
191   _graph->retrieve_disjoint_paths();
192
193 #ifdef VERBOSE
194   for(int p = 0; p < _graph->nb_paths; p++) {
195     Path *path = _graph->paths[p];
196     cout << "PATH " << p << " [length " << path->nb_nodes << "] " << path->nodes[0];
197     for(int n = 1; n < path->nb_nodes; n++) {
198       cout << " -> " << path->nodes[n];
199     }
200     cout << endl;
201   }
202 #endif
203 }
204
205 int Tracker::nb_trajectories() {
206   return _graph->nb_paths;
207 }
208
209 scalar_t Tracker::trajectory_score(int k) {
210   return -_graph->paths[k]->length;
211 }
212
213 int Tracker::trajectory_entrance_time(int k) {
214   return (_graph->paths[k]->nodes[1] - 1) / (2 * _nb_locations);
215 }
216
217 int Tracker::trajectory_duration(int k) {
218   return (_graph->paths[k]->nb_nodes - 2) / 2;
219 }
220
221 int Tracker::trajectory_location(int k, int time_from_entry) {
222   return (_graph->paths[k]->nodes[2 * time_from_entry + 1] - 1) % _nb_locations;
223 }