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 }
mrpt::math::TPoint3Df
Lightweight 3D point (float version).
Definition: lightweight_geom_data.h:315
n
GLenum GLsizei n
Definition: glext.h:5074
mrpt::opengl::CPointCloudColoured::TPointColour
Definition: CPointCloudColoured.h:52
ops_containers.h
glBegin
GLAPI void GLAPIENTRY glBegin(GLenum mode)
mrpt::opengl::CPointCloudColoured::PLY_export_get_vertex_count
size_t PLY_export_get_vertex_count() const override
In a base class, return the number of vertices.
Definition: CPointCloudColoured.cpp:229
mrpt::global_settings
Global variables to change the run-time behaviour of some MRPT classes within mrpt-base.
Definition: CHeightGridMap2D.h:178
mrpt::img::TColormap
TColormap
Different colormaps for use in mrpt::img::colormap()
Definition: color_maps.h:31
mrpt::opengl::CPointCloudColoured::push_back
void push_back(float x, float y, float z, float R, float G, float B)
Inserts a new point into the point cloud.
Definition: CPointCloudColoured.cpp:192
G
const double G
Definition: vision_stereo_rectify/test.cpp:31
GL_SRC_ALPHA
#define GL_SRC_ALPHA
Definition: glew.h:286
mrpt::math::TPoint3Df::z
float z
Definition: lightweight_geom_data.h:323
glColor4f
GLAPI void GLAPIENTRY glColor4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
mrpt::opengl::operator>>
mrpt::serialization::CArchive & operator>>(mrpt::serialization::CArchive &in, mrpt::opengl::CLight &o)
Definition: CLight.cpp:124
mrpt::img::colormap
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
mrpt::opengl::CRenderizable
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:43
mrpt::opengl::CPointCloudColoured::TPointColour::z
float z
Definition: CPointCloudColoured.h:60
GL_BLEND
#define GL_BLEND
Definition: glew.h:432
mrpt::opengl::CPointCloudColoured::render_subset
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)
Definition: CPointCloudColoured.cpp:75
CPointCloudColoured.h
glDisable
GLAPI void GLAPIENTRY glDisable(GLenum cap)
MRPT_UNUSED_PARAM
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186
mrpt::img::TColorf::R
float R
Definition: TColor.h:94
glEnable
GLAPI void GLAPIENTRY glEnable(GLenum cap)
mrpt::opengl::CPointCloudColoured::PLY_export_get_vertex
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.
Definition: CPointCloudColoured.cpp:239
GL_POINT_SMOOTH
#define GL_POINT_SMOOTH
Definition: glew.h:363
stl_serialization.h
mrpt::opengl::CPointCloudColoured::PLY_import_set_vertex_count
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.
Definition: CPointCloudColoured.cpp:206
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
g
GLubyte g
Definition: glext.h:6279
coord
GLuint coord
Definition: glext.h:7131
uint8_t
unsigned char uint8_t
Definition: rptypes.h:41
glVertex3f
GLAPI void GLAPIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z)
R
const float R
Definition: CKinematicChain.cpp:138
mrpt::opengl::gl_utils::TRenderInfo
Information about the rendering process being issued.
Definition: gl_utils.h:30
p
GLfloat GLfloat p
Definition: glext.h:6305
mrpt::math::TPoint3Df::y
float y
Definition: lightweight_geom_data.h:322
mrpt::opengl::CPointCloudColoured::TPointColour::x
float x
Definition: CPointCloudColoured.h:60
mrpt::opengl::CPointCloudColoured::markAllPointsAsNew
void markAllPointsAsNew()
Do needed internal work if all points are new (octree rebuilt,...)
Definition: CPointCloudColoured.cpp:203
mrpt::serialization::CArchive
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:48
mrpt::opengl::CPointCloudColoured::serializeFrom
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
Definition: CPointCloudColoured.cpp:127
mrpt::opengl::CPointCloudColoured::recolorizeByCoordinate
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,...
Definition: CPointCloudColoured.cpp:253
glEnd
GLAPI void GLAPIENTRY glEnd(void)
r
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
mrpt::img::TColorf::B
float B
Definition: TColor.h:94
ASSERT_BELOW_
#define ASSERT_BELOW_(__A, __B)
Definition: exceptions.h:165
mrpt::opengl::CPointCloudColoured::serializeGetVersion
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
Definition: CPointCloudColoured.cpp:118
mrpt::opengl::CPointCloudColoured::TPointColour::B
float B
Definition: CPointCloudColoured.h:60
mrpt::opengl::CPointCloudColoured::PLY_import_set_vertex
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.
Definition: CPointCloudColoured.cpp:216
mrpt::opengl::gl_utils::checkOpenGLError
void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:143
GL_LIGHTING
#define GL_LIGHTING
Definition: glew.h:385
mrpt::round
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:23
GL_ONE_MINUS_SRC_ALPHA
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:287
mrpt::opengl::CPointCloudColoured::TPointColour::y
float y
Definition: CPointCloudColoured.h:60
b
GLubyte GLubyte b
Definition: glext.h:6279
mrpt::img::TColorf
A RGB color - floats in the range [0,1].
Definition: TColor.h:79
round.h
GL_POINTS
#define GL_POINTS
Definition: glew.h:272
glPointSize
GLAPI void GLAPIENTRY glPointSize(GLfloat size)
mrpt::global_settings::OCTREE_RENDER_MAX_DENSITY_POINTS_PER_SQPIXEL
void OCTREE_RENDER_MAX_DENSITY_POINTS_PER_SQPIXEL(float value)
Default value = 0.01 points/px^2.
Definition: CPointCloud.cpp:32
mrpt::opengl::CPointCloudColoured::TPointColour::R
float R
Definition: CPointCloudColoured.h:60
IMPLEMENTS_SERIALIZABLE
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
Definition: CSerializable.h:114
mrpt::opengl::operator<<
mrpt::serialization::CArchive & operator<<(mrpt::serialization::CArchive &out, const mrpt::opengl::CLight &o)
Definition: CLight.cpp:130
ASSERT_ABOVEEQ_
#define ASSERT_ABOVEEQ_(__A, __B)
Definition: exceptions.h:183
opengl-precomp.h
mrpt::opengl::gl_utils::getCurrentRenderingInfo
void getCurrentRenderingInfo(TRenderInfo &ri)
Gather useful information on the render parameters.
Definition: gl_utils.cpp:216
mrpt::img::TColorf::G
float G
Definition: TColor.h:94
min
#define min(a, b)
Definition: rplidar_driver.cpp:42
mrpt::math::TPoint3Df::x
float x
Definition: lightweight_geom_data.h:321
opengl_internals.h
void
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
z
GLdouble GLdouble z
Definition: glext.h:3872
in
GLuint in
Definition: glext.h:7274
CArchive.h
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:90
mrpt::opengl::CPointCloudColoured::serializeTo
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Definition: CPointCloudColoured.cpp:119
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
size
GLsizeiptr size
Definition: glext.h:3923
y
GLenum GLint GLint y
Definition: glext.h:3538
mrpt::opengl::CPointCloudColoured::setPoint
void setPoint(size_t i, const TPointColour &p)
Write an individual point (checks for "i" in the valid range only in Debug).
Definition: CPointCloudColoured.cpp:179
mrpt::opengl::CPointCloudColoured::TPointColour::G
float G
Definition: CPointCloudColoured.h:60
mrpt::opengl::CPointCloudColoured
A cloud of points, each one with an individual colour (R,G,B).
Definition: CPointCloudColoured.h:44
uint32_t
unsigned __int32 uint32_t
Definition: rptypes.h:47
x
GLenum GLint x
Definition: glext.h:3538
glBlendFunc
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST