| 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/CSphere.h> |
| 13 |
#include <mrpt/serialization/CArchive.h> |
| 14 |
|
| 15 |
using namespace mrpt; |
| 16 |
using namespace mrpt::opengl; |
| 17 |
using namespace mrpt::poses; |
| 18 |
using namespace mrpt::math; |
| 19 |
using namespace std; |
| 20 |
|
| 21 |
IMPLEMENTS_SERIALIZABLE(CSphere, CRenderizable, mrpt::opengl) |
| 22 |
|
| 23 |
uint8_t CSphere::serializeGetVersion() const { return 3; } |
| 24 |
void CSphere::serializeTo(mrpt::serialization::CArchive& out) const |
| 25 |
{ |
| 26 |
writeToStreamRender(out); |
| 27 |
out << m_radius; |
| 28 |
out << (uint32_t)m_nDivs; |
| 29 |
} |
| 30 |
void CSphere::serializeFrom(mrpt::serialization::CArchive& in, uint8_t version) |
| 31 |
{ |
| 32 |
switch (version) |
| 33 |
{ |
| 34 |
case 0: |
| 35 |
case 1: |
| 36 |
case 2: |
| 37 |
case 3: |
| 38 |
{ |
| 39 |
readFromStreamRender(in); |
| 40 |
in >> m_radius; |
| 41 |
m_nDivs = in.ReadAs<uint32_t>(); |
| 42 |
if (version < 3) |
| 43 |
{ |
| 44 |
in.ReadAs<uint32_t>(); // dummy old value, now unused |
| 45 |
} |
| 46 |
if (version == 1) |
| 47 |
{ |
| 48 |
bool keepRadiusIndependentEyeDistance; |
| 49 |
in >> keepRadiusIndependentEyeDistance; |
| 50 |
} |
| 51 |
|
| 52 |
regenerateBaseParams(); |
| 53 |
} |
| 54 |
break; |
| 55 |
default: |
| 56 |
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version); |
| 57 |
}; |
| 58 |
CRenderizable::notifyChange(); |
| 59 |
} |
| 60 |
|
| 61 |
bool CSphere::traceRay(const mrpt::poses::CPose3D& o, double& dist) const |
| 62 |
{ |
| 63 |
// We need to find the points of the sphere which collide with the laser |
| 64 |
// beam. |
| 65 |
// The sphere's equation is invariant to rotations (but not to |
| 66 |
// translations), and we can take advantage of this; |
| 67 |
// we'll simply transform the center and then compute the beam's points |
| 68 |
// whose distance to that transformed point |
| 69 |
// equals the sphere's radius. |
| 70 |
|
| 71 |
CPose3D transf = getCPose() - o; |
| 72 |
double x = transf.x(), y = transf.y(), z = transf.z(); |
| 73 |
double r2 = m_radius * m_radius; |
| 74 |
double dyz = y * y + z * z; |
| 75 |
if (dyz > r2) return false; |
| 76 |
double dx = sqrt(r2 - dyz); |
| 77 |
if (x - dx >= 0) |
| 78 |
{ |
| 79 |
dist = x - dx; |
| 80 |
return true; |
| 81 |
} |
| 82 |
else if (x + dx >= 0) |
| 83 |
{ |
| 84 |
dist = x + dx; |
| 85 |
return true; |
| 86 |
} |
| 87 |
else |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
auto CSphere::internalBoundingBoxLocal() const -> mrpt::math::TBoundingBoxf |
| 92 |
{ |
| 93 |
const float R = m_radius; |
| 94 |
return { |
| 95 |
{-R, -R, -R}, |
| 96 |
{ R, R, R} |
| 97 |
}; |
| 98 |
} |
| 99 |
|
| 100 |
void CSphere::renderUpdateBuffers() const |
| 101 |
{ |
| 102 |
const_cast<CSphere*>(this)->regenerateBaseParams(); |
| 103 |
BASE::renderUpdateBuffers(); |
| 104 |
} |