| 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 <Eigen/Dense> // First! to avoid conflicts with X.h |
| 13 |
// |
| 14 |
#include <mrpt/img/color_maps.h> |
| 15 |
#include <mrpt/math/ops_containers.h> |
| 16 |
#include <mrpt/opengl/CMeshFast.h> |
| 17 |
#include <mrpt/opengl/CSetOfTriangles.h> |
| 18 |
#include <mrpt/poses/CPose3D.h> |
| 19 |
#include <mrpt/serialization/CArchive.h> |
| 20 |
|
| 21 |
using namespace mrpt; |
| 22 |
using namespace mrpt::opengl; |
| 23 |
using namespace mrpt::img; |
| 24 |
using namespace mrpt::poses; |
| 25 |
using namespace mrpt::math; |
| 26 |
using namespace std; |
| 27 |
|
| 28 |
IMPLEMENTS_SERIALIZABLE(CMeshFast, CRenderizable, mrpt::opengl) |
| 29 |
|
| 30 |
void CMeshFast::updatePoints() const |
| 31 |
{ |
| 32 |
CRenderizable::notifyChange(); |
| 33 |
|
| 34 |
const auto cols = Z.cols(); |
| 35 |
const auto rows = Z.rows(); |
| 36 |
|
| 37 |
if ((m_colorFromZ) || (m_isImage)) updateColorsMatrix(); |
| 38 |
|
| 39 |
ASSERT_((cols > 0) && (rows > 0)); |
| 40 |
ASSERT_((xMax > xMin) && (yMax > yMin)); |
| 41 |
X.setSize(rows, cols); |
| 42 |
Y.setSize(rows, cols); |
| 43 |
const float sCellX = (xMax - xMin) / (rows - 1); |
| 44 |
const float sCellY = (yMax - yMin) / (cols - 1); |
| 45 |
|
| 46 |
for (int iX = 0; iX < rows; iX++) |
| 47 |
for (int iY = 0; iY < cols; iY++) |
| 48 |
{ |
| 49 |
X(iX, iY) = xMin + iX * sCellX; |
| 50 |
Y(iX, iY) = yMin + iY * sCellY; |
| 51 |
} |
| 52 |
|
| 53 |
pointsUpToDate = true; |
| 54 |
} |
| 55 |
|
| 56 |
void CMeshFast::onUpdateBuffers_Points() |
| 57 |
{ |
| 58 |
using mrpt::img::TColor; |
| 59 |
using mrpt::img::TColorf; |
| 60 |
|
| 61 |
if (!pointsUpToDate) updatePoints(); |
| 62 |
|
| 63 |
ASSERT_EQUAL_(X.size(), Y.size()); |
| 64 |
ASSERT_EQUAL_(X.size(), Z.size()); |
| 65 |
|
| 66 |
auto& vbd = CRenderizableShaderPoints::m_vertex_buffer_data; |
| 67 |
auto& cbd = CRenderizableShaderPoints::m_color_buffer_data; |
| 68 |
std::unique_lock<std::shared_mutex> wfWriteLock(CRenderizableShaderPoints::m_pointsMtx.data); |
| 69 |
|
| 70 |
vbd.clear(); |
| 71 |
cbd.clear(); |
| 72 |
|
| 73 |
const auto myColor = getColor_u8(); |
| 74 |
|
| 75 |
for (int i = 0; i < X.rows(); i++) |
| 76 |
{ |
| 77 |
for (int j = 0; j < X.cols(); j++) |
| 78 |
{ |
| 79 |
TColor col; |
| 80 |
|
| 81 |
if (m_isImage && m_textureImage.isColor()) |
| 82 |
col = TColor(C_r(i, j), C_g(i, j), C_b(i, j), myColor.A); |
| 83 |
else if (m_isImage) |
| 84 |
col = TColor(C(i, j), C(i, j), C(i, j), myColor.A); |
| 85 |
else if (m_colorFromZ) |
| 86 |
{ |
| 87 |
float rz, gz, bz; |
| 88 |
colormap(m_colorMap, C(i, j) / 255.0f, rz, gz, bz); |
| 89 |
col = TColorf(rz, gz, bz, myColor.A / 255.f).asTColor(); |
| 90 |
} |
| 91 |
else |
| 92 |
col = myColor; |
| 93 |
|
| 94 |
cbd.emplace_back(col); |
| 95 |
vbd.emplace_back(X(i, j), Y(i, j), Z(i, j)); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
void CMeshFast::assignImage(const CImage& img) |
| 101 |
{ |
| 102 |
MRPT_START |
| 103 |
|
| 104 |
// Make a copy: |
| 105 |
m_textureImage = img; |
| 106 |
|
| 107 |
// Delete content in Z |
| 108 |
Z.setZero(img.getHeight(), img.getWidth()); |
| 109 |
|
| 110 |
// Update flags/states |
| 111 |
m_modified_Image = true; |
| 112 |
m_enableTransparency = false; |
| 113 |
m_colorFromZ = false; |
| 114 |
m_isImage = true; |
| 115 |
pointsUpToDate = false; |
| 116 |
|
| 117 |
CRenderizable::notifyChange(); |
| 118 |
|
| 119 |
MRPT_END |
| 120 |
} |
| 121 |
|
| 122 |
void CMeshFast::assignImageAndZ(const CImage& img, const mrpt::math::CMatrixDynamic<float>& in_Z) |
| 123 |
{ |
| 124 |
MRPT_START |
| 125 |
|
| 126 |
ASSERT_( |
| 127 |
(img.getWidth() == static_cast<size_t>(in_Z.cols())) && |
| 128 |
(img.getHeight() == static_cast<size_t>(in_Z.rows()))); |
| 129 |
|
| 130 |
Z = in_Z; |
| 131 |
|
| 132 |
// Make a copy: |
| 133 |
m_textureImage = img; |
| 134 |
|
| 135 |
// Update flags/states |
| 136 |
m_modified_Image = true; |
| 137 |
m_enableTransparency = false; |
| 138 |
m_colorFromZ = false; |
| 139 |
m_isImage = true; |
| 140 |
pointsUpToDate = false; |
| 141 |
|
| 142 |
CRenderizable::notifyChange(); |
| 143 |
|
| 144 |
MRPT_END |
| 145 |
} |
| 146 |
|
| 147 |
uint8_t CMeshFast::serializeGetVersion() const { return 0; } |
| 148 |
void CMeshFast::serializeTo(mrpt::serialization::CArchive& out) const |
| 149 |
{ |
| 150 |
writeToStreamRender(out); |
| 151 |
|
| 152 |
out << m_textureImage; |
| 153 |
out << m_isImage; |
| 154 |
out << xMin << xMax << yMin << yMax; |
| 155 |
out << X << Y << Z; // We don't need to serialize C, it's computed |
| 156 |
out << m_enableTransparency; |
| 157 |
out << m_colorFromZ; |
| 158 |
out << int16_t(m_colorMap); |
| 159 |
out << m_pointSize; |
| 160 |
} |
| 161 |
|
| 162 |
void CMeshFast::serializeFrom(mrpt::serialization::CArchive& in, uint8_t version) |
| 163 |
{ |
| 164 |
switch (version) |
| 165 |
{ |
| 166 |
case 0: |
| 167 |
{ |
| 168 |
readFromStreamRender(in); |
| 169 |
|
| 170 |
in >> m_textureImage; |
| 171 |
in >> m_isImage; |
| 172 |
|
| 173 |
in >> xMin; |
| 174 |
in >> xMax; |
| 175 |
in >> yMin; |
| 176 |
in >> yMax; |
| 177 |
|
| 178 |
in >> X >> Y >> Z; |
| 179 |
in >> m_enableTransparency; |
| 180 |
in >> m_colorFromZ; |
| 181 |
|
| 182 |
int16_t i; |
| 183 |
in >> i; |
| 184 |
m_colorMap = TColormap(i); |
| 185 |
in >> m_pointSize; |
| 186 |
m_modified_Z = true; |
| 187 |
} |
| 188 |
|
| 189 |
pointsUpToDate = false; |
| 190 |
break; |
| 191 |
|
| 192 |
default: |
| 193 |
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version); |
| 194 |
}; |
| 195 |
CRenderizable::notifyChange(); |
| 196 |
} |
| 197 |
|
| 198 |
void CMeshFast::updateColorsMatrix() const |
| 199 |
{ |
| 200 |
if (!m_modified_Z && !m_modified_Image) return; |
| 201 |
|
| 202 |
CRenderizable::notifyChange(); |
| 203 |
|
| 204 |
if (m_isImage) |
| 205 |
{ |
| 206 |
const int cols = m_textureImage.getWidth(); |
| 207 |
const int rows = m_textureImage.getHeight(); |
| 208 |
|
| 209 |
ASSERT_EQUAL_(cols, Z.cols()); |
| 210 |
ASSERT_EQUAL_(rows, Z.rows()); |
| 211 |
|
| 212 |
if (m_textureImage.isColor()) |
| 213 |
{ |
| 214 |
C_r.setSize(rows, cols); |
| 215 |
C_g.setSize(rows, cols); |
| 216 |
C_b.setSize(rows, cols); |
| 217 |
m_textureImage.getAsRGBMatrices(C_r, C_g, C_b); |
| 218 |
} |
| 219 |
else |
| 220 |
{ |
| 221 |
C.setSize(rows, cols); |
| 222 |
m_textureImage.getAsMatrix(C); |
| 223 |
} |
| 224 |
} |
| 225 |
else |
| 226 |
{ |
| 227 |
const size_t cols = Z.cols(); |
| 228 |
const size_t rows = Z.rows(); |
| 229 |
|
| 230 |
C.setSize(rows, cols); |
| 231 |
|
| 232 |
// Color is proportional to difference between height of a cell and |
| 233 |
// the mean of the nearby cells MEANS: |
| 234 |
Eigen::MatrixXf Zf = Z.asEigen().cast<float>(); |
| 235 |
mrpt::math::normalize(Zf, 0.01f, 0.99f); |
| 236 |
|
| 237 |
Zf *= 255; |
| 238 |
|
| 239 |
C = Zf.cast<uint8_t>(); |
| 240 |
} |
| 241 |
|
| 242 |
m_modified_Image = false; |
| 243 |
m_modified_Z = false; // Done |
| 244 |
pointsUpToDate = false; |
| 245 |
} |
| 246 |
|
| 247 |
void CMeshFast::setZ(const mrpt::math::CMatrixDynamic<float>& in_Z) |
| 248 |
{ |
| 249 |
Z = in_Z; |
| 250 |
m_modified_Z = true; |
| 251 |
pointsUpToDate = false; |
| 252 |
|
| 253 |
// Delete previously loaded images |
| 254 |
m_isImage = false; |
| 255 |
|
| 256 |
CRenderizable::notifyChange(); |
| 257 |
} |
| 258 |
|
| 259 |
auto CMeshFast::internalBoundingBoxLocal() const -> mrpt::math::TBoundingBoxf |
| 260 |
{ |
| 261 |
return verticesBoundingBox(); |
| 262 |
} |