| 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/containers/yaml.h> |
| 13 |
#include <mrpt/math/TPose3D.h> |
| 14 |
#include <mrpt/math/utils.h> |
| 15 |
#include <mrpt/opengl/CRenderizable.h> // Include these before windows.h!! |
| 16 |
#include <mrpt/opengl/CText.h> |
| 17 |
#include <mrpt/opengl/opengl_api.h> |
| 18 |
#include <mrpt/poses/CPoint2D.h> |
| 19 |
#include <mrpt/poses/CPoint3D.h> |
| 20 |
#include <mrpt/poses/CPose3D.h> |
| 21 |
#include <mrpt/serialization/CArchive.h> |
| 22 |
|
| 23 |
#include <mutex> |
| 24 |
|
| 25 |
using namespace std; |
| 26 |
using namespace mrpt; |
| 27 |
using namespace mrpt::opengl; |
| 28 |
|
| 29 |
IMPLEMENTS_VIRTUAL_SERIALIZABLE(CRenderizable, CSerializable, mrpt::opengl) |
| 30 |
|
| 31 |
// Destructor: |
| 32 |
CRenderizable::~CRenderizable() = default; |
| 33 |
|
| 34 |
void CRenderizable::writeToStreamRender(mrpt::serialization::CArchive& out) const |
| 35 |
{ |
| 36 |
std::shared_lock<std::shared_mutex> lckRead(m_stateMtx.data); |
| 37 |
const auto& _ = m_state; |
| 38 |
|
| 39 |
// MRPT 0.9.5 svn 2774 (Dec 14th 2011): |
| 40 |
// Added support of versioning at this level of serialization too. |
| 41 |
// Should have been done from the beginning, terrible mistake on my part. |
| 42 |
// Now, the only solution is something as ugly as this: |
| 43 |
// |
| 44 |
// For reference: In the past this started as: |
| 45 |
// out << m_name << (float)(m_color.R) << (float)(m_color.G) << |
| 46 |
// (float)(m_color.B) << (float)(m_color.A); |
| 47 |
// ... |
| 48 |
|
| 49 |
// can't be >31 (but it would be mad getting to that situation!) |
| 50 |
const uint8_t serialization_version = 2; |
| 51 |
|
| 52 |
const bool all_scales_equal = (_.scale_x == _.scale_y && _.scale_z == _.scale_x); |
| 53 |
const bool all_scales_unity = (all_scales_equal && _.scale_x == 1.0f); |
| 54 |
|
| 55 |
const uint8_t magic_signature[2] = { |
| 56 |
0xFF, |
| 57 |
// bit7: fixed to 1 to mark this new header format |
| 58 |
// bit6: whether the 3 scale{x,y,z} are equal to 1.0 |
| 59 |
// bit5: whether the 3 scale{x,y,z} are equal to each other |
| 60 |
static_cast<uint8_t>( |
| 61 |
serialization_version | (all_scales_unity ? 0xC0 : (all_scales_equal ? 0xA0 : 0x80)))}; |
| 62 |
|
| 63 |
out << magic_signature[0] << magic_signature[1]; |
| 64 |
|
| 65 |
// "m_name" |
| 66 |
const auto nameLen = static_cast<uint16_t>(_.name.size()); |
| 67 |
out << nameLen; |
| 68 |
if (nameLen) out.WriteBuffer(_.name.c_str(), _.name.size()); |
| 69 |
|
| 70 |
// Color, as u8: |
| 71 |
out << _.color.R << _.color.G << _.color.B << _.color.A; |
| 72 |
|
| 73 |
// the rest of fields: |
| 74 |
out << (float)_.pose.x() << (float)_.pose.y() << (float)_.pose.z() << (float)_.pose.yaw() |
| 75 |
<< (float)_.pose.pitch() << (float)_.pose.roll(); |
| 76 |
|
| 77 |
if (!all_scales_unity) |
| 78 |
{ |
| 79 |
if (all_scales_equal) |
| 80 |
out << _.scale_x; |
| 81 |
else |
| 82 |
out << _.scale_x << _.scale_y << _.scale_z; |
| 83 |
} |
| 84 |
|
| 85 |
out << _.show_name << _.visible; |
| 86 |
out << _.representativePoint; // v1 |
| 87 |
out << _.materialShininess << _.castShadows; // v2 |
| 88 |
} |
| 89 |
|
| 90 |
void CRenderizable::readFromStreamRender(mrpt::serialization::CArchive& in) |
| 91 |
{ |
| 92 |
// MRPT 0.9.5 svn 2774 (Dec 14th 2011): |
| 93 |
// See comments in CRenderizable::writeToStreamRender() for the employed |
| 94 |
// serialization mechanism. |
| 95 |
// |
| 96 |
// MRPT 1.9.9 (Aug 2019): Was |
| 97 |
// union { |
| 98 |
// uint8_t magic_signature[2 + 2]; |
| 99 |
// (the extra 4 bytes will be used only for the old format) |
| 100 |
// uint32_t magic_signature_uint32; |
| 101 |
// So we can interpret the 4bytes above as a 32bit number cleanly. |
| 102 |
// }; |
| 103 |
// Get rid of the "old" serialization format to avoid using "union". |
| 104 |
|
| 105 |
uint8_t magic_signature[2]; |
| 106 |
|
| 107 |
in >> magic_signature[0] >> magic_signature[1]; |
| 108 |
|
| 109 |
const bool is_new_format = (magic_signature[0] == 0xFF) && ((magic_signature[1] & 0x80) != 0); |
| 110 |
|
| 111 |
std::unique_lock<std::shared_mutex> lckWrite(m_stateMtx.data); |
| 112 |
auto& _ = m_state; |
| 113 |
|
| 114 |
if (is_new_format) |
| 115 |
{ |
| 116 |
// NEW FORMAT: |
| 117 |
uint8_t serialization_version = (magic_signature[1] & 0x1F); |
| 118 |
const bool all_scales_unity = ((magic_signature[1] & 0x40) != 0); |
| 119 |
const bool all_scales_equal_but_not_unity = ((magic_signature[1] & 0x20) != 0); |
| 120 |
|
| 121 |
switch (serialization_version) |
| 122 |
{ |
| 123 |
case 0: |
| 124 |
case 1: |
| 125 |
case 2: |
| 126 |
{ |
| 127 |
// "m_name" |
| 128 |
uint16_t nameLen; |
| 129 |
in >> nameLen; |
| 130 |
_.name.resize(nameLen); |
| 131 |
if (nameLen) in.ReadBuffer((void*)(&_.name[0]), _.name.size()); |
| 132 |
|
| 133 |
// Color, as u8: |
| 134 |
in >> _.color.R >> _.color.G >> _.color.B >> _.color.A; |
| 135 |
|
| 136 |
// the rest of fields: |
| 137 |
float x, y, z, yaw, pitch, roll; |
| 138 |
in >> x >> y >> z >> yaw >> pitch >> roll; |
| 139 |
_.pose.x(x); |
| 140 |
_.pose.y(y); |
| 141 |
_.pose.z(z); |
| 142 |
_.pose.setYawPitchRoll(yaw, pitch, roll); |
| 143 |
|
| 144 |
if (all_scales_unity) |
| 145 |
_.scale_x = _.scale_y = _.scale_z = 1; |
| 146 |
else |
| 147 |
{ |
| 148 |
if (all_scales_equal_but_not_unity) |
| 149 |
{ |
| 150 |
in >> _.scale_x; |
| 151 |
_.scale_y = _.scale_z = _.scale_x; |
| 152 |
} |
| 153 |
else |
| 154 |
in >> _.scale_x >> _.scale_y >> _.scale_z; |
| 155 |
} |
| 156 |
|
| 157 |
in >> _.show_name >> _.visible; |
| 158 |
if (serialization_version >= 1) |
| 159 |
in >> _.representativePoint; |
| 160 |
else |
| 161 |
_.representativePoint = mrpt::math::TPoint3Df(0, 0, 0); |
| 162 |
|
| 163 |
if (serialization_version >= 2) |
| 164 |
in >> _.materialShininess >> _.castShadows; |
| 165 |
else |
| 166 |
{ |
| 167 |
// default |
| 168 |
_.materialShininess = 0.2f; |
| 169 |
_.castShadows = true; |
| 170 |
} |
| 171 |
} |
| 172 |
break; |
| 173 |
default: |
| 174 |
THROW_EXCEPTION_FMT( |
| 175 |
"Can't parse CRenderizable standard data field: corrupt " |
| 176 |
"data stream or format in a newer MRPT format? " |
| 177 |
"(serialization version=%u)", |
| 178 |
static_cast<unsigned int>(serialization_version)); |
| 179 |
}; |
| 180 |
} |
| 181 |
else |
| 182 |
{ |
| 183 |
// OLD FORMAT: |
| 184 |
THROW_EXCEPTION("Serialized object is too old! Unsupported format."); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
/*-------------------------------------------------------------- |
| 189 |
setPose |
| 190 |
---------------------------------------------------------------*/ |
| 191 |
CRenderizable& CRenderizable::setPose(const mrpt::poses::CPose3D& o) |
| 192 |
{ |
| 193 |
m_stateMtx.data.lock(); |
| 194 |
m_state.pose = o; |
| 195 |
m_stateMtx.data.unlock(); |
| 196 |
return *this; |
| 197 |
} |
| 198 |
CRenderizable& CRenderizable::setPose(const mrpt::poses::CPose2D& o) |
| 199 |
{ |
| 200 |
return setPose(mrpt::poses::CPose3D(o)); |
| 201 |
} |
| 202 |
CRenderizable& CRenderizable::setPose(const mrpt::math::TPose3D& o) |
| 203 |
{ |
| 204 |
return setPose(mrpt::poses::CPose3D(o)); |
| 205 |
} |
| 206 |
CRenderizable& CRenderizable::setPose(const mrpt::math::TPose2D& o) |
| 207 |
{ |
| 208 |
return setPose(mrpt::poses::CPose3D(o)); |
| 209 |
} |
| 210 |
|
| 211 |
CRenderizable& CRenderizable::setPose(const mrpt::poses::CPoint3D& o) |
| 212 |
{ |
| 213 |
m_stateMtx.data.lock(); |
| 214 |
m_state.pose.setFromValues(o.x(), o.y(), o.z(), 0, 0, 0); |
| 215 |
m_stateMtx.data.unlock(); |
| 216 |
return *this; |
| 217 |
} |
| 218 |
CRenderizable& CRenderizable::setPose(const mrpt::poses::CPoint2D& o) |
| 219 |
{ |
| 220 |
m_stateMtx.data.lock(); |
| 221 |
m_state.pose.setFromValues(o.x(), o.y(), 0, 0, 0, 0); |
| 222 |
m_stateMtx.data.unlock(); |
| 223 |
return *this; |
| 224 |
} |
| 225 |
|
| 226 |
mrpt::math::TPose3D CRenderizable::getPose() const |
| 227 |
{ |
| 228 |
std::shared_lock<std::shared_mutex> lckRead(m_stateMtx.data); |
| 229 |
return m_state.pose.asTPose(); |
| 230 |
} |
| 231 |
bool CRenderizable::traceRay(const mrpt::poses::CPose3D&, double&) const { return false; } |
| 232 |
|
| 233 |
CRenderizable& CRenderizable::setColor_u8(const mrpt::img::TColor& c) |
| 234 |
{ |
| 235 |
std::unique_lock<std::shared_mutex> lckWrite(m_stateMtx.data); |
| 236 |
m_state.color.R = c.R; |
| 237 |
m_state.color.G = c.G; |
| 238 |
m_state.color.B = c.B; |
| 239 |
m_state.color.A = c.A; |
| 240 |
notifyChange(); |
| 241 |
return *this; |
| 242 |
} |
| 243 |
|
| 244 |
CText& CRenderizable::labelObject() const |
| 245 |
{ |
| 246 |
if (!m_label_obj) |
| 247 |
{ |
| 248 |
m_label_obj = std::make_shared<mrpt::opengl::CText>(); |
| 249 |
m_label_obj->setString(getName()); |
| 250 |
} |
| 251 |
return *m_label_obj; |
| 252 |
} |
| 253 |
|
| 254 |
void CRenderizable::toYAMLMap(mrpt::containers::yaml& propertiesMap) const |
| 255 |
{ |
| 256 |
propertiesMap["name"] = getName(); |
| 257 |
propertiesMap["show_name"] = isShowNameEnabled(); |
| 258 |
propertiesMap["location"] = getPose().asString(); |
| 259 |
propertiesMap["visible"] = isVisible(); |
| 260 |
} |
| 261 |
|
| 262 |
#ifdef MRPT_OPENGL_PROFILER |
| 263 |
mrpt::system::CTimeLogger& mrpt::opengl::opengl_profiler() |
| 264 |
{ |
| 265 |
static mrpt::system::CTimeLogger tl; |
| 266 |
return tl; |
| 267 |
} |
| 268 |
#endif |
| 269 |
|
| 270 |
auto CRenderizable::getBoundingBoxLocalf() const -> mrpt::math::TBoundingBoxf |
| 271 |
{ |
| 272 |
if (!m_cachedLocalBBox) |
| 273 |
{ |
| 274 |
std::unique_lock<std::shared_mutex> lckWrite(m_outdatedStateMtx.data); |
| 275 |
m_cachedLocalBBox = internalBoundingBoxLocal(); |
| 276 |
return m_cachedLocalBBox.value(); |
| 277 |
} |
| 278 |
else |
| 279 |
{ |
| 280 |
std::shared_lock<std::shared_mutex> lckWrite(m_outdatedStateMtx.data); |
| 281 |
return m_cachedLocalBBox.value(); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
auto CRenderizable::getBoundingBoxLocal() const -> mrpt::math::TBoundingBox |
| 286 |
{ |
| 287 |
const auto& bb = getBoundingBoxLocalf(); |
| 288 |
return {bb.min.cast<double>(), bb.max.cast<double>()}; |
| 289 |
} |