MRPT  1.9.9
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-2019, 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/gl_utils.h>
15 #include <mrpt/system/os.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 
39 {
40  m_marks.fill(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 {
58  m_markLen = len;
60 }
61 
62 void CAxis::render_dl() const
63 {
64 #if MRPT_HAS_OPENGL_GLUT
67 
72 
73  ASSERT_(m_frequency >= 0);
74 
75  glLineWidth(m_lineWidth);
78  glColor4ub(m_color.R, m_color.G, m_color.B, m_color.A);
79  // X axis
80  glVertex3f(m_xmin, 0.0f, 0.0f);
81  glVertex3f(m_xmax, 0.0f, 0.0f);
82  // Y axis
83  glVertex3f(0.0f, m_ymin, 0.0f);
84  glVertex3f(0.0f, m_ymax, 0.0f);
85  // Z axis
86  glVertex3f(0.0f, 0.0f, m_zmin);
87  glVertex3f(0.0f, 0.0f, m_zmax);
88 
89  glEnd();
91 
92  glLineWidth(1.0f);
94 
97 
98  // Draw the "tick marks" for X,Y,Z
99  const float ml = m_markLen * m_frequency;
100 
101  char n[50];
102  const std::array<mrpt::math::TPoint3Df, 3> init_trans = {
103  {{m_xmin, .0f, ml}, {.0f, m_ymin, ml}, {.0f, .0f, m_zmin}}};
104  const std::array<std::array<float, 2>, 3> xyz_ranges = {
105  {{m_xmin, m_xmax}, {m_ymin, m_ymax}, {m_zmin, m_zmax}}};
106  const std::array<mrpt::math::TPoint3Df, 3> tick0 = {
107  {{0, -ml, -ml}, {-ml, .0f, -ml}, {-ml, .0f, .0f}}};
108  const std::array<mrpt::math::TPoint3Df, 3> tick1 = {
109  {{0, ml, -ml}, {ml, .0f, -ml}, {ml, .0f, .0f}}};
110  const std::array<mrpt::math::TPoint3Df, 3> endMark = {
111  {{m_xmax + 1.0f * m_frequency, 0, 0},
112  {0, m_ymax + .5f * m_frequency, 0},
113  {0, 0, m_zmax + 0.5f * m_frequency}}};
114  const std::array<const char*, 3> axis2name = {{"+X", "+Y", "+Z"}};
115 
116  for (int axis = 0; axis < 3; axis++)
117  {
118  if (!m_marks[axis]) continue;
119 
120  glPushMatrix();
121  const auto& tf = init_trans[axis];
122  glTranslatef(tf.x, tf.y, tf.z);
123  for (float i = xyz_ranges[axis][0]; i <= xyz_ranges[axis][1];
124  i = i + m_frequency)
125  {
126  // Dont draw the "0" more than once
127  if (axis == 0 || std::abs(i) > 1e-4)
128  {
129  os::sprintf(n, 50, "%.02f", i);
130  glPushMatrix();
131  glRotatef(m_textRot[0][0], 0, 0, 1);
132  glRotatef(m_textRot[0][1], 0, 1, 0);
133  glRotatef(m_textRot[0][2], 1, 0, 0);
135  glBegin(GL_LINES);
136  glVertex3f(tick0[axis].x, tick0[axis].y, tick0[axis].z);
137  glVertex3f(tick1[axis].x, tick1[axis].y, tick1[axis].z);
138  glEnd();
139  glPopMatrix();
141  }
142  glTranslatef(
143  axis == 0 ? m_frequency : 0, axis == 1 ? m_frequency : 0,
144  axis == 2 ? m_frequency : 0);
145  }
146 
147  glPopMatrix();
148  glPushMatrix();
149  glTranslatef(endMark[axis].x, endMark[axis].y, endMark[axis].z);
150  glRotatef(m_textRot[0][0], 0, 0, 1);
151  glRotatef(m_textRot[0][1], 0, 1, 0);
152  glRotatef(m_textRot[0][2], 1, 0, 0);
154  axis2name[axis], m_textScale * 1.2, mrpt::opengl::NICE);
155  glPopMatrix();
156  }
157 
159  MRPT_END
160 /*******************************************************/
161 #endif
162 }
163 
164 uint8_t CAxis::serializeGetVersion() const { return 2; }
166 {
167  writeToStreamRender(out);
168  out << m_xmin << m_ymin << m_zmin;
169  out << m_xmax << m_ymax << m_zmax;
170  out << m_frequency << m_lineWidth;
171  // v1:
172  out << m_marks[0] << m_marks[1] << m_marks[2] << m_textScale;
173  for (auto i : m_textRot)
174  for (int j = 0; j < 3; j++) out << i[j];
175  // v2:
176  out << m_markLen;
177 }
178 
180 {
181  switch (version)
182  {
183  case 0:
184  case 1:
185  case 2:
186  {
187  readFromStreamRender(in);
188  in >> m_xmin >> m_ymin >> m_zmin;
189  in >> m_xmax >> m_ymax >> m_zmax;
190  in >> m_frequency >> m_lineWidth;
191  if (version >= 1)
192  {
193  in >> m_marks[0] >> m_marks[1] >> m_marks[2] >> m_textScale;
194  for (auto& i : m_textRot)
195  for (int j = 0; j < 3; j++) in >> i[j];
196  }
197  else
198  {
199  bool v;
200  in >> v;
201  m_marks.fill(v);
202  m_textScale = 0.25f;
203  }
204  if (version >= 2) in >> m_markLen;
205  }
206  break;
207  default:
209  };
211 }
212 
215 {
216  bb_min.x = m_xmin;
217  bb_min.y = m_ymin;
218  bb_min.z = m_zmin;
219 
220  bb_max.x = m_xmax;
221  bb_max.y = m_ymax;
222  bb_max.z = m_zmax;
223 
224  // Convert to coordinates of my parent:
225  m_pose.composePoint(bb_min, bb_min);
226  m_pose.composePoint(bb_max, bb_max);
227 }
228 
229 void CAxis::setFrequency(float f)
230 {
231  ASSERT_(f > 0);
232  m_frequency = f;
234 }
235 float CAxis::getFrequency() const { return m_frequency; }
237 {
238  m_lineWidth = w;
240 }
241 float CAxis::getLineWidth() const { return m_lineWidth; }
243 {
244  m_marks.fill(v);
246 }
247 void CAxis::enableTickMarks(bool show_x, bool show_y, bool show_z)
248 {
249  m_marks[0] = show_x;
250  m_marks[1] = show_y;
251  m_marks[2] = show_z;
253 }
254 void CAxis::setTextScale(float f)
255 {
256  ASSERT_(f > 0);
257  m_textScale = f;
259 }
260 float CAxis::getTextScale() const { return m_textScale; }
262  float xmin, float ymin, float zmin, float xmax, float ymax, float zmax)
263 {
264  m_xmin = xmin;
265  m_ymin = ymin;
266  m_zmin = zmin;
267  m_xmax = xmax;
268  m_ymax = ymax;
269  m_zmax = zmax;
271 }
273  int axis, float yaw_deg, float pitch_deg, float roll_deg)
274 {
275  ASSERT_(axis >= 0 && axis < 3);
276  m_textRot[axis][0] = yaw_deg;
277  m_textRot[axis][1] = pitch_deg;
278  m_textRot[axis][2] = roll_deg;
279 }
281  int axis, float& yaw_deg, float& pitch_deg, float& roll_deg) const
282 {
283  ASSERT_(axis >= 0 && axis < 3);
284  yaw_deg = m_textRot[axis][0];
285  pitch_deg = m_textRot[axis][1];
286  roll_deg = m_textRot[axis][2];
287 }
float getFrequency() const
Definition: CAxis.cpp:235
void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
#define MRPT_START
Definition: exceptions.h:241
GLdouble GLdouble z
Definition: glext.h:3879
GLAPI void GLAPIENTRY glEnable(GLenum cap)
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
GLAPI void GLAPIENTRY glPopMatrix(void)
GLenum GLsizei n
Definition: glext.h:5136
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Definition: CAxis.cpp:165
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:545
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:288
GLenum GLsizei len
Definition: glext.h:4756
#define GL_LIGHTING
Definition: glew.h:386
GLAPI void GLAPIENTRY glLineWidth(GLfloat width)
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:4199
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
unsigned char uint8_t
Definition: rptypes.h:44
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:97
float getTextScale() const
Definition: CAxis.cpp:260
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
void enableTickMarks(bool v=true)
Definition: CAxis.cpp:242
void setLineWidth(float w)
Definition: CAxis.cpp:236
void render_dl() const override
Render.
Definition: CAxis.cpp:62
GLAPI void GLAPIENTRY glBegin(GLenum mode)
#define GL_BLEND
Definition: glew.h:433
void getTextLabelOrientation(int axis, float &yaw_deg, float &pitch_deg, float &roll_deg) const
axis: {0,1,2}=>{X,Y,Z}
Definition: CAxis.cpp:280
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:8064
GLAPI void GLAPIENTRY glTranslatef(GLfloat x, GLfloat y, GLfloat z)
#define GL_SRC_ALPHA
Definition: glew.h:287
const GLdouble * v
Definition: glext.h:3684
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:272
float getLineWidth() const
Definition: CAxis.cpp:241
void setTickMarksLength(float len)
As a ratio of "marks frequency" (default: 0.05)
Definition: CAxis.cpp:56
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:53
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:213
void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:155
#define MRPT_END
Definition: exceptions.h:245
Draw a 3D world axis, with coordinate marks at some regular interval.
Definition: CAxis.h:29
GLuint in
Definition: glext.h:7391
renders glyphs filled with antialiased outlines
Definition: opengl_fonts.h:37
void setFrequency(float f)
Changes the frequency of the "ticks".
Definition: CAxis.cpp:229
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
const auto bb_max
GLAPI void GLAPIENTRY glEnd(void)
GLAPI void GLAPIENTRY glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
GLenum GLint GLint y
Definition: glext.h:3542
const auto bb_min
#define GL_LINES
Definition: glew.h:274
GLenum GLint x
Definition: glext.h:3542
GLAPI void GLAPIENTRY glPushMatrix(void)
Lightweight 3D point.
Definition: TPoint3D.h:90
void setAxisLimits(float xmin, float ymin, float zmin, float xmax, float ymax, float zmax)
Definition: CAxis.cpp:261
renders glyphs as filled polygons
Definition: opengl_fonts.h:35
GLAPI void GLAPIENTRY glDisable(GLenum cap)
void setTextScale(float f)
Changes the size of text labels (default:0.25)
Definition: CAxis.cpp:254
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
Definition: CAxis.cpp:179
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:191
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
Definition: CAxis.cpp:164



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019