X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?p=mtp.git;a=blobdiff_plain;f=mtp_tracker.cc;h=8cc45179fbd9217b58aa2336e777d5aee8cd451d;hp=825b4d1f904b5e34aa66d87831d05208e0f6faeb;hb=HEAD;hpb=513b8c10f328e5868082f09063c7e51669c952c7 diff --git a/mtp_tracker.cc b/mtp_tracker.cc index 825b4d1..8cc4517 100644 --- a/mtp_tracker.cc +++ b/mtp_tracker.cc @@ -32,34 +32,34 @@ void MTPTracker::free() { delete[] _edge_lengths; delete _graph; deallocate_array(detection_scores); - deallocate_array(allowed_motion); - delete[] exits; - delete[] entrances; + deallocate_array(allowed_motions); + deallocate_array(exits); + deallocate_array(entrances); } -void MTPTracker::allocate(int nb_time_steps, int nb_locations) { +void MTPTracker::allocate(int t, int l) { free(); - _nb_locations = nb_locations; - _nb_time_steps = nb_time_steps; + nb_locations = l; + nb_time_steps = t; - detection_scores = allocate_array(_nb_time_steps, _nb_locations); - allowed_motion = allocate_array(_nb_locations, _nb_locations); + detection_scores = allocate_array(nb_time_steps, nb_locations); + allowed_motions = allocate_array(nb_locations, nb_locations); - entrances = new int[_nb_locations]; - exits = new int[_nb_locations]; + entrances = allocate_array(nb_time_steps, nb_locations); + exits = allocate_array(nb_time_steps, nb_locations); - for(int l = 0; l < _nb_locations; l++) { - entrances[l] = 0; - exits[l] = 0; - for(int m = 0; m < _nb_locations; m++) { - allowed_motion[l][m] = 0; + for(int l = 0; l < nb_locations; l++) { + for(int m = 0; m < nb_locations; m++) { + allowed_motions[l][m] = 0; } } - for(int t = 0; t < _nb_time_steps; t++) { - for(int l = 0; l < _nb_locations; l++) { + for(int t = 0; t < nb_time_steps; t++) { + for(int l = 0; l < nb_locations; l++) { detection_scores[t][l] = 0.0; + entrances[t][l] = 0; + exits[t][l] = 0; } } @@ -68,64 +68,72 @@ void MTPTracker::allocate(int nb_time_steps, int nb_locations) { } void MTPTracker::write(ostream *os) { - (*os) << _nb_locations << " " << _nb_time_steps <> nb_locations >> nb_time_steps; + (*is) >> l >> t; - allocate(nb_time_steps, nb_locations); + allocate(t, l); - for(int l = 0; l < _nb_locations; l++) { - for(int m = 0; m < _nb_locations; m++) { - (*is) >> allowed_motion[l][m]; + for(int l = 0; l < nb_locations; l++) { + for(int m = 0; m < nb_locations; m++) { + (*is) >> allowed_motions[l][m]; } } - for(int l = 0; l < _nb_locations; l++) { - (*is) >> entrances[l]; + for(int t = 0; t < nb_time_steps; t++) { + for(int l = 0; l < nb_locations; l++) { + (*is) >> entrances[t][l]; + } } - for(int l = 0; l < _nb_locations; l++) { - (*is) >> exits[l]; + for(int t = 0; t < nb_time_steps; t++) { + for(int l = 0; l < nb_locations; l++) { + (*is) >> exits[t][l]; + } } - for(int t = 0; t < _nb_time_steps; t++) { - for(int l = 0; l < _nb_locations; l++) { + for(int t = 0; t < nb_time_steps; t++) { + for(int l = 0; l < nb_locations; l++) { (*is) >> detection_scores[t][l]; } } @@ -146,11 +154,11 @@ void MTPTracker::write_trajectories(ostream *os) { } MTPTracker::MTPTracker() { - _nb_locations = 0; - _nb_time_steps = 0; + nb_locations = 0; + nb_time_steps = 0; detection_scores = 0; - allowed_motion = 0; + allowed_motions = 0; entrances = 0; exits = 0; @@ -160,20 +168,15 @@ MTPTracker::MTPTracker() { } MTPTracker::~MTPTracker() { - delete[] _edge_lengths; - delete _graph; - deallocate_array(detection_scores); - deallocate_array(allowed_motion); - delete[] exits; - delete[] entrances; + free(); } int MTPTracker::early_pair_node(int t, int l) { - return 1 + (2 * t + 0) * _nb_locations + l; + return 1 + (2 * t + 0) * nb_locations + l; } int MTPTracker::late_pair_node(int t, int l) { - return 1 + (2 * t + 1) * _nb_locations + l; + return 1 + (2 * t + 1) * nb_locations + l; } void MTPTracker::build_graph() { @@ -183,28 +186,26 @@ void MTPTracker::build_graph() { int nb_motions = 0, nb_exits = 0, nb_entrances = 0; - for(int l = 0; l < _nb_locations; l++) { - if(exits[l]) nb_exits++; - if(entrances[l]) nb_entrances++; - for(int m = 0; m < _nb_locations; m++) { - if(allowed_motion[l][m]) nb_motions++; + for(int l = 0; l < nb_locations; l++) { + for(int t = 0; t < nb_time_steps; t++) { + if(exits[t][l]) nb_exits++; + if(entrances[t][l]) nb_entrances++; + } + for(int m = 0; m < nb_locations; m++) { + if(allowed_motions[l][m]) nb_motions++; } } - int nb_vertices = 2 + 2 * _nb_time_steps * _nb_locations; + int nb_vertices = 2 + 2 * nb_time_steps * nb_locations; int nb_edges = - // The edges from the source to the first frame, and from the last - // frame to the sink - _nb_locations * 2 + // The edges from the source to the entrances and from the exits - // to the sink (in every time frames but the first for the - // entrances, and last for the exits) - (_nb_time_steps - 1) * (nb_exits + nb_entrances) + + // to the sink + nb_exits + nb_entrances + // The edges for the motions, between every successive frames - (_nb_time_steps - 1) * nb_motions + + (nb_time_steps - 1) * nb_motions + // The edges inside the duplicated nodes - _nb_locations * _nb_time_steps; + nb_locations * nb_time_steps; int *node_from = new int[nb_edges]; int *node_to = new int[nb_edges]; @@ -218,38 +219,20 @@ void MTPTracker::build_graph() { // lengths we will have to change before tracking, according to the // detection scores - for(int t = 0; t < _nb_time_steps; t++) { - for(int l = 0; l < _nb_locations; l++) { + for(int t = 0; t < nb_time_steps; t++) { + for(int l = 0; l < nb_locations; l++) { node_from[e] = early_pair_node(t, l); node_to[e] = late_pair_node(t, l); e++; } } - // The edges from the source to the first time frame - - for(int l = 0; l < _nb_locations; l++) { - node_from[e] = source; - node_to[e] = 1 + l + 0 * _nb_locations; - _edge_lengths[e] = 0.0; - e++; - } - - // The edges from the last frame to the sink - - for(int l = 0; l < _nb_locations; l++) { - node_from[e] = late_pair_node(_nb_time_steps - 1, l); - node_to[e] = sink; - _edge_lengths[e] = 0.0; - e++; - } - // The edges between frames, corresponding to allowed motions - for(int t = 0; t < _nb_time_steps - 1; t++) { - for(int l = 0; l < _nb_locations; l++) { - for(int k = 0; k < _nb_locations; k++) { - if(allowed_motion[l][k]) { + for(int t = 0; t < nb_time_steps - 1; t++) { + for(int l = 0; l < nb_locations; l++) { + for(int k = 0; k < nb_locations; k++) { + if(allowed_motions[l][k]) { node_from[e] = late_pair_node(t, l); node_to[e] = early_pair_node(t+1, k); _edge_lengths[e] = 0.0; @@ -262,15 +245,15 @@ void MTPTracker::build_graph() { // The edges from the source to the entrances, and from the exits to // the sink - for(int t = 0; t < _nb_time_steps; t++) { - for(int l = 0; l < _nb_locations; l++) { - if(t > 0 && entrances[l]) { + for(int t = 0; t < nb_time_steps; t++) { + for(int l = 0; l < nb_locations; l++) { + if(entrances[t][l]) { node_from[e] = source; node_to[e] = early_pair_node(t, l); _edge_lengths[e] = 0.0; e++; } - if(t < _nb_time_steps - 1 && exits[l]) { + if(exits[t][l]) { node_from[e] = late_pair_node(t, l); node_to[e] = sink; _edge_lengths[e] = 0.0; @@ -291,8 +274,8 @@ void MTPTracker::build_graph() { void MTPTracker::print_graph_dot(ostream *os) { int e = 0; - for(int t = 0; t < _nb_time_steps; t++) { - for(int l = 0; l < _nb_locations; l++) { + for(int t = 0; t < nb_time_steps; t++) { + for(int l = 0; l < nb_locations; l++) { _edge_lengths[e++] = - detection_scores[t][l]; } } @@ -303,8 +286,8 @@ void MTPTracker::track() { ASSERT(_graph); int e = 0; - for(int t = 0; t < _nb_time_steps; t++) { - for(int l = 0; l < _nb_locations; l++) { + for(int t = 0; t < nb_time_steps; t++) { + for(int l = 0; l < nb_locations; l++) { _edge_lengths[e++] = - detection_scores[t][l]; } } @@ -333,7 +316,7 @@ scalar_t MTPTracker::trajectory_score(int k) { } int MTPTracker::trajectory_entrance_time(int k) { - return (_graph->paths[k]->nodes[1] - 1) / (2 * _nb_locations); + return (_graph->paths[k]->nodes[1] - 1) / (2 * nb_locations); } int MTPTracker::trajectory_duration(int k) { @@ -341,5 +324,5 @@ int MTPTracker::trajectory_duration(int k) { } int MTPTracker::trajectory_location(int k, int time_from_entry) { - return (_graph->paths[k]->nodes[2 * time_from_entry + 1] - 1) % _nb_locations; + return (_graph->paths[k]->nodes[2 * time_from_entry + 1] - 1) % nb_locations; }