Main MRPT website > C++ reference for MRPT 1.9.9
CVectorField3D.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 
13 #include <mrpt/utils/CStream.h>
14 #include "opengl_internals.h"
15 
16 using namespace mrpt;
17 using namespace mrpt::opengl;
18 using namespace mrpt::utils;
19 using namespace mrpt::math;
20 using namespace std;
21 
23 
24 /** Constructor */
26  : x_vf(0, 0),
27  y_vf(0, 0),
28  z_vf(0, 0),
29  x_p(0, 0),
30  y_p(0, 0),
31  z_p(0, 0),
32  m_LineWidth(1.0),
33  m_pointSize(1.0),
34  m_antiAliasing(true),
35  m_colorFromModule(false),
36  m_showPoints(true)
37 {
38  m_point_color = m_color;
39  m_field_color = m_color;
40  m_still_color = m_color;
41  m_maxspeed_color = m_color;
42  m_maxspeed = 1.f;
43 }
44 
45 /** Constructor with a initial set of lines. */
47  CMatrixFloat x_vf_ini, CMatrixFloat y_vf_ini, CMatrixFloat z_vf_ini,
48  CMatrixFloat x_p_ini, CMatrixFloat y_p_ini, CMatrixFloat z_p_ini)
49  : m_LineWidth(1.0),
50  m_pointSize(1.0),
51  m_antiAliasing(true),
52  m_colorFromModule(false),
53  m_showPoints(true)
54 {
55  x_vf = x_vf_ini;
56  y_vf = y_vf_ini;
57  z_vf = z_vf_ini;
58  x_p = x_p_ini;
59  y_p = y_p_ini;
60  z_p = z_p_ini;
65  m_maxspeed = 1.f;
66 }
67 
68 /*---------------------------------------------------------------
69  render
70  ---------------------------------------------------------------*/
72 {
73 #if MRPT_HAS_OPENGL_GLUT
74 
75  // Enable antialiasing:
77  if (m_antiAliasing || m_color.A != 255)
78  {
81  }
82  if (m_antiAliasing)
83  {
86  }
87 
90 
92 
93  glDisable(GL_LIGHTING); // Disable lights when drawing lines
94 
95  if (m_showPoints)
96  {
98  glColor4ub(
100 
101  for (unsigned int i = 0; i < x_p.getColCount(); i++)
102  for (unsigned int j = 0; j < x_p.getRowCount(); j++)
103  {
104  glVertex3f(x_p(j, i), y_p(j, i), z_p(j, i));
105  }
106 
107  glEnd();
108  }
109 
110  glBegin(GL_LINES);
111  if (m_colorFromModule == false)
112  {
113  glColor4ub(
115  for (unsigned int i = 0; i < x_vf.getColCount(); i++)
116  for (unsigned int j = 0; j < x_vf.getRowCount(); j++)
117  {
118  glVertex3f(x_p(j, i), y_p(j, i), z_p(j, i));
119  glVertex3f(
120  x_p(j, i) + x_vf(j, i), y_p(j, i) + y_vf(j, i),
121  z_p(j, i) + z_vf(j, i));
122  }
123  }
124  else
125  {
126  for (unsigned int i = 0; i < x_vf.getColCount(); i++)
127  for (unsigned int j = 0; j < x_vf.getRowCount(); j++)
128  {
129  // Compute color
130  const float module = sqrt(
131  square(x_vf(j, i)) + square(y_vf(j, i)) +
132  square(z_vf(j, i)));
133  if (module > m_maxspeed)
134  glColor4ub(
137  else
138  {
139  const float R =
140  (m_maxspeed - module) * m_still_color.R / m_maxspeed +
141  module * m_maxspeed_color.R / m_maxspeed;
142  const float G =
143  (m_maxspeed - module) * m_still_color.G / m_maxspeed +
144  module * m_maxspeed_color.G / m_maxspeed;
145  const float B =
146  (m_maxspeed - module) * m_still_color.B / m_maxspeed +
147  module * m_maxspeed_color.B / m_maxspeed;
148  const float A =
149  (m_maxspeed - module) * m_still_color.A / m_maxspeed +
150  module * m_maxspeed_color.A / m_maxspeed;
151  glColor4ub(R, G, B, A);
152  }
153 
154  glVertex3f(x_p(j, i), y_p(j, i), z_p(j, i));
155  glVertex3f(
156  x_p(j, i) + x_vf(j, i), y_p(j, i) + y_vf(j, i),
157  z_p(j, i) + z_vf(j, i));
158  }
159  }
160  glEnd();
161 
162  //******** Future ************
163  // glBegin(GL_TRIANGLES);
164  // glColor4ub( m_field_color.R, m_field_color.G, m_field_color.B,
165  // m_field_color.A);
166  // for (unsigned int i=0; i<xcomp.getColCount(); i++)
167  // for (unsigned int j=0; j<xcomp.getRowCount(); j++)
168  // {
169  // const float tri_side = 0.25*sqrt(xcomp(j,i)*xcomp(j,i) +
170  // ycomp(j,i)*ycomp(j,i));
171  // const float ang = ::atan2(ycomp(j,i), xcomp(j,i)) - 1.5708;
172  // glVertex3f( -sin(ang)*0.866*tri_side + xMin+i*x_cell_size +
173  // xcomp(j,i), cos(ang)*0.866*tri_side + yMin+j*y_cell_size + ycomp(j,i),
174  // 0);
175  // glVertex3f( cos(ang)*0.5*tri_side + xMin+i*x_cell_size +
176  // xcomp(j,i),
177  // sin(ang)*0.5*tri_side + yMin+j*y_cell_size + ycomp(j,i), 0);
178  // glVertex3f( -cos(ang)*0.5*tri_side + xMin+i*x_cell_size +
179  // xcomp(j,i), -sin(ang)*0.5*tri_side + yMin+j*y_cell_size + ycomp(j,i), 0);
180  // }
181  // glEnd();
182 
184  glEnable(GL_LIGHTING); // Disable lights when drawing lines
185 
186  // End of antialiasing:
187  glPopAttrib();
188 
189 #endif
190 }
191 
192 /*---------------------------------------------------------------
193  Implements the writing to a CStream capability of
194  CSerializable objects
195  ---------------------------------------------------------------*/
197  mrpt::utils::CStream& out, int* version) const
198 {
199  if (version)
200  *version = 0;
201  else
202  {
203  writeToStreamRender(out);
204 
205  out << x_vf << y_vf << z_vf;
206  out << x_p << y_p << z_p;
207  out << m_LineWidth;
208  out << m_pointSize;
209  out << m_antiAliasing;
210  out << m_point_color;
211  out << m_field_color;
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:
225 
226  in >> x_vf >> y_vf >> z_vf;
227  in >> x_p >> y_p >> z_p;
228  in >> m_LineWidth;
229  in >> m_pointSize;
230  in >> m_antiAliasing;
231  in >> m_point_color;
232  in >> m_field_color;
233  break;
234 
235  default:
237  break;
238  };
240 }
241 
243  mrpt::math::TPoint3D& bb_min, mrpt::math::TPoint3D& bb_max) const
244 {
245  bb_min.x = 10e10;
246  bb_min.y = 10e10;
247  bb_min.z = 10e10;
248  bb_max.x = -10e10;
249  bb_max.y = -10e10;
250  bb_max.z = -10e10;
251 
252  for (unsigned int i = 0; i < x_p.getColCount(); i++)
253  for (unsigned int j = 0; j < x_p.getRowCount(); j++)
254  {
255  // Minimum values
256  if (x_p(j, i) < bb_min.x) bb_min.x = x_p(j, i);
257 
258  if (x_p(j, i) + x_vf(j, i) < bb_min.x)
259  bb_min.x = x_p(j, i) + x_vf(j, i);
260 
261  if (y_p(j, i) < bb_min.y) bb_min.y = y_p(j, i);
262 
263  if (y_p(j, i) + y_vf(j, i) < bb_min.y)
264  bb_min.y = y_p(j, i) + y_vf(j, i);
265 
266  if (z_p(j, i) < bb_min.z) bb_min.z = z_p(j, i);
267 
268  if (z_p(j, i) + z_vf(j, i) < bb_min.z)
269  bb_min.z = z_p(j, i) + z_vf(j, i);
270 
271  // Maximum values
272  if (x_p(j, i) > bb_max.x) bb_max.x = x_p(j, i);
273 
274  if (x_p(j, i) + x_vf(j, i) > bb_max.x)
275  bb_max.x = x_p(j, i) + x_vf(j, i);
276 
277  if (y_p(j, i) > bb_max.y) bb_max.y = y_p(j, i);
278 
279  if (y_p(j, i) + y_vf(j, i) > bb_max.y)
280  bb_max.y = y_p(j, i) + y_vf(j, i);
281 
282  if (z_p(j, i) > bb_max.z) bb_max.z = z_p(j, i);
283 
284  if (z_p(j, i) + z_vf(j, i) > bb_max.z)
285  bb_max.z = z_p(j, i) + z_vf(j, i);
286  }
287 
288  // Convert to coordinates of my parent:
289  m_pose.composePoint(bb_min, bb_min);
290  m_pose.composePoint(bb_max, bb_max);
291 }
const float R
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
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)
void readFromStreamRender(mrpt::utils::CStream &in)
mrpt::poses::CPose3D m_pose
6D pose wrt the parent coordinate reference.
Definition: CRenderizable.h:57
void writeToStreamRender(utils::CStream &out) const
static void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
mrpt::utils::TColor m_color
Color components in the range [0,255].
Definition: CRenderizable.h:54
A 3D vector field representation, consisting of points and arrows drawn at any spatial position.
mrpt::math::CMatrix y_p
Y coordinate of the points at which the vector field is plotted.
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...
bool m_showPoints
By default it is true.
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...
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
float m_LineWidth
By default it is 1.0.
mrpt::utils::TColor m_still_color
Color associated to fields with null module.
mrpt::utils::TColor m_field_color
float m_maxspeed
Value of the module of the motion field which will correspond to 'm_maxspeed_color'.
bool m_colorFromModule
By default it is false.
bool m_antiAliasing
By default it is true.
mrpt::math::CMatrix x_vf
X component of the vector field.
float m_pointSize
By default it is 1.0.
mrpt::math::CMatrix y_vf
Y component of the vector field.
mrpt::math::CMatrix z_p
Z coordinate of the points at which the vector field is plotted.
void render_dl() const override
Render.
mrpt::utils::TColor m_maxspeed_color
Color associated to fields whose module is equal or larger than 'm_maxspeed'.
mrpt::math::CMatrix z_vf
Z component of the vector field.
mrpt::utils::TColor m_point_color
mrpt::math::CMatrix x_p
X coordinate of the points at which the vector field is plotted.
void composePoint(double lx, double ly, double lz, double &gx, double &gy, double &gz, mrpt::math::CMatrixFixedNumeric< double, 3, 3 > *out_jacobian_df_dpoint=nullptr, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dpose=nullptr, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dse3=nullptr, bool use_small_rot_approx=false) const
An alternative, slightly more efficient way of doing with G and L being 3D points and P this 6D pose...
Definition: CPose3D.cpp:453
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 glEnable(GLenum cap)
#define GL_SRC_ALPHA
Definition: glew.h:286
#define GL_LINES
Definition: glew.h:273
#define GL_POINT_SMOOTH
Definition: glew.h:363
GLAPI void GLAPIENTRY glPointSize(GLfloat size)
#define GL_LINE_BIT
Definition: glew.h:253
GLAPI void GLAPIENTRY glLineWidth(GLfloat width)
#define GL_POINTS
Definition: glew.h:272
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
#define GL_COLOR_BUFFER_BIT
Definition: glew.h:265
GLAPI void GLAPIENTRY glBegin(GLenum mode)
GLAPI void GLAPIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z)
#define GL_BLEND
Definition: glew.h:432
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:287
GLAPI void GLAPIENTRY glEnd(void)
GLAPI void GLAPIENTRY glPopAttrib(void)
GLAPI void GLAPIENTRY glDisable(GLenum cap)
GLAPI void GLAPIENTRY glColor4ub(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha)
GLAPI void GLAPIENTRY glPushAttrib(GLbitfield mask)
#define GL_LINE_SMOOTH
Definition: glew.h:367
#define GL_LIGHTING
Definition: glew.h:385
GLuint in
Definition: glext.h:7274
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: mrpt_macros.h:181
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:20
T square(const T x)
Inline function for the square of a number.
Definition: bits.h:55
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:16
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