Properly puts the edge occupancy back to 0 when starting tracking.
[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   _detection_score = allocate_array<scalar_t>(_nb_time_steps, _nb_locations);
29   _allowed_motion = allocate_array<int>(_nb_locations, _nb_locations);
30   _entrances = new int[_nb_locations];
31   _exits = new int[_nb_locations];
32
33   for(int l = 0; l < nb_locations; l++) {
34     _entrances[l] = 0;
35     _exits[l] = 0;
36     for(int m = 0; m < nb_locations; m++) {
37       _allowed_motion[l][m] = 0;
38     }
39   }
40
41   _edge_lengths = 0;
42   _graph = 0;
43 }
44
45 Tracker::~Tracker() {
46   delete[] _edge_lengths;
47   delete _graph;
48   deallocate_array<scalar_t>(_detection_score);
49   deallocate_array<int>(_allowed_motion);
50   delete[] _exits;
51   delete[] _entrances;
52 }
53
54 void Tracker::set_allowed_motion(int from_location, int to_location, int v) {
55   _allowed_motion[from_location][to_location] = v;
56 }
57
58 void Tracker::set_as_entrance(int location, int v) {
59   _entrances[location] = v;
60 }
61
62 void Tracker::set_as_exit(int location, int v) {
63   _exits[location] = v;
64 }
65
66 void Tracker::set_detection_score(int time, int location, scalar_t score) {
67   _detection_score[time][location] = score;
68 }
69
70 void Tracker::build_graph() {
71
72   // Delete existing graph
73   delete[] _edge_lengths;
74   delete _graph;
75
76   int nb_motions = 0, nb_exits = 0, nb_entrances = 0;
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     _nb_locations * 2 +
89     _nb_time_steps * (nb_exits + nb_entrances) +
90     (_nb_time_steps - 1) * nb_motions +
91     _nb_locations * _nb_time_steps;
92
93   int source = 0, sink = nb_vertices - 1;
94   int *node_from = new int[nb_edges];
95   int *node_to = new int[nb_edges];
96   int e = 0;
97
98   _edge_lengths = new scalar_t[nb_edges];
99
100   // We put the in-node edges first, since these are the ones whose
101   // lengths we will have to change according to the detection score
102
103   for(int t = 0; t < _nb_time_steps; t++) {
104     for(int l = 0; l < _nb_locations; l++) {
105       node_from[e] = 1 + (2 * (t + 0) + 0) * _nb_locations + l;
106       node_to[e] =   1 + (2 * (t + 0) + 1) * _nb_locations + l;
107       e++;
108     }
109   }
110
111   // We put the other edges after
112
113   for(int l = 0; l < _nb_locations; l++) {
114     node_from[e] = source;
115     node_to[e] = 1 + l + 0 * _nb_locations;
116     _edge_lengths[e] = 0.0;
117     e++;
118   }
119
120   for(int t = 0; t < _nb_time_steps; t++) {
121     for(int l = 0; l < _nb_locations; l++) {
122       if(t == _nb_time_steps - 1) {
123         node_from[e] = 1 + (2 * (t + 0) + 1) * _nb_locations + l;
124         node_to[e] = sink;
125         _edge_lengths[e] = 0.0;
126         e++;
127       } else {
128         for(int k = 0; k < _nb_locations; k++) {
129           if(_allowed_motion[l][k]) {
130             node_from[e] = 1 + (2 * (t + 0) + 1) * _nb_locations + l;
131             node_to[e] =   1 + (2 * (t + 1) + 0) * _nb_locations + k;
132             _edge_lengths[e] = 0.0;
133             e++;
134           }
135         }
136       }
137     }
138   }
139
140   for(int t = 0; t < _nb_time_steps; t++) {
141     for(int l = 0; l < _nb_locations; l++) {
142       if(_entrances[l]) {
143         node_from[e] = source;
144         node_to[e] =   1 + (2 * (t + 0) + 0) * _nb_locations + l;
145         _edge_lengths[e] = 0.0;
146         e++;
147       }
148       if(_exits[l]) {
149         node_from[e] =   1 + (2 * (t + 0) + 1) * _nb_locations + l;
150         node_from[e] = sink;
151         _edge_lengths[e] = 0.0;
152         e++;
153       }
154     }
155   }
156
157   _graph = new MTPGraph(nb_vertices, nb_edges,
158                         node_from, node_to,
159                         source, sink);
160
161   delete[] node_from;
162   delete[] node_to;
163 }
164
165 void Tracker::print_dot_graph(ostream *os) {
166   _graph->print_dot(os);
167 }
168
169 void Tracker::track() {
170   int e = 0;
171   for(int t = 0; t < _nb_time_steps; t++) {
172     for(int l = 0; l < _nb_locations; l++) {
173       _edge_lengths[e++] = - _detection_score[t][l];
174     }
175   }
176
177   _graph->find_best_paths(_edge_lengths);
178   _graph->retrieve_disjoint_paths();
179 }
180
181 int Tracker::nb_trajectories() {
182   return _graph->nb_paths;
183 }
184
185 int Tracker::trajectory_duration(int k) {
186   return (_graph->paths[k]->length - 2) / 2;
187 }
188
189 int Tracker::trajectory_location(int k, int time) {
190   return (_graph->paths[k]->nodes[2 * time + 1] - 1) % _nb_locations;
191 }