Debian Codegraph @

libs/opengl/src/CPointCloud.cpp

raw
1 /* +------------------------------------------------------------------------+
2 | Mobile Robot Programming Toolkit (MRPT) |
3 | https://www.mrpt.org/ |
4 | |
5 | Copyright (c) 2005-2026, 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/containers/yaml.h>
13 #include <mrpt/core/round.h> // round()
14 #include <mrpt/math/ops_containers.h>
15 #include <mrpt/opengl/CPointCloud.h>
16 #include <mrpt/opengl/opengl_api.h>
17 #include <mrpt/serialization/CArchive.h>
18 #include <mrpt/serialization/CSchemeArchiveBase.h>
19
20 using namespace mrpt;
21 using namespace mrpt::opengl;
22 using namespace mrpt::img;
23 using namespace mrpt::math;
24 using namespace std;
25
26 float OCTREE_RENDER_MAX_DENSITY_POINTS_PER_SQPIXEL_value = 0.10f;
27 size_t OCTREE_RENDER_MAX_POINTS_PER_NODE_value = 1e6;
28
29 float mrpt::global_settings::OCTREE_RENDER_MAX_DENSITY_POINTS_PER_SQPIXEL()
30 {
31 return OCTREE_RENDER_MAX_DENSITY_POINTS_PER_SQPIXEL_value;
32 }
33 void mrpt::global_settings::OCTREE_RENDER_MAX_DENSITY_POINTS_PER_SQPIXEL(float value)
34 {
35 OCTREE_RENDER_MAX_DENSITY_POINTS_PER_SQPIXEL_value = value;
36 }
37
38 size_t mrpt::global_settings::OCTREE_RENDER_MAX_POINTS_PER_NODE()
39 {
40 return OCTREE_RENDER_MAX_POINTS_PER_NODE_value;
41 }
42 void mrpt::global_settings::OCTREE_RENDER_MAX_POINTS_PER_NODE(size_t value)
43 {
44 OCTREE_RENDER_MAX_POINTS_PER_NODE_value = value;
45 }
46
47 IMPLEMENTS_SERIALIZABLE(CPointCloud, CRenderizable, mrpt::opengl)
48
49 CPointCloud::CPointCloud() { markAllPointsAsNew(); }
50
51 void CPointCloud::onUpdateBuffers_Points()
52 {
53 std::unique_lock<std::shared_mutex> wfWriteLock(CRenderizableShaderPoints::m_pointsMtx.data);
54
55 {
56 mrpt::math::TPoint3Df tst[2];
57 // was static_assert(), error in gcc9.1, cannot use ptr+3 in constexpr.
58 ASSERTMSG_(&tst[1].x == (&tst[0].x + 3), "memory layout not as expected");
59 ASSERTMSG_(&tst[1].y == (&tst[0].y + 3), "memory layout not as expected");
60 ASSERTMSG_(&tst[1].z == (&tst[0].z + 3), "memory layout not as expected");
61 }
62
63 const auto N = m_points.size();
64
65 octree_assure_uptodate(); // Rebuild octree if needed
66 m_last_rendered_count_ongoing = 0;
67
68 if (m_colorFromDepth != colNone)
69 {
70 if (!m_minmax_valid)
71 {
72 m_minmax_valid = true;
73 if (!m_points.empty())
74 {
75 const float* vs =
76 m_colorFromDepth == CPointCloud::colZ
77 ? &m_points[0].z
78 : (m_colorFromDepth == CPointCloud::colY ? &m_points[0].y : &m_points[0].x);
79 m_min = m_max = vs[0];
80 for (size_t i = 1; i < N; i++)
81 {
82 float v = vs[3 * i];
83 if (v < m_min) m_min = v;
84 if (v > m_max) m_max = v;
85 }
86 }
87 else
88 m_max = m_min = 0;
89 }
90
91 m_max_m_min = m_max - m_min;
92 if (std::abs(m_max_m_min) < 1e-4)
93 m_max_m_min = -1;
94 else
95 m_min = m_max - m_max_m_min * 1.01f;
96 m_max_m_min_inv = 1.0f / m_max_m_min;
97 }
98
99 // Slopes of color interpolation:
100 m_col_slop.R = m_colorFromDepth_max.R - m_colorFromDepth_min.R;
101 m_col_slop.G = m_colorFromDepth_max.G - m_colorFromDepth_min.G;
102 m_col_slop.B = m_colorFromDepth_max.B - m_colorFromDepth_min.B;
103
104 m_col_slop_inv.R = m_col_slop.R != 0 ? 1.0f / m_col_slop.R : 0;
105 m_col_slop_inv.G = m_col_slop.G != 0 ? 1.0f / m_col_slop.G : 0;
106 m_col_slop_inv.B = m_col_slop.B != 0 ? 1.0f / m_col_slop.B : 0;
107
108 // TODO: Restore rendering using octrees?
109 // octree_render(*rc.state); // Render all points recursively:
110
111 // ------------------------------
112 // Fill the shader buffers
113 // ------------------------------
114 // "CRenderizableShaderPoints::m_vertex_buffer_data" is already done, since
115 // "m_points" is an alias for it.
116
117 const auto myColor = getColor_u8();
118
119 // color buffer:
120 auto& cbd = CRenderizableShaderPoints::m_color_buffer_data;
121 cbd.clear();
122 cbd.reserve(N);
123
124 // color for each point:
125 if (m_colorFromDepth != colNone && m_max_m_min > 0)
126 {
127 for (size_t i = 0; i < N; i++)
128 {
129 const float depthCol =
130 (m_colorFromDepth == colX ? m_points[i].x
131 : (m_colorFromDepth == colY ? m_points[i].y : m_points[i].z));
132
133 float f = (depthCol - m_min) * m_max_m_min_inv;
134 f = std::max(0.0f, min(1.0f, f));
135
136 cbd.push_back(
137 {f2u8(m_colorFromDepth_min.R + f * m_col_slop_inv.R),
138 f2u8(m_colorFromDepth_min.G + f * m_col_slop_inv.G),
139 f2u8(m_colorFromDepth_min.B + f * m_col_slop_inv.B), myColor.A});
140 }
141 }
142 else
143 {
144 // all points: same color
145 cbd.assign(N, myColor);
146 }
147
148 m_last_rendered_count = m_last_rendered_count_ongoing;
149 }
150
151 inline void CPointCloud::internal_render_one_point([[maybe_unused]] size_t i) const
152 {
153 #if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL
154 #endif
155 }
156
157 /** Render a subset of points (required by octree renderer) */
158 void CPointCloud::render_subset(
159 [[maybe_unused]] const bool all,
160 [[maybe_unused]] const std::vector<size_t>& idxs,
161 [[maybe_unused]] const float render_area_sqpixels) const
162 {
163 #if 0 && MRPT_HAS_OPENGL_GLUT
164 // Disabled for now... (Feb 2020)
165
166 const size_t N = (all ? m_xs.size() : idxs.size());
167 const size_t decimation = mrpt::round(std::max(
168 1.0f, d2f(
169 N / (OCTREE_RENDER_MAX_DENSITY_POINTS_PER_SQPIXEL_value *
170 render_area_sqpixels))));
171
172 m_last_rendered_count_ongoing += N / decimation;
173
174 if (all)
175 {
176 for (size_t i = 0; i < N; i++) internal_render_one_point(i);
177 }
178 else
179 {
180 const size_t Np = idxs.size();
181 for (size_t i = 0; i < Np; i += decimation)
182 internal_render_one_point(idxs[i]);
183 }
184 #endif
185 }
186 void CPointCloud::serializeTo(mrpt::serialization::CSchemeArchiveBase& out) const
187 {
188 SCHEMA_SERIALIZE_DATATYPE_VERSION(1);
189 out["colorFromDepth"] = static_cast<int32_t>(m_colorFromDepth);
190 out["pointSize"] = m_pointSize;
191 const auto N = m_points.size();
192 out["N"] = static_cast<uint64_t>(N);
193 for (size_t i = 0; i < N; i++)
194 {
195 out["xs"][i] = m_points[i].x;
196 out["ys"][i] = m_points[i].y;
197 out["zs"][i] = m_points[i].z;
198 }
199 out["colorFromDepth_min"]["R"] = m_colorFromDepth_min.R;
200 out["colorFromDepth_min"]["G"] = m_colorFromDepth_min.G;
201 out["colorFromDepth_min"]["B"] = m_colorFromDepth_min.B;
202 out["colorFromDepth_max"]["R"] = m_colorFromDepth_max.R;
203 out["colorFromDepth_max"]["G"] = m_colorFromDepth_max.G;
204 out["colorFromDepth_max"]["B"] = m_colorFromDepth_max.B;
205 out["pointSmooth"] = m_pointSmooth;
206 }
207 void CPointCloud::serializeFrom(mrpt::serialization::CSchemeArchiveBase& in)
208 {
209 uint8_t version;
210 SCHEMA_DESERIALIZE_DATATYPE_VERSION();
211 switch (version)
212 {
213 case 1:
214 {
215 /**
216 * currently below is being left to what is being set by
217 * the default constructor i.e.,CPointCloud::colNone
218 */
219 // m_colorFromDepth = static_cast<float>(in["colorDepth"]);
220 m_pointSize = static_cast<float>(in["pointSize"]);
221 const size_t N = static_cast<uint64_t>(in["N"]);
222 m_points.resize(N);
223 for (size_t i = 0; i < N; i++)
224 {
225 m_points[i].x = static_cast<float>(in["xs"][i]);
226 m_points[i].y = static_cast<float>(in["ys"][i]);
227 m_points[i].z = static_cast<float>(in["zs"][i]);
228 }
229 m_colorFromDepth_min.R = static_cast<float>(in["colorFromDepth_min"]["R"]);
230 m_colorFromDepth_min.G = static_cast<float>(in["colorFromDepth_min"]["G"]);
231 m_colorFromDepth_min.B = static_cast<float>(in["colorFromDepth_min"]["B"]);
232 m_colorFromDepth_max.R = static_cast<float>(in["colorFromDepth_max"]["R"]);
233 m_colorFromDepth_max.G = static_cast<float>(in["colorFromDepth_max"]["G"]);
234 m_colorFromDepth_max.B = static_cast<float>(in["colorFromDepth_max"]["B"]);
235 m_pointSmooth = static_cast<bool>(in["pointSmooth"]);
236 }
237 break;
238 default:
239 MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version);
240 }
241 }
242 uint8_t CPointCloud::serializeGetVersion() const { return 6; }
243 void CPointCloud::serializeTo(mrpt::serialization::CArchive& out) const
244 {
245 std::shared_lock<std::shared_mutex> wfReadLock(CRenderizableShaderPoints::m_pointsMtx.data);
246
247 writeToStreamRender(out);
248 // Changed from bool to enum/int32_t in version 3.
249 out.WriteAs<int32_t>(m_colorFromDepth);
250
251 // out << m_xs << m_ys << m_zs;// was: v4
252 out.WriteAs<uint32_t>(m_points.size());
253 for (const auto& pt : m_points) out << pt;
254
255 // New in version 2:
256 out << m_colorFromDepth_min.R << m_colorFromDepth_min.G << m_colorFromDepth_min.B;
257 out << m_colorFromDepth_max.R << m_colorFromDepth_max.G << m_colorFromDepth_max.B;
258
259 // New in version 4:
260 out << m_pointSmooth;
261
262 // new v6:
263 CRenderizableShaderPoints::params_serialize(out);
264 }
265
266 void CPointCloud::serializeFrom(mrpt::serialization::CArchive& in, uint8_t version)
267 {
268 std::unique_lock<std::shared_mutex> wfWriteLock(CRenderizableShaderPoints::m_pointsMtx.data);
269
270 switch (version)
271 {
272 case 0:
273 case 1:
274 case 2:
275 case 3:
276 case 4:
277 case 5:
278 case 6:
279 {
280 readFromStreamRender(in);
281 if (version >= 3)
282 {
283 int32_t axis;
284 in >> axis;
285 m_colorFromDepth = Axis(axis);
286 }
287 else
288 {
289 bool colorFromZ;
290 in >> colorFromZ;
291 m_colorFromDepth = colorFromZ ? CPointCloud::colZ : CPointCloud::colNone;
292 }
293 if (version < 5)
294 {
295 std::vector<float> xs, ys, zs;
296 in >> xs >> ys >> zs;
297 wfWriteLock.unlock(); // avoid recursive lock
298 this->setAllPoints(xs, ys, zs);
299 wfWriteLock.lock();
300 }
301 else
302 {
303 // New in v5:
304 auto N = in.ReadAs<uint32_t>();
305 m_points.resize(N);
306 for (auto& pt : m_points) in >> pt;
307 }
308
309 if (version >= 1 && version < 6)
310 in >> m_pointSize;
311 else
312 m_pointSize = 1;
313
314 if (version >= 2)
315 {
316 in >> m_colorFromDepth_min.R >> m_colorFromDepth_min.G >> m_colorFromDepth_min.B;
317 in >> m_colorFromDepth_max.R >> m_colorFromDepth_max.G >> m_colorFromDepth_max.B;
318 }
319 else
320 {
321 m_colorFromDepth_min = TColorf(0, 0, 0);
322 m_colorFromDepth_max = getColor();
323 }
324
325 if (version >= 4)
326 in >> m_pointSmooth;
327 else
328 m_pointSmooth = false;
329
330 if (version >= 6) CRenderizableShaderPoints::params_deserialize(in);
331 }
332 break;
333 default:
334 MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version);
335 };
336
337 wfWriteLock.unlock();
338 markAllPointsAsNew();
339 CRenderizable::notifyChange();
340 }
341
342 void CPointCloud::clear()
343 {
344 {
345 std::unique_lock<std::shared_mutex> wfWriteLock(CRenderizableShaderPoints::m_pointsMtx.data);
346
347 m_points.clear();
348 }
349
350 markAllPointsAsNew();
351 CRenderizable::notifyChange();
352 }
353
354 void CPointCloud::insertPoint(float x, float y, float z)
355 {
356 std::unique_lock<std::shared_mutex> wfWriteLock(CRenderizableShaderPoints::m_pointsMtx.data);
357
358 m_points.emplace_back(x, y, z);
359
360 m_minmax_valid = false;
361
362 // JL: TODO note: Well, this can be clearly done much more efficiently
363 // but...I don't have time! :-(
364 wfWriteLock.unlock();
365 markAllPointsAsNew();
366 CRenderizable::notifyChange();
367 }
368
369 /** Write an individual point (checks for "i" in the valid range only in
370 * Debug).
371 */
372 void CPointCloud::setPoint(size_t i, const float x, const float y, const float z)
373 {
374 std::unique_lock<std::shared_mutex> wfWriteLock(CRenderizableShaderPoints::m_pointsMtx.data);
375
376 m_points.at(i) = {x, y, z};
377
378 m_minmax_valid = false;
379
380 // JL: TODO note: Well, this can be clearly done much more efficiently
381 // but...I don't have time! :-(
382 wfWriteLock.unlock();
383 markAllPointsAsNew();
384 CRenderizable::notifyChange();
385 }
386
387 /*---------------------------------------------------------------
388 setGradientColors
389 ---------------------------------------------------------------*/
390 void CPointCloud::setGradientColors(
391 const mrpt::img::TColorf& colorMin, const mrpt::img::TColorf& colorMax)
392 {
393 m_colorFromDepth_min = colorMin;
394 m_colorFromDepth_max = colorMax;
395 }
396
397 // Do needed internal work if all points are new (octree rebuilt,...)
398 void CPointCloud::markAllPointsAsNew()
399 {
400 std::unique_lock<std::shared_mutex> wfWriteLock(CRenderizableShaderPoints::m_pointsMtx.data);
401
402 m_minmax_valid = false;
403 octree_mark_as_outdated();
404 CRenderizable::notifyChange();
405 }
406
407 /** In a base class, reserve memory to prepare subsequent calls to
408 * PLY_import_set_vertex */
409 void CPointCloud::PLY_import_set_vertex_count(size_t N) { this->resize(N); }
410
411 /** In a base class, will be called after PLY_import_set_vertex_count() once
412 * for each loaded point. \param pt_color Will be nullptr if the loaded file
413 * does not provide color info.
414 */
415 void CPointCloud::PLY_import_set_vertex(
416 size_t idx,
417 const mrpt::math::TPoint3Df& pt,
418 [[maybe_unused]] const mrpt::img::TColorf* pt_color)
419 {
420 this->setPoint(idx, pt.x, pt.y, pt.z);
421 }
422
423 /** In a base class, return the number of vertices */
424 size_t CPointCloud::PLY_export_get_vertex_count() const { return this->size(); }
425 /** In a base class, will be called after PLY_export_get_vertex_count() once
426 * for each exported point. \param pt_color Will be nullptr if the loaded
427 * file does not provide color info.
428 */
429 void CPointCloud::PLY_export_get_vertex(
430 size_t idx,
431 mrpt::math::TPoint3Df& pt,
432 bool& pt_has_color,
433 [[maybe_unused]] mrpt::img::TColorf& pt_color) const
434 {
435 std::shared_lock<std::shared_mutex> wfReadLock(CRenderizableShaderPoints::m_pointsMtx.data);
436
437 pt_has_color = false;
438
439 pt = m_points[idx];
440 }
441
442 void CPointCloud::setAllPoints(const std::vector<mrpt::math::TPoint3D>& pts)
443 {
444 std::unique_lock<std::shared_mutex> wfWriteLock(CRenderizableShaderPoints::m_pointsMtx.data);
445
446 const auto N = pts.size();
447 m_points.resize(N);
448 for (size_t i = 0; i < N; i++) m_points[i] = pts[i];
449 m_minmax_valid = false;
450 wfWriteLock.unlock();
451 markAllPointsAsNew();
452 CRenderizable::notifyChange();
453 }
454
455 void CPointCloud::toYAMLMap(mrpt::containers::yaml& propertiesMap) const
456 {
457 CRenderizable::toYAMLMap(propertiesMap);
458 propertiesMap["point_count"] = m_points.size();
459 }