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-2018, 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>
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 
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 uint8_t CAxis::serializeGetVersion() const { return 1; }
196 {
197  writeToStreamRender(out);
198  out << m_xmin << m_ymin << m_zmin;
199  out << m_xmax << m_ymax << m_zmax;
200  out << m_frequency << m_lineWidth;
201  // v1:
202  out << m_marks[0] << m_marks[1] << m_marks[2] << m_textScale;
203  for (int i = 0; i < 3; i++)
204  for (int j = 0; j < 3; j++) out << m_textRot[i][j];
205 }
206 
208 {
209  switch (version)
210  {
211  case 0:
212  case 1:
213  {
214  readFromStreamRender(in);
215  in >> m_xmin >> m_ymin >> m_zmin;
216  in >> m_xmax >> m_ymax >> m_zmax;
217  in >> m_frequency >> m_lineWidth;
218  if (version >= 1)
219  {
220  in >> m_marks[0] >> m_marks[1] >> m_marks[2] >> m_textScale;
221  for (int i = 0; i < 3; i++)
222  for (int j = 0; j < 3; j++) in >> m_textRot[i][j];
223  }
224  else
225  {
226  bool v;
227  in >> v;
228  for (int i = 0; i < 3; i++) m_marks[i] = v;
229  m_textScale = 0.25f;
230  }
231  }
232  break;
233  default:
235  };
237 }
238 
240  mrpt::math::TPoint3D& bb_min, mrpt::math::TPoint3D& bb_max) const
241 {
242  bb_min.x = m_xmin;
243  bb_min.y = m_ymin;
244  bb_min.z = m_zmin;
245 
246  bb_max.x = m_xmax;
247  bb_max.y = m_ymax;
248  bb_max.z = m_zmax;
249 
250  // Convert to coordinates of my parent:
251  m_pose.composePoint(bb_min, bb_min);
252  m_pose.composePoint(bb_max, bb_max);
253 }
254 
255 void CAxis::setFrequency(float f)
256 {
257  ASSERT_(f > 0);
258  m_frequency = f;
260 }
261 float CAxis::getFrequency() const { return m_frequency; }
263 {
264  m_lineWidth = w;
266 }
267 float CAxis::getLineWidth() const { return m_lineWidth; }
269 {
270  for (int i = 0; i < 3; i++) m_marks[i] = v;
272 }
273 void CAxis::enableTickMarks(bool show_x, bool show_y, bool show_z)
274 {
275  m_marks[0] = show_x;
276  m_marks[1] = show_y;
277  m_marks[2] = show_z;
279 }
280 void CAxis::setTextScale(float f)
281 {
282  ASSERT_(f > 0);
283  m_textScale = f;
285 }
286 float CAxis::getTextScale() const { return m_textScale; }
288  float xmin, float ymin, float zmin, float xmax, float ymax, float zmax)
289 {
290  m_xmin = xmin;
291  m_ymin = ymin;
292  m_zmin = zmin;
293  m_xmax = xmax;
294  m_ymax = ymax;
295  m_zmax = zmax;
297 }
299  int axis, float yaw_deg, float pitch_deg, float roll_deg)
300 {
301  ASSERT_(axis >= 0 && axis < 3);
302  m_textRot[axis][0] = yaw_deg;
303  m_textRot[axis][1] = pitch_deg;
304  m_textRot[axis][2] = roll_deg;
305 }
307  int axis, float& yaw_deg, float& pitch_deg, float& roll_deg) const
308 {
309  ASSERT_(axis >= 0 && axis < 3);
310  yaw_deg = m_textRot[axis][0];
311  pitch_deg = m_textRot[axis][1];
312  roll_deg = m_textRot[axis][2];
313 }
float getFrequency() const
Definition: CAxis.cpp:261
#define MRPT_START
Definition: exceptions.h:262
GLAPI void GLAPIENTRY glEnable(GLenum cap)
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:25
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
GLAPI void GLAPIENTRY glPopMatrix(void)
GLenum GLsizei n
Definition: glext.h:5074
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Definition: CAxis.cpp:195
EIGEN_STRONG_INLINE void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
STL namespace.
mrpt::img::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:532
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:287
#define GL_LIGHTING
Definition: glew.h:385
GLAPI void GLAPIENTRY glLineWidth(GLfloat width)
static Ptr Create(Args &&... args)
Definition: CAxis.h:33
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:4178
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
unsigned char uint8_t
Definition: rptypes.h:41
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:90
float getTextScale() const
Definition: CAxis.cpp:286
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
void enableTickMarks(bool v=true)
Definition: CAxis.cpp:268
void setLineWidth(float w)
Definition: CAxis.cpp:262
std::shared_ptr< CAxis > Ptr
Definition: CAxis.h:33
void render_dl() const override
Render.
Definition: CAxis.cpp:65
double x
X,Y,Z coordinates.
GLAPI void GLAPIENTRY glBegin(GLenum mode)
#define GL_BLEND
Definition: glew.h:432
void getTextLabelOrientation(int axis, float &yaw_deg, float &pitch_deg, float &roll_deg) const
axis: {0,1,2}=>{X,Y,Z}
Definition: CAxis.cpp:306
GLAPI void GLAPIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z)
GLAPI void GLAPIENTRY glColor4ub(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha)
GLclampd zmax
Definition: glext.h:7918
GLAPI void GLAPIENTRY glTranslatef(GLfloat x, GLfloat y, GLfloat z)
#define GL_SRC_ALPHA
Definition: glew.h:286
const GLdouble * v
Definition: glext.h:3678
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:298
float getLineWidth() const
Definition: CAxis.cpp:267
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:48
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:239
void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:143
#define MRPT_END
Definition: exceptions.h:266
Draw a 3D world axis, with coordinate marks at some regular interval.
Definition: CAxis.h:31
GLuint in
Definition: glext.h:7274
renders glyphs filled with antialiased outlines
Definition: opengl_fonts.h:40
void setFrequency(float f)
Changes the frequency of the "ticks".
Definition: CAxis.cpp:255
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
GLAPI void GLAPIENTRY glEnd(void)
GLAPI void GLAPIENTRY glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
#define GL_LINES
Definition: glew.h:273
GLAPI void GLAPIENTRY glPushMatrix(void)
Lightweight 3D point.
void setAxisLimits(float xmin, float ymin, float zmin, float xmax, float ymax, float zmax)
Definition: CAxis.cpp:287
renders glyphs as filled polygons
Definition: opengl_fonts.h:38
GLAPI void GLAPIENTRY glDisable(GLenum cap)
void setTextScale(float f)
Changes the size of text labels (default:0.25)
Definition: CAxis.cpp:280
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
Definition: CAxis.cpp:207
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:189
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
Definition: CAxis.cpp:194



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019