Main MRPT website > C++ reference for MRPT 1.5.6
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 
17 using namespace mrpt;
18 using namespace mrpt::opengl;
19 using namespace mrpt::utils;
20 using namespace mrpt::math;
21 using namespace std;
22 
24 
25 /** Constructor */
27  : x_vf(0,0), y_vf(0,0), z_vf(0,0), x_p(0,0), y_p(0,0), z_p(0,0), m_LineWidth(1.0),m_pointSize(1.0),m_antiAliasing(true),m_colorFromModule(false),m_showPoints(true)
28 {
29  m_point_color = m_color;
30  m_field_color = m_color;
31  m_still_color = m_color;
32  m_maxspeed_color = m_color;
33  m_maxspeed = 1.f;
34 }
35 
36 /** Constructor with a initial set of lines. */
38  : m_LineWidth(1.0),m_pointSize(1.0),m_antiAliasing(true),m_colorFromModule(false),m_showPoints(true)
39 {
40  x_vf = x_vf_ini;
41  y_vf = y_vf_ini;
42  z_vf = z_vf_ini;
43  x_p = x_p_ini;
44  y_p = y_p_ini;
45  z_p = z_p_ini;
50  m_maxspeed = 1.f;
51 }
52 
53 
54 /*---------------------------------------------------------------
55  render
56  ---------------------------------------------------------------*/
58 {
59 #if MRPT_HAS_OPENGL_GLUT
60 
61  // Enable antialiasing:
63  if (m_antiAliasing || m_color.A != 255)
64  {
67  }
68  if (m_antiAliasing)
69  {
72  }
73 
76 
78 
79  glDisable(GL_LIGHTING); // Disable lights when drawing lines
80 
81  if (m_showPoints)
82  {
85 
86  for (unsigned int i=0; i<x_p.getColCount(); i++)
87  for (unsigned int j=0; j<x_p.getRowCount(); j++)
88  {
89  glVertex3f( x_p(j,i), y_p(j,i), z_p(j,i));
90  }
91 
92  glEnd();
93  }
94 
96  if (m_colorFromModule == false)
97  {
99  for (unsigned int i=0; i<x_vf.getColCount(); i++)
100  for (unsigned int j=0; j<x_vf.getRowCount(); j++)
101  {
102  glVertex3f( x_p(j,i), y_p(j,i), z_p(j,i));
103  glVertex3f( x_p(j,i) + x_vf(j,i), y_p(j,i) + y_vf(j,i), z_p(j,i) + z_vf(j,i));
104  }
105  }
106  else
107  {
108  for (unsigned int i=0; i<x_vf.getColCount(); i++)
109  for (unsigned int j=0; j<x_vf.getRowCount(); j++)
110  {
111  //Compute color
112  const float module = sqrt(square(x_vf(j,i)) + square(y_vf(j,i)) + square(z_vf(j,i)));
113  if (module > m_maxspeed)
115  else
116  {
117  const float R = (m_maxspeed-module)*m_still_color.R/m_maxspeed + module*m_maxspeed_color.R/m_maxspeed;
118  const float G = (m_maxspeed-module)*m_still_color.G/m_maxspeed + module*m_maxspeed_color.G/m_maxspeed;
119  const float B = (m_maxspeed-module)*m_still_color.B/m_maxspeed + module*m_maxspeed_color.B/m_maxspeed;
120  const float A = (m_maxspeed-module)*m_still_color.A/m_maxspeed + module*m_maxspeed_color.A/m_maxspeed;
121  glColor4ub( R, G, B, A);
122  }
123 
124  glVertex3f( x_p(j,i), y_p(j,i), z_p(j,i));
125  glVertex3f( x_p(j,i) + x_vf(j,i), y_p(j,i) + y_vf(j,i), z_p(j,i) + z_vf(j,i));
126  }
127  }
128  glEnd();
129 
130  //******** Future ************
131 // glBegin(GL_TRIANGLES);
132 // glColor4ub( m_field_color.R, m_field_color.G, m_field_color.B, m_field_color.A);
133 // for (unsigned int i=0; i<xcomp.getColCount(); i++)
134 // for (unsigned int j=0; j<xcomp.getRowCount(); j++)
135 // {
136 // const float tri_side = 0.25*sqrt(xcomp(j,i)*xcomp(j,i) + ycomp(j,i)*ycomp(j,i));
137 // const float ang = ::atan2(ycomp(j,i), xcomp(j,i)) - 1.5708;
138 // glVertex3f( -sin(ang)*0.866*tri_side + xMin+i*x_cell_size + xcomp(j,i), cos(ang)*0.866*tri_side + yMin+j*y_cell_size + ycomp(j,i), 0);
139 // glVertex3f( cos(ang)*0.5*tri_side + xMin+i*x_cell_size + xcomp(j,i), sin(ang)*0.5*tri_side + yMin+j*y_cell_size + ycomp(j,i), 0);
140 // glVertex3f( -cos(ang)*0.5*tri_side + xMin+i*x_cell_size + xcomp(j,i), -sin(ang)*0.5*tri_side + yMin+j*y_cell_size + ycomp(j,i), 0);
141 // }
142 // glEnd();
143 
145  glEnable(GL_LIGHTING); // Disable lights when drawing lines
146 
147  // End of antialiasing:
148  glPopAttrib();
149 
150 #endif
151 }
152 
153 /*---------------------------------------------------------------
154  Implements the writing to a CStream capability of
155  CSerializable objects
156  ---------------------------------------------------------------*/
158 {
159  if (version)
160  *version = 0;
161  else
162  {
163  writeToStreamRender(out);
164 
165  out << x_vf << y_vf << z_vf;
166  out << x_p << y_p << z_p;
167  out << m_LineWidth;
168  out << m_pointSize;
169  out << m_antiAliasing;
170  out << m_point_color;
171  out << m_field_color;
172  }
173 }
174 
175 /*---------------------------------------------------------------
176  Implements the reading from a CStream capability of
177  CSerializable objects
178  ---------------------------------------------------------------*/
180 {
181  switch(version)
182  {
183  case 0:
185 
186  in >> x_vf >> y_vf >> z_vf;
187  in >> x_p >> y_p >> z_p;
188  in >> m_LineWidth;
189  in >> m_pointSize;
190  in >> m_antiAliasing;
191  in >> m_point_color;
192  in >> m_field_color;
193  break;
194 
195  default:
197  break;
198  };
200 }
201 
202 
204 {
205  bb_min.x = 10e10; bb_min.y = 10e10; bb_min.z = 10e10;
206  bb_max.x = -10e10; bb_max.y = -10e10; bb_max.z = -10e10;
207 
208  for (unsigned int i=0; i<x_p.getColCount(); i++)
209  for (unsigned int j=0; j<x_p.getRowCount(); j++)
210  {
211  //Minimum values
212  if (x_p(j,i) < bb_min.x)
213  bb_min.x = x_p(j,i);
214 
215  if (x_p(j,i) + x_vf(j,i) < bb_min.x)
216  bb_min.x = x_p(j,i) + x_vf(j,i);
217 
218  if (y_p(j,i) < bb_min.y)
219  bb_min.y = y_p(j,i);
220 
221  if (y_p(j,i) + y_vf(j,i) < bb_min.y)
222  bb_min.y = y_p(j,i) + y_vf(j,i);
223 
224  if (z_p(j,i) < bb_min.z)
225  bb_min.z = z_p(j,i);
226 
227  if (z_p(j,i) + z_vf(j,i) < bb_min.z)
228  bb_min.z = z_p(j,i) + z_vf(j,i);
229 
230  //Maximum values
231  if (x_p(j,i) > bb_max.x)
232  bb_max.x = x_p(j,i);
233 
234  if (x_p(j,i) + x_vf(j,i) > bb_max.x)
235  bb_max.x = x_p(j,i) + x_vf(j,i);
236 
237  if (y_p(j,i) > bb_max.y)
238  bb_max.y = y_p(j,i);
239 
240  if (y_p(j,i) + y_vf(j,i) > bb_max.y)
241  bb_max.y = y_p(j,i) + y_vf(j,i);
242 
243  if (z_p(j,i) > bb_max.z)
244  bb_max.z = z_p(j,i);
245 
246  if (z_p(j,i) + z_vf(j,i) > bb_max.z)
247  bb_max.z = z_p(j,i) + z_vf(j,i);
248  }
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 
mrpt::utils::TColor m_point_color
mrpt::math::CMatrix x_vf
X component of the vector field.
float m_LineWidth
By default it is 1.0.
void writeToStreamRender(utils::CStream &out) const
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
void render_dl() const MRPT_OVERRIDE
Render.
GLAPI void GLAPIENTRY glEnable(GLenum cap)
GLAPI void GLAPIENTRY glPointSize(GLfloat size)
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
EIGEN_STRONG_INLINE void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
mrpt::utils::TColor m_still_color
Color associated to fields with null module.
STL namespace.
mrpt::utils::TColor m_field_color
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const
Introduces a pure virtual method responsible for writing to a CStream.
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:283
float m_maxspeed
Value of the module of the motion field which will correspond to &#39;m_maxspeed_color&#39;.
double z
X,Y,Z coordinates.
GLAPI void GLAPIENTRY glPopAttrib(void)
float m_pointSize
By default it is 1.0.
#define GL_LIGHTING
Definition: glew.h:381
GLAPI void GLAPIENTRY glLineWidth(GLfloat width)
mrpt::poses::CPose3D m_pose
6D pose wrt the parent coordinate reference. This class automatically holds the cached 3x3 rotation m...
Definition: CRenderizable.h:55
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
T square(const T x)
Inline function for the square of a number.
Definition: bits.h:52
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
bool m_showPoints
By default it is true.
#define GL_COLOR_BUFFER_BIT
Definition: glew.h:261
void composePoint(double lx, double ly, double lz, double &gx, double &gy, double &gz, mrpt::math::CMatrixFixedNumeric< double, 3, 3 > *out_jacobian_df_dpoint=NULL, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dpose=NULL, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dse3=NULL, 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:427
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
mrpt::math::CMatrix y_p
Y coordinate of the points at which the vector field is plotted.
#define GL_LINE_SMOOTH
Definition: glew.h:363
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
void readFromStreamRender(mrpt::utils::CStream &in)
mrpt::math::CMatrix z_vf
Z component of the vector field.
mrpt::math::CMatrix y_vf
Y component of the vector field.
mrpt::utils::TColor m_color
Color components in the range [0,255].
Definition: CRenderizable.h:54
#define GL_POINT_SMOOTH
Definition: glew.h:359
int version
Definition: mrpt_jpeglib.h:898
GLAPI void GLAPIENTRY glBegin(GLenum mode)
#define GL_BLEND
Definition: glew.h:428
#define GL_POINTS
Definition: glew.h:268
mrpt::math::CMatrix z_p
Z coordinate of the points at which the vector field is plotted.
A 3D vector field representation, consisting of points and arrows drawn at any spatial position...
GLAPI void GLAPIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z)
GLAPI void GLAPIENTRY glColor4ub(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha)
#define GL_SRC_ALPHA
Definition: glew.h:282
static void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
mrpt::utils::TColor m_maxspeed_color
Color associated to fields whose module is equal or larger than &#39;m_maxspeed&#39;.
bool m_colorFromModule
By default it is false.
void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const MRPT_OVERRIDE
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
GLAPI void GLAPIENTRY glPushAttrib(GLbitfield mask)
const float R
void readFromStream(mrpt::utils::CStream &in, int version)
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
GLuint in
Definition: glext.h:6301
The namespace for 3D scene representation and rendering.
GLAPI void GLAPIENTRY glEnd(void)
#define GL_LINES
Definition: glew.h:269
bool m_antiAliasing
By default it is true.
Lightweight 3D point.
GLAPI void GLAPIENTRY glDisable(GLenum cap)
#define GL_LINE_BIT
Definition: glew.h:249
mrpt::math::CMatrix x_p
X coordinate of the points at which the vector field is plotted.



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019