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 }
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:30
Global variables to change the run-time behaviour of some MRPT classes within mrpt-base.
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
size_t size(const MATRIXLIKE &m, const int dim)
mrpt::math::TPoint3Df pt
Definition: TPoint3D.h:332
#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
void notifyChange() const
Call to enable calling renderUpdateBuffers() before the next render() rendering iteration.
A cloud of points, each one with an individual colour (R,G,B).
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:48
STL namespace.
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
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.
float d2f(const double d)
shortcut for static_cast<float>(double)
void params_deserialize(mrpt::serialization::CArchive &in)
This base provides a set of functions for maths stuff.
void OCTREE_RENDER_MAX_DENSITY_POINTS_PER_SQPIXEL(float value)
Default value = 0.01 points/px^2.
Definition: CPointCloud.cpp:33
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 ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:108
XYZ point (float) + RGBA(u8)
Definition: TPoint3D.h:330
#define ASSERT_ABOVEEQ_(__A, __B)
Definition: exceptions.h:167
void params_serialize(mrpt::serialization::CArchive &out) const
T x
X,Y,Z coordinates.
Definition: TPoint3D.h:29
uint8_t f2u8(const float f)
converts a float [0,1] into an uint8_t [0,255] (without checking for out of bounds) ...
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
const float R
mrpt::vision::TStereoCalibResults out
An RGBA color - floats in the range [0,1].
Definition: TColor.h:88
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)
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
void setPoint(size_t i, const mrpt::math::TPointXYZfRGBAu8 &p)
Write an individual point (checks for "i" in the valid range only in Debug).
TColor asTColor() const
Returns the 0-255 integer version of this color: RGBA_u8.
Definition: TColor.h:101
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.
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)
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:24



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020