Debian Codegraph @

libs/opengl/src/CAxis.cpp

raw
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/CAxis.h>
13 #include <mrpt/opengl/CText3D.h>
14 #include <mrpt/serialization/CArchive.h>
15 #include <mrpt/system/os.h>
16
17 #include "gltext.h"
18
19 using namespace mrpt;
20 using namespace mrpt::opengl;
21 using namespace mrpt::system;
22 using namespace std;
23
24 IMPLEMENTS_SERIALIZABLE(CAxis, CRenderizableShaderWireFrame, mrpt::opengl)
25
26 CAxis::CAxis(
27 float xmin,
28 float ymin,
29 float zmin,
30 float xmax,
31 float ymax,
32 float zmax,
33 float frecuency,
34 float lineWidth,
35 bool marks) :
36 m_xmin(xmin),
37 m_ymin(ymin),
38 m_zmin(zmin),
39 m_xmax(xmax),
40 m_ymax(ymax),
41 m_zmax(zmax),
42 m_frequency(frecuency)
43 {
44 CRenderizableShaderWireFrame::setLineWidth(lineWidth);
45
46 m_marks.fill(marks);
47
48 // x:180, 0, 90
49 m_textRot[0][0] = 180.f;
50 m_textRot[0][1] = 0.f;
51 m_textRot[0][2] = 90.f;
52 // y:90, 0, 90
53 m_textRot[1][0] = 90.f;
54 m_textRot[1][1] = 0.f;
55 m_textRot[1][2] = 90.f;
56 // z:180, 0, 90
57 m_textRot[2][0] = 180.f;
58 m_textRot[2][1] = 0.f;
59 m_textRot[2][2] = 90.f;
60 }
61
62 void CAxis::setTickMarksLength(float len)
63 {
64 m_markLen = len;
65 CRenderizable::notifyChange();
66 }
67
68 void CAxis::onUpdateBuffers_Wireframe()
69 {
70 using mrpt::math::TPoint3Df;
71
72 auto& vbd = CRenderizableShaderWireFrame::m_vertex_buffer_data;
73 auto& cbd = CRenderizableShaderWireFrame::m_color_buffer_data;
74 std::unique_lock<std::shared_mutex> wfWriteLock(
75 CRenderizableShaderWireFrame::m_wireframeMtx.data);
76
77 vbd.clear();
78
79 m_gl_labels.get().clear();
80
81 // X axis
82 vbd.emplace_back(m_xmin, 0.0f, 0.0f);
83 vbd.emplace_back(m_xmax, 0.0f, 0.0f);
84 // Y axis
85 vbd.emplace_back(0.0f, m_ymin, 0.0f);
86 vbd.emplace_back(0.0f, m_ymax, 0.0f);
87 // Z axis
88 vbd.emplace_back(0.0f, 0.0f, m_zmin);
89 vbd.emplace_back(0.0f, 0.0f, m_zmax);
90
91 // Draw the "tick marks" for X,Y,Z
92 const float ml = m_markLen * m_frequency;
93
94 char n[50];
95 const std::array<TPoint3Df, 3> init_trans = {
96 {{m_xmin, .0f, ml}, {.0f, m_ymin, ml}, {.0f, .0f, m_zmin}}
97 };
98 const std::array<std::array<float, 2>, 3> xyz_ranges = {
99 {{m_xmin, m_xmax}, {m_ymin, m_ymax}, {m_zmin, m_zmax}}
100 };
101 const std::array<TPoint3Df, 3> tick0 = {
102 {{0, -ml, -ml}, {-ml, .0f, -ml}, {-ml, .0f, .0f}}
103 };
104 const std::array<TPoint3Df, 3> tick1 = {
105 {{0, ml, -ml}, {ml, .0f, -ml}, {ml, .0f, .0f}}
106 };
107 const std::array<TPoint3Df, 3> endMark = {
108 {{m_xmax + 1.0f * m_frequency, 0, 0},
109 {0, m_ymax + .5f * m_frequency, 0},
110 {0, 0, m_zmax + 0.5f * m_frequency}}
111 };
112 const std::array<const char*, 3> axis2name = {
113 {"+X", "+Y", "+Z"}
114 };
115
116 for (unsigned int axis = 0; axis < 3; axis++)
117 {
118 if (!m_marks[axis]) continue;
119
120 TPoint3Df tick_incr(0, 0, 0);
121 tick_incr[axis] = m_frequency;
122
123 const auto& tf = init_trans[axis];
124 TPoint3Df cur_tf = tf;
125
126 for (float i = xyz_ranges[axis][0]; i <= xyz_ranges[axis][1];
127 i = i + m_frequency, cur_tf = cur_tf + tick_incr)
128 {
129 // Dont draw the "0" more than once
130 if (axis != 0 && std::abs(i) < 1e-4f) continue;
131
132 os::sprintf(n, 50, "%.02f", i);
133 mrpt::opengl::internal::glSetFont("mono");
134
135 auto label = mrpt::opengl::CText3D::Create();
136 label->setTextStyle(mrpt::opengl::FILL);
137 label->setScale(m_textScale);
138
139 label->setPose(mrpt::poses::CPose3D(
140 cur_tf.x, cur_tf.y, cur_tf.z, mrpt::DEG2RAD(m_textRot[axis][0]),
141 mrpt::DEG2RAD(m_textRot[axis][1]), mrpt::DEG2RAD(m_textRot[axis][2])));
142 label->setString(n);
143 m_gl_labels.get().emplace_back(label);
144
145 // tick line:
146 vbd.emplace_back(cur_tf + tick0[axis]);
147 vbd.emplace_back(cur_tf + tick1[axis]);
148 }
149
150 auto label = mrpt::opengl::CText3D::Create();
151 label->setTextStyle(mrpt::opengl::FILL);
152 label->setScale(m_textScale * 1.2f);
153 label->setPose(mrpt::poses::CPose3D(
154 endMark[axis].x, endMark[axis].y, endMark[axis].z, mrpt::DEG2RAD(m_textRot[axis][0]),
155 mrpt::DEG2RAD(m_textRot[axis][1]), mrpt::DEG2RAD(m_textRot[axis][2])));
156 label->setString(axis2name[axis]);
157 m_gl_labels.get().emplace_back(label);
158 }
159
160 cbd.assign(vbd.size(), getColor_u8());
161
162 for (auto& lb : m_gl_labels.get()) lb->updateBuffers();
163 }
164
165 void CAxis::enqueueForRenderRecursive(
166 const mrpt::opengl::TRenderMatrices& state,
167 RenderQueue& rq,
168 bool wholeInView,
169 bool is1stShadowMapPass) const
170 {
171 // Enque rendering all text labels:
172 mrpt::opengl::enqueueForRendering(m_gl_labels.get(), state, rq, wholeInView, is1stShadowMapPass);
173 }
174
175 void CAxis::render(const RenderContext& rc) const
176 {
177 // Base lines render:
178 CRenderizableShaderWireFrame::render(rc);
179
180 // Do nothing for text labels: the enqueueForRenderRecursive() does the
181 // actual job.
182 }
183
184 uint8_t CAxis::serializeGetVersion() const { return 2; }
185 void CAxis::serializeTo(mrpt::serialization::CArchive& out) const
186 {
187 writeToStreamRender(out);
188 out << m_xmin << m_ymin << m_zmin;
189 out << m_xmax << m_ymax << m_zmax;
190 out << m_frequency << m_lineWidth;
191 // v1:
192 out << m_marks[0] << m_marks[1] << m_marks[2] << m_textScale;
193 for (auto i : m_textRot)
194 for (int j = 0; j < 3; j++) out << i[j];
195 // v2:
196 out << m_markLen;
197 }
198
199 void CAxis::serializeFrom(mrpt::serialization::CArchive& in, uint8_t version)
200 {
201 switch (version)
202 {
203 case 0:
204 case 1:
205 case 2:
206 {
207 readFromStreamRender(in);
208 in >> m_xmin >> m_ymin >> m_zmin;
209 in >> m_xmax >> m_ymax >> m_zmax;
210 in >> m_frequency >> m_lineWidth;
211 if (version >= 1)
212 {
213 in >> m_marks[0] >> m_marks[1] >> m_marks[2] >> m_textScale;
214 for (auto& i : m_textRot)
215 for (int j = 0; j < 3; j++) in >> i[j];
216 }
217 else
218 {
219 bool v;
220 in >> v;
221 m_marks.fill(v);
222 m_textScale = 0.25f;
223 }
224 if (version >= 2) in >> m_markLen;
225 }
226 break;
227 default:
228 MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version);
229 };
230 CRenderizable::notifyChange();
231 }
232
233 auto CAxis::internalBoundingBoxLocal() const -> mrpt::math::TBoundingBoxf
234 {
235 return mrpt::math::TBoundingBoxf::FromUnsortedPoints(
236 {m_xmin, m_ymin, m_zmin}, {m_xmax, m_ymax, m_zmax});
237 }
238
239 void CAxis::setFrequency(float f)
240 {
241 ASSERT_(f > 0);
242 m_frequency = f;
243 CRenderizable::notifyChange();
244 }
245 float CAxis::getFrequency() const { return m_frequency; }
246 void CAxis::enableTickMarks(bool v)
247 {
248 m_marks.fill(v);
249 CRenderizable::notifyChange();
250 }
251 void CAxis::enableTickMarks(bool show_x, bool show_y, bool show_z)
252 {
253 m_marks[0] = show_x;
254 m_marks[1] = show_y;
255 m_marks[2] = show_z;
256 CRenderizable::notifyChange();
257 }
258 void CAxis::setTextScale(float f)
259 {
260 ASSERT_(f > 0);
261 m_textScale = f;
262 CRenderizable::notifyChange();
263 }
264 float CAxis::getTextScale() const { return m_textScale; }
265 void CAxis::setAxisLimits(float xmin, float ymin, float zmin, float xmax, float ymax, float zmax)
266 {
267 m_xmin = xmin;
268 m_ymin = ymin;
269 m_zmin = zmin;
270 m_xmax = xmax;
271 m_ymax = ymax;
272 m_zmax = zmax;
273 CRenderizable::notifyChange();
274 }
275 void CAxis::setTextLabelOrientation(int axis, float yaw_deg, float pitch_deg, float roll_deg)
276 {
277 ASSERT_(axis >= 0 && axis < 3);
278 m_textRot[axis][0] = yaw_deg;
279 m_textRot[axis][1] = pitch_deg;
280 m_textRot[axis][2] = roll_deg;
281 }
282 void CAxis::getTextLabelOrientation(
283 int axis, float& yaw_deg, float& pitch_deg, float& roll_deg) const
284 {
285 ASSERT_(axis >= 0 && axis < 3);
286 yaw_deg = m_textRot[axis][0];
287 pitch_deg = m_textRot[axis][1];
288 roll_deg = m_textRot[axis][2];
289 }