Main MRPT website > C++ reference for MRPT 1.9.9
CPose2D.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-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 #ifndef CPOSE2D_H
10 #define CPOSE2D_H
11 
12 #include <mrpt/poses/CPose.h>
14 
15 namespace mrpt
16 {
17 namespace poses
18 {
19 /** A class used to store a 2D pose, including the 2D coordinate point and a
20  * heading (phi) angle.
21  * Use this class instead of lightweight mrpt::math::TPose2D when pose/point
22  * composition is to be called
23  * multiple times with the same pose, since this class caches calls to
24  * expensive trigronometric functions.
25  *
26  * For a complete description of Points/Poses, see mrpt::poses::CPoseOrPoint,
27  * or refer to [this documentation page]
28  *(http://www.mrpt.org/tutorials/programming/maths-and-geometry/2d_3d_geometry/)
29  *
30  * <div align=center>
31  * <img src="CPose2D.gif">
32  * </div>
33  *
34  * \note Read also: "A tutorial on SE(3) transformation parameterizations and
35  * on-manifold optimization", Jose-Luis Blanco.
36  * http://ingmec.ual.es/~jlblanco/papers/jlblanco2010geometry3D_techrep.pdf
37  * \sa CPoseOrPoint,CPoint2D
38  * \ingroup poses_grp
39  */
40 class CPose2D : public CPose<CPose2D>, public mrpt::utils::CSerializable
41 {
42  public:
44 
45  public:
46  /** [x,y] */
48 
49  protected:
50  /** The orientation of the pose, in radians. */
51  double m_phi;
52  /** Precomputed cos() & sin() of phi. */
53  mutable double m_cosphi, m_sinphi;
54  mutable bool m_cossin_uptodate;
55 
56  inline void update_cached_cos_sin() const
57  {
58  if (m_cossin_uptodate) return;
59 #ifdef HAVE_SINCOS
60  ::sincos(m_phi, &m_sinphi, &m_cosphi);
61 #else
62  m_cosphi = ::cos(m_phi);
63  m_sinphi = ::sin(m_phi);
64 #endif
65  m_cossin_uptodate = true;
66  }
67 
68  public:
69  /** Default constructor (all coordinates to 0) */
70  CPose2D();
71 
72  /** Constructor from an initial value of the pose.*/
73  CPose2D(const double x, const double y, const double phi);
74 
75  /** Constructor from a CPoint2D object. */
76  explicit CPose2D(const CPoint2D&);
77 
78  /** Aproximation!! Avoid its use, since information is lost. */
79  explicit CPose2D(const CPose3D&);
80 
81  /** Constructor from lightweight object. */
82  explicit CPose2D(const mrpt::math::TPose2D&);
83 
84  /** Constructor from CPoint3D with information loss. */
85  explicit CPose2D(const CPoint3D&);
86 
87  /** Fast constructor that leaves all the data uninitialized - call with
88  * UNINITIALIZED_POSE as argument */
90  /** Get the phi angle of the 2D pose (in radians) */
91  inline const double& phi() const { return m_phi; }
92  //! \overload
93  inline double& phi()
94  {
95  m_cossin_uptodate = false;
96  return m_phi;
97  } //-V659
98 
99  /** Get a (cached) value of cos(phi), recomputing it only once when phi
100  * changes. */
101  inline double phi_cos() const
102  {
104  return m_cosphi;
105  }
106  /** Get a (cached) value of sin(phi), recomputing it only once when phi
107  * changes. */
108  inline double phi_sin() const
109  {
111  return m_sinphi;
112  }
113 
114  /** Set the phi angle of the 2D pose (in radians) */
115  inline void phi(double angle)
116  {
117  m_phi = angle;
118  m_cossin_uptodate = false;
119  }
120 
121  /** Increment the PHI angle (without checking the 2 PI range, call
122  * normalizePhi is needed) */
123  inline void phi_incr(const double Aphi)
124  {
125  m_phi += Aphi;
126  m_cossin_uptodate = false;
127  }
128 
129  /** Returns a 1x3 vector with [x y phi] */
131  /// \overload
133 
134  /** Returns the corresponding 4x4 homogeneous transformation matrix for the
135  * point(translation) or pose (translation+orientation).
136  * \sa getInverseHomogeneousMatrix
137  */
139 
140  /** Returns the SE(2) 2x2 rotation matrix */
142  /** Returns the equivalent SE(3) 3x3 rotation matrix, with (2,2)=1. */
144 
146  {
149  return R;
150  }
151 
152  /** The operator \f$ a = this \oplus D \f$ is the pose compounding operator.
153  */
154  CPose2D operator+(const CPose2D& D) const;
155 
156  /** Makes \f$ this = A \oplus B \f$
157  * \note A or B can be "this" without problems. */
158  void composeFrom(const CPose2D& A, const CPose2D& B);
159 
160  /** The operator \f$ a = this \oplus D \f$ is the pose compounding operator.
161  */
162  CPose3D operator+(const CPose3D& D) const;
163 
164  /** The operator \f$ u' = this \oplus u \f$ is the pose/point compounding
165  * operator. */
166  CPoint2D operator+(const CPoint2D& u) const;
167 
168  /** An alternative, slightly more efficient way of doing \f$ G = P \oplus L
169  * \f$ with G and L being 2D points and P this 2D pose. */
170  void composePoint(double lx, double ly, double& gx, double& gy) const;
171 
172  /** overload \f$ G = P \oplus L \f$ with G and L being 2D points and P this
173  * 2D pose */
174  void composePoint(
175  const mrpt::math::TPoint2D& l, mrpt::math::TPoint2D& g) const;
176 
177  /** overload \f$ G = P \oplus L \f$ with G and L being 3D points and P this
178  * 2D pose (the "z" coordinate remains unmodified) */
179  void composePoint(
180  const mrpt::math::TPoint3D& l, mrpt::math::TPoint3D& g) const;
181  /** overload (the "z" coordinate remains unmodified) */
182  void composePoint(
183  double lx, double ly, double lz, double& gx, double& gy,
184  double& gz) const;
185 
186  /** Computes the 2D point L such as \f$ L = G \ominus this \f$. \sa
187  * composePoint, composeFrom */
188  void inverseComposePoint(
189  const double gx, const double gy, double& lx, double& ly) const;
190  /** \overload */
191  inline void inverseComposePoint(
193  {
194  inverseComposePoint(g.x, g.y, l.x, l.y);
195  }
196 
197  /** The operator \f$ u' = this \oplus u \f$ is the pose/point compounding
198  * operator. */
199  CPoint3D operator+(const CPoint3D& u) const;
200 
201  /** Makes \f$ this = A \ominus B \f$ this method is slightly more efficient
202  * than "this= A - B;" since it avoids the temporary object.
203  * \note A or B can be "this" without problems.
204  * \sa composeFrom, composePoint
205  */
206  void inverseComposeFrom(const CPose2D& A, const CPose2D& B);
207 
208  /** Convert this pose into its inverse, saving the result in itself. \sa
209  * operator- */
210  void inverse();
211 
212  /** Compute \f$ RET = this \ominus b \f$ */
213  inline CPose2D operator-(const CPose2D& b) const
214  {
216  ret.inverseComposeFrom(*this, b);
217  return ret;
218  }
219 
220  /** The operator \f$ a \ominus b \f$ is the pose inverse compounding
221  * operator. */
222  CPose3D operator-(const CPose3D& b) const;
223 
224  /** Scalar sum of components: This is diferent from poses
225  * composition, which is implemented as "+" operators in "CPose" derived
226  * classes.
227  */
228  void AddComponents(const CPose2D& p);
229 
230  /** Scalar multiplication.
231  */
232  void operator*=(const double s);
233 
234  /** Make \f$ this = this \oplus b \f$ */
235  CPose2D& operator+=(const CPose2D& b);
236 
237  /** Forces "phi" to be in the range [-pi,pi];
238  */
239  void normalizePhi();
240 
241  /** Return the opposite of the current pose instance by taking the negative
242  * of all its components \a individually
243  */
244  CPose2D getOppositeScalar() const;
245 
246  /** Returns a human-readable textual representation of the object (eg: "[x y
247  * yaw]", yaw in degrees)
248  * \sa fromString
249  */
250  void asString(std::string& s) const;
251  inline std::string asString() const
252  {
253  std::string s;
254  asString(s);
255  return s;
256  }
257 
258  /** Set the current object value from a string generated by 'asString' (eg:
259  * "[0.02 1.04 -0.8]" )
260  * \sa asString
261  * \exception std::exception On invalid format
262  */
263  void fromString(const std::string& s);
264  /** Same as fromString, but without requiring the square brackets in the
265  * string */
266  void fromStringRaw(const std::string& s);
267 
268  inline const double& operator[](unsigned int i) const
269  {
270  switch (i)
271  {
272  case 0:
273  return m_coords[0];
274  case 1:
275  return m_coords[1];
276  case 2:
277  return m_phi;
278  default:
279  throw std::runtime_error(
280  "CPose2D::operator[]: Index of bounds.");
281  }
282  }
283  inline double& operator[](unsigned int i)
284  {
285  switch (i)
286  {
287  case 0:
288  return m_coords[0];
289  case 1:
290  return m_coords[1];
291  case 2:
292  return m_phi;
293  default:
294  throw std::runtime_error(
295  "CPose2D::operator[]: Index of bounds.");
296  }
297  }
298 
299  /** makes: this = p (+) this */
301  {
302  composeFrom(p, CPose2D(*this));
303  }
304 
305  /** Returns the 2D distance from this pose/point to a 2D pose using the
306  * Frobenius distance. */
307  double distance2DFrobeniusTo(const CPose2D& p) const;
308 
309  /** Used to emulate CPosePDF types, for example, in
310  * mrpt::graphs::CNetworkOfPoses */
312  enum
313  {
315  };
316  static inline bool is_3D() { return is_3D_val != 0; }
317  enum
318  {
320  };
321  enum
322  {
324  };
325  static inline bool is_PDF() { return is_PDF_val != 0; }
326  inline const type_value& getPoseMean() const { return *this; }
327  inline type_value& getPoseMean() { return *this; }
328  void setToNaN() override;
329 
330  /** @name STL-like methods and typedefs
331  @{ */
332  /** The type of the elements */
333  typedef double value_type;
334  typedef double& reference;
335  typedef const double& const_reference;
336  typedef std::size_t size_type;
338 
339  // size is constant
340  enum
341  {
343  };
344  static inline size_type size() { return static_size; }
345  static inline bool empty() { return false; }
346  static inline size_type max_size() { return static_size; }
347  static inline void resize(const size_t n)
348  {
349  if (n != static_size)
350  throw std::logic_error(
351  format(
352  "Try to change the size of CPose2D to %u.",
353  static_cast<unsigned>(n)));
354  }
355 
356  /** @} */
357 
358 }; // End of class def.
359 
360 std::ostream& operator<<(std::ostream& o, const CPose2D& p);
361 
362 /** Unary - operator: return the inverse pose "-p" (Note that is NOT the same
363  * than a pose with negative x y phi) \sa CPose2D::inverse() */
364 CPose2D operator-(const CPose2D& p);
365 
366 /** Compose a 2D point from a new coordinate base given by a 2D pose. */
368  const CPose2D& pose, const mrpt::math::TPoint2D& pnt);
369 
370 bool operator==(const CPose2D& p1, const CPose2D& p2);
371 bool operator!=(const CPose2D& p1, const CPose2D& p2);
372 
373 /** Eigen aligment-compatible container */
375 /** Eigen aligment-compatible container */
377 
378 } // End of namespace
379 } // End of namespace
380 
381 #endif
void changeCoordinatesReference(const CPose2D &p)
makes: this = p (+) this
Definition: CPose2D.h:300
double phi_sin() const
Get a (cached) value of sin(phi), recomputing it only once when phi changes.
Definition: CPose2D.h:108
std::size_t size_type
Definition: CPose2D.h:336
double x
X,Y coordinates.
const type_value & getPoseMean() const
Definition: CPose2D.h:326
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:44
mrpt::math::TPoint2D operator+(const CPose2D &pose, const mrpt::math::TPoint2D &pnt)
Compose a 2D point from a new coordinate base given by a 2D pose.
Definition: CPose2D.cpp:377
GLenum GLsizei n
Definition: glext.h:5074
type_value & getPoseMean()
Definition: CPose2D.h:327
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:42
void operator*=(const double s)
Scalar multiplication.
Definition: CPose2D.cpp:275
void AddComponents(const CPose2D &p)
Scalar sum of components: This is diferent from poses composition, which is implemented as "+" operat...
Definition: CPose2D.cpp:264
mrpt::math::CArrayDouble< 2 > m_coords
[x,y]
Definition: CPose2D.h:47
double m_cosphi
Precomputed cos() & sin() of phi.
Definition: CPose2D.h:53
std::deque< TYPE1, Eigen::aligned_allocator< TYPE1 > > deque_t
GLdouble s
Definition: glext.h:3676
void composePoint(double lx, double ly, double &gx, double &gy) const
An alternative, slightly more efficient way of doing with G and L being 2D points and P this 2D pose...
Definition: CPose2D.cpp:188
CPose2D(TConstructorFlags_Poses)
Fast constructor that leaves all the data uninitialized - call with UNINITIALIZED_POSE as argument...
Definition: CPose2D.h:89
double value_type
The type of the elements.
Definition: CPose2D.h:333
CPose2D operator-(const CPose2D &b) const
Compute .
Definition: CPose2D.h:213
void fromStringRaw(const std::string &s)
Same as fromString, but without requiring the square brackets in the string.
Definition: CPose2D.cpp:405
A numeric matrix of compile-time fixed size.
bool operator!=(const CPoint< DERIVEDCLASS > &p1, const CPoint< DERIVEDCLASS > &p2)
Definition: CPoint.h:171
double m_phi
The orientation of the pose, in radians.
Definition: CPose2D.h:51
CPose2D operator-(const CPose2D &p)
Unary - operator: return the inverse pose "-p" (Note that is NOT the same than a pose with negative x...
Definition: CPose2D.cpp:328
static bool is_3D()
Definition: CPose2D.h:316
void setToNaN() override
Set all data fields to quiet NaN.
Definition: CPose2D.cpp:438
GLubyte g
Definition: glext.h:6279
A base class for representing a pose in 2D or 3D.
Definition: CPose.h:27
mrpt::aligned_containers< CPose2D >::deque_t StdDeque_CPose2D
Eigen aligment-compatible container.
Definition: CPose2D.h:376
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:19
GLubyte GLubyte b
Definition: glext.h:6279
void inverse()
Convert this pose into its inverse, saving the result in itself.
Definition: CPose2D.cpp:337
CPose2D type_value
Used to emulate CPosePDF types, for example, in mrpt::graphs::CNetworkOfPoses.
Definition: CPose2D.h:311
CPose2D operator+(const CPose2D &D) const
The operator is the pose compounding operator.
Definition: CPose2D.cpp:111
GLsizei const GLchar ** string
Definition: glext.h:4101
A class used to store a 2D point.
Definition: CPoint2D.h:36
A class used to store a 3D point.
Definition: CPoint3D.h:32
_W64 int ptrdiff_t
Definition: glew.h:137
static bool is_PDF()
Definition: CPose2D.h:325
std::string asString() const
Definition: CPose2D.h:251
const GLdouble * v
Definition: glext.h:3678
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...
double & operator[](unsigned int i)
Definition: CPose2D.h:283
mrpt::math::CMatrixDouble22 getRotationMatrix() const
Definition: CPose2D.h:145
bool operator==(const CPoint< DERIVEDCLASS > &p1, const CPoint< DERIVEDCLASS > &p2)
Definition: CPoint.h:163
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle...
Definition: CPose2D.h:40
const float R
double & phi()
Definition: CPose2D.h:93
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
CPose2D & operator+=(const CPose2D &b)
Make .
Definition: CPose2D.cpp:386
CPose2D getOppositeScalar() const
Return the opposite of the current pose instance by taking the negative of all its components individ...
Definition: CPose2D.cpp:428
static size_type max_size()
Definition: CPose2D.h:346
void getHomogeneousMatrix(mrpt::math::CMatrixDouble44 &out_HM) const
Returns the corresponding 4x4 homogeneous transformation matrix for the point(translation) or pose (t...
Definition: CPose2D.cpp:288
const double & phi() const
Get the phi angle of the 2D pose (in radians)
Definition: CPose2D.h:91
static void resize(const size_t n)
Definition: CPose2D.h:347
void composeFrom(const CPose2D &A, const CPose2D &B)
Makes .
Definition: CPose2D.cpp:124
double phi_cos() const
Get a (cached) value of cos(phi), recomputing it only once when phi changes.
Definition: CPose2D.h:101
void inverseComposePoint(const double gx, const double gy, double &lx, double &ly) const
Computes the 2D point L such as .
Definition: CPose2D.cpp:219
void fromString(const std::string &s)
Set the current object value from a string generated by &#39;asString&#39; (eg: "[0.02 1.04 -0...
Definition: CPose2D.cpp:392
Lightweight 2D pose.
GLenum GLint GLint y
Definition: glext.h:3538
void phi(double angle)
Set the phi angle of the 2D pose (in radians)
Definition: CPose2D.h:115
double distance2DFrobeniusTo(const CPose2D &p) const
Returns the 2D distance from this pose/point to a 2D pose using the Frobenius distance.
Definition: CPose2D.cpp:410
GLenum GLint x
Definition: glext.h:3538
double & reference
Definition: CPose2D.h:334
Lightweight 3D point.
const double & const_reference
Definition: CPose2D.h:335
std::ptrdiff_t difference_type
Definition: CPose2D.h:337
void inverseComposeFrom(const CPose2D &A, const CPose2D &B)
Makes this method is slightly more efficient than "this= A - B;" since it avoids the temporary objec...
Definition: CPose2D.cpp:247
static size_type size()
Definition: CPose2D.h:344
bool m_cossin_uptodate
Definition: CPose2D.h:54
void getAsVector(mrpt::math::CVectorDouble &v) const
Returns a 1x3 vector with [x y phi].
Definition: CPose2D.cpp:352
Lightweight 2D point.
GLfloat GLfloat p
Definition: glext.h:6305
const double & operator[](unsigned int i) const
Definition: CPose2D.h:268
void inverseComposePoint(const mrpt::math::TPoint2D &g, mrpt::math::TPoint2D &l) const
Definition: CPose2D.h:191
CPose2D()
Default constructor (all coordinates to 0)
Definition: CPose2D.cpp:30
std::vector< TYPE1, Eigen::aligned_allocator< TYPE1 > > vector_t
mrpt::aligned_containers< CPose2D >::vector_t StdVector_CPose2D
Eigen aligment-compatible container.
Definition: CPose2D.h:374
void normalizePhi()
Forces "phi" to be in the range [-pi,pi];.
Definition: CPose2D.cpp:305
void phi_incr(const double Aphi)
Increment the PHI angle (without checking the 2 PI range, call normalizePhi is needed) ...
Definition: CPose2D.h:123
std::ostream & operator<<(std::ostream &o, const CPoint< DERIVEDCLASS > &p)
Dumps a point as a string [x,y] or [x,y,z].
Definition: CPoint.h:137
static bool empty()
Definition: CPose2D.h:345
void update_cached_cos_sin() const
Definition: CPose2D.h:56



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019