MRPT  2.0.4
CAxis.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, 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>
15 #include <mrpt/system/os.h>
16 #include "gltext.h"
17 
18 using namespace mrpt;
19 using namespace mrpt::opengl;
20 using namespace mrpt::system;
21 using namespace std;
22 
24 
26  float xmin, float ymin, float zmin, float xmax, float ymax, float zmax,
27  float frecuency, float lineWidth, bool marks)
28  : m_xmin(xmin),
29  m_ymin(ymin),
30  m_zmin(zmin),
31  m_xmax(xmax),
32  m_ymax(ymax),
33  m_zmax(zmax),
34  m_frequency(frecuency)
35 {
37 
38  m_marks.fill(marks);
39 
40  // x:180, 0, 90
41  m_textRot[0][0] = 180.f;
42  m_textRot[0][1] = 0.f;
43  m_textRot[0][2] = 90.f;
44  // y:90, 0, 90
45  m_textRot[1][0] = 90.f;
46  m_textRot[1][1] = 0.f;
47  m_textRot[1][2] = 90.f;
48  // z:180, 0, 90
49  m_textRot[2][0] = 180.f;
50  m_textRot[2][1] = 0.f;
51  m_textRot[2][2] = 90.f;
52 }
53 
55 {
56  m_markLen = len;
58 }
59 
61 {
63 
66  vbd.clear();
67 
68  m_gl_labels.clear();
69 
70  // X axis
71  vbd.emplace_back(m_xmin, 0.0f, 0.0f);
72  vbd.emplace_back(m_xmax, 0.0f, 0.0f);
73  // Y axis
74  vbd.emplace_back(0.0f, m_ymin, 0.0f);
75  vbd.emplace_back(0.0f, m_ymax, 0.0f);
76  // Z axis
77  vbd.emplace_back(0.0f, 0.0f, m_zmin);
78  vbd.emplace_back(0.0f, 0.0f, m_zmax);
79 
80  // Draw the "tick marks" for X,Y,Z
81  const float ml = m_markLen * m_frequency;
82 
83  char n[50];
84  const std::array<TPoint3Df, 3> init_trans = {
85  {{m_xmin, .0f, ml}, {.0f, m_ymin, ml}, {.0f, .0f, m_zmin}}};
86  const std::array<std::array<float, 2>, 3> xyz_ranges = {
87  {{m_xmin, m_xmax}, {m_ymin, m_ymax}, {m_zmin, m_zmax}}};
88  const std::array<TPoint3Df, 3> tick0 = {
89  {{0, -ml, -ml}, {-ml, .0f, -ml}, {-ml, .0f, .0f}}};
90  const std::array<TPoint3Df, 3> tick1 = {
91  {{0, ml, -ml}, {ml, .0f, -ml}, {ml, .0f, .0f}}};
92  const std::array<TPoint3Df, 3> endMark = {
93  {{m_xmax + 1.0f * m_frequency, 0, 0},
94  {0, m_ymax + .5f * m_frequency, 0},
95  {0, 0, m_zmax + 0.5f * m_frequency}}};
96  const std::array<const char*, 3> axis2name = {{"+X", "+Y", "+Z"}};
97 
98  for (unsigned int axis = 0; axis < 3; axis++)
99  {
100  if (!m_marks[axis]) continue;
101 
102  TPoint3Df tick_incr(0, 0, 0);
103  tick_incr[axis] = m_frequency;
104 
105  const auto& tf = init_trans[axis];
106  TPoint3Df cur_tf = tf;
107 
108  for (float i = xyz_ranges[axis][0]; i <= xyz_ranges[axis][1];
109  i = i + m_frequency, cur_tf = cur_tf + tick_incr)
110  {
111  // Dont draw the "0" more than once
112  if (axis != 0 && std::abs(i) < 1e-4f) continue;
113 
114  os::sprintf(n, 50, "%.02f", i);
116 
117  auto label = mrpt::opengl::CText3D::Create();
118  label->setTextStyle(mrpt::opengl::FILL);
119  label->setScale(m_textScale);
120 
121  label->setPose(mrpt::poses::CPose3D(
122  cur_tf.x, cur_tf.y, cur_tf.z, mrpt::DEG2RAD(m_textRot[0][0]),
123  mrpt::DEG2RAD(m_textRot[0][1]),
124  mrpt::DEG2RAD(m_textRot[0][2])));
125  label->setString(n);
126  m_gl_labels.emplace_back(label);
127 
128  // tick line:
129  vbd.emplace_back(cur_tf + tick0[axis]);
130  vbd.emplace_back(cur_tf + tick1[axis]);
131  }
132 
133  auto label = mrpt::opengl::CText3D::Create();
134  label->setTextStyle(mrpt::opengl::FILL);
135  label->setScale(m_textScale * 1.2f);
136  label->setPose(mrpt::poses::CPose3D(
137  endMark[axis].x, endMark[axis].y, endMark[axis].z,
138  mrpt::DEG2RAD(m_textRot[0][0]), mrpt::DEG2RAD(m_textRot[0][1]),
139  mrpt::DEG2RAD(m_textRot[0][2])));
140  label->setString(axis2name[axis]);
141  m_gl_labels.emplace_back(label);
142  }
143 
144  cbd.assign(vbd.size(), m_color);
145 
146  for (auto& lb : m_gl_labels) lb->updateBuffers();
147 }
148 
150  const mrpt::opengl::TRenderMatrices& state, RenderQueue& rq) const
151 {
152  // Enque rendering all text labels:
153  mrpt::opengl::enqueForRendering(m_gl_labels, state, rq);
154 }
155 
156 void CAxis::render(const RenderContext& rc) const
157 {
158  // Base lines render:
160 
161  // Do nothing for text labels: the enqueForRenderRecursive() does the actual
162  // job.
163 }
164 
165 uint8_t CAxis::serializeGetVersion() const { return 2; }
167 {
168  writeToStreamRender(out);
169  out << m_xmin << m_ymin << m_zmin;
170  out << m_xmax << m_ymax << m_zmax;
171  out << m_frequency << m_lineWidth;
172  // v1:
173  out << m_marks[0] << m_marks[1] << m_marks[2] << m_textScale;
174  for (auto i : m_textRot)
175  for (int j = 0; j < 3; j++) out << i[j];
176  // v2:
177  out << m_markLen;
178 }
179 
181 {
182  switch (version)
183  {
184  case 0:
185  case 1:
186  case 2:
187  {
188  readFromStreamRender(in);
189  in >> m_xmin >> m_ymin >> m_zmin;
190  in >> m_xmax >> m_ymax >> m_zmax;
191  in >> m_frequency >> m_lineWidth;
192  if (version >= 1)
193  {
194  in >> m_marks[0] >> m_marks[1] >> m_marks[2] >> m_textScale;
195  for (auto& i : m_textRot)
196  for (int j = 0; j < 3; j++) in >> i[j];
197  }
198  else
199  {
200  bool v;
201  in >> v;
202  m_marks.fill(v);
203  m_textScale = 0.25f;
204  }
205  if (version >= 2) in >> m_markLen;
206  }
207  break;
208  default:
210  };
212 }
213 
216 {
217  bb_min.x = m_xmin;
218  bb_min.y = m_ymin;
219  bb_min.z = m_zmin;
220 
221  bb_max.x = m_xmax;
222  bb_max.y = m_ymax;
223  bb_max.z = m_zmax;
224 
225  // Convert to coordinates of my parent:
226  m_pose.composePoint(bb_min, bb_min);
227  m_pose.composePoint(bb_max, bb_max);
228 }
229 
230 void CAxis::setFrequency(float f)
231 {
232  ASSERT_(f > 0);
233  m_frequency = f;
235 }
236 float CAxis::getFrequency() const { return m_frequency; }
238 {
239  m_marks.fill(v);
241 }
242 void CAxis::enableTickMarks(bool show_x, bool show_y, bool show_z)
243 {
244  m_marks[0] = show_x;
245  m_marks[1] = show_y;
246  m_marks[2] = show_z;
248 }
249 void CAxis::setTextScale(float f)
250 {
251  ASSERT_(f > 0);
252  m_textScale = f;
254 }
255 float CAxis::getTextScale() const { return m_textScale; }
257  float xmin, float ymin, float zmin, float xmax, float ymax, float zmax)
258 {
259  m_xmin = xmin;
260  m_ymin = ymin;
261  m_zmin = zmin;
262  m_xmax = xmax;
263  m_ymax = ymax;
264  m_zmax = zmax;
266 }
268  int axis, float yaw_deg, float pitch_deg, float roll_deg)
269 {
270  ASSERT_(axis >= 0 && axis < 3);
271  m_textRot[axis][0] = yaw_deg;
272  m_textRot[axis][1] = pitch_deg;
273  m_textRot[axis][2] = roll_deg;
274 }
276  int axis, float& yaw_deg, float& pitch_deg, float& roll_deg) const
277 {
278  ASSERT_(axis >= 0 && axis < 3);
279  yaw_deg = m_textRot[axis][0];
280  pitch_deg = m_textRot[axis][1];
281  roll_deg = m_textRot[axis][2];
282 }
float getFrequency() const
Definition: CAxis.cpp:236
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
void notifyChange() const
Call to enable calling renderUpdateBuffers() before the next render() rendering iteration.
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Definition: CAxis.cpp:166
STL namespace.
Context for calls to render()
std::vector< mrpt::math::TPoint3Df > m_vertex_buffer_data
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
float getTextScale() const
Definition: CAxis.cpp:255
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
void enableTickMarks(bool v=true)
Definition: CAxis.cpp:237
void enqueForRendering(const mrpt::opengl::CListOpenGLObjects &objs, const mrpt::opengl::TRenderMatrices &state, RenderQueue &rq)
Processes, recursively, all objects in the list, classifying them by shader programs into a list suit...
Definition: RenderQueue.cpp:28
constexpr double DEG2RAD(const double x)
Degrees to radians.
static Ptr Create(Args &&... args)
Definition: CText3D.h:44
Rendering state related to the projection and model-view matrices.
void getTextLabelOrientation(int axis, float &yaw_deg, float &pitch_deg, float &roll_deg) const
axis: {0,1,2}=>{X,Y,Z}
Definition: CAxis.cpp:275
std::vector< mrpt::img::TColor > m_color_buffer_data
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void setTextLabelOrientation(int axis, float yaw_deg, float pitch_deg, float roll_deg)
axis: {0,1,2}=>{X,Y,Z}
Definition: CAxis.cpp:267
void setTickMarksLength(float len)
As a ratio of "marks frequency" (default: 0.05)
Definition: CAxis.cpp:54
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const override
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
Definition: CAxis.cpp:214
void onUpdateBuffers_Wireframe() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
Definition: CAxis.cpp:60
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:85
mrpt::vision::TStereoCalibResults out
void glSetFont(const std::string &fontname)
sets the font to use for future font rendering commands.
Definition: gltext.cpp:146
Renderizable generic renderer for objects using the wireframe shader.
Draw a 3D world axis, with coordinate marks at some regular interval.
Definition: CAxis.h:29
void setFrequency(float f)
Changes the frequency of the "ticks".
Definition: CAxis.cpp:230
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
const auto bb_max
void enqueForRenderRecursive(const mrpt::opengl::TRenderMatrices &state, RenderQueue &rq) const override
Definition: CAxis.cpp:149
const auto bb_min
TPoint3D_< float > TPoint3Df
Definition: TPoint3D.h:269
void setAxisLimits(float xmin, float ymin, float zmin, float xmax, float ymax, float zmax)
Definition: CAxis.cpp:256
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
Definition: CAxis.cpp:156
renders glyphs as filled polygons
Definition: opengl_fonts.h:21
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
void setTextScale(float f)
Changes the size of text labels (default:0.25)
Definition: CAxis.cpp:249
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
Definition: CAxis.cpp:180
int sprintf(char *buf, size_t bufSize, const char *format,...) noexcept MRPT_printf_format_check(3
An OS-independent version of sprintf (Notice the bufSize param, which may be ignored in some compiler...
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
Definition: CAxis.cpp:165
std::map< shader_id_t, std::multimap< float, RenderQueueElement > > RenderQueue
A queue for rendering, sorted by shader program to minimize changes of OpenGL shader programs while r...
Definition: RenderQueue.h:46



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020