MachineIntelligenceCore:Visualization
WindowProbability.cpp
Go to the documentation of this file.
1 
27 
28 namespace mic {
29 namespace opengl {
30 namespace visualization {
31 
32 
34  unsigned int position_x_, unsigned int position_y_,
35  unsigned int width_ , unsigned int height_) :
36  Window(name_, position_x_, position_y_, width_, height_)
37 {
38  // NULL pointer.
39  displayed_matrix1 = nullptr;
40  displayed_matrix2 = nullptr;
41 }
42 
43 
45  // TODO Auto-generated destructor stub
46 }
47 
48 
50  LOG(LTRACE) << "WindowProbability::Display handler of window " << glutGetWindow();
51  // Enter critical section.
52  APP_DATA_SYNCHRONIZATION_SCOPED_LOCK();
53 
54  // Clear buffer.
55  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
56  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
57 
58  // Draw chart boundary.
59  glLineWidth(1.0f);
60  draw_rectangle(1.0f, 1.0f, (float)glutGet(GLUT_WINDOW_HEIGHT)*0.9, (float)glutGet(GLUT_WINDOW_WIDTH)-2.0f, 0.7f, 0.7f, 0.7f, 1.0f);
61 
62  if (displayed_matrix1 != nullptr){
63  // Assume vector (1d matrix).
64  assert(displayed_matrix1->cols() == 1);
65 
66 /* std::cout << "Drawing matrix: r =" << displayed_matrix1->rows() << " c=" << displayed_matrix1->cols() << std::endl;
67  std::cout << displayed_matrix1->transpose() << std::endl;*/
68  size_t elements = displayed_matrix1->rows();
69  float* data_ptr = displayed_matrix1->data();
70 
71  // Compute scale.
72  float scale_x = (float)glutGet(GLUT_WINDOW_WIDTH)/(float)(elements);
73  float scale_y = (float)glutGet(GLUT_WINDOW_HEIGHT) * 0.9 - 1.0f;
74 
75  // Iterate through elements of the vector.
76  for (size_t x = 0; x < elements; x++) {
77  // Get value.
78  float val = data_ptr[x];
79 
80  glColor4f(1.0, 0.5, 0.5, 1.0);
81  glLineWidth(5.0);
82  glBegin(GL_LINES);
83  glVertex2i(((float) (x+0.4) * scale_x), scale_y);
84  glVertex2i(((float) (x+0.4) * scale_x), (float) (1.0-val) * scale_y);
85  glEnd();
86 
87  }//: for
88 
89  // Print labels.
90  scale_y = (float)glutGet(GLUT_WINDOW_HEIGHT) * 0.97;
91  for (size_t x = 0; x < elements; x++) {
92  char* str_value = (char*)std::to_string(x).c_str();
93  draw_text((float) (x+0.45) * scale_x, scale_y, str_value, 1.0f, 1.0f, 1.0f, 1.0f, GLUT_BITMAP_HELVETICA_10);
94  }//: for
95 
96  }//: if !null
97 
98  if (displayed_matrix2 != nullptr){
99  // Assume vector (1d matrix).
100  assert(displayed_matrix2->cols() == 1);
101 
102  size_t elements = displayed_matrix2->rows();
103  float* data_ptr = displayed_matrix2->data();
104 
105  // Compute scale.
106  float scale_x = (float)glutGet(GLUT_WINDOW_WIDTH)/(float)(elements);
107  float scale_y = (float)glutGet(GLUT_WINDOW_HEIGHT) * 0.9 - 1.0f;
108 
109  // Iterate through elements of the vector.
110  for (size_t x = 0; x < elements; x++) {
111  // Get value.
112  float val = data_ptr[x];
113 
114  glColor4f(0.5, 1.0, 0.5, 1.0);
115  glLineWidth(5.0);
116  glBegin(GL_LINES);
117  glVertex2i(((float) (x+0.6) * scale_x), scale_y);
118  glVertex2i(((float) (x+0.6) * scale_x), (float) (1.0-val) * scale_y);
119  glEnd();
120 
121  }//: for
122 
123  // Print labels.
124  scale_y = (float)glutGet(GLUT_WINDOW_HEIGHT) * 0.97;
125  for (size_t x = 0; x < elements; x++) {
126  char* str_value = (char*)std::to_string(x).c_str();
127  draw_text((float) (x+0.45) * scale_x, scale_y, str_value, 1.0f, 1.0f, 1.0f, 1.0f, GLUT_BITMAP_HELVETICA_10);
128  }//: for
129 
130  }//: if !null
131 
132  // Swap buffers.
133  glutSwapBuffers();
134 
135  // End of critical section.
136 }
137 
138 void WindowProbability::setMatrixPointer1(mic::types::MatrixXfPtr displayed_matrix_) {
139  // Enter critical section.
140  APP_DATA_SYNCHRONIZATION_SCOPED_LOCK();
141 
142  displayed_matrix1 = displayed_matrix_;
143  // End of critical section.
144 }
145 
146 void WindowProbability::setMatrixPointer2(mic::types::MatrixXfPtr displayed_matrix_) {
147  // Enter critical section.
148  APP_DATA_SYNCHRONIZATION_SCOPED_LOCK();
149 
150  displayed_matrix2 = displayed_matrix_;
151  // End of critical section.
152 }
153 
154 } /* namespace visualization */
155 } /* namespace opengl */
156 } /* namespace mic */
WindowProbability(std::string name_="WindowProbability", unsigned int position_x_=0, unsigned int position_y_=0, unsigned int width_=512, unsigned int height_=512)
void draw_text(float x, float y, char *string, float r, float g, float b, float a, void *font)
void setMatrixPointer2(mic::types::MatrixXfPtr displayed_matrix_)
Parent class of all OpenGL-based windows (abstract).
Definition: Window.hpp:51
void setMatrixPointer1(mic::types::MatrixXfPtr displayed_matrix_)
void draw_rectangle(float x, float y, float h, float w, float r, float g, float b, float a)
Declaration of WindowManager class along with a bunch of helpful types and macros.