From f419bacb4b11f42f87e6e9bc9e8a4719499559f2 Mon Sep 17 00:00:00 2001 From: Francois Fleuret Date: Sun, 23 Dec 2012 14:10:46 +0100 Subject: [PATCH] Now parses options from the command line. To get the same behavior as before, please use: ./mtp --verbose --trajectory-file result.trj --graph-file graph.dot < tracker.dat instead of simply ./mtp tracker.dat --- README.txt | 2 +- mtp.cc | 125 ++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 97 insertions(+), 30 deletions(-) diff --git a/README.txt b/README.txt index afe5cb2..894f59d 100644 --- a/README.txt +++ b/README.txt @@ -30,7 +30,7 @@ tracker.dat, and print the optimal detected trajectories. If you now execute - ./mtp tracker.dat + ./mtp --verbose --trajectory-file result.trj --graph-file graph.dot < tracker.dat It will load the file tracker.dat saved by the previous command, run the detection, save the detected trajectories in result.trj, and the diff --git a/mtp.cc b/mtp.cc index 61dcc21..957e56f 100644 --- a/mtp.cc +++ b/mtp.cc @@ -25,64 +25,131 @@ #include #include #include +#include +#include using namespace std; #include "mtp_tracker.h" +#define FILENAME_SIZE 1024 + +char trajectory_filename[FILENAME_SIZE]; +char graph_filename[FILENAME_SIZE]; +int verbose; + +void usage(ostream *os) { + (*os) << "mtp [-h|--help] [-v|--verbose] [-t|--trajectory-filename ] [-g|--graph-filename ] []" << endl; +} + scalar_t diff_in_second(struct timeval *start, struct timeval *end) { return scalar_t(end->tv_sec - start->tv_sec) + scalar_t(end->tv_usec - start->tv_usec)/1000000; } -int main(int argc, char **argv) { +void do_tracking(istream *in_tracker) { timeval start_time, end_time; + MTPTracker *tracker = new MTPTracker(); - if(argc < 2) { - cerr << argv[0] << " " << endl; - exit(EXIT_FAILURE); + if(verbose) { cout << "Reading the tracking parameters." << endl; } + tracker->read(in_tracker); + + if(verbose) { cout << "Building the graph ... "; cout.flush(); } + gettimeofday(&start_time, 0); + tracker->build_graph(); + gettimeofday(&end_time, 0); + if(verbose) { cout << "done (" << diff_in_second(&start_time, &end_time) << "s)." << endl; } + + if(verbose) { cout << "Tracking ... "; cout.flush(); } + gettimeofday(&start_time, 0); + tracker->track(); + gettimeofday(&end_time, 0); + if(verbose) { cout << "done (" << diff_in_second(&start_time, &end_time) << "s)." << endl; } + + if(strcmp(trajectory_filename, "")) { + ofstream out_traj(trajectory_filename); + tracker->write_trajectories(&out_traj); + if(verbose) { cout << "Wrote " << trajectory_filename << "." << endl; } + } else { + tracker->write_trajectories(&cout); + } + + if(strcmp(graph_filename, "") != 0) { + ofstream out_dot(graph_filename); + tracker->print_graph_dot(&out_dot); + if(verbose) { cout << "Wrote " << graph_filename << "." << endl; } } - ifstream *in_tracker = new ifstream(argv[1]); + delete tracker; +} - if(in_tracker->good()) { +static struct option long_options[] = { + { "trajectory-file", 1, 0, 't' }, + { "graph-file", 1, 0, 'g' }, + { "help", no_argument, 0, 'h' }, + { "verbose", no_argument, 0, 'v' }, + { 0, 0, 0, 0 } +}; - MTPTracker *tracker = new MTPTracker(); +int main(int argc, char **argv) { + int c; + int error = 0, show_help = 0; - cout << "Reading " << argv[1] << "." << endl; - tracker->read(in_tracker); + // strncpy(trajectory_filename, "result.trj", FILENAME_SIZE); + strncpy(trajectory_filename, "", FILENAME_SIZE); + strncpy(graph_filename, "", FILENAME_SIZE); + verbose = 0; - cout << "Building the graph ... "; cout.flush(); - gettimeofday(&start_time, 0); - tracker->build_graph(); - gettimeofday(&end_time, 0); - cout << "done (" << diff_in_second(&start_time, &end_time) << "s)." << endl; + while ((c = getopt_long(argc, argv, "t:g:hv", + long_options, NULL)) != -1) { - cout << "Tracking ... "; cout.flush(); - gettimeofday(&start_time, 0); - tracker->track(); - gettimeofday(&end_time, 0); - cout << "done (" << diff_in_second(&start_time, &end_time) << "s)." << endl; + switch(c) { - ofstream out_traj("result.trj"); - tracker->write_trajectories(&out_traj); - cout << "Wrote result.trj" << endl; + case 't': + strncpy(trajectory_filename, optarg, FILENAME_SIZE); + break; - ofstream out_dot("graph.dot"); - tracker->print_graph_dot(&out_dot); - cout << "Wrote graph.dot" << endl; + case 'g': + strncpy(graph_filename, optarg, FILENAME_SIZE); + break; - delete tracker; + case 'h': + show_help = 1; + break; - } else { + case 'v': + verbose = 1; + break; + + default: + error = 1; + break; + } + } - cerr << "Can not open " << argv[1] << endl; + if(error) { + usage(&cerr); exit(EXIT_FAILURE); + } + if(show_help) { + usage(&cout); + exit(EXIT_SUCCESS); } - delete in_tracker; + if(optind < argc) { + ifstream *file_in_tracker = new ifstream(argv[optind]); + if(file_in_tracker->good()) { + do_tracking(file_in_tracker); + } else { + cerr << "Can not open " << argv[optind] << endl; + exit(EXIT_FAILURE); + } + delete file_in_tracker; + } else { + do_tracking(&cin); + } exit(EXIT_SUCCESS); } -- 2.39.5