Main MRPT website > C++ reference
MRPT logo
CQuaternion.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 
00029 #ifndef CQuaternion_H
00030 #define CQuaternion_H
00031 
00032 #include <mrpt/math/CMatrixTemplateNumeric.h>
00033 #include <mrpt/math/CArray.h>
00034 
00035 namespace mrpt
00036 {
00037         namespace math
00038         {
00039                 // For use with a constructor of CQuaternion
00040                 enum TConstructorFlags_Quaternions
00041                 {
00042                         UNINITIALIZED_QUATERNION = 0
00043                 };
00044 
00045                 /** A quaternion, which can represent a 3D rotation as pair \f$ (r,\mathbf{u}) \f$, with a real part "r" and a 3D vector \f$ \mathbf{u} = (x,y,z) \f$, or alternatively, q = r + ix + jy + kz.
00046                  *
00047                  *  The elements of the quaternion can be accessed by either:
00048                  *              - r(), x(), y(), z(), or
00049                  *              - the operator [] with indices running from 0 (=r) to 3 (=z).
00050                  *
00051                  *  Users will usually employ the typedef "CQuaternionDouble" instead of this template.
00052                  *
00053                  * For more information about quaternions, see:
00054                  *  - http://people.csail.mit.edu/bkph/articles/Quaternions.pdf
00055                  *  - http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
00056                  *
00057                  * \ingroup mrpt_base_grp
00058                  * \sa mrpt::poses::CPose3D
00059                  */
00060                 template <class T>
00061                 class CQuaternion : public CArrayNumeric<T,4>
00062                 {
00063                         typedef CArrayNumeric<T,4> Base;
00064                 public:
00065         /* @{ Constructors
00066          */
00067 
00068                 /**     Can be used with UNINITIALIZED_QUATERNION as argument, does not initialize the 4 elements of the quaternion (use this constructor when speed is critical). */
00069                 inline CQuaternion(TConstructorFlags_Quaternions constructor_dummy_param) { }
00070 
00071                 /**     Default constructor: construct a (1, (0,0,0) ) quaternion representing no rotation. */
00072                 inline CQuaternion()
00073                 {
00074                         (*this)[0] = 1;
00075                         (*this)[1] = 0;
00076                         (*this)[2] = 0;
00077                         (*this)[3] = 0;
00078                 }
00079 
00080                 /**     Construct a quaternion from its parameters 'r', 'x', 'y', 'z', with q = r + ix + jy + kz. */
00081                 inline CQuaternion(const T r,const T x,const T y,const T z)
00082                 {
00083                         (*this)[0] = r;
00084                         (*this)[1] = x;
00085                         (*this)[2] = y;
00086                         (*this)[3] = z;
00087                         ASSERTDEBMSG_(std::abs(normSqr()-1.0)<1e-5, mrpt::format("Initialization data for quaternion is not normalized: %f %f %f %f -> sqrNorm=%f",r,x,y,z,normSqr()) );
00088                 }
00089 
00090                 /* @}
00091                  */
00092 
00093 
00094                 inline T  r()const {return (*this)[0];} //!< Return r coordinate of the quaternion
00095                 inline T  x()const {return (*this)[1];} //!< Return x coordinate of the quaternion
00096                 inline T  y()const {return (*this)[2];} //!< Return y coordinate of the quaternion
00097                 inline T  z()const {return (*this)[3];} //!< Return z coordinate of the quaternion
00098                 inline void  r(const T r) {(*this)[0]=r;}       //!< Set r coordinate of the quaternion
00099                 inline void  x(const T x) {(*this)[1]=x;}       //!< Set x coordinate of the quaternion
00100                 inline void  y(const T y) {(*this)[2]=y;}       //!< Set y coordinate of the quaternion
00101                 inline void  z(const T z) {(*this)[3]=z;}       //!< Set z coordinate of the quaternion
00102 
00103                 /**     Set this quaternion to the rotation described by a 3D Rodrigues rotation vector.
00104                   */
00105                 template <class ARRAYLIKE>
00106                 void  fromRodriguesVector(const ARRAYLIKE &in)
00107                 {
00108                         if (in.size()!=3) THROW_EXCEPTION("Wrong Dimension in input vector for quaternion Constructor");
00109 
00110                         const T x = in[0];
00111                         const T y = in[1];
00112                         const T z = in[2];
00113                         if ((x==0)&&(y==0)&&(z==0))
00114                         {
00115                                 (*this)[0] = 1;
00116                                 (*this)[1] = 0;
00117                                 (*this)[2] = 0;
00118                                 (*this)[3] = 0;
00119                         }
00120                         else
00121                         {
00122                                 const T angle = sqrt(x*x+y*y+z*z);
00123                                 const T s = (sin(angle/2))/angle;
00124                                 const T c = cos(angle/2);
00125                                 (*this)[0] = c;
00126                                 (*this)[1] = x * s;
00127                                 (*this)[2] = y * s;
00128                                 (*this)[3] = z * s;
00129                         }
00130                 }
00131 
00132                 /**     Calculate the "cross" product (or "composed rotation") of two quaternion: this = q1 x q2
00133                   *   After the operation, "this" will represent the composed rotations of q1 and q2 (q2 applied "after" q1).
00134                   */
00135                 inline void  crossProduct(const CQuaternion &q1, const CQuaternion &q2)
00136                 {
00137                         this->r(  q1.r()*q2.r() - q1.x()*q2.x() - q1.y()*q2.y() - q1.z()*q2.z() );
00138                         this->x(  q1.r()*q2.x() + q2.r()*q1.x() + q1.y()*q2.z() - q2.y()*q1.z() );
00139             this->y(  q1.r()*q2.y() + q2.r()*q1.y() + q1.z()*q2.x() - q2.z()*q1.x() );
00140                         this->z(  q1.r()*q2.z() + q2.r()*q1.z() + q1.x()*q2.y() - q2.x()*q1.y() );
00141                         this->normalize();
00142                 }
00143 
00144                 /** Rotate a 3D point (lx,ly,lz) -> (gx,gy,gz) as described by this quaternion
00145                   */
00146                 void rotatePoint(const double lx,const double ly,const double lz, double &gx,double &gy,double &gz ) const
00147                 {
00148                         const double t2 = r()*x(); const double t3 = r()*y(); const double t4 = r()*z(); const double t5 =-x()*x(); const double t6 = x()*y();
00149                         const double t7 = x()*z(); const double t8 =-y()*y(); const double t9 = y()*z(); const double t10=-z()*z();
00150                         gx = 2*((t8+ t10)*lx+(t6 - t4)*ly+(t3+t7)*lz)+lx;
00151                         gy = 2*((t4+  t6)*lx+(t5 +t10)*ly+(t9-t2)*lz)+ly;
00152                         gz = 2*((t7-  t3)*lx+(t2 + t9)*ly+(t5+t8)*lz)+lz;
00153                 }
00154 
00155                 /** Rotate a 3D point (lx,ly,lz) -> (gx,gy,gz) as described by the inverse (conjugate) of this quaternion
00156                   */
00157                 void inverseRotatePoint(const double lx,const double ly,const double lz, double &gx,double &gy,double &gz ) const
00158                 {
00159                         const double t2 =-r()*x(); const double t3 =-r()*y(); const double t4 =-r()*z(); const double t5 =-x()*x(); const double t6 = x()*y();
00160                         const double t7 = x()*z(); const double t8 =-y()*y(); const double t9 = y()*z(); const double t10=-z()*z();
00161                         gx = 2*((t8+ t10)*lx+(t6 - t4)*ly+(t3+t7)*lz)+lx;
00162                         gy = 2*((t4+  t6)*lx+(t5 +t10)*ly+(t9-t2)*lz)+ly;
00163                         gz = 2*((t7-  t3)*lx+(t2 + t9)*ly+(t5+t8)*lz)+lz;
00164                 }
00165 
00166                 /** Return the squared norm of the quaternion */
00167                 inline double normSqr() const { return mrpt::utils::square(r()) + mrpt::utils::square(x()) + mrpt::utils::square(y()) + mrpt::utils::square(z()); }
00168 
00169                 /**     Normalize this quaternion, so its norm becomes the unitity.
00170                   */
00171                 inline void normalize()
00172                 {
00173                         const T qq = 1.0/std::sqrt( normSqr() );
00174                         for (unsigned int i=0;i<4;i++)
00175                                 (*this)[i] *= qq;
00176                 }
00177 
00178                 /** Calculate the 4x4 Jacobian of the normalization operation of this quaternion.
00179                   *  The output matrix can be a dynamic or fixed size (4x4) matrix.
00180                   */
00181                 template <class MATRIXLIKE>
00182                 void  normalizationJacobian(MATRIXLIKE &J) const
00183                 {
00184                         const T n = 1.0/std::pow(normSqr(),T(1.5));
00185                         J.setSize(4,4);
00186                         J.get_unsafe(0,0)=x()*x()+y()*y()+z()*z();
00187                         J.get_unsafe(0,1)=-r()*x();
00188                         J.get_unsafe(0,2)=-r()*y();
00189                         J.get_unsafe(0,3)=-r()*z();
00190 
00191                         J.get_unsafe(1,0)=-x()*r();
00192                         J.get_unsafe(1,1)=r()*r()+y()*y()+z()*z();
00193                         J.get_unsafe(1,2)=-x()*y();
00194                         J.get_unsafe(1,3)=-x()*z();
00195 
00196                         J.get_unsafe(2,0)=-y()*r();
00197                         J.get_unsafe(2,1)=-y()*x();
00198                         J.get_unsafe(2,2)=r()*r()+x()*x()+z()*z();
00199                         J.get_unsafe(2,3)=-y()*z();
00200 
00201                         J.get_unsafe(3,0)=-z()*r();
00202                         J.get_unsafe(3,1)=-z()*x();
00203                         J.get_unsafe(3,2)=-z()*y();
00204                         J.get_unsafe(3,3)=r()*r()+x()*x()+y()*y();
00205                         J *=n;
00206                 }
00207 
00208                 /** Compute the Jacobian of the rotation composition operation \f$ p = f(\cdot) = q_{this} \times r \f$, that is the 4x4 matrix \f$ \frac{\partial f}{\partial q_{this} }  \f$.
00209                   *  The output matrix can be a dynamic or fixed size (4x4) matrix.
00210                   */
00211                 template <class MATRIXLIKE>
00212                 inline void rotationJacobian(MATRIXLIKE &J) const
00213                 {
00214                         J.setSize(4,4);
00215                         J.get_unsafe(0,0)=r(); J.get_unsafe(0,1)=-x(); J.get_unsafe(0,2)=-y(); J.get_unsafe(0,3)=-z();
00216                         J.get_unsafe(1,0)=x(); J.get_unsafe(1,1)= r(); J.get_unsafe(1,2)=-z(); J.get_unsafe(1,3)= y();
00217                         J.get_unsafe(2,0)=y(); J.get_unsafe(2,1)= z(); J.get_unsafe(2,2)= r(); J.get_unsafe(2,3)=-x();
00218                         J.get_unsafe(3,0)=z(); J.get_unsafe(3,1)=-y(); J.get_unsafe(3,2)= x(); J.get_unsafe(3,3)= r();
00219                 }
00220 
00221                 /** Calculate the 3x3 rotation matrix associated to this quaternion */
00222                 template <class MATRIXLIKE>
00223         inline void  rotationMatrix(MATRIXLIKE &M) const
00224                 {
00225                         M.setSize(3,3);
00226                         rotationMatrixNoResize(M);
00227                 }
00228 
00229                 /** Fill out the top-left 3x3 block of the given matrix with the rotation matrix associated to this quaternion (does not resize the matrix, for that, see rotationMatrix) */
00230                 template <class MATRIXLIKE>
00231         inline void  rotationMatrixNoResize(MATRIXLIKE &M) const
00232                 {
00233                         M.get_unsafe(0,0)=r()*r()+x()*x()-y()*y()-z()*z();              M.get_unsafe(0,1)=2*(x()*y() -r()*z());                 M.get_unsafe(0,2)=2*(z()*x()+r()*y());
00234                         M.get_unsafe(1,0)=2*(x()*y()+r()*z());                          M.get_unsafe(1,1)=r()*r()-x()*x()+y()*y()-z()*z();              M.get_unsafe(1,2)=2*(y()*z()-r()*x());
00235                         M.get_unsafe(2,0)=2*(z()*x()-r()*y());                          M.get_unsafe(2,1)=2*(y()*z()+r()*x());                          M.get_unsafe(2,2)=r()*r()-x()*x()-y()*y()+z()*z();
00236                 }
00237 
00238 
00239                 /**     Return the conjugate quaternion  */
00240                 inline void conj(CQuaternion &q_out) const
00241                 {
00242                         q_out.r( r() );
00243                         q_out.x(-x() );
00244                         q_out.y(-y() );
00245                         q_out.z(-z() );
00246                 }
00247 
00248                 /**     Return the conjugate quaternion  */
00249                 inline CQuaternion conj() const
00250                 {
00251                         CQuaternion q_aux;
00252                         conj(q_aux);
00253                         return q_aux;
00254                 }
00255 
00256                 /**     Return the yaw, pitch & roll angles associated to quaternion
00257                   *  \sa For the equations, see The MRPT Book, or see http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/Quaternions.pdf
00258                   *  \sa rpy_and_jacobian
00259                 */
00260                 inline void rpy(T &roll, T &pitch, T &yaw) const
00261                 {
00262                         rpy_and_jacobian(roll,pitch,yaw,static_cast<mrpt::math::CMatrixDouble*>(NULL));
00263                 }
00264 
00265                 /**     Return the yaw, pitch & roll angles associated to quaternion, and (optionally) the 3x4 Jacobian of the transformation.
00266                   *  Note that both the angles and the Jacobian have one set of normal equations, plus other special formulas for the degenerated cases of |pitch|=90 degrees.
00267                   *  \sa For the equations, see The MRPT Book, or http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/Quaternions.pdf
00268                   * \sa rpy
00269                 */
00270                 template <class MATRIXLIKE>
00271                 void rpy_and_jacobian(T &roll, T &pitch, T &yaw, MATRIXLIKE *out_dr_dq = NULL, bool resize_out_dr_dq_to3x4 = true ) const
00272                 {
00273                         using mrpt::utils::square;
00274                         using std::sqrt;
00275 
00276                         if (out_dr_dq && resize_out_dr_dq_to3x4)
00277                                 out_dr_dq->setSize(3,4);
00278                         const T discr = r()*y()-x()*z();
00279                         if (fabs(discr)>0.49999)
00280                         {       // pitch = 90 deg
00281                                 pitch =  0.5*M_PI;
00282                                 yaw   =-2*atan2(x(),r());
00283                                 roll  = 0;
00284                                 if (out_dr_dq) {
00285                                         out_dr_dq->zeros();
00286                                         out_dr_dq->get_unsafe(0,0) = +2/x();
00287                                         out_dr_dq->get_unsafe(0,2) = -2*r()/(x()*x());
00288                                 }
00289                         }
00290                         else if (discr<-0.49999)
00291                         {       // pitch =-90 deg
00292                                 pitch = -0.5*M_PI;
00293                                 yaw   =+2*atan2(x(),r());
00294                                 roll  = 0;
00295                                 if (out_dr_dq) {
00296                                         out_dr_dq->zeros();
00297                                         out_dr_dq->get_unsafe(0,0) = -2/x();
00298                                         out_dr_dq->get_unsafe(0,2) = +2*r()/(x()*x());
00299                                 }
00300                         }
00301                         else
00302                         {       // Non-degenerate case:
00303                                 yaw   = atan2( 2*(r()*z()+x()*y()), 1-2*(y()*y()+z()*z()) );
00304                                 pitch = asin ( 2*discr );
00305                                 roll  = atan2( 2*(r()*x()+y()*z()), 1-2*(x()*x()+y()*y()) );
00306                                 if (out_dr_dq) {
00307                                         // Auxiliary terms:
00308                                         const double val1=(2*x()*x() + 2*y()*y() - 1);
00309                                         const double val12=square(val1);
00310                                         const double val2=(2*r()*x() + 2*y()*z());
00311                                         const double val22=square(val2);
00312                                         const double xy2 = 2*x()*y();
00313                                         const double rz2 = 2*r()*z();
00314                                         const double ry2 = 2*r()*y();
00315                                         const double val3 = (2*y()*y() + 2*z()*z() - 1);
00316                                         const double val4 = ((square(rz2 + xy2)/square(val3) + 1)*val3);
00317                                         const double val5 = (4*(rz2 + xy2))/square(val3);
00318                                         const double val6 = 1.0/(square(rz2 + xy2)/square(val3) + 1);
00319                                         const double val7 = 2.0/ sqrt(1 - square(ry2 - 2*x()*z()));
00320                                         const double val8 = (val22/val12 + 1);
00321                                         const double val9 = -2.0/val8;
00322                                         // row 1:
00323                                         out_dr_dq->get_unsafe(0,0) = -2*z()/val4;
00324                                         out_dr_dq->get_unsafe(0,1) = -2*y()/val4;
00325                                         out_dr_dq->get_unsafe(0,2) = -(2*x()/val3 - y()*val5)*val6 ;
00326                                         out_dr_dq->get_unsafe(0,3) = -(2*r()/val3 - z()*val5)*val6;
00327                                         // row 2:
00328                                         out_dr_dq->get_unsafe(1,0) = y()*val7  ;
00329                                         out_dr_dq->get_unsafe(1,1) = -z()*val7 ;
00330                                         out_dr_dq->get_unsafe(1,2) = r()*val7 ;
00331                                         out_dr_dq->get_unsafe(1,3) = -x()*val7 ;
00332                                         // row 3:
00333                                         out_dr_dq->get_unsafe(2,0) = val9*x()/val1 ;
00334                                         out_dr_dq->get_unsafe(2,1) = val9*(r()/val1 - (2*x()*val2)/val12) ;
00335                                         out_dr_dq->get_unsafe(2,2) = val9*(z()/val1 - (2*y()*val2)/val12) ;
00336                                         out_dr_dq->get_unsafe(2,3) = val9*y()/val1 ;
00337                                 }
00338                         }
00339                 }
00340 
00341                 inline CQuaternion  operator * (const T &factor)
00342                 {
00343                         CQuaternion q = *this;
00344                         q*=factor;
00345                         return q;
00346                 }
00347 
00348                 };      // end class
00349 
00350                 typedef CQuaternion<double> CQuaternionDouble;  //!< A quaternion of data type "double"
00351                 typedef CQuaternion<float>  CQuaternionFloat;   //!< A quaternion of data type "float"
00352 
00353         }       // end namespace
00354 
00355 } // end namespace mrpt
00356 
00357 #endif



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