MRPT  1.9.9
CColorBar.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "opengl-precomp.h" // Precompiled header
11 
12 #include <mrpt/opengl/CColorBar.h>
13 #include <mrpt/opengl/gl_utils.h>
15 
16 #include "opengl_internals.h"
17 
18 using namespace mrpt;
19 using namespace mrpt::opengl;
20 
21 using namespace mrpt::math;
22 using namespace std;
23 
25 
27  /** The colormap to represent. */
28  const mrpt::img::TColormap colormap,
29  /** size of the color bar */
30  double width, double height,
31  /** limits for [0,1] colormap indices */
32  double min_col, double max_col,
33  /** limits for values associated to extreme colors */
34  double min_value, double max_value,
35  /** sprintf-like format string for values */
36  const std::string& label_format,
37  /** Label text font size */
38  double label_font_size)
39  : m_colormap(colormap),
40  m_width(width),
41  m_height(height),
42  m_label_format(label_format),
43  m_min_col(min_col),
44  m_max_col(max_col),
45  m_min_value(min_value),
46  m_max_value(max_value),
47  m_label_font_size(label_font_size)
48 
49 {
50 }
51 
53 {
54  m_colormap = colormap;
56 }
57 
59  double col_min, double col_max, double value_min, double value_max)
60 {
61  m_min_col = col_min;
62  m_max_col = col_max;
63  m_min_value = value_min;
64  m_max_value = value_max;
66 }
67 
69 {
70  m_disable_depth_test = enable;
72 }
73 
74 /*---------------------------------------------------------------
75  render
76  ---------------------------------------------------------------*/
78 {
79 #if MRPT_HAS_OPENGL_GLUT
80  if (m_disable_depth_test)
81  glDisable(GL_DEPTH_TEST); // colobars are typically displayed on-top of
82  // the rest of objects!
84 
85  // solid:
87 
88  unsigned int num_divisions = 64;
89  unsigned int num_labels = 4;
90  unsigned int one_label_each_nth = floor((num_divisions) / num_labels);
91 
92  const double x0 = .0, x1 = m_width, x2 = m_width * 1.3;
93  const double Ay = m_height / (num_divisions - 1);
94 
95  std::vector<mrpt::img::TColorf> cols(num_divisions);
96  for (unsigned int i = 0; i < num_divisions; i++)
97  {
98  const double col_idx =
99  m_min_col + i * (m_max_col - m_min_col) / (num_divisions - 1);
101  m_colormap, col_idx, cols[i].R, cols[i].G, cols[i].B);
102  }
103 
104  for (unsigned int i = 0; i < num_divisions - 1; i++)
105  {
106  const double y0 = Ay * i, y1 = Ay * (i + 1);
107  const TPoint3D pt00(x0, y0, 0), pt10(x1, y0, 0);
108  const TPoint3D pt01(x0, y1, 0), pt11(x1, y1, 0);
109 
110  // Color quad:
111 
113  glColor3f(cols[i].R, cols[i].G, cols[i].B);
114  glVertex3f(pt00.x, pt00.y, pt00.z);
115  glVertex3f(pt10.x, pt10.y, pt10.z);
116  glColor3f(cols[i + 1].R, cols[i + 1].G, cols[i + 1].B);
117  glVertex3f(pt11.x, pt11.y, pt11.z);
118  //
119  glColor3f(cols[i].R, cols[i].G, cols[i].B);
120  glVertex3f(pt00.x, pt00.y, pt00.z);
121  glColor3f(cols[i + 1].R, cols[i + 1].G, cols[i + 1].B);
122  glVertex3f(pt11.x, pt11.y, pt11.z);
123  glVertex3f(pt01.x, pt01.y, pt01.z);
124  glEnd();
125  }
126 
128 
129  for (unsigned int i = 0; i < num_divisions; i++)
130  {
131  const double val =
132  m_min_value + i * (m_max_value - m_min_value) / (num_divisions - 1);
133  const double y0 = Ay * i; //, y1 = Ay*(i + 1);
134 
135  // Text label:
136  bool draw_label =
137  (i % one_label_each_nth) == 0 || i == (num_divisions - 1);
138 
139  if (draw_label)
140  {
141  // Line:
142  glLineWidth(1.0);
143  glBegin(GL_LINES);
144  glColor3b(0, 0, 0);
145  glVertex2d(x0, y0);
146  glVertex2d(x2, y0);
147  glEnd();
148 
149  // Text:
150  glPushMatrix();
151 
152  glTranslatef(x2, y0, 0.0);
153  glColor3ub(0xff, 0xff, 0xff);
155  mrpt::format(m_label_format.c_str(), val), m_label_font_size);
156 
157  glPopMatrix();
158  }
159  }
160 
162 
163 #endif
164 }
165 
168 {
169  writeToStreamRender(out);
170  // version 0
171  out << uint32_t(m_colormap) << m_min_col << m_max_col << m_min_value
172  << m_max_value << m_label_format << m_label_font_size
173  << m_disable_depth_test;
174 }
177 {
178  switch (version)
179  {
180  case 0:
181  readFromStreamRender(in);
182 
183  in.ReadAsAndCastTo<uint32_t, mrpt::img::TColormap>(m_colormap);
184  in >> m_min_col >> m_max_col >> m_min_value >> m_max_value >>
185  m_label_format >> m_label_font_size >> m_disable_depth_test;
186  break;
187  default:
189  };
191 }
192 
195 {
196  bb_min.x = 0;
197  bb_min.y = 0;
198  bb_min.z = 0;
199 
200  bb_max.x = m_width;
201  bb_max.y = m_height;
202  bb_max.z = 0;
203 
204  // Convert to coordinates of my parent:
205  m_pose.composePoint(bb_min, bb_min);
206  m_pose.composePoint(bb_max, bb_max);
207 }
void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
void colormap(const TColormap &color_map, const float color_index, float &r, float &g, float &b)
Transform a float number in the range [0,1] into RGB components.
Definition: color_maps.cpp:114
TColormap
Different colormaps for use in mrpt::img::colormap()
Definition: color_maps.h:29
double x
X,Y,Z coordinates.
Definition: TPoint3D.h:83
GLAPI void GLAPIENTRY glColor3b(GLbyte red, GLbyte green, GLbyte blue)
GLAPI void GLAPIENTRY glEnable(GLenum cap)
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
const double G
GLAPI void GLAPIENTRY glPopMatrix(void)
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
Definition: CColorBar.cpp:175
#define GL_TRIANGLES
Definition: glew.h:277
STL namespace.
mrpt::img::TPixelCoordf glDrawText(const std::string &text, const double textScale, enum TOpenGLFontStyle style=NICE, double spacing=1.5, double kerning=0.1)
renders a string in GL using the current settings.
Definition: gl_utils.cpp:545
#define GL_DEPTH_TEST
Definition: glew.h:402
#define GL_SMOOTH
Definition: glew.h:636
GLAPI void GLAPIENTRY glShadeModel(GLenum mode)
#define GL_LIGHTING
Definition: glew.h:386
GLAPI void GLAPIENTRY glLineWidth(GLfloat width)
void glSetFont(const std::string &fontname)
sets the font to use for future font rendering commands.
Definition: gl_utils.cpp:527
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Definition: CColorBar.cpp:167
GLenum GLsizei width
Definition: glext.h:3535
unsigned char uint8_t
Definition: rptypes.h:44
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
Definition: CColorBar.cpp:166
This base provides a set of functions for maths stuff.
void render_dl() const override
Render.
Definition: CColorBar.cpp:77
GLint GLvoid * img
Definition: glext.h:3769
int val
Definition: mrpt_jpeglib.h:957
GLAPI void GLAPIENTRY glColor3f(GLfloat red, GLfloat green, GLfloat blue)
GLAPI void GLAPIENTRY glColor3ub(GLubyte red, GLubyte green, GLubyte blue)
GLAPI void GLAPIENTRY glBegin(GLenum mode)
GLAPI void GLAPIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z)
GLAPI void GLAPIENTRY glTranslatef(GLfloat x, GLfloat y, GLfloat z)
void setColorAndValueLimits(double col_min, double col_max, double value_min, double value_max)
Definition: CColorBar.cpp:58
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void setColormap(const mrpt::img::TColormap colormap)
Definition: CColorBar.cpp:52
GLAPI void GLAPIENTRY glVertex2d(GLdouble x, GLdouble y)
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:53
const float R
A colorbar indicator.
Definition: CColorBar.h:34
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
GLuint in
Definition: glext.h:7391
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
const auto bb_max
GLAPI void GLAPIENTRY glEnd(void)
const auto bb_min
void enableDepthTest(bool enable)
Definition: CColorBar.cpp:68
#define GL_LINES
Definition: glew.h:274
GLAPI void GLAPIENTRY glPushMatrix(void)
Lightweight 3D point.
Definition: TPoint3D.h:90
GLenum GLsizei GLsizei height
Definition: glext.h:3558
unsigned __int32 uint32_t
Definition: rptypes.h:50
GLAPI void GLAPIENTRY glDisable(GLenum cap)
void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const override
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
Definition: CColorBar.cpp:193



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019