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. //
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. //
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/>. //
15 // Written by and Copyright (C) Francois Fleuret //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports //
17 ///////////////////////////////////////////////////////////////////////////
25 void Tracker::free() {
26 delete[] _edge_lengths;
28 deallocate_array<scalar_t>(detection_scores);
29 deallocate_array<int>(allowed_motion);
34 void Tracker::allocate(int nb_time_steps, int nb_locations) {
37 _nb_locations = nb_locations;
38 _nb_time_steps = nb_time_steps;
40 detection_scores = allocate_array<scalar_t>(_nb_time_steps, _nb_locations);
41 allowed_motion = allocate_array<int>(_nb_locations, _nb_locations);
43 entrances = new int[_nb_locations];
44 exits = new int[_nb_locations];
46 for(int l = 0; l < _nb_locations; l++) {
49 for(int m = 0; m < _nb_locations; m++) {
50 allowed_motion[l][m] = 0;
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;
64 void Tracker::write(ostream *os) {
65 (*os) << _nb_locations << " " << _nb_time_steps <<endl;
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;
78 for(int l = 0; l < _nb_locations; l++) {
79 (*os) << entrances[l];
80 if(l < _nb_locations - 1) (*os) << " "; else (*os) << endl;
85 for(int l = 0; l < _nb_locations; l++) {
87 if(l < _nb_locations - 1) (*os) << " "; else (*os) << endl;
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;
100 void Tracker::read(istream *is) {
101 int nb_locations, nb_time_steps;
103 (*is) >> nb_locations >> nb_time_steps;
105 allocate(nb_time_steps, nb_locations);
107 for(int l = 0; l < _nb_locations; l++) {
108 for(int m = 0; m < _nb_locations; m++) {
109 (*is) >> allowed_motion[l][m];
113 for(int l = 0; l < _nb_locations; l++) {
114 (*is) >> entrances[l];
117 for(int l = 0; l < _nb_locations; l++) {
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];
128 void Tracker::write_trajectories(ostream *os) {
129 for(int t = 0; t < nb_trajectories(); 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);
145 detection_scores = 0;
155 Tracker::~Tracker() {
156 delete[] _edge_lengths;
158 deallocate_array<scalar_t>(detection_scores);
159 deallocate_array<int>(allowed_motion);
164 int Tracker::early_pair_node(int t, int l) {
165 return 1 + (2 * (t + 0) + 0) * _nb_locations + l;
168 int Tracker::late_pair_node(int t, int l) {
169 return 1 + (2 * (t + 0) + 1) * _nb_locations + l;
172 void Tracker::build_graph() {
173 // Delete the existing graph if there was one
174 delete[] _edge_lengths;
177 int nb_motions = 0, nb_exits = 0, nb_entrances = 0;
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++;
187 int nb_vertices = 2 + 2 * _nb_time_steps * _nb_locations;
190 // The edges from the source to the first frame, and from the last
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;
202 int *node_from = new int[nb_edges];
203 int *node_to = new int[nb_edges];
205 int source = 0, sink = nb_vertices - 1;
208 _edge_lengths = new scalar_t[nb_edges];
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
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);
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;
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);
234 _edge_lengths[e] = 0.0;
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;
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;
257 if(t < _nb_time_steps - 1 && exits[l]) {
258 node_from[e] = late_pair_node(t, l);
260 _edge_lengths[e] = 0.0;
266 _graph = new MTPGraph(nb_vertices, nb_edges,
274 void Tracker::print_graph_dot(ostream *os) {
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];
281 _graph->print_dot(os);
284 void Tracker::track() {
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];
294 _graph->find_best_paths(_edge_lengths);
295 _graph->retrieve_disjoint_paths();
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];
309 int Tracker::nb_trajectories() {
310 return _graph->nb_paths;
313 scalar_t Tracker::trajectory_score(int k) {
314 return -_graph->paths[k]->length;
317 int Tracker::trajectory_entrance_time(int k) {
318 return (_graph->paths[k]->nodes[1] - 1) / (2 * _nb_locations);
321 int Tracker::trajectory_duration(int k) {
322 return (_graph->paths[k]->nb_nodes - 2) / 2;
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;