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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020