Main MRPT website > C++ reference for MRPT 1.9.9
CColorBar.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "opengl-precomp.h" // Precompiled header
11 #include <mrpt/opengl/CColorBar.h>
12 #include <mrpt/utils/CStream.h>
13 #include <mrpt/opengl/gl_utils.h>
14 
15 #include "opengl_internals.h"
16 
17 using namespace mrpt;
18 using namespace mrpt::opengl;
19 using namespace mrpt::utils;
20 using namespace mrpt::math;
21 using namespace std;
22 
24 
26  /** The colormap to represent. */
27  const mrpt::utils::TColormap colormap,
28  /** size of the color bar */
29  double width, double height,
30  /** limits for [0,1] colormap indices */
31  double min_col, double max_col,
32  /** limits for values associated to extreme colors */
33  double min_value, double max_value,
34  /** sprintf-like format string for values */
35  const std::string& label_format,
36  /** Label text font size */
37  double label_font_size)
38  : m_colormap(colormap),
39  m_width(width),
40  m_height(height),
41  m_label_format(label_format),
42  m_min_col(min_col),
43  m_max_col(max_col),
44  m_min_value(min_value),
45  m_max_value(max_value),
46  m_label_font_size(label_font_size),
47  m_disable_depth_test(true)
48 {
49 }
50 
52  /** The colormap to represent. */
54  /** size of the color bar */
55  double width, double height,
56  /** limits for [0,1] colormap indices */
57  double min_col, double max_col,
58  /** limits for values associated to extreme colors */
59  double min_value, double max_value,
60  /** sprintf-like format string for values */
61  const std::string& label_format,
62  /** Label text font size */
63  double label_font_size)
64 {
65  return CColorBar::Ptr(
66  new CColorBar(
67  colormap, width, height, min_col, max_col, min_value, max_value,
68  label_format, label_font_size));
69 }
70 
72 {
73  m_colormap = colormap;
75 }
76 
78  double col_min, double col_max, double value_min, double value_max)
79 {
80  m_min_col = col_min;
81  m_max_col = col_max;
82  m_min_value = value_min;
83  m_max_value = value_max;
85 }
86 
88 {
89  m_disable_depth_test = enable;
91 }
92 
93 /*---------------------------------------------------------------
94  render
95  ---------------------------------------------------------------*/
97 {
98 #if MRPT_HAS_OPENGL_GLUT
99  if (m_disable_depth_test)
100  glDisable(GL_DEPTH_TEST); // colobars are typically displayed on-top of
101  // the rest of objects!
103 
104  // solid:
106 
107  unsigned int num_divisions = 64;
108  unsigned int num_labels = 4;
109  unsigned int one_label_each_nth = floor((num_divisions) / num_labels);
110 
111  const double x0 = .0, x1 = m_width, x2 = m_width * 1.3;
112  const double Ay = m_height / (num_divisions - 1);
113 
114  std::vector<mrpt::utils::TColorf> cols(num_divisions);
115  for (unsigned int i = 0; i < num_divisions; i++)
116  {
117  const double col_idx =
118  m_min_col + i * (m_max_col - m_min_col) / (num_divisions - 1);
120  m_colormap, col_idx, cols[i].R, cols[i].G, cols[i].B);
121  }
122 
123  for (unsigned int i = 0; i < num_divisions - 1; i++)
124  {
125  const double y0 = Ay * i, y1 = Ay * (i + 1);
126  const TPoint3D pt00(x0, y0, 0), pt10(x1, y0, 0);
127  const TPoint3D pt01(x0, y1, 0), pt11(x1, y1, 0);
128 
129  // Color quad:
130 
132  glColor3f(cols[i].R, cols[i].G, cols[i].B);
133  glVertex3f(pt00.x, pt00.y, pt00.z);
134  glVertex3f(pt10.x, pt10.y, pt10.z);
135  glColor3f(cols[i + 1].R, cols[i + 1].G, cols[i + 1].B);
136  glVertex3f(pt11.x, pt11.y, pt11.z);
137  //
138  glColor3f(cols[i].R, cols[i].G, cols[i].B);
139  glVertex3f(pt00.x, pt00.y, pt00.z);
140  glColor3f(cols[i + 1].R, cols[i + 1].G, cols[i + 1].B);
141  glVertex3f(pt11.x, pt11.y, pt11.z);
142  glVertex3f(pt01.x, pt01.y, pt01.z);
143  glEnd();
144  }
145 
147 
148  for (unsigned int i = 0; i < num_divisions; i++)
149  {
150  const double val =
151  m_min_value + i * (m_max_value - m_min_value) / (num_divisions - 1);
152  const double y0 = Ay * i; //, y1 = Ay*(i + 1);
153 
154  // Text label:
155  bool draw_label =
156  (i % one_label_each_nth) == 0 || i == (num_divisions - 1);
157 
158  if (draw_label)
159  {
160  // Line:
161  glLineWidth(1.0);
162  glBegin(GL_LINES);
163  glColor3b(0, 0, 0);
164  glVertex2d(x0, y0);
165  glVertex2d(x2, y0);
166  glEnd();
167 
168  // Text:
169  glPushMatrix();
170 
171  glTranslatef(x2, y0, 0.0);
172  glColor3ub(0xff, 0xff, 0xff);
174  mrpt::format(m_label_format.c_str(), val), m_label_font_size);
175 
176  glPopMatrix();
177  }
178  }
179 
181 
182 #endif
183 }
184 
185 /*---------------------------------------------------------------
186  Implements the writing to a CStream capability of
187  CSerializable objects
188  ---------------------------------------------------------------*/
189 void CColorBar::writeToStream(mrpt::utils::CStream& out, int* version) const
190 {
191  if (version)
192  *version = 0;
193  else
194  {
195  writeToStreamRender(out);
196  // version 0
197  out << uint32_t(m_colormap) << m_min_col << m_max_col << m_min_value
198  << m_max_value << m_label_format << m_label_font_size
199  << m_disable_depth_test;
200  }
201 }
202 
203 /*---------------------------------------------------------------
204  Implements the reading from a CStream capability of
205  CSerializable objects
206  ---------------------------------------------------------------*/
208 {
209  switch (version)
210  {
211  case 0:
212  readFromStreamRender(in);
213 
214  in.ReadAsAndCastTo<uint32_t, mrpt::utils::TColormap>(m_colormap);
215  in >> m_min_col >> m_max_col >> m_min_value >> m_max_value >>
216  m_label_format >> m_label_font_size >> m_disable_depth_test;
217  break;
218  default:
220  };
222 }
223 
225  mrpt::math::TPoint3D& bb_min, mrpt::math::TPoint3D& bb_max) const
226 {
227  bb_min.x = 0;
228  bb_min.y = 0;
229  bb_min.z = 0;
230 
231  bb_max.x = m_width;
232  bb_max.y = m_height;
233  bb_max.z = 0;
234 
235  // Convert to coordinates of my parent:
236  m_pose.composePoint(bb_min, bb_min);
237  m_pose.composePoint(bb_max, bb_max);
238 }
const float R
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
A colorbar indicator.
Definition: CColorBar.h:37
void readFromStream(mrpt::utils::CStream &in, int version) override
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
Definition: CColorBar.cpp:207
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:224
static Ptr Create(Args &&... args)
Definition: CColorBar.h:38
std::shared_ptr< CColorBar > Ptr
Definition: CColorBar.h:38
void setColorAndValueLimits(double col_min, double col_max, double value_min, double value_max)
Definition: CColorBar.cpp:77
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
Definition: CColorBar.cpp:189
void setColormap(const mrpt::utils::TColormap colormap)
Definition: CColorBar.cpp:71
void render_dl() const override
Render.
Definition: CColorBar.cpp:96
void enableDepthTest(bool enable)
Definition: CColorBar.cpp:87
A renderizable object suitable for rendering with OpenGL's display lists.
EIGEN_STRONG_INLINE void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated)
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:42
GLAPI void GLAPIENTRY glTranslatef(GLfloat x, GLfloat y, GLfloat z)
GLAPI void GLAPIENTRY glVertex2d(GLdouble x, GLdouble y)
GLAPI void GLAPIENTRY glEnable(GLenum cap)
#define GL_DEPTH_TEST
Definition: glew.h:401
#define GL_LINES
Definition: glew.h:273
#define GL_SMOOTH
Definition: glew.h:635
#define GL_TRIANGLES
Definition: glew.h:276
GLAPI void GLAPIENTRY glPushMatrix(void)
GLAPI void GLAPIENTRY glLineWidth(GLfloat width)
GLAPI void GLAPIENTRY glBegin(GLenum mode)
GLAPI void GLAPIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z)
GLAPI void GLAPIENTRY glPopMatrix(void)
GLAPI void GLAPIENTRY glEnd(void)
GLAPI void GLAPIENTRY glColor3ub(GLubyte red, GLubyte green, GLubyte blue)
GLAPI void GLAPIENTRY glColor3f(GLfloat red, GLfloat green, GLfloat blue)
GLAPI void GLAPIENTRY glDisable(GLenum cap)
GLAPI void GLAPIENTRY glColor3b(GLbyte red, GLbyte green, GLbyte blue)
GLAPI void GLAPIENTRY glShadeModel(GLenum mode)
#define GL_LIGHTING
Definition: glew.h:385
GLenum GLsizei width
Definition: glext.h:3531
GLuint in
Definition: glext.h:7274
GLenum GLsizei GLsizei height
Definition: glext.h:3554
GLsizei const GLchar ** string
Definition: glext.h:4101
TColormap
Different colormaps for use in mrpt::utils::colormap()
Definition: color_maps.h:32
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:115
int val
Definition: mrpt_jpeglib.h:955
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: mrpt_macros.h:181
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:20
void glSetFont(const std::string &fontname)
sets the font to use for future font rendering commands.
Definition: gl_utils.cpp:621
mrpt::utils::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:640
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:16
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values,...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:19
unsigned __int32 uint32_t
Definition: rptypes.h:47
Lightweight 3D point.
double x
X,Y,Z coordinates.



Page generated by Doxygen 1.9.1 for MRPT 1.9.9 Git: 63ea9d1f1 Thu Nov 23 00:06:53 2017 +0100 at mar 26 may 2026 12:19:29 CEST