MRPT  1.9.9
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  const bool all, const std::vector<size_t>& idxs,
67  const float render_area_sqpixels) const
68 {
69 #if 0 && MRPT_HAS_OPENGL_GLUT
70  // Disabled for now... (Feb 2020)
71  const size_t N = all ? m_points.size() : idxs.size();
72  const size_t decimation = mrpt::round(std::max(
73  1.0f, d2f(N / (mrpt::global_settings::
75  render_area_sqpixels))));
76 
77  m_last_rendered_count_ongoing += N / decimation;
78 
79  m_last_rendered_count_ongoing +=
80  (all ? m_points.size() : idxs.size()) / decimation;
81 
82  if (all)
83  {
84  for (size_t i = 0; i < N; i += decimation)
85  {
86  const TPointColour& p = m_points[i];
87  glColor4ub(p.r, p.g, p.b, m_color.A);
88  glVertex3f(p.pt.x, p.pt.y, p.pt.z);
89  }
90  }
91  else
92  {
93  for (size_t i = 0; i < N; i += decimation)
94  {
95  const TPointColour& p = m_points[idxs[i]];
96  glColor4ub(p.r, p.g, p.b, m_color.A);
97  glVertex3f(p.pt.x, p.pt.y, p.pt.z);
98  }
99  }
100 #else
101  MRPT_UNUSED_PARAM(all);
102  MRPT_UNUSED_PARAM(idxs);
103  MRPT_UNUSED_PARAM(render_area_sqpixels);
104 #endif
105 }
106 
107 uint8_t CPointCloudColoured::serializeGetVersion() const { return 4; }
109 {
110  writeToStreamRender(out);
111  out << m_points << m_point_colors;
113 }
114 
116  mrpt::serialization::CArchive& in, uint8_t version)
117 {
118  switch (version)
119  {
120  case 0:
121  case 1:
122  case 2:
123  case 3:
124  {
126  "Binary backward compatibility lost for this class.");
127  }
128  break;
129  case 4:
130  {
131  readFromStreamRender(in);
132  in >> m_points >> m_point_colors;
133 
135  }
136  break;
137  default:
139  };
140  markAllPointsAsNew();
142 }
143 
144 /** Write an individual point (checks for "i" in the valid range only in Debug).
145  */
147 {
148 #ifdef _DEBUG
149  ASSERT_BELOW_(i, size());
150 #endif
151  m_points[i] = p.pt;
152  auto& c = m_point_colors[i];
153  c.R = p.r;
154  c.G = p.g;
155  c.B = p.b;
156  c.A = p.a;
157 
158  // JL: TODO note: Well, this can be clearly done much more efficiently
159  // but...I don't have time! :-(
160  markAllPointsAsNew();
162 }
163 
164 /** Inserts a new point into the point cloud. */
166  float x, float y, float z, float R, float G, float B, float A)
167 {
168  m_points.emplace_back(x, y, z);
169  m_point_colors.emplace_back(f2u8(R), f2u8(G), f2u8(B), f2u8(A));
170 
171  // JL: TODO note: Well, this can be clearly done much more efficiently
172  // but...I don't have time! :-(
173  markAllPointsAsNew();
175 }
176 
177 // Do needed internal work if all points are new (octree rebuilt,...)
178 void CPointCloudColoured::markAllPointsAsNew() { octree_mark_as_outdated(); }
179 /** In a base class, reserve memory to prepare subsequent calls to
180  * PLY_import_set_vertex */
182 {
183  this->resize(N);
184 }
185 
186 /** In a base class, will be called after PLY_import_set_vertex_count() once for
187  * each loaded point.
188  * \param pt_color Will be nullptr if the loaded file does not provide color
189  * info.
190  */
192  const size_t idx, const mrpt::math::TPoint3Df& pt,
193  const mrpt::img::TColorf* pt_color)
194 {
195  if (!pt_color)
196  this->setPoint(
197  idx, TPointXYZfRGBAu8(pt.x, pt.y, pt.z, 0xff, 0xff, 0xff));
198  else
199  this->setPoint(
200  idx, TPointXYZfRGBAu8(
201  pt.x, pt.y, pt.z, f2u8(pt_color->R), f2u8(pt_color->G),
202  f2u8(pt_color->B)));
203 }
204 
205 /** In a base class, return the number of vertices */
207 {
208  return this->size();
209 }
210 
211 /** In a base class, will be called after PLY_export_get_vertex_count() once for
212  * each exported point.
213  * \param pt_color Will be nullptr if the loaded file does not provide color
214  * info.
215  */
217  const size_t idx, mrpt::math::TPoint3Df& pt, bool& pt_has_color,
218  mrpt::img::TColorf& pt_color) const
219 {
220  auto& p = m_points[idx];
221  auto& p_color = m_point_colors[idx];
222  p = pt;
223  p_color = pt_color.asTColor();
224  pt_has_color = true;
225 }
226 
228  const float coord_min, const float coord_max, const int coord_index,
229  const mrpt::img::TColormap color_map)
230 {
231  ASSERT_ABOVEEQ_(coord_index, 0);
232  ASSERT_BELOW_(coord_index, 3);
233 
234  const float coord_range = coord_max - coord_min;
235  const float coord_range_1 = coord_range != 0.0f ? 1.0f / coord_range : 1.0f;
236  for (size_t i = 0; i < m_points.size(); i++)
237  {
238  float coord = .0f;
239  switch (coord_index)
240  {
241  case 0:
242  coord = m_points[i].x;
243  break;
244  case 1:
245  coord = m_points[i].y;
246  break;
247  case 2:
248  coord = m_points[i].z;
249  break;
250  };
251  const float col_idx =
252  std::max(0.0f, std::min(1.0f, (coord - coord_min) * coord_range_1));
253  float r, g, b;
254  mrpt::img::colormap(color_map, col_idx, r, g, b);
255  this->setPointColor_fast(i, r, g, b);
256  }
257 }
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.
#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:24



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 3a26b90fd Wed Mar 25 20:17:03 2020 +0100 at miƩ mar 25 23:05:41 CET 2020