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-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 
13 #include <mrpt/core/round.h> // round()
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::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(
82  std::max(
83  1.0f, static_cast<float>(
86  render_area_sqpixels))));
87 
88  m_last_rendered_count_ongoing += N / decimation;
89 
90  m_last_rendered_count_ongoing +=
91  (all ? m_points.size() : idxs.size()) / decimation;
92 
93  if (all)
94  {
95  for (size_t i = 0; i < N; i += decimation)
96  {
97  const TPointColour& p = m_points[i];
98  glColor4f(p.R, p.G, p.B, m_color.A * 1.0f / 255.f);
99  glVertex3f(p.x, p.y, p.z);
100  }
101  }
102  else
103  {
104  for (size_t i = 0; i < N; i += decimation)
105  {
106  const TPointColour& p = m_points[idxs[i]];
107  glColor4f(p.R, p.G, p.B, m_color.A * 1.0f / 255.f);
108  glVertex3f(p.x, p.y, p.z);
109  }
110  }
111 #else
112  MRPT_UNUSED_PARAM(all);
113  MRPT_UNUSED_PARAM(idxs);
114  MRPT_UNUSED_PARAM(render_area_sqpixels);
115 #endif
116 }
117 
120 {
121  writeToStreamRender(out);
122  out << m_points;
123  out << m_pointSize;
124  out << m_pointSmooth; // Added in v2
125 }
126 
129 {
130  switch (version)
131  {
132  case 1:
133  case 2:
134  {
135  readFromStreamRender(in);
136  in >> m_points >> m_pointSize;
137 
138  if (version >= 2)
139  in >> m_pointSmooth;
140  else
141  m_pointSmooth = false;
142  }
143  break;
144  case 0:
145  {
146  readFromStreamRender(in);
147 
148  // Old vector_serializable:
149  uint32_t n;
150  in >> n;
151  m_points.resize(n);
152  for (uint32_t i = 0; i < n; i++) in >> m_points[i];
153 
154  in >> m_pointSize;
155  }
156  break;
157  default:
159  };
160  markAllPointsAsNew();
161 }
162 
165 {
166  in >> o.x >> o.y >> o.z >> o.R >> o.G >> o.B;
167  return in;
168 }
169 
172 {
173  out << o.x << o.y << o.z << o.R << o.G << o.B;
174  return out;
175 }
176 
177 /** Write an individual point (checks for "i" in the valid range only in Debug).
178  */
180 {
181 #ifdef _DEBUG
182  ASSERT_BELOW_(i, size());
183 #endif
184  m_points[i] = p;
185 
186  // JL: TODO note: Well, this can be clearly done much more efficiently
187  // but...I don't have time! :-(
188  markAllPointsAsNew();
189 }
190 
191 /** Inserts a new point into the point cloud. */
193  float x, float y, float z, float R, float G, float B)
194 {
195  m_points.push_back(TPointColour(x, y, z, R, G, B));
196 
197  // JL: TODO note: Well, this can be clearly done much more efficiently
198  // but...I don't have time! :-(
199  markAllPointsAsNew();
200 }
201 
202 // Do needed internal work if all points are new (octree rebuilt,...)
203 void CPointCloudColoured::markAllPointsAsNew() { octree_mark_as_outdated(); }
204 /** In a base class, reserve memory to prepare subsequent calls to
205  * PLY_import_set_vertex */
207 {
208  this->resize(N);
209 }
210 
211 /** In a base class, will be called after PLY_import_set_vertex_count() once for
212  * each loaded point.
213  * \param pt_color Will be nullptr if the loaded file does not provide color
214  * info.
215  */
217  const size_t idx, const mrpt::math::TPoint3Df& pt,
218  const mrpt::img::TColorf* pt_color)
219 {
220  if (!pt_color)
221  this->setPoint(idx, TPointColour(pt.x, pt.y, pt.z, 1, 1, 1));
222  else
223  this->setPoint(
224  idx, TPointColour(
225  pt.x, pt.y, pt.z, pt_color->R, pt_color->G, pt_color->B));
226 }
227 
228 /** In a base class, return the number of vertices */
230 {
231  return this->size();
232 }
233 
234 /** In a base class, will be called after PLY_export_get_vertex_count() once for
235  * each exported point.
236  * \param pt_color Will be nullptr if the loaded file does not provide color
237  * info.
238  */
240  const size_t idx, mrpt::math::TPoint3Df& pt, bool& pt_has_color,
241  mrpt::img::TColorf& pt_color) const
242 {
243  const TPointColour& p = m_points[idx];
244  pt.x = p.x;
245  pt.y = p.y;
246  pt.z = p.z;
247  pt_color.R = p.R;
248  pt_color.G = p.G;
249  pt_color.B = p.B;
250  pt_has_color = true;
251 }
252 
254  const float coord_min, const float coord_max, const int coord_index,
255  const mrpt::img::TColormap color_map)
256 {
257  ASSERT_ABOVEEQ_(coord_index, 0);
258  ASSERT_BELOW_(coord_index, 3);
259 
260  const float coord_range = coord_max - coord_min;
261  const float coord_range_1 = coord_range != 0.0f ? 1.0f / coord_range : 1.0f;
262  for (size_t i = 0; i < m_points.size(); i++)
263  {
264  float coord = .0f;
265  switch (coord_index)
266  {
267  case 0:
268  coord = m_points[i].x;
269  break;
270  case 1:
271  coord = m_points[i].y;
272  break;
273  case 2:
274  coord = m_points[i].z;
275  break;
276  };
277  const float col_idx =
278  std::max(0.0f, std::min(1.0f, (coord - coord_min) * coord_range_1));
279  float r, g, b;
280  mrpt::img::colormap(color_map, col_idx, r, g, b);
281  this->setPointColor_fast(i, r, g, b);
282  }
283 }
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:31
GLdouble GLdouble z
Definition: glext.h:3872
#define min(a, b)
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:130
#define ASSERT_BELOW_(__A, __B)
Definition: exceptions.h:165
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in 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: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 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:385
GLuint coord
Definition: glext.h:7131
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
unsigned char uint8_t
Definition: rptypes.h:41
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:90
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).
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:30
GLubyte g
Definition: glext.h:6279
GLubyte GLubyte b
Definition: glext.h:6279
virtual 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:363
GLAPI void GLAPIENTRY glBegin(GLenum mode)
#define GL_BLEND
Definition: glew.h:432
#define GL_POINTS
Definition: glew.h:272
#define ASSERT_ABOVEEQ_(__A, __B)
Definition: exceptions.h:183
GLAPI void GLAPIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z)
#define GL_SRC_ALPHA
Definition: glew.h:286
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:48
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:143
A RGB color - floats in the range [0,1].
Definition: TColor.h:79
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: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:216
GLAPI void GLAPIENTRY glEnd(void)
mrpt::serialization::CArchive & operator>>(mrpt::serialization::CArchive &in, mrpt::opengl::CLight &o)
Definition: CLight.cpp:124
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
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.
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
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: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019