MachineIntelligenceCore:Visualization
WindowMatrix2D.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_matrix_ptr = nullptr;
37 }
38 
39 
41  // TODO Auto-generated destructor stub
42 }
43 
44 
46  LOG(LTRACE) << "WindowMatrix2D::Display handler of window " << glutGetWindow();
47  // Enter critical section.
48  APP_DATA_SYNCHRONIZATION_SCOPED_LOCK();
49 
50  // Clear buffer.
51  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
52  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
53 
54  // Draw matrix 2d.
55  if (displayed_matrix_ptr != nullptr){
56  // Set temporal variables.
57  size_t rows = displayed_matrix_ptr->rows();
58  size_t cols = displayed_matrix_ptr->cols();
59  float* data_ptr = displayed_matrix_ptr->data();
60 
61  // Compute scale.
62  float scale_x = (float)glutGet(GLUT_WINDOW_HEIGHT)/(float)(rows);
63  float scale_y = (float)glutGet(GLUT_WINDOW_WIDTH)/(float)(cols);
64 
65  // Iterate through matrix elements.
66  for (size_t y = 0; y < rows; y++) {
67  for (size_t x = 0; x < cols; x++) {
68  // Get value - REVERSED! as Eigen::Matrix by default is column-major!!
69  float val = data_ptr[x*rows + y];
70 
71  // Draw rectangle.
72  draw_filled_rectangle(float(x) * scale_y, float(y) * scale_x, scale_x, scale_y,
73  (float)val,
74  (float)val,
75  (float)val,
76  (float)1.0f);
77 
78  }//: for
79  }//: for
80 
81  draw_grid(0.5f, 0.3f, 0.3f, 0.3f, cols, rows);
82  }//: if !null
83 
84  // Swap buffers.
85  glutSwapBuffers();
86 
87  // End of critical section.
88 }
89 
90 
91 
92 void WindowMatrix2D::setMatrixSynchronized(mic::types::MatrixXf & displayed_matrix_) {
93  // Enter critical section.
94  APP_DATA_SYNCHRONIZATION_SCOPED_LOCK();
95 
96  // Initialize the pointer.
97  if (displayed_matrix_ptr == nullptr) // int Rows_, int Cols_
98  displayed_matrix_ptr = MAKE_MATRIX_PTR(float, displayed_matrix_);
99  else
100  *displayed_matrix_ptr = displayed_matrix_;
101  // End of critical section.
102 }
103 
104 void WindowMatrix2D::setMatrixUnsynchronized(mic::types::MatrixXf & displayed_matrix_) {
105  // Initialize the pointer.
106  if (displayed_matrix_ptr == nullptr) // int Rows_, int Cols_
107  displayed_matrix_ptr = MAKE_MATRIX_PTR(float, displayed_matrix_);
108  else
109  *displayed_matrix_ptr = displayed_matrix_;
110 }
111 
112 
113 
114 void WindowMatrix2D::setMatrixPointerSynchronized(mic::types::MatrixXfPtr displayed_matrix_ptr_) {
115  // Enter critical section.
116  APP_DATA_SYNCHRONIZATION_SCOPED_LOCK();
117 
118  displayed_matrix_ptr = displayed_matrix_ptr_;
119  // End of critical section.
120 }
121 
122 void WindowMatrix2D::setMatrixPointerUnsynchronized(mic::types::MatrixXfPtr displayed_matrix_ptr_) {
123  displayed_matrix_ptr = displayed_matrix_ptr_;
124 }
125 
126 
127 } /* namespace visualization */
128 } /* namespace opengl */
129 } /* namespace mic */
void draw_filled_rectangle(float x, float y, float h, float w, float r, float g, float b, float a)
void setMatrixPointerUnsynchronized(mic::types::MatrixXfPtr displayed_matrix_ptr_)
WindowMatrix2D(std::string name_="Matrix2D", unsigned int position_x_=0, unsigned int position_y_=0, unsigned int width_=512, unsigned int height_=512)
Window displaying 2d matrix (e.g. a grayscale image).
void setMatrixUnsynchronized(mic::types::MatrixXf &displayed_matrix_)
Parent class of all OpenGL-based windows (abstract).
Definition: Window.hpp:51
void setMatrixSynchronized(mic::types::MatrixXf &displayed_matrix_)
void setMatrixPointerSynchronized(mic::types::MatrixXfPtr displayed_matrix_ptr_)
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.