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 Francois Fleuret //
16 // (C) Ecole Polytechnique Federale de Lausanne //
17 // Contact <pom@epfl.ch> for comments & bug reports //
18 //////////////////////////////////////////////////////////////////////////////////
29 #include "pom_solver.h"
31 void check_parameter(char *s, int line_number, char *buffer) {
33 cerr << "Missing parameter line " << line_number << ":" << endl;
34 cerr << buffer << endl;
39 int main(int argc, char **argv) {
42 cerr << argv[0] << " [-h | --help | <configuration file>]" << endl;
46 ifstream *configuration_file = 0;
47 istream *input_stream;
50 if(strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
51 cout << argv[0] << " [-h | --help | <configuration file>]" << endl
53 << " If a configuration file name is provided, the program processes it" << endl
54 << " and prints information about the files it generates. Otherwise, it" << endl
55 << " reads the standard input and does not produce any printout on the" << endl
56 << " standard output unless an error occurs." << endl
60 configuration_file = new ifstream(argv[1]);
61 if(configuration_file->fail()) {
62 cerr << "Can not open " << argv[1] << " for reading." << endl;
65 input_stream = configuration_file;
66 } else input_stream = &cin;
68 char input_view_format[buffer_size] = "";
69 char result_format[buffer_size] = "";
70 char result_view_format[buffer_size] = "";
71 char convergence_view_format[buffer_size] = "";
73 char buffer[buffer_size], token[buffer_size];
76 Vector<ProbaView *> *proba_views = 0;
80 while(!input_stream->eof()) {
82 input_stream->getline(buffer, buffer_size);
86 s = next_word(token, s, buffer_size);
88 if(strcmp(token, "ROOM") == 0) {
89 int view_width = -1, view_height = -1;
90 int nb_positions = -1;
93 check_parameter(s, line_number, buffer);
94 s = next_word(token, s, buffer_size);
95 view_width = atoi(token);
97 check_parameter(s, line_number, buffer);
98 s = next_word(token, s, buffer_size);
99 view_height = atoi(token);
101 check_parameter(s, line_number, buffer);
102 s = next_word(token, s, buffer_size);
103 nb_cameras = atoi(token);
105 check_parameter(s, line_number, buffer);
106 s = next_word(token, s, buffer_size);
107 nb_positions = atoi(token);
110 cerr << "Room already defined, line" << line_number << "." << endl;
114 room = new Room(view_width, view_height, nb_cameras, nb_positions);
115 proba_views = new Vector<ProbaView *>(nb_cameras);
116 for(int c = 0; c < proba_views->length(); c++)
117 (*proba_views)[c] = new ProbaView(view_width, view_height);
120 else if(strcmp(token, "CONVERGENCE_VIEW_FORMAT") == 0) {
121 check_parameter(s, line_number, buffer);
122 s = next_word(convergence_view_format, s, buffer_size);
125 else if(strcmp(token, "INPUT_VIEW_FORMAT") == 0) {
126 check_parameter(s, line_number, buffer);
127 s = next_word(input_view_format, s, buffer_size);
130 else if(strcmp(token, "RESULT_VIEW_FORMAT") == 0) {
131 check_parameter(s, line_number, buffer);
132 s = next_word(result_view_format, s, buffer_size);
135 else if(strcmp(token, "RESULT_FORMAT") == 0) {
136 check_parameter(s, line_number, buffer);
137 s = next_word(result_format, s, buffer_size);
140 else if(strcmp(token, "PROCESS") == 0) {
142 int first_frame, nb_frames;
144 check_parameter(s, line_number, buffer);
145 s = next_word(token, s, buffer_size);
146 first_frame = atoi(token);
148 check_parameter(s, line_number, buffer);
149 s = next_word(token, s, buffer_size);
150 nb_frames = atoi(token);
152 POMSolver solver(room);
154 Vector<scalar_t> prior(room->nb_positions());
155 Vector<scalar_t> proba_presence(room->nb_positions());
156 for(int i = 0; i < room->nb_positions(); i++) prior[i] = global_prior;
158 if(strcmp(input_view_format, "") == 0) {
159 cerr << "You must specify the input view format." << endl;
163 for(int f = first_frame; f < first_frame + nb_frames; f++) {
165 if(configuration_file)
166 cout << "Processing frame " << f << endl;
168 for(int c = 0; c < room->nb_cameras(); c++) {
169 pomsprintf(buffer, buffer_size, input_view_format, c, f, 0);
170 tmp.read_png(buffer);
171 (*proba_views)[c]->from_image(&tmp);
174 if(strcmp(convergence_view_format, "") != 0)
175 solver.solve(room, &prior, proba_views, &proba_presence, f, convergence_view_format);
177 solver.solve(room, &prior, proba_views, &proba_presence, f, 0);
179 if(strcmp(result_view_format, "") != 0) {
180 for(int c = 0; c < room->nb_cameras(); c++) {
181 pomsprintf(buffer, buffer_size, result_view_format, c, f, 0);
182 if(configuration_file)
183 cout << "Saving " << buffer << endl;
184 room->save_stochastic_view(buffer, c, (*proba_views)[c], &proba_presence);
188 if(strcmp(result_format, "") != 0) {
189 pomsprintf(buffer, buffer_size, result_format, 0, f, 0);
190 ofstream result(buffer);
192 cerr << "Can not open " << token << " for writing." << endl;
195 if(configuration_file)
196 cout << "Saving " << buffer << endl;
197 for(int i = 0; i < room->nb_positions(); i++)
198 result << i << " " << proba_presence[i] << endl;
204 else if(strcmp(token, "RECTANGLE") == 0) {
205 int n_camera, n_position;
208 cerr << "You must define a room before adding rectangles, line" << line_number << "." << endl;
212 check_parameter(s, line_number, buffer);
213 s = next_word(token, s, buffer_size);
214 n_camera = atoi(token);
216 if(n_camera < 0 || n_camera >= room->nb_cameras()) {
217 cerr << "Out of range camera number line " << line_number << "." << endl;
221 check_parameter(s, line_number, buffer);
222 s = next_word(token, s, buffer_size);
223 n_position = atoi(token);
225 if(n_position < 0 || n_camera >= room->nb_positions()) {
226 cerr << "Out of range position number line " << line_number << "." << endl;
230 Rectangle *current = room->avatar(n_camera, n_position);
232 check_parameter(s, line_number, buffer);
233 s = next_word(token, s, buffer_size);
234 if(strcmp(token, "notvisible") == 0) {
235 current->visible = false;
241 current->visible = true;
242 current->xmin = atoi(token);
243 check_parameter(s, line_number, buffer);
244 s = next_word(token, s, buffer_size);
245 current->ymin = atoi(token);
246 check_parameter(s, line_number, buffer);
247 s = next_word(token, s, buffer_size);
248 current->xmax = atoi(token);
249 check_parameter(s, line_number, buffer);
250 s = next_word(token, s, buffer_size);
251 current->ymax = atoi(token);
253 if(current->xmin < 0 || current->xmax >= room->view_width() ||
254 current->ymin < 0 || current->ymax >= room->view_height()) {
255 cerr << "Rectangle out of bounds, line " << line_number << endl;
261 else if(strcmp(token, "PRIOR") == 0) {
262 check_parameter(s, line_number, buffer);
263 s = next_word(token, s, buffer_size);
264 global_prior = atof(token);
267 else if(strcmp(token, "SIGMA_IMAGE_DENSITY") == 0) {
268 check_parameter(s, line_number, buffer);
269 s = next_word(token, s, buffer_size);
270 global_sigma_image_density = atof(token);
273 else if(strcmp(token, "SMOOTHING_COEFFICIENT") == 0) {
274 check_parameter(s, line_number, buffer);
275 s = next_word(token, s, buffer_size);
276 global_smoothing_coefficient = atof(token);
279 else if(strcmp(token, "MAX_NB_SOLVER_ITERATIONS") == 0) {
280 check_parameter(s, line_number, buffer);
281 s = next_word(token, s, buffer_size);
282 global_max_nb_solver_iterations = atoi(token);
285 else if(strcmp(token, "ERROR_MAX") == 0) {
286 check_parameter(s, line_number, buffer);
287 s = next_word(token, s, buffer_size);
288 global_error_max = atof(token);
291 else if(strcmp(token, "NB_STABLE_ERROR_FOR_CONVERGENCE") == 0) {
292 check_parameter(s, line_number, buffer);
293 s = next_word(token, s, buffer_size);
294 global_nb_stable_error_for_convergence = atoi(token);
297 else if(strcmp(token, "PROBA_IGNORED") == 0) {
298 check_parameter(s, line_number, buffer);
299 s = next_word(token, s, buffer_size);
300 global_proba_ignored = atof(token);
303 else if(strcmp(buffer, "") == 0 || buffer[0] == '#') { }
306 cerr << "Unknown token " << token << "." << endl;
312 for(int c = 0; c < proba_views->length(); c++) delete (*proba_views)[c];
317 delete configuration_file;