Main MRPT website > C++ reference
MRPT logo
CPose2D.h
Go to the documentation of this file.
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 CPOSE2D_H
00029 #define CPOSE2D_H
00030 
00031 #include <mrpt/poses/CPose.h>
00032 
00033 namespace mrpt
00034 {
00035 namespace poses
00036 {
00037         DEFINE_SERIALIZABLE_PRE( CPose2D )
00038 
00039         /** A class used to store a 2D pose.
00040          *    A class used to store a 2D pose, including the 2D coordinate
00041          *      point and a heading (phi) angle.
00042          *
00043          *  For a complete description of Points/Poses, see mrpt::poses::CPoseOrPoint, or refer
00044          *    to the <a href="http://www.mrpt.org/2D_3D_Geometry" >2D/3D Geometry tutorial</a> in the wiki.
00045          *
00046          *  <div align=center>
00047          *   <img src="CPose2D.gif">
00048          *  </div>
00049          *
00050          * \sa CPoseOrPoint,CPoint2D
00051          * \ingroup poses_grp
00052          */
00053         class BASE_IMPEXP CPose2D : public CPose<CPose2D>, public mrpt::utils::CSerializable
00054         {
00055         public:
00056                 // This must be added to any CSerializable derived class:
00057                 DEFINE_SERIALIZABLE( CPose2D )
00058 
00059         public:
00060                 mrpt::math::CArrayDouble<2>   m_coords; //!< [x,y]
00061 
00062         protected:
00063                 double m_phi;  //!< The orientation of the pose, in radians.
00064 
00065         public:
00066                  /** Default constructor (all coordinates to 0) */
00067                  CPose2D();
00068 
00069                  /** Constructor from an initial value of the pose.*/
00070                  CPose2D(const double x,const double y,const double phi);
00071 
00072                  /** Constructor from a CPoint2D object. */
00073                  CPose2D(const CPoint2D &);
00074 
00075                  /** Aproximation!! Avoid its use, since information is lost. */
00076                  explicit CPose2D(const CPose3D &);
00077 
00078                  /** Constructor from lightweight object. */
00079                  CPose2D(const mrpt::math::TPose2D &);
00080 
00081                  /** Constructor from CPoint3D with information loss. */
00082                  explicit CPose2D(const CPoint3D &);
00083 
00084                  /** Fast constructor that leaves all the data uninitialized - call with UNINITIALIZED_POSE as argument */
00085                  inline CPose2D(TConstructorFlags_Poses constructor_dummy_param) { }
00086 
00087                  /** Get the phi angle of the 2D pose (in radians) */
00088                  inline const double &phi() const { return m_phi; }
00089                  //! \overload
00090                  inline       double &phi()       { return m_phi; }
00091 
00092                  /** Set the phi angle of the 2D pose (in radians) */
00093                  inline void phi(double angle) { m_phi=angle; }
00094 
00095                  inline void phi_incr(const double Aphi) { m_phi+=Aphi; }  //!< Increment the PHI angle (without checking the 2 PI range, call normalizePhi is needed)
00096 
00097                 /** Returns a 1x3 vector with [x y phi] */
00098                 void getAsVector(vector_double &v) const;
00099 
00100                  /** Returns the corresponding 4x4 homogeneous transformation matrix for the point(translation) or pose (translation+orientation).
00101                    * \sa getInverseHomogeneousMatrix
00102                    */
00103                  void  getHomogeneousMatrix(CMatrixDouble44 & out_HM ) const;
00104 
00105                  /** The operator \f$ a = this \oplus D \f$ is the pose compounding operator.
00106                   */
00107                  CPose2D  operator + (const CPose2D& D) const ;
00108 
00109                  /** Makes \f$ this = A \oplus B \f$
00110                   *  \note A or B can be "this" without problems.
00111                   */
00112                  void composeFrom(const CPose2D &A, const CPose2D &B);
00113 
00114                  /** The operator \f$ a = this \oplus D \f$ is the pose compounding operator.
00115                   */
00116                  CPose3D  operator + (const CPose3D& D) const ;
00117 
00118                  /** The operator \f$ u' = this \oplus u \f$ is the pose/point compounding operator.
00119                    */
00120                  CPoint2D operator + (const CPoint2D& u) const ;
00121 
00122                  /** An alternative, slightly more efficient way of doing \f$ G = P \oplus L \f$ with G and L being 2D points and P this 2D pose.  */
00123                  void composePoint(double lx,double ly,double &gx, double &gy) const;
00124 
00125                  /** The operator \f$ u' = this \oplus u \f$ is the pose/point compounding operator.
00126                    */
00127                  CPoint3D operator + (const CPoint3D& u) const ;
00128 
00129                 /**  Makes \f$ this = A \ominus B \f$ this method is slightly more efficient than "this= A - B;" since it avoids the temporary object.
00130                   *  \note A or B can be "this" without problems.
00131                   * \sa composeFrom, composePoint
00132                   */
00133                 void inverseComposeFrom(const CPose2D& A, const CPose2D& B );
00134 
00135                 /** Compute \f$ RET = this \oplus b \f$  */
00136                 inline CPose2D  operator - (const CPose2D& b) const
00137                 {
00138                         CPose2D ret(UNINITIALIZED_POSE);
00139                         ret.inverseComposeFrom(*this,b);
00140                         return ret;
00141                 }
00142 
00143                  /** Scalar sum of components: This is diferent from poses
00144                   *    composition, which is implemented as "+" operators in "CPose" derived classes.
00145                   */
00146                  void AddComponents(CPose2D &p);
00147 
00148                  /** Scalar multiplication.
00149                   */
00150                  void operator *=(const double  s);
00151 
00152                  /** Make \f$ this = this \oplus b \f$  */
00153                  inline CPose2D&  operator += (const CPose2D& b)
00154                  {
00155                         composeFrom(*this,b);
00156                         return *this;
00157                  }
00158 
00159                  /** Forces "phi" to be in the range [-pi,pi];
00160                    */
00161                  void  normalizePhi();
00162 
00163                  /** Returns a human-readable textual representation of the object (eg: "[x y yaw]", yaw in degrees)
00164                    * \sa fromString
00165                    */
00166                 void asString(std::string &s) const { s = mrpt::format("[%f %f %f]",x(),y(),RAD2DEG(m_phi)); }
00167                 inline std::string asString() const { std::string s; asString(s); return s; }
00168 
00169                  /** Set the current object value from a string generated by 'asString' (eg: "[0.02 1.04 -0.8]" )
00170                    * \sa asString
00171                    * \exception std::exception On invalid format
00172                    */
00173                  void fromString(const std::string &s) {
00174                         CMatrixDouble  m;
00175                         if (!m.fromMatlabStringFormat(s)) THROW_EXCEPTION("Malformed expression in ::fromString");
00176                         ASSERTMSG_(mrpt::math::size(m,1)==1 && mrpt::math::size(m,2)==3, "Wrong size of vector in ::fromString");
00177                         x( m.get_unsafe(0,0) );
00178                         y( m.get_unsafe(0,1) );
00179                         phi( DEG2RAD(m.get_unsafe(0,2)) );
00180                  }
00181 
00182                  inline const double &operator[](unsigned int i)const
00183                  {
00184                         switch(i)
00185                         {
00186                                 case 0:return m_coords[0];
00187                                 case 1:return m_coords[1];
00188                                 case 2:return m_phi;
00189                                 default:
00190                                 throw std::runtime_error("CPose2D::operator[]: Index of bounds.");
00191                         }
00192                  }
00193                  inline double &operator[](unsigned int i)
00194                  {
00195                         switch(i)
00196                         {
00197                                 case 0:return m_coords[0];
00198                                 case 1:return m_coords[1];
00199                                 case 2:return m_phi;
00200                                 default:
00201                                 throw std::runtime_error("CPose2D::operator[]: Index of bounds.");
00202                         }
00203                  }
00204 
00205                 /** makes: this = p (+) this */
00206                 inline void  changeCoordinatesReference( const CPose2D & p ) { composeFrom(p,CPose2D(*this)); }
00207 
00208                 typedef CPose2D  type_value; //!< Used to emulate CPosePDF types, for example, in mrpt::graphs::CNetworkOfPoses
00209                 enum { is_3D_val = 0 };
00210                 static inline bool is_3D() { return is_3D_val!=0; }
00211                 enum { rotation_dimensions = 2 };
00212                 enum { is_PDF_val = 0 };
00213                 static inline bool is_PDF() { return is_PDF_val!=0; }
00214 
00215                 inline const type_value & getPoseMean() const { return *this; }
00216                 inline       type_value & getPoseMean()       { return *this; }
00217 
00218                 /** @name STL-like methods and typedefs
00219                    @{   */
00220                 typedef double         value_type;              //!< The type of the elements
00221                 typedef double&        reference;
00222                 typedef const double&  const_reference;
00223                 typedef std::size_t    size_type;
00224                 typedef std::ptrdiff_t difference_type;
00225 
00226                  // size is constant
00227                 enum { static_size = 3 };
00228                 static inline size_type size() { return static_size; }
00229                 static inline bool empty() { return false; }
00230                 static inline size_type max_size() { return static_size; }
00231                 static inline void resize(const size_t n) { if (n!=static_size) throw std::logic_error(format("Try to change the size of CPose2D to %u.",static_cast<unsigned>(n))); }
00232 
00233                 /** @} */
00234 
00235         }; // End of class def.
00236 
00237 
00238         std::ostream BASE_IMPEXP & operator << (std::ostream& o, const CPose2D& p);
00239 
00240         /** Unary - operator: return the inverse pose "-p" (Note that is NOT the same than a pose with negative x y phi) */
00241         CPose2D BASE_IMPEXP operator -(const CPose2D &p);
00242 
00243         mrpt::math::TPoint2D BASE_IMPEXP  operator +(const CPose2D &pose, const mrpt::math::TPoint2D &pnt);  //!< Compose a 2D point from a new coordinate base given by a 2D pose.
00244 
00245         bool BASE_IMPEXP operator==(const CPose2D &p1,const CPose2D &p2);
00246         bool BASE_IMPEXP operator!=(const CPose2D &p1,const CPose2D &p2);
00247 
00248         typedef mrpt::aligned_containers<CPose2D>::vector_t             StdVector_CPose2D; //!< Eigen aligment-compatible container
00249         typedef mrpt::aligned_containers<CPose2D>::deque_t              StdDeque_CPose2D; //!< Eigen aligment-compatible container
00250 
00251         } // End of namespace
00252 } // End of namespace
00253 
00254 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011