Debian Codegraph @

libs/opengl/src/CDisk.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/CDisk.h>
13 #include <mrpt/serialization/CArchive.h>
14
15 using namespace mrpt;
16 using namespace mrpt::opengl;
17 using namespace std;
18 using mrpt::poses::CPose3D;
19
20 IMPLEMENTS_SERIALIZABLE(CDisk, CRenderizable, mrpt::opengl)
21
22 void CDisk::onUpdateBuffers_Triangles()
23 {
24 using mrpt::math::TPoint3Df;
25
26 std::unique_lock<std::shared_mutex> trisWriteLock(
27 CRenderizableShaderTriangles::m_trianglesMtx.data);
28 auto& tris = CRenderizableShaderTriangles::m_triangles;
29
30 tris.clear();
31
32 // precomputed table:
33 ASSERT_GT_(m_nSlices, 2);
34
35 const float dAng = 2 * M_PIf / m_nSlices;
36 float a = 0;
37 // unit circle points: cos(ang),sin(ang)
38 std::vector<mrpt::math::TPoint2Df> circle(m_nSlices);
39 for (unsigned int i = 0; i < m_nSlices; i++, a += dAng)
40 {
41 circle[i].x = cos(a);
42 circle[i].y = sin(a);
43 }
44
45 const float r0 = m_radiusIn, r1 = m_radiusOut;
46
47 if (std::abs(r0) < 1e-6f)
48 {
49 // a filled disk:
50 for (unsigned int i = 0; i < m_nSlices; i++)
51 {
52 const auto ip = (i + 1) % m_nSlices;
53 tris.emplace_back(
54 TPoint3Df(r1 * circle[i].x, r1 * circle[i].y, .0f),
55 TPoint3Df(r1 * circle[ip].x, r1 * circle[ip].y, .0f), TPoint3Df(.0f, .0f, .0f));
56 }
57 }
58 else
59 {
60 // A ring:
61 for (unsigned int i = 0; i < m_nSlices; i++)
62 {
63 const auto ip = (i + 1) % m_nSlices;
64 tris.emplace_back(
65 TPoint3Df(r1 * circle[i].x, r1 * circle[i].y, .0f),
66 TPoint3Df(r1 * circle[ip].x, r1 * circle[ip].y, .0f),
67 TPoint3Df(r0 * circle[i].x, r0 * circle[i].y, .0f));
68
69 tris.emplace_back(
70 TPoint3Df(r1 * circle[ip].x, r1 * circle[ip].y, .0f),
71 TPoint3Df(r0 * circle[ip].x, r0 * circle[ip].y, .0f),
72 TPoint3Df(r0 * circle[i].x, r0 * circle[i].y, .0f));
73 }
74 }
75
76 // All faces, same color:
77 for (auto& t : tris) t.setColor(getColor_u8());
78 }
79
80 uint8_t CDisk::serializeGetVersion() const { return 2; }
81 void CDisk::serializeTo(mrpt::serialization::CArchive& out) const
82 {
83 writeToStreamRender(out);
84 out << m_radiusIn << m_radiusOut;
85 out << m_nSlices;
86 CRenderizableShaderTriangles::params_serialize(out); // v2
87 }
88
89 void CDisk::serializeFrom(mrpt::serialization::CArchive& in, uint8_t version)
90 {
91 switch (version)
92 {
93 case 0:
94 case 1:
95 case 2:
96 {
97 readFromStreamRender(in);
98 in >> m_radiusIn >> m_radiusOut;
99 in >> m_nSlices;
100 if (version < 1)
101 {
102 float dummy_loops;
103 in >> dummy_loops;
104 }
105
106 if (version >= 2) CRenderizableShaderTriangles::params_deserialize(in);
107 }
108 break;
109 default:
110 MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version);
111 };
112 CRenderizable::notifyChange();
113 }
114
115 bool CDisk::traceRay(const mrpt::poses::CPose3D& o, double& dist) const
116 {
117 // The disk is contained initially in a plane which contains (0,0,0),
118 // (1,0,0) and (0,1,0)
119 // These points are converted into:
120 //(x,y,z)
121 //( cos(w)*cos(p)+x, sin(w)*cos(p)*y, -sin(p)+z )
122 //( -sin(w)*cos(r)+cos(w)*sin(p)*sin(r)+x,
123 // cos(w)*cos(r)+sin(w)*sin(p)*sin(r)+y, cos(p)*sin(r)*z )
124 CPose3D transf = getCPose() - o;
125 double x = transf.x(), y = transf.y(), z = transf.z(), w = transf.yaw(), p = transf.pitch(),
126 r = transf.roll();
127 double coef = sin(w) * sin(r) + cos(w) * sin(p) * cos(r);
128 // coef is the first component of the normal to the transformed Z plane. So,
129 // the scalar product between
130 // this normal and (1,0,0) (which happens to be the beam's vector) equals
131 // coef. And if it's 0, then both
132 // are orthogonal, that is, the beam is parallel to the plane.
133 if (coef == 0) return false;
134 // The following expression yields the collision point between the plane and
135 // the beam (the y and z
136 // coordinates are zero).
137 dist = x + (y * (sin(p) * sin(w) * cos(r) - cos(w) * sin(r)) + z * cos(p) * cos(r)) / coef;
138 if (dist < 0) return false;
139 // Euclidean distance is invariant to rotations...
140 double d2 = (x - dist) * (x - dist) + y * y + z * z;
141 return d2 >= (m_radiusIn * m_radiusIn) && d2 <= (m_radiusOut * m_radiusOut);
142
143 // IMPORTANT NOTICE: using geometric intersection between Z plane and
144 // CPose's line intersection is SLOWER than the used method.
145 }
146
147 mrpt::math::TBoundingBoxf CDisk::internalBoundingBoxLocal() const
148 {
149 const float R = std::max(m_radiusIn, m_radiusOut);
150 return mrpt::math::TBoundingBoxf::FromUnsortedPoints({-R, -R, 0}, {R, R, .0});
151 }