Main MRPT website > C++ reference for MRPT 1.9.9
CAxis.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "opengl-precomp.h" // Precompiled header
11 
12 #include <mrpt/opengl/CAxis.h>
13 #include <mrpt/utils/CStream.h>
14 #include <mrpt/system/os.h>
15 #include <mrpt/opengl/gl_utils.h>
16 
17 #include "opengl_internals.h"
18 
19 using namespace mrpt;
20 using namespace mrpt::opengl;
21 using namespace mrpt::system;
22 using namespace mrpt::utils;
23 using namespace std;
24 
26 
28  float xmin, float ymin, float zmin, float xmax, float ymax, float zmax,
29  float frecuency, float lineWidth, bool marks)
30  : m_xmin(xmin),
31  m_ymin(ymin),
32  m_zmin(zmin),
33  m_xmax(xmax),
34  m_ymax(ymax),
35  m_zmax(zmax),
36  m_frequency(frecuency),
37  m_lineWidth(lineWidth),
38  m_textScale(0.25f)
39 {
40  for (int i = 0; i < 3; i++) m_marks[i] = marks;
41 
42  // x:180, 0, 90
43  m_textRot[0][0] = 180.f;
44  m_textRot[0][1] = 0.f;
45  m_textRot[0][2] = 90.f;
46  // y:90, 0, 90
47  m_textRot[1][0] = 90.f;
48  m_textRot[1][1] = 0.f;
49  m_textRot[1][2] = 90.f;
50  // z:180, 0, 90
51  m_textRot[2][0] = 180.f;
52  m_textRot[2][1] = 0.f;
53  m_textRot[2][2] = 90.f;
54 }
55 
57  float xmin, float ymin, float zmin, float xmax, float ymax, float zmax,
58  float frecuency, float lineWidth, bool marks)
59 {
60  return CAxis::Ptr(
61  new CAxis(
62  xmin, ymin, zmin, xmax, ymax, zmax, frecuency, lineWidth, marks));
63 }
64 
65 void CAxis::render_dl() const
66 {
67 #if MRPT_HAS_OPENGL_GLUT
70 
75 
76  ASSERT_(m_frequency >= 0);
77 
78  glLineWidth(m_lineWidth);
81  glColor4ub(m_color.R, m_color.G, m_color.B, m_color.A);
82  // X axis
83  glVertex3f(m_xmin, 0.0f, 0.0f);
84  glVertex3f(m_xmax, 0.0f, 0.0f);
85  // Y axis
86  glVertex3f(0.0f, m_ymin, 0.0f);
87  glVertex3f(0.0f, m_ymax, 0.0f);
88  // Z axis
89  glVertex3f(0.0f, 0.0f, m_zmin);
90  glVertex3f(0.0f, 0.0f, m_zmax);
91 
92  glEnd();
94 
95  glLineWidth(1.0f);
97 
100 
101  // Draw the "tick marks":
102  char n[50];
103  if (m_marks[0])
104  {
105  // X axis
106  glPushMatrix();
107  glTranslatef(m_xmin, .0f, .05f);
108  for (float i = m_xmin; i <= m_xmax; i = i + m_frequency)
109  {
110  os::sprintf(n, 50, "%.02f", i);
111  glPushMatrix();
112  glRotatef(m_textRot[0][0], 0, 0, 1);
113  glRotatef(m_textRot[0][1], 0, 1, 0);
114  glRotatef(m_textRot[0][2], 1, 0, 0);
116  glPopMatrix();
117  glTranslatef(m_frequency, 0, 0);
118  }
119 
120  glPopMatrix();
121  glPushMatrix();
122  glTranslatef(m_xmax + 1.0f * m_frequency, 0, 0);
123  glRotatef(m_textRot[0][0], 0, 0, 1);
124  glRotatef(m_textRot[0][1], 0, 1, 0);
125  glRotatef(m_textRot[0][2], 1, 0, 0);
126  gl_utils::glDrawText("+X", m_textScale * 1.2, mrpt::opengl::NICE);
127  glPopMatrix();
128  }
129  if (m_marks[1])
130  {
131  // Y axis
132  glPushMatrix();
133  glTranslatef(.0f, m_ymin, .05f);
134  for (float i = m_ymin; i <= m_ymax; i = i + m_frequency)
135  {
136  if (std::abs(i) > 1e-4)
137  { // Dont draw the "0" more than once
138  os::sprintf(n, 50, "%.02f", i);
139  glPushMatrix();
140  glRotatef(m_textRot[1][0], 0, 0, 1);
141  glRotatef(m_textRot[1][1], 0, 1, 0);
142  glRotatef(m_textRot[1][2], 1, 0, 0);
144  glPopMatrix();
145  }
146  glTranslatef(0, m_frequency, 0);
147  }
148 
149  glPopMatrix();
150  glPushMatrix();
151  glTranslatef(0, m_ymax + .5f * m_frequency, 0);
152  glRotatef(m_textRot[1][0], 0, 0, 1);
153  glRotatef(m_textRot[1][1], 0, 1, 0);
154  glRotatef(m_textRot[1][2], 1, 0, 0);
155  gl_utils::glDrawText("+Y", m_textScale * 1.2, mrpt::opengl::NICE);
156  glPopMatrix();
157  }
158  if (m_marks[2])
159  {
160  // Z axis
161  glPushMatrix();
162  glTranslatef(.0f, .0f, m_zmin);
163  for (float i = m_zmin; i <= m_zmax; i = i + m_frequency)
164  {
165  if (std::abs(i) > 1e-4)
166  { // Dont draw the "0" more than once
167  glPushMatrix();
168  glRotatef(m_textRot[2][0], 0, 0, 1);
169  glRotatef(m_textRot[2][1], 0, 1, 0);
170  glRotatef(m_textRot[2][2], 1, 0, 0);
171  os::sprintf(n, 50, "%.02f", i);
173  glPopMatrix();
174  }
175  glTranslatef(0, 0, m_frequency);
176  }
177 
178  glPopMatrix();
179  glPushMatrix();
180  glTranslatef(0, 0, m_zmax + 0.5f * m_frequency);
181  glRotatef(m_textRot[2][0], 0, 0, 1);
182  glRotatef(m_textRot[2][1], 0, 1, 0);
183  glRotatef(m_textRot[2][2], 1, 0, 0);
184  gl_utils::glDrawText("+Z", m_textScale * 1.2, mrpt::opengl::NICE);
185  glPopMatrix();
186  }
187 
189  MRPT_END
190 /*******************************************************/
191 #endif
192 }
193 
194 /*---------------------------------------------------------------
195  Implements the writing to a CStream capability of
196  CSerializable objects
197  ---------------------------------------------------------------*/
198 void CAxis::writeToStream(mrpt::utils::CStream& out, int* version) const
199 {
200  if (version)
201  *version = 1;
202  else
203  {
204  writeToStreamRender(out);
205  out << m_xmin << m_ymin << m_zmin;
206  out << m_xmax << m_ymax << m_zmax;
207  out << m_frequency << m_lineWidth;
208  // v1:
209  out << m_marks[0] << m_marks[1] << m_marks[2] << m_textScale;
210  for (int i = 0; i < 3; i++)
211  for (int j = 0; j < 3; j++) out << m_textRot[i][j];
212  }
213 }
214 
215 /*---------------------------------------------------------------
216  Implements the reading from a CStream capability of
217  CSerializable objects
218  ---------------------------------------------------------------*/
220 {
221  switch (version)
222  {
223  case 0:
224  case 1:
225  {
226  readFromStreamRender(in);
227  in >> m_xmin >> m_ymin >> m_zmin;
228  in >> m_xmax >> m_ymax >> m_zmax;
229  in >> m_frequency >> m_lineWidth;
230  if (version >= 1)
231  {
232  in >> m_marks[0] >> m_marks[1] >> m_marks[2] >> m_textScale;
233  for (int i = 0; i < 3; i++)
234  for (int j = 0; j < 3; j++) in >> m_textRot[i][j];
235  }
236  else
237  {
238  bool v;
239  in >> v;
240  for (int i = 0; i < 3; i++) m_marks[i] = v;
241  m_textScale = 0.25f;
242  }
243  }
244  break;
245  default:
247  };
249 }
250 
252  mrpt::math::TPoint3D& bb_min, mrpt::math::TPoint3D& bb_max) const
253 {
254  bb_min.x = m_xmin;
255  bb_min.y = m_ymin;
256  bb_min.z = m_zmin;
257 
258  bb_max.x = m_xmax;
259  bb_max.y = m_ymax;
260  bb_max.z = m_zmax;
261 
262  // Convert to coordinates of my parent:
263  m_pose.composePoint(bb_min, bb_min);
264  m_pose.composePoint(bb_max, bb_max);
265 }
266 
267 void CAxis::setFrequency(float f)
268 {
269  ASSERT_(f > 0);
270  m_frequency = f;
272 }
273 float CAxis::getFrequency() const { return m_frequency; }
275 {
276  m_lineWidth = w;
278 }
279 float CAxis::getLineWidth() const { return m_lineWidth; }
281 {
282  for (int i = 0; i < 3; i++) m_marks[i] = v;
284 }
285 void CAxis::enableTickMarks(bool show_x, bool show_y, bool show_z)
286 {
287  m_marks[0] = show_x;
288  m_marks[1] = show_y;
289  m_marks[2] = show_z;
291 }
292 void CAxis::setTextScale(float f)
293 {
294  ASSERT_(f > 0);
295  m_textScale = f;
297 }
298 float CAxis::getTextScale() const { return m_textScale; }
300  float xmin, float ymin, float zmin, float xmax, float ymax, float zmax)
301 {
302  m_xmin = xmin;
303  m_ymin = ymin;
304  m_zmin = zmin;
305  m_xmax = xmax;
306  m_ymax = ymax;
307  m_zmax = zmax;
309 }
311  int axis, float yaw_deg, float pitch_deg, float roll_deg)
312 {
313  ASSERT_(axis >= 0 && axis < 3);
314  m_textRot[axis][0] = yaw_deg;
315  m_textRot[axis][1] = pitch_deg;
316  m_textRot[axis][2] = roll_deg;
317 }
319  int axis, float& yaw_deg, float& pitch_deg, float& roll_deg) const
320 {
321  ASSERT_(axis >= 0 && axis < 3);
322  yaw_deg = m_textRot[axis][0];
323  pitch_deg = m_textRot[axis][1];
324  roll_deg = m_textRot[axis][2];
325 }
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
Draw a 3D world axis, with coordinate marks at some regular interval.
Definition: CAxis.h:32
float getLineWidth() const
Definition: CAxis.cpp:279
void setAxisLimits(float xmin, float ymin, float zmin, float xmax, float ymax, float zmax)
Definition: CAxis.cpp:299
float getTextScale() const
Definition: CAxis.cpp:298
float getFrequency() const
Definition: CAxis.cpp:273
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:251
void setTextLabelOrientation(int axis, float yaw_deg, float pitch_deg, float roll_deg)
axis: {0,1,2}=>{X,Y,Z}
Definition: CAxis.cpp:310
static Ptr Create(Args &&... args)
Definition: CAxis.h:33
void setFrequency(float f)
Changes the frequency of the "ticks".
Definition: CAxis.cpp:267
std::shared_ptr< CAxis > Ptr
Definition: CAxis.h:33
void setLineWidth(float w)
Definition: CAxis.cpp:274
void setTextScale(float f)
Changes the size of text labels (default:0.25)
Definition: CAxis.cpp:292
void enableTickMarks(bool v=true)
Definition: CAxis.cpp:280
void getTextLabelOrientation(int axis, float &yaw_deg, float &pitch_deg, float &roll_deg) const
axis: {0,1,2}=>{X,Y,Z}
Definition: CAxis.cpp:318
void readFromStream(mrpt::utils::CStream &in, int version) override
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
Definition: CAxis.cpp:219
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
Definition: CAxis.cpp:198
void render_dl() const override
Render.
Definition: CAxis.cpp:65
A renderizable object suitable for rendering with OpenGL's display lists.
EIGEN_STRONG_INLINE void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated)
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:42
GLAPI void GLAPIENTRY glTranslatef(GLfloat x, GLfloat y, GLfloat z)
GLAPI void GLAPIENTRY glEnable(GLenum cap)
#define GL_SRC_ALPHA
Definition: glew.h:286
#define GL_LINES
Definition: glew.h:273
GLAPI void GLAPIENTRY glPushMatrix(void)
GLAPI void GLAPIENTRY glLineWidth(GLfloat width)
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
GLAPI void GLAPIENTRY glBegin(GLenum mode)
GLAPI void GLAPIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z)
GLAPI void GLAPIENTRY glPopMatrix(void)
#define GL_BLEND
Definition: glew.h:432
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:287
GLAPI void GLAPIENTRY glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
GLAPI void GLAPIENTRY glEnd(void)
GLAPI void GLAPIENTRY glDisable(GLenum cap)
GLAPI void GLAPIENTRY glColor4ub(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha)
#define GL_LIGHTING
Definition: glew.h:385
GLenum GLsizei n
Definition: glext.h:5074
const GLdouble * v
Definition: glext.h:3678
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:4178
GLclampd zmax
Definition: glext.h:7918
GLuint in
Definition: glext.h:7274
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...
Definition: os.cpp:188
#define MRPT_START
Definition: mrpt_macros.h:425
#define ASSERT_(f)
Definition: mrpt_macros.h:309
#define MRPT_END
Definition: mrpt_macros.h:429
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: mrpt_macros.h:181
void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:140
mrpt::utils::TPixelCoordf glDrawText(const std::string &text, const double textScale, enum TOpenGLFontStyle style=NICE, double spacing=1.5, double kerning=0.1)
renders a string in GL using the current settings.
Definition: gl_utils.cpp:640
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:16
@ NICE
renders glyphs filled with antialiased outlines
Definition: opengl_fonts.h:40
@ FILL
renders glyphs as filled polygons
Definition: opengl_fonts.h:38
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:31
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values,...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Lightweight 3D point.
double x
X,Y,Z coordinates.



Page generated by Doxygen 1.9.1 for MRPT 1.9.9 Git: 63ea9d1f1 Thu Nov 23 00:06:53 2017 +0100 at mar 26 may 2026 12:19:29 CEST