MRPT  2.0.4
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-2020, 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 <mrpt/opengl/opengl_api.h>
19 
20 using namespace mrpt;
21 using namespace mrpt::opengl;
22 using namespace mrpt::math;
23 using namespace std;
25 
27 
28 void CPointCloudColoured::onUpdateBuffers_Points()
29 {
30  octree_assure_uptodate(); // Rebuild octree if needed
31  m_last_rendered_count_ongoing = 0;
32 
33  {
34  mrpt::math::TPoint3Df tst[2];
35  // was static_assert(), error in gcc9.1, cannot use ptr+3 in constexpr.
36  ASSERTMSG_(
37  &tst[1].x == (&tst[0].x + 3), "memory layout not as expected");
38  ASSERTMSG_(
39  &tst[1].y == (&tst[0].y + 3), "memory layout not as expected");
40  ASSERTMSG_(
41  &tst[1].z == (&tst[0].z + 3), "memory layout not as expected");
42  }
43 
44  // const auto N = m_points.size();
45 
46  octree_assure_uptodate(); // Rebuild octree if needed
47  m_last_rendered_count_ongoing = 0;
48 
49  // TODO: Restore rendering using octrees?
50  // octree_render(*rc.state); // Render all points recursively:
51 
52  // ------------------------------
53  // Fill the shader buffers
54  // ------------------------------
55  // "CRenderizableShaderPoints::m_vertex_buffer_data" is already done, since
56  // "m_points" is an alias for it.
57 
58  // color buffer: idem. "m_point_colors" is an alias for
59  // CRenderizableShaderPoints::m_color_buffer_data.
60 
61  m_last_rendered_count = m_last_rendered_count_ongoing;
62 }
63 
64 /** Render a subset of points (required by octree renderer) */
66  [[maybe_unused]] const bool all,
67  [[maybe_unused]] const std::vector<size_t>& idxs,
68  [[maybe_unused]] const float render_area_sqpixels) const
69 {
70 #if 0 && MRPT_HAS_OPENGL_GLUT
71  // Disabled for now... (Feb 2020)
72  const size_t N = all ? m_points.size() : idxs.size();
73  const size_t decimation = mrpt::round(std::max(
74  1.0f, d2f(N / (mrpt::global_settings::
76  render_area_sqpixels))));
77 
78  m_last_rendered_count_ongoing += N / decimation;
79 
80  m_last_rendered_count_ongoing +=
81  (all ? m_points.size() : idxs.size()) / decimation;
82 
83  if (all)
84  {
85  for (size_t i = 0; i < N; i += decimation)
86  {
87  const TPointColour& p = m_points[i];
88  glColor4ub(p.r, p.g, p.b, m_color.A);
89  glVertex3f(p.pt.x, p.pt.y, p.pt.z);
90  }
91  }
92  else
93  {
94  for (size_t i = 0; i < N; i += decimation)
95  {
96  const TPointColour& p = m_points[idxs[i]];
97  glColor4ub(p.r, p.g, p.b, m_color.A);
98  glVertex3f(p.pt.x, p.pt.y, p.pt.z);
99  }
100  }
101 #endif
102 }
103 
104 uint8_t CPointCloudColoured::serializeGetVersion() const { return 4; }
106 {
107  writeToStreamRender(out);
108  out << m_points << m_point_colors;
110 }
111 
113  mrpt::serialization::CArchive& in, uint8_t version)
114 {
115  switch (version)
116  {
117  case 0:
118  case 1:
119  case 2:
120  case 3:
121  {
123  "Binary backward compatibility lost for this class.");
124  }
125  break;
126  case 4:
127  {
128  readFromStreamRender(in);
129  in >> m_points >> m_point_colors;
130 
132  }
133  break;
134  default:
136  };
137  markAllPointsAsNew();
139 }
140 
141 /** Write an individual point (checks for "i" in the valid range only in Debug).
142  */
144 {
145 #ifdef _DEBUG
146  ASSERT_BELOW_(i, size());
147 #endif
148  m_points[i] = p.pt;
149  auto& c = m_point_colors[i];
150  c.R = p.r;
151  c.G = p.g;
152  c.B = p.b;
153  c.A = p.a;
154 
155  // JL: TODO note: Well, this can be clearly done much more efficiently
156  // but...I don't have time! :-(
157  markAllPointsAsNew();
159 }
160 
161 /** Inserts a new point into the point cloud. */
163  float x, float y, float z, float R, float G, float B, float A)
164 {
165  m_points.emplace_back(x, y, z);
166  m_point_colors.emplace_back(f2u8(R), f2u8(G), f2u8(B), f2u8(A));
167 
168  // JL: TODO note: Well, this can be clearly done much more efficiently
169  // but...I don't have time! :-(
170  markAllPointsAsNew();
172 }
173 
174 // Do needed internal work if all points are new (octree rebuilt,...)
175 void CPointCloudColoured::markAllPointsAsNew() { octree_mark_as_outdated(); }
176 /** In a base class, reserve memory to prepare subsequent calls to
177  * PLY_import_set_vertex */
179 {
180  this->resize(N);
181 }
182 
183 /** In a base class, will be called after PLY_import_set_vertex_count() once for
184  * each loaded point.
185  * \param pt_color Will be nullptr if the loaded file does not provide color
186  * info.
187  */
189  const size_t idx, const mrpt::math::TPoint3Df& pt,
190  const mrpt::img::TColorf* pt_color)
191 {
192  if (!pt_color)
193  this->setPoint(
194  idx, TPointXYZfRGBAu8(pt.x, pt.y, pt.z, 0xff, 0xff, 0xff));
195  else
196  this->setPoint(
197  idx, TPointXYZfRGBAu8(
198  pt.x, pt.y, pt.z, f2u8(pt_color->R), f2u8(pt_color->G),
199  f2u8(pt_color->B)));
200 }
201 
202 /** In a base class, return the number of vertices */
204 {
205  return this->size();
206 }
207 
208 /** In a base class, will be called after PLY_export_get_vertex_count() once for
209  * each exported point.
210  * \param pt_color Will be nullptr if the loaded file does not provide color
211  * info.
212  */
214  const size_t idx, mrpt::math::TPoint3Df& pt, bool& pt_has_color,
215  mrpt::img::TColorf& pt_color) const
216 {
217  auto& p = m_points[idx];
218  auto& p_color = m_point_colors[idx];
219  p = pt;
220  p_color = pt_color.asTColor();
221  pt_has_color = true;
222 }
223 
225  const float coord_min, const float coord_max, const int coord_index,
226  const mrpt::img::TColormap color_map)
227 {
228  ASSERT_ABOVEEQ_(coord_index, 0);
229  ASSERT_BELOW_(coord_index, 3);
230 
231  const float coord_range = coord_max - coord_min;
232  const float coord_range_1 = coord_range != 0.0f ? 1.0f / coord_range : 1.0f;
233  for (size_t i = 0; i < m_points.size(); i++)
234  {
235  float coord = .0f;
236  switch (coord_index)
237  {
238  case 0:
239  coord = m_points[i].x;
240  break;
241  case 1:
242  coord = m_points[i].y;
243  break;
244  case 2:
245  coord = m_points[i].z;
246  break;
247  };
248  const float col_idx =
249  std::max(0.0f, std::min(1.0f, (coord - coord_min) * coord_range_1));
250  float r, g, b;
251  mrpt::img::colormap(color_map, col_idx, r, g, b);
252  this->setPointColor_fast(i, r, g, b);
253  }
254 }
ops_containers.h
opengl_api.h
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:203
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:30
mrpt::opengl::CRenderizable::notifyChange
void notifyChange() const
Call to enable calling renderUpdateBuffers() before the next render() rendering iteration.
Definition: CRenderizable.h:315
mrpt::math::TPoint3D_< float >
G
const double G
Definition: vision_stereo_rectify/test.cpp:32
mrpt::math::TPointXYZfRGBAu8::b
uint8_t b
Definition: TPoint3D.h:333
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:114
mrpt::opengl::CRenderizable
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:48
resize
images resize(NUM_IMGS)
mrpt::math::TPoint3D_data::z
T z
Definition: TPoint3D.h:29
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:65
CPointCloudColoured.h
mrpt::f2u8
uint8_t f2u8(const float f)
converts a float [0,1] into an uint8_t [0,255] (without checking for out of bounds)
Definition: core/include/mrpt/core/bits_math.h:193
mrpt::img::TColorf::R
float R
Definition: TColor.h:107
mrpt::img::TColorf::asTColor
TColor asTColor() const
Returns the 0-255 integer version of this color: RGBA_u8
Definition: TColor.h:101
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:213
stl_serialization.h
out
mrpt::vision::TStereoCalibResults out
Definition: chessboard_stereo_camera_calib_unittest.cpp:25
mrpt::opengl::CPointCloudColoured::PLY_import_set_vertex_count
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:178
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: BaseAppDataSource.h:15
R
const float R
Definition: CKinematicChain.cpp:137
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
mrpt::opengl::CPointCloudColoured::markAllPointsAsNew
void markAllPointsAsNew()
Definition: CPointCloudColoured.cpp:175
mrpt::serialization::CArchive
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
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:112
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:224
mrpt::img::TColorf::B
float B
Definition: TColor.h:107
ASSERT_BELOW_
#define ASSERT_BELOW_(__A, __B)
Definition: exceptions.h:149
mrpt::opengl::CPointCloudColoured::serializeGetVersion
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
Definition: CPointCloudColoured.cpp:104
mrpt::opengl::CPointCloudColoured::PLY_import_set_vertex
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:188
mrpt::math::TPointXYZfRGBAu8::pt
mrpt::math::TPoint3Df pt
Definition: TPoint3D.h:332
mrpt::math::TPointXYZfRGBAu8::r
uint8_t r
Definition: TPoint3D.h:333
mrpt::round
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:24
mrpt::opengl::CRenderizableShaderPoints::params_deserialize
void params_deserialize(mrpt::serialization::CArchive &in)
Definition: CRenderizableShaderPoints.cpp:115
mrpt::opengl::CPointCloudColoured::setPoint
void setPoint(size_t i, const mrpt::math::TPointXYZfRGBAu8 &p)
Write an individual point (checks for "i" in the valid range only in Debug).
Definition: CPointCloudColoured.cpp:143
mrpt::img::TColorf
An RGBA color - floats in the range [0,1].
Definition: TColor.h:88
round.h
mrpt::math::size
size_t size(const MATRIXLIKE &m, const int dim)
Definition: math/include/mrpt/math/bits_math.h:21
mrpt::math::TPointXYZfRGBAu8
XYZ point (float) + RGBA(u8)
Definition: TPoint3D.h:330
mrpt::d2f
float d2f(const double d)
shortcut for static_cast<float>(double)
Definition: core/include/mrpt/core/bits_math.h:189
A
Definition: is_defined_unittest.cpp:13
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:33
IMPLEMENTS_SERIALIZABLE
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
Definition: CSerializable.h:166
ASSERTMSG_
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:108
mrpt::math::TPoint3D_data::y
T y
Definition: TPoint3D.h:29
ASSERT_ABOVEEQ_
#define ASSERT_ABOVEEQ_(__A, __B)
Definition: exceptions.h:167
opengl-precomp.h
mrpt::math::TPointXYZfRGBAu8::g
uint8_t g
Definition: TPoint3D.h:333
mrpt::img::TColorf::G
float G
Definition: TColor.h:107
mrpt::opengl::CPointCloudColoured::push_back
void push_back(float x, float y, float z, float R, float G, float B, float A=1)
Inserts a new point into the point cloud.
Definition: CPointCloudColoured.cpp:162
mrpt::math::TPoint3D_data::x
T x
X,Y,Z coordinates.
Definition: TPoint3D.h:29
mrpt::opengl::CRenderizableShaderPoints::params_serialize
void params_serialize(mrpt::serialization::CArchive &out) const
Definition: CRenderizableShaderPoints.cpp:108
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:11
mrpt::math::TPointXYZfRGBAu8::a
uint8_t a
Definition: TPoint3D.h:333
CArchive.h
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
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:105
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
mrpt::opengl::CPointCloudColoured
A cloud of points, each one with an individual colour (R,G,B).
Definition: CPointCloudColoured.h:43



Page generated by Doxygen 1.8.17 for MRPT 2.0.4 at Fri Jul 17 08:43:33 UTC 2020