From 9665f6b8d842f02facda1590200742482d4362fa Mon Sep 17 00:00:00 2001 From: Francois Fleuret Date: Sat, 25 Aug 2012 07:27:21 +0200 Subject: [PATCH] Preparing to make mtp be a command line using an input file. --- mtp_command.cc | 61 ++++++++++++++++++++++ mtp.cc => mtp_example.cc | 17 +++++- tracker.cc | 110 +++++++++++++++++++++++++++++++++++++-- tracker.h | 9 +++- 4 files changed, 192 insertions(+), 5 deletions(-) create mode 100644 mtp_command.cc rename mtp.cc => mtp_example.cc (91%) diff --git a/mtp_command.cc b/mtp_command.cc new file mode 100644 index 0000000..2f9e7a9 --- /dev/null +++ b/mtp_command.cc @@ -0,0 +1,61 @@ + +/////////////////////////////////////////////////////////////////////////// +// This program is free software: you can redistribute it and/or modify // +// it under the terms of the version 3 of the GNU General Public License // +// as published by the Free Software Foundation. // +// // +// This program is distributed in the hope that it will be useful, but // +// WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // +// General Public License for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +// // +// Written by and Copyright (C) Francois Fleuret // +// Contact for comments & bug reports // +/////////////////////////////////////////////////////////////////////////// + +// Multi-Tracked Path + +#include +#include + +using namespace std; + +#include "tracker.h" + +int main(int argc, char **argv) { + + if(argc < 2) { + cerr << argv[0] << " " << endl; + exit(EXIT_FAILURE); + } + + Tracker tracker; + + ifstream in_tracker(argv[1]); + + if(in_tracker.good()) { + + tracker.read(&in_tracker); + tracker.build_graph(); + tracker.track(); + + ofstream out_traj("result.trj"); + tracker.write_trajectories(&out_traj); + cout << "Wrote result.trj" << endl; + + ofstream out_dot("graph.dot"); + tracker.print_graph_dot(&out_dot); + cout << "Wrote graph.dot" << endl; + + } else { + + cerr << "Can not open " << argv[1] << endl; + exit(EXIT_FAILURE); + + } + + exit(EXIT_SUCCESS); +} diff --git a/mtp.cc b/mtp_example.cc similarity index 91% rename from mtp.cc rename to mtp_example.cc index 7f55e0a..13cd4f1 100644 --- a/mtp.cc +++ b/mtp_example.cc @@ -40,7 +40,9 @@ int main(int argc, char **argv) { int nb_time_steps = 8; int motion_amplitude = 1; - Tracker *tracker = new Tracker(nb_time_steps, nb_locations); + Tracker *tracker = new Tracker(); + + tracker->allocate(nb_time_steps, nb_locations); // We define the spatial structures by stating what are the possible // motions of targets, and what are the entrances and the @@ -111,6 +113,19 @@ int main(int argc, char **argv) { // Does the tracking per se + { + ofstream out_tracker("/tmp/tracker.dat"); + tracker->write(&out_tracker); + + ifstream in_tracker("/tmp/tracker.dat"); + Tracker tracker2; + tracker2.read(&in_tracker); + tracker2.build_graph(); + tracker2.track(); + ofstream out_traj("/tmp/result.trj"); + tracker2.write_trajectories(&out_traj); + } + tracker->track(); // Prints the detected trajectories diff --git a/tracker.cc b/tracker.cc index 675ba2d..4d12e61 100644 --- a/tracker.cc +++ b/tracker.cc @@ -22,7 +22,18 @@ using namespace std; -Tracker::Tracker(int nb_time_steps, int nb_locations) { +void Tracker::free() { + delete[] _edge_lengths; + delete _graph; + deallocate_array(detection_scores); + deallocate_array(allowed_motion); + delete[] exits; + delete[] entrances; +} + +void Tracker::allocate(int nb_time_steps, int nb_locations) { + free(); + _nb_locations = nb_locations; _nb_time_steps = nb_time_steps; @@ -32,10 +43,10 @@ Tracker::Tracker(int nb_time_steps, int nb_locations) { entrances = new int[_nb_locations]; exits = new int[_nb_locations]; - for(int l = 0; l < nb_locations; l++) { + for(int l = 0; l < _nb_locations; l++) { entrances[l] = 0; exits[l] = 0; - for(int m = 0; m < nb_locations; m++) { + for(int m = 0; m < _nb_locations; m++) { allowed_motion[l][m] = 0; } } @@ -50,6 +61,97 @@ Tracker::Tracker(int nb_time_steps, int nb_locations) { _graph = 0; } +void Tracker::write(ostream *os) { + (*os) << _nb_locations << " " << _nb_time_steps <> nb_locations >> nb_time_steps; + + allocate(nb_time_steps, nb_locations); + + 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++) { + (*is) >> entrances[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) >> detection_scores[t][l]; + } + } +} + +void Tracker::write_trajectories(ostream *os) { + for(int t = 0; t < nb_trajectories(); t++) { + (*os) << t + << " " << trajectory_entrance_time(t) + << " " << trajectory_duration(t) + << " " << trajectory_score(t); + for(int u = 0; u < trajectory_duration(t); u++) { + (*os) << " " << trajectory_location(t, u); + } + (*os) << endl; + } +} + +Tracker::Tracker() { + _nb_locations = 0; + _nb_time_steps = 0; + + detection_scores = 0; + allowed_motion = 0; + + entrances = 0; + exits = 0; + + _edge_lengths = 0; + _graph = 0; +} + Tracker::~Tracker() { delete[] _edge_lengths; delete _graph; @@ -180,6 +282,8 @@ void Tracker::print_graph_dot(ostream *os) { } void Tracker::track() { + ASSERT(_graph); + int e = 0; for(int t = 0; t < _nb_time_steps; t++) { for(int l = 0; l < _nb_locations; l++) { diff --git a/tracker.h b/tracker.h index ce615b3..b56ac61 100644 --- a/tracker.h +++ b/tracker.h @@ -47,9 +47,16 @@ public: // The detection scores at each node scalar_t **detection_scores; - Tracker(int nb_time_steps, int nb_locations); + Tracker(); ~Tracker(); + void allocate(int nb_time_steps, int nb_locations); + void free(); + + void write(ostream *os); + void read(istream *is); + void write_trajectories(ostream *os); + // Build or print the graph needed for the tracking per se void build_graph(); -- 2.20.1