Added the images for test.
[pom.git] / room.cc
1
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.                                //
6 //                                                                              //
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.                                     //
11 //                                                                              //
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/>.         //
14 //                                                                              //
15 // Written by Francois Fleuret                                                  //
16 // (C) Ecole Polytechnique Federale de Lausanne                                 //
17 // Contact <pom@epfl.ch> for comments & bug reports                             //
18 //////////////////////////////////////////////////////////////////////////////////
19
20 #include <cmath>
21
22 #include "room.h"
23 #include "misc.h"
24
25 Room::Room(int view_width, int view_height, int nb_cameras, int nb_positions) {
26   _view_width = view_width;
27   _view_height = view_height;
28   _nb_cameras = nb_cameras;
29   _nb_positions = nb_positions;
30   _rectangles = new Rectangle[_nb_cameras * _nb_positions];
31 }
32
33 Room::~Room() {
34   delete[] _rectangles;
35 }
36
37 void Room::save_stochastic_view(char *name,
38                                 int n_camera,
39                                 const ProbaView *view,
40                                 const Vector<scalar_t> *proba_presence) const  {
41
42   RGBImage image(view->get_width(), view->get_height());
43
44   Array<scalar_t> proba_pixel_off(_view_width, _view_height);
45
46   for(int px = 0; px < _view_width; px++) for(int py = 0; py < _view_height; py++)
47     proba_pixel_off(px, py) = 1.0;
48
49   Array<bool> dots(_view_width, _view_height);
50   dots.clear();
51
52   for(int n = 0; n < nb_positions(); n++) {
53     Rectangle *r = avatar(n_camera, n);
54     if(r->visible) {
55       for(int py = r->ymin; py < r->ymax; py++)
56         for(int px = r->xmin; px < r->xmax; px++)
57           proba_pixel_off(px, py) *= (1 - (*proba_presence)[n]);
58       if(r->xmin > 0 && r->xmax < _view_width-1 && r->ymax < _view_height-1)
59         dots((r->xmax + r->xmin)/2, r->ymax) = true;
60     }
61   }
62
63   for(int py = 0; py < _view_height; py++) for(int px = 0; px < _view_width; px++) {
64     scalar_t r, g, b;
65     scalar_t a = proba_pixel_off(px, py);
66
67     if(dots(px, py)) { r = 0.0; g = 0.0; b = 0.0; }
68     else {
69       if(a < 0.5) { r = 0; g = 0; b = 2*a; }
70       else        { r = (a - 0.5) * 2; g = (a - 0.5) * 2; b = 1.0; }
71     }
72
73     scalar_t c = (*view)(px, py);
74
75     r = c * 0.0 + (1 - c) * r;
76     g = c * 0.8 + (1 - c) * g;
77     b = c * 0.6 + (1 - c) * b;
78
79     image.set_pixel(px, py, (unsigned char) (255 * r), (unsigned char) (255 * g), (unsigned char) (255 * b));
80   }
81
82   image.write_png(name);
83 }