transformation2.hh
Go to the documentation of this file.
00001 /**********************************************************************
00002  *
00003  * This source code is part of the Tree-based Network Optimizer (TORO)
00004  *
00005  * TORO Copyright (c) 2007 Giorgio Grisetti, Cyrill Stachniss, 
00006  *                         Slawomir Grzonka, and Wolfram Burgard
00007  *
00008  * TORO is licences under the Common Creative License,
00009  * Attribution-NonCommercial-ShareAlike 3.0
00010  *
00011  * You are free:
00012  *   - to Share - to copy, distribute and transmit the work
00013  *   - to Remix - to adapt the work
00014  *
00015  * Under the following conditions:
00016  *
00017  *   - Attribution. You must attribute the work in the manner specified
00018  *     by the author or licensor (but not in any way that suggests that
00019  *     they endorse you or your use of the work).
00020  *  
00021  *   - Noncommercial. You may not use this work for commercial purposes.
00022  *  
00023  *   - Share Alike. If you alter, transform, or build upon this work,
00024  *     you may distribute the resulting work only under the same or
00025  *     similar license to this one.
00026  *
00027  * Any of the above conditions can be waived if you get permission
00028  * from the copyright holder.  Nothing in this license impairs or
00029  * restricts the author's moral rights.
00030  *
00031  * TORO is distributed in the hope that it will be useful,
00032  * but WITHOUT ANY WARRANTY; without even the implied 
00033  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00034  * PURPOSE.  
00035  **********************************************************************/
00036 
00044 #ifndef _TRANSFORMATION2_HXX_
00045 #define _TRANSFORMATION2_HXX_
00046 
00047 #include <cmath>
00048 
00049 namespace AISNavigation
00050 {
00051 
00053 template <class T>
00054 struct Vector2{
00055   T values[2] ; 
00056 
00058   Vector2(T x, T y)        {values[0]=x; values[1]=y;}
00060   Vector2()                {values[0]=0; values[1]=0;}
00061 
00063   inline const T& x()      const  {return values[0];}
00065   inline const T& y()      const  {return values[1];}
00066 
00068   inline T& x()            {return values[0];}
00070   inline T& y()            {return values[1];}
00071 
00073   inline T norm2() const   {
00074     return values[0]*values[0]+values[1]*values[1];
00075   }
00076 
00077 };
00078 
00080 template <class T>
00081 inline Vector2<T> operator * (const T& d, const Vector2<T>& v) {
00082   return Vector2<T>(v.values[0]*d, v.values[1]*d);
00083 }
00084 
00086 template <class T>
00087 inline Vector2<T> operator * (const Vector2<T>& v, const T& d) {
00088   return Vector2<T>(v.values[0]*d, v.values[1]*d);
00089 }
00090 
00092 template <class T>
00093 inline T operator * (const Vector2<T>& v1, const Vector2<T>& v2){
00094   return v1.values[0]*v2.values[0] 
00095     + v1.values[1]*v2.values[1];
00096 }
00097 
00099 template <class T>
00100 inline Vector2<T> operator + (const Vector2<T>& v1, const Vector2<T>& v2){
00101   return Vector2<T>(v1.values[0]+v2.values[0], 
00102                     v1.values[1]+v2.values[1]);
00103 }
00104 
00106 template <class T>
00107 Vector2<T> operator - (const Vector2<T>& v1, const Vector2<T>& v2){
00108   return Vector2<T>(v1.values[0]-v2.values[0], 
00109                     v1.values[1]-v2.values[1]);
00110 }
00111 
00112 
00119 template <class T>
00120 struct Pose2{
00121   T values[3];
00122 
00124   inline const T& x()     const  {return values[0];}
00126   inline const T& y()     const  {return values[1];}
00128   inline const T& theta() const  {return values[2];}
00129 
00131   inline T& x()            {return values[0];}
00133   inline T& y()            {return values[1];}
00135   inline T& theta()        {return values[2];}
00136 
00138   Pose2(){
00139     values[0]=0.; values[1]=0.;  values[2]=0.;
00140   }
00141 
00143   Pose2(const T& x, const T& y, const T& theta){
00144     values[0]=x, values[1]=y, values[2]=theta;
00145   }
00146 };
00147 
00149 template <class T>
00150 Pose2<T> operator * (const Pose2<T>& v, const T& d){
00151   Pose2<T> r;
00152   for (int i=0; i<3; i++){
00153     r.values[i]=v.values[i]*d;
00154   }
00155   return r;
00156 }
00157 
00158 
00160 template <class T>
00161 struct Transformation2{
00162   T rotationMatrix[2][2]; 
00163   T translationVector[2]; 
00164 
00168   Transformation2(bool initAsIdentity = true){
00169     if (initAsIdentity) {
00170       rotationMatrix[0][0]=1.; rotationMatrix[0][1]=0.;
00171       rotationMatrix[1][0]=0.; rotationMatrix[1][1]=1.;
00172       translationVector[0]=0.;
00173       translationVector[1]=0.;
00174     }
00175   }
00176 
00178   inline static Transformation2<T> identity(){
00179     Transformation2<T> m(true);
00180     return m;
00181   }
00182 
00184   Transformation2 (const T& x, const T& y, const T& theta){
00185     setRotation(theta);
00186     setTranslation(x,y);
00187   }
00188 
00190   Transformation2 (const T& _theta, const Vector2<T>& trans):
00191     Transformation2(trans.x(), trans.y(), _theta){}
00192 
00193 
00195   Transformation2 (const Pose2<T>& v){
00196     setRotation(v.theta());
00197     setTranslation(v.x(),v.y());
00198   }
00199 
00200 
00202   inline Vector2<T> translation() const {
00203     return Vector2<T>(translationVector[0], 
00204                       translationVector[1]);
00205   }
00206   
00208   inline T rotation()    const {
00209     return atan2(rotationMatrix[1][0],rotationMatrix[0][0]);
00210   }
00211   
00213   inline Pose2<T> toPoseType()   const {
00214     Vector2<T> t=translation();
00215     T r=rotation();
00216     Pose2<T> rv(t.x(), t.y(), r );
00217     return rv;
00218   }
00219   
00221   inline void setTranslation(const Vector2<T>& t){
00222     setTranslation(t.x(),t.y());
00223   }
00224 
00226   inline void setRotation(const T& theta){
00227     T s=sin(theta), c=cos(theta);
00228     rotationMatrix[0][0]=c, rotationMatrix[0][1]=-s;
00229     rotationMatrix[1][0]=s, rotationMatrix[1][1]= c;
00230   }
00231 
00233   inline void setTranslation(const T& x, const T& y){
00234     translationVector[0]=x;
00235     translationVector[1]=y;
00236   }
00237 
00239   inline Transformation2<T> inv() const {
00240     Transformation2<T> rv(*this);
00241     for (int i=0; i<2; i++)
00242       for (int j=0; j<2; j++){
00243         rv.rotationMatrix[i][j]=rotationMatrix[j][i];
00244       }
00245 
00246     for (int i=0; i<2; i++){
00247       rv.translationVector[i]=0;
00248       for (int j=0; j<2; j++){
00249         rv.translationVector[i]-=rv.rotationMatrix[i][j]*translationVector[j];
00250       }
00251     }
00252     return rv;
00253   }
00254 
00255 };
00256 
00258 template <class T>
00259 Vector2<T> operator * (const Transformation2<T>& m, const Vector2<T>& v){
00260   return Vector2<T>(
00261                     m.rotationMatrix[0][0]*v.values[0]+
00262                     m.rotationMatrix[0][1]*v.values[1]+
00263                     m.translationVector[0],
00264                     m.rotationMatrix[1][0]*v.values[0]+
00265                     m.rotationMatrix[1][1]*v.values[1]+
00266                     m.translationVector[1]);
00267 }
00268 
00270 template <class T>
00271 Transformation2<T> operator * (const Transformation2<T>& m1, const Transformation2<T>& m2){
00272   Transformation2<T> rt;
00273   for (int i=0; i<2; i++)
00274     for (int j=0; j<2; j++){
00275       rt.rotationMatrix[i][j]=0.;
00276       for (int k=0; k<2; k++)
00277         rt.rotationMatrix[i][j]+=m1.rotationMatrix[i][k]*m2.rotationMatrix[k][j];
00278     }
00279   for (int i=0; i<2; i++){
00280     rt.translationVector[i]=m1.translationVector[i];
00281     for (int j=0; j<2; j++)
00282       rt.translationVector[i]+=m1.rotationMatrix[i][j]*m2.translationVector[j];
00283   }
00284   return rt;
00285 }
00286 
00287 
00289 template <class T>
00290 struct SMatrix3{
00291   T values[3][3];
00292   T det() const;
00293   SMatrix3<T> transpose() const;
00294   SMatrix3<T> adj() const;
00295   SMatrix3<T> inv() const;
00296 };
00297 
00298 
00300 template <class T>
00301 Pose2<T> operator * (const SMatrix3<T>& m, const Pose2<T>& p){
00302   Pose2<T> v;
00303   for (int i=0; i<3; i++){
00304     v.values[i]=0.;
00305     for (int j=0; j<3; j++)
00306       v.values[i]+=m.values[i][j]*p.values[j];
00307   }
00308   return v;
00309 }
00310 
00312 template <class T>
00313 SMatrix3<T> operator * (const SMatrix3<T>& s, T& d){
00314   SMatrix3<T> m;
00315   for (int i=0; i<3; i++)
00316     for (int j=0; j<3; j++)
00317       m.values[i][j]=d*s.values[i][j];
00318   return m;
00319 }
00320 
00322 template <class T>
00323 SMatrix3<T> operator * (const SMatrix3<T>& s1, const SMatrix3<T>& s2){
00324   SMatrix3<T> m;
00325   for (int i=0; i<3; i++)
00326     for (int j=0; j<3; j++){
00327       m.values[i][j]=0.;
00328       for (int k=0; k<3; k++){
00329         m.values[i][j]+=s1.values[i][k]*s2.values[k][j];
00330       }
00331     }
00332   return m;
00333 }
00334 
00336 template <class T>
00337 SMatrix3<T> operator + (const SMatrix3<T>& s1, const SMatrix3<T>& s2){
00338   SMatrix3<T> m;
00339   for (int i=0; i<3; i++)
00340     for (int j=0; j<3; j++){
00341       m.values[i][j]=s1.values[i][j]+s2.values[i][j];
00342     }
00343   return m;
00344 }
00345 
00346 
00348 template <class T>
00349 T SMatrix3<T>::det() const{
00350    T dp= values[0][0]*values[1][1]*values[2][2]
00351      +values[0][1]*values[1][2]*values[2][0]
00352      +values[0][2]*values[1][0]*values[2][1];
00353    T dm=values[2][0]*values[1][1]*values[0][2]
00354      +values[2][1]*values[1][2]*values[0][0]
00355      +values[2][2]*values[1][0]*values[0][1];
00356    return dp-dm;
00357 }
00358 
00360 template <class T>
00361 SMatrix3<T>  SMatrix3<T>::transpose() const{
00362    SMatrix3<T> m;
00363    for (int i=0; i<3; i++)
00364      for (int j=0; j<3; j++)
00365        m.values[j][i]=values[i][j];
00366    return m;
00367 }
00368 
00370 template <class T>
00371 SMatrix3<T>  SMatrix3<T>::adj() const{
00372    SMatrix3<T> m;
00373    m.values[0][0]= values[1][1]*values[2][2]-values[2][1]*values[1][2];
00374    m.values[0][1]=-values[1][0]*values[2][2]+values[1][2]*values[2][0];
00375    m.values[0][2]= values[1][0]*values[2][1]-values[2][0]*values[1][1];
00376    m.values[1][0]=-values[0][1]*values[2][2]+values[2][1]*values[0][2];
00377    m.values[1][1]= values[0][0]*values[2][2]-values[2][0]*values[0][2];
00378    m.values[1][2]=-values[0][0]*values[2][1]+values[2][0]*values[0][1];
00379    m.values[2][0]= values[0][1]*values[1][2]-values[1][1]*values[0][2];
00380    m.values[2][1]=-values[0][0]*values[1][2]+values[1][0]*values[0][2];
00381    m.values[2][2]= values[0][0]*values[1][1]-values[1][0]*values[0][1];
00382    return m;
00383 }
00384 
00386 template <class T>
00387 SMatrix3<T>  SMatrix3<T>::inv() const{
00388    T id=1./det();
00389    SMatrix3<T> i=adj().transpose();
00390    return i*id;
00391 }
00392 
00393 
00394 
00396 template <class T>
00397 struct Operations2D{
00398   typedef T                  BaseType;            
00399   typedef Pose2<T>           PoseType;            
00400   typedef Pose2<T>           ParametersType;      
00401   typedef T                  RotationType;        
00402   typedef Vector2<T>         TranslationType;     
00403   typedef Transformation2<T> TransformationType;  
00404   typedef SMatrix3<T>        CovarianceType;      
00405   typedef SMatrix3<T>        InformationType;     
00406 };
00407 
00408 } // namespace AISNavigation
00409 
00410 #endif


rtabmap
Author(s): Mathieu Labbe
autogenerated on Fri Aug 28 2015 12:51:42