Cosmetics + fixed the entrance in last time frame and exits in first time frame.
[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_score = 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_score[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_score);
57   deallocate_array<int>(_allowed_motion);
58   delete[] _exits;
59   delete[] _entrances;
60 }
61
62 void Tracker::set_allowed_motion(int from_location, int to_location, int v) {
63   ASSERT(from_location >= 0 && from_location < _nb_locations &&
64          to_location >= 0 && to_location < _nb_locations);
65   _allowed_motion[from_location][to_location] = v;
66 }
67
68 void Tracker::set_as_entrance(int location, int v) {
69   ASSERT(location >= 0 && location < _nb_locations);
70   _entrances[location] = v;
71 }
72
73 void Tracker::set_as_exit(int location, int v) {
74   ASSERT(location >= 0 && location < _nb_locations);
75   _exits[location] = v;
76 }
77
78 void Tracker::set_detection_score(int time, int location, scalar_t score) {
79   ASSERT(time >= 0 && time < _nb_time_steps &&
80          location >= 0 && location < _nb_locations);
81   _detection_score[time][location] = score;
82 }
83
84 void Tracker::build_graph() {
85   // Delete existing graph if there was one
86   delete[] _edge_lengths;
87   delete _graph;
88
89   int nb_motions = 0, nb_exits = 0, nb_entrances = 0;
90
91   for(int l = 0; l < _nb_locations; l++) {
92     if(_exits[l]) nb_exits++;
93     if(_entrances[l]) nb_entrances++;
94     for(int m = 0; m < _nb_locations; m++) {
95       if(_allowed_motion[l][m]) nb_motions++;
96     }
97   }
98
99   int nb_vertices = 2 + 2 * _nb_time_steps * _nb_locations;
100
101   int nb_edges =
102     // The edges from the source to the first frame, and from the last
103     // frame to the sink
104     _nb_locations * 2 +
105     // The edges from the source to the entrances and from the exists
106     // to the sink (in every time frames but the first and last)
107     (_nb_time_steps - 1) * (nb_exits + nb_entrances) +
108     // The edges for the motions, between every pair of successive
109     // frames
110     (_nb_time_steps - 1) * nb_motions +
111     // The edges inside the duplicated nodes
112     _nb_locations * _nb_time_steps;
113
114   int *node_from = new int[nb_edges];
115   int *node_to = new int[nb_edges];
116
117   int source = 0, sink = nb_vertices - 1;
118   int e = 0;
119
120   _edge_lengths = new scalar_t[nb_edges];
121
122   // We put the in-node edges first, since these are the ones whose
123   // lengths we will have to change according to the detection score
124
125   for(int t = 0; t < _nb_time_steps; t++) {
126     for(int l = 0; l < _nb_locations; l++) {
127       node_from[e] = 1 + (2 * (t + 0) + 0) * _nb_locations + l;
128       node_to[e] =   1 + (2 * (t + 0) + 1) * _nb_locations + l;
129       e++;
130     }
131   }
132
133   for(int l = 0; l < _nb_locations; l++) {
134     node_from[e] = source;
135     node_to[e] = 1 + l + 0 * _nb_locations;
136     _edge_lengths[e] = 0.0;
137     e++;
138   }
139
140   for(int t = 0; t < _nb_time_steps; t++) {
141     for(int l = 0; l < _nb_locations; l++) {
142       if(t == _nb_time_steps - 1) {
143         node_from[e] = 1 + (2 * (t + 0) + 1) * _nb_locations + l;
144         node_to[e] = sink;
145         _edge_lengths[e] = 0.0;
146         e++;
147       } else {
148         for(int k = 0; k < _nb_locations; k++) {
149           if(_allowed_motion[l][k]) {
150             node_from[e] = 1 + (2 * (t + 0) + 1) * _nb_locations + l;
151             node_to[e] =   1 + (2 * (t + 1) + 0) * _nb_locations + k;
152             _edge_lengths[e] = 0.0;
153             e++;
154           }
155         }
156       }
157     }
158   }
159
160   for(int t = 0; t < _nb_time_steps; t++) {
161     for(int l = 0; l < _nb_locations; l++) {
162       if(t > 0 && _entrances[l]) {
163         node_from[e] = source;
164         node_to[e] =   1 + (2 * (t + 0) + 0) * _nb_locations + l;
165         _edge_lengths[e] = 0.0;
166         e++;
167       }
168       if(t < _nb_time_steps - 1 && _exits[l]) {
169         node_from[e] =   1 + (2 * (t + 0) + 1) * _nb_locations + l;
170         node_to[e] = sink;
171         _edge_lengths[e] = 0.0;
172         e++;
173       }
174     }
175   }
176
177   _graph = new MTPGraph(nb_vertices, nb_edges,
178                         node_from, node_to,
179                         source, sink);
180
181   delete[] node_from;
182   delete[] node_to;
183 }
184
185 void Tracker::print_graph_dot(ostream *os) {
186   int e = 0;
187   for(int t = 0; t < _nb_time_steps; t++) {
188     for(int l = 0; l < _nb_locations; l++) {
189       _edge_lengths[e++] = - _detection_score[t][l];
190     }
191   }
192   _graph->print_dot(os);
193 }
194
195 void Tracker::track() {
196   int e = 0;
197   for(int t = 0; t < _nb_time_steps; t++) {
198     for(int l = 0; l < _nb_locations; l++) {
199       _edge_lengths[e++] = - _detection_score[t][l];
200     }
201   }
202
203   _graph->find_best_paths(_edge_lengths);
204   _graph->retrieve_disjoint_paths();
205
206 #ifdef VERBOSE
207   for(int p = 0; p < _graph->nb_paths; p++) {
208     Path *path = _graph->paths[p];
209     cout << "PATH " << p << " [length " << path->length << "] " << path->nodes[0];
210     for(int n = 1; n < path->length; n++) {
211       cout << " -> " << path->nodes[n];
212     }
213     cout << endl;
214   }
215 #endif
216 }
217
218 int Tracker::nb_trajectories() {
219   return _graph->nb_paths;
220 }
221
222 int Tracker::trajectory_entrance_time(int k) {
223   return (_graph->paths[k]->nodes[1] - 1) / (2 * _nb_locations);
224 }
225
226 int Tracker::trajectory_duration(int k) {
227   return (_graph->paths[k]->length - 2) / 2;
228 }
229
230 int Tracker::trajectory_location(int k, int time) {
231   return (_graph->paths[k]->nodes[2 * time + 1] - 1) % _nb_locations;
232 }