| 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/CVectorField2D.h> |
| 13 |
#include <mrpt/serialization/CArchive.h> |
| 14 |
|
| 15 |
using namespace mrpt; |
| 16 |
using namespace mrpt::opengl; |
| 17 |
using namespace mrpt::math; |
| 18 |
using namespace std; |
| 19 |
|
| 20 |
IMPLEMENTS_SERIALIZABLE(CVectorField2D, CRenderizable, mrpt::opengl) |
| 21 |
|
| 22 |
/** Constructor */ |
| 23 |
CVectorField2D::CVectorField2D() : xcomp(0, 0), ycomp(0, 0) |
| 24 |
{ |
| 25 |
m_point_color = getColor_u8(); |
| 26 |
m_field_color = getColor_u8(); |
| 27 |
} |
| 28 |
|
| 29 |
/** Constructor with a initial set of lines. */ |
| 30 |
CVectorField2D::CVectorField2D( |
| 31 |
[[maybe_unused]] CMatrixFloat Matrix_x, |
| 32 |
[[maybe_unused]] CMatrixFloat Matrix_y, |
| 33 |
[[maybe_unused]] float xmin, |
| 34 |
[[maybe_unused]] float xmax, |
| 35 |
[[maybe_unused]] float ymin, |
| 36 |
[[maybe_unused]] float ymax) |
| 37 |
{ |
| 38 |
m_point_color = getColor_u8(); |
| 39 |
m_field_color = getColor_u8(); |
| 40 |
} |
| 41 |
|
| 42 |
void CVectorField2D::render(const RenderContext& rc) const |
| 43 |
{ |
| 44 |
switch (rc.shader_id) |
| 45 |
{ |
| 46 |
case DefaultShaderID::TRIANGLES_NO_LIGHT: |
| 47 |
CRenderizableShaderTriangles::render(rc); |
| 48 |
break; |
| 49 |
case DefaultShaderID::WIREFRAME: |
| 50 |
CRenderizableShaderWireFrame::render(rc); |
| 51 |
break; |
| 52 |
case DefaultShaderID::POINTS: |
| 53 |
CRenderizableShaderWireFrame::render(rc); |
| 54 |
break; |
| 55 |
}; |
| 56 |
} |
| 57 |
void CVectorField2D::renderUpdateBuffers() const |
| 58 |
{ |
| 59 |
CRenderizableShaderPoints::renderUpdateBuffers(); |
| 60 |
CRenderizableShaderTriangles::renderUpdateBuffers(); |
| 61 |
CRenderizableShaderWireFrame::renderUpdateBuffers(); |
| 62 |
} |
| 63 |
|
| 64 |
void CVectorField2D::onUpdateBuffers_Wireframe() |
| 65 |
{ |
| 66 |
auto& vbd = CRenderizableShaderWireFrame::m_vertex_buffer_data; |
| 67 |
auto& cbd = CRenderizableShaderWireFrame::m_color_buffer_data; |
| 68 |
std::unique_lock<std::shared_mutex> wfWriteLock( |
| 69 |
CRenderizableShaderWireFrame::m_wireframeMtx.data); |
| 70 |
|
| 71 |
vbd.clear(); |
| 72 |
|
| 73 |
vbd.reserve(xcomp.size() * 2); |
| 74 |
|
| 75 |
const float x_cell_size = (xMax - xMin) / (xcomp.cols() - 1); |
| 76 |
const float y_cell_size = (yMax - yMin) / (ycomp.rows() - 1); |
| 77 |
|
| 78 |
for (int i = 0; i < xcomp.cols(); i++) |
| 79 |
for (int j = 0; j < xcomp.rows(); j++) |
| 80 |
{ |
| 81 |
vbd.emplace_back(xMin + i * x_cell_size, yMin + j * y_cell_size, 0); |
| 82 |
vbd.emplace_back( |
| 83 |
xMin + i * x_cell_size + xcomp(j, i), yMin + j * y_cell_size + ycomp(j, i), 0); |
| 84 |
} |
| 85 |
|
| 86 |
cbd.assign(vbd.size(), m_field_color); |
| 87 |
} |
| 88 |
void CVectorField2D::onUpdateBuffers_Triangles() |
| 89 |
{ |
| 90 |
using P3f = mrpt::math::TPoint3Df; |
| 91 |
|
| 92 |
std::unique_lock<std::shared_mutex> trisWriteLock( |
| 93 |
CRenderizableShaderTriangles::m_trianglesMtx.data); |
| 94 |
auto& tris = CRenderizableShaderTriangles::m_triangles; |
| 95 |
|
| 96 |
tris.clear(); |
| 97 |
|
| 98 |
tris.reserve(xcomp.size()); |
| 99 |
|
| 100 |
const float x_cell_size = (xMax - xMin) / (xcomp.cols() - 1); |
| 101 |
const float y_cell_size = (yMax - yMin) / (ycomp.rows() - 1); |
| 102 |
|
| 103 |
for (int i = 0; i < xcomp.cols(); i++) |
| 104 |
for (int j = 0; j < xcomp.rows(); j++) |
| 105 |
{ |
| 106 |
const float tri_side = 0.25f * sqrt(xcomp(j, i) * xcomp(j, i) + ycomp(j, i) * ycomp(j, i)); |
| 107 |
const float ang = ::atan2f(ycomp(j, i), xcomp(j, i)) - 1.5708f; |
| 108 |
tris.emplace_back( |
| 109 |
P3f(-sin(ang) * 0.866f * tri_side + xMin + i * x_cell_size + xcomp(j, i), |
| 110 |
cos(ang) * 0.866f * tri_side + yMin + j * y_cell_size + ycomp(j, i), 0), |
| 111 |
P3f(cos(ang) * 0.5f * tri_side + xMin + i * x_cell_size + xcomp(j, i), |
| 112 |
sin(ang) * 0.5f * tri_side + yMin + j * y_cell_size + ycomp(j, i), 0), |
| 113 |
P3f(-cos(ang) * 0.5f * tri_side + xMin + i * x_cell_size + xcomp(j, i), |
| 114 |
-sin(ang) * 0.5f * tri_side + yMin + j * y_cell_size + ycomp(j, i), 0)); |
| 115 |
} |
| 116 |
|
| 117 |
for (auto& t : tris) |
| 118 |
{ |
| 119 |
t.computeNormals(); |
| 120 |
t.setColor(m_field_color); |
| 121 |
} |
| 122 |
} |
| 123 |
void CVectorField2D::onUpdateBuffers_Points() |
| 124 |
{ |
| 125 |
auto& vbd = CRenderizableShaderPoints::m_vertex_buffer_data; |
| 126 |
auto& cbd = CRenderizableShaderPoints::m_color_buffer_data; |
| 127 |
std::unique_lock<std::shared_mutex> wfWriteLock(CRenderizableShaderPoints::m_pointsMtx.data); |
| 128 |
|
| 129 |
vbd.clear(); |
| 130 |
vbd.reserve(xcomp.size()); |
| 131 |
|
| 132 |
const float x_cell_size = (xMax - xMin) / (xcomp.cols() - 1); |
| 133 |
const float y_cell_size = (yMax - yMin) / (ycomp.rows() - 1); |
| 134 |
|
| 135 |
for (int i = 0; i < xcomp.cols(); i++) |
| 136 |
for (int j = 0; j < xcomp.rows(); j++) |
| 137 |
vbd.emplace_back(xMin + i * x_cell_size, yMin + j * y_cell_size, .0f); |
| 138 |
|
| 139 |
cbd.assign(vbd.size(), m_point_color); |
| 140 |
} |
| 141 |
|
| 142 |
/*--------------------------------------------------------------- |
| 143 |
Implements the writing to a CStream capability of |
| 144 |
CSerializable objects |
| 145 |
---------------------------------------------------------------*/ |
| 146 |
uint8_t CVectorField2D::serializeGetVersion() const { return 1; } |
| 147 |
void CVectorField2D::serializeTo(mrpt::serialization::CArchive& out) const |
| 148 |
{ |
| 149 |
writeToStreamRender(out); |
| 150 |
|
| 151 |
out << xcomp << ycomp; |
| 152 |
out << xMin << xMax << yMin << yMax; |
| 153 |
out << m_lineWidth; |
| 154 |
out << m_pointSize; |
| 155 |
out << m_antiAliasing; |
| 156 |
out << m_point_color; |
| 157 |
out << m_field_color; |
| 158 |
CRenderizableShaderTriangles::params_serialize(out); // v1 |
| 159 |
} |
| 160 |
|
| 161 |
void CVectorField2D::serializeFrom(mrpt::serialization::CArchive& in, uint8_t version) |
| 162 |
{ |
| 163 |
switch (version) |
| 164 |
{ |
| 165 |
case 0: |
| 166 |
case 1: |
| 167 |
readFromStreamRender(in); |
| 168 |
|
| 169 |
in >> xcomp >> ycomp; |
| 170 |
in >> xMin >> xMax >> yMin >> yMax; |
| 171 |
in >> m_lineWidth; |
| 172 |
in >> m_pointSize; |
| 173 |
in >> m_antiAliasing; |
| 174 |
in >> m_point_color; |
| 175 |
in >> m_field_color; |
| 176 |
|
| 177 |
if (version >= 1) CRenderizableShaderTriangles::params_deserialize(in); |
| 178 |
break; |
| 179 |
|
| 180 |
default: |
| 181 |
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version); |
| 182 |
break; |
| 183 |
}; |
| 184 |
CRenderizable::notifyChange(); |
| 185 |
} |
| 186 |
|
| 187 |
auto CVectorField2D::internalBoundingBoxLocal() const -> mrpt::math::TBoundingBoxf |
| 188 |
{ |
| 189 |
return verticesBoundingBox(); |
| 190 |
} |
| 191 |
|
| 192 |
void CVectorField2D::adjustVectorFieldToGrid() |
| 193 |
{ |
| 194 |
ASSERT_(xcomp.size() > 0); |
| 195 |
|
| 196 |
const float ratio_xp = xcomp.maxCoeff() * (xcomp.cols() - 1) / (xMax - xMin); |
| 197 |
const float ratio_xn = xcomp.minCoeff() * (xcomp.cols() - 1) / (xMax - xMin); |
| 198 |
const float ratio_yp = ycomp.maxCoeff() * (ycomp.rows() - 1) / (yMax - yMin); |
| 199 |
const float ratio_yn = ycomp.minCoeff() * (ycomp.rows() - 1) / (yMax - yMin); |
| 200 |
const float norm_factor = |
| 201 |
0.85f / max(max(ratio_xp, std::abs(ratio_xn)), max(ratio_yp, std::abs(ratio_yn))); |
| 202 |
|
| 203 |
xcomp *= norm_factor; |
| 204 |
ycomp *= norm_factor; |
| 205 |
CRenderizable::notifyChange(); |
| 206 |
} |