MRPT  1.9.9
CPointCloudColoured.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-2019, 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/core/round.h> // round()
13 #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::math;
23 using namespace std;
25 
27 
28 /*---------------------------------------------------------------
29  render
30  ---------------------------------------------------------------*/
31 void CPointCloudColoured::render() const
32 {
33 #if MRPT_HAS_OPENGL_GLUT
34  octree_assure_uptodate(); // Rebuild octree if needed
35  m_last_rendered_count_ongoing = 0;
36 
37  // Info needed by octree renderer:
40 
41  if (m_color.A != 255)
42  {
45  }
46 
47  glPointSize(m_pointSize);
48 
49  if (m_pointSmooth)
51  else
53 
54  // Disable lighting for point clouds:
56 
58  octree_render(ri); // Render all points recursively:
59  glEnd();
60 
62 
63  // Undo flags:
64  if (m_color.A != 255) glDisable(GL_BLEND);
65 
66  if (m_pointSmooth) glDisable(GL_POINT_SMOOTH);
67 
68  m_last_rendered_count = m_last_rendered_count_ongoing;
69 
71 #endif
72 }
73 
74 /** Render a subset of points (required by octree renderer) */
76  const bool all, const std::vector<size_t>& idxs,
77  const float render_area_sqpixels) const
78 {
79 #if MRPT_HAS_OPENGL_GLUT
80  const size_t N = all ? m_points.size() : idxs.size();
81  const size_t decimation = mrpt::round(std::max(
82  1.0f, static_cast<float>(
85  render_area_sqpixels))));
86 
87  m_last_rendered_count_ongoing += N / decimation;
88 
89  m_last_rendered_count_ongoing +=
90  (all ? m_points.size() : idxs.size()) / decimation;
91 
92  if (all)
93  {
94  for (size_t i = 0; i < N; i += decimation)
95  {
96  const TPointColour& p = m_points[i];
97  glColor4f(p.R, p.G, p.B, m_color.A * 1.0f / 255.f);
98  glVertex3f(p.x, p.y, p.z);
99  }
100  }
101  else
102  {
103  for (size_t i = 0; i < N; i += decimation)
104  {
105  const TPointColour& p = m_points[idxs[i]];
106  glColor4f(p.R, p.G, p.B, m_color.A * 1.0f / 255.f);
107  glVertex3f(p.x, p.y, p.z);
108  }
109  }
110 #else
111  MRPT_UNUSED_PARAM(all);
112  MRPT_UNUSED_PARAM(idxs);
113  MRPT_UNUSED_PARAM(render_area_sqpixels);
114 #endif
115 }
116 
117 uint8_t CPointCloudColoured::serializeGetVersion() const { return 2; }
119 {
120  writeToStreamRender(out);
121  out << m_points;
122  out << m_pointSize;
123  out << m_pointSmooth; // Added in v2
124 }
125 
127  mrpt::serialization::CArchive& in, uint8_t version)
128 {
129  switch (version)
130  {
131  case 1:
132  case 2:
133  {
134  readFromStreamRender(in);
135  in >> m_points >> m_pointSize;
136 
137  if (version >= 2)
138  in >> m_pointSmooth;
139  else
140  m_pointSmooth = false;
141  }
142  break;
143  case 0:
144  {
145  readFromStreamRender(in);
146 
147  // Old vector_serializable:
148  uint32_t n;
149  in >> n;
150  m_points.resize(n);
151  for (uint32_t i = 0; i < n; i++) in >> m_points[i];
152 
153  in >> m_pointSize;
154  }
155  break;
156  default:
158  };
159  markAllPointsAsNew();
160 }
161 
164 {
165  in >> o.x >> o.y >> o.z >> o.R >> o.G >> o.B;
166  return in;
167 }
168 
171 {
172  out << o.x << o.y << o.z << o.R << o.G << o.B;
173  return out;
174 }
175 
176 /** Write an individual point (checks for "i" in the valid range only in Debug).
177  */
179 {
180 #ifdef _DEBUG
181  ASSERT_BELOW_(i, size());
182 #endif
183  m_points[i] = p;
184 
185  // JL: TODO note: Well, this can be clearly done much more efficiently
186  // but...I don't have time! :-(
187  markAllPointsAsNew();
188 }
189 
190 /** Inserts a new point into the point cloud. */
192  float x, float y, float z, float R, float G, float B)
193 {
194  m_points.push_back(TPointColour(x, y, z, R, G, B));
195 
196  // JL: TODO note: Well, this can be clearly done much more efficiently
197  // but...I don't have time! :-(
198  markAllPointsAsNew();
199 }
200 
201 // Do needed internal work if all points are new (octree rebuilt,...)
202 void CPointCloudColoured::markAllPointsAsNew() { octree_mark_as_outdated(); }
203 /** In a base class, reserve memory to prepare subsequent calls to
204  * PLY_import_set_vertex */
206 {
207  this->resize(N);
208 }
209 
210 /** In a base class, will be called after PLY_import_set_vertex_count() once for
211  * each loaded point.
212  * \param pt_color Will be nullptr if the loaded file does not provide color
213  * info.
214  */
216  const size_t idx, const mrpt::math::TPoint3Df& pt,
217  const mrpt::img::TColorf* pt_color)
218 {
219  if (!pt_color)
220  this->setPoint(idx, TPointColour(pt.x, pt.y, pt.z, 1, 1, 1));
221  else
222  this->setPoint(
223  idx, TPointColour(
224  pt.x, pt.y, pt.z, pt_color->R, pt_color->G, pt_color->B));
225 }
226 
227 /** In a base class, return the number of vertices */
229 {
230  return this->size();
231 }
232 
233 /** In a base class, will be called after PLY_export_get_vertex_count() once for
234  * each exported point.
235  * \param pt_color Will be nullptr if the loaded file does not provide color
236  * info.
237  */
239  const size_t idx, mrpt::math::TPoint3Df& pt, bool& pt_has_color,
240  mrpt::img::TColorf& pt_color) const
241 {
242  const TPointColour& p = m_points[idx];
243  pt.x = p.x;
244  pt.y = p.y;
245  pt.z = p.z;
246  pt_color.R = p.R;
247  pt_color.G = p.G;
248  pt_color.B = p.B;
249  pt_has_color = true;
250 }
251 
253  const float coord_min, const float coord_max, const int coord_index,
254  const mrpt::img::TColormap color_map)
255 {
256  ASSERT_ABOVEEQ_(coord_index, 0);
257  ASSERT_BELOW_(coord_index, 3);
258 
259  const float coord_range = coord_max - coord_min;
260  const float coord_range_1 = coord_range != 0.0f ? 1.0f / coord_range : 1.0f;
261  for (size_t i = 0; i < m_points.size(); i++)
262  {
263  float coord = .0f;
264  switch (coord_index)
265  {
266  case 0:
267  coord = m_points[i].x;
268  break;
269  case 1:
270  coord = m_points[i].y;
271  break;
272  case 2:
273  coord = m_points[i].z;
274  break;
275  };
276  const float col_idx =
277  std::max(0.0f, std::min(1.0f, (coord - coord_min) * coord_range_1));
278  float r, g, b;
279  mrpt::img::colormap(color_map, col_idx, r, g, b);
280  this->setPointColor_fast(i, r, g, b);
281  }
282 }
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:29
GLdouble GLdouble z
Definition: glext.h:3879
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)
mrpt::serialization::CArchive & operator<<(mrpt::serialization::CArchive &out, const mrpt::opengl::CLight &o)
Definition: CLight.cpp:122
#define ASSERT_BELOW_(__A, __B)
Definition: exceptions.h:149
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
const double G
A cloud of points, each one with an individual colour (R,G,B).
GLenum GLsizei n
Definition: glext.h:5136
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:40
STL namespace.
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:288
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
#define GL_LIGHTING
Definition: glew.h:386
GLuint coord
Definition: glext.h:7245
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
void setPoint(size_t i, const TPointColour &p)
Write an individual point (checks for "i" in the valid range only in Debug).
This base provides a set of functions for maths stuff.
Lightweight 3D point (float version).
Definition: TPoint3D.h:21
void OCTREE_RENDER_MAX_DENSITY_POINTS_PER_SQPIXEL(float value)
Default value = 0.01 points/px^2.
Definition: CPointCloud.cpp:33
Information about the rendering process being issued.
Definition: gl_utils.h:31
GLubyte g
Definition: glext.h:6372
GLubyte GLubyte b
Definition: glext.h:6372
void PLY_import_set_vertex(const size_t idx, const mrpt::math::TPoint3Df &pt, const mrpt::img::TColorf *pt_color=nullptr) override
In a base class, will be called after PLY_import_set_vertex_count() once for each loaded point...
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:364
GLAPI void GLAPIENTRY glBegin(GLenum mode)
#define GL_BLEND
Definition: glew.h:433
#define GL_POINTS
Definition: glew.h:273
#define ASSERT_ABOVEEQ_(__A, __B)
Definition: exceptions.h:167
GLAPI void GLAPIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z)
#define GL_SRC_ALPHA
Definition: glew.h:287
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
GLdouble GLdouble GLdouble r
Definition: glext.h:3711
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
mrpt::vision::TStereoCalibResults out
void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:155
A RGB color - floats in the range [0,1].
Definition: TColor.h:78
void PLY_export_get_vertex(const size_t idx, mrpt::math::TPoint3Df &pt, bool &pt_has_color, mrpt::img::TColorf &pt_color) const override
In a base class, will be called after PLY_export_get_vertex_count() once for each exported point...
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:7391
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:228
GLAPI void GLAPIENTRY glEnd(void)
mrpt::serialization::CArchive & operator>>(mrpt::serialization::CArchive &in, mrpt::opengl::CLight &o)
Definition: CLight.cpp:116
GLenum GLint GLint y
Definition: glext.h:3542
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
GLAPI void GLAPIENTRY glColor4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
GLsizeiptr size
Definition: glext.h:3934
GLenum GLint x
Definition: glext.h:3542
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
void recolorizeByCoordinate(const float coord_min, const float coord_max, const int coord_index=2, const mrpt::img::TColormap color_map=mrpt::img::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.
GLAPI void GLAPIENTRY glDisable(GLenum cap)
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.
images resize(NUM_IMGS)
GLfloat GLfloat p
Definition: glext.h:6398
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
void markAllPointsAsNew()
Do needed internal work if all points are new (octree rebuilt,...)
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:23



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: cb560b230 Wed Nov 13 08:06:48 2019 +0100 at miƩ nov 13 08:15:10 CET 2019