| 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/CColorBar.h> |
| 13 |
#include <mrpt/serialization/CArchive.h> |
| 14 |
|
| 15 |
#include "gltext.h" |
| 16 |
|
| 17 |
using namespace mrpt; |
| 18 |
using namespace mrpt::opengl; |
| 19 |
using namespace mrpt::math; |
| 20 |
using namespace std; |
| 21 |
|
| 22 |
IMPLEMENTS_SERIALIZABLE(CColorBar, CRenderizable, mrpt::opengl) |
| 23 |
|
| 24 |
CColorBar::CColorBar( |
| 25 |
/** The colormap to represent. */ |
| 26 |
const mrpt::img::TColormap colormap, |
| 27 |
/** size of the color bar */ |
| 28 |
double width, |
| 29 |
double height, |
| 30 |
/** limits for [0,1] colormap indices */ |
| 31 |
float min_col, |
| 32 |
float max_col, |
| 33 |
/** limits for values associated to extreme colors */ |
| 34 |
float min_value, |
| 35 |
float max_value, |
| 36 |
/** sprintf-like format string for values */ |
| 37 |
const std::string& label_format, |
| 38 |
/** Label text font size */ |
| 39 |
float label_font_size) : |
| 40 |
m_colormap(colormap), |
| 41 |
m_width(width), |
| 42 |
m_height(height), |
| 43 |
m_label_format(label_format), |
| 44 |
m_min_col(min_col), |
| 45 |
m_max_col(max_col), |
| 46 |
m_min_value(min_value), |
| 47 |
m_max_value(max_value), |
| 48 |
m_label_font_size(label_font_size) |
| 49 |
{ |
| 50 |
} |
| 51 |
|
| 52 |
void CColorBar::setColormap(const mrpt::img::TColormap colormap) |
| 53 |
{ |
| 54 |
m_colormap = colormap; |
| 55 |
CRenderizable::notifyChange(); |
| 56 |
} |
| 57 |
|
| 58 |
void CColorBar::setColorAndValueLimits( |
| 59 |
float col_min, float col_max, float value_min, float value_max) |
| 60 |
{ |
| 61 |
m_min_col = col_min; |
| 62 |
m_max_col = col_max; |
| 63 |
m_min_value = value_min; |
| 64 |
m_max_value = value_max; |
| 65 |
CRenderizable::notifyChange(); |
| 66 |
} |
| 67 |
|
| 68 |
void CColorBar::render(const RenderContext& rc) const |
| 69 |
{ |
| 70 |
// colobars are typically displayed on-top |
| 71 |
switch (rc.shader_id) |
| 72 |
{ |
| 73 |
case DefaultShaderID::TRIANGLES_NO_LIGHT: |
| 74 |
CRenderizableShaderTriangles::render(rc); |
| 75 |
break; |
| 76 |
case DefaultShaderID::WIREFRAME: |
| 77 |
CRenderizableShaderWireFrame::render(rc); |
| 78 |
break; |
| 79 |
}; |
| 80 |
} |
| 81 |
void CColorBar::renderUpdateBuffers() const |
| 82 |
{ |
| 83 |
const_cast<CColorBar&>(*this).onUpdateBuffers_all(); |
| 84 |
|
| 85 |
CRenderizableShaderTriangles::renderUpdateBuffers(); |
| 86 |
CRenderizableShaderWireFrame::renderUpdateBuffers(); |
| 87 |
} |
| 88 |
|
| 89 |
void CColorBar::onUpdateBuffers_all() |
| 90 |
{ |
| 91 |
std::unique_lock<std::shared_mutex> trisWriteLock( |
| 92 |
CRenderizableShaderTriangles::m_trianglesMtx.data); |
| 93 |
auto& tris = CRenderizableShaderTriangles::m_triangles; |
| 94 |
|
| 95 |
auto& lines_vbd = CRenderizableShaderWireFrame::m_vertex_buffer_data; |
| 96 |
auto& lines_cbd = CRenderizableShaderWireFrame::m_color_buffer_data; |
| 97 |
std::unique_lock<std::shared_mutex> wfWriteLock( |
| 98 |
CRenderizableShaderWireFrame::m_wireframeMtx.data); |
| 99 |
|
| 100 |
lines_vbd.clear(); |
| 101 |
lines_cbd.clear(); |
| 102 |
|
| 103 |
// precomputed params: |
| 104 |
unsigned int num_divisions = 64; |
| 105 |
unsigned int num_labels = 4; |
| 106 |
unsigned int one_label_each_nth = num_divisions / num_labels; |
| 107 |
|
| 108 |
const double x0 = .0, x1 = m_width, x2 = m_width * 1.3; |
| 109 |
const double Ay = m_height / (num_divisions - 1); |
| 110 |
|
| 111 |
std::vector<mrpt::img::TColor> colors(num_divisions); |
| 112 |
for (unsigned int i = 0; i < num_divisions; i++) |
| 113 |
{ |
| 114 |
const float col_idx = m_min_col + i * (m_max_col - m_min_col) / (num_divisions - 1); |
| 115 |
mrpt::img::TColorf colf; |
| 116 |
mrpt::img::colormap(m_colormap, col_idx, colf.R, colf.G, colf.B); |
| 117 |
colors[i] = colf.asTColor(); |
| 118 |
} |
| 119 |
|
| 120 |
// Text labels: |
| 121 |
mrpt::opengl::internal::glSetFont("mono"); |
| 122 |
|
| 123 |
const auto tickColor = mrpt::img::TColor::black(); |
| 124 |
const auto textColor = mrpt::img::TColor::white(); |
| 125 |
|
| 126 |
for (unsigned int i = 0; i < num_divisions; i++) |
| 127 |
{ |
| 128 |
const double val = m_min_value + i * (m_max_value - m_min_value) / (num_divisions - 1); |
| 129 |
const double y0 = Ay * i; //, y1 = Ay*(i + 1); |
| 130 |
|
| 131 |
// Text label: |
| 132 |
bool draw_label = (i % one_label_each_nth) == 0 || i == (num_divisions - 1); |
| 133 |
|
| 134 |
if (draw_label) |
| 135 |
{ |
| 136 |
// Line: |
| 137 |
lines_vbd.emplace_back(x0, y0, 0); |
| 138 |
lines_vbd.emplace_back(x2, y0, 0); |
| 139 |
lines_cbd.emplace_back(tickColor); |
| 140 |
lines_cbd.emplace_back(tickColor); |
| 141 |
|
| 142 |
// Text: |
| 143 |
|
| 144 |
// Size: m_label_font_size |
| 145 |
// glTranslated(x2, y0, 0.0); |
| 146 |
mrpt::poses::CPose3D p(x2, y0, 0.0, 0, 0, 0); |
| 147 |
|
| 148 |
mrpt::opengl::internal::glDrawTextTransformed( |
| 149 |
mrpt::format(m_label_format.c_str(), val), tris, lines_vbd, lines_cbd, p, |
| 150 |
m_label_font_size, textColor, FILL, 1.5, 0.1); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
// Color bar itself: |
| 155 |
for (unsigned int i = 0; i < num_divisions - 1; i++) |
| 156 |
{ |
| 157 |
const double y0 = Ay * i, y1 = Ay * (i + 1); |
| 158 |
const TPoint3Df pt00(x0, y0, 0), pt10(x1, y0, 0); |
| 159 |
const TPoint3Df pt01(x0, y1, 0), pt11(x1, y1, 0); |
| 160 |
|
| 161 |
// Color quad: |
| 162 |
// triangle 1/2 |
| 163 |
mrpt::opengl::TTriangle t; |
| 164 |
t.vertices[0].xyzrgba.pt = pt00; |
| 165 |
t.vertices[0].setColor(colors[i]); |
| 166 |
|
| 167 |
t.vertices[1].xyzrgba.pt = pt10; |
| 168 |
t.vertices[1].setColor(colors[i]); |
| 169 |
|
| 170 |
t.vertices[2].xyzrgba.pt = pt11; |
| 171 |
t.vertices[2].setColor(colors[i + 1]); |
| 172 |
t.computeNormals(); |
| 173 |
tris.emplace_back(t); |
| 174 |
|
| 175 |
// triangle 2/2 |
| 176 |
t.vertices[0].xyzrgba.pt = pt00; |
| 177 |
t.vertices[0].setColor(colors[i]); |
| 178 |
|
| 179 |
t.vertices[1].xyzrgba.pt = pt11; |
| 180 |
t.vertices[1].setColor(colors[i + 1]); |
| 181 |
|
| 182 |
t.vertices[2].xyzrgba.pt = pt01; |
| 183 |
t.vertices[2].setColor(colors[i + 1]); |
| 184 |
t.computeNormals(); |
| 185 |
tris.emplace_back(t); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
void CColorBar::onUpdateBuffers_Wireframe() |
| 190 |
{ |
| 191 |
// Already done in onUpdateBuffers_all() |
| 192 |
} |
| 193 |
void CColorBar::onUpdateBuffers_Triangles() |
| 194 |
{ |
| 195 |
// Already done in onUpdateBuffers_all() |
| 196 |
} |
| 197 |
|
| 198 |
uint8_t CColorBar::serializeGetVersion() const { return 2; } |
| 199 |
void CColorBar::serializeTo(mrpt::serialization::CArchive& out) const |
| 200 |
{ |
| 201 |
writeToStreamRender(out); |
| 202 |
// version 0 |
| 203 |
out << uint32_t(m_colormap) << m_min_col << m_max_col << m_min_value << m_max_value |
| 204 |
<< m_label_format << m_label_font_size; |
| 205 |
CRenderizableShaderTriangles::params_serialize(out); // v2 |
| 206 |
} |
| 207 |
void CColorBar::serializeFrom(mrpt::serialization::CArchive& in, uint8_t version) |
| 208 |
{ |
| 209 |
switch (version) |
| 210 |
{ |
| 211 |
case 0: |
| 212 |
case 1: |
| 213 |
case 2: |
| 214 |
readFromStreamRender(in); |
| 215 |
|
| 216 |
in.ReadAsAndCastTo<uint32_t, mrpt::img::TColormap>(m_colormap); |
| 217 |
in >> m_min_col >> m_max_col >> m_min_value >> m_max_value >> m_label_format >> |
| 218 |
m_label_font_size; |
| 219 |
if (version == 0) |
| 220 |
{ |
| 221 |
bool old_disable_depth_test; |
| 222 |
in >> old_disable_depth_test; |
| 223 |
} |
| 224 |
if (version >= 2) CRenderizableShaderTriangles::params_deserialize(in); |
| 225 |
|
| 226 |
break; |
| 227 |
default: |
| 228 |
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version); |
| 229 |
}; |
| 230 |
CRenderizable::notifyChange(); |
| 231 |
} |
| 232 |
|
| 233 |
mrpt::math::TBoundingBoxf CColorBar::internalBoundingBoxLocal() const |
| 234 |
{ |
| 235 |
return mrpt::math::TBoundingBoxf::FromUnsortedPoints( |
| 236 |
{.0f, .0f, .0f}, {d2f(m_width), d2f(m_height), .0f}); |
| 237 |
} |