Main MRPT website > C++ reference for MRPT 1.5.6
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 
12 
14 #include <mrpt/utils/round.h> // round()
15 #include <mrpt/utils/CStream.h>
16 #include <mrpt/math/ops_containers.h> // for << ops
18 
19 #include "opengl_internals.h"
20 
21 using namespace mrpt;
22 using namespace mrpt::opengl;
23 using namespace mrpt::utils;
24 //using namespace mrpt::slam;
25 using namespace mrpt::math;
26 using namespace std;
27 
29 
30 /*---------------------------------------------------------------
31  render
32  ---------------------------------------------------------------*/
33 void CPointCloudColoured::render() const
34 {
35 #if MRPT_HAS_OPENGL_GLUT
36  octree_assure_uptodate(); // Rebuild octree if needed
37  m_last_rendered_count_ongoing = 0;
38 
39  // Info needed by octree renderer:
42 
43  if ( m_color.A != 255 )
44  {
47  }
48 
49  glPointSize( m_pointSize );
50 
51  if (m_pointSmooth)
53  else glDisable( GL_POINT_SMOOTH );
54 
55  // Disable lighting for point clouds:
57 
58  glBegin( GL_POINTS );
59  octree_render(ri); // Render all points recursively:
60  glEnd();
61 
63 
64  // Undo flags:
65  if ( m_color.A != 255 )
67 
68  if (m_pointSmooth)
70 
71  m_last_rendered_count = m_last_rendered_count_ongoing;
72 
74 #endif
75 }
76 
77 /** Render a subset of points (required by octree renderer) */
78 void CPointCloudColoured::render_subset(const bool all, const std::vector<size_t>& idxs, 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( std::max(1.0f, static_cast<float>(N / (mrpt::global_settings::OCTREE_RENDER_MAX_DENSITY_POINTS_PER_SQPIXEL * render_area_sqpixels)) ) );
83 
84  m_last_rendered_count_ongoing += N/decimation;
85 
86  m_last_rendered_count_ongoing += (all ? m_points.size() : idxs.size())/decimation;
87 
88  if (all)
89  {
90  for (size_t i=0;i<N;i+=decimation)
91  {
92  const TPointColour &p = m_points[i];
93  glColor4f( p.R, p.G, p.B,m_color.A*1.0f/255.f );
94  glVertex3f( p.x,p.y,p.z );
95  }
96  }
97  else
98  {
99  for (size_t i=0;i<N;i+=decimation)
100  {
101  const TPointColour &p = m_points[idxs[i]];
102  glColor4f( p.R, p.G, p.B,m_color.A*1.0f/255.f );
103  glVertex3f( p.x,p.y,p.z );
104  }
105  }
106 #else
107  MRPT_UNUSED_PARAM(all); MRPT_UNUSED_PARAM(idxs); MRPT_UNUSED_PARAM(render_area_sqpixels);
108 #endif
109 }
110 
111 
112 /*---------------------------------------------------------------
113  Implements the writing to a CStream capability of
114  CSerializable objects
115  ---------------------------------------------------------------*/
117 {
118 
119  if (version)
120  *version = 2;
121  else
122  {
123  writeToStreamRender(out);
124  out << m_points;
125  out << m_pointSize;
126  out << m_pointSmooth; // Added in v2
127  }
128 }
129 
130 /*---------------------------------------------------------------
131  Implements the reading from a CStream capability of
132  CSerializable objects
133  ---------------------------------------------------------------*/
135 {
136  switch(version)
137  {
138  case 1:
139  case 2:
140  {
141  readFromStreamRender(in);
142  in >> m_points >> m_pointSize;
143 
144  if (version>=2)
145  in >> m_pointSmooth;
146  else m_pointSmooth = false;
147 
148  } break;
149  case 0:
150  {
151  readFromStreamRender(in);
152 
153  // Old vector_serializable:
154  uint32_t n;
155  in >> n;
156  m_points.resize(n);
157  for (uint32_t i=0;i<n;i++) in >> m_points[i];
158 
159  in >> m_pointSize;
160  } break;
161  default:
163  };
164  markAllPointsAsNew();
165 }
166 
168 {
169  in >> o.x >> o.y >> o.z >> o.R >> o.G >> o.B;
170  return in;
171 }
172 
173 
175 {
176  out << o.x << o.y << o.z << o.R << o.G << o.B;
177  return out;
178 }
179 
180 
181 /** Write an individual point (checks for "i" in the valid range only in Debug). */
183 {
184 #ifdef _DEBUG
185  ASSERT_BELOW_(i,size())
186 #endif
187  m_points[i] = p;
188 
189  // JL: TODO note: Well, this can be clearly done much more efficiently but...I don't have time! :-(
190  markAllPointsAsNew();
191 }
192 
193 /** Inserts a new point into the point cloud. */
194 void CPointCloudColoured::push_back(float x,float y,float z, float R, float G, float B)
195 {
196  m_points.push_back(TPointColour(x,y,z,R,G,B));
197 
198  // JL: TODO note: Well, this can be clearly done much more efficiently but...I don't have time! :-(
199  markAllPointsAsNew();
200 }
201 
202 
203 // Do needed internal work if all points are new (octree rebuilt,...)
205 {
206  octree_mark_as_outdated();
207 }
208 
209 /** In a base class, reserve memory to prepare subsequent calls to PLY_import_set_vertex */
211 {
212  this->resize(N);
213 }
214 
215 /** In a base class, will be called after PLY_import_set_vertex_count() once for each loaded point.
216  * \param pt_color Will be NULL if the loaded file does not provide color info.
217  */
219 {
220  if (!pt_color)
221  this->setPoint(idx,TPointColour(pt.x,pt.y,pt.z,1,1,1));
222  else this->setPoint(idx,TPointColour(pt.x,pt.y,pt.z,pt_color->R,pt_color->G,pt_color->B));
223 }
224 
225 /** In a base class, return the number of vertices */
227 {
228  return this->size();
229 }
230 
231 /** In a base class, will be called after PLY_export_get_vertex_count() once for each exported point.
232  * \param pt_color Will be NULL if the loaded file does not provide color info.
233  */
235  const size_t idx,
237  bool &pt_has_color,
238  mrpt::utils::TColorf &pt_color) const
239 {
240  const TPointColour &p = m_points[idx];
241  pt.x = p.x;
242  pt.y = p.y;
243  pt.z = p.z;
244  pt_color.R = p.R;
245  pt_color.G = p.G;
246  pt_color.B = p.B;
247  pt_has_color=true;
248 }
249 
250 void CPointCloudColoured::recolorizeByCoordinate(const float coord_min, const float coord_max, const int coord_index, const mrpt::utils::TColormap color_map)
251 {
252  ASSERT_ABOVEEQ_(coord_index,0);
253  ASSERT_BELOW_(coord_index,3);
254 
255  const float coord_range = coord_max-coord_min;
256  const float coord_range_1 = coord_range!=0.0f ? 1.0f/coord_range : 1.0f;
257  for (size_t i=0;i<m_points.size();i++)
258  {
259  float coord =.0f;
260  switch (coord_index) {
261  case 0: coord = m_points[i].x; break;
262  case 1: coord = m_points[i].y; break;
263  case 2: coord = m_points[i].z; break;
264  };
265  const float col_idx = std::max(0.0f, std::min(1.0f,(coord-coord_min)*coord_range_1 ) );
266  float r,g,b;
267  mrpt::utils::colormap(color_map, col_idx,r,g,b);
268  this->setPointColor_fast(i,r,g,b);
269  }
270 }
271 
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
GLdouble GLdouble z
Definition: glext.h:3734
#define min(a, b)
OPENGL_IMPEXP mrpt::utils::CStream & operator<<(mrpt::utils::CStream &out, const mrpt::opengl::CLight &o)
Definition: CLight.cpp:132
GLAPI void GLAPIENTRY glEnable(GLenum cap)
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const
Introduces a pure virtual method responsible for writing to a CStream.
GLAPI void GLAPIENTRY glPointSize(GLfloat size)
TColormap
Different colormaps for use in mrpt::utils::colormap()
Definition: color_maps.h:30
::mrpt::utils::CStream & operator>>(mrpt::utils::CStream &in, CAngularObservationMeshPtr &pObj)
#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:4618
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:44
STL namespace.
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:283
void BASE_IMPEXP 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:101
#define GL_LIGHTING
Definition: glew.h:381
GLuint coord
Definition: glext.h:6196
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:38
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
size_t PLY_export_get_vertex_count() const MRPT_OVERRIDE
In a base class, return the number of vertices.
virtual void PLY_import_set_vertex_count(const size_t N) MRPT_OVERRIDE
In a base class, reserve memory to prepare subsequent calls to PLY_import_set_vertex.
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.
OPENGL_IMPEXP float OCTREE_RENDER_MAX_DENSITY_POINTS_PER_SQPIXEL
Default value = 0.01 points/px^2.
Definition: CPointCloud.cpp:26
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.
Information about the rendering process being issued.
Definition: gl_utils.h:35
GLubyte g
Definition: glext.h:5575
GLubyte GLubyte b
Definition: glext.h:5575
#define GL_POINT_SMOOTH
Definition: glew.h:359
int version
Definition: mrpt_jpeglib.h:898
GLAPI void GLAPIENTRY glBegin(GLenum mode)
#define GL_BLEND
Definition: glew.h:428
#define GL_POINTS
Definition: glew.h:268
void readFromStream(mrpt::utils::CStream &in, int version)
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
GLAPI void GLAPIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z)
#define GL_SRC_ALPHA
Definition: glew.h:282
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:3618
void PLY_export_get_vertex(const size_t idx, mrpt::math::TPoint3Df &pt, bool &pt_has_color, mrpt::utils::TColorf &pt_color) const MRPT_OVERRIDE
In a base class, will be called after PLY_export_get_vertex_count() once for each exported point...
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 OPENGL_IMPEXP checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:134
virtual void PLY_import_set_vertex(const size_t idx, const mrpt::math::TPoint3Df &pt, const mrpt::utils::TColorf *pt_color=NULL) MRPT_OVERRIDE
In a base class, will be called after PLY_import_set_vertex_count() once for each loaded 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:6301
The namespace for 3D scene representation and rendering.
void OPENGL_IMPEXP getCurrentRenderingInfo(TRenderInfo &ri)
Gather useful information on the render parameters.
Definition: gl_utils.cpp:197
A RGB color - floats in the range [0,1].
Definition: TColor.h:80
GLAPI void GLAPIENTRY glEnd(void)
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:26
GLenum GLint GLint y
Definition: glext.h:3516
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
GLAPI void GLAPIENTRY glColor4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
GLsizeiptr size
Definition: glext.h:3779
GLenum GLint x
Definition: glext.h:3516
unsigned __int32 uint32_t
Definition: rptypes.h:49
GLAPI void GLAPIENTRY glDisable(GLenum cap)
GLfloat GLfloat p
Definition: glext.h:5587
void markAllPointsAsNew()
Do needed internal work if all points are new (octree rebuilt,...)



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019