Main MRPT website > C++ reference for MRPT 1.9.9
CPointCloudColoured.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 
13 #include <mrpt/utils/round.h> // round()
14 #include <mrpt/utils/CStream.h>
15 #include <mrpt/math/ops_containers.h> // for << ops
17 
18 #include "opengl_internals.h"
19 
20 using namespace mrpt;
21 using namespace mrpt::opengl;
22 using namespace mrpt::utils;
23 // using namespace mrpt::slam;
24 using namespace mrpt::math;
25 using namespace std;
26 
28 
29 /*---------------------------------------------------------------
30  render
31  ---------------------------------------------------------------*/
32 void CPointCloudColoured::render() const
33 {
34 #if MRPT_HAS_OPENGL_GLUT
35  octree_assure_uptodate(); // Rebuild octree if needed
36  m_last_rendered_count_ongoing = 0;
37 
38  // Info needed by octree renderer:
41 
42  if (m_color.A != 255)
43  {
46  }
47 
48  glPointSize(m_pointSize);
49 
50  if (m_pointSmooth)
52  else
54 
55  // Disable lighting for point clouds:
57 
59  octree_render(ri); // Render all points recursively:
60  glEnd();
61 
63 
64  // Undo flags:
65  if (m_color.A != 255) glDisable(GL_BLEND);
66 
67  if (m_pointSmooth) glDisable(GL_POINT_SMOOTH);
68 
69  m_last_rendered_count = m_last_rendered_count_ongoing;
70 
72 #endif
73 }
74 
75 /** Render a subset of points (required by octree renderer) */
77  const bool all, const std::vector<size_t>& idxs,
78  const float render_area_sqpixels) const
79 {
80 #if MRPT_HAS_OPENGL_GLUT
81  const size_t N = all ? m_points.size() : idxs.size();
82  const size_t decimation = mrpt::utils::round(
83  std::max(
84  1.0f, static_cast<float>(
87  render_area_sqpixels))));
88 
89  m_last_rendered_count_ongoing += N / decimation;
90 
91  m_last_rendered_count_ongoing +=
92  (all ? m_points.size() : idxs.size()) / decimation;
93 
94  if (all)
95  {
96  for (size_t i = 0; i < N; i += decimation)
97  {
98  const TPointColour& p = m_points[i];
99  glColor4f(p.R, p.G, p.B, m_color.A * 1.0f / 255.f);
100  glVertex3f(p.x, p.y, p.z);
101  }
102  }
103  else
104  {
105  for (size_t i = 0; i < N; i += decimation)
106  {
107  const TPointColour& p = m_points[idxs[i]];
108  glColor4f(p.R, p.G, p.B, m_color.A * 1.0f / 255.f);
109  glVertex3f(p.x, p.y, p.z);
110  }
111  }
112 #else
113  MRPT_UNUSED_PARAM(all);
114  MRPT_UNUSED_PARAM(idxs);
115  MRPT_UNUSED_PARAM(render_area_sqpixels);
116 #endif
117 }
118 
119 /*---------------------------------------------------------------
120  Implements the writing to a CStream capability of
121  CSerializable objects
122  ---------------------------------------------------------------*/
124  mrpt::utils::CStream& out, int* version) const
125 {
126  if (version)
127  *version = 2;
128  else
129  {
130  writeToStreamRender(out);
131  out << m_points;
132  out << m_pointSize;
133  out << m_pointSmooth; // Added in v2
134  }
135 }
136 
137 /*---------------------------------------------------------------
138  Implements the reading from a CStream capability of
139  CSerializable objects
140  ---------------------------------------------------------------*/
142 {
143  switch (version)
144  {
145  case 1:
146  case 2:
147  {
148  readFromStreamRender(in);
149  in >> m_points >> m_pointSize;
150 
151  if (version >= 2)
152  in >> m_pointSmooth;
153  else
154  m_pointSmooth = false;
155  }
156  break;
157  case 0:
158  {
159  readFromStreamRender(in);
160 
161  // Old vector_serializable:
162  uint32_t n;
163  in >> n;
164  m_points.resize(n);
165  for (uint32_t i = 0; i < n; i++) in >> m_points[i];
166 
167  in >> m_pointSize;
168  }
169  break;
170  default:
172  };
173  markAllPointsAsNew();
174 }
175 
178 {
179  in >> o.x >> o.y >> o.z >> o.R >> o.G >> o.B;
180  return in;
181 }
182 
185 {
186  out << o.x << o.y << o.z << o.R << o.G << o.B;
187  return out;
188 }
189 
190 /** Write an individual point (checks for "i" in the valid range only in Debug).
191  */
193 {
194 #ifdef _DEBUG
195  ASSERT_BELOW_(i, size())
196 #endif
197  m_points[i] = p;
198 
199  // JL: TODO note: Well, this can be clearly done much more efficiently
200  // but...I don't have time! :-(
201  markAllPointsAsNew();
202 }
203 
204 /** Inserts a new point into the point cloud. */
206  float x, float y, float z, float R, float G, float B)
207 {
208  m_points.push_back(TPointColour(x, y, z, R, G, B));
209 
210  // JL: TODO note: Well, this can be clearly done much more efficiently
211  // but...I don't have time! :-(
212  markAllPointsAsNew();
213 }
214 
215 // Do needed internal work if all points are new (octree rebuilt,...)
216 void CPointCloudColoured::markAllPointsAsNew() { octree_mark_as_outdated(); }
217 /** In a base class, reserve memory to prepare subsequent calls to
218  * PLY_import_set_vertex */
220 {
221  this->resize(N);
222 }
223 
224 /** In a base class, will be called after PLY_import_set_vertex_count() once for
225  * each loaded point.
226  * \param pt_color Will be nullptr if the loaded file does not provide color
227  * info.
228  */
230  const size_t idx, const mrpt::math::TPoint3Df& pt,
231  const mrpt::utils::TColorf* pt_color)
232 {
233  if (!pt_color)
234  this->setPoint(idx, TPointColour(pt.x, pt.y, pt.z, 1, 1, 1));
235  else
236  this->setPoint(
237  idx, TPointColour(
238  pt.x, pt.y, pt.z, pt_color->R, pt_color->G, pt_color->B));
239 }
240 
241 /** In a base class, return the number of vertices */
243 {
244  return this->size();
245 }
246 
247 /** In a base class, will be called after PLY_export_get_vertex_count() once for
248  * each exported point.
249  * \param pt_color Will be nullptr if the loaded file does not provide color
250  * info.
251  */
253  const size_t idx, mrpt::math::TPoint3Df& pt, bool& pt_has_color,
254  mrpt::utils::TColorf& pt_color) const
255 {
256  const TPointColour& p = m_points[idx];
257  pt.x = p.x;
258  pt.y = p.y;
259  pt.z = p.z;
260  pt_color.R = p.R;
261  pt_color.G = p.G;
262  pt_color.B = p.B;
263  pt_has_color = true;
264 }
265 
267  const float coord_min, const float coord_max, const int coord_index,
268  const mrpt::utils::TColormap color_map)
269 {
270  ASSERT_ABOVEEQ_(coord_index, 0);
271  ASSERT_BELOW_(coord_index, 3);
272 
273  const float coord_range = coord_max - coord_min;
274  const float coord_range_1 = coord_range != 0.0f ? 1.0f / coord_range : 1.0f;
275  for (size_t i = 0; i < m_points.size(); i++)
276  {
277  float coord = .0f;
278  switch (coord_index)
279  {
280  case 0:
281  coord = m_points[i].x;
282  break;
283  case 1:
284  coord = m_points[i].y;
285  break;
286  case 2:
287  coord = m_points[i].z;
288  break;
289  };
290  const float col_idx =
291  std::max(0.0f, std::min(1.0f, (coord - coord_min) * coord_range_1));
292  float r, g, b;
293  mrpt::utils::colormap(color_map, col_idx, r, g, b);
294  this->setPointColor_fast(i, r, g, b);
295  }
296 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
GLdouble GLdouble z
Definition: glext.h:3872
#define min(a, b)
mrpt::utils::CStream & operator<<(mrpt::utils::CStream &out, const mrpt::opengl::CLight &o)
Definition: CLight.cpp:132
Global variables to change the run-time behaviour of some MRPT classes within mrpt-base.
GLAPI void GLAPIENTRY glEnable(GLenum cap)
GLAPI void GLAPIENTRY glPointSize(GLfloat size)
TColormap
Different colormaps for use in mrpt::utils::colormap()
Definition: color_maps.h:31
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
A cloud of points, each one with an individual colour (R,G,B).
#define ASSERT_BELOW_(__A, __B)
GLenum GLsizei n
Definition: glext.h:5074
This file implements several operations that operate element-wise on individual or pairs of container...
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:43
STL namespace.
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:287
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
#define GL_LIGHTING
Definition: glew.h:385
GLuint coord
Definition: glext.h:7131
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
void setPoint(size_t i, const TPointColour &p)
Write an individual point (checks for "i" in the valid range only in Debug).
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
Lightweight 3D point (float version).
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
void recolorizeByCoordinate(const float coord_min, const float coord_max, const int coord_index=2, const mrpt::utils::TColormap color_map=mrpt::utils::cmJET)
Regenerates the color of each point according the one coordinate (coord_index:0,1,2 for X,Y,Z) and the given color map.
void OCTREE_RENDER_MAX_DENSITY_POINTS_PER_SQPIXEL(float value)
Default value = 0.01 points/px^2.
Definition: CPointCloud.cpp:32
Information about the rendering process being issued.
Definition: gl_utils.h:34
GLubyte g
Definition: glext.h:6279
GLubyte GLubyte b
Definition: glext.h:6279
size_t PLY_export_get_vertex_count() const override
In a base class, return the number of vertices.
#define GL_POINT_SMOOTH
Definition: glew.h:363
GLAPI void GLAPIENTRY glBegin(GLenum mode)
#define GL_BLEND
Definition: glew.h:432
#define GL_POINTS
Definition: glew.h:272
virtual void PLY_import_set_vertex(const size_t idx, const mrpt::math::TPoint3Df &pt, const mrpt::utils::TColorf *pt_color=nullptr) override
In a base class, will be called after PLY_import_set_vertex_count() once for each loaded point...
GLAPI void GLAPIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z)
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...
mrpt::utils::CStream & operator>>(mrpt::utils::CStream &in, mrpt::opengl::CLight &o)
Definition: CLight.cpp:126
#define GL_SRC_ALPHA
Definition: glew.h:286
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define ASSERT_ABOVEEQ_(__A, __B)
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
void push_back(float x, float y, float z, float R, float G, float B)
Inserts a new point into the point cloud.
const float R
void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:140
void render_subset(const bool all, const std::vector< size_t > &idxs, const float render_area_sqpixels) const
Render a subset of points (required by octree renderer)
GLuint in
Definition: glext.h:7274
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
void getCurrentRenderingInfo(TRenderInfo &ri)
Gather useful information on the render parameters.
Definition: gl_utils.cpp:213
A RGB color - floats in the range [0,1].
Definition: TColor.h:78
GLAPI void GLAPIENTRY glEnd(void)
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:25
GLenum GLint GLint y
Definition: glext.h:3538
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
GLAPI void GLAPIENTRY glColor4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
GLsizeiptr size
Definition: glext.h:3923
GLenum GLint x
Definition: glext.h:3538
unsigned __int32 uint32_t
Definition: rptypes.h:47
GLAPI void GLAPIENTRY glDisable(GLenum cap)
virtual void PLY_import_set_vertex_count(const size_t N) override
In a base class, reserve memory to prepare subsequent calls to PLY_import_set_vertex.
GLfloat GLfloat p
Definition: glext.h:6305
void PLY_export_get_vertex(const size_t idx, mrpt::math::TPoint3Df &pt, bool &pt_has_color, mrpt::utils::TColorf &pt_color) const override
In a base class, will be called after PLY_export_get_vertex_count() once for each exported point...
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
void markAllPointsAsNew()
Do needed internal work if all points are new (octree rebuilt,...)



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019