Debian Codegraph @

libs/opengl/src/CPointCloudColoured.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> // for << ops
15 #include <mrpt/opengl/CPointCloudColoured.h>
16 #include <mrpt/opengl/opengl_api.h>
17 #include <mrpt/serialization/CArchive.h>
18 #include <mrpt/serialization/stl_serialization.h>
19
20 using namespace mrpt;
21 using namespace mrpt::opengl;
22 using namespace mrpt::math;
23 using namespace std;
24 using mrpt::serialization::CArchive;
25
26 IMPLEMENTS_SERIALIZABLE(CPointCloudColoured, CRenderizable, mrpt::opengl)
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_(&tst[1].x == (&tst[0].x + 3), "memory layout not as expected");
37 ASSERTMSG_(&tst[1].y == (&tst[0].y + 3), "memory layout not as expected");
38 ASSERTMSG_(&tst[1].z == (&tst[0].z + 3), "memory layout not as expected");
39 }
40
41 // const auto N = m_points.size();
42
43 octree_assure_uptodate(); // Rebuild octree if needed
44 m_last_rendered_count_ongoing = 0;
45
46 // TODO: Restore rendering using octrees?
47 // octree_render(*rc.state); // Render all points recursively:
48
49 // ------------------------------
50 // Fill the shader buffers
51 // ------------------------------
52 // "CRenderizableShaderPoints::m_vertex_buffer_data" is already done, since
53 // "m_points" is an alias for it.
54
55 // color buffer: idem. "m_point_colors" is an alias for
56 // CRenderizableShaderPoints::m_color_buffer_data.
57
58 m_last_rendered_count = m_last_rendered_count_ongoing;
59 }
60
61 /** Render a subset of points (required by octree renderer) */
62 void CPointCloudColoured::render_subset(
63 [[maybe_unused]] const bool all,
64 [[maybe_unused]] const std::vector<size_t>& idxs,
65 [[maybe_unused]] const float render_area_sqpixels) const
66 {
67 #if 0 && MRPT_HAS_OPENGL_GLUT
68 // Disabled for now... (Feb 2020)
69 const size_t N = all ? m_points.size() : idxs.size();
70 const size_t decimation = mrpt::round(std::max(
71 1.0f, d2f(N / (mrpt::global_settings::
72 OCTREE_RENDER_MAX_DENSITY_POINTS_PER_SQPIXEL() *
73 render_area_sqpixels))));
74
75 m_last_rendered_count_ongoing += N / decimation;
76
77 m_last_rendered_count_ongoing +=
78 (all ? m_points.size() : idxs.size()) / decimation;
79
80 if (all)
81 {
82 for (size_t i = 0; i < N; i += decimation)
83 {
84 const TPointColour& p = m_points[i];
85 glColor4ub(p.r, p.g, p.b, m_color.A);
86 glVertex3f(p.pt.x, p.pt.y, p.pt.z);
87 }
88 }
89 else
90 {
91 for (size_t i = 0; i < N; i += decimation)
92 {
93 const TPointColour& p = m_points[idxs[i]];
94 glColor4ub(p.r, p.g, p.b, m_color.A);
95 glVertex3f(p.pt.x, p.pt.y, p.pt.z);
96 }
97 }
98 #endif
99 }
100
101 uint8_t CPointCloudColoured::serializeGetVersion() const { return 4; }
102 void CPointCloudColoured::serializeTo(mrpt::serialization::CArchive& out) const
103 {
104 std::shared_lock<std::shared_mutex> wfReadLock(CRenderizableShaderPoints::m_pointsMtx.data);
105
106 writeToStreamRender(out);
107 out << m_points << m_point_colors;
108 CRenderizableShaderPoints::params_serialize(out);
109 }
110
111 void CPointCloudColoured::serializeFrom(mrpt::serialization::CArchive& in, uint8_t version)
112 {
113 std::unique_lock<std::shared_mutex> wfWriteLock(CRenderizableShaderPoints::m_pointsMtx.data);
114
115 switch (version)
116 {
117 case 0:
118 case 1:
119 case 2:
120 case 3:
121 {
122 THROW_EXCEPTION("Binary backward compatibility lost for this class.");
123 }
124 break;
125 case 4:
126 {
127 readFromStreamRender(in);
128 in >> m_points >> m_point_colors;
129
130 CRenderizableShaderPoints::params_deserialize(in);
131 }
132 break;
133 default:
134 MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version);
135 };
136
137 wfWriteLock.unlock();
138 markAllPointsAsNew();
139 CRenderizable::notifyChange();
140 }
141
142 /** Write an individual point (checks for "i" in the valid range only in Debug).
143 */
144 void CPointCloudColoured::setPoint(size_t i, const TPointXYZfRGBAu8& p)
145 {
146 std::unique_lock<std::shared_mutex> wfWriteLock(CRenderizableShaderPoints::m_pointsMtx.data);
147
148 #ifdef _DEBUG
149 ASSERT_LT_(i, size_unprotected());
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 wfWriteLock.unlock();
161 markAllPointsAsNew();
162 CRenderizable::notifyChange();
163 }
164
165 /** Inserts a new point into the point cloud. */
166 void CPointCloudColoured::push_back(float x, float y, float z, float R, float G, float B, float A)
167 {
168 std::unique_lock<std::shared_mutex> wfWriteLock(CRenderizableShaderPoints::m_pointsMtx.data);
169
170 m_points.emplace_back(x, y, z);
171 m_point_colors.emplace_back(f2u8(R), f2u8(G), f2u8(B), f2u8(A));
172
173 // JL: TODO note: Well, this can be clearly done much more efficiently
174 // but...I don't have time! :-(
175 wfWriteLock.unlock();
176 markAllPointsAsNew();
177 CRenderizable::notifyChange();
178 }
179
180 void CPointCloudColoured::insertPoint(const mrpt::math::TPointXYZfRGBAu8& p)
181 {
182 std::unique_lock<std::shared_mutex> wfWriteLock(CRenderizableShaderPoints::m_pointsMtx.data);
183
184 m_points.emplace_back(p.pt);
185 m_point_colors.emplace_back(p.r, p.g, p.b, p.a);
186
187 wfWriteLock.unlock();
188 markAllPointsAsNew();
189 CRenderizable::notifyChange();
190 }
191
192 // Do needed internal work if all points are new (octree rebuilt,...)
193 void CPointCloudColoured::markAllPointsAsNew()
194 {
195 octree_mark_as_outdated();
196 CRenderizable::notifyChange();
197 }
198 /** In a base class, reserve memory to prepare subsequent calls to
199 * PLY_import_set_vertex */
200 void CPointCloudColoured::PLY_import_set_vertex_count(size_t N) { this->resize(N); }
201
202 /** In a base class, will be called after PLY_import_set_vertex_count() once for
203 * each loaded point.
204 * \param pt_color Will be nullptr if the loaded file does not provide color
205 * info.
206 */
207 void CPointCloudColoured::PLY_import_set_vertex(
208 size_t idx, const mrpt::math::TPoint3Df& pt, const mrpt::img::TColorf* pt_color)
209 {
210 if (!pt_color)
211 this->setPoint(idx, TPointXYZfRGBAu8(pt.x, pt.y, pt.z, 0xff, 0xff, 0xff));
212 else
213 this->setPoint(
214 idx, TPointXYZfRGBAu8(
215 pt.x, pt.y, pt.z, f2u8(pt_color->R), f2u8(pt_color->G), f2u8(pt_color->B)));
216 }
217
218 /** In a base class, return the number of vertices */
219 size_t CPointCloudColoured::PLY_export_get_vertex_count() const { return this->size(); }
220
221 /** In a base class, will be called after PLY_export_get_vertex_count() once for
222 * each exported point.
223 * \param pt_color Will be nullptr if the loaded file does not provide color
224 * info.
225 */
226 void CPointCloudColoured::PLY_export_get_vertex(
227 size_t idx, mrpt::math::TPoint3Df& pt, bool& pt_has_color, mrpt::img::TColorf& pt_color) const
228 {
229 std::shared_lock<std::shared_mutex> wfReadLock(CRenderizableShaderPoints::m_pointsMtx.data);
230
231 auto& p = m_points[idx];
232 auto& p_color = m_point_colors[idx];
233 p = pt;
234 p_color = pt_color.asTColor();
235 pt_has_color = true;
236 }
237
238 void CPointCloudColoured::recolorizeByCoordinate(
239 const float coord_min,
240 const float coord_max,
241 const int coord_index,
242 const mrpt::img::TColormap color_map)
243 {
244 ASSERT_GE_(coord_index, 0);
245 ASSERT_LT_(coord_index, 3);
246
247 const float coord_range = coord_max - coord_min;
248 const float coord_range_1 = coord_range != 0.0f ? 1.0f / coord_range : 1.0f;
249 for (size_t i = 0; i < m_points.size(); i++)
250 {
251 float coord = .0f;
252 switch (coord_index)
253 {
254 case 0:
255 coord = m_points[i].x;
256 break;
257 case 1:
258 coord = m_points[i].y;
259 break;
260 case 2:
261 coord = m_points[i].z;
262 break;
263 };
264 const float col_idx = std::max(0.0f, std::min(1.0f, (coord - coord_min) * coord_range_1));
265 float r, g, b;
266 mrpt::img::colormap(color_map, col_idx, r, g, b);
267 this->setPointColor_fast(i, r, g, b);
268 }
269 }
270
271 void CPointCloudColoured::toYAMLMap(mrpt::containers::yaml& propertiesMap) const
272 {
273 CRenderizable::toYAMLMap(propertiesMap);
274 propertiesMap["point_count"] = m_points.size();
275 propertiesMap["bounding_box"] = this->getBoundingBox().asString();
276 }