| 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/geometry.h> |
| 13 |
#include <mrpt/opengl/CArrow.h> |
| 14 |
#include <mrpt/serialization/CArchive.h> |
| 15 |
#include <mrpt/serialization/CSchemeArchiveBase.h> |
| 16 |
|
| 17 |
#include <memory> |
| 18 |
|
| 19 |
using namespace mrpt; |
| 20 |
using namespace mrpt::opengl; |
| 21 |
using namespace mrpt::math; |
| 22 |
using namespace std; |
| 23 |
|
| 24 |
IMPLEMENTS_SERIALIZABLE(CArrow, CRenderizableShaderTriangles, mrpt::opengl) |
| 25 |
|
| 26 |
void CArrow::onUpdateBuffers_Triangles() |
| 27 |
{ |
| 28 |
using P3f = mrpt::math::TPoint3Df; |
| 29 |
using V3f = mrpt::math::TVector3Df; |
| 30 |
|
| 31 |
std::unique_lock<std::shared_mutex> trisWriteLock( |
| 32 |
CRenderizableShaderTriangles::m_trianglesMtx.data); |
| 33 |
auto& tris = CRenderizableShaderTriangles::m_triangles; |
| 34 |
|
| 35 |
tris.clear(); |
| 36 |
|
| 37 |
// Compute the XYZ local frame of reference for the arrow: |
| 38 |
// XY radial, Z from Point #0 -> point #1: |
| 39 |
const P3f p0(m_x0, m_y0, m_z0), p1(m_x1, m_y1, m_z1); |
| 40 |
auto p = p1 - p0; |
| 41 |
const float P10_norm = p.norm(); |
| 42 |
ASSERT_GT_(P10_norm, .0f); |
| 43 |
// Unit vector: |
| 44 |
p *= (1.0f / P10_norm); |
| 45 |
|
| 46 |
setLocalRepresentativePoint((p0 + p1) * 0.5f); |
| 47 |
|
| 48 |
// each column is a unit vector: |
| 49 |
const CMatrixDouble44 HM = |
| 50 |
mrpt::math::generateAxisBaseFromDirectionAndAxis(p, 2 /* provided vector is "z"*/); |
| 51 |
|
| 52 |
// Transformation: |
| 53 |
const mrpt::poses::CPose3D T(HM.blockCopy<3, 3>(0, 0), mrpt::math::TPoint3D(m_x0, m_y0, m_z0)); |
| 54 |
|
| 55 |
// precomputed table: |
| 56 |
ASSERT_GT_(m_slices, 2); |
| 57 |
|
| 58 |
const float dAng = 2 * M_PIf / m_slices; |
| 59 |
float a = 0; |
| 60 |
// unit cc points: cos(ang),sin(ang) |
| 61 |
std::vector<mrpt::math::TPoint2Df> cc(m_slices); |
| 62 |
for (unsigned int i = 0; i < m_slices; i++, a += dAng) |
| 63 |
{ |
| 64 |
cc[i].x = cos(a); |
| 65 |
cc[i].y = sin(a); |
| 66 |
} |
| 67 |
|
| 68 |
ASSERT_GE_(m_headRatio, .0f); |
| 69 |
ASSERT_LE_(m_headRatio, 1.0f); |
| 70 |
|
| 71 |
const float r0 = m_smallRadius, r1 = m_largeRadius, h0 = P10_norm * (1.0f - m_headRatio), |
| 72 |
h1 = P10_norm; |
| 73 |
|
| 74 |
const float wall_tilt = 0; |
| 75 |
const float coswt = std::cos(wall_tilt), sinwt = std::sin(wall_tilt); |
| 76 |
|
| 77 |
const float head_tilt = std::atan2(r1, P10_norm * m_headRatio); |
| 78 |
const float cosht = std::cos(head_tilt), sinht = std::sin(head_tilt); |
| 79 |
|
| 80 |
// cylinder walls: |
| 81 |
for (unsigned int i = 0; i < m_slices; i++) |
| 82 |
{ |
| 83 |
const auto ip = (i + 1) % m_slices; |
| 84 |
|
| 85 |
tris.emplace_back( |
| 86 |
// Points: |
| 87 |
T.composePoint(P3f(r0 * cc[i].x, r0 * cc[i].y, .0f)), |
| 88 |
T.composePoint(P3f(r0 * cc[ip].x, r0 * cc[ip].y, .0f)), |
| 89 |
T.composePoint(P3f(r0 * cc[i].x, r0 * cc[i].y, h0)), |
| 90 |
// Normals: |
| 91 |
T.rotateVector(V3f(coswt * cc[i].x, coswt * cc[i].y, sinwt)), |
| 92 |
T.rotateVector(V3f(coswt * cc[ip].x, coswt * cc[ip].y, sinwt)), |
| 93 |
T.rotateVector(V3f(coswt * cc[i].x, coswt * cc[i].y, sinwt))); |
| 94 |
|
| 95 |
tris.emplace_back( |
| 96 |
// Points: |
| 97 |
T.composePoint(P3f(r0 * cc[ip].x, r0 * cc[ip].y, .0f)), |
| 98 |
T.composePoint(P3f(r0 * cc[ip].x, r0 * cc[ip].y, h0)), |
| 99 |
T.composePoint(P3f(r0 * cc[i].x, r0 * cc[i].y, h0)), |
| 100 |
// Normals: |
| 101 |
T.rotateVector(V3f(coswt * cc[ip].x, coswt * cc[ip].y, sinwt)), |
| 102 |
T.rotateVector(V3f(coswt * cc[ip].x, coswt * cc[ip].y, sinwt)), |
| 103 |
T.rotateVector(V3f(coswt * cc[i].x, coswt * cc[i].y, sinwt))); |
| 104 |
} |
| 105 |
|
| 106 |
// top cone: |
| 107 |
if (m_headRatio > 0) |
| 108 |
{ |
| 109 |
for (unsigned int i = 0; i < m_slices; i++) |
| 110 |
{ |
| 111 |
const auto ip = (i + 1) % m_slices; |
| 112 |
tris.emplace_back( |
| 113 |
// Points: |
| 114 |
T.composePoint(P3f(r1 * cc[i].x, r1 * cc[i].y, h0)), |
| 115 |
T.composePoint(P3f(r1 * cc[ip].x, r1 * cc[ip].y, h0)), T.composePoint(P3f(.0f, .0f, h1)), |
| 116 |
// Normals: |
| 117 |
T.rotateVector(V3f(cosht * cc[i].x, cosht * cc[i].y, sinht)), |
| 118 |
T.rotateVector(V3f(cosht * cc[ip].x, cosht * cc[ip].y, sinht)), |
| 119 |
T.rotateVector(V3f(0, 0, 1))); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
// All faces, same color: |
| 124 |
for (auto& t : tris) t.setColor(getColor_u8()); |
| 125 |
} |
| 126 |
|
| 127 |
uint8_t CArrow::serializeGetVersion() const { return 3; } |
| 128 |
void CArrow::serializeTo(mrpt::serialization::CArchive& out) const |
| 129 |
{ |
| 130 |
writeToStreamRender(out); |
| 131 |
out << m_x0 << m_y0 << m_z0; |
| 132 |
out << m_x1 << m_y1 << m_z1; |
| 133 |
out << m_headRatio << m_smallRadius << m_largeRadius; |
| 134 |
out << m_slices; |
| 135 |
CRenderizableShaderTriangles::params_serialize(out); // v3 |
| 136 |
} |
| 137 |
|
| 138 |
void CArrow::serializeFrom(mrpt::serialization::CArchive& in, uint8_t version) |
| 139 |
{ |
| 140 |
switch (version) |
| 141 |
{ |
| 142 |
case 0: |
| 143 |
case 1: |
| 144 |
case 2: |
| 145 |
case 3: |
| 146 |
{ |
| 147 |
readFromStreamRender(in); |
| 148 |
in >> m_x0 >> m_y0 >> m_z0; |
| 149 |
in >> m_x1 >> m_y1 >> m_z1; |
| 150 |
in >> m_headRatio >> m_smallRadius >> m_largeRadius; |
| 151 |
if (version == 1) |
| 152 |
{ |
| 153 |
float arrow_roll, arrow_pitch, arrow_yaw; |
| 154 |
in >> arrow_roll >> arrow_pitch >> arrow_yaw; |
| 155 |
} |
| 156 |
if (version >= 2) in >> m_slices; |
| 157 |
if (version >= 3) CRenderizableShaderTriangles::params_deserialize(in); |
| 158 |
} |
| 159 |
break; |
| 160 |
default: |
| 161 |
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version); |
| 162 |
}; |
| 163 |
CRenderizable::notifyChange(); |
| 164 |
} |
| 165 |
|
| 166 |
void CArrow::serializeTo(mrpt::serialization::CSchemeArchiveBase& out) const |
| 167 |
{ |
| 168 |
SCHEMA_SERIALIZE_DATATYPE_VERSION(1); |
| 169 |
out["x0"] = m_x0; |
| 170 |
out["y0"] = m_y0; |
| 171 |
out["z0"] = m_z0; |
| 172 |
out["x1"] = m_x1; |
| 173 |
out["y1"] = m_y1; |
| 174 |
out["z1"] = m_z1; |
| 175 |
out["headRatio"] = m_headRatio; |
| 176 |
out["smallRadius"] = m_smallRadius; |
| 177 |
out["largeRadius"] = m_largeRadius; |
| 178 |
out["slices"] = m_slices; |
| 179 |
} |
| 180 |
|
| 181 |
void CArrow::serializeFrom(mrpt::serialization::CSchemeArchiveBase& in) |
| 182 |
{ |
| 183 |
uint8_t version; |
| 184 |
SCHEMA_DESERIALIZE_DATATYPE_VERSION(); |
| 185 |
switch (version) |
| 186 |
{ |
| 187 |
case 1: |
| 188 |
{ |
| 189 |
m_x0 = static_cast<float>(in["x0"]); |
| 190 |
m_y0 = static_cast<float>(in["y0"]); |
| 191 |
m_z0 = static_cast<float>(in["z0"]); |
| 192 |
m_x1 = static_cast<float>(in["x1"]); |
| 193 |
m_y1 = static_cast<float>(in["y1"]); |
| 194 |
m_z1 = static_cast<float>(in["z1"]); |
| 195 |
m_headRatio = static_cast<float>(in["headRatio"]); |
| 196 |
m_smallRadius = static_cast<float>(in["smallRadius"]); |
| 197 |
m_largeRadius = static_cast<float>(in["largeRadius"]); |
| 198 |
m_slices = static_cast<unsigned int>(in["slices"]); |
| 199 |
} |
| 200 |
break; |
| 201 |
default: |
| 202 |
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version); |
| 203 |
} |
| 204 |
} |
| 205 |
auto CArrow::internalBoundingBoxLocal() const -> mrpt::math::TBoundingBoxf |
| 206 |
{ |
| 207 |
return mrpt::math::TBoundingBoxf::FromUnsortedPoints( |
| 208 |
{std::min(m_x0, m_x1), std::min(m_y0, m_y1), std::min(m_z0, m_z1)}, |
| 209 |
{std::max(m_x0, m_x1), std::max(m_y0, m_y1), std::max(m_z0, m_z1)}); |
| 210 |
} |