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 Tracker::Tracker(int nb_time_steps, int nb_locations) {
26 _nb_locations = nb_locations;
27 _nb_time_steps = nb_time_steps;
29 detection_score = allocate_array<scalar_t>(_nb_time_steps, _nb_locations);
30 allowed_motion = allocate_array<int>(_nb_locations, _nb_locations);
32 entrances = new int[_nb_locations];
33 exits = new int[_nb_locations];
35 for(int l = 0; l < nb_locations; l++) {
38 for(int m = 0; m < nb_locations; m++) {
39 allowed_motion[l][m] = 0;
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;
54 delete[] _edge_lengths;
56 deallocate_array<scalar_t>(detection_score);
57 deallocate_array<int>(allowed_motion);
62 void Tracker::build_graph() {
63 // Delete existing graph if there was one
64 delete[] _edge_lengths;
67 int nb_motions = 0, nb_exits = 0, nb_entrances = 0;
69 for(int l = 0; l < _nb_locations; l++) {
70 if(exits[l]) nb_exits++;
71 if(entrances[l]) nb_entrances++;
72 for(int m = 0; m < _nb_locations; m++) {
73 if(allowed_motion[l][m]) nb_motions++;
77 int nb_vertices = 2 + 2 * _nb_time_steps * _nb_locations;
80 // The edges from the source to the first frame, and from the last
83 // The edges from the source to the entrances and from the exists
84 // to the sink (in every time frames but the first for the source,
85 // and last for the exits)
86 (_nb_time_steps - 1) * (nb_exits + nb_entrances) +
87 // The edges for the motions, between every pair of successive
89 (_nb_time_steps - 1) * nb_motions +
90 // The edges inside the duplicated nodes
91 _nb_locations * _nb_time_steps;
93 int *node_from = new int[nb_edges];
94 int *node_to = new int[nb_edges];
96 int source = 0, sink = nb_vertices - 1;
99 _edge_lengths = new scalar_t[nb_edges];
101 // We put the in-node edges first, since these are the ones whose
102 // lengths we will have to set later, according to the detection
105 for(int t = 0; t < _nb_time_steps; t++) {
106 for(int l = 0; l < _nb_locations; l++) {
107 node_from[e] = 1 + (2 * (t + 0) + 0) * _nb_locations + l;
108 node_to[e] = 1 + (2 * (t + 0) + 1) * _nb_locations + l;
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;
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;
125 _edge_lengths[e] = 0.0;
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;
140 for(int t = 0; t < _nb_time_steps; t++) {
141 for(int l = 0; l < _nb_locations; l++) {
142 if(t > 0 && 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;
148 if(t < _nb_time_steps - 1 && exits[l]) {
149 node_from[e] = 1 + (2 * (t + 0) + 1) * _nb_locations + l;
151 _edge_lengths[e] = 0.0;
157 _graph = new MTPGraph(nb_vertices, nb_edges,
165 void Tracker::print_graph_dot(ostream *os) {
167 for(int t = 0; t < _nb_time_steps; t++) {
168 for(int l = 0; l < _nb_locations; l++) {
169 _edge_lengths[e++] = - detection_score[t][l];
172 _graph->print_dot(os);
175 void Tracker::track() {
177 for(int t = 0; t < _nb_time_steps; t++) {
178 for(int l = 0; l < _nb_locations; l++) {
179 _edge_lengths[e++] = - detection_score[t][l];
183 _graph->find_best_paths(_edge_lengths);
184 _graph->retrieve_disjoint_paths();
187 for(int p = 0; p < _graph->nb_paths; p++) {
188 Path *path = _graph->paths[p];
189 cout << "PATH " << p << " [length " << path->nb_nodes << "] " << path->nodes[0];
190 for(int n = 1; n < path->nb_nodes; n++) {
191 cout << " -> " << path->nodes[n];
198 int Tracker::nb_trajectories() {
199 return _graph->nb_paths;
202 scalar_t Tracker::trajectory_score(int k) {
203 return -_graph->paths[k]->length;
206 int Tracker::trajectory_entrance_time(int k) {
207 return (_graph->paths[k]->nodes[1] - 1) / (2 * _nb_locations);
210 int Tracker::trajectory_duration(int k) {
211 return (_graph->paths[k]->nb_nodes - 2) / 2;
214 int Tracker::trajectory_location(int k, int time_from_entry) {
215 return (_graph->paths[k]->nodes[2 * time_from_entry + 1] - 1) % _nb_locations;