Main MRPT website > C++ reference for MRPT 1.5.9
CVectorField2D.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 
15 #include "opengl_internals.h"
16 
17 
18 using namespace mrpt;
19 using namespace mrpt::opengl;
20 using namespace mrpt::utils;
21 using namespace mrpt::math;
22 using namespace std;
23 
25 
26 CVectorField2DPtr CVectorField2D::Create(const CMatrixFloat &Matrix_x, const CMatrixFloat &Matrix_y, float xmin, float xmax, float ymin, float ymax)
27 {
28  return CVectorField2DPtr(new CVectorField2D( Matrix_x, Matrix_y, xmin, xmax, ymin, ymax));
29 }
30 
31 /** Constructor */
33  : xcomp(0,0), ycomp(0,0), xMin(-1.0), xMax(1.0), yMin(-1.0), yMax(1.0), m_LineWidth(1.0),m_pointSize(1.0),m_antiAliasing(true)
34 {
37 }
38 
39 /** Constructor with a initial set of lines. */
40 CVectorField2D::CVectorField2D( CMatrixFloat Matrix_x, CMatrixFloat Matrix_y, float xmin, float xmax, float ymin, float ymax)
41  : m_LineWidth(1.0),m_pointSize(1.0),m_antiAliasing(true)
42 {
43  MRPT_UNUSED_PARAM(Matrix_x); MRPT_UNUSED_PARAM(Matrix_y);
47 }
48 
49 
50 /*---------------------------------------------------------------
51  render
52  ---------------------------------------------------------------*/
54 {
55 #if MRPT_HAS_OPENGL_GLUT
56 
57  // Enable antialiasing:
59  if (m_antiAliasing || m_color.A != 255)
60  {
63  }
64  if (m_antiAliasing)
65  {
68  }
69 
72 
74 
75  glDisable(GL_LIGHTING); // Disable lights when drawing lines
78 
79  const float x_cell_size = (xMax - xMin)/(xcomp.getColCount()-1);
80  const float y_cell_size = (yMax - yMin)/(ycomp.getRowCount()-1);
81 
82  for (unsigned int i=0; i<xcomp.getColCount(); i++)
83  for (unsigned int j=0; j<xcomp.getRowCount(); j++)
84  {
85  glVertex3f( xMin+i*x_cell_size, yMin+j*y_cell_size, 0);
86  }
87 
88  glEnd();
89 
92  for (unsigned int i=0; i<xcomp.getColCount(); i++)
93  for (unsigned int j=0; j<xcomp.getRowCount(); j++)
94  {
95  glVertex3f( xMin+i*x_cell_size, yMin+j*y_cell_size, 0);
96  glVertex3f( xMin+i*x_cell_size + xcomp(j,i), yMin+j*y_cell_size + ycomp(j,i), 0);
97  }
98  glEnd();
99 
102  for (unsigned int i=0; i<xcomp.getColCount(); i++)
103  for (unsigned int j=0; j<xcomp.getRowCount(); j++)
104  {
105  const float tri_side = 0.25*sqrt(xcomp(j,i)*xcomp(j,i) + ycomp(j,i)*ycomp(j,i));
106  const float ang = ::atan2(ycomp(j,i), xcomp(j,i)) - 1.5708;
107  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);
108  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);
109  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);
110  }
111  glEnd();
112 
114  glEnable(GL_LIGHTING); // Disable lights when drawing lines
115 
116  // End of antialiasing:
117  glPopAttrib();
118 
119 #endif
120 }
121 
122 /*---------------------------------------------------------------
123  Implements the writing to a CStream capability of
124  CSerializable objects
125  ---------------------------------------------------------------*/
127 {
128  if (version)
129  *version = 0;
130  else
131  {
132  writeToStreamRender(out);
133 
134  out << xcomp << ycomp;
135  out << xMin << xMax << yMin << yMax;
136  out << m_LineWidth;
137  out << m_pointSize;
138  out << m_antiAliasing;
139  out << m_point_color;
140  out << m_field_color;
141 
142  }
143 }
144 
145 /*---------------------------------------------------------------
146  Implements the reading from a CStream capability of
147  CSerializable objects
148  ---------------------------------------------------------------*/
150 {
151  switch(version)
152  {
153  case 0:
155 
156  in >> xcomp >> ycomp;
157  in >> xMin >> xMax >> yMin >> yMax;
158  in >> m_LineWidth;
159  in >> m_pointSize;
160  in >> m_antiAliasing;
161  in >> m_point_color;
162  in >> m_field_color;
163  break;
164 
165  default:
167  break;
168  };
170 }
171 
172 
174 {
175  bb_min.x = xMin;
176  bb_min.y = yMin;
177  bb_min.z = 0;
178 
179  bb_max.x = xMax;
180  bb_max.y = yMax;
181  bb_max.z = 0;
182 
183  const float x_cell_size = (xMax - xMin)/(xcomp.getColCount()-1);
184  const float y_cell_size = (yMax - yMin)/(ycomp.getRowCount()-1);
185 
186  for (unsigned int i=0; i<xcomp.getColCount(); i++)
187  for (unsigned int j=0; j<xcomp.getRowCount(); j++)
188  {
189  const float tri_side = 0.25*sqrt(xcomp(j,i)*xcomp(j,i) + ycomp(j,i)*ycomp(j,i));
190  const float ang = ::atan2(ycomp(j,i), xcomp(j,i)) - 1.5708;
191 
192  if ( -sin(ang)*0.866*tri_side + xMin+i*x_cell_size + xcomp(j,i) < bb_min.x)
193  bb_min.x = -sin(ang)*0.866*tri_side + xMin+i*x_cell_size + xcomp(j,i);
194 
195  if ( cos(ang)*0.866*tri_side + yMin+j*y_cell_size + ycomp(j,i) < bb_min.y)
196  bb_min.y = cos(ang)*0.866*tri_side + yMin+j*y_cell_size + ycomp(j,i);
197 
198  if ( -sin(ang)*0.866*tri_side + xMin+i*x_cell_size + xcomp(j,i) > bb_max.x)
199  bb_max.x = -sin(ang)*0.866*tri_side + xMin+i*x_cell_size + xcomp(j,i);
200 
201  if ( cos(ang)*0.866*tri_side + yMin+j*y_cell_size + ycomp(j,i) > bb_max.y)
202  bb_max.y = cos(ang)*0.866*tri_side + yMin+j*y_cell_size + ycomp(j,i);
203  }
204 
205  // Convert to coordinates of my parent:
206  m_pose.composePoint(bb_min, bb_min);
207  m_pose.composePoint(bb_max, bb_max);
208 }
209 
211 {
212  ASSERT_(xcomp.size() > 0)
213 
214  const float ratio_xp = xcomp.maxCoeff()*(xcomp.getColCount()-1)/(xMax-xMin);
215  const float ratio_xn = xcomp.minCoeff()*(xcomp.getColCount()-1)/(xMax-xMin);
216  const float ratio_yp = ycomp.maxCoeff()*(ycomp.getRowCount()-1)/(yMax-yMin);
217  const float ratio_yn = ycomp.minCoeff()*(ycomp.getRowCount()-1)/(yMax-yMin);
218  const float norm_factor = 0.85/max(max(ratio_xp, abs(ratio_xn)), max(ratio_yp, abs(ratio_yn)));
219 
220  xcomp *= norm_factor;
221  ycomp *= norm_factor;
223 }
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
GLAPI void GLAPIENTRY glEnable(GLenum cap)
GLAPI void GLAPIENTRY glPointSize(GLfloat size)
mrpt::math::CMatrix ycomp
Y component of the vector field.
#define GL_TRIANGLES
Definition: glew.h:272
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.
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:283
double z
X,Y,Z coordinates.
mrpt::math::CMatrix xcomp
X component of the vector field.
GLAPI void GLAPIENTRY glPopAttrib(void)
IMPLEMENTS_SERIALIZABLE(CLogFileRecord_FullEval, CHolonomicLogFileRecord, mrpt::nav) IMPLEMENTS_SERIALIZABLE(CHolonomicFullEval
#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)
mrpt::utils::TColor m_point_color
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
#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:439
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
#define GL_LINE_SMOOTH
Definition: glew.h:363
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
mrpt::utils::TColor m_field_color
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
void readFromStreamRender(mrpt::utils::CStream &in)
void render_dl() const MRPT_OVERRIDE
Render.
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...
mrpt::utils::TColor m_color
Color components in the range [0,255].
Definition: CRenderizable.h:54
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...
#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
A 2D vector field representation, consisting of points and arrows drawn on a plane (invisible grid)...
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.
bool m_antiAliasing
By default is true.
GLAPI void GLAPIENTRY glPushAttrib(GLbitfield mask)
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const
Introduces a pure virtual method responsible for writing to a CStream.
GLuint in
Definition: glext.h:6301
The namespace for 3D scene representation and rendering.
#define ASSERT_(f)
void adjustVectorFieldToGrid()
Adjust the vector field in the scene (vectors magnitude) according to the grid size.
GLAPI void GLAPIENTRY glEnd(void)
float m_pointSize
By default is 1.0.
#define GL_LINES
Definition: glew.h:269
Lightweight 3D point.
GLAPI void GLAPIENTRY glDisable(GLenum cap)
float m_LineWidth
By default is 1.0.
#define GL_LINE_BIT
Definition: glew.h:249



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020