Debian Codegraph @

libs/opengl/src/COctoMapVoxels.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/opengl/COctoMapVoxels.h>
13 #include <mrpt/opengl/opengl_api.h>
14 #include <mrpt/serialization/CArchive.h>
15 #include <mrpt/serialization/stl_serialization.h>
16
17 using namespace mrpt;
18 using namespace mrpt::opengl;
19 using namespace mrpt::math;
20 using namespace std;
21
22 IMPLEMENTS_SERIALIZABLE(COctoMapVoxels, CRenderizable, mrpt::opengl)
23
24 /** Ctor */
25 COctoMapVoxels::COctoMapVoxels() : m_grid_color(0xE0, 0xE0, 0xE0, 0x90) {}
26 /** Clears everything */
27 void COctoMapVoxels::clear()
28 {
29 m_voxel_sets.clear();
30 m_grid_cubes.clear();
31
32 CRenderizable::notifyChange();
33 }
34
35 void COctoMapVoxels::setBoundingBox(
36 const mrpt::math::TPoint3D& bb_min, const mrpt::math::TPoint3D& bb_max)
37 {
38 m_bb_min = bb_min;
39 m_bb_max = bb_max;
40 }
41
42 void COctoMapVoxels::render(const RenderContext& rc) const
43 {
44 switch (rc.shader_id)
45 {
46 case DefaultShaderID::POINTS:
47 if (m_showVoxelsAsPoints) CRenderizableShaderPoints::render(rc);
48 break;
49 case DefaultShaderID::TRIANGLES_LIGHT:
50 if (!m_showVoxelsAsPoints) CRenderizableShaderTriangles::render(rc);
51 break;
52 case DefaultShaderID::WIREFRAME:
53 if (m_show_grids) CRenderizableShaderWireFrame::render(rc);
54 break;
55 };
56 }
57 void COctoMapVoxels::renderUpdateBuffers() const
58 {
59 CRenderizableShaderPoints::renderUpdateBuffers();
60 CRenderizableShaderTriangles::renderUpdateBuffers();
61 CRenderizableShaderWireFrame::renderUpdateBuffers();
62 }
63
64 // See: http://www.songho.ca/opengl/gl_vertexarray.html
65
66 // cube ///////////////////////////////////////////////////////////////////////
67 // v6----- v5
68 // +Z /| /|
69 // A v1------v0|
70 // | | | | |
71 // | | |v7---|-|v4 / -X
72 // | |/ |/ /
73 // | v2------v3 L +X
74 // ----------------------> +Y
75 //
76
77 static const uint8_t grid_line_indices[] = {0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6,
78 6, 7, 7, 4, 0, 5, 1, 6, 2, 7, 3, 4};
79
80 static const uint8_t cube_indices[2 * 6 * 3] = {
81 // front:
82 0, 1, 2, 0, 2, 3,
83 // right:
84 3, 4, 0, 4, 5, 0,
85 // top
86 0, 5, 6, 6, 1, 0,
87 // left
88 1, 6, 7, 7, 2, 1,
89 // bottom
90 7, 3, 4, 2, 3, 7,
91 // back
92 4, 7, 6, 5, 6, 4};
93
94 // normal array: one per triangle
95 static const mrpt::math::TPoint3Df normals_cube[6 * 2] = {
96 { 1, 0, 0},
97 { 1, 0, 0}, // v0,v1,v2,v3 (front)
98 { 0, 1, 0},
99 { 0, 1, 0}, // v0,v3,v4,v5 (right)
100 { 0, 0, 1},
101 { 0, 0, 1}, // v0,v5,v6,v1 (top)
102 { 0, -1, 0},
103 { 0, -1, 0}, // v1,v6,v7,v2 (left)
104 { 0, 0, -1},
105 { 0, 0, -1}, // v7,v4,v3,v2 (bottom)
106 {-1, 0, 0},
107 {-1, 0, 0}
108 }; // v4,v7,v6,v5 (back)
109
110 void COctoMapVoxels::onUpdateBuffers_Wireframe()
111 {
112 auto& vbd = CRenderizableShaderWireFrame::m_vertex_buffer_data;
113 auto& cbd = CRenderizableShaderWireFrame::m_color_buffer_data;
114 std::unique_lock<std::shared_mutex> wfWriteLock(
115 CRenderizableShaderWireFrame::m_wireframeMtx.data);
116
117 vbd.clear();
118
119 CRenderizableShaderWireFrame::setLineWidth(m_grid_width);
120
121 const size_t nGrids = m_grid_cubes.size();
122 for (size_t i = 0; i < nGrids; i++)
123 {
124 const TGridCube& c = m_grid_cubes[i];
125
126 const mrpt::math::TPoint3Df vs[8] = {
127 {c.max.x, c.max.y, c.max.z},
128 {c.max.x, c.min.y, c.max.z},
129 {c.max.x, c.min.y, c.min.z},
130 {c.max.x, c.max.y, c.min.z},
131 {c.min.x, c.max.y, c.min.z},
132 {c.min.x, c.max.y, c.max.z},
133 {c.min.x, c.min.y, c.max.z},
134 {c.min.x, c.min.y, c.min.z}
135 };
136
137 const auto& gli = grid_line_indices;
138
139 // glDrawElements(GL_LINES, , GL_UNSIGNED_BYTE, grid_line_indices);
140 for (size_t k = 0; k < sizeof(gli) / sizeof(gli[0]); k += 2)
141 {
142 vbd.emplace_back(vs[gli[k]]);
143 vbd.emplace_back(vs[gli[k + 1]]);
144 }
145 }
146
147 cbd.assign(vbd.size(), m_grid_color);
148 }
149
150 void COctoMapVoxels::onUpdateBuffers_Triangles()
151 {
152 std::unique_lock<std::shared_mutex> trisWriteLock(
153 CRenderizableShaderTriangles::m_trianglesMtx.data);
154 auto& tris = CRenderizableShaderTriangles::m_triangles;
155
156 tris.clear();
157
158 for (const auto& m_voxel_set : m_voxel_sets)
159 {
160 if (!m_voxel_set.visible) continue;
161
162 const std::vector<TVoxel>& voxels = m_voxel_set.voxels;
163 const size_t N = voxels.size();
164 for (size_t j = 0; j < N; j++)
165 {
166 const mrpt::img::TColor& vx_j_col = voxels[j].color;
167 // Was: glColor4ub(vx_j_col.R, vx_j_col.G, vx_j_col.B, vx_j_col.A);
168
169 const mrpt::math::TPoint3Df& c = voxels[j].coords;
170 const float L = voxels[j].side_length * 0.5f;
171
172 // Render as cubes:
173 const mrpt::math::TPoint3Df vs[8] = {
174 {c.x + L, c.y + L, c.z + L},
175 {c.x + L, c.y - L, c.z + L},
176 {c.x + L, c.y - L, c.z - L},
177 {c.x + L, c.y + L, c.z - L},
178 {c.x - L, c.y + L, c.z - L},
179 {c.x - L, c.y + L, c.z + L},
180 {c.x - L, c.y - L, c.z + L},
181 {c.x - L, c.y - L, c.z - L}
182 };
183
184 // Was: glDrawElements(
185 // GL_TRIANGLES, sizeof(cube_indices) / sizeof(cube_indices[0]),
186 // GL_UNSIGNED_BYTE, cube_indices);
187 // glDrawElements(GL_LINES, , GL_UNSIGNED_BYTE, grid_line_indices);
188
189 const auto& ci = cube_indices;
190 const auto& ns = normals_cube;
191
192 for (size_t k = 0; k < sizeof(ci) / sizeof(ci[0]); k += 3)
193 {
194 mrpt::opengl::TTriangle tri(
195 // vertices:
196 vs[ci[k]], vs[ci[k + 1]], vs[ci[k + 2]],
197 // normals:
198 ns[k / 3], ns[k / 3], ns[k / 3]);
199
200 for (int p = 0; p < 3; p++)
201 {
202 tri.vertices[p].xyzrgba.r = vx_j_col.R;
203 tri.vertices[p].xyzrgba.g = vx_j_col.G;
204 tri.vertices[p].xyzrgba.b = vx_j_col.B;
205 tri.vertices[p].xyzrgba.a = vx_j_col.A;
206 }
207
208 tris.emplace_back(std::move(tri));
209 }
210 }
211 }
212 }
213
214 void COctoMapVoxels::onUpdateBuffers_Points()
215 {
216 auto& vbd = CRenderizableShaderPoints::m_vertex_buffer_data;
217 auto& cbd = CRenderizableShaderPoints::m_color_buffer_data;
218 std::unique_lock<std::shared_mutex> wfWriteLock(CRenderizableShaderPoints::m_pointsMtx.data);
219
220 for (const auto& m_voxel_set : m_voxel_sets)
221 {
222 if (!m_voxel_set.visible) continue;
223
224 const std::vector<TVoxel>& voxels = m_voxel_set.voxels;
225 const size_t N = voxels.size();
226 for (size_t j = 0; j < N; j++)
227 {
228 const mrpt::img::TColor& vx_j_col = voxels[j].color;
229 const mrpt::math::TPoint3D& c = voxels[j].coords;
230
231 vbd.emplace_back(c);
232 cbd.emplace_back(vx_j_col);
233 }
234 }
235 }
236
237 DECLARE_CUSTOM_TTYPENAME(COctoMapVoxels::TInfoPerVoxelSet)
238 DECLARE_CUSTOM_TTYPENAME(COctoMapVoxels::TGridCube)
239 DECLARE_CUSTOM_TTYPENAME(COctoMapVoxels::TVoxel)
240
241 namespace mrpt::opengl
242 {
243 using mrpt::serialization::CArchive;
244 CArchive& operator<<(CArchive& out, const COctoMapVoxels::TInfoPerVoxelSet& a)
245 {
246 out << a.visible << a.voxels;
247 return out;
248 }
249 CArchive& operator>>(CArchive& in, COctoMapVoxels::TInfoPerVoxelSet& a)
250 {
251 in >> a.visible >> a.voxels;
252 return in;
253 }
254
255 CArchive& operator<<(CArchive& out, const COctoMapVoxels::TGridCube& a)
256 {
257 out << a.min << a.max;
258 return out;
259 }
260 CArchive& operator>>(CArchive& in, COctoMapVoxels::TGridCube& a)
261 {
262 in >> a.min >> a.max;
263 return in;
264 }
265
266 CArchive& operator<<(CArchive& out, const COctoMapVoxels::TVoxel& a)
267 {
268 out << a.coords << a.side_length << a.color;
269 return out;
270 }
271 CArchive& operator>>(CArchive& in, COctoMapVoxels::TVoxel& a)
272 {
273 in >> a.coords >> a.side_length >> a.color;
274 return in;
275 }
276 } // end of namespace mrpt::opengl
277
278 uint8_t COctoMapVoxels::serializeGetVersion() const { return 4; }
279 void COctoMapVoxels::serializeTo(CArchive& out) const
280 {
281 writeToStreamRender(out);
282
283 out << m_voxel_sets << m_grid_cubes << m_bb_min << m_bb_max << m_enable_lighting
284 << m_showVoxelsAsPoints << m_showVoxelsAsPointsSize << m_show_grids << m_grid_width
285 << m_grid_color << m_enable_cube_transparency // added in v1
286 << uint32_t(m_visual_mode); // added in v2
287 CRenderizableShaderTriangles::params_serialize(out); // v3
288 out.WriteAs<uint8_t>(m_color_map); // v4
289 }
290
291 void COctoMapVoxels::serializeFrom(CArchive& in, uint8_t version)
292 {
293 switch (version)
294 {
295 case 0:
296 case 1:
297 case 2:
298 case 3:
299 case 4:
300 {
301 readFromStreamRender(in);
302
303 in >> m_voxel_sets >> m_grid_cubes >> m_bb_min >> m_bb_max >> m_enable_lighting >>
304 m_showVoxelsAsPoints >> m_showVoxelsAsPointsSize >> m_show_grids >> m_grid_width >>
305 m_grid_color;
306
307 if (version >= 1)
308 in >> m_enable_cube_transparency;
309 else
310 m_enable_cube_transparency = false;
311
312 if (version >= 2)
313 {
314 uint32_t i;
315 in >> i;
316 m_visual_mode = static_cast<COctoMapVoxels::visualization_mode_t>(i);
317 }
318 else
319 m_visual_mode = COctoMapVoxels::COLOR_FROM_OCCUPANCY;
320
321 if (version >= 3) CRenderizableShaderTriangles::params_deserialize(in);
322
323 if (version >= 4) m_color_map = static_cast<mrpt::img::TColormap>(in.ReadAs<uint8_t>());
324 }
325 break;
326 default:
327 MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version);
328 };
329
330 CRenderizable::notifyChange();
331 }
332
333 auto COctoMapVoxels::internalBoundingBoxLocal() const -> mrpt::math::TBoundingBoxf
334 {
335 return mrpt::math::TBoundingBoxf::FromUnsortedPoints(m_bb_min, m_bb_max);
336 }
337
338 bool sort_voxels_z(const COctoMapVoxels::TVoxel& a, const COctoMapVoxels::TVoxel& b)
339 {
340 return a.coords.z < b.coords.z;
341 }
342
343 void COctoMapVoxels::sort_voxels_by_z()
344 {
345 for (auto& m_voxel_set : m_voxel_sets)
346 {
347 std::sort(m_voxel_set.voxels.begin(), m_voxel_set.voxels.end(), &sort_voxels_z);
348 }
349 }