Debian Codegraph @

libs/opengl/src/CPolyhedron.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/math/CMatrixDynamic.h>
13 #include <mrpt/math/CMatrixF.h>
14 #include <mrpt/math/TLine3D.h>
15 #include <mrpt/math/TObject3D.h>
16 #include <mrpt/math/geometry.h>
17 #include <mrpt/math/ops_containers.h> // dotProduct()
18 #include <mrpt/opengl/CPolyhedron.h>
19 #include <mrpt/poses/CPose3D.h>
20 #include <mrpt/random.h>
21 #include <mrpt/serialization/CArchive.h>
22 #include <mrpt/serialization/stl_serialization.h>
23
24 using namespace mrpt;
25 using namespace mrpt::math;
26 using namespace mrpt::opengl;
27 using namespace mrpt::poses;
28 using namespace std;
29 using mrpt::serialization::CArchive;
30
31 IMPLEMENTS_SERIALIZABLE(CPolyhedron, CRenderizable, mrpt::opengl)
32
33 // Auxiliary data and code
34 template <class T>
35 class FCreatePolygonFromFace
36 {
37 public:
38 const vector<TPoint3D>& verts;
39 FCreatePolygonFromFace(const vector<TPoint3D>& v) : verts(v) {}
40 TPolygon3D p;
41 T operator()(const CPolyhedron::TPolyhedronFace& f)
42 {
43 p = TPolygon3D(f.vertices.size());
44 for (size_t i = 0; i < f.vertices.size(); i++) p[i] = verts[f.vertices[i]];
45 return p;
46 }
47 };
48
49 bool getVerticesAndFaces(
50 const vector<math::TPolygon3D>& polys,
51 vector<TPoint3D>& vertices,
52 vector<CPolyhedron::TPolyhedronFace>& faces)
53 {
54 vertices.reserve(4 * polys.size());
55 faces.reserve(polys.size());
56 for (const auto& poly : polys)
57 {
58 size_t N = poly.size();
59 if (N < 3) return false;
60 CPolyhedron::TPolyhedronFace f;
61 f.vertices.resize(N);
62 for (size_t i = 0; i < N; i++)
63 {
64 auto it2 = find(vertices.begin(), vertices.end(), poly[i]);
65 if (it2 == vertices.end())
66 {
67 f.vertices[i] = vertices.size();
68 vertices.push_back(poly[i]);
69 }
70 else
71 f.vertices[i] = it2 - vertices.begin();
72 }
73 faces.push_back(f);
74 }
75 return true;
76 }
77
78 enum JohnsonBodyPart
79 {
80 INF_NO_BODY = -2,
81 SUP_NO_BODY = -1,
82 UPWARDS_PYRAMID = 0,
83 DOWNWARDS_PYRAMID = 1,
84 UPWARDS_CUPOLA = 2,
85 DOWNWARDS_CUPOLA = 3,
86 ROTATED_UPWARDS_CUPOLA = 4,
87 ROTATED_DOWNWARDS_CUPOLA = 5,
88 PRISM = 6,
89 ANTIPRISM = 7,
90 UPWARDS_ROTUNDA = 8,
91 DOWNWARDS_ROTUNDA = 9,
92 ROTATED_UPWARDS_ROTUNDA = 10,
93 ROTATED_DOWNWARDS_ROTUNDA = 11
94 };
95 bool analyzeJohnsonPartsString(
96 const std::string& components, uint32_t numBaseEdges, vector<JohnsonBodyPart>& parts)
97 {
98 size_t N = components.length();
99 size_t i = 0;
100 bool rot = false;
101 while (i < N)
102 {
103 switch (components[i])
104 {
105 case 'A':
106 case 'a':
107 if (parts.size() == 0) parts.push_back(INF_NO_BODY);
108 parts.push_back(ANTIPRISM);
109 break;
110 case 'C':
111 case 'c':
112 if (numBaseEdges & 1) return false;
113 if (i == N - 1) return false;
114 i++;
115 if (components[i] == '+')
116 {
117 if (i != N - 1) return false;
118 if (parts.size() == 0) parts.push_back(INF_NO_BODY);
119 parts.push_back(rot ? ROTATED_UPWARDS_CUPOLA : UPWARDS_CUPOLA);
120 rot = false;
121 }
122 else if (components[i] == '-')
123 {
124 if (parts.size() > 0) return false;
125 parts.push_back(rot ? ROTATED_DOWNWARDS_CUPOLA : DOWNWARDS_CUPOLA);
126 rot = false;
127 }
128 else
129 return false;
130 break;
131 case 'R':
132 case 'r':
133 if (numBaseEdges != 10) return false;
134 if (i == N - 1) return false;
135 i++;
136 if (components[i] == '+')
137 {
138 if (i != N - 1) return false;
139 if (parts.size() == 0) parts.push_back(INF_NO_BODY);
140 parts.push_back(rot ? ROTATED_UPWARDS_ROTUNDA : UPWARDS_ROTUNDA);
141 rot = false;
142 }
143 else if (components[i] == '-')
144 {
145 if (parts.size() > 0) return false;
146 parts.push_back(rot ? ROTATED_DOWNWARDS_ROTUNDA : DOWNWARDS_ROTUNDA);
147 rot = false;
148 }
149 else
150 return false;
151 break;
152 case 'G':
153 case 'g':
154 if (i == N - 1) return false;
155 i++;
156 if (components[i] == 'C' || components[i] == 'R')
157 {
158 rot = true;
159 continue;
160 }
161 else
162 return false;
163 case 'P':
164 case 'p':
165 if (i == N - 1) return false;
166 i++;
167 switch (components[i])
168 {
169 case '+':
170 if (numBaseEdges > 5) return false;
171 if (i != N - 1) return false;
172 if (parts.size() == 0) parts.push_back(INF_NO_BODY);
173 parts.push_back(UPWARDS_PYRAMID);
174 break;
175 case '-':
176 if (numBaseEdges > 5) return false;
177 if (i != 1) return false;
178 parts.push_back(DOWNWARDS_PYRAMID);
179 break;
180 case 'R':
181 case 'r':
182 if (parts.size() > 0 && (*parts.rbegin() == PRISM)) return false;
183 if (parts.size() == 0) parts.push_back(INF_NO_BODY);
184 parts.push_back(PRISM);
185 break;
186 default:
187 return false;
188 }
189 break;
190 default:
191 return false;
192 }
193 i++;
194 }
195 if (parts.size() == 0) return false;
196 JohnsonBodyPart p = *parts.rbegin();
197 if (p != UPWARDS_PYRAMID && p != UPWARDS_CUPOLA && p != ROTATED_UPWARDS_CUPOLA &&
198 p != UPWARDS_ROTUNDA && p != ROTATED_UPWARDS_ROTUNDA)
199 parts.push_back(SUP_NO_BODY);
200 return true;
201 }
202 inline size_t additionalVertices(JohnsonBodyPart j, uint32_t numBaseEdges)
203 {
204 if (j == INF_NO_BODY || j == SUP_NO_BODY)
205 return 0;
206 else if (j < UPWARDS_CUPOLA)
207 return 1; // j is a pyramid
208 else if (j < PRISM)
209 return numBaseEdges >> 1; // j is a cupola
210 else if (j < UPWARDS_ROTUNDA)
211 return numBaseEdges; // j is a prism or antiprism
212 else
213 return 10; // j is a rotunda
214 }
215 void insertCupola(
216 size_t numBaseEdges,
217 double angleShift,
218 double baseRadius,
219 double edgeLength,
220 bool isRotated,
221 bool isUpwards,
222 size_t base,
223 vector<TPoint3D>& verts,
224 vector<CPolyhedron::TPolyhedronFace>& faces)
225 {
226 size_t edges2 = numBaseEdges >> 1;
227 double minorRadius = baseRadius * sin(M_PI / numBaseEdges) / sin(M_PI / edges2);
228 //"Proper base"'s apothem=base radius*cos(2*pi/(2*numBaseEdges))
229 //"Small base"'s apothem=small radius*cos(2*pi/2*(numBaseEdges/2))
230 // Cupola's height is so that (Da^2+Height^2=Edge length^2, where Da is the
231 // difference between both apothems.
232 double h = sqrt(
233 square(edgeLength) -
234 square(baseRadius * cos(M_PI / numBaseEdges) - minorRadius * cos(M_PI / edges2)));
235 double height = verts[base].z + (isUpwards ? h : -h);
236 angleShift += M_PI / edges2 + (isRotated ? -M_PI / numBaseEdges : M_PI / numBaseEdges);
237 size_t minorBase = verts.size();
238 for (size_t i = 0; i < edges2; i++)
239 {
240 double ang = angleShift + 2 * M_PI * i / edges2;
241 verts.emplace_back(minorRadius * cos(ang), minorRadius * sin(ang), height);
242 }
243 CPolyhedron::TPolyhedronFace tri, quad, cBase;
244 tri.vertices.resize(3);
245 quad.vertices.resize(4);
246 cBase.vertices.resize(edges2);
247 size_t iq = isRotated ? 1 : 2, it = 0;
248 for (size_t i = 0; i < edges2; i++)
249 {
250 cBase.vertices[i] = it + minorBase;
251 size_t iiq = (iq + 1) % numBaseEdges + base;
252 size_t iiiq = (iiq + 1) % numBaseEdges + base;
253 size_t iit = (it + 1) % edges2 + minorBase;
254 quad.vertices[0] = it + minorBase;
255 quad.vertices[1] = iit;
256 quad.vertices[2] = iiq;
257 quad.vertices[3] = iq + base;
258 tri.vertices[0] = iit;
259 tri.vertices[1] = iiq;
260 tri.vertices[2] = iiiq;
261 iq = (iq + 2) % numBaseEdges;
262 it = (it + 1) % edges2;
263 faces.push_back(tri);
264 faces.push_back(quad);
265 }
266 if (edges2 >= 3) faces.push_back(cBase);
267 }
268 void insertRotunda(
269 double angleShift,
270 double baseRadius,
271 bool isRotated,
272 bool isUpwards,
273 size_t base,
274 vector<TPoint3D>& verts,
275 vector<CPolyhedron::TPolyhedronFace>& faces)
276 {
277 double R1 = baseRadius * sqrt((5.0 - sqrt(5.0)) / 10.0);
278 double R2 = baseRadius * sqrt((5.0 + sqrt(5.0)) / 10.0);
279 double baseHeight = verts[base].z;
280 TPoint3D p1[5], p2[5];
281 angleShift += M_PI / 10;
282 if (isRotated) angleShift += M_PI / 5;
283 for (size_t i = 0; i < 5; i++)
284 {
285 double a = (i + i + 1) * M_PI / 5 + angleShift;
286 double b = (i + i) * M_PI / 5 + angleShift;
287 double ca = cos(a), sa = sin(a), cb = cos(b), sb = sin(b);
288 p1[i].x = R1 * ca;
289 p1[i].y = R1 * sa;
290 p1[i].z = baseHeight + (isUpwards ? R2 : -R2);
291 p2[i].x = R2 * cb;
292 p2[i].y = R2 * sb;
293 p2[i].z = baseHeight + (isUpwards ? R1 : -R1);
294 }
295 size_t newBase = verts.size();
296 for (const auto& i : p1) verts.push_back(i);
297 for (const auto& i : p2) verts.push_back(i);
298 CPolyhedron::TPolyhedronFace f, g;
299 f.vertices.resize(3);
300 g.vertices.resize(5);
301 size_t baseStart = isRotated ? 2 : 1;
302 for (size_t i = 0; i < 5; i++)
303 {
304 size_t ii = (i + 1) % 5;
305 f.vertices[0] = newBase + i;
306 f.vertices[1] = newBase + ii;
307 f.vertices[2] = newBase + ii + 5;
308 faces.push_back(f);
309 f.vertices[0] = newBase + i + 5;
310 f.vertices[1] = ((i + i + baseStart) % 10) + base;
311 f.vertices[2] = ((i + i + 9 + baseStart) % 10) + base;
312 faces.push_back(f);
313 g.vertices[0] = newBase + (ii % 5) + 5;
314 g.vertices[1] = newBase + i;
315 g.vertices[2] = newBase + i + 5;
316 g.vertices[3] = (i + i + baseStart) % 10 + base;
317 g.vertices[4] = (i + i + baseStart + 1) % 10 + base;
318 faces.push_back(g);
319 }
320 for (size_t i = 0; i < 5; i++) g.vertices[i] = i + newBase;
321 faces.push_back(g);
322 return;
323 }
324 inline size_t additionalFaces(JohnsonBodyPart j, uint32_t numBaseEdges)
325 {
326 if (j == INF_NO_BODY || j == SUP_NO_BODY)
327 return 1; // j is a base
328 else if (j < UPWARDS_CUPOLA)
329 return numBaseEdges; // j is a pyramid
330 else if (j < PRISM)
331 return numBaseEdges + ((numBaseEdges >= 6) ? 1 : 0); // j is a cupola
332 else if (j == PRISM)
333 return numBaseEdges;
334 else if (j == ANTIPRISM)
335 return numBaseEdges << 1;
336 else
337 return 16; // j is a rotunda
338 }
339
340 inline bool faceContainsEdge(
341 const CPolyhedron::TPolyhedronFace& f, const CPolyhedron::TPolyhedronEdge& e)
342 {
343 char hm = 0;
344 for (unsigned int vertice : f.vertices)
345 if (vertice == e.v1 || vertice == e.v2) hm++;
346 return hm == 2;
347 }
348
349 bool getPlanesIntersection(const vector<const TPlane*>& planes, TPoint3D& pnt)
350 {
351 if (planes.size() < 3) return false;
352 char o = 0;
353 TPlane pl = *planes[0];
354 TLine3D l;
355 TObject3D obj;
356 for (size_t i = 1; i < planes.size(); i++) switch (o)
357 {
358 case 0:
359 if (!intersect(pl, *planes[i], obj)) return false;
360 if (obj.getPlane(pl))
361 o = 0;
362 else if (obj.getLine(l))
363 o = 1;
364 else if (obj.getPoint(pnt))
365 o = 2;
366 else
367 return false;
368 break;
369 case 1:
370 if (!intersect(l, *planes[i], obj)) return false;
371 if (obj.getLine(l))
372 o = 1;
373 else if (obj.getPoint(pnt))
374 o = 2;
375 else
376 return false;
377 break;
378 case 2:
379 if (!planes[i]->contains(pnt)) return false;
380 break;
381 default:
382 return false;
383 }
384 return o == 2;
385 }
386
387 bool searchForFace(
388 const vector<CPolyhedron::TPolyhedronFace>& fs, uint32_t v1, uint32_t v2, uint32_t v3)
389 {
390 for (const auto& it : fs)
391 {
392 const vector<uint32_t>& f = it.vertices;
393 size_t hmf = 0;
394 for (unsigned int it2 : f)
395 {
396 if (it2 == v1)
397 hmf |= 1;
398 else if (it2 == v2)
399 hmf |= 2;
400 else if (it2 == v3)
401 hmf |= 4;
402 }
403 if (hmf == 7) return true;
404 }
405 return false;
406 }
407 bool searchForEdge(
408 const vector<CPolyhedron::TPolyhedronEdge>& es, uint32_t v1, uint32_t v2, size_t& where)
409 {
410 for (where = 0; where < es.size(); where++)
411 {
412 const CPolyhedron::TPolyhedronEdge& e = es[where];
413 if (e.v1 == v1 && e.v2 == v2)
414 return true;
415 else if (e.v1 == v2 && e.v2 == v1)
416 return false;
417 }
418 throw std::logic_error("Internal error. Edge not found");
419 }
420 bool searchForEdge(
421 const vector<CPolyhedron::TPolyhedronFace>::const_iterator& begin,
422 const vector<CPolyhedron::TPolyhedronFace>::const_iterator& end,
423 uint32_t v1,
424 uint32_t v2)
425 {
426 for (vector<CPolyhedron::TPolyhedronFace>::const_iterator it = begin; it != end; ++it)
427 {
428 const vector<uint32_t>& f = it->vertices;
429 char res = 0;
430 for (unsigned int it2 : f)
431 if (it2 == v1)
432 res |= 1;
433 else if (it2 == v2)
434 res |= 2;
435 if (res == 3) return true;
436 }
437 return false;
438 }
439 double getHeight(const TPolygon3D& p, const TPoint3D& c)
440 {
441 size_t N = p.size();
442 if (N > 5 || N < 3) throw std::logic_error("Faces must have exactly 3, 4 or 5 vertices.");
443 double r = mrpt::math::distance(p[0], c);
444 double l = mrpt::math::distance(p[0], p[1]);
445 for (size_t i = 1; i < N; i++)
446 if (std::abs(mrpt::math::distance(p[i], c) - r) >= mrpt::math::getEpsilon())
447 throw std::logic_error("There is a non-regular polygon.");
448 else if (std::abs(mrpt::math::distance(p[i], p[(i + 1) % N]) - l) >= mrpt::math::getEpsilon())
449 throw std::logic_error("There is a non-regular polygon.");
450 return sqrt(square(l) - square(r));
451 }
452 struct SegmentVector
453 {
454 double d[3];
455 double mod;
456 inline double& operator[](size_t i) { return d[i]; }
457 inline double operator[](size_t i) const { return d[i]; }
458 };
459 // End of auxiliary data and code
460
461 double CPolyhedron::TPolyhedronEdge::length(const vector<TPoint3D>& vertices) const
462 {
463 return mrpt::math::distance(vertices[v1], vertices[v2]);
464 }
465
466 double CPolyhedron::TPolyhedronFace::area(const vector<TPoint3D>& vs) const
467 {
468 // Calculate as fan of triangles.
469 size_t N = vertices.size();
470 vector<SegmentVector> d(N - 1);
471 for (size_t i = 1; i < N; i++)
472 {
473 d[i - 1].mod = 0;
474 for (size_t j = 0; j < 3; j++)
475 {
476 d[i - 1][j] = vs[vertices[i]][j] - vs[vertices[0]][j];
477 d[i - 1].mod += square(d[i - 1][j]);
478 }
479 d[i - 1].mod = sqrt(d[i - 1].mod);
480 }
481 double res = 0;
482 for (size_t i = 1; i < N - 1; i++)
483 res += sqrt(square(d[i - 1].mod * d[i].mod) - square(dotProduct<3, double>(d[i - 1], d[i])));
484 return res / 2;
485 }
486
487 void CPolyhedron::TPolyhedronFace::getCenter(const vector<TPoint3D>& vrts, TPoint3D& p) const
488 {
489 p.x = p.y = p.z = 0.0;
490 for (unsigned int vertice : vertices)
491 {
492 p.x += vrts[vertice].x;
493 p.y += vrts[vertice].y;
494 p.z += vrts[vertice].z;
495 }
496 size_t N = vertices.size();
497 p.x /= N;
498 p.y /= N;
499 p.z /= N;
500 }
501
502 CPolyhedron::CPolyhedron(const std::vector<math::TPolygon3D>& polys) :
503 m_Edges(), m_Wireframe(false), polygonsUpToDate(false)
504 {
505 std::vector<TPoint3D> vertices(0);
506 std::vector<TPolyhedronFace> faces;
507 if (!getVerticesAndFaces(polys, vertices, faces)) throw std::logic_error("Can't create CPolygon");
508 m_Vertices = std::move(vertices);
509 m_Faces = std::move(faces);
510
511 InitFromVertAndFaces(m_Vertices, m_Faces);
512 }
513
514 CPolyhedron::CPolyhedron(const vector<TPoint3D>& vertices, const vector<vector<uint32_t>>& faces)
515 {
516 vector<TPolyhedronFace> aux;
517 for (const auto& face : faces)
518 {
519 TPolyhedronFace f;
520 f.vertices = face;
521 aux.push_back(f);
522 }
523 InitFromVertAndFaces(vertices, aux);
524 }
525
526 CPolyhedron::Ptr CPolyhedron::CreateCubicPrism(
527 double x1, double x2, double y1, double y2, double z1, double z2)
528 {
529 vector<TPoint3D> verts;
530 vector<TPolyhedronFace> faces;
531 for (int i = 0; i < 8; i++)
532 verts.emplace_back((i & 1) ? x2 : x1, (i & 2) ? y2 : y1, (i & 4) ? z2 : z1);
533 static uint32_t faceVertices[] = {0, 1, 5, 4, 2, 3, 7, 6, 0, 2, 6, 4,
534 1, 3, 7, 5, 0, 1, 3, 2, 4, 5, 7, 6};
535 TPolyhedronFace f;
536 for (auto* p = reinterpret_cast<uint32_t*>(&faceVertices);
537 p < 24 + reinterpret_cast<uint32_t*>(&faceVertices); p += 4)
538 {
539 f.vertices.insert(f.vertices.begin(), p, p + 4);
540 faces.push_back(f);
541 f.vertices.clear();
542 }
543 return CreateNoCheck(verts, faces);
544 }
545
546 CPolyhedron::Ptr CPolyhedron::CreatePyramid(const vector<TPoint2D>& baseVertices, double height)
547 {
548 uint32_t n = baseVertices.size();
549 if (baseVertices.size() < 3) throw std::logic_error("Not enough vertices");
550 vector<TPoint3D> verts;
551 vector<TPolyhedronFace> faces;
552 verts.emplace_back(0, 0, height);
553 for (auto baseVertice : baseVertices) verts.emplace_back(baseVertice.x, baseVertice.y, 0);
554 TPolyhedronFace f, g;
555 f.vertices.push_back(0);
556 f.vertices.push_back(n);
557 f.vertices.push_back(1);
558 g.vertices.push_back(1);
559 faces.push_back(f);
560 for (uint32_t i = 2; i <= n; i++)
561 {
562 f.vertices.erase(f.vertices.begin() + 1);
563 f.vertices.push_back(i);
564 faces.push_back(f);
565 g.vertices.push_back(i);
566 }
567 faces.push_back(g);
568 return CreateNoCheck(verts, faces);
569 }
570
571 CPolyhedron::Ptr CPolyhedron::CreateDoublePyramid(
572 const vector<TPoint2D>& baseVertices, double height1, double height2)
573 {
574 uint32_t N = baseVertices.size();
575 if (N < 3) throw std::logic_error("Not enough vertices");
576 vector<TPoint3D> verts;
577 verts.reserve(N + 2);
578 vector<TPolyhedronFace> faces;
579 faces.reserve(N << 1);
580 verts.emplace_back(0, 0, height1);
581 for (auto baseVertice : baseVertices) verts.emplace_back(baseVertice.x, baseVertice.y, 0);
582 verts.emplace_back(0, 0, -height2);
583 TPolyhedronFace f, g;
584 f.vertices.resize(3);
585 g.vertices.resize(3);
586 f.vertices[0] = 0;
587 g.vertices[0] = N + 1;
588 for (uint32_t i = 1; i < N; i++)
589 {
590 f.vertices[1] = g.vertices[1] = i;
591 f.vertices[2] = g.vertices[2] = i + 1;
592 faces.push_back(f);
593 faces.push_back(g);
594 }
595 f.vertices[1] = g.vertices[1] = 1;
596 faces.push_back(f);
597 faces.push_back(g);
598 return CreateNoCheck(verts, faces);
599 }
600
601 CPolyhedron::Ptr CPolyhedron::CreateTruncatedPyramid(
602 const vector<TPoint2D>& baseVertices, double height, double ratio)
603 {
604 uint32_t n = baseVertices.size();
605 if (n < 3) throw std::logic_error("Not enough vertices");
606 vector<TPoint3D> verts(n + n);
607 vector<TPolyhedronFace> faces(n + 2);
608 TPolyhedronFace f, g, h;
609 f.vertices.resize(4);
610 g.vertices.resize(n);
611 h.vertices.resize(n);
612 for (uint32_t i = 0; i < n; i++)
613 {
614 verts[i] = TPoint3D(baseVertices[i].x, baseVertices[i].y, 0);
615 verts[i + n] = TPoint3D(baseVertices[i].x * ratio, baseVertices[i].y * ratio, height);
616 uint32_t ii = (i + 1) % n;
617 f.vertices[0] = i;
618 f.vertices[1] = ii;
619 f.vertices[2] = ii + n;
620 f.vertices[3] = i + n;
621 faces[i] = f;
622 g.vertices[i] = i;
623 h.vertices[i] = i + n;
624 }
625 faces[n] = g;
626 faces[n + 1] = h;
627 return CreateNoCheck(verts, faces);
628 }
629
630 CPolyhedron::Ptr CPolyhedron::CreateCustomAntiprism(
631 const vector<TPoint2D>& bottomBase, const vector<TPoint2D>& topBase, const double height)
632 {
633 uint32_t n = bottomBase.size();
634 if (n < 3) throw std::logic_error("Not enough vertices");
635 if (n != topBase.size()) throw std::logic_error("Bases' number of vertices do not match");
636 vector<TPoint3D> verts(n + n);
637 vector<TPolyhedronFace> faces(n + n + 2);
638 TPolyhedronFace f, g, h;
639 f.vertices.resize(3);
640 g.vertices.resize(n);
641 h.vertices.resize(n);
642 for (uint32_t i = 0; i < n; i++)
643 {
644 verts[i] = TPoint3D(bottomBase[i].x, bottomBase[i].y, 0);
645 verts[n + i] = TPoint3D(topBase[i].x, topBase[i].y, height);
646 uint32_t ii = (i + 1) % n;
647 f.vertices[0] = i;
648 f.vertices[1] = ii;
649 f.vertices[2] = i + n;
650 faces[i] = f;
651 f.vertices[0] = i + n;
652 f.vertices[1] = ii + n;
653 f.vertices[2] = ii;
654 faces[n + i] = f;
655 g.vertices[i] = i;
656 h.vertices[i] = n + i;
657 }
658 faces[n + n] = g;
659 faces[n + n + 1] = h;
660 return CreateNoCheck(verts, faces);
661 }
662
663 CPolyhedron::Ptr CPolyhedron::CreateParallelepiped(
664 const TPoint3D& base, const TPoint3D& v1, const TPoint3D& v2, const TPoint3D& v3)
665 {
666 vector<TPoint3D> verts(8);
667 vector<TPolyhedronFace> faces(6);
668 for (uint32_t i = 0; i < 8; i++)
669 {
670 verts[i] = base;
671 if (i & 1) verts[i] = verts[i] + v1;
672 if (i & 2) verts[i] = verts[i] + v2;
673 if (i & 4) verts[i] = verts[i] + v3;
674 }
675 TPolyhedronFace f;
676 f.vertices.resize(4);
677 f.vertices[0] = 0;
678 f.vertices[1] = 1;
679 f.vertices[2] = 3;
680 f.vertices[3] = 2;
681 faces[0] = f;
682 // f.vertices[0]=0;
683 // f.vertices[1]=1;
684 f.vertices[2] = 5;
685 f.vertices[3] = 4;
686 faces[1] = f;
687 // f.vertices[0]=0;
688 f.vertices[1] = 2;
689 f.vertices[2] = 6;
690 // f.vertices[3]=4;
691 faces[2] = f;
692 for (uint32_t i = 0; i < 3; i++)
693 {
694 uint32_t valueAdd = 4 >> i;
695 faces[i + 3].vertices.resize(4);
696 for (uint32_t j = 0; j < 4; j++) faces[i + 3].vertices[j] = faces[i].vertices[j] + valueAdd;
697 }
698 return CreateNoCheck(verts, faces);
699 }
700
701 CPolyhedron::Ptr CPolyhedron::CreateBifrustum(
702 const vector<TPoint2D>& baseVertices,
703 double height1,
704 double ratio1,
705 double height2,
706 double ratio2)
707 {
708 // TODO: check special case in which ratio=0.
709 size_t N = baseVertices.size();
710 vector<TPoint3D> verts(3 * N);
711 size_t N2 = N + N;
712 for (size_t i = 0; i < N; i++)
713 {
714 double x = baseVertices[i].x;
715 double y = baseVertices[i].y;
716 verts[i].x = x;
717 verts[i].y = y;
718 verts[i].z = 0;
719 verts[i + N].x = x * ratio1;
720 verts[i + N].y = y * ratio1;
721 verts[i + N].z = height1;
722 verts[i + N2].x = x * ratio2;
723 verts[i + N2].y = y * ratio2;
724 verts[i + N2].z = -height2; // This is not an error. This way, two
725 // positive heights produce an actual
726 // bifrustum.
727 }
728 vector<TPolyhedronFace> faces(N2 + 2);
729 TPolyhedronFace f, g, h;
730 f.vertices.resize(4);
731 g.vertices.resize(N);
732 h.vertices.resize(N);
733 for (size_t i = 0; i < N; i++)
734 {
735 size_t i2 = (i + 1) % N;
736 f.vertices[0] = i;
737 f.vertices[1] = i2;
738 f.vertices[2] = i2 + N;
739 f.vertices[3] = i + N;
740 faces[i] = f;
741 f.vertices[2] = i2 + N2;
742 f.vertices[3] = i + N2;
743 faces[i + N] = f;
744 g.vertices[i] = i + N;
745 h.vertices[i] = i + N2;
746 }
747 faces[N2] = g;
748 faces[N2 + 1] = h;
749 return CreateNoCheck(verts, faces);
750 }
751
752 CPolyhedron::Ptr CPolyhedron::CreateTrapezohedron(
753 uint32_t numBaseEdges, double baseRadius, double basesDistance)
754 {
755 if (numBaseEdges < 3) throw std::logic_error("Not enough vertices");
756 if (basesDistance == 0 || baseRadius == 0) return CreateEmpty();
757 size_t numBaseEdges2 = numBaseEdges << 1;
758 vector<TPoint3D> verts(numBaseEdges2 + 2);
759 double space = 2 * M_PI / numBaseEdges;
760 double shift = space / 2;
761 double height1 = basesDistance / 2;
762 double cospii = cos(M_PI / numBaseEdges);
763 double height2 = height1 * (cospii + 1) /
764 (1 - cospii); // Apex height, calculated so each face conforms a plane
765 for (size_t i = 0; i < numBaseEdges; i++)
766 {
767 double ang = space * i;
768 double ang2 = ang + shift;
769 size_t ii = i + numBaseEdges;
770 verts[i].x = baseRadius * cos(ang);
771 verts[i].y = baseRadius * sin(ang);
772 verts[i].z = -height1;
773 verts[ii].x = baseRadius * cos(ang2);
774 verts[ii].y = baseRadius * sin(ang2);
775 verts[ii].z = height1;
776 }
777 verts[numBaseEdges2].x = 0;
778 verts[numBaseEdges2].y = 0;
779 verts[numBaseEdges2].z = -height2;
780 verts[numBaseEdges2 + 1].x = 0;
781 verts[numBaseEdges2 + 1].y = 0;
782 verts[numBaseEdges2 + 1].z = height2;
783 vector<TPolyhedronFace> faces(numBaseEdges2);
784 TPolyhedronFace f, g;
785 f.vertices.resize(4);
786 g.vertices.resize(4);
787 f.vertices[3] = numBaseEdges2;
788 g.vertices[3] = numBaseEdges2 + 1;
789 for (size_t i = 0; i < numBaseEdges; i++)
790 {
791 size_t ii = (i + 1) % numBaseEdges;
792 size_t i2 = i + numBaseEdges;
793 f.vertices[0] = i;
794 f.vertices[1] = i2;
795 f.vertices[2] = ii;
796 g.vertices[0] = i2;
797 g.vertices[1] = ii;
798 g.vertices[2] = ii + numBaseEdges;
799 faces[i] = f;
800 faces[i + numBaseEdges] = g;
801 }
802 return CreateNoCheck(verts, faces);
803 }
804
805 CPolyhedron::Ptr CPolyhedron::CreateJohnsonSolidWithConstantBase(
806 uint32_t numBaseEdges, double baseRadius, const std::string& components, size_t shifts)
807 {
808 if (baseRadius == 0) return CreateEmpty();
809 if (numBaseEdges < 3) throw std::logic_error("Not enough vertices");
810 vector<JohnsonBodyPart> parts;
811 if (!analyzeJohnsonPartsString(components, numBaseEdges, parts))
812 throw std::logic_error("Invalid string");
813 // Some common values are computed
814 size_t nParts = parts.size();
815 double edgeLength = 2 * baseRadius * sin(M_PI / numBaseEdges);
816 double antiPrism_height =
817 sqrt(square(edgeLength) - square(baseRadius) * (2 - 2 * cos(M_PI / numBaseEdges)));
818 // Vertices' and faces' vectors are computed
819 size_t nVerts = numBaseEdges * (nParts - 1) + additionalVertices(parts[0], numBaseEdges) +
820 additionalVertices(*parts.rbegin(), numBaseEdges);
821 size_t nFaces = 0;
822 for (size_t i = 0; i < nParts; i++) nFaces += additionalFaces(parts[i], numBaseEdges);
823 vector<TPoint3D> verts;
824 verts.reserve(nVerts);
825 vector<TPolyhedronFace> faces;
826 faces.reserve(nFaces);
827 // Each base's position is computed. Also, the height is set so that the
828 // polyhedron is vertically centered in z=0.
829 double h, m_height = 0;
830 vector<pair<double, size_t>> basePositionInfo(nParts - 1);
831 for (size_t i = 0; i < nParts - 1; i++)
832 {
833 if (parts[i] == PRISM)
834 h = edgeLength;
835 else if (parts[i] == ANTIPRISM)
836 {
837 h = antiPrism_height;
838 shifts++;
839 }
840 else
841 h = 0;
842 basePositionInfo[i] = make_pair(m_height += h, shifts);
843 }
844 m_height /= 2;
845 double semi = M_PI / numBaseEdges;
846 // All the bases are generated and inserted into the vertices' vector.
847 for (auto it = basePositionInfo.begin(); it != basePositionInfo.end(); ++it)
848 generateShiftedBase(numBaseEdges, baseRadius, it->first - m_height, semi * it->second, verts);
849 size_t initialBase = 0, endBase = 0;
850 TPolyhedronFace face;
851 face.vertices.reserve(numBaseEdges);
852 // Each body is inserted.
853 for (size_t p = 0; p < nParts; p++)
854 {
855 switch (parts[p])
856 {
857 case INF_NO_BODY:
858 // Inferior base.
859 face.vertices.resize(numBaseEdges);
860 for (size_t i = 0; i < numBaseEdges; i++) face.vertices[i] = endBase + i;
861 faces.push_back(face);
862 break;
863 case SUP_NO_BODY:
864 // Superior base.
865 face.vertices.resize(numBaseEdges);
866 for (size_t i = 0; i < numBaseEdges; i++) face.vertices[i] = initialBase + i;
867 faces.push_back(face);
868 break;
869 case UPWARDS_PYRAMID:
870 {
871 // Upwards-pointing pyramid. There must be 5 or less vertices.
872 double apexHeight = baseRadius * sqrt(4 * square(sin(M_PI / numBaseEdges)) - 1);
873 face.vertices.resize(3);
874 face.vertices[0] = verts.size();
875 face.vertices[1] = initialBase + numBaseEdges - 1;
876 face.vertices[2] = initialBase;
877 do
878 {
879 faces.push_back(face);
880 face.vertices[1] = face.vertices[2];
881 face.vertices[2]++;
882 } while (face.vertices[2] < initialBase + numBaseEdges);
883 verts.emplace_back(0, 0, verts[initialBase].z + apexHeight);
884 break;
885 }
886 case DOWNWARDS_PYRAMID:
887 {
888 // Downwards-pointing pyramid. There must be 5 or less vertices.
889 double apexHeight = baseRadius * sqrt(4 * square(sin(M_PI / numBaseEdges)) - 1);
890 face.vertices.resize(3);
891 face.vertices[0] = verts.size();
892 face.vertices[1] = endBase + numBaseEdges - 1;
893 face.vertices[2] = endBase;
894 do
895 {
896 faces.push_back(face);
897 face.vertices[1] = face.vertices[2];
898 face.vertices[2]++;
899 } while (face.vertices[2] < endBase + numBaseEdges);
900 verts.emplace_back(0, 0, verts[endBase].z - apexHeight);
901 break;
902 }
903 case UPWARDS_CUPOLA:
904 // Upwards-pointing cupola. There must be an even amount of
905 // vertices.
906 insertCupola(
907 numBaseEdges, basePositionInfo.rbegin()->second * semi, baseRadius, edgeLength, false,
908 true, initialBase, verts, faces);
909 break;
910 case DOWNWARDS_CUPOLA:
911 // Downwards-pointing cupola. There must be an even amount of
912 // vertices.
913 insertCupola(
914 numBaseEdges, basePositionInfo[0].second * semi, baseRadius, edgeLength, false, false,
915 endBase, verts, faces);
916 break;
917 case ROTATED_UPWARDS_CUPOLA:
918 // Upwards-pointing, slightly rotated, cupola. There must be an
919 // even amount of vertices.
920 insertCupola(
921 numBaseEdges, basePositionInfo.rbegin()->second * semi, baseRadius, edgeLength, true,
922 true, initialBase, verts, faces);
923 break;
924 case ROTATED_DOWNWARDS_CUPOLA:
925 // Downwards-pointing, slightly rotated, cupola. There must be
926 // an even amount of vertices.
927 insertCupola(
928 numBaseEdges, basePositionInfo[0].second * semi, baseRadius, edgeLength, true, false,
929 endBase, verts, faces);
930 break;
931 case PRISM:
932 // Archimedean prism.
933 face.vertices.resize(4);
934 for (size_t i = 0; i < numBaseEdges; i++)
935 {
936 size_t ii = (i + 1) % numBaseEdges;
937 face.vertices[0] = initialBase + i;
938 face.vertices[1] = endBase + i;
939 face.vertices[2] = endBase + ii;
940 face.vertices[3] = initialBase + ii;
941 faces.push_back(face);
942 }
943 break;
944 case ANTIPRISM:
945 {
946 // Archimedean antiprism.
947 face.vertices.resize(3);
948 face.vertices[0] = initialBase;
949 face.vertices[1] = endBase;
950 face.vertices[2] = initialBase + 1;
951 bool nextIsEnd = true;
952 size_t nextEnd = 1;
953 size_t nextInitial = 2;
954 for (size_t i = 0; i < numBaseEdges << 1; i++)
955 {
956 faces.push_back(face);
957 face.vertices[0] = face.vertices[1];
958 face.vertices[1] = face.vertices[2];
959 if (nextIsEnd)
960 {
961 face.vertices[2] = endBase + nextEnd;
962 nextEnd = (nextEnd + 1) % numBaseEdges;
963 }
964 else
965 {
966 face.vertices[2] = initialBase + nextInitial;
967 nextInitial = (nextInitial + 1) % numBaseEdges;
968 }
969 nextIsEnd = !nextIsEnd;
970 }
971 break;
972 }
973 case UPWARDS_ROTUNDA:
974 // Upwards-pointing pentagonal rotunda. Only for bases of
975 // exactly 10 vertices.
976 insertRotunda(
977 basePositionInfo.rbegin()->second * semi, baseRadius, false, true, initialBase, verts,
978 faces);
979 break;
980 case DOWNWARDS_ROTUNDA:
981 // Downwards-pointing pentagonal rotunda. Only for bases of
982 // exactly 10 vertices.
983 insertRotunda(
984 basePositionInfo[0].second * semi, baseRadius, false, false, endBase, verts, faces);
985 break;
986 case ROTATED_UPWARDS_ROTUNDA:
987 // Upwards-pointing, slightly rotated, pentagonal rotunda. Only
988 // for bases of exactly 10 vertices.
989 insertRotunda(
990 basePositionInfo.rbegin()->second * semi, baseRadius, true, true, initialBase, verts,
991 faces);
992 break;
993 case ROTATED_DOWNWARDS_ROTUNDA:
994 // Downwards-pointing, slightly rotated, pentagonal rotunda.
995 // Only for bases of exactly 10 vertices.
996 insertRotunda(
997 basePositionInfo[0].second * semi, baseRadius, true, false, endBase, verts, faces);
998 break;
999 default:
1000 throw std::logic_error("Internal error");
1001 }
1002 initialBase = endBase;
1003 endBase += numBaseEdges;
1004 }
1005 return CreateNoCheck(verts, faces);
1006 }
1007
1008 bool CPolyhedron::traceRay(const mrpt::poses::CPose3D& o, double& dist) const
1009 {
1010 if (!polygonsUpToDate) updatePolygons();
1011 return math::traceRay(tempPolygons, (o - getCPose()).asTPose(), dist);
1012 }
1013
1014 void CPolyhedron::getEdgesLength(std::vector<double>& lengths) const
1015 {
1016 lengths.resize(m_Edges.size());
1017 auto it2 = lengths.begin();
1018 for (auto it = m_Edges.begin(); it != m_Edges.end(); ++it, ++it2) *it2 = it->length(m_Vertices);
1019 }
1020
1021 void CPolyhedron::getFacesArea(std::vector<double>& areas) const
1022 {
1023 areas.resize(m_Faces.size());
1024 auto it2 = areas.begin();
1025 for (auto it = m_Faces.begin(); it != m_Faces.end(); ++it, ++it2) *it2 = it->area(m_Vertices);
1026 }
1027
1028 double CPolyhedron::getVolume() const
1029 {
1030 // TODO. Calculate as set of pyramids whose apices are situated in the
1031 // center of the polyhedron (will work only with convex polyhedrons).
1032 // Pyramid volume=V=1/3*base area*height. Height=abs((A-V)·N), where A is
1033 // the apex, V is any other vertex, N is the base's normal vector and (·) is
1034 // the scalar product.
1035 TPoint3D center;
1036 getCenter(center);
1037 double res = 0;
1038 if (!polygonsUpToDate) updatePolygons();
1039 auto itP = tempPolygons.begin();
1040 vector<double> areas(m_Faces.size());
1041 getFacesArea(areas);
1042 auto itA = areas.begin();
1043 for (auto it = m_Faces.begin(); it != m_Faces.end(); ++it, ++itP, ++itA)
1044 res += std::abs(itP->plane.distance(center)) * (*itA);
1045 return res / 3;
1046 }
1047
1048 void CPolyhedron::getSetOfPolygons(std::vector<math::TPolygon3D>& vec) const
1049 {
1050 if (!polygonsUpToDate) updatePolygons();
1051 size_t N = tempPolygons.size();
1052 vec.resize(N);
1053 for (size_t i = 0; i < N; i++) vec[i] = tempPolygons[i].poly;
1054 }
1055
1056 void CPolyhedron::getSetOfPolygonsAbsolute(std::vector<math::TPolygon3D>& vec) const
1057 {
1058 vec.resize(m_Faces.size());
1059 size_t N = m_Vertices.size();
1060 vector<TPoint3D> nVerts;
1061 nVerts.resize(N);
1062 CPose3D pose = getCPose();
1063 for (size_t i = 0; i < N; i++) pose.composePoint(m_Vertices[i], nVerts[i]);
1064 transform(
1065 m_Faces.begin(), m_Faces.end(), vec.begin(), FCreatePolygonFromFace<TPolygon3D>(nVerts));
1066 }
1067
1068 void CPolyhedron::makeConvexPolygons()
1069 {
1070 vector<TPolygon3D> polys, polysTMP, polys2;
1071 getSetOfPolygons(polys);
1072 polys2.reserve(polys.size());
1073 for (auto it = polys.begin(); it != polys.end(); ++it)
1074 if (mrpt::math::splitInConvexComponents(*it, polysTMP))
1075 polys2.insert(polys2.end(), polysTMP.begin(), polysTMP.end());
1076 else
1077 polys2.push_back(*it);
1078 m_Vertices.clear();
1079 m_Edges.clear();
1080 m_Faces.clear();
1081 getVerticesAndFaces(polys2, m_Vertices, m_Faces);
1082 for (auto& mFace : m_Faces)
1083 {
1084 if (!setNormal(mFace, false)) throw std::logic_error("Bad face specification");
1085 addEdges(mFace);
1086 }
1087 }
1088
1089 void CPolyhedron::getCenter(TPoint3D& center) const
1090 {
1091 size_t N = m_Vertices.size();
1092 if (N == 0) throw new std::logic_error("There are no vertices");
1093 center.x = center.y = center.z = 0;
1094 for (const auto& mVertice : m_Vertices)
1095 {
1096 center.x += mVertice.x;
1097 center.y += mVertice.y;
1098 center.z += mVertice.z;
1099 }
1100 center.x /= N;
1101 center.y /= N;
1102 center.z /= N;
1103 }
1104
1105 CPolyhedron::Ptr CPolyhedron::CreateRandomPolyhedron(double radius)
1106 {
1107 switch (mrpt::random::getRandomGenerator().drawUniform32bit() % 34)
1108 {
1109 case 0:
1110 return CreateTetrahedron(radius);
1111 case 1:
1112 return CreateHexahedron(radius);
1113 case 2:
1114 return CreateOctahedron(radius);
1115 case 3:
1116 return CreateDodecahedron(radius);
1117 case 4:
1118 return CreateIcosahedron(radius);
1119 case 5:
1120 return CreateTruncatedTetrahedron(radius);
1121 case 6:
1122 return CreateTruncatedHexahedron(radius);
1123 case 7:
1124 return CreateTruncatedOctahedron(radius);
1125 case 8:
1126 return CreateTruncatedDodecahedron(radius);
1127 case 9:
1128 return CreateTruncatedIcosahedron(radius);
1129 case 10:
1130 return CreateCuboctahedron(radius);
1131 case 11:
1132 return CreateRhombicuboctahedron(radius);
1133 case 12:
1134 return CreateIcosidodecahedron(radius);
1135 case 13:
1136 return CreateRhombicosidodecahedron(radius);
1137 case 14:
1138 return CreateTriakisTetrahedron(radius);
1139 case 15:
1140 return CreateTriakisOctahedron(radius);
1141 case 16:
1142 return CreateTetrakisHexahedron(radius);
1143 case 17:
1144 return CreateTriakisIcosahedron(radius);
1145 case 18:
1146 return CreatePentakisDodecahedron(radius);
1147 case 19:
1148 return CreateRhombicDodecahedron(radius);
1149 case 20:
1150 return CreateDeltoidalIcositetrahedron(radius);
1151 case 21:
1152 return CreateRhombicTriacontahedron(radius);
1153 case 22:
1154 return CreateDeltoidalHexecontahedron(radius);
1155 case 23:
1156 return CreateArchimedeanRegularPrism(
1157 (mrpt::random::getRandomGenerator().drawUniform32bit() % 10) + 3, radius);
1158 case 24:
1159 return CreateArchimedeanRegularAntiprism(
1160 (mrpt::random::getRandomGenerator().drawUniform32bit() % 10) + 3, radius);
1161 case 25:
1162 return CreateJohnsonSolidWithConstantBase(
1163 ((mrpt::random::getRandomGenerator().drawUniform32bit() % 4) << 1) + 4, radius, "C+");
1164 case 26:
1165 return CreateJohnsonSolidWithConstantBase(
1166 ((mrpt::random::getRandomGenerator().drawUniform32bit() % 4) << 1) + 4, radius, "C-C+");
1167 case 27:
1168 return CreateJohnsonSolidWithConstantBase(
1169 ((mrpt::random::getRandomGenerator().drawUniform32bit() % 4) << 1) + 4, radius, "C-PRC+");
1170 case 28:
1171 return CreateJohnsonSolidWithConstantBase(
1172 ((mrpt::random::getRandomGenerator().drawUniform32bit() % 4) << 1) + 4, radius, "C-AC+");
1173 case 29:
1174 return CreateJohnsonSolidWithConstantBase(10, radius, "R+");
1175 case 30:
1176 return CreateJohnsonSolidWithConstantBase(10, radius, "R-PRR+");
1177 case 31:
1178 return CreateJohnsonSolidWithConstantBase(10, radius, "R-AR+");
1179 case 32:
1180 return CreateCatalanTrapezohedron(
1181 (mrpt::random::getRandomGenerator().drawUniform32bit() % 5) + 3, radius);
1182 case 33:
1183 return CreateCatalanDoublePyramid(
1184 (mrpt::random::getRandomGenerator().drawUniform32bit() % 5) + 3, radius);
1185 default:
1186 return CreateEmpty();
1187 }
1188 }
1189
1190 CPolyhedron::Ptr CPolyhedron::getDual() const
1191 {
1192 // This methods builds the dual of a given polyhedron, which is assumed to
1193 // be centered in (0,0,0), using polar reciprocation.
1194 // A vertex (x0,y0,z0), which is inside a circunference x^2+y^2+z^2=r^2, its
1195 // dual face will lie on the x0·x+y0·y+z0·z=r^2 plane.
1196 // The new vertices can, then, be calculated as the corresponding
1197 // intersections between three or more planes.
1198 size_t NV = m_Faces.size();
1199 size_t NE = m_Edges.size();
1200 size_t NF = m_Vertices.size();
1201 vector<TPlane> planes(NF);
1202 for (size_t i = 0; i < NF; i++)
1203 {
1204 const TPoint3D& p = m_Vertices[i];
1205 TPlane& pl = planes[i];
1206 pl.coefs[0] = p.x;
1207 pl.coefs[1] = p.y;
1208 pl.coefs[2] = p.z;
1209 pl.coefs[3] = -square(p.x) - square(p.y) - square(p.z);
1210 }
1211 CMatrixDynamic<bool> incidence(NV, NF);
1212 vector<TPoint3D> vertices(NV);
1213 for (size_t i = 0; i < NV; i++)
1214 {
1215 for (size_t j = 0; j < NF; j++) incidence(i, j) = false;
1216 vector<const TPlane*> fPls;
1217 fPls.reserve(m_Faces[i].vertices.size());
1218 for (auto it = m_Faces[i].vertices.begin(); it != m_Faces[i].vertices.end(); ++it)
1219 {
1220 incidence(i, *it) = true;
1221 fPls.push_back(&planes[*it]);
1222 }
1223 if (!getPlanesIntersection(fPls, vertices[i]))
1224 throw std::logic_error("Dual polyhedron cannot be found");
1225 }
1226 vector<TPolyhedronFace> faces(NF);
1227 for (size_t i = 0; i < NF; i++)
1228 for (size_t j = 0; j < NV; j++)
1229 if (incidence(j, i)) faces[i].vertices.push_back(j);
1230 // The following code ensures that the faces' vertex list is in the adequate
1231 // order.
1232 CMatrixDynamic<bool> arrayEF(NE, NV);
1233 for (size_t i = 0; i < NE; i++)
1234 for (size_t j = 0; j < NV; j++) arrayEF(i, j) = faceContainsEdge(m_Faces[j], m_Edges[i]);
1235 for (auto& it : faces)
1236 {
1237 vector<uint32_t>& face = it.vertices;
1238 if (face.size() <= 3) continue;
1239 size_t index = 0;
1240 while (index < face.size() - 1)
1241 {
1242 bool err = true;
1243 while (err)
1244 {
1245 for (size_t i = 0; i < NE; i++)
1246 if (arrayEF(i, face[index]) && arrayEF(i, face[index + 1]))
1247 {
1248 err = false;
1249 break;
1250 }
1251 if (err)
1252 {
1253 size_t val = face[index + 1];
1254 face.erase(face.begin() + index + 1);
1255 face.push_back(val);
1256 }
1257 }
1258 index++;
1259 }
1260 }
1261 return std::make_shared<CPolyhedron>(vertices, faces);
1262 }
1263
1264 CPolyhedron::Ptr CPolyhedron::truncate(double factor) const
1265 {
1266 if (factor < 0) return CreateEmpty();
1267 if (factor == 0)
1268 return CreateNoCheck(m_Vertices, m_Faces);
1269 else if (factor < 1)
1270 {
1271 size_t NE = m_Edges.size();
1272 size_t NV = m_Vertices.size();
1273 size_t NF = m_Faces.size();
1274 vector<TPoint3D> vertices(NE << 1);
1275 vector<TPolyhedronFace> faces(NV + NF);
1276 for (size_t i = 0; i < NE; i++)
1277 {
1278 const TPoint3D& p1 = m_Vertices[m_Edges[i].v1];
1279 const TPoint3D& p2 = m_Vertices[m_Edges[i].v2];
1280 TPoint3D& v1 = vertices[i + i];
1281 TPoint3D& v2 = vertices[i + i + 1];
1282 for (size_t j = 0; j < 3; j++)
1283 {
1284 double d = (p2[j] - p1[j]) * factor / 2;
1285 v1[j] = p1[j] + d;
1286 v2[j] = p2[j] - d;
1287 }
1288 faces[m_Edges[i].v1].vertices.push_back(i + i);
1289 faces[m_Edges[i].v2].vertices.push_back(i + i + 1);
1290 }
1291 for (size_t i = 0; i < NV; i++)
1292 {
1293 vector<uint32_t>& f = faces[i].vertices;
1294 size_t sf = f.size();
1295 if (sf == 3) continue;
1296 for (size_t j = 1; j < sf - 1; j++)
1297 {
1298 const TPolyhedronEdge& e1 = m_Edges[f[j - 1] / 2];
1299 for (;;)
1300 {
1301 const TPolyhedronEdge& e2 = m_Edges[f[j] / 2];
1302 if (!((e1.v1 == i || e1.v2 == i) && (e2.v1 == i || e2.v2 == i)))
1303 THROW_EXCEPTION("En algo te has equivocado, chaval.");
1304 if (searchForFace(m_Faces, i, (e1.v1 == i) ? e1.v2 : e1.v1, (e2.v1 == i) ? e2.v2 : e2.v1))
1305 break;
1306 uint32_t tmpV = f[j];
1307 f.erase(f.begin() + j);
1308 f.push_back(tmpV);
1309 }
1310 }
1311 }
1312 for (size_t i = 0; i < NF; i++)
1313 {
1314 vector<uint32_t>& f = faces[i + NV].vertices;
1315 const vector<uint32_t>& cf = m_Faces[i].vertices;
1316 size_t hmV = cf.size();
1317 f.reserve(hmV << 1);
1318 for (size_t j = 0; j < hmV; j++)
1319 {
1320 size_t where;
1321 if (searchForEdge(m_Edges, cf[j], cf[(j + 1) % hmV], where))
1322 {
1323 f.push_back(where << 1);
1324 f.push_back((where << 1) + 1);
1325 }
1326 else
1327 {
1328 f.push_back((where << 1) + 1);
1329 f.push_back(where << 1);
1330 }
1331 }
1332 }
1333 return std::make_shared<CPolyhedron>(vertices, faces);
1334 }
1335 else if (factor == 1)
1336 {
1337 size_t NE = m_Edges.size();
1338 size_t NV = m_Vertices.size();
1339 size_t NF = m_Faces.size();
1340 vector<TPoint3D> vertices(NE);
1341 vector<TPolyhedronFace> faces(NV + NF);
1342 for (size_t i = 0; i < NE; i++)
1343 {
1344 const TPoint3D& p1 = m_Vertices[m_Edges[i].v1];
1345 const TPoint3D& p2 = m_Vertices[m_Edges[i].v2];
1346 TPoint3D& dst = vertices[i];
1347 for (size_t j = 0; j < 3; j++) dst[j] = (p1[j] + p2[j]) / 2;
1348 faces[m_Edges[i].v1].vertices.push_back(i);
1349 faces[m_Edges[i].v2].vertices.push_back(i);
1350 }
1351 for (size_t i = 0; i < NV; i++)
1352 {
1353 vector<uint32_t>& f = faces[i].vertices;
1354 size_t sf = f.size();
1355 if (sf == 3) continue;
1356 for (size_t j = 1; j < sf - 1; j++)
1357 {
1358 const TPolyhedronEdge& e1 = m_Edges[f[j - 1]];
1359 for (;;)
1360 {
1361 const TPolyhedronEdge& e2 = m_Edges[f[j - 1]];
1362 if (!((e1.v1 == i || e1.v2 == i) && (e2.v1 == 1 || e2.v2 == i)))
1363 THROW_EXCEPTION("En algo te has equivocado, chaval.");
1364 if (searchForFace(m_Faces, i, (e1.v1 == i) ? e1.v2 : e1.v1, (e2.v1 == i) ? e2.v2 : e2.v1))
1365 break;
1366 uint32_t tmpV = f[j];
1367 f.erase(f.begin() + j);
1368 f.push_back(tmpV);
1369 }
1370 }
1371 }
1372 for (size_t i = 0; i < NF; i++)
1373 {
1374 vector<uint32_t>& f = faces[i + NV].vertices;
1375 const vector<uint32_t>& cf = m_Faces[i].vertices;
1376 size_t hmV = cf.size();
1377 f.reserve(hmV);
1378 for (size_t j = 0; j < hmV; j++)
1379 {
1380 size_t where;
1381 searchForEdge(m_Edges, cf[j], cf[(j + 1) % hmV], where);
1382 f.push_back(where);
1383 }
1384 }
1385 return std::make_shared<CPolyhedron>(vertices, faces);
1386 }
1387 else
1388 return CreateEmpty();
1389 }
1390
1391 CPolyhedron::Ptr CPolyhedron::cantellate(double factor) const
1392 {
1393 if (factor < 0)
1394 return CreateEmpty();
1395 else if (factor == 0)
1396 return CreateNoCheck(m_Vertices, m_Faces);
1397 size_t NV = m_Vertices.size();
1398 size_t NE = m_Edges.size();
1399 size_t NF = m_Faces.size();
1400 vector<TPolygon3D> origFaces(NF);
1401 getSetOfPolygons(origFaces);
1402 TPoint3D cnt;
1403 getCenter(cnt);
1404 vector<TPoint3D> polyCenters(NF);
1405 vector<TPoint3D> polyNewCenters(NF);
1406 size_t NNV = 0;
1407 for (size_t i = 0; i < NF; i++)
1408 {
1409 origFaces[i].getCenter(polyCenters[i]);
1410 polyCenters[i] -= cnt;
1411 polyNewCenters[i] = polyCenters[i];
1412 polyNewCenters[i] *= (1 + factor);
1413 polyNewCenters[i] += cnt;
1414 NNV += origFaces[i].size();
1415 }
1416 vector<TPoint3D> vertices(NNV);
1417 vector<TPolyhedronFace> faces(NF + NV + NE);
1418 size_t ind = 0;
1419 for (size_t i = 0; i < NF; i++)
1420 {
1421 const TPoint3D& oC = polyCenters[i];
1422 const TPoint3D& nC = polyNewCenters[i];
1423 const TPolygon3D& oP = origFaces[i];
1424 vector<uint32_t>& f = faces[i].vertices;
1425 size_t oPS = oP.size();
1426 for (size_t j = 0; j < oPS; j++)
1427 {
1428 vertices[j + ind] = nC + (oP[j] - oC);
1429 f.push_back(j + ind);
1430 size_t curr = m_Faces[i].vertices[j];
1431 faces[NF + curr].vertices.push_back(j + ind);
1432 size_t edge;
1433 searchForEdge(m_Edges, curr, m_Faces[i].vertices[(j + oPS - 1) % oPS], edge);
1434 faces[NF + NV + edge].vertices.push_back(j + ind);
1435 searchForEdge(m_Edges, curr, m_Faces[i].vertices[(j + 1) % oPS], edge);
1436 faces[NF + NV + edge].vertices.push_back(j + ind);
1437 }
1438 ind += oPS;
1439 }
1440 auto begin = faces.begin(), edgeBegin = faces.begin() + NF + NV, end = faces.end();
1441 for (auto it = faces.begin() + NF; it != faces.begin() + NF + NV; ++it)
1442 {
1443 vector<uint32_t>& f = it->vertices;
1444 if (f.size() == 3) continue;
1445 for (size_t i = 1; i < f.size() - 1; i++)
1446 for (;;)
1447 if (searchForEdge(edgeBegin, end, f[i - 1], f[i]))
1448 break;
1449 else
1450 {
1451 uint32_t tmp = f[i];
1452 f.erase(f.begin() + i);
1453 f.push_back(tmp);
1454 }
1455 }
1456 for (auto it = faces.begin() + NF + NV; it != faces.end(); ++it)
1457 {
1458 vector<uint32_t>& f = it->vertices; // Will always have exactly 4 vertices
1459 for (size_t i = 1; i < 3; i++)
1460 for (;;)
1461 if (searchForEdge(begin, edgeBegin, f[i - 1], f[i]))
1462 break;
1463 else
1464 {
1465 uint32_t tmp = f[i];
1466 f.erase(f.begin() + i);
1467 f.push_back(tmp);
1468 }
1469 }
1470 return std::make_shared<CPolyhedron>(vertices, faces);
1471 }
1472
1473 CPolyhedron::Ptr CPolyhedron::augment(double height) const
1474 {
1475 size_t NV = m_Vertices.size();
1476 size_t NF = m_Faces.size();
1477 vector<TPoint3D> vertices(NV + NF);
1478 std::copy(m_Vertices.begin(), m_Vertices.end(), vertices.begin());
1479 size_t tnf = 0;
1480 for (const auto& mFace : m_Faces) tnf += mFace.vertices.size();
1481 vector<TPolyhedronFace> faces(tnf);
1482 TPolygon3D tmp;
1483 TPlane pTmp;
1484 TPoint3D cTmp;
1485 size_t iF = 0;
1486 TPoint3D phCenter;
1487 getCenter(phCenter);
1488 TPolyhedronFace fTmp;
1489 fTmp.vertices.resize(3);
1490 for (size_t i = 0; i < NF; i++)
1491 {
1492 TPoint3D& vertex = vertices[NV + i];
1493 const vector<uint32_t>& face = m_Faces[i].vertices;
1494 size_t N = face.size();
1495 tmp.resize(N);
1496 for (size_t j = 0; j < N; j++) tmp[j] = m_Vertices[face[j]];
1497 tmp.getBestFittingPlane(pTmp);
1498 pTmp.unitarize();
1499 tmp.getCenter(cTmp);
1500 if (pTmp.evaluatePoint(phCenter) > 0)
1501 for (size_t j = 0; j < 3; j++) vertex[j] = cTmp[j] - height * pTmp.coefs[j];
1502 else
1503 for (size_t j = 0; j < 3; j++) vertex[j] = cTmp[j] + height * pTmp.coefs[j];
1504 fTmp.vertices[0] = NV + i;
1505 for (size_t j = 0; j < N; j++)
1506 {
1507 fTmp.vertices[1] = face[j];
1508 fTmp.vertices[2] = face[(j + 1) % N];
1509 faces[iF + j] = fTmp;
1510 }
1511 iF += N;
1512 }
1513 return CreateNoCheck(vertices, faces);
1514 }
1515
1516 CPolyhedron::Ptr CPolyhedron::augment(double height, size_t numVertices) const
1517 {
1518 size_t NV = m_Vertices.size();
1519 size_t NF = m_Faces.size();
1520 size_t tnf = 0;
1521 size_t tnv = NV;
1522 for (const auto& mFace : m_Faces)
1523 if (mFace.vertices.size() == numVertices)
1524 {
1525 tnf += numVertices;
1526 tnv++;
1527 }
1528 else
1529 tnf++;
1530 if (tnv == NV) return CreateNoCheck(m_Vertices, m_Faces);
1531 vector<TPoint3D> vertices(tnv);
1532 std::copy(m_Vertices.begin(), m_Vertices.end(), vertices.begin());
1533 vector<TPolyhedronFace> faces(tnf);
1534 TPolygon3D tmp(numVertices);
1535 TPlane pTmp;
1536 TPoint3D cTmp;
1537 size_t iF = 0;
1538 size_t iV = NV;
1539 TPoint3D phCenter;
1540 getCenter(phCenter);
1541 TPolyhedronFace fTmp;
1542 fTmp.vertices.resize(3);
1543 for (size_t i = 0; i < NF; i++)
1544 {
1545 const vector<uint32_t>& face = m_Faces[i].vertices;
1546 size_t N = face.size();
1547 if (N != numVertices)
1548 {
1549 faces[iF].vertices = face;
1550 iF++;
1551 continue;
1552 }
1553 TPoint3D& vertex = vertices[iV];
1554 for (size_t j = 0; j < numVertices; j++) tmp[j] = m_Vertices[face[j]];
1555 tmp.getBestFittingPlane(pTmp);
1556 pTmp.unitarize();
1557 tmp.getCenter(cTmp);
1558 if (pTmp.evaluatePoint(phCenter) > 0)
1559 for (size_t j = 0; j < 3; j++) vertex[j] = cTmp[j] - height * pTmp.coefs[j];
1560 else
1561 for (size_t j = 0; j < 3; j++) vertex[j] = cTmp[j] + height * pTmp.coefs[j];
1562 fTmp.vertices[0] = iV;
1563 for (size_t j = 0; j < N; j++)
1564 {
1565 fTmp.vertices[1] = face[j];
1566 fTmp.vertices[2] = face[(j + 1) % N];
1567 faces[iF + j] = fTmp;
1568 }
1569 iF += N;
1570 iV++;
1571 }
1572 return CreateNoCheck(vertices, faces);
1573 }
1574
1575 CPolyhedron::Ptr CPolyhedron::augment(bool direction) const
1576 {
1577 size_t NV = m_Vertices.size();
1578 size_t NF = m_Faces.size();
1579 vector<TPoint3D> vertices(NV + NF);
1580 std::copy(m_Vertices.begin(), m_Vertices.end(), vertices.begin());
1581 size_t tnf = 0;
1582 for (const auto& mFace : m_Faces) tnf += mFace.vertices.size();
1583 vector<TPolyhedronFace> faces(tnf);
1584 TPolygon3D tmp;
1585 TPlane pTmp;
1586 TPoint3D cTmp;
1587 size_t iF = 0;
1588 TPoint3D phCenter;
1589 getCenter(phCenter);
1590 TPolyhedronFace fTmp;
1591 fTmp.vertices.resize(3);
1592 for (size_t i = 0; i < NF; i++)
1593 {
1594 TPoint3D& vertex = vertices[NV + i];
1595 const vector<uint32_t>& face = m_Faces[i].vertices;
1596 size_t N = face.size();
1597 tmp.resize(N);
1598 for (size_t j = 0; j < N; j++) tmp[j] = m_Vertices[face[j]];
1599 tmp.getCenter(cTmp);
1600 double height = getHeight(tmp, cTmp); // throws std::logic_error
1601 tmp.getBestFittingPlane(pTmp);
1602 pTmp.unitarize();
1603 if ((pTmp.evaluatePoint(phCenter) < 0) == direction)
1604 for (size_t j = 0; j < 3; j++) vertex[j] = cTmp[j] - height * pTmp.coefs[j];
1605 else
1606 for (size_t j = 0; j < 3; j++) vertex[j] = cTmp[j] + height * pTmp.coefs[j];
1607 fTmp.vertices[0] = NV + i;
1608 for (size_t j = 0; j < N; j++)
1609 {
1610 fTmp.vertices[1] = face[j];
1611 fTmp.vertices[2] = face[(j + 1) % N];
1612 faces[iF + j] = fTmp;
1613 }
1614 iF += N;
1615 }
1616 return CreateNoCheck(vertices, faces);
1617 }
1618
1619 CPolyhedron::Ptr CPolyhedron::augment(size_t numVertices, bool direction) const
1620 {
1621 size_t NV = m_Vertices.size();
1622 size_t NF = m_Faces.size();
1623 size_t tnf = 0;
1624 size_t tnv = NV;
1625 for (const auto& mFace : m_Faces)
1626 if (mFace.vertices.size() == numVertices)
1627 {
1628 tnf += numVertices;
1629 tnv++;
1630 }
1631 else
1632 tnf++;
1633 if (tnv == NV) return CreateNoCheck(m_Vertices, m_Faces);
1634 vector<TPoint3D> vertices(tnv);
1635 std::copy(m_Vertices.begin(), m_Vertices.end(), vertices.begin());
1636 vector<TPolyhedronFace> faces(tnf);
1637 TPolygon3D tmp(numVertices);
1638 TPlane pTmp;
1639 TPoint3D cTmp;
1640 size_t iF = 0;
1641 size_t iV = NV;
1642 TPoint3D phCenter;
1643 getCenter(phCenter);
1644 TPolyhedronFace fTmp;
1645 fTmp.vertices.resize(3);
1646 for (size_t i = 0; i < NF; i++)
1647 {
1648 const vector<uint32_t>& face = m_Faces[i].vertices;
1649 size_t N = face.size();
1650 if (N != numVertices)
1651 {
1652 faces[iF].vertices = face;
1653 iF++;
1654 continue;
1655 }
1656 TPoint3D& vertex = vertices[iV];
1657 for (size_t j = 0; j < numVertices; j++) tmp[j] = m_Vertices[face[j]];
1658 tmp.getBestFittingPlane(pTmp);
1659 pTmp.unitarize();
1660 tmp.getCenter(cTmp);
1661 double height = getHeight(tmp, cTmp); // throws std::logic_error
1662 if ((pTmp.evaluatePoint(phCenter) < 0) == direction)
1663 for (size_t j = 0; j < 3; j++) vertex[j] = cTmp[j] - height * pTmp.coefs[j];
1664 else
1665 for (size_t j = 0; j < 3; j++) vertex[j] = cTmp[j] + height * pTmp.coefs[j];
1666 fTmp.vertices[0] = iV;
1667 for (size_t j = 0; j < N; j++)
1668 {
1669 fTmp.vertices[1] = face[j];
1670 fTmp.vertices[2] = face[(j + 1) % N];
1671 faces[iF + j] = fTmp;
1672 }
1673 iF += N;
1674 iV++;
1675 }
1676 return CreateNoCheck(vertices, faces);
1677 }
1678
1679 CPolyhedron::Ptr CPolyhedron::rotate(double angle) const
1680 {
1681 vector<TPoint3D> vertices(m_Vertices);
1682 double c = cos(angle), s = sin(angle);
1683 for (auto& vertice : vertices)
1684 {
1685 double A = vertice.x;
1686 double B = vertice.y;
1687 vertice.x = A * c - B * s;
1688 vertice.y = B * c + A * s;
1689 }
1690 return CreateNoCheck(vertices, m_Faces);
1691 }
1692
1693 CPolyhedron::Ptr CPolyhedron::scale(double factor) const
1694 {
1695 vector<TPoint3D> vertices(m_Vertices);
1696 if (factor <= 0) throw std::logic_error("Factor must be a strictly positive number");
1697 for (auto& vertice : vertices)
1698 {
1699 vertice.x *= factor;
1700 vertice.y *= factor;
1701 }
1702 return CreateNoCheck(vertices, m_Faces);
1703 }
1704
1705 vector<TPoint2D> CPolyhedron::generateBase(uint32_t numBaseEdges, double baseRadius)
1706 {
1707 vector<TPoint2D> base(numBaseEdges);
1708 for (size_t i = 0; i < numBaseEdges; i++)
1709 {
1710 double ang = 2 * M_PI * i / numBaseEdges;
1711 base[i].x = baseRadius * cos(ang);
1712 base[i].y = baseRadius * sin(ang);
1713 }
1714 return base;
1715 }
1716
1717 vector<TPoint2D> CPolyhedron::generateShiftedBase(uint32_t numBaseEdges, double baseRadius)
1718 {
1719 vector<TPoint2D> base(numBaseEdges);
1720 double shift = M_PI / numBaseEdges;
1721 for (size_t i = 0; i < numBaseEdges; i++)
1722 {
1723 double ang = shift + 2 * M_PI * i / numBaseEdges;
1724 base[i].x = baseRadius * cos(ang);
1725 base[i].y = baseRadius * sin(ang);
1726 }
1727 return base;
1728 }
1729
1730 void CPolyhedron::generateBase(
1731 uint32_t numBaseEdges, double baseRadius, double height, vector<TPoint3D>& vec)
1732 {
1733 for (size_t i = 0; i < numBaseEdges; i++)
1734 {
1735 double ang = 2 * M_PI * i / numBaseEdges;
1736 vec.emplace_back(baseRadius * cos(ang), baseRadius * sin(ang), height);
1737 }
1738 }
1739
1740 void CPolyhedron::generateShiftedBase(
1741 uint32_t numBaseEdges, double baseRadius, double height, double shift, vector<TPoint3D>& vec)
1742 {
1743 for (size_t i = 0; i < numBaseEdges; i++)
1744 {
1745 double ang = 2 * M_PI * i / numBaseEdges + shift;
1746 vec.emplace_back(baseRadius * cos(ang), baseRadius * sin(ang), height);
1747 }
1748 }
1749
1750 void CPolyhedron::updatePolygons() const
1751 {
1752 tempPolygons.resize(m_Faces.size());
1753 transform(
1754 m_Faces.begin(), m_Faces.end(), tempPolygons.begin(),
1755 FCreatePolygonFromFace<TPolygonWithPlane>(m_Vertices));
1756 polygonsUpToDate = true;
1757 }
1758
1759 bool CPolyhedron::setNormal(TPolyhedronFace& f, bool doCheck)
1760 {
1761 size_t N = doCheck ? f.vertices.size() : 3;
1762 TPolygon3D poly(N);
1763 for (size_t i = 0; i < N; i++) poly[i] = m_Vertices[f.vertices[i]];
1764 TPlane tmp;
1765 if (!poly.getPlane(tmp)) return false;
1766 f.normal = tmp.getNormalVector();
1767 TPoint3D c;
1768 getCenter(c);
1769 if (tmp.evaluatePoint(c) > 0) f.normal *= -1;
1770 return true;
1771 }
1772
1773 void CPolyhedron::addEdges(const TPolyhedronFace& f)
1774 {
1775 TPolyhedronEdge e;
1776 auto it = f.vertices.begin();
1777 e.v1 = *it;
1778 ++it;
1779 while (it != f.vertices.end())
1780 {
1781 e.v2 = *it;
1782 if (find(m_Edges.begin(), m_Edges.end(), e) == m_Edges.end()) m_Edges.push_back(e);
1783 e.v1 = e.v2;
1784 ++it;
1785 }
1786 e.v2 = *(f.vertices.begin());
1787 if (find(m_Edges.begin(), m_Edges.end(), e) == m_Edges.end()) m_Edges.push_back(e);
1788 }
1789
1790 bool CPolyhedron::checkConsistence(
1791 const vector<TPoint3D>& vertices, const vector<TPolyhedronFace>& faces)
1792 {
1793 size_t N = vertices.size();
1794 if (vertices.size() > 0)
1795 for (auto it = vertices.begin(); it != vertices.end() - 1; ++it)
1796 for (auto it2 = it + 1; it2 != vertices.end(); ++it2)
1797 if (*it == *it2) return false;
1798 for (const auto& face : faces)
1799 {
1800 const vector<uint32_t>& e = face.vertices;
1801 for (unsigned int it2 : e)
1802 if (it2 >= N) return false;
1803 }
1804 return true;
1805 }
1806
1807 size_t CPolyhedron::edgesInVertex(size_t vertex) const
1808 {
1809 size_t res = 0;
1810 for (const auto& mEdge : m_Edges)
1811 if (mEdge.v1 == vertex || mEdge.v2 == vertex) res++;
1812 return res;
1813 }
1814
1815 size_t CPolyhedron::facesInVertex(size_t vertex) const
1816 {
1817 size_t res = 0;
1818 for (const auto& mFace : m_Faces)
1819 if (find(mFace.vertices.begin(), mFace.vertices.end(), vertex) != mFace.vertices.end()) res++;
1820 return res;
1821 }
1822
1823 CArchive& mrpt::opengl::operator>>(CArchive& in, CPolyhedron::TPolyhedronEdge& o)
1824 {
1825 in >> o.v1 >> o.v2;
1826 return in;
1827 }
1828
1829 CArchive& mrpt::opengl::operator<<(CArchive& out, const CPolyhedron::TPolyhedronEdge& o)
1830 {
1831 out << o.v1 << o.v2;
1832 return out;
1833 }
1834
1835 CArchive& mrpt::opengl::operator>>(CArchive& in, CPolyhedron::TPolyhedronFace& o)
1836 {
1837 in >> o.vertices >> o.normal[0] >> o.normal[1] >> o.normal[2];
1838 return in;
1839 }
1840
1841 CArchive& mrpt::opengl::operator<<(CArchive& out, const CPolyhedron::TPolyhedronFace& o)
1842 {
1843 out << o.vertices << o.normal[0] << o.normal[1] << o.normal[2];
1844 return out;
1845 }
1846
1847 uint8_t CPolyhedron::serializeGetVersion() const { return 1; }
1848 void CPolyhedron::serializeTo(mrpt::serialization::CArchive& out) const
1849 {
1850 writeToStreamRender(out);
1851 // version 0
1852 out << m_Vertices << m_Faces << m_Wireframe << m_lineWidth;
1853 CRenderizableShaderTriangles::params_serialize(out); // v1
1854 }
1855
1856 void CPolyhedron::serializeFrom(mrpt::serialization::CArchive& in, uint8_t version)
1857 {
1858 switch (version)
1859 {
1860 case 0:
1861 case 1:
1862 readFromStreamRender(in);
1863 in >> m_Vertices >> m_Faces >> m_Wireframe >> m_lineWidth;
1864 if (!checkConsistence(m_Vertices, m_Faces))
1865 throw std::logic_error("Inconsistent data read from stream");
1866 for (auto& mFace : m_Faces)
1867 {
1868 if (!setNormal(mFace)) throw std::logic_error("Bad face specification");
1869 addEdges(mFace);
1870 }
1871
1872 if (version >= 1) CRenderizableShaderTriangles::params_deserialize(in);
1873
1874 break;
1875 default:
1876 MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version);
1877 };
1878 CRenderizable::notifyChange();
1879 }
1880
1881 auto CPolyhedron::internalBoundingBoxLocal() const -> mrpt::math::TBoundingBoxf
1882 {
1883 auto bb = mrpt::math::TBoundingBoxf::PlusMinusInfinity();
1884 for (const auto& pt : m_Vertices) bb.updateWithPoint(pt);
1885 return bb;
1886 }
1887
1888 /*CPolyhedron::Ptr CPolyhedron::CreateCuboctahedron(double radius) {
1889 if (radius==0) return CreateEmpty();
1890 vector<TPoint3D> verts;
1891 vector<TPolyhedronFace> faces;
1892 double d=radius/sqrt(2.0);
1893 verts.push_back(TPoint3D(d,0,d));
1894 verts.push_back(TPoint3D(0,d,d));
1895 verts.push_back(TPoint3D(0,-d,d));
1896 verts.push_back(TPoint3D(-d,0,d));
1897 verts.push_back(TPoint3D(d,d,0));
1898 verts.push_back(TPoint3D(d,-d,0));
1899 verts.push_back(TPoint3D(-d,d,0));
1900 verts.push_back(TPoint3D(-d,-d,0));
1901 verts.push_back(TPoint3D(d,0,-d));
1902 verts.push_back(TPoint3D(0,d,-d));
1903 verts.push_back(TPoint3D(0,-d,-d));
1904 verts.push_back(TPoint3D(-d,0,-d));
1905 TPolyhedronFace f;
1906 static uint32_t faces3[]={0,1,4, 0,2,5, 1,3,6, 2,3,7, 8,9,4, 8,10,5, 9,11,6,
1907 10,11,7};
1908 static uint32_t faces4[]={0,1,3,2, 8,9,11,10, 0,4,8,5, 1,4,9,6, 2,5,10,7,
1909 3,6,11,7};
1910 for (uint32_t *p=reinterpret_cast<uint32_t
1911 *>(&faces3);p<24+reinterpret_cast<uint32_t *>(&faces3);p+=3) {
1912 f.vertices.insert(f.vertices.begin(),p,p+3);
1913 faces.push_back(f);
1914 f.vertices.clear();
1915 }
1916 for (uint32_t *p=reinterpret_cast<uint32_t
1917 *>(&faces4);p<24+reinterpret_cast<uint32_t *>(&faces4);p+=4) {
1918 f.vertices.insert(f.vertices.begin(),p,p+4);
1919 faces.push_back(f);
1920 f.vertices.clear();
1921 }
1922 return CreateNoCheck(verts,faces);
1923 }*/
1924
1925 CPolyhedron::Ptr CPolyhedron::CreateTetrahedron(double radius)
1926 {
1927 CPolyhedron::Ptr tetra = CreateJohnsonSolidWithConstantBase(3, radius * sqrt(8.0) / 3.0, "P+");
1928 for (auto& mVertice : tetra->m_Vertices) mVertice.z -= radius / 3;
1929 return tetra;
1930 }
1931 CPolyhedron::Ptr CPolyhedron::CreateHexahedron(double radius)
1932 {
1933 if (radius == 0.0) return CreateEmpty();
1934 double r = radius / sqrt(3.0);
1935 return CreateCubicPrism(-r, r, -r, r, -r, r);
1936 }
1937 CPolyhedron::Ptr CPolyhedron::CreateOctahedron(double radius)
1938 {
1939 return CreateJohnsonSolidWithConstantBase(4, radius, "P-P+");
1940 }
1941 CPolyhedron::Ptr CPolyhedron::CreateDodecahedron(double radius)
1942 {
1943 return CreateIcosahedron(radius / sqrt(15 - 6 * sqrt(5.0)))->getDual();
1944 }
1945 CPolyhedron::Ptr CPolyhedron::CreateIcosahedron(double radius)
1946 {
1947 double ang = M_PI / 5;
1948 double s2 = 4 * square(sin(ang));
1949 double prop = sqrt(s2 - 1) + sqrt(s2 - 2 + 2 * cos(ang)) / 2;
1950 return CreateJohnsonSolidWithConstantBase(5, radius / prop, "P-AP+", 1);
1951 }
1952 CPolyhedron::Ptr CPolyhedron::CreateTruncatedTetrahedron(double radius)
1953 {
1954 return CreateTetrahedron(radius * sqrt(27.0 / 11.0))->truncate(2.0 / 3.0);
1955 }
1956 CPolyhedron::Ptr CPolyhedron::CreateCuboctahedron(double radius)
1957 {
1958 return CreateHexahedron(radius * sqrt(1.5))->truncate(1.0);
1959 }
1960 CPolyhedron::Ptr CPolyhedron::CreateTruncatedHexahedron(double radius)
1961 {
1962 return CreateHexahedron(radius * sqrt(3.0 / (5 - sqrt(8.0))))->truncate(2 - sqrt(2.0));
1963 }
1964 CPolyhedron::Ptr CPolyhedron::CreateTruncatedOctahedron(double radius)
1965 {
1966 return CreateOctahedron(radius * 3 / sqrt(5.0))->truncate(2.0 / 3.0);
1967 }
1968 CPolyhedron::Ptr CPolyhedron::CreateRhombicuboctahedron(double radius, bool type)
1969 {
1970 return CreateJohnsonSolidWithConstantBase(
1971 8, radius / sqrt(1 + square(sin(M_PI / 8))), type ? "C-PRC+" : "GC-PRC+", 3);
1972 }
1973 CPolyhedron::Ptr CPolyhedron::CreateIcosidodecahedron(double radius, bool type)
1974 {
1975 return CreateJohnsonSolidWithConstantBase(10, radius, type ? "GR-R+" : "R-R+", 1);
1976 }
1977 CPolyhedron::Ptr CPolyhedron::CreateTruncatedDodecahedron(double radius)
1978 {
1979 return CreateDodecahedron(radius * sqrt(45.0) / sqrt(27 + 6 * sqrt(5.0)))
1980 ->truncate(1 - sqrt(0.2));
1981 }
1982 CPolyhedron::Ptr CPolyhedron::CreateTruncatedIcosahedron(double radius)
1983 {
1984 return CreateIcosahedron(radius * sqrt(45.0) / sqrt(25 + 4 * sqrt(5.0)))->truncate(2.0 / 3.0);
1985 }
1986 CPolyhedron::Ptr CPolyhedron::CreateRhombicosidodecahedron(double radius)
1987 {
1988 return CreateIcosahedron(radius * sqrt(10.0 / (35.0 + 9.0 * sqrt(5.0))))
1989 ->cantellate(1.5 * (sqrt(5.0) - 1));
1990 }
1991 CPolyhedron::Ptr CPolyhedron::CreatePentagonalRotunda(double radius)
1992 {
1993 return CreateJohnsonSolidWithConstantBase(10, radius, "R+");
1994 }
1995 CPolyhedron::Ptr CPolyhedron::CreateTriakisTetrahedron(double radius)
1996 {
1997 return CreateTruncatedTetrahedron(radius * 3 / sqrt(33.0))->getDual();
1998 }
1999 CPolyhedron::Ptr CPolyhedron::CreateRhombicDodecahedron(double radius)
2000 {
2001 return CreateCuboctahedron(radius / sqrt(2.0))->getDual();
2002 }
2003 CPolyhedron::Ptr CPolyhedron::CreateTriakisOctahedron(double radius)
2004 {
2005 return CreateTruncatedHexahedron(radius / sqrt((5 - sqrt(8.0))))->getDual();
2006 }
2007 CPolyhedron::Ptr CPolyhedron::CreateTetrakisHexahedron(double radius)
2008 {
2009 return CreateTruncatedOctahedron(radius * sqrt(0.6))->getDual();
2010 }
2011 CPolyhedron::Ptr CPolyhedron::CreateDeltoidalIcositetrahedron(double radius)
2012 {
2013 return CreateRhombicuboctahedron(radius / sqrt(7 - sqrt(32.0)), true)->getDual();
2014 }
2015 CPolyhedron::Ptr CPolyhedron::CreateRhombicTriacontahedron(double radius)
2016 {
2017 return CreateIcosidodecahedron(radius * sqrt(2 / (5 - sqrt(5.0))), true)->getDual();
2018 }
2019 CPolyhedron::Ptr CPolyhedron::CreateTriakisIcosahedron(double radius)
2020 {
2021 return CreateTruncatedDodecahedron(radius * sqrt(5 / (25 - 8 * sqrt(5.0))))->getDual();
2022 }
2023 CPolyhedron::Ptr CPolyhedron::CreatePentakisDodecahedron(double radius)
2024 {
2025 return CreateTruncatedIcosahedron(radius * sqrt(3 / (17 - 6 * sqrt(5.0))))->getDual();
2026 }
2027 CPolyhedron::Ptr CPolyhedron::CreateDeltoidalHexecontahedron(double radius)
2028 {
2029 return CreateRhombicosidodecahedron(radius * 3.0 / sqrt(15 - 2 * sqrt(5.0)))->getDual();
2030 }
2031 CPolyhedron::Ptr CPolyhedron::CreateCubicPrism(const TPoint3D& p1, const TPoint3D& p2)
2032 {
2033 return CreateCubicPrism(p1.x, p2.x, p1.y, p2.y, p1.z, p2.z);
2034 }
2035 CPolyhedron::Ptr CPolyhedron::CreateFrustum(
2036 const vector<TPoint2D>& baseVertices, double height, double ratio)
2037 {
2038 return CreateTruncatedPyramid(baseVertices, height, ratio);
2039 }
2040 CPolyhedron::Ptr CPolyhedron::CreateCustomPrism(const vector<TPoint2D>& baseVertices, double height)
2041 {
2042 return CreateTruncatedPyramid(baseVertices, height, 1.0);
2043 }
2044 CPolyhedron::Ptr CPolyhedron::CreateRegularAntiprism(
2045 uint32_t numBaseEdges, double baseRadius, double height)
2046 {
2047 return CreateCustomAntiprism(
2048 generateBase(numBaseEdges, baseRadius), generateShiftedBase(numBaseEdges, baseRadius),
2049 height);
2050 }
2051 CPolyhedron::Ptr CPolyhedron::CreateRegularPrism(
2052 uint32_t numBaseEdges, double baseRadius, double height)
2053 {
2054 return CreateCustomPrism(generateBase(numBaseEdges, baseRadius), height);
2055 }
2056 CPolyhedron::Ptr CPolyhedron::CreateRegularPyramid(
2057 uint32_t numBaseEdges, double baseRadius, double height)
2058 {
2059 return CreatePyramid(generateBase(numBaseEdges, baseRadius), height);
2060 }
2061 CPolyhedron::Ptr CPolyhedron::CreateRegularDoublePyramid(
2062 uint32_t numBaseEdges, double baseRadius, double height1, double height2)
2063 {
2064 return CreateDoublePyramid(generateBase(numBaseEdges, baseRadius), height1, height2);
2065 }
2066 CPolyhedron::Ptr CPolyhedron::CreateArchimedeanRegularPrism(
2067 uint32_t numBaseEdges, double baseRadius)
2068 {
2069 return CreateJohnsonSolidWithConstantBase(numBaseEdges, baseRadius, "PR");
2070 }
2071 CPolyhedron::Ptr CPolyhedron::CreateArchimedeanRegularAntiprism(
2072 uint32_t numBaseEdges, double baseRadius)
2073 {
2074 return CreateJohnsonSolidWithConstantBase(numBaseEdges, baseRadius, "A");
2075 }
2076 CPolyhedron::Ptr CPolyhedron::CreateRegularTruncatedPyramid(
2077 uint32_t numBaseEdges, double baseRadius, double height, double ratio)
2078 {
2079 return CreateTruncatedPyramid(generateBase(numBaseEdges, baseRadius), height, ratio);
2080 }
2081 CPolyhedron::Ptr CPolyhedron::CreateRegularFrustum(
2082 uint32_t numBaseEdges, double baseRadius, double height, double ratio)
2083 {
2084 return CreateRegularTruncatedPyramid(numBaseEdges, baseRadius, height, ratio);
2085 }
2086 CPolyhedron::Ptr CPolyhedron::CreateRegularBifrustum(
2087 uint32_t numBaseEdges,
2088 double baseRadius,
2089 double height1,
2090 double ratio1,
2091 double height2,
2092 double ratio2)
2093 {
2094 return CreateBifrustum(generateBase(numBaseEdges, baseRadius), height1, ratio1, height2, ratio2);
2095 }
2096 CPolyhedron::Ptr CPolyhedron::CreateCupola(uint32_t numBaseEdges, double edgeLength)
2097 {
2098 return CreateJohnsonSolidWithConstantBase(
2099 numBaseEdges, edgeLength / (2 * sin(M_PI / numBaseEdges)), "C+");
2100 }
2101 CPolyhedron::Ptr CPolyhedron::CreateCatalanTrapezohedron(uint32_t numBaseEdges, double height)
2102 {
2103 return CreateArchimedeanRegularAntiprism(numBaseEdges, height)->getDual();
2104 }
2105 CPolyhedron::Ptr CPolyhedron::CreateCatalanDoublePyramid(uint32_t numBaseEdges, double height)
2106 {
2107 return CreateArchimedeanRegularPrism(numBaseEdges, height)->getDual();
2108 }
2109
2110 CPolyhedron::Ptr CPolyhedron::CreateNoCheck(
2111 const vector<TPoint3D>& vertices, const vector<TPolyhedronFace>& faces)
2112 {
2113 return CPolyhedron::Create(vertices, faces, false);
2114 }
2115 CPolyhedron::Ptr CPolyhedron::CreateEmpty() { return CPolyhedron::Create(); }
2116
2117 void CPolyhedron::render(const RenderContext& rc) const
2118 {
2119 switch (rc.shader_id)
2120 {
2121 case DefaultShaderID::TRIANGLES_LIGHT:
2122 if (!m_Wireframe) CRenderizableShaderTriangles::render(rc);
2123 break;
2124 case DefaultShaderID::WIREFRAME:
2125 if (m_Wireframe) CRenderizableShaderWireFrame::render(rc);
2126 break;
2127 };
2128 }
2129 void CPolyhedron::renderUpdateBuffers() const
2130 {
2131 CRenderizableShaderTriangles::renderUpdateBuffers();
2132 CRenderizableShaderWireFrame::renderUpdateBuffers();
2133 }
2134
2135 void CPolyhedron::onUpdateBuffers_Wireframe()
2136 {
2137 auto& vbd = CRenderizableShaderWireFrame::m_vertex_buffer_data;
2138 auto& cbd = CRenderizableShaderWireFrame::m_color_buffer_data;
2139 std::unique_lock<std::shared_mutex> wfWriteLock(
2140 CRenderizableShaderWireFrame::m_wireframeMtx.data);
2141
2142 vbd.clear();
2143
2144 for (const auto& edge : m_Edges)
2145 {
2146 vbd.emplace_back(m_Vertices[edge.v1]);
2147 vbd.emplace_back(m_Vertices[edge.v2]);
2148 }
2149
2150 cbd.assign(vbd.size(), getColor_u8());
2151 }
2152
2153 void CPolyhedron::onUpdateBuffers_Triangles()
2154 {
2155 std::unique_lock<std::shared_mutex> trisWriteLock(
2156 CRenderizableShaderTriangles::m_trianglesMtx.data);
2157 auto& tris = CRenderizableShaderTriangles::m_triangles;
2158
2159 tris.clear();
2160
2161 for (const auto& face : m_Faces)
2162 {
2163 const size_t N = face.vertices.size();
2164 if (N < 3) continue;
2165
2166 // convert polygon -> triangle fan:
2167 for (size_t i = 0; i < N - 2; i++)
2168 {
2169 const size_t ip0 = 0;
2170 const size_t ip1 = (i + 1) % N;
2171 const size_t ip2 = (i + 2) % N;
2172
2173 const TPoint3D& p0 = m_Vertices[face.vertices[ip0]];
2174 TPoint3D p1 = m_Vertices[face.vertices[ip1]];
2175 TPoint3D p2 = m_Vertices[face.vertices[ip2]];
2176
2177 // Ensure the normal points outwards:
2178 const auto normal = mrpt::math::crossProduct3D(p1 - p0, p2 - p0);
2179 if (mrpt::math::dotProduct<3, double, TPoint3D, TPoint3D>(normal, p0) < 0) std::swap(p1, p2);
2180
2181 tris.emplace_back(p0, p1, p2);
2182 }
2183 }
2184
2185 // All faces, all vertices, same color:
2186 for (auto& t : tris) t.setColor(getColor_u8());
2187 }