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-2018, 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>
13 #include <mrpt/opengl/gl_utils.h>
14 
15 #include "opengl_internals.h"
16 
17 using namespace mrpt;
18 using namespace mrpt::opengl;
19 
20 using namespace mrpt::math;
21 using namespace std;
22 
24 
26  /** The colormap to represent. */
27  const mrpt::img::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::img::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 
187 {
188  writeToStreamRender(out);
189  // version 0
190  out << uint32_t(m_colormap) << m_min_col << m_max_col << m_min_value
191  << m_max_value << m_label_format << m_label_font_size
192  << m_disable_depth_test;
193 }
196 {
197  switch (version)
198  {
199  case 0:
200  readFromStreamRender(in);
201 
202  in.ReadAsAndCastTo<uint32_t, mrpt::img::TColormap>(m_colormap);
203  in >> m_min_col >> m_max_col >> m_min_value >> m_max_value >>
204  m_label_format >> m_label_font_size >> m_disable_depth_test;
205  break;
206  default:
208  };
210 }
211 
213  mrpt::math::TPoint3D& bb_min, mrpt::math::TPoint3D& bb_max) const
214 {
215  bb_min.x = 0;
216  bb_min.y = 0;
217  bb_min.z = 0;
218 
219  bb_max.x = m_width;
220  bb_max.y = m_height;
221  bb_max.z = 0;
222 
223  // Convert to coordinates of my parent:
224  m_pose.composePoint(bb_min, bb_min);
225  m_pose.composePoint(bb_max, bb_max);
226 }
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:113
TColormap
Different colormaps for use in mrpt::img::colormap()
Definition: color_maps.h:29
GLAPI void GLAPIENTRY glColor3b(GLbyte red, GLbyte green, GLbyte blue)
GLAPI void GLAPIENTRY glEnable(GLenum cap)
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in 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:194
#define GL_TRIANGLES
Definition: glew.h:276
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.
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:532
#define GL_DEPTH_TEST
Definition: glew.h:401
#define GL_SMOOTH
Definition: glew.h:635
GLAPI void GLAPIENTRY glShadeModel(GLenum mode)
#define GL_LIGHTING
Definition: glew.h:385
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:514
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Definition: CColorBar.cpp:186
GLenum GLsizei width
Definition: glext.h:3531
unsigned char uint8_t
Definition: rptypes.h:41
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:90
static Ptr Create(Args &&... args)
Definition: CColorBar.h:36
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
Definition: CColorBar.cpp:185
This base provides a set of functions for maths stuff.
void render_dl() const override
Render.
Definition: CColorBar.cpp:96
GLint GLvoid * img
Definition: glext.h:3763
int val
Definition: mrpt_jpeglib.h:955
std::shared_ptr< CColorBar > Ptr
Definition: CColorBar.h:36
GLAPI void GLAPIENTRY glColor3f(GLfloat red, GLfloat green, GLfloat blue)
GLAPI void GLAPIENTRY glColor3ub(GLubyte red, GLubyte green, GLubyte blue)
double x
X,Y,Z coordinates.
GLAPI void GLAPIENTRY glBegin(GLenum mode)
GLsizei const GLchar ** string
Definition: glext.h:4101
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:77
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void setColormap(const mrpt::img::TColormap colormap)
Definition: CColorBar.cpp:71
GLAPI void GLAPIENTRY glVertex2d(GLdouble x, GLdouble y)
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:52
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:7274
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
GLAPI void GLAPIENTRY glEnd(void)
void enableDepthTest(bool enable)
Definition: CColorBar.cpp:87
#define GL_LINES
Definition: glew.h:273
GLAPI void GLAPIENTRY glPushMatrix(void)
Lightweight 3D point.
GLenum GLsizei GLsizei height
Definition: glext.h:3554
unsigned __int32 uint32_t
Definition: rptypes.h:47
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:212



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020