| 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/CSetOfObjects.h> |
| 13 |
#include <mrpt/opengl/CTexturedPlane.h> |
| 14 |
#include <mrpt/opengl/opengl_api.h> |
| 15 |
#include <mrpt/serialization/CArchive.h> |
| 16 |
|
| 17 |
#include <algorithm> |
| 18 |
|
| 19 |
using namespace mrpt; |
| 20 |
using namespace mrpt::opengl; |
| 21 |
using namespace mrpt::poses; |
| 22 |
using namespace mrpt::math; |
| 23 |
using namespace std; |
| 24 |
|
| 25 |
#include <mrpt/serialization/metaprogramming_serialization.h> |
| 26 |
using namespace mrpt::serialization::metaprogramming; |
| 27 |
|
| 28 |
IMPLEMENTS_SERIALIZABLE(CSetOfObjects, CRenderizable, mrpt::opengl) |
| 29 |
|
| 30 |
void CSetOfObjects::clear() { m_objects.clear(); } |
| 31 |
|
| 32 |
void CSetOfObjects::renderUpdateBuffers() const |
| 33 |
{ |
| 34 |
// Do nothing: |
| 35 |
} |
| 36 |
|
| 37 |
void CSetOfObjects::render(const RenderContext& rc) const |
| 38 |
{ |
| 39 |
// Do nothing: the enqueueForRenderRecursive() does the actual job. |
| 40 |
} |
| 41 |
|
| 42 |
void CSetOfObjects::enqueueForRenderRecursive( |
| 43 |
const mrpt::opengl::TRenderMatrices& state, |
| 44 |
RenderQueue& rq, |
| 45 |
bool wholeInView, |
| 46 |
bool is1stShadowMapPass) const |
| 47 |
{ |
| 48 |
mrpt::opengl::enqueueForRendering(m_objects, state, rq, wholeInView, is1stShadowMapPass); |
| 49 |
} |
| 50 |
|
| 51 |
uint8_t CSetOfObjects::serializeGetVersion() const { return 0; } |
| 52 |
void CSetOfObjects::serializeTo(mrpt::serialization::CArchive& out) const |
| 53 |
{ |
| 54 |
writeToStreamRender(out); |
| 55 |
|
| 56 |
out.WriteAs<uint32_t>(m_objects.size()); |
| 57 |
for (const auto& m_object : m_objects) out << *m_object; |
| 58 |
} |
| 59 |
|
| 60 |
/*--------------------------------------------------------------- |
| 61 |
Implements the reading from a CStream capability of |
| 62 |
CSerializable objects |
| 63 |
---------------------------------------------------------------*/ |
| 64 |
void CSetOfObjects::serializeFrom(mrpt::serialization::CArchive& in, uint8_t version) |
| 65 |
{ |
| 66 |
switch (version) |
| 67 |
{ |
| 68 |
case 0: |
| 69 |
{ |
| 70 |
readFromStreamRender(in); |
| 71 |
|
| 72 |
uint32_t n; |
| 73 |
in >> n; |
| 74 |
clear(); |
| 75 |
m_objects.resize(n); |
| 76 |
|
| 77 |
for_each(m_objects.begin(), m_objects.end(), ObjectReadFromStream(&in)); |
| 78 |
} |
| 79 |
break; |
| 80 |
default: |
| 81 |
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version); |
| 82 |
}; |
| 83 |
} |
| 84 |
|
| 85 |
void CSetOfObjects::insert(const CRenderizable::Ptr& newObject) |
| 86 |
{ |
| 87 |
ASSERTMSG_(newObject.get() != this, "Error: Trying to insert container into itself!"); |
| 88 |
m_objects.push_back(newObject); |
| 89 |
} |
| 90 |
|
| 91 |
void CSetOfObjects::dumpListOfObjects(std::vector<std::string>& lst) const |
| 92 |
{ |
| 93 |
for (auto& o : m_objects) |
| 94 |
{ |
| 95 |
if (!o) continue; |
| 96 |
// Single obj: |
| 97 |
string s(o->GetRuntimeClass()->className); |
| 98 |
if (!o->getName().empty()) s += string(" (") + o->getName() + string(")"); |
| 99 |
lst.emplace_back(s); |
| 100 |
|
| 101 |
if (o->GetRuntimeClass() == CLASS_ID_NAMESPACE(CSetOfObjects, mrpt::opengl)) |
| 102 |
{ |
| 103 |
auto* objs = dynamic_cast<CSetOfObjects*>(o.get()); |
| 104 |
|
| 105 |
std::vector<std::string> auxLst; |
| 106 |
objs->dumpListOfObjects(auxLst); |
| 107 |
for (const auto& i : auxLst) lst.emplace_back(string(" ") + i); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
mrpt::containers::yaml CSetOfObjects::asYAML() const |
| 113 |
{ |
| 114 |
mrpt::containers::yaml d = mrpt::containers::yaml::Sequence(); |
| 115 |
|
| 116 |
d.asSequence().resize(m_objects.size()); |
| 117 |
|
| 118 |
for (uint32_t i = 0; i < m_objects.size(); i++) |
| 119 |
{ |
| 120 |
const auto obj = m_objects.at(i); |
| 121 |
mrpt::containers::yaml de = mrpt::containers::yaml::Map(); |
| 122 |
|
| 123 |
// class-specific properties: |
| 124 |
obj->toYAMLMap(de); |
| 125 |
|
| 126 |
de["index"] = i; // type for "i" must be a stdint type |
| 127 |
if (!obj) |
| 128 |
{ |
| 129 |
de["class"] = "nullptr"; |
| 130 |
continue; |
| 131 |
} |
| 132 |
de["class"] = obj->GetRuntimeClass()->className; |
| 133 |
|
| 134 |
// Single obj: |
| 135 |
if (obj->GetRuntimeClass() == CLASS_ID_NAMESPACE(CSetOfObjects, mrpt::opengl)) |
| 136 |
{ |
| 137 |
de["obj_children"] = dynamic_cast<CSetOfObjects*>(obj.get())->asYAML(); |
| 138 |
} |
| 139 |
d.asSequence().at(i) = std::move(de); |
| 140 |
} |
| 141 |
return d; |
| 142 |
} |
| 143 |
|
| 144 |
/*-------------------------------------------------------------- |
| 145 |
removeObject |
| 146 |
---------------------------------------------------------------*/ |
| 147 |
void CSetOfObjects::removeObject(const CRenderizable::Ptr& obj) |
| 148 |
{ |
| 149 |
for (auto it = m_objects.begin(); it != m_objects.end(); ++it) |
| 150 |
if (*it == obj) |
| 151 |
{ |
| 152 |
m_objects.erase(it); |
| 153 |
return; |
| 154 |
} |
| 155 |
else if ((*it)->GetRuntimeClass() == CLASS_ID_NAMESPACE(CSetOfObjects, opengl)) |
| 156 |
dynamic_cast<CSetOfObjects*>(it->get())->removeObject(obj); |
| 157 |
} |
| 158 |
|
| 159 |
bool CSetOfObjects::traceRay(const mrpt::poses::CPose3D& o, double& dist) const |
| 160 |
{ |
| 161 |
CPose3D nueva = (CPose3D() - getCPose()) + o; |
| 162 |
bool found = false; |
| 163 |
double tmp; |
| 164 |
for (const auto& m_object : m_objects) |
| 165 |
if (m_object->traceRay(nueva, tmp)) |
| 166 |
{ |
| 167 |
if (!found) |
| 168 |
{ |
| 169 |
found = true; |
| 170 |
dist = tmp; |
| 171 |
} |
| 172 |
else if (tmp < dist) |
| 173 |
dist = tmp; |
| 174 |
} |
| 175 |
return found; |
| 176 |
} |
| 177 |
|
| 178 |
bool CSetOfObjects::contains(const CRenderizable::Ptr& obj) const |
| 179 |
{ |
| 180 |
return find(m_objects.begin(), m_objects.end(), obj) != m_objects.end(); |
| 181 |
} |
| 182 |
|
| 183 |
CRenderizable& CSetOfObjects::setColor_u8(const mrpt::img::TColor& c) |
| 184 |
{ |
| 185 |
{ |
| 186 |
std::unique_lock<std::shared_mutex> lckWrite(m_stateMtx.data); |
| 187 |
m_state.color = c; |
| 188 |
} |
| 189 |
for (auto& o : m_objects) |
| 190 |
{ |
| 191 |
if (!o) continue; |
| 192 |
o->setColor_u8(c); |
| 193 |
} |
| 194 |
return *this; |
| 195 |
} |
| 196 |
|
| 197 |
CRenderizable& CSetOfObjects::setColorA_u8(const uint8_t a) |
| 198 |
{ |
| 199 |
{ |
| 200 |
std::unique_lock<std::shared_mutex> lckWrite(m_stateMtx.data); |
| 201 |
m_state.color.A = a; |
| 202 |
} |
| 203 |
for (auto& o : m_objects) |
| 204 |
{ |
| 205 |
if (!o) continue; |
| 206 |
o->setColorA_u8(a); |
| 207 |
} |
| 208 |
return *this; |
| 209 |
} |
| 210 |
|
| 211 |
/*--------------------------------------------------------------- |
| 212 |
getByName |
| 213 |
---------------------------------------------------------------*/ |
| 214 |
CRenderizable::Ptr CSetOfObjects::getByName(const string& str) |
| 215 |
{ |
| 216 |
for (auto& o : m_objects) |
| 217 |
{ |
| 218 |
if (!o) continue; |
| 219 |
if (o->getName() == str) |
| 220 |
return o; |
| 221 |
else if (auto objs = dynamic_cast<CSetOfObjects*>(o.get())) |
| 222 |
{ |
| 223 |
CRenderizable::Ptr ret = objs->getByName(str); |
| 224 |
if (ret) return ret; |
| 225 |
} |
| 226 |
} |
| 227 |
return {}; |
| 228 |
} |
| 229 |
|
| 230 |
auto CSetOfObjects::internalBoundingBoxLocal() const -> mrpt::math::TBoundingBoxf |
| 231 |
{ |
| 232 |
mrpt::math::TBoundingBoxf bb; |
| 233 |
bool first = true; |
| 234 |
|
| 235 |
for (const auto& o : m_objects) |
| 236 |
{ |
| 237 |
if (!o) continue; |
| 238 |
if (first) |
| 239 |
{ |
| 240 |
bb = o->getBoundingBoxLocalf(); |
| 241 |
first = false; |
| 242 |
} |
| 243 |
else |
| 244 |
bb = bb.unionWith(o->getBoundingBoxLocalf()); |
| 245 |
} |
| 246 |
|
| 247 |
return bb; |
| 248 |
} |