Main MRPT website > C++ reference
MRPT logo
CPoseOrPoint.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 #ifndef CPOSEORPOINT_H
10 #define CPOSEORPOINT_H
11 
16 
18 
19 namespace mrpt
20 {
21  /** \defgroup poses_grp 2D/3D points and poses
22  * \ingroup mrpt_base_grp */
23 
24  /** \defgroup poses_pdf_grp 2D/3D point and pose PDFs
25  * \ingroup mrpt_base_grp */
26 
27  /** Classes for 2D/3D geometry representation, both of single values and probability density distributions (PDFs) in many forms.
28  * \ingroup poses_grp poses_pdf_grp
29  */
30  namespace poses
31  {
33 
34  // For use in some constructors (eg. CPose3D)
36  {
38  };
39 
40  /** The base template class for 2D & 3D points and poses.
41  * This class use the Curiously Recurring Template Pattern (CRTP) to define
42  * a set of common methods to all the children classes without the cost
43  * of virtual methods. Since most important methods are inline, they will be expanded
44  * at compile time and optimized for every specific derived case.
45  *
46  * For more information and examples, refer
47  * to the <a href="http://www.mrpt.org/2D_3D_Geometry">2D/3D Geometry tutorial</a> online.
48  *
49  *
50  * <center><h2>Introduction to 2D and 3D representation classes</h2></center>
51  * <hr>
52  * <p>
53  * There are two class of spatial representation classes:
54  * - Point: A point in the common mathematical sense, with no directional information.
55  * - 2D: A 2D point is represented just by its coordinates (x,y).
56  * - 3D: A 3D point is represented by its coordinates (x,y,z).
57  * - Pose: It is a point, plus a direction.
58  * - 2D: A 2D pose is a 2D point plus a single angle, the yaw or &#966; angle: the angle from the positive X angle.
59  * - 3D: A 3D point is a 3D point plus three orientation angles (More details above).
60  * </p>
61  * In the case for a 3D orientation many representation angles can be used (Euler angles,yaw/pitch/roll,...)
62  * but all of them can be handled by a 4x4 matrix called "Homogeneous Matrix". This matrix includes both, the
63  * translation and the orientation for a point or a pose, and it can be obtained using
64  * the method getHomogeneousMatrix() which is defined for any pose or point. Note that when the YPR angles are
65  * used to define a 3D orientation, these three values can not be extracted from the matrix again.<br><br>
66  *
67  * <b>Homogeneous matrices:</b> These are 4x4 matrices which can represent any translation or rotation in 2D & 3D.
68  * See the tutorial online for more details. *
69  *
70  * <b>Operators:</b> There are operators defined for the pose compounding \f$ \oplus \f$ and inverse pose
71  * compounding \f$ \ominus \f$ of poses and points. For example, let "a" and "b" be 2D or 3D poses. Then "a+b"
72  * returns the resulting pose of "moving b" from "a"; and "b-a" returns the pose of "b" as it is seen
73  * "from a". They can be mixed points and poses, being 2D or 3D, in these operators, with the following
74  * results: <br>
75  *
76  * <div align="center" >
77  * <pre>
78  * Does "a+b" return a Pose or a Point?
79  * +---------------------------------+
80  * | a \ b | Pose | Point |
81  * +----------+-----------+----------+
82  * | Pose | Pose | Point |
83  * | Point | Pose | Point |
84  * +---------------------------------+
85  *
86  * Does "a-b" return a Pose or a Point?
87  * +---------------------------------+
88  * | a \ b | Pose | Point |
89  * +----------+-----------+----------+
90  * | Pose | Pose | Pose |
91  * | Point | Point | Point |
92  * +---------------------------------+
93  *
94  * Does "a+b" and "a-b" return a 2D or 3D object?
95  * +-------------------------+
96  * | a \ b | 2D | 3D |
97  * +----------+--------------+
98  * | 2D | 2D | 3D |
99  * | 3D | 3D | 3D |
100  * +-------------------------+
101  *
102  * </pre>
103  * </div>
104  *
105  * \sa CPose,CPoint
106  * \ingroup poses_grp
107  */
108  template <class DERIVEDCLASS>
109  class CPoseOrPoint : public mrpt::poses::detail::pose_point_impl<DERIVEDCLASS, mrpt::poses::detail::T3DTypeHelper<DERIVEDCLASS>::is_3D_val>
110  {
111  public:
112  /** Common members of all points & poses classes.
113  @{ */
114  // Note: the access to "z" is implemented (only for 3D data types), in detail::pose_point_impl<>
115  inline double x() const /*!< Get X coord. */ { return static_cast<const DERIVEDCLASS*>(this)->m_coords[0]; }
116  inline double y() const /*!< Get Y coord. */ { return static_cast<const DERIVEDCLASS*>(this)->m_coords[1]; }
117 
118  inline double &x() /*!< Get ref to X coord. */ { return static_cast<DERIVEDCLASS*>(this)->m_coords[0]; }
119  inline double &y() /*!< Get ref to Y coord. */ { return static_cast<DERIVEDCLASS*>(this)->m_coords[1]; }
120 
121  inline void x(const double v) /*!< Set X coord. */ { static_cast<DERIVEDCLASS*>(this)->m_coords[0]=v; }
122  inline void y(const double v) /*!< Set Y coord. */ { static_cast<DERIVEDCLASS*>(this)->m_coords[1]=v; }
123 
124  inline void x_incr(const double v) /*!< X+=v */ { static_cast<DERIVEDCLASS*>(this)->m_coords[0]+=v; }
125  inline void y_incr(const double v) /*!< Y+=v */ { static_cast<DERIVEDCLASS*>(this)->m_coords[1]+=v; }
126 
127 
128  /** Return true for poses or points with a Z component, false otherwise. */
129  static inline bool is3DPoseOrPoint() { return DERIVEDCLASS::is_3D_val!=0; }
130 
131  /** Returns the squared euclidean distance to another pose/point: */
132  template <class OTHERCLASS> inline double sqrDistanceTo(const CPoseOrPoint<OTHERCLASS> &b) const
133  {
134  using mrpt::utils::square;
135 
136  if (b.is3DPoseOrPoint())
137  {
138  if (is3DPoseOrPoint())
139  return square(x()-b.x()) + square(y()-b.y()) + square(static_cast<const DERIVEDCLASS*>(this)->m_coords[2]-static_cast<const OTHERCLASS*>(&b)->m_coords[2]);
140  else return square(x()-b.x()) + square(y()-b.y()) + square(static_cast<const OTHERCLASS*>(&b)->m_coords[2]);
141  }
142  else
143  {
144  if (is3DPoseOrPoint())
145  return square(x()-b.x()) + square(y()-b.y()) + square(static_cast<const OTHERCLASS*>(&b)->m_coords[2]);
146  else return square(x()-b.x()) + square(y()-b.y());
147  }
148  }
149 
150  /** Returns the Euclidean distance to another pose/point: */
151  template <class OTHERCLASS>
152  inline double distanceTo(const CPoseOrPoint<OTHERCLASS> &b) const
153  {
154  return std::sqrt( sqrDistanceTo(b));
155  }
156 
157  /** Returns the squared 2D distance from this pose/point to a 2D point (ignores Z, if it exists). */
158  inline double distance2DToSquare( double ax, double ay ) const { using mrpt::utils::square; return square(ax-x())+square(ay-y()); }
159 
160  /** Returns the squared 3D distance from this pose/point to a 3D point */
161  inline double distance3DToSquare( double ax, double ay, double az ) const {
162  using mrpt::utils::square;
163  return square(ax-x())+square(ay-y())+square(az-(is3DPoseOrPoint() ? static_cast<const DERIVEDCLASS*>(this)->m_coords[2] : 0) );
164  }
165 
166  /** Returns the 2D distance from this pose/point to a 2D point (ignores Z, if it exists). */
167  inline double distance2DTo( double ax, double ay ) const { return std::sqrt(distance2DToSquare(ax,ay)); }
168 
169  /** Returns the 3D distance from this pose/point to a 3D point */
170  inline double distance3DTo( double ax, double ay, double az ) const { return std::sqrt(distance3DToSquare(ax,ay,az)); }
171 
172  /** Returns the euclidean distance to a 3D point: */
173  inline double distanceTo(const mrpt::math::TPoint3D &b) const { return distance3DTo(b.x,b.y,b.z); }
174 
175  /** Returns the euclidean norm of vector: \f$ ||\mathbf{x}|| = \sqrt{x^2+y^2+z^2} \f$ */
176  inline double norm() const
177  {
178  using mrpt::utils::square;
179  return std::sqrt( square(x())+square(y())+ (!is3DPoseOrPoint() ? 0 : square(static_cast<const DERIVEDCLASS*>(this)->m_coords[2]) ) );
180  }
181 
182  /** Return the pose or point as a 1xN vector with all the components (see derived classes for each implementation) */
184  {
185  CVectorDouble v;
186  static_cast<const DERIVEDCLASS*>(this)->getAsVector(v);
187  return v;
188  }
189 
190  /** Returns the corresponding 4x4 homogeneous transformation matrix for the point(translation) or pose (translation+orientation).
191  * \sa getInverseHomogeneousMatrix
192  */
194  {
196  static_cast<const DERIVEDCLASS*>(this)->getHomogeneousMatrix(m);
197  return m;
198  }
199 
200  /** Returns the corresponding 4x4 inverse homogeneous transformation matrix for this point or pose.
201  * \sa getHomogeneousMatrix
202  */
204  { // Get current HM & inverse in-place:
205  static_cast<const DERIVEDCLASS*>(this)->getHomogeneousMatrix(out_HM);
207  }
208 
209  //! \overload
211  {
214  return M;
215  }
216 
217  /** Set all data fields to quiet NaN */
218  virtual void setToNaN() = 0;
219 
220  /** @} */
221  }; // End of class def.
222 
223 
224  } // End of namespace
225 } // End of namespace
226 
227 #endif
double distance3DToSquare(double ax, double ay, double az) const
Returns the squared 3D distance from this pose/point to a 3D point.
Definition: CPoseOrPoint.h:161
double distanceTo(const mrpt::math::TPoint3D &b) const
Returns the euclidean distance to a 3D point:
Definition: CPoseOrPoint.h:173
void y_incr(const double v)
Definition: CPoseOrPoint.h:125
T square(const T x)
Inline function for the square of a number.
static bool is3DPoseOrPoint()
Return true for poses or points with a Z component, false otherwise.
Definition: CPoseOrPoint.h:129
mrpt::math::CMatrixDouble44 getInverseHomogeneousMatrix() const
Definition: CPoseOrPoint.h:210
mrpt::math::CMatrixDouble44 getHomogeneousMatrixVal() const
Returns the corresponding 4x4 homogeneous transformation matrix for the point(translation) or pose (t...
Definition: CPoseOrPoint.h:193
double sqrDistanceTo(const CPoseOrPoint< OTHERCLASS > &b) const
Returns the squared euclidean distance to another pose/point:
Definition: CPoseOrPoint.h:132
double z
X,Y,Z coordinates.
void y(const double v)
Definition: CPoseOrPoint.h:122
double distance2DToSquare(double ax, double ay) const
Returns the squared 2D distance from this pose/point to a 2D point (ignores Z, if it exists)...
Definition: CPoseOrPoint.h:158
void getInverseHomogeneousMatrix(mrpt::math::CMatrixDouble44 &out_HM) const
Returns the corresponding 4x4 inverse homogeneous transformation matrix for this point or pose...
Definition: CPoseOrPoint.h:203
A numeric matrix of compile-time fixed size.
double distance3DTo(double ax, double ay, double az) const
Returns the 3D distance from this pose/point to a 3D point.
Definition: CPoseOrPoint.h:170
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:115
void x_incr(const double v)
Definition: CPoseOrPoint.h:124
double distanceTo(const CPoseOrPoint< OTHERCLASS > &b) const
Returns the Euclidean distance to another pose/point:
Definition: CPoseOrPoint.h:152
The base template class for 2D & 3D points and poses.
Definition: CPoseOrPoint.h:109
void x(const double v)
Definition: CPoseOrPoint.h:121
CVectorDouble getAsVectorVal() const
Return the pose or point as a 1xN vector with all the components (see derived classes for each implem...
Definition: CPoseOrPoint.h:183
T square(const T x)
Inline function for the square of a number.
Definition: bits.h:110
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void homogeneousMatrixInverse(const MATRIXLIKE1 &M, MATRIXLIKE2 &out_inverse_M)
Efficiently compute the inverse of a 4x4 homogeneous matrix by only transposing the rotation 3x3 part...
double norm() const
Returns the euclidean norm of vector: .
Definition: CPoseOrPoint.h:176
dynamic_vector< double > CVectorDouble
Column vector, like Eigen::MatrixXd, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:53
Lightweight 3D point.
double distance2DTo(double ax, double ay) const
Returns the 2D distance from this pose/point to a 2D point (ignores Z, if it exists).
Definition: CPoseOrPoint.h:167
virtual void setToNaN()=0
Set all data fields to quiet NaN.



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