Main MRPT website > C++ reference
MRPT logo
CVectorField2D.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2014, 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 #ifndef opengl_CVectorField2D_H
11 #define opengl_CVectorField2D_H
12 
15 #include <mrpt/math/CMatrix.h>
16 
17 namespace mrpt
18 {
19  namespace opengl
20  {
23  using mrpt::math::CMatrix;
25  using mrpt::utils::TColor;
27 
28 
29  // This must be added to any CSerializable derived class:
30  DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE( CVectorField2D, CRenderizableDisplayList, OPENGL_IMPEXP )
31 
32  /** A 2D vector field representation, consisting of points and arrows drawn on a plane (invisible grid).
33  * \sa opengl::COpenGLScene
34  *
35  * <div align="center">
36  * <table border="0" cellspan="4" cellspacing="4" style="border-width: 1px; border-style: solid;">
37  * <tr> <td> mrpt::opengl::CVectorField2D </td> <td> \image html preview_CVectorField2D.png </td> </tr>
38  * </table>
39  * </div>
40  *
41  * \ingroup mrpt_opengl_grp
42  */
43 
45  {
47  protected:
48  CMatrix xcomp; //!< X component of the vector field
49  CMatrix ycomp; //!< Y component of the vector field
50 
51  float xMin,xMax,yMin,yMax; //!< Grid bounds
52  float m_LineWidth; //!< By default is 1.0
53  float m_pointSize; //!< By default is 1.0
54  bool m_antiAliasing; //!< By default is true
55 
56  TColor m_point_color;
57  TColor m_field_color;
58 
59  public:
60  /**
61  * Clear the matrices
62  */
63  inline void clear() {
64  xcomp.resize(0,0);
65  ycomp.resize(0,0);
67  }
68 
69  /**
70  * Set the point color in the range [0,1]
71  */
72  inline void setPointColor( const float R, const float G, const float B, const float A = 1)
73  {
74  m_point_color = TColor(R*255,G*255,B*255,A*255);
76  }
77 
78  /**
79  * Get the point color in the range [0,1]
80  */
81  inline TColorf getPointColor() const { return mrpt::utils::TColorf(m_point_color); }
82 
83  /**
84  * Set the arrow color in the range [0,1]
85  */
86  inline void setVectorFieldColor( const float R, const float G, const float B, const float A = 1)
87  {
88  m_field_color = TColor(R*255,G*255,B*255,A*255);
90  }
91 
92  /**
93  * Get the arrow color in the range [0,1]
94  */
95  inline TColorf getVectorFieldColor() const { return mrpt::utils::TColorf(m_field_color); }
96 
97  /**
98  * Set the size with which points will be drawn. By default 1.0
99  */
100  inline void setPointSize(const float p) { m_pointSize=p; CRenderizableDisplayList::notifyChange(); }
101 
102  /**
103  * Get the size with which points are drawn. By default 1.0
104  */
105  inline float getPointSize() const { return m_pointSize; }
106 
107  /**
108  * Set the width with which lines will be drawn.
109  */
110  inline void setLineWidth(const float w) { m_LineWidth = w; CRenderizableDisplayList::notifyChange(); }
111 
112  /**
113  * Get the width with which lines are drawn.
114  */
115  float getLineWidth() const {
116  return m_LineWidth;
117  }
118 
119  /**
120  * Set the coordinates of the grid on where the vector field will be drawn by setting its center and the cell size.
121  * The number of cells is marked by the content of xcomp and ycomp.
122  * \sa xcomp, ycomp
123  */
124  void setGridCenterAndCellSize(const float center_x, const float center_y, const float cellsize_x, const float cellsize_y)
125  {
126  xMin = center_x - 0.5*cellsize_x*(xcomp.getColCount()-1);
127  xMax = center_x + 0.5*cellsize_x*(xcomp.getColCount()-1);
128  yMin = center_y - 0.5*cellsize_y*(xcomp.getRowCount()-1);
129  yMax = center_y + 0.5*cellsize_y*(xcomp.getRowCount()-1);
131  }
132 
133  /**
134  * Set the coordinates of the grid on where the vector field will be drawn using x-y max and min values.
135  */
136  void setGridLimits(const float xmin,const float xmax, const float ymin, const float ymax)
137  {
138  xMin=xmin; xMax = xmax;
139  yMin=ymin; yMax = ymax;
141  }
142 
143  /**
144  * Get the coordinates of the grid on where the vector field is drawn using the max and min values.
145  */
146  void getGridLimits(float &xmin,float &xmax, float &ymin, float &ymax) const
147  {
148  xmin=xMin; xmax=xMax;
149  ymin=yMin; ymax=yMax;
150  }
151 
152  /**
153  * Get the vector field. Matrix_x stores the "x" component and Matrix_y stores the "y" component.
154  */
155  void getVectorField(CMatrixFloat &Matrix_x, CMatrixFloat &Matrix_y) const {
156  Matrix_x = xcomp;
157  Matrix_y = ycomp;
158  }
159 
160  /** Get the "x" component of the vector field, as a matrix where each entry represents a point in the 2D grid. */
161  inline const CMatrixFloat & getVectorField_x() const { return xcomp; }
162  /** \overload */
163  inline CMatrixFloat & getVectorField_x() { return xcomp; }
164 
165  /** Get the "y" component of the vector field, as a matrix where each entry represents a point in the 2D grid. */
166  inline const CMatrixFloat & getVectorField_y() const { return ycomp; }
167  /** \overload */
168  inline CMatrixFloat & getVectorField_y() { return ycomp; }
169 
170  /**
171  * Set the vector field. Matrix_x contains the "x" component and Matrix_y contains the "y" component.
172  */
173  void setVectorField(CMatrixFloat &Matrix_x, CMatrixFloat &Matrix_y) {
174  ASSERT_((Matrix_x.getRowCount() == Matrix_y.getRowCount())&&(Matrix_x.getColCount() == Matrix_y.getColCount()))
175  xcomp = Matrix_x;
176  ycomp = Matrix_y;
178  }
179 
180  /**
181  * Adjust the vector field in the scene (vectors magnitude) according to the grid size.
182  */
183  void adjustVectorFieldToGrid();
184 
185  /**
186  * Resizes the set.
187  * \sa reserve
188  */
189  void resize(size_t rows, size_t cols) {
190  xcomp.resize(rows, cols);
191  ycomp.resize(rows, cols);
193  }
194  /**
195  * Reserves an amount of lines to the set. This method should be used when some known amount of lines is going to be inserted, so that only a memory allocation is needed.
196  * \sa resize
197  */
198  void reserve(size_t rows, size_t cols) {
199  xcomp.resize(rows, cols);
200  ycomp.resize(rows, cols);
202  }
203 
204  /** Returns the total count of rows used to represent the vector field. */
205  inline size_t getColCount() const { return xcomp.getColCount(); }
206 
207  /** Returns the total count of columns used to represent the vector field. */
208  inline size_t getRowCount() const { return xcomp.getRowCount(); }
209 
210  /**
211  * Class factory
212  */
213  static CVectorField2DPtr Create(const CMatrixFloat Matrix_x, const CMatrixFloat Matrix_y, float xmin=-1, float xmax=1, float ymin=-1, float ymax=1);
214  /** Render
215  */
216  void render_dl() const;
217 
218 
219  /** Evaluates the bounding box of this object (including possible children) in the coordinate frame of the object parent. */
220  virtual void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const;
221 
222  void enableAntiAliasing(bool enable=true) { m_antiAliasing = enable; CRenderizableDisplayList::notifyChange(); }
223  bool isAntiAliasingEnabled() const { return m_antiAliasing; }
224 
225  private:
226  /** Constructor */
227  CVectorField2D();
228  /** Constructor with a initial set of lines. */
229  CVectorField2D( CMatrixFloat Matrix_x, CMatrixFloat Matrix_y, float xmin=-1, float xmax=1, float ymin=-1, float ymax=1);
230  /** Private, virtual destructor: only can be deleted from smart pointers. */
231  virtual ~CVectorField2D() { }
232  };
233  DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE( CVectorField2D, CRenderizableDisplayList, OPENGL_IMPEXP )
234 
235 
236  } // end namespace
237 
238 } // End of namespace
239 
240 
241 #endif
virtual ~CVectorField2D()
Private, virtual destructor: only can be deleted from smart pointers.
void enableAntiAliasing(bool enable=true)
float getPointSize() const
Get the size with which points are drawn.
float getLineWidth() const
Get the width with which lines are drawn.
EIGEN_STRONG_INLINE void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
struct BASE_IMPEXP TSegment3D
TColorf getPointColor() const
Get the point color in the range [0,1].
const CMatrixFloat & getVectorField_y() const
Get the "y" component of the vector field, as a matrix where each entry represents a point in the 2D ...
TColorf getVectorFieldColor() const
Get the arrow color in the range [0,1].
A renderizable object suitable for rendering with OpenGL's display lists.
CMatrixFloat & getVectorField_x()
void setPointSize(const float p)
Set the size with which points will be drawn.
#define DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
This declaration must be inserted in all CSerializable classes definition, before the class declarati...
void reserve(size_t rows, size_t cols)
Reserves an amount of lines to the set.
void resize(size_t rows, size_t cols)
Resizes the set.
A RGB color - 8bit.
Definition: TColor.h:25
size_t getRowCount() const
Returns the total count of columns used to represent the vector field.
CMatrixTemplateNumeric< float > CMatrixFloat
Declares a matrix of float numbers (non serializable).
void setGridLimits(const float xmin, const float xmax, const float ymin, const float ymax)
Set the coordinates of the grid on where the vector field will be drawn using x-y max and min values...
void setVectorField(CMatrixFloat &Matrix_x, CMatrixFloat &Matrix_y)
Set the vector field.
A 2D vector field representation, consisting of points and arrows drawn on a plane (invisible grid)...
void setLineWidth(const float w)
Set the width with which lines will be drawn.
size_t getColCount() const
Returns the total count of rows used to represent the vector field.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
void setGridCenterAndCellSize(const float center_x, const float center_y, const float cellsize_x, const float cellsize_y)
Set the coordinates of the grid on where the vector field will be drawn by setting its center and the...
void setPointColor(const float R, const float G, const float B, const float A=1)
Set the point color in the range [0,1].
void getVectorField(CMatrixFloat &Matrix_x, CMatrixFloat &Matrix_y) const
Get the vector field.
const CMatrixFloat & getVectorField_x() const
Get the "x" component of the vector field, as a matrix where each entry represents a point in the 2D ...
#define ASSERT_(f)
A RGB color - floats in the range [0,1].
Definition: TColor.h:52
Lightweight 3D point.
void getGridLimits(float &xmin, float &xmax, float &ymin, float &ymax) const
Get the coordinates of the grid on where the vector field is drawn using the max and min values...
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrix.h:30
#define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
void setVectorFieldColor(const float R, const float G, const float B, const float A=1)
Set the arrow color in the range [0,1].
CMatrixFloat & getVectorField_y()



Page generated by Doxygen 1.8.8 for MRPT 1.2.2 SVN:Unversioned directory at Tue Oct 14 02:14:08 UTC 2014