Main MRPT website > C++ reference for MRPT 1.5.7
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  const mrpt::utils::TColormap colormap, //!< The colormap to represent.
27  double width, double height, //!< size of the color bar
28  double min_col, double max_col, //!< limits for [0,1] colormap indices
29  double min_value, double max_value, //!< limits for values associated to extreme colors
30  const std::string &label_format, //!< sprintf-like format string for values
31  double label_font_size //!< Label text font size
32 ) :
33  m_colormap(colormap),
34  m_width(width), m_height(height),
35  m_label_format(label_format),
36  m_min_col(min_col), m_max_col(max_col),
37  m_min_value(min_value), m_max_value(max_value),
38  m_label_font_size(label_font_size),
39  m_disable_depth_test(true)
40 {
41 
42 }
43 
44 CColorBarPtr CColorBar::Create(
45  const mrpt::utils::TColormap colormap, //!< The colormap to represent.
46  double width, double height, //!< size of the color bar
47  double min_col, double max_col, //!< limits for [0,1] colormap indices
48  double min_value, double max_value, //!< limits for values associated to extreme colors
49  const std::string &label_format, //!< sprintf-like format string for values
50  double label_font_size //!< Label text font size
51 )
52 {
53  return CColorBarPtr(new CColorBar(colormap,width,height,min_col,max_col, min_value, max_value, label_format, label_font_size));
54 }
55 
57 {
58  m_colormap = colormap;
60 }
61 
62 void CColorBar::setColorAndValueLimits(double col_min, double col_max, double value_min, double value_max)
63 {
64  m_min_col = col_min;
65  m_max_col = col_max;
66  m_min_value = value_min;
67  m_max_value = value_max;
69 }
70 
72 {
73  m_disable_depth_test = enable;
75 }
76 
77 /*---------------------------------------------------------------
78  render
79  ---------------------------------------------------------------*/
80 void CColorBar::render_dl() const {
81 #if MRPT_HAS_OPENGL_GLUT
82  if (m_disable_depth_test) glDisable(GL_DEPTH_TEST); // colobars are typically displayed on-top of 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::utils::TColorf> cols(num_divisions);
96  for (unsigned int i = 0; i < num_divisions; i++) {
97  const double col_idx = m_min_col + i*(m_max_col - m_min_col) / (num_divisions - 1);
98  mrpt::utils::colormap(m_colormap, col_idx, cols[i].R, cols[i].G, cols[i].B);
99  }
100 
101  for (unsigned int i = 0; i < num_divisions-1; i++)
102  {
103  const double y0 = Ay*i, y1 = Ay*(i + 1);
104  const TPoint3D pt00(x0, y0, 0), pt10(x1, y0, 0);
105  const TPoint3D pt01(x0, y1, 0), pt11(x1, y1, 0);
106 
107  // Color quad:
108 
110  glColor3f(cols[i].R, cols[i].G, cols[i].B);
111  glVertex3f(pt00.x, pt00.y, pt00.z);
112  glVertex3f(pt10.x, pt10.y, pt10.z);
113  glColor3f(cols[i + 1].R, cols[i + 1].G, cols[i + 1].B);
114  glVertex3f(pt11.x, pt11.y, pt11.z);
115  //
116  glColor3f(cols[i].R, cols[i].G, cols[i].B);
117  glVertex3f(pt00.x, pt00.y, pt00.z);
118  glColor3f(cols[i + 1].R, cols[i + 1].G, cols[i + 1].B);
119  glVertex3f(pt11.x, pt11.y, pt11.z);
120  glVertex3f(pt01.x, pt01.y, pt01.z);
121  glEnd();
122  }
123 
125 
126  for (unsigned int i = 0; i < num_divisions; i++)
127  {
128  const double val = m_min_value + i*(m_max_value - m_min_value) / (num_divisions - 1);
129  const double y0 = Ay*i; //, y1 = Ay*(i + 1);
130 
131  // Text label:
132  bool draw_label = (i % one_label_each_nth) == 0 || i == (num_divisions - 1);
133 
134  if (draw_label)
135  {
136  // Line:
137  glLineWidth(1.0);
138  glBegin(GL_LINES);
139  glColor3b(0, 0, 0);
140  glVertex2d(x0, y0);
141  glVertex2d(x2, y0);
142  glEnd();
143 
144  // Text:
145  glPushMatrix();
146 
147  glTranslatef(x2, y0, 0.0);
148  glColor3ub(0xff, 0xff, 0xff);
149  mrpt::opengl::gl_utils::glDrawText(mrpt::format(m_label_format.c_str(), val),m_label_font_size);
150 
151  glPopMatrix();
152  }
153  }
154 
156 
157 #endif
158 }
159 
160 /*---------------------------------------------------------------
161  Implements the writing to a CStream capability of
162  CSerializable objects
163  ---------------------------------------------------------------*/
165  if (version) *version=0;
166  else {
167  writeToStreamRender(out);
168  //version 0
169  out <<
170  uint32_t(m_colormap) <<
171  m_min_col << m_max_col <<
172  m_min_value << m_max_value <<
173  m_label_format <<
174  m_label_font_size << m_disable_depth_test;
175  }
176 }
177 
178 /*---------------------------------------------------------------
179  Implements the reading from a CStream capability of
180  CSerializable objects
181  ---------------------------------------------------------------*/
183  switch (version) {
184  case 0:
185  readFromStreamRender(in);
186 
187  in.ReadAsAndCastTo<uint32_t, mrpt::utils::TColormap>(m_colormap);
188  in >>
189  m_min_col >> m_max_col >>
190  m_min_value >> m_max_value >>
191  m_label_format >>
192  m_label_font_size >>
193  m_disable_depth_test;
194  break;
195  default:
197  };
199 }
200 
201 
203 {
204  bb_min.x = 0;
205  bb_min.y = 0;
206  bb_min.z = 0;
207 
208  bb_max.x = m_width;
209  bb_max.y = m_height;
210  bb_max.z = 0;
211 
212  // Convert to coordinates of my parent:
213  m_pose.composePoint(bb_min, bb_min);
214  m_pose.composePoint(bb_max, bb_max);
215 }
static CColorBarPtr Create()
mrpt::utils::TPixelCoordf OPENGL_IMPEXP 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:600
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
void readFromStream(mrpt::utils::CStream &in, int version)
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
Definition: CColorBar.cpp:182
void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const MRPT_OVERRIDE
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
Definition: CColorBar.cpp:202
GLAPI void GLAPIENTRY glColor3b(GLbyte red, GLbyte green, GLbyte blue)
GLAPI void GLAPIENTRY glEnable(GLenum cap)
TColormap
Different colormaps for use in mrpt::utils::colormap()
Definition: color_maps.h:30
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
GLAPI void GLAPIENTRY glPopMatrix(void)
#define GL_TRIANGLES
Definition: glew.h:272
EIGEN_STRONG_INLINE void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
STL namespace.
double z
X,Y,Z coordinates.
void BASE_IMPEXP 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:101
#define GL_DEPTH_TEST
Definition: glew.h:397
#define GL_SMOOTH
Definition: glew.h:631
GLAPI void GLAPIENTRY glShadeModel(GLenum mode)
#define GL_LIGHTING
Definition: glew.h:381
GLAPI void GLAPIENTRY glLineWidth(GLfloat width)
void OPENGL_IMPEXP glSetFont(const std::string &fontname)
sets the font to use for future font rendering commands.
Definition: gl_utils.cpp:583
GLenum GLsizei width
Definition: glext.h:3513
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const
Introduces a pure virtual method responsible for writing to a CStream.
Definition: CColorBar.cpp:164
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
void setColormap(const mrpt::utils::TColormap colormap)
Definition: CColorBar.cpp:56
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
void render_dl() const MRPT_OVERRIDE
Render.
Definition: CColorBar.cpp:80
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
int val
Definition: mrpt_jpeglib.h:953
GLAPI void GLAPIENTRY glColor3f(GLfloat red, GLfloat green, GLfloat blue)
GLAPI void GLAPIENTRY glColor3ub(GLubyte red, GLubyte green, GLubyte blue)
int version
Definition: mrpt_jpeglib.h:898
GLAPI void GLAPIENTRY glBegin(GLenum mode)
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
GLsizei const GLchar ** string
Definition: glext.h:3919
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:62
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLAPI void GLAPIENTRY glVertex2d(GLdouble x, GLdouble y)
const float R
A colorbar indicator.
Definition: CColorBar.h:34
GLuint in
Definition: glext.h:6301
The namespace for 3D scene representation and rendering.
GLAPI void GLAPIENTRY glEnd(void)
void enableDepthTest(bool enable)
Definition: CColorBar.cpp:71
#define GL_LINES
Definition: glew.h:269
GLAPI void GLAPIENTRY glPushMatrix(void)
Lightweight 3D point.
GLenum GLsizei GLsizei height
Definition: glext.h:3523
unsigned __int32 uint32_t
Definition: rptypes.h:49
GLAPI void GLAPIENTRY glDisable(GLenum cap)



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019