00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://www.mrpt.org/ | 00005 | | 00006 | Copyright (C) 2005-2011 University of Malaga | 00007 | | 00008 | This software was written by the Machine Perception and Intelligent | 00009 | Robotics Lab, University of Malaga (Spain). | 00010 | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> | 00011 | | 00012 | This file is part of the MRPT project. | 00013 | | 00014 | MRPT is free software: you can redistribute it and/or modify | 00015 | it under the terms of the GNU General Public License as published by | 00016 | the Free Software Foundation, either version 3 of the License, or | 00017 | (at your option) any later version. | 00018 | | 00019 | MRPT is distributed in the hope that it will be useful, | 00020 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00021 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 00022 | GNU General Public License for more details. | 00023 | | 00024 | You should have received a copy of the GNU General Public License | 00025 | along with MRPT. If not, see <http://www.gnu.org/licenses/>. | 00026 | | 00027 +---------------------------------------------------------------------------+ */ 00028 #ifndef CPOSEORPOINT_H 00029 #define CPOSEORPOINT_H 00030 00031 #include <mrpt/math/CMatrixFixedNumeric.h> 00032 #include <mrpt/math/lightweight_geom_data.h> 00033 #include <mrpt/math/ops_matrices.h> // Added here so many classes have access to these templates 00034 #include <mrpt/math/utils.h> // For homogeneousMatrixInverse 00035 00036 #include <mrpt/poses/CPoseOrPoint_detail.h> 00037 00038 namespace mrpt 00039 { 00040 /** \defgroup poses_grp 2D/3D points and poses 00041 * \ingroup mrpt_base_grp */ 00042 00043 /** \defgroup poses_pdf_grp 2D/3D point and pose PDFs 00044 * \ingroup mrpt_base_grp */ 00045 00046 /** Classes for 2D/3D geometry representation, both of single values and probability density distributions (PDFs) in many forms. 00047 * \ingroup poses_grp poses_pdf_grp 00048 */ 00049 namespace poses 00050 { 00051 using namespace mrpt::utils; // For square 00052 using namespace mrpt::math; // For ligh. geom data 00053 00054 // For use in some constructors (eg. CPose3D) 00055 enum TConstructorFlags_Poses 00056 { 00057 UNINITIALIZED_POSE = 0 00058 }; 00059 00060 /** The base template class for 2D & 3D points and poses. 00061 * This class use the Curiously Recurring Template Pattern (CRTP) to define 00062 * a set of common methods to all the children classes without the cost 00063 * of virtual methods. Since most important methods are inline, they will be expanded 00064 * at compile time and optimized for every specific derived case. 00065 * 00066 * For more information and examples, refer 00067 * to the <a href="http://www.mrpt.org/2D_3D_Geometry">2D/3D Geometry tutorial</a> online. 00068 * 00069 * 00070 * <center><h2>Introduction to 2D and 3D representation classes</h2></center> 00071 * <hr> 00072 * <p> 00073 * There are two class of spatial representation classes: 00074 * - Point: A point in the common mathematical sense, with no directional information. 00075 * - 2D: A 2D point is represented just by its coordinates (x,y). 00076 * - 3D: A 3D point is represented by its coordinates (x,y,z). 00077 * - Pose: It is a point, plus a direction. 00078 * - 2D: A 2D pose is a 2D point plus a single angle, the yaw or φ angle: the angle from the positive X angle. 00079 * - 3D: A 3D point is a 3D point plus three orientation angles (More details above). 00080 * </p> 00081 * In the case for a 3D orientation many representation angles can be used (Euler angles,yaw/pitch/roll,...) 00082 * but all of them can be handled by a 4x4 matrix called "Homogeneous Matrix". This matrix includes both, the 00083 * translation and the orientation for a point or a pose, and it can be obtained using 00084 * the method getHomogeneousMatrix() which is defined for any pose or point. Note that when the YPR angles are 00085 * used to define a 3D orientation, these three values can not be extracted from the matrix again.<br><br> 00086 * 00087 * <b>Homogeneous matrices:</b> These are 4x4 matrices which can represent any translation or rotation in 2D & 3D. 00088 * See the tutorial online for more details. * 00089 * 00090 * <b>Operators:</b> There are operators defined for the pose compounding \f$ \oplus \f$ and inverse pose 00091 * compounding \f$ \ominus \f$ of poses and points. For example, let "a" and "b" be 2D or 3D poses. Then "a+b" 00092 * returns the resulting pose of "moving b" from "a"; and "b-a" returns the pose of "b" as it is seen 00093 * "from a". They can be mixed points and poses, being 2D or 3D, in these operators, with the following 00094 * results: <br> 00095 * 00096 * <div align="center" > 00097 * <pre> 00098 * Does "a+b" return a Pose or a Point? 00099 * +---------------------------------+ 00100 * | a \ b | Pose | Point | 00101 * +----------+-----------+----------+ 00102 * | Pose | Pose | Point | 00103 * | Point | Pose | Point | 00104 * +---------------------------------+ 00105 * 00106 * Does "a-b" return a Pose or a Point? 00107 * +---------------------------------+ 00108 * | a \ b | Pose | Point | 00109 * +----------+-----------+----------+ 00110 * | Pose | Pose | Pose | 00111 * | Point | Point | Point | 00112 * +---------------------------------+ 00113 * 00114 * Does "a+b" and "a-b" return a 2D or 3D object? 00115 * +-------------------------+ 00116 * | a \ b | 2D | 3D | 00117 * +----------+--------------+ 00118 * | 2D | 2D | 3D | 00119 * | 3D | 3D | 3D | 00120 * +-------------------------+ 00121 * 00122 * </pre> 00123 * </div> 00124 * 00125 * \sa CPose,CPoint 00126 * \ingroup poses_grp 00127 */ 00128 template <class DERIVEDCLASS> 00129 class CPoseOrPoint : public mrpt::poses::detail::pose_point_impl<DERIVEDCLASS, mrpt::poses::detail::T3DTypeHelper<DERIVEDCLASS>::is_3D_val> 00130 { 00131 public: 00132 /** Common members of all points & poses classes. 00133 @{ */ 00134 // Note: the access to "z" is implemented (only for 3D data types), in detail::pose_point_impl<> 00135 inline double x() const /*!< Get X coord. */ { return static_cast<const DERIVEDCLASS*>(this)->m_coords[0]; } 00136 inline double y() const /*!< Get Y coord. */ { return static_cast<const DERIVEDCLASS*>(this)->m_coords[1]; } 00137 00138 inline double &x() /*!< Get ref to X coord. */ { return static_cast<DERIVEDCLASS*>(this)->m_coords[0]; } 00139 inline double &y() /*!< Get ref to Y coord. */ { return static_cast<DERIVEDCLASS*>(this)->m_coords[1]; } 00140 00141 inline void x(const double v) /*!< Set X coord. */ { static_cast<DERIVEDCLASS*>(this)->m_coords[0]=v; } 00142 inline void y(const double v) /*!< Set Y coord. */ { static_cast<DERIVEDCLASS*>(this)->m_coords[1]=v; } 00143 00144 inline void x_incr(const double v) /*!< X+=v */ { static_cast<DERIVEDCLASS*>(this)->m_coords[0]+=v; } 00145 inline void y_incr(const double v) /*!< Y+=v */ { static_cast<DERIVEDCLASS*>(this)->m_coords[1]+=v; } 00146 00147 00148 /** Return true for poses or points with a Z component, false otherwise. */ 00149 static inline bool is3DPoseOrPoint() { return DERIVEDCLASS::is_3D_val!=0; } 00150 00151 /** Returns the squared euclidean distance to another pose/point: */ 00152 template <class OTHERCLASS> inline double sqrDistanceTo(const CPoseOrPoint<OTHERCLASS> &b) const 00153 { 00154 if (b.is3DPoseOrPoint()) 00155 { 00156 if (is3DPoseOrPoint()) 00157 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]); 00158 else return square(x()-b.x()) + square(y()-b.y()) + square(static_cast<const OTHERCLASS*>(&b)->m_coords[2]); 00159 } 00160 else 00161 { 00162 if (is3DPoseOrPoint()) 00163 return square(x()-b.x()) + square(y()-b.y()) + square(static_cast<const OTHERCLASS*>(&b)->m_coords[2]); 00164 else return square(x()-b.x()) + square(y()-b.y()); 00165 } 00166 } 00167 00168 /** Returns the Euclidean distance to another pose/point: */ 00169 template <class OTHERCLASS> 00170 inline double distanceTo(const CPoseOrPoint<OTHERCLASS> &b) const 00171 { 00172 return std::sqrt( sqrDistanceTo(b)); 00173 } 00174 00175 /** Returns the squared 2D distance from this pose/point to a 2D point (ignores Z, if it exists). */ 00176 inline double distance2DToSquare( double ax, double ay ) const { return square(ax-x())+square(ay-y()); } 00177 00178 /** Returns the squared 3D distance from this pose/point to a 3D point */ 00179 inline double distance3DToSquare( double ax, double ay, double az ) const { 00180 return square(ax-x())+square(ay-y())+square(az-(is3DPoseOrPoint() ? static_cast<const DERIVEDCLASS*>(this)->m_coords[2] : 0) ); 00181 } 00182 00183 /** Returns the 2D distance from this pose/point to a 2D point (ignores Z, if it exists). */ 00184 inline double distance2DTo( double ax, double ay ) const { return std::sqrt(distance2DToSquare(ax,ay)); } 00185 00186 /** Returns the 3D distance from this pose/point to a 3D point */ 00187 inline double distance3DTo( double ax, double ay, double az ) const { return std::sqrt(distance3DToSquare(ax,ay,az)); } 00188 00189 /** Returns the euclidean distance to a 3D point: */ 00190 inline double distanceTo(const mrpt::math::TPoint3D &b) const { return distance3DTo(b.x,b.y,b.z); } 00191 00192 /** Returns the euclidean norm of vector: \f$ ||\mathbf{x}|| = \sqrt{x^2+y^2+z^2} \f$ */ 00193 inline double norm() const 00194 { 00195 return std::sqrt( square(x())+square(y())+ (!is3DPoseOrPoint() ? 0 : square(static_cast<const DERIVEDCLASS*>(this)->m_coords[2]) ) ); 00196 } 00197 00198 /** Return the pose or point as a 1xN vector with all the components (see derived classes for each implementation) */ 00199 inline vector_double getAsVectorVal() const 00200 { 00201 vector_double v; 00202 static_cast<const DERIVEDCLASS*>(this)->getAsVector(v); 00203 return v; 00204 } 00205 00206 /** Returns the corresponding 4x4 homogeneous transformation matrix for the point(translation) or pose (translation+orientation). 00207 * \sa getInverseHomogeneousMatrix 00208 */ 00209 inline CMatrixDouble44 getHomogeneousMatrixVal() const 00210 { 00211 CMatrixDouble44 m(UNINITIALIZED_MATRIX); 00212 static_cast<const DERIVEDCLASS*>(this)->getHomogeneousMatrix(m); 00213 return m; 00214 } 00215 00216 /** Returns the corresponding 4x4 inverse homogeneous transformation matrix for this point or pose. 00217 * \sa getHomogeneousMatrix 00218 */ 00219 inline void getInverseHomogeneousMatrix( math::CMatrixDouble44 &out_HM ) const 00220 { // Get current HM & inverse in-place: 00221 static_cast<const DERIVEDCLASS*>(this)->getHomogeneousMatrix(out_HM); 00222 mrpt::math::homogeneousMatrixInverse(out_HM); 00223 } 00224 00225 //! \overload 00226 inline mrpt::math::CMatrixDouble44 getInverseHomogeneousMatrix() const 00227 { 00228 mrpt::math::CMatrixDouble44 M(UNINITIALIZED_MATRIX); 00229 getInverseHomogeneousMatrix(M); 00230 return M; 00231 } 00232 00233 /** @} */ 00234 }; // End of class def. 00235 00236 00237 } // End of namespace 00238 } // End of namespace 00239 00240 #endif
| Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011 |