MRPT  2.0.1
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-2020, 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>
14 #include "gltext.h"
15 
16 using namespace mrpt;
17 using namespace mrpt::opengl;
18 using namespace mrpt::math;
19 using namespace std;
20 
22 
24  /** The colormap to represent. */
25  const mrpt::img::TColormap colormap,
26  /** size of the color bar */
27  double width, double height,
28  /** limits for [0,1] colormap indices */
29  float min_col, float max_col,
30  /** limits for values associated to extreme colors */
31  float min_value, float max_value,
32  /** sprintf-like format string for values */
33  const std::string& label_format,
34  /** Label text font size */
35  float label_font_size)
36  : m_colormap(colormap),
37  m_width(width),
38  m_height(height),
39  m_label_format(label_format),
40  m_min_col(min_col),
41  m_max_col(max_col),
42  m_min_value(min_value),
43  m_max_value(max_value),
44  m_label_font_size(label_font_size)
45 {
46 }
47 
49 {
50  m_colormap = colormap;
52 }
53 
55  float col_min, float col_max, float value_min, float value_max)
56 {
57  m_min_col = col_min;
58  m_max_col = col_max;
59  m_min_value = value_min;
60  m_max_value = value_max;
62 }
63 
64 void CColorBar::render(const RenderContext& rc) const
65 {
66  // colobars are typically displayed on-top
67  switch (rc.shader_id)
68  {
71  break;
74  break;
75  };
76 }
78 {
79  const_cast<CColorBar&>(*this).onUpdateBuffers_all();
80 
83 }
84 
86 {
90  lines_vbd.clear();
91  lines_cbd.clear();
92 
93  // precomputed params:
94  unsigned int num_divisions = 64;
95  unsigned int num_labels = 4;
96  unsigned int one_label_each_nth = num_divisions / num_labels;
97 
98  const double x0 = .0, x1 = m_width, x2 = m_width * 1.3;
99  const double Ay = m_height / (num_divisions - 1);
100 
101  std::vector<mrpt::img::TColor> colors(num_divisions);
102  for (unsigned int i = 0; i < num_divisions; i++)
103  {
104  const float col_idx =
105  m_min_col + i * (m_max_col - m_min_col) / (num_divisions - 1);
106  mrpt::img::TColorf colf;
107  mrpt::img::colormap(m_colormap, col_idx, colf.R, colf.G, colf.B);
108  colors[i] = colf.asTColor();
109  }
110 
111  // Text labels:
113 
114  const auto tickColor = mrpt::img::TColor::black();
115  const auto textColor = mrpt::img::TColor::white();
116 
117  for (unsigned int i = 0; i < num_divisions; i++)
118  {
119  const double val =
120  m_min_value + i * (m_max_value - m_min_value) / (num_divisions - 1);
121  const double y0 = Ay * i; //, y1 = Ay*(i + 1);
122 
123  // Text label:
124  bool draw_label =
125  (i % one_label_each_nth) == 0 || i == (num_divisions - 1);
126 
127  if (draw_label)
128  {
129  // Line:
130  lines_vbd.emplace_back(x0, y0, 0);
131  lines_vbd.emplace_back(x2, y0, 0);
132  lines_cbd.emplace_back(tickColor);
133  lines_cbd.emplace_back(tickColor);
134 
135  // Text:
136 
137  // Size: m_label_font_size
138  // glTranslated(x2, y0, 0.0);
139  mrpt::poses::CPose3D p(x2, y0, 0.0, 0, 0, 0);
140 
142  mrpt::format(m_label_format.c_str(), val), tris, lines_vbd,
143  lines_cbd, p, m_label_font_size, textColor, FILL, 1.5, 0.1);
144  }
145  }
146 
147  // Color bar itself:
148  for (unsigned int i = 0; i < num_divisions - 1; i++)
149  {
150  const double y0 = Ay * i, y1 = Ay * (i + 1);
151  const TPoint3Df pt00(x0, y0, 0), pt10(x1, y0, 0);
152  const TPoint3Df pt01(x0, y1, 0), pt11(x1, y1, 0);
153 
154  // Color quad:
155  // triangle 1/2
157  t.vertices[0].xyzrgba.pt = pt00;
158  t.vertices[0].setColor(colors[i]);
159 
160  t.vertices[1].xyzrgba.pt = pt10;
161  t.vertices[1].setColor(colors[i]);
162 
163  t.vertices[2].xyzrgba.pt = pt11;
164  t.vertices[2].setColor(colors[i + 1]);
165  t.computeNormals();
166  tris.emplace_back(t);
167 
168  // triangle 2/2
169  t.vertices[0].xyzrgba.pt = pt00;
170  t.vertices[0].setColor(colors[i]);
171 
172  t.vertices[1].xyzrgba.pt = pt11;
173  t.vertices[1].setColor(colors[i + 1]);
174 
175  t.vertices[2].xyzrgba.pt = pt01;
176  t.vertices[2].setColor(colors[i + 1]);
177  t.computeNormals();
178  tris.emplace_back(t);
179  }
180 }
181 
183 {
184  // Already done in onUpdateBuffers_all()
185 }
187 {
188  // Already done in onUpdateBuffers_all()
189 }
190 
191 uint8_t CColorBar::serializeGetVersion() const { return 1; }
193 {
194  writeToStreamRender(out);
195  // version 0
196  out << uint32_t(m_colormap) << m_min_col << m_max_col << m_min_value
197  << m_max_value << m_label_format << m_label_font_size;
198 }
200  mrpt::serialization::CArchive& in, uint8_t version)
201 {
202  switch (version)
203  {
204  case 0:
205  case 1:
206  readFromStreamRender(in);
207 
208  in.ReadAsAndCastTo<uint32_t, mrpt::img::TColormap>(m_colormap);
209  in >> m_min_col >> m_max_col >> m_min_value >> m_max_value >>
210  m_label_format >> m_label_font_size;
211  if (version == 0)
212  {
213  bool old_disable_depth_test;
214  in >> old_disable_depth_test;
215  }
216  break;
217  default:
219  };
221 }
222 
225 {
226  bb_min.x = 0;
227  bb_min.y = 0;
228  bb_min.z = 0;
229 
230  bb_max.x = m_width;
231  bb_max.y = m_height;
232  bb_max.z = 0;
233 
234  // Convert to coordinates of my parent:
235  m_pose.composePoint(bb_min, bb_min);
236  m_pose.composePoint(bb_max, bb_max);
237 }
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:30
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
void onUpdateBuffers_Wireframe() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
Definition: CColorBar.cpp:182
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
void notifyChange() const
Call to enable calling renderUpdateBuffers() before the next render() rendering iteration.
A triangle (float coordinates) with RGBA colors (u8) and UV (texture coordinates) for each vertex...
Definition: TTriangle.h:35
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
Definition: CColorBar.cpp:199
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:48
STL namespace.
void glDrawTextTransformed(const std::string &text, std::vector< mrpt::opengl::TTriangle > &tris, std::vector< mrpt::math::TPoint3Df > &lines, std::vector< mrpt::img::TColor > &line_colors, const mrpt::poses::CPose3D &text_pose, float text_scale, const mrpt::img::TColor &text_color, TEXT_STYLE style, double spacing, double kerning)
Appends to {tris,lines} the entities representing a given text including a pose and scale transformat...
Definition: gltext.cpp:268
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Definition: CColorBar.cpp:192
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers, etc.
Context for calls to render()
std::vector< mrpt::math::TPoint3Df > m_vertex_buffer_data
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
std::array< Vertex, 3 > vertices
Definition: TTriangle.h:88
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
Definition: CColorBar.cpp:191
This base provides a set of functions for maths stuff.
void ReadAsAndCastTo(CAST_TO_TYPE &read_here)
Read a value from a stream stored in a type different of the target variable, making the conversion v...
Definition: CArchive.h:147
static constexpr shader_id_t WIREFRAME
static constexpr TColor black()
Definition: TColor.h:69
int val
Definition: mrpt_jpeglib.h:957
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers, etc.
Definition: CColorBar.cpp:77
void setColorAndValueLimits(float col_min, float col_max, float value_min, float value_max)
Definition: CColorBar.cpp:54
void onUpdateBuffers_Triangles() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
Definition: CColorBar.cpp:186
static constexpr shader_id_t TRIANGLES
std::vector< mrpt::img::TColor > m_color_buffer_data
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void setColormap(const mrpt::img::TColormap colormap)
Definition: CColorBar.cpp:48
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
std::vector< mrpt::opengl::TTriangle > m_triangles
List of triangles.
A colorbar indicator.
Definition: CColorBar.h:35
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:85
mrpt::vision::TStereoCalibResults out
void glSetFont(const std::string &fontname)
sets the font to use for future font rendering commands.
Definition: gltext.cpp:146
An RGBA color - floats in the range [0,1].
Definition: TColor.h:88
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
const auto bb_max
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
Definition: CColorBar.cpp:64
void computeNormals()
Compute the three normals from the cross-product of "v01 x v02".
Definition: TTriangle.cpp:21
const auto bb_min
TColor asTColor() const
Returns the 0-255 integer version of this color: RGBA_u8.
Definition: TColor.h:101
renders glyphs as filled polygons
Definition: opengl_fonts.h:21
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
static constexpr TColor white()
Definition: TColor.h:70
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:223
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers, etc.



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020