MachineIntelligenceCore:Visualization
WindowMazeOfDigits.cpp
Go to the documentation of this file.
1 
26 
27 namespace mic {
28 namespace opengl {
29 namespace visualization {
30 
32  unsigned int position_x_, unsigned int position_y_,
33  unsigned int width_ , unsigned int height_) :
34  Window(name_, position_x_, position_y_, width_, height_)
35 {
36  // NULL pointer.
37  displayed_maze = nullptr;
38 }
39 
40 
42  // TODO Auto-generated destructor stub
43 }
44 
45 
47  LOG(LTRACE) << "WindowMazeOfDigits::Display handler of window " << glutGetWindow();
48 
49  // Enter critical section.
50  APP_DATA_SYNCHRONIZATION_SCOPED_LOCK();
51 
52  // Clear buffer.
53  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
54  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
55 
56  // Draw matrix 2d.
57  if (displayed_maze != nullptr){
58  // Get dimensions.
59  size_t w_tensor = displayed_maze->dim(0);
60  size_t h_tensor = displayed_maze->dim(1);
61  size_t d_tensor = displayed_maze->dim(2);
62  size_t w_window = glutGet(GLUT_WINDOW_WIDTH);
63  size_t h_window = glutGet(GLUT_WINDOW_HEIGHT);
64 
65  // Compute scales.
66  float w_scale = (float) w_window / w_tensor;
67  float h_scale = (float) h_window / h_tensor;
68  float scale_min = (w_scale < h_scale) ? w_scale : h_scale;
69 
70 /* std::cout<< " width_tensor= "<< w_tensor << " height_tensor= "<< h_tensor<<std::endl;
71  std::cout<< " width_window= "<< w_window<< " height_window= "<< h_window <<std::endl;
72  std::cout<< " w_scale= "<< w_scale << " h_scale= "<< h_scale<< std::endl;*/
73 
74 
75  // Iterate through maze of digits elements.
76  for (size_t y = 0; y < h_tensor; y++) {
77  for (size_t x = 0; x < w_tensor; x++) {
78 
79  // Get value.
80  float r, g, b;
81 
82  // Check cell.
83  if ((d_tensor > (size_t)MazeOfDigitsChannels::Walls) && (*displayed_maze)({x,y, (size_t)MazeOfDigitsChannels::Walls})) {
84  r = 0.0; g = 0.0; b = 0.0;
85  } else {
86  unsigned short digit = (*displayed_maze)({x,y, (size_t)MazeOfDigitsChannels::Digits});
87  switch(digit){
88  case 0: r = 0.0; g = 0.0; b = 0.3; break;
89  case 1: r = 0.0; g = 0.0; b = 0.6; break;
90  case 2: r = 0.0; g = 0.0; b = 0.9; break;
91  case 3: r = 0.0; g = 0.3; b = 1.0; break;
92  case 4: r = 0.0; g = 0.6; b = 0.6; break;
93  case 5: r = 0.0; g = 0.9; b = 0.3; break;
94  case 6: r = 1.0; g = 0.75; b = 0.0; break;
95  case 7: r = 1.0; g = 0.5; b = 0.0; break;
96  case 8: r = 1.0; g = 0.25; b = 0.0; break;
97  case 9: r = 1.0; g = 1.0; b = 1.0; break;
98  }
99  }//: else
100 
101  // Draw rectangle.
102  draw_filled_rectangle(float(x) * w_scale, float(y) * h_scale, h_scale, w_scale, r,g, b, (float)1.0f);
103 
104  // Draw goal.
105  if ((d_tensor > (size_t)MazeOfDigitsChannels::Goals) && (*displayed_maze)({x,y, (size_t)MazeOfDigitsChannels::Goals})) {
106  // Draw circle.
107  r = 0.0; g = 0.0; b = 0.0;
108  draw_cross((float(x) + 0.5)* w_scale, (float(y) + 0.5)* h_scale, scale_min/4, 4.0, r, g, b, (float)1.0f);
109  r = 1.0; g = 0.0; b = 0.0;
110  draw_cross((float(x) + 0.5)* w_scale, (float(y) + 0.5)* h_scale, scale_min/4, 2.0, r, g, b, (float)1.0f);
111  }
112 
113  // Draw agent.
114  if ((d_tensor > (size_t)MazeOfDigitsChannels::Agent) && (*displayed_maze)({x,y, (size_t)MazeOfDigitsChannels::Agent})) {
115  // Draw circle.
116  r = 0.0; g = 0.0; b = 0.0;
117  draw_circle((float(x) + 0.5)* w_scale, (float(y) + 0.5)* h_scale, scale_min/4, 4.0, r, g, b, (float)1.0f);
118  r = 1.0; g = 1.0; b = 1.0;
119  draw_circle((float(x) + 0.5)* w_scale, (float(y) + 0.5)* h_scale, scale_min/4, 2.0, r, g, b, (float)1.0f);
120  }
121 
122 
123  }//: for
124  }//: for
125 
126  // Draw grid on top.
127  draw_grid(0.5f, 0.3f, 0.3f, 0.3f, w_tensor, h_tensor);
128 
129  // Draw saccadic path.
130  if ((saccadic_path != nullptr) && (saccadic_path->size() > 1)){
131 
132  draw_circle((float((*saccadic_path)[0].x) + 0.5)* w_scale, (float((*saccadic_path)[0].y) + 0.5)* h_scale, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0);
133 
134  // White contour.
135  for(size_t i=1; i <saccadic_path->size(); i++) {
136  // Get points.
137  Position2D prev = (*saccadic_path)[i-1];
138  Position2D next = (*saccadic_path)[i];
139 
140  // Draw line between those two.
141  glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
142  glLineWidth(4.0);
143  glBegin(GL_LINES);
144  glVertex2i((float(prev.x) + 0.5) * w_scale, (float(prev.y) + 0.5) * h_scale);
145  glVertex2i((float(next.x) + 0.5) * w_scale, (float(next.y) + 0.5) * h_scale);
146  glEnd();
147 
148  //if (i != saccadic_path->size()-1)
149  draw_circle((float(next.x) + 0.5)* w_scale, (float(next.y) + 0.5)* h_scale, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0);
150 
151  }//: for
152 
153  // Green path.
154  glLineWidth(2.0);
155  glBegin(GL_LINES);
156  for(size_t i=1; i <saccadic_path->size(); i++) {
157  // Get points.
158  Position2D prev = (*saccadic_path)[i-1];
159  Position2D next = (*saccadic_path)[i];
160 
161  // Draw line between those two.
162  float g = 0.1 + 0.9*((float)i/saccadic_path->size());
163  glColor4f(0.0f, g, 0.0f, 1.0f);
164  glVertex2i((float(prev.x) + 0.5) * w_scale, (float(prev.y) + 0.5) * h_scale);
165  glVertex2i((float(next.x) + 0.5) * w_scale, (float(next.y) + 0.5) * h_scale);
166 
167  //if (i != saccadic_path->size()-1)
168  // draw_circle((float(next.x) + 0.5)* w_scale, (float(next.y) + 0.5)* h_scale, 1.0, 1.0, 0.0, g, 0.0, 1.0);
169 
170  }//: for
171  glEnd();
172 
173  }//: if !null
174 
175  }//: if !null
176 
177  // Swap buffers.
178  glutSwapBuffers();
179 
180  // End of critical section.
181 }
182 
183 void WindowMazeOfDigits::setMazePointer(mic::types::TensorXfPtr displayed_maze_) {
184  // Enter critical section.
185  APP_DATA_SYNCHRONIZATION_SCOPED_LOCK();
186 
187  (displayed_maze) = (displayed_maze_);
188  // End of critical section.
189 }
190 
191 void WindowMazeOfDigits::setPathPointer(std::shared_ptr<std::vector <mic::types::Position2D> > saccadic_path_) {
192  // Enter critical section.
193  APP_DATA_SYNCHRONIZATION_SCOPED_LOCK();
194 
195  saccadic_path = saccadic_path_;
196  // End of critical section.
197 }
198 
199 
200 } /* namespace visualization */
201 } /* namespace opengl */
202 } /* namespace mic */
void draw_circle(float x, float y, float size, float line_width, float r, float g, float b, float a)
WindowMazeOfDigits(std::string name_="MazeOfDigits", unsigned int position_x_=0, unsigned int position_y_=0, unsigned int width_=512, unsigned int height_=512)
void setMazePointer(mic::types::TensorXfPtr displayed_maze_)
void draw_filled_rectangle(float x, float y, float h, float w, float r, float g, float b, float a)
std::shared_ptr< std::vector< mic::types::Position2D > > saccadic_path
Saccadic path to be displayed - a sequence of consecutive agent positions.
Parent class of all OpenGL-based windows (abstract).
Definition: Window.hpp:51
void draw_cross(float x, float y, float size, float line_width, float r, float g, float b, float a)
void setPathPointer(std::shared_ptr< std::vector< mic::types::Position2D > > saccadic_path_)
void draw_grid(float r, float g, float b, float a, float cells_h, float cells_v, float line_width_=1.0)
Declaration of WindowManager class along with a bunch of helpful types and macros.