MachineIntelligenceCore:Visualization
DrawingUtils.cpp
Go to the documentation of this file.
1 
23 #include <stdio.h>
24 #include <cmath>
25 #include <cstring>
26 
28 
29 namespace mic {
30  namespace opengl {
31  namespace visualization {
32 
33  /*void DrawingUtils::display_image_roi(image* img, unsigned w, unsigned h, int x1, int y1, int x2, int y2, float r, float g, float b, float a) {
34 
35  if (img != NULL) {
36 
37  float scale_x = (float) w / (float) (img->width);
38  float scale_y = (float) h / (float) (img->height);
39 
40  draw_rectangle((float) x1 * scale_x, (float) y1 * scale_y, (float) (x2 - x1) * scale_y, (float) (y2 - y1) * scale_x, r, g, b, a);
41 
42  }
43 
44  }
45 
46  void DrawingUtils::display_image(image* img, unsigned w, unsigned h, bool show_grid) {
47 
48  if (img != NULL && img->image_data != NULL) {
49 
50  float scale_x = (float) w / (float) (img->width);
51  float scale_y = (float) h / (float) (img->height);
52 
53  for (unsigned x = 0; x < img->width; x++) {
54 
55  for (unsigned y = 0; y < img->height; y++) {
56 
57  if (img->type == BINARY) {
58 
59  uint8_t color = get_color(img, GRAY, y, x);
60 
61  draw_filled_rectangle((float) x * scale_x, float(y) * scale_y, scale_y, scale_x,
62  (float) color / 255.0f,
63  (float) color / 255.0f,
64  (float) color / 255.0f,
65  1.0f);
66 
67  }
68  else if (img->type == GRAYSCALE) {
69 
70  float color = get_color_float(img, GRAY, y, x);
71 
72  draw_filled_rectangle((float) x * scale_x, float(y) * scale_y, scale_y, scale_x,
73  (float) color,
74  (float) color,
75  (float) color,
76  1.0f);
77 
78  }
79  else if (img->type == RGBA) {
80 
81  uint8_t color_r = get_color(img, RED, y, x);
82  uint8_t color_g = get_color(img, GREEN, y, x);
83  uint8_t color_b = get_color(img, BLUE, y, x);
84  uint8_t color_a = get_color(img, ALPHA, y, x);
85 
86  draw_filled_rectangle((float) x * scale_x, float(y) * scale_y, scale_y, scale_x,
87  (float) color_r / 255.0f,
88  (float) color_g / 255.0f,
89  (float) color_b / 255.0f,
90  (float) color_a / 255.0f);
91 
92  }
93 
94  }
95  }
96 
97  if (show_grid) {
98 
99  glColor4f(0.8f, 0.0f, 0.0f, 0.5f);
100  glBegin(GL_LINES);
101 
102  for (unsigned i = 0; i < w; i++) {
103 
104  glVertex2i((int) ((float) i * scale_x), 0);
105  glVertex2i((int) ((float) i * scale_x), (GLint) h);
106 
107  }
108 
109  for (unsigned i = 0; i < h; i++) {
110 
111  glVertex2i(0, (int) ((float) i * scale_y));
112  glVertex2i((GLint) w, (int) ((float) i * scale_y));
113 
114  }
115 
116  glEnd();
117 
118  }
119 
120  }
121  else {
122 
123  //CHECK(img != NULL && img->image_data != NULL);
124  }
125 
126  }*/
127 
128  void DrawingUtils::draw_frame(float x1, float y1, float x2, float y2, float r, float g, float b, float a) {
129 
130  glColor4f(r, g, b, a);
131 
132  glBegin(GL_LINE_STRIP);
133 
134  //margins, top frame
135  glVertex2i((int) x1, (int) y1);
136  glVertex2i((int) x1, (int) y2);
137  glVertex2i((int) x2, (int) y2);
138  glVertex2i((int) x2, (int) y1);
139  glVertex2i((int) x1, (int) y1);
140 
141  glEnd();
142 
143  }
144 
145  void DrawingUtils::draw_grid(float r, float g, float b, float a, float cells_h, float cells_v, float line_width_) {
146 
147  GLint h = glutGet(GLUT_WINDOW_HEIGHT);
148  GLint w = glutGet(GLUT_WINDOW_WIDTH);
149 
150  float scale_x = (float) w / (float) (cells_h);
151  float scale_y = (float) h / (float) (cells_v);
152 
153  glColor4f(r, g, b, a);
154  glLineWidth(line_width_);
155  glBegin(GL_LINES);
156 
157  for (unsigned i = 1; i < cells_h; i++) {
158 
159  glVertex2i((int) ((float) i * scale_x), 0);
160  glVertex2i((int) ((float) i * scale_x), (GLint) h);
161 
162  }
163 
164  for (unsigned i = 1; i < cells_v; i++) {
165 
166  glVertex2i(0, (int) ((float) i * scale_y));
167  glVertex2i((GLint) w, (int) ((float) i * scale_y));
168 
169  }
170 
171  glEnd();
172  }
173 
174  void DrawingUtils::draw_mark(mark m, float x, float y, float size, float line_width, float r, float g, float b, float a) {
175  switch (m) {
176  case CIRCLE:
177  case ONE_CIRCLE:
178  draw_circle(x, y, size, line_width, r, g, b, a);
179  break;
180 
181  case PLUS:
182  draw_plus(x, y, size, line_width, r, g, b, a);
183  break;
184 
185  case CROSS:
186  draw_cross(x, y, size, line_width, r, g, b, a);
187  break;
188 
189  case SQUARE:
190  draw_square(x, y, size, line_width, r, g, b, a);
191  break;
192  case NO_MARK:
193  case NO_LINE:
194  break;
195  }
196  }
197 
198  void DrawingUtils::draw_circle(float x, float y, float radius, float line_width, float r, float g, float b, float a) {
199  glColor4f(r, g, b, a);
200  glLineWidth(line_width);
201  glBegin(GL_LINE_STRIP);
202  for (float angle = 0.0f; angle <= 360.0f; angle += 30.0f) {
203  glVertex2f((x + sinf((angle * (float) M_PI) / 180.0f) * radius), (y + cosf((angle * (float) M_PI) / 180.0f) * radius));
204  }
205  glEnd();
206  }
207 
208  void DrawingUtils::draw_plus(float x, float y, float radius, float line_width, float r, float g, float b, float a) {
209  glColor4f(r, g, b, a);
210  glLineWidth(line_width);
211  glBegin(GL_LINES);
212  glVertex2i((int) x, (int) (y - radius));
213  glVertex2i((int) x, (int) (y + radius));
214 
215  glVertex2i((int) (x - radius), (int) y);
216  glVertex2i((int) (x + radius), (int) y);
217  glEnd();
218  glLineWidth(1.0f);
219  }
220 
221  void DrawingUtils::draw_cross(float x, float y, float radius, float line_width, float r, float g, float b, float a) {
222 
223  glColor4f(r, g, b, a);
224 
225  glLineWidth(line_width);
226 
227  glBegin(GL_LINES);
228 
229  glVertex2i((int) (x - radius), (int) (y - radius));
230  glVertex2i((int) (x + radius), (int) (y + radius));
231 
232  glVertex2i((int) (x + radius), (int) (y - radius));
233  glVertex2i((int) (x - radius), (int) (y + radius));
234 
235  glEnd();
236 
237  }
238 
239  void DrawingUtils::draw_square(float x, float y, float radius, float line_width, float r, float g, float b, float a) {
240 
241  glColor4f(r, g, b, a);
242 
243  glLineWidth(line_width);
244 
245  glBegin(GL_LINE_STRIP);
246 
247  glVertex2i((int) (x - radius), (int) (y - radius));
248  glVertex2i((int) (x + radius), (int) (y - radius));
249  glVertex2i((int) (x + radius), (int) (y + radius));
250  glVertex2i((int) (x - radius), (int) (y + radius));
251  glVertex2i((int) (x - radius), (int) (y - radius));
252 
253  glEnd();
254 
255  }
256 
257  void DrawingUtils::draw_square_in_place(float radius, float line_width, float r, float g, float b, float a) {
258 
259  glColor4f(r, g, b, a);
260 
261  glLineWidth(line_width);
262 
263  glBegin(GL_LINE_STRIP);
264 
265  glNormal3f(0.0f, 0.0f, 1.0f);
266  glVertex3f(-radius, -radius, 0.0f);
267  glVertex3f(radius, -radius, 0.0f);
268  glVertex3f(radius, radius, 0.0f);
269  glVertex3f(-radius, radius, 0.0f);
270  glVertex3f(-radius, -radius, 0.0f);
271 
272  glEnd();
273 
274  }
275 
276  void DrawingUtils::draw_filled_square_in_place(float radius, float line_width, float r, float g, float b, float a) {
277 
278  glColor4f(r, g, b, a);
279 
280  glLineWidth(line_width);
281 
282  glBegin(GL_QUADS);
283 
284  glNormal3f(0.0f, 0.0f, 1.0f);
285  glVertex3f(-radius, -radius, 0.0f);
286  glVertex3f(radius, -radius, 0.0f);
287  glVertex3f(radius, radius, 0.0f);
288  glVertex3f(-radius, radius, 0.0f);
289 
290  glEnd();
291 
292  }
293 
294  void DrawingUtils::draw_filled_rectangle_in_place(float radius_x, float radius_y, float line_width, float r, float g, float b, float a) {
295 
296  glColor4f(r, g, b, a);
297 
298  glLineWidth(line_width);
299 
300  glBegin(GL_QUADS);
301 
302  glNormal3f(0.0f, 0.0f, 1.0f);
303  glVertex3f(-radius_x, -radius_y, 0.0f);
304  glVertex3f(radius_x, -radius_y, 0.0f);
305  glVertex3f(radius_x, radius_y, 0.0f);
306  glVertex3f(-radius_x, radius_y, 0.0f);
307 
308  glEnd();
309 
310  }
311 
312  void DrawingUtils::draw_cuboid(float radius, float line_width, float height, float r, float g, float b, float a) {
313 
314  glColor4f(r, g, b, a);
315 
316  glLineWidth(line_width);
317 
318  glBegin(GL_POLYGON);
319  glNormal3f(0, 0, 1);
320  glVertex3f(-radius, -radius, height);
321  glVertex3f(radius, -radius, height);
322  glVertex3f(radius, radius, height);
323  glVertex3f(-radius, radius, height);
324  glEnd();
325 
326  glBegin(GL_POLYGON);
327  glNormal3f(-1.0f, 0.0f, 0.0f);
328  glVertex3f(-radius, -radius, 0.0f);
329  glVertex3f(-radius, -radius, height);
330  glVertex3f(-radius, radius, height);
331  glVertex3f(-radius, radius, 0.0f);
332  glEnd();
333 
334  glBegin(GL_POLYGON);
335  glNormal3f(1.0f, 0.0f, 0.0f);
336  glVertex3f(radius, -radius, 0.0f);
337  glVertex3f(radius, -radius, height);
338  glVertex3f(radius, radius, height);
339  glVertex3f(radius, radius, 0.0f);
340  glEnd();
341 
342  glBegin(GL_POLYGON);
343  glNormal3f(0.0f, 1.0f, 0.0f);
344  glVertex3f(-radius, radius, 0.0f);
345  glVertex3f(-radius, radius, height);
346  glVertex3f(radius, radius, height);
347  glVertex3f(radius, radius, 0.0f);
348  glEnd();
349 
350  glBegin(GL_POLYGON);
351  glNormal3f(0.0f, -1.0f, 0.0f);
352  glVertex3f(-radius, -radius, 0.0f);
353  glVertex3f(-radius, -radius, height);
354  glVertex3f(radius, -radius, height);
355  glVertex3f(radius, -radius, 0.0f);
356  glEnd();
357 
358 
359  }
360 
362 
363  glBegin(GL_QUADS);
364 
365  glNormal3f(0.0f, 0.0f, 1.0f);
366  glTexCoord2f(0.0f, 0.0f);
367  glVertex3f(-radius, -radius, 0.0f);
368  glTexCoord2f(1.0f, 0.0f);
369  glVertex3f(radius, -radius, 0.0f);
370  glTexCoord2f(1.0f, 1.0f);
371  glVertex3f(radius, radius, 0.0f);
372  glTexCoord2f(0.0f, 1.0f);
373  glVertex3f(-radius, radius, 0.0f);
374 
375  glEnd();
376 
377  }
378 
379  void DrawingUtils::draw_filled_square(float x, float y, float radius, float r, float g, float b, float a) {
380 
381  draw_filled_rectangle(x, y, radius / 2.0f, radius / 2.0f, r, g, b, a);
382 
383  }
384 
385  void DrawingUtils::draw_filled_rectangle(float x, float y, float h, float w, float r, float g, float b, float a) {
386 
387  glColor4f(r, g, b, a);
388 
389  glBegin(GL_QUADS);
390 
391  //margins, top frame
392  glVertex2i((int) (x), (int) (y));
393  glVertex2i((int) (x + w), (int) (y));
394  glVertex2i((int) (x + w), (int) (y + h));
395  glVertex2i((int) (x), (int) (y + h));
396 
397  glEnd();
398 
399  }
400 
401  void DrawingUtils::draw_rectangle(float x, float y, float h, float w, float r, float g, float b, float a) {
402 
403  glColor4f(r, g, b, a);
404 
405  glBegin(GL_LINE_STRIP);
406 
407  //margins, top frame
408  glVertex2i((int) (x), (int) (y));
409  glVertex2i((int) (x + w), (int) (y));
410  glVertex2i((int) (x + w), (int) (y + h));
411  glVertex2i((int) (x), (int) (y + h));
412  glVertex2i((int) (x), (int) (y));
413 
414  glEnd();
415 
416  }
417 
418  void DrawingUtils::draw_text(float x, float y, char* string, float r, float g, float b, float a, void* font) {
419 
420  size_t len, i;
421 
422  glColor4f(r, g, b, a);
423 
424  glRasterPos2i((int) x, (int) y);
425 
426  len = (size_t) strlen(string);
427 
428  for (i = 0; i < len; i++)
429  glutBitmapCharacter(font, string[i]);
430 
431  }
432 
433  void DrawingUtils::draw_text_3i(float x, float y, float z, char* string, float r, float g, float b, float a, void* font) {
434 
435  int len, i;
436 
437  glColor4f(r, g, b, a);
438  glRasterPos3i((int) x, (int) y, (int) z);
439 
440  len = (int) strlen(string);
441 
442  for (i = 0; i < len; i++)
443  glutBitmapCharacter(font, string[i]);
444 
445  }
446 
447  void DrawingUtils::draw_cursor(float x, float y, float r, float g, float b, float a) {
448 
449  char str[32];
450 
451  sprintf(str, "[%.3f, %.3f]", x, y);
452 
453  draw_text(x, y, str, r, g, b, a, GLUT_BITMAP_HELVETICA_10);
454 
455  }
456 
457  float DrawingUtils::to_radians(float degrees) {
458  return degrees * (float) (M_PI / 180.0f);
459  }
460 
461  /*v_3f DrawingUtils::get_3d_position(int x, int y, float plane) {
462 
463  GLint viewport[4];
464  GLdouble modelview[16];
465  GLdouble projection[16];
466  GLfloat winX, winY, winZ;
467  GLdouble posX, posY, posZ;
468  v_3f point;
469 
470  glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
471  glGetDoublev(GL_PROJECTION_MATRIX, projection);
472  glGetIntegerv(GL_VIEWPORT, viewport);
473 
474  winX = (float) x;
475  winY = (float) viewport[3] - (float) y;
476  glReadPixels(x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
477 
478  gluUnProject(winX, winY, plane, modelview, projection, viewport, &posX, &posY, &posZ);
479 
480  point.x = (float) posX;
481  point.y = (float) posY;
482  point.z = (float) posZ;
483 
484  return point;
485  }*/
486 
487  } /* namespace visualization */
488  } /* namespace opengl */
489 } /* namespace mic */
490 
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)
void draw_text(float x, float y, char *string, float r, float g, float b, float a, void *font)
void draw_filled_square(float x, float y, float size, float r, float g, float b, float a)
Declaration of a class encapsulating methods for drawing in OpenGL windows.
void draw_square_in_place(float size, float line_width, float r, float g, float b, float a)
void draw_text_3i(float x, float y, float z, char *string, float r, float g, float b, float a, void *font)
void draw_square(float x, float y, float size, float line_width, float r, float g, float b, float a)
void draw_plus(float x, float y, float size, float line_width, float r, float g, float b, float a)
void draw_mark(mark m, float x, float y, float size, float line_width, float r, float g, float b, float a)
void draw_filled_square_in_place(float radius, float line_width, float r, float g, float b, float a)
mark
Different types of marks drawn on chars.
void draw_cuboid(float radius, float line_width, float height, float r, float g, float b, float a)
void draw_cross(float x, float y, float size, float line_width, float r, float g, float b, float a)
void draw_cursor(float x, float y, float r, float g, float b, float a)
void draw_filled_rectangle_in_place(float radius_x, float radius_y, float line_width, float r, float g, float b, float a)
void draw_rectangle(float x, float y, float h, float w, float r, float g, float b, float a)
void draw_frame(float x1, float y1, float x2, float y2, float r, float g, float b, float a)
void draw_grid(float r, float g, float b, float a, float cells_h, float cells_v, float line_width_=1.0)