MachineIntelligenceCore:Visualization
WindowMNISTDigit.cpp
Go to the documentation of this file.
1 
25 
26 namespace mic {
27 namespace opengl {
28 namespace visualization {
29 
31  unsigned int position_x_, unsigned int position_y_,
32  unsigned int width_ , unsigned int height_) :
33  Window(name_, position_x_, position_y_, width_, height_)
34 {
35  // NULL pointer.
36  displayed_digit = nullptr;
37 }
38 
39 
41  // TODO Auto-generated destructor stub
42 }
43 
44 
46  LOG(LTRACE) << "WindowMNISTDigit::Display handler of window " << glutGetWindow();
47 
48  // Enter critical section.
49  APP_DATA_SYNCHRONIZATION_SCOPED_LOCK();
50 
51  // Clear buffer.
52  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
53  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
54 
55  // Draw matrix 2d.
56  if (displayed_digit != nullptr){
57  // Get dimensions.
58  size_t w_tensor = displayed_digit->dim(0);
59  size_t h_tensor = displayed_digit->dim(1);
60  //size_t d_tensor = displayed_digit->dim(2);
61  size_t w_window = glutGet(GLUT_WINDOW_WIDTH);
62  size_t h_window = glutGet(GLUT_WINDOW_HEIGHT);
63 
64  // Compute scales.
65  float w_scale = (float) w_window / w_tensor;
66  float h_scale = (float) h_window / h_tensor;
67  float scale_min = (w_scale < h_scale) ? w_scale : h_scale;
68 
69 /* std::cout<< " width_tensor= "<< w_tensor << " height_tensor= "<< h_tensor<<std::endl;
70  std::cout<< " width_window= "<< w_window<< " height_window= "<< h_window <<std::endl;
71  std::cout<< " w_scale= "<< w_scale << " h_scale= "<< h_scale<< std::endl;*/
72 
73 
74  // Iterate through maze of digits elements.
75  for (size_t y = 0; y < h_tensor; y++) {
76  for (size_t x = 0; x < w_tensor; x++) {
77 
78  // Get value.
79  float r, g, b;
80  r = g = b = (*displayed_digit)({x,y, (size_t)MNISTDigitChannels::Pixels});
81 
82  // Draw rectangle.
83  draw_filled_rectangle(float(x) * w_scale, float(y) * h_scale, h_scale, w_scale, r, g, b, (float)1.0f);
84 
85  // Draw goal.
86  if ((*displayed_digit)({x,y, (size_t)MNISTDigitChannels::Goals})) {
87  // Draw circle.
88  r = 0.0; g = 0.0; b = 0.0;
89  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);
90  r = 1.0; g = 0.0; b = 0.0;
91  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);
92  }
93 
94  // Draw agent.
95  if ((*displayed_digit)({x,y, (size_t)MNISTDigitChannels::Agent})) {
96  // Draw circle.
97  r = 0.0; g = 0.0; b = 0.0;
98  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);
99  r = 1.0; g = 1.0; b = 1.0;
100  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);
101  }
102 
103  }//: for
104  }//: for
105 
106  // Draw grid on top.
107  draw_grid(0.5f, 0.3f, 0.3f, 0.3f, w_tensor, h_tensor);
108 
109  // Draw saccadic path.
110  if ((saccadic_path != nullptr) && (saccadic_path->size() > 1)){
111 
112  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);
113 
114  // White contour.
115  for(size_t i=1; i <saccadic_path->size(); i++) {
116  // Get points.
117  Position2D prev = (*saccadic_path)[i-1];
118  Position2D next = (*saccadic_path)[i];
119 
120  // Draw line between those two.
121  glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
122  glLineWidth(4.0);
123  glBegin(GL_LINES);
124  glVertex2i((float(prev.x) + 0.5) * w_scale, (float(prev.y) + 0.5) * h_scale);
125  glVertex2i((float(next.x) + 0.5) * w_scale, (float(next.y) + 0.5) * h_scale);
126  glEnd();
127 
128  //if (i != saccadic_path->size()-1)
129  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);
130 
131  }//: for
132 
133  // Green path.
134  glLineWidth(2.0);
135  glBegin(GL_LINES);
136  for(size_t i=1; i <saccadic_path->size(); i++) {
137  // Get points.
138  Position2D prev = (*saccadic_path)[i-1];
139  Position2D next = (*saccadic_path)[i];
140 
141  // Draw line between those two.
142  float g = 0.1 + 0.9*((float)i/saccadic_path->size());
143  glColor4f(0.0f, g, 0.0f, 1.0f);
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 
147  //if (i != saccadic_path->size()-1)
148  // 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);
149 
150  }//: for
151  glEnd();
152 
153  }//: if !null
154  }//: if !null
155 
156 
157  // Swap buffers.
158  glutSwapBuffers();
159 
160  // End of critical section.
161 }
162 
163 void WindowMNISTDigit::setDigitPointer(mic::types::TensorXfPtr displayed_digit_) {
164  // Enter critical section.
165  APP_DATA_SYNCHRONIZATION_SCOPED_LOCK();
166 
167  (displayed_digit) = (displayed_digit_);
168  // End of critical section.
169 }
170 
171 void WindowMNISTDigit::setPathPointer(std::shared_ptr<std::vector <mic::types::Position2D> > saccadic_path_) {
172  // Enter critical section.
173  APP_DATA_SYNCHRONIZATION_SCOPED_LOCK();
174 
175  saccadic_path = saccadic_path_;
176  // End of critical section.
177 }
178 
179 } /* namespace visualization */
180 } /* namespace opengl */
181 } /* namespace mic */
void draw_circle(float x, float y, float size, float line_width, float r, float g, float b, float a)
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.
void setPathPointer(std::shared_ptr< std::vector< mic::types::Position2D > > saccadic_path_)
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)
Channel storing image intensities (this is a grayscale image)
void draw_grid(float r, float g, float b, float a, float cells_h, float cells_v, float line_width_=1.0)
WindowMNISTDigit(std::string name_="MNISTDigit", unsigned int position_x_=0, unsigned int position_y_=0, unsigned int width_=512, unsigned int height_=512)
Declaration of WindowManager class along with a bunch of helpful types and macros.
void setDigitPointer(mic::types::TensorXfPtr displayed_digit_)