Now parses options from the command line.
[mtp.git] / mtp.cc
1
2 /*
3  *  mtp is the ``Multi Tracked Paths'', an implementation of the
4  *  k-shortest paths algorithm for multi-target tracking.
5  *
6  *  Copyright (c) 2012 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of mtp.
10  *
11  *  mtp is free software: you can redistribute it and/or modify it
12  *  under the terms of the GNU General Public License version 3 as
13  *  published by the Free Software Foundation.
14  *
15  *  mtp is distributed in the hope that it will be useful, but WITHOUT
16  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
18  *  License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with selector.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include <iostream>
26 #include <fstream>
27 #include <sys/time.h>
28 #include <getopt.h>
29 #include <string.h>
30
31 using namespace std;
32
33 #include "mtp_tracker.h"
34
35 #define FILENAME_SIZE 1024
36
37 char trajectory_filename[FILENAME_SIZE];
38 char graph_filename[FILENAME_SIZE];
39 int verbose;
40
41 void usage(ostream *os) {
42   (*os) << "mtp [-h|--help] [-v|--verbose] [-t|--trajectory-filename <trajectory filename>] [-g|--graph-filename <graph filename>] [<tracking parameter file>]" << endl;
43 }
44
45 scalar_t diff_in_second(struct timeval *start, struct timeval *end) {
46   return
47     scalar_t(end->tv_sec - start->tv_sec) +
48     scalar_t(end->tv_usec - start->tv_usec)/1000000;
49 }
50
51 void do_tracking(istream *in_tracker) {
52   timeval start_time, end_time;
53   MTPTracker *tracker = new MTPTracker();
54
55   if(verbose) { cout << "Reading the tracking parameters." << endl; }
56   tracker->read(in_tracker);
57
58   if(verbose) { cout << "Building the graph ... "; cout.flush(); }
59   gettimeofday(&start_time, 0);
60   tracker->build_graph();
61   gettimeofday(&end_time, 0);
62   if(verbose) { cout << "done (" << diff_in_second(&start_time, &end_time) << "s)." << endl; }
63
64   if(verbose) { cout << "Tracking ... "; cout.flush(); }
65   gettimeofday(&start_time, 0);
66   tracker->track();
67   gettimeofday(&end_time, 0);
68   if(verbose) { cout << "done (" << diff_in_second(&start_time, &end_time) << "s)." << endl; }
69
70   if(strcmp(trajectory_filename, "")) {
71     ofstream out_traj(trajectory_filename);
72     tracker->write_trajectories(&out_traj);
73     if(verbose) { cout << "Wrote " << trajectory_filename << "." << endl; }
74   } else {
75     tracker->write_trajectories(&cout);
76   }
77
78   if(strcmp(graph_filename, "") != 0) {
79     ofstream out_dot(graph_filename);
80     tracker->print_graph_dot(&out_dot);
81     if(verbose) { cout << "Wrote " << graph_filename << "." << endl; }
82   }
83
84   delete tracker;
85 }
86
87 static struct option long_options[] = {
88   { "trajectory-file", 1, 0, 't' },
89   { "graph-file", 1, 0, 'g' },
90   { "help", no_argument, 0, 'h' },
91   { "verbose", no_argument, 0, 'v' },
92   { 0, 0, 0, 0 }
93 };
94
95 int main(int argc, char **argv) {
96   int c;
97   int error = 0, show_help = 0;
98
99   // strncpy(trajectory_filename, "result.trj", FILENAME_SIZE);
100   strncpy(trajectory_filename, "", FILENAME_SIZE);
101   strncpy(graph_filename, "", FILENAME_SIZE);
102   verbose = 0;
103
104   while ((c = getopt_long(argc, argv, "t:g:hv",
105                           long_options, NULL)) != -1) {
106
107     switch(c) {
108
109     case 't':
110       strncpy(trajectory_filename, optarg, FILENAME_SIZE);
111       break;
112
113     case 'g':
114       strncpy(graph_filename, optarg, FILENAME_SIZE);
115       break;
116
117     case 'h':
118       show_help = 1;
119       break;
120
121     case 'v':
122       verbose = 1;
123       break;
124
125     default:
126       error = 1;
127       break;
128     }
129   }
130
131   if(error) {
132     usage(&cerr);
133     exit(EXIT_FAILURE);
134   }
135
136   if(show_help) {
137     usage(&cout);
138     exit(EXIT_SUCCESS);
139   }
140
141   if(optind < argc) {
142     ifstream *file_in_tracker = new ifstream(argv[optind]);
143     if(file_in_tracker->good()) {
144       do_tracking(file_in_tracker);
145     } else {
146       cerr << "Can not open " << argv[optind] << endl;
147       exit(EXIT_FAILURE);
148     }
149     delete file_in_tracker;
150   } else {
151     do_tracking(&cin);
152   }
153
154   exit(EXIT_SUCCESS);
155 }