| 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/TLine3D.h> |
| 13 |
#include <mrpt/math/geometry.h> |
| 14 |
#include <mrpt/opengl/CCylinder.h> |
| 15 |
#include <mrpt/serialization/CArchive.h> |
| 16 |
#include <mrpt/serialization/CSchemeArchiveBase.h> |
| 17 |
|
| 18 |
using namespace mrpt; |
| 19 |
using namespace mrpt::opengl; |
| 20 |
using namespace mrpt::math; |
| 21 |
using namespace std; |
| 22 |
|
| 23 |
IMPLEMENTS_SERIALIZABLE(CCylinder, CRenderizable, mrpt::opengl) |
| 24 |
|
| 25 |
void CCylinder::onUpdateBuffers_Triangles() |
| 26 |
{ |
| 27 |
cullFaces(TCullFace::BACK); // dont render back faces |
| 28 |
|
| 29 |
std::unique_lock<std::shared_mutex> trisWriteLock( |
| 30 |
CRenderizableShaderTriangles::m_trianglesMtx.data); |
| 31 |
auto& tris = CRenderizableShaderTriangles::m_triangles; |
| 32 |
|
| 33 |
tris.clear(); |
| 34 |
|
| 35 |
// precomputed table: |
| 36 |
ASSERT_GT_(m_slices, 2); |
| 37 |
|
| 38 |
const float dAng = 2 * M_PIf / m_slices; |
| 39 |
float a = 0; |
| 40 |
// unit circle points: cos(ang),sin(ang) |
| 41 |
std::vector<mrpt::math::TPoint2Df> circle(m_slices); |
| 42 |
for (unsigned int i = 0; i < m_slices; i++, a += dAng) |
| 43 |
{ |
| 44 |
circle[i].x = cos(a); |
| 45 |
circle[i].y = sin(a); |
| 46 |
} |
| 47 |
|
| 48 |
// Handle negative heights so triangles winding is OK for GL culling: |
| 49 |
const float r0 = m_height > 0 ? m_baseRadius : m_topRadius; |
| 50 |
const float r1 = m_height > 0 ? m_topRadius : m_baseRadius; |
| 51 |
const float z0 = m_height > 0 ? .0f : m_height; |
| 52 |
const float z1 = m_height > 0 ? m_height : .0f; |
| 53 |
|
| 54 |
const float wall_tilt = std::atan2(r0 - r1, m_height); |
| 55 |
const float coswt = std::cos(wall_tilt) * mrpt::sign(m_height), |
| 56 |
sinwt = std::sin(wall_tilt) * mrpt::sign(m_height); |
| 57 |
|
| 58 |
// cylinder walls: |
| 59 |
for (unsigned int i = 0; i < m_slices; i++) |
| 60 |
{ |
| 61 |
const auto ip = (i + 1) % m_slices; |
| 62 |
|
| 63 |
tris.emplace_back( |
| 64 |
// Points: |
| 65 |
TPoint3Df(r0 * circle[i].x, r0 * circle[i].y, z0), |
| 66 |
TPoint3Df(r0 * circle[ip].x, r0 * circle[ip].y, z0), |
| 67 |
TPoint3Df(r1 * circle[i].x, r1 * circle[i].y, z1), |
| 68 |
// Normals: |
| 69 |
TVector3Df(-coswt * circle[i].y, coswt * circle[i].x, sinwt), |
| 70 |
TVector3Df(-coswt * circle[ip].y, coswt * circle[ip].x, sinwt), |
| 71 |
TVector3Df(-coswt * circle[i].y, coswt * circle[i].x, sinwt)); |
| 72 |
|
| 73 |
tris.emplace_back( |
| 74 |
// Points: |
| 75 |
TPoint3Df(r0 * circle[ip].x, r0 * circle[ip].y, z0), |
| 76 |
TPoint3Df(r1 * circle[ip].x, r1 * circle[ip].y, z1), |
| 77 |
TPoint3Df(r1 * circle[i].x, r1 * circle[i].y, z1), |
| 78 |
// Normals: |
| 79 |
TVector3Df(-coswt * circle[ip].y, coswt * circle[ip].x, sinwt), |
| 80 |
TVector3Df(-coswt * circle[ip].y, coswt * circle[ip].x, sinwt), |
| 81 |
TVector3Df(-coswt * circle[i].y, coswt * circle[i].x, sinwt)); |
| 82 |
} |
| 83 |
|
| 84 |
// bottom & top disks: |
| 85 |
if (m_hasTopBase || m_hasBottomBase) |
| 86 |
{ |
| 87 |
for (unsigned int i = 0; i < m_slices; i++) |
| 88 |
{ |
| 89 |
const auto ip = (i + 1) % m_slices; |
| 90 |
if (m_hasBottomBase) |
| 91 |
tris.emplace_back( |
| 92 |
TPoint3Df(r0 * circle[i].x, r0 * circle[i].y, z0), TPoint3Df(.0f, .0f, z0), |
| 93 |
TPoint3Df(r0 * circle[ip].x, r0 * circle[ip].y, z0)); |
| 94 |
|
| 95 |
if (m_hasTopBase) |
| 96 |
tris.emplace_back( |
| 97 |
TPoint3Df(r1 * circle[i].x, r1 * circle[i].y, z1), |
| 98 |
TPoint3Df(r1 * circle[ip].x, r1 * circle[ip].y, z1), TPoint3Df(.0f, .0f, z1)); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
// All faces, same color: |
| 103 |
for (auto& t : tris) t.setColor(getColor_u8()); |
| 104 |
} |
| 105 |
|
| 106 |
void CCylinder::serializeTo(mrpt::serialization::CSchemeArchiveBase& out) const |
| 107 |
{ |
| 108 |
SCHEMA_SERIALIZE_DATATYPE_VERSION(1); |
| 109 |
out["baseRadius"] = m_baseRadius; |
| 110 |
out["topRadius"] = m_topRadius; |
| 111 |
out["height"] = m_height; |
| 112 |
out["slices"] = m_slices; |
| 113 |
out["hasBottomBase"] = m_hasBottomBase; |
| 114 |
out["hasTopBase"] = m_hasTopBase; |
| 115 |
} |
| 116 |
void CCylinder::serializeFrom(mrpt::serialization::CSchemeArchiveBase& in) |
| 117 |
{ |
| 118 |
uint8_t version; |
| 119 |
SCHEMA_DESERIALIZE_DATATYPE_VERSION(); |
| 120 |
switch (version) |
| 121 |
{ |
| 122 |
case 1: |
| 123 |
{ |
| 124 |
m_baseRadius = static_cast<float>(in["baseRadius"]); |
| 125 |
m_topRadius = static_cast<float>(in["topRadius"]); |
| 126 |
m_height = static_cast<float>(in["height"]); |
| 127 |
m_slices = static_cast<uint32_t>(in["slices"]); |
| 128 |
m_hasBottomBase = static_cast<bool>(in["hasBottomBase"]); |
| 129 |
m_hasTopBase = static_cast<bool>(in["hasTopBase"]); |
| 130 |
} |
| 131 |
break; |
| 132 |
default: |
| 133 |
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version); |
| 134 |
} |
| 135 |
} |
| 136 |
uint8_t CCylinder::serializeGetVersion() const { return 2; } |
| 137 |
void CCylinder::serializeTo(mrpt::serialization::CArchive& out) const |
| 138 |
{ |
| 139 |
writeToStreamRender(out); |
| 140 |
// version 0 |
| 141 |
out << m_baseRadius << m_topRadius << m_height << m_slices << m_hasBottomBase << m_hasTopBase; |
| 142 |
CRenderizableShaderTriangles::params_serialize(out); // v2 |
| 143 |
} |
| 144 |
void CCylinder::serializeFrom(mrpt::serialization::CArchive& in, uint8_t version) |
| 145 |
{ |
| 146 |
switch (version) |
| 147 |
{ |
| 148 |
case 0: |
| 149 |
case 1: |
| 150 |
case 2: |
| 151 |
readFromStreamRender(in); |
| 152 |
in >> m_baseRadius >> m_topRadius >> m_height >> m_slices; |
| 153 |
|
| 154 |
if (version < 1) |
| 155 |
{ |
| 156 |
float old_mStacks; |
| 157 |
in >> old_mStacks; |
| 158 |
} |
| 159 |
|
| 160 |
in >> m_hasBottomBase >> m_hasTopBase; |
| 161 |
|
| 162 |
if (version >= 2) CRenderizableShaderTriangles::params_deserialize(in); |
| 163 |
break; |
| 164 |
default: |
| 165 |
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version); |
| 166 |
}; |
| 167 |
CRenderizable::notifyChange(); |
| 168 |
} |
| 169 |
|
| 170 |
bool solveEqn(double a, double b, double c, double& t) |
| 171 |
{ // Actually, the b from the quadratic equation is |
| 172 |
// the DOUBLE of this. But |
| 173 |
// this way, operations are simpler. |
| 174 |
if (a < 0) |
| 175 |
{ |
| 176 |
a = -a; |
| 177 |
b = -b; |
| 178 |
c = -c; |
| 179 |
} |
| 180 |
if (a >= mrpt::math::getEpsilon()) |
| 181 |
{ |
| 182 |
double delta = square(b) - a * c; |
| 183 |
if (delta == 0) |
| 184 |
return (t = -b / a) >= 0; |
| 185 |
else if (delta >= 0) |
| 186 |
{ |
| 187 |
delta = sqrt(delta); |
| 188 |
if (-b - delta > 0) |
| 189 |
{ |
| 190 |
t = (-b - delta) / a; |
| 191 |
return true; |
| 192 |
} |
| 193 |
else if (-b + delta > 0) |
| 194 |
{ |
| 195 |
t = (-b + delta) / a; |
| 196 |
return true; |
| 197 |
} // else return false; Both solutions are negative |
| 198 |
} // else return false; Both solutions are complex |
| 199 |
} |
| 200 |
else if (std::abs(b) >= mrpt::math::getEpsilon()) |
| 201 |
{ |
| 202 |
t = -c / (b + b); |
| 203 |
return t >= 0; |
| 204 |
} // else return false; This actually isn't an equation |
| 205 |
return false; |
| 206 |
} |
| 207 |
|
| 208 |
bool CCylinder::traceRay(const mrpt::poses::CPose3D& o, double& dist) const |
| 209 |
{ |
| 210 |
TLine3D lin; |
| 211 |
mrpt::math::createFromPoseX((o - getCPose()).asTPose(), lin); |
| 212 |
lin.unitarize(); // By adding this line, distance from any point of the |
| 213 |
|
| 214 |
const float zz = d2f(lin.pBase.z); |
| 215 |
|
| 216 |
// line to its base is exactly equal to the "t". |
| 217 |
if (std::abs(lin.director[2]) < getEpsilon()) |
| 218 |
{ |
| 219 |
if (!reachesHeight(zz)) return false; |
| 220 |
float r; |
| 221 |
return getRadius(zz, r) ? solveEqn( |
| 222 |
square(lin.director[0]) + square(lin.director[1]), |
| 223 |
lin.director[0] * lin.pBase.x + lin.director[1] * lin.pBase.y, |
| 224 |
square(lin.pBase.x) + square(lin.pBase.y) - square(r), dist) |
| 225 |
: false; |
| 226 |
} |
| 227 |
bool fnd = false; |
| 228 |
double nDist, tZ0; |
| 229 |
if (m_hasBottomBase && (tZ0 = -lin.pBase.z / lin.director[2]) > 0) |
| 230 |
{ |
| 231 |
nDist = sqrt( |
| 232 |
square(lin.pBase.x + tZ0 * lin.director[0]) + square(lin.pBase.y + tZ0 * lin.director[1])); |
| 233 |
if (nDist <= m_baseRadius) |
| 234 |
{ |
| 235 |
fnd = true; |
| 236 |
dist = tZ0; |
| 237 |
} |
| 238 |
} |
| 239 |
if (m_hasTopBase) |
| 240 |
{ |
| 241 |
tZ0 = (m_height - lin.pBase.z) / lin.director[2]; |
| 242 |
if (tZ0 > 0 && (!fnd || tZ0 < dist)) |
| 243 |
{ |
| 244 |
nDist = sqrt( |
| 245 |
square(lin.pBase.x + tZ0 * lin.director[0]) + |
| 246 |
square(lin.pBase.y + tZ0 * lin.director[1])); |
| 247 |
if (nDist <= m_topRadius) |
| 248 |
{ |
| 249 |
fnd = true; |
| 250 |
dist = tZ0; |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
if (m_baseRadius == m_topRadius) |
| 255 |
{ |
| 256 |
if (solveEqn( |
| 257 |
square(lin.director[0]) + square(lin.director[1]), |
| 258 |
lin.director[0] * lin.pBase.x + lin.director[1] * lin.pBase.y, |
| 259 |
square(lin.pBase.x) + square(lin.pBase.y) - square(m_baseRadius), nDist)) |
| 260 |
if ((!fnd || nDist < dist) && reachesHeight(lin.pBase.z + nDist * lin.director[2])) |
| 261 |
{ |
| 262 |
dist = nDist; |
| 263 |
fnd = true; |
| 264 |
} |
| 265 |
} |
| 266 |
else |
| 267 |
{ |
| 268 |
double slope = (m_topRadius - m_baseRadius) / m_height; |
| 269 |
if (solveEqn( |
| 270 |
square(lin.director[0]) + square(lin.director[1]) - square(lin.director[2] * slope), |
| 271 |
lin.pBase.x * lin.director[0] + lin.pBase.y * lin.director[1] - |
| 272 |
(m_baseRadius + slope * lin.pBase.z) * slope * lin.director[2], |
| 273 |
square(lin.pBase.x) + square(lin.pBase.y) - square(m_baseRadius + slope * lin.pBase.z), |
| 274 |
nDist)) |
| 275 |
if ((!fnd || nDist < dist) && reachesHeight(lin.pBase.z + nDist * lin.director[2])) |
| 276 |
{ |
| 277 |
dist = nDist; |
| 278 |
fnd = true; |
| 279 |
} |
| 280 |
} |
| 281 |
return fnd; |
| 282 |
} |
| 283 |
|
| 284 |
auto CCylinder::internalBoundingBoxLocal() const -> mrpt::math::TBoundingBoxf |
| 285 |
{ |
| 286 |
const float R = std::max(m_baseRadius, m_topRadius); |
| 287 |
return mrpt::math::TBoundingBoxf::FromUnsortedPoints({-R, -R, 0}, {R, R, m_height}); |
| 288 |
} |