MachineIntelligenceCore:Visualization
WindowCollectorChart.hpp
Go to the documentation of this file.
1 
23 #ifndef SRC_OPENGL_VISUALIZATION_WINDOWFLOATCOLLECTORCHART_HPP_
24 #define SRC_OPENGL_VISUALIZATION_WINDOWFLOATCOLLECTORCHART_HPP_
25 
27 
28 #include <types/Color.hpp>
29 
30 #include <utils/DataCollector.hpp>
31 
32 namespace mic {
33 namespace opengl {
34 namespace visualization {
35 
42 template <typename eT=float>
44 public:
48  WindowCollectorChart(std::string name_ = "Chart",
49  unsigned int position_x_ = 0, unsigned int position_y_ = 0,
50  unsigned int width_ = 1024, unsigned int height_ = 256) :
51  Window(name_, position_x_, position_y_, width_, height_),
52  collector_ptr(nullptr)
53  {
54  // Set default values of variables.
55  zoom_factor = 1.0f;
56 
57  chart_width = 0.9;
58  chart_height = 0.45;
59  label_offset_x = 50;
60  label_offset_y = 20;
62 
63  // Reset data.
64  //...
65 
66  // Register key handlers.
67  registerKeyhandler('<', "< - zoom in (upscale chart)", &WindowCollectorChart::keyhandlerZoomIn, this);
68  registerKeyhandler('>', "> - zoom out (downscale chart)", &WindowCollectorChart::keyhandlerZoomOut, this);
69  registerKeyhandler('/', "/ - reset zoom (set zoom_factor to default: 1.0)", &WindowCollectorChart::keyhandlerZoomReset, this);
70 
71  // Register window!
72  VGL_MANAGER->registerWindow(this);
73  }
74 
75 
79  virtual ~WindowCollectorChart() { }
80 
84  void displayHandler(void) {
85  LOG(LTRACE) << "WindowFloatCollectorChart::Display handler of window " << glutGetWindow();
86  // Enter critical section.
87  APP_DATA_SYNCHRONIZATION_SCOPED_LOCK();
88 
89  // Clear buffer.
90  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
91  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
92 
93  // Check whether the collector pointer is set.
94  if (collector_ptr != nullptr){
95 
96  // Refresh the chart window.
98 
99  // Get access to data containers.
100  mic::utils::DataContainers<std::string, eT> containers = collector_ptr->getContainers();
101 
102  // Refresh charts one by one.
103  unsigned int label_x_offset = 10;
104  unsigned int label_y_offset = 15;
105  // Iterate through the data containers and display them 1 by 1.
106  for(mic::utils::DataContainerIt<std::string, eT> it = containers.begin(); it != containers.end(); it++, label_y_offset+=15) {
107  // Get vector.
108  std::string l = it->first;
109 
110  // Get data.
111  std::vector<eT> vector = (it->second)->data;
112  // Get min-max values.
113  eT min_value = (it->second)->min_value;
114  eT max_value = (it->second)->max_value;
115 
116  // Get display properties.
117  color_rgba c = (it->second)->color;
118  eT line_width = (it->second)->line_width;
119 
120  // If do not fits in image - Move labels to next "column".
121  //std::cout << glutGet((GLenum) GLUT_WINDOW_HEIGHT) << std::endl;
122  //std::cout << label_y_offset << std::endl;
123  int acc_y = (int)(label_offset_y);
124  int acc_h = (int)(height * chart_height);
125  //std::cout << acc_y + acc_h + label_y_offset << std::endl;
126  if (acc_y + acc_h + label_y_offset > (unsigned int) glutGet((GLenum) GLUT_WINDOW_HEIGHT)) {
127  label_y_offset = 15;
128  label_x_offset += 200;
129  }//: if
130 
131  // Draw chart associated with given data container.
132  redrawSingleContainer(l, vector, min_value, max_value, c, line_width, label_x_offset, label_y_offset);
133  }//: end
134  }//: if !null
135 
136  // Swap buffers.
137  glutSwapBuffers();
138 
139  // End of critical section.
140  }
141 
146  void setDataCollectorPtr(mic::utils::DataCollectorPtr<std::string, eT> collector_ptr_) {
147  // Enter critical section.
148  APP_DATA_SYNCHRONIZATION_SCOPED_LOCK();
149 
150  collector_ptr = collector_ptr_;
151  // End of critical section.
152  }
153 
158  LOG(LTRACE)<< "WindowFloatCollectorChart::refreshChart";
159 
160  int acc_x = (int)(width * ((1.0 - chart_width)/2.0));
161  int acc_y = (int)(label_offset_y);
162  int acc_w = (int)(width * chart_width);
163  int acc_h = (int)(height * chart_height);
164 
165  // Draw chart boundary.
166  glLineWidth(1.0f);
167  draw_rectangle(acc_x, acc_y, acc_h, acc_w, 0.7f, 0.7f, 0.7f, 1.0f);
168  draw_rectangle(acc_x, acc_y + acc_h, acc_h, acc_w, 0.7f, 0.7f, 0.7f, 1.0f);
169 
170  // Draw horizontal bars and print labels on both sides..
171  for (int i = 0; i <= number_of_horizontal_bars; i++) {
172  float h = (float)i/(float)number_of_horizontal_bars;
173  // Print labels.
174  char value[10];
175  sprintf(value, "%d%%", (unsigned)(float)round((100.0 * h)));
176  draw_text(acc_x - 30, acc_y + acc_h - (int)(h * acc_h) + 2, value, 1.0f, 1.0f, 1.0f, 0.5f, GLUT_BITMAP_HELVETICA_10);
177  draw_text(acc_x + acc_w + 5, acc_y + acc_h - (int)(h * acc_h) + 2, value, 1.0f, 1.0f, 1.0f, 0.5f, GLUT_BITMAP_HELVETICA_10);
178  // Draw horizontal lines.
179  glColor4f(0.5f, 0.5f, 0.5f, 0.5f);
180  glBegin(GL_LINES);
181  glVertex2i(acc_x, acc_y + acc_h - (int)(h * acc_h));
182  glVertex2i(acc_x + acc_w, acc_y + acc_h - (int)(h * acc_h));
183  glEnd();
184  }//: for
185  }
186 
198  void redrawSingleContainer(std:: string & label_, std::vector<eT> & data_, eT min_value_, eT max_value_, mic::types::color_rgba color_, eT line_width_, unsigned short label_x_offset_, unsigned short label_y_offset_) {
199  LOG(LTRACE)<< "WindowFloatCollectorChart::refreshSingleChart";
200 
201  int acc_x = (int)(width * ((1.0 - chart_width)/2.0));
202  int acc_y = (int)(label_offset_y);
203  int acc_w = (int)(width * chart_width);
204  int acc_h = (int)(height * chart_height);
205 
206  // Set line width.
207  glLineWidth(line_width_);
208  glBegin(GL_LINE_STRIP);
209  eT value;
210  // Special case: division by 0. Place values in the "middle" (0.5).
211  eT diff = max_value_ - min_value_;
212  if (diff == 0.0){
213  diff = 0.5;
214  min_value_ = 0.0;
215  }
216  // Draw data.
217  for (int i = 0; i < chart_width * (width); i++) {
218 
219  if (data_.size() - 1 - (size_t)round(i * zoom_factor) < data_.size()) {
220  value = data_[data_.size() - 1 - (size_t)(i * zoom_factor)];
221  // Scale the value.
222  value = (value - min_value_)/(diff);
223  glColor4f(color_.r/255.0f, color_.g/255.0f, color_.b/255.0f, color_.a/255.0f);
224  glVertex2i(acc_x + acc_w - (int)(i), acc_y + acc_h - (int)(value * acc_h));
225  }
226 
227  }
228  glEnd();
229 
230  // Print label.
231  char str_value[100] = "-";
232  if (data_.size() > 0) {
233  value = data_[data_.size() - 1];
234  sprintf(str_value, "%s: %.2f (%.2f : %.2f)", label_.c_str(), value, min_value_, max_value_);
235  } else {
236  // If there are no data.
237  sprintf(str_value, "%s: -", label_.c_str());
238  }
239  draw_text(acc_x + label_x_offset_, acc_y + acc_h + label_y_offset_, str_value, color_.r/255.0f, color_.g/255.0f, color_.b/255.0f, color_.a/255.0f, GLUT_BITMAP_HELVETICA_10);
240  }
241 
242 private:
243 
245  mic::utils::DataCollectorPtr<std::string, eT> collector_ptr;
246 
248  float zoom_factor;
249 
251  double chart_width;
252 
254  double chart_height;
255 
258 
261 
264 
268  void keyhandlerZoomIn(void) {
269  zoom_factor = zoom_factor / 1.1f;
270  }
271 
275  void keyhandlerZoomOut(void) {
276  zoom_factor = zoom_factor * 1.1f;
277  }
278 
282  void keyhandlerZoomReset(void) {
283  zoom_factor = 1.0f;
284  }
285 
286 };
287 
288 // Typedef for compatibility.
290 
291 
292 } /* namespace visualization */
293 } /* namespace opengl */
294 } /* namespace mic */
295 
296 #endif /* SRC_OPENGL_VISUALIZATION_WINDOWFLOATCOLLECTORCHART_HPP_ */
WindowCollectorChart(std::string name_="Chart", unsigned int position_x_=0, unsigned int position_y_=0, unsigned int width_=1024, unsigned int height_=256)
float zoom_factor
Zoom factor - used for zoomin in and out in the chart window.
void setDataCollectorPtr(mic::utils::DataCollectorPtr< std::string, eT > collector_ptr_)
void draw_text(float x, float y, char *string, float r, float g, float b, float a, void *font)
void redrawSingleContainer(std::string &label_, std::vector< eT > &data_, eT min_value_, eT max_value_, mic::types::color_rgba color_, eT line_width_, unsigned short label_x_offset_, unsigned short label_y_offset_)
OpenGL-based window responsible for displaying data collected in DataCollector objects in the form of...
#define VGL_MANAGER
Macro returning OpenGL window manager instance.
Contains declaration of parent class of all OpenGL-based windows.
mic::utils::DataCollectorPtr< std::string, eT > collector_ptr
Data collector associated with .
WindowCollectorChart< float > WindowCollectorFloatChart
Parent class of all OpenGL-based windows (abstract).
Definition: Window.hpp:51
void draw_rectangle(float x, float y, float h, float w, float r, float g, float b, float a)
double number_of_horizontal_bars
Number of horizontal bars in chart.