SE3.h

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------------
00002 //
00003 // $Id$
00004 //
00005 // Copyright 2008, 2009, 2010, 2011, 2012  Antonio Franchi and Paolo Stegagno    
00006 //
00007 // This file is part of MIP.
00008 //
00009 // MIP is free software: you can redistribute it and/or modify
00010 // it under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation, either version 3 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // MIP is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with MIP. If not, see <http://www.gnu.org/licenses/>.
00021 //
00022 // Contact info: antonio.franchi@tuebingen.mpg.de stegagno@diag.uniroma1.it
00023 //
00024 // ----------------------------------------------------------------------------
00025 
00026 
00030 
00032 /* @{ */
00033 
00034 
00035 #ifndef __SE3_H_
00036 #define __SE3_H_
00037 
00038 #ifdef MIP_HOST_APPLE
00039 #include <applePatch.h>
00040 #endif
00041 
00042 #include <stdio.h>
00043 #include <math.h>
00044 #include <vector>
00045 #include <iostream>
00046 #include <string>
00047 #include <sstream>
00048 #include <sys/time.h>
00049 #include <sys/select.h>
00050 #include <assert.h>
00051 
00052 #ifdef FOUND_BOOST_SER
00053  #include <boost/serialization/vector.hpp>
00054  #include <boost/archive/text_iarchive.hpp>
00055  #include <boost/archive/text_oarchive.hpp>
00056 #endif
00057 
00058 #include <Types.h>
00059 
00060 #include <R3.h>
00061 #include <S1.h>
00062 #include <S3.h>
00063 
00064 using namespace std;
00065 
00068 class Pose3D {
00069  public:
00071   Pose3D() {
00072    _valid = false;
00073   };
00075   Pose3D(Decimal x, Decimal y, Decimal z, Angle roll, Angle pitch, Angle yaw) {
00076    _pos = Position3D(x,y,z);
00077    _ori = Orientation3D(roll, pitch, yaw);
00078    _valid = true;
00079   };
00081   Pose3D(Position3D pos,Orientation3D ori) {
00082    _pos = pos;
00083    _ori = ori;
00084    _valid = true;
00085   };
00087   Pose3D(Decimal x, Decimal y, Decimal z,Orientation3D ori) {
00088    _pos = Position3D(x,y,z);
00089    _ori = ori;
00090    _valid = true;
00091   };
00093   Pose3D(Position3D pos,Angle roll, Angle pitch, Angle yaw) {
00094    _pos = pos;
00095    _ori = Orientation3D(roll, pitch, yaw);
00096    _valid = true;
00097   };
00099   Pose3D(const Pose3D& p) {
00100    _pos = p._pos;
00101    _ori = p._ori;
00102    _valid =  p._valid;
00103   };
00105   Pose3D& operator=(const Pose3D& rhs){
00106    if (this != &rhs){      // Not necessary in this case but it is useful to don't forget it
00107     _ori = rhs._ori;
00108     _pos = rhs._pos;
00109     _valid =  rhs._valid;
00110    }
00111    return *this;
00112   }
00114   Pose3D& operator+=(const Pose3D& a) {
00115    _ori += a._ori;
00116    _pos += a._pos;
00117    return *this;
00118   }
00120   Pose3D& operator-=(const Pose3D& a) {
00121    _ori -= a._ori;
00122    _pos -= a._pos;
00123    return *this;
00124   }
00126   const Pose3D operator+(const Pose3D &other) const {
00127    return Pose3D(*this) += other;
00128   }
00130   const Pose3D operator-(const Pose3D &other) const {
00131    return Pose3D(*this) -= other;
00132   }
00133   
00137   const Position3D operator*(Position3D p) {
00138    // BUG manca termine di traslazione!!!!
00139    /*TODO*/ Decimal x = cos(_ori.yaw().dCast2Pi())*cos(_ori.pitch().dCast2Pi())*p.x() +
00140           (cos(_ori.yaw().dCast2Pi())*sin(_ori.pitch().dCast2Pi())*sin(_ori.roll().dCast2Pi())
00141           -sin(_ori.yaw().dCast2Pi())*cos(_ori.roll().dCast2Pi()))*p.y() +
00142           (cos(_ori.yaw().dCast2Pi())*sin(_ori.pitch().dCast2Pi())*cos(_ori.roll().dCast2Pi())
00143           -sin(_ori.yaw().dCast2Pi())*sin(_ori.roll().dCast2Pi()))*p.z();
00144    /*TODO*/ Decimal y = sin(_ori.yaw().dCast2Pi())*cos(_ori.pitch().dCast2Pi())*p.x() +
00145           (sin(_ori.yaw().dCast2Pi())*sin(_ori.pitch().dCast2Pi())*sin(_ori.roll().dCast2Pi())
00146           -cos(_ori.yaw().dCast2Pi())*cos(_ori.roll().dCast2Pi()))*p.y() +
00147           (sin(_ori.yaw().dCast2Pi())*sin(_ori.pitch().dCast2Pi())*cos(_ori.roll().dCast2Pi())
00148           -cos(_ori.yaw().dCast2Pi())*sin(_ori.roll().dCast2Pi()))*p.z();
00149    /*TODO*/ Decimal z = -sin(_ori.pitch().dCast2Pi())*p.x() +
00150           cos(_ori.pitch().dCast2Pi())*sin(_ori.roll().dCast2Pi())*p.y() +
00151           cos(_ori.pitch().dCast2Pi())*cos(_ori.roll().dCast2Pi())*p.z();
00152    return Position3D(x,y,z);
00153   }
00155   bool operator==(const Pose3D &other) const {
00156    if( (_pos == other._pos) && (_ori == other._ori) ) 
00157     return true;
00158    else return false;
00159   }
00161   bool operator!=(const Pose3D &other) const {
00162    return !(*this == other);
00163   }
00165   Position3D pos() {
00166    return _pos;
00167   }
00168   Orientation3D ori () {
00169    return _ori;
00170   }
00172   bool valid () {
00173    return _valid;
00174   }
00176   string print(){
00177    stringstream s;
00178    s << "(" << _pos.print() << "," << _ori.print() << ")" ;
00179    return s.str();
00180   }
00182   void directComposition(Pose3D &t){
00183    //TODO
00184 /*   Decimal cosTh = cos( t.ori().dCast() );
00185    Decimal sinTh = sin( t.ori().dCast() );
00186    
00187    Position pos0(pos());
00188    Angle ori0 = ori();
00189  
00190    (*this) = Pose3D(cosTh*pos0.x()-sinTh*pos0.y()+t.pos().x(), sinTh*pos0.x()+cosTh*pos0.y()+t.pos().y(), ori()+t.ori());*/
00191   }
00193   void inverseComposition(Pose3D &t){
00194    //TODO
00195 /*   Decimal cosAth = cos(t.ori().dCast());
00196    Decimal sinAth = sin(t.ori().dCast());
00197    Pose3D temp(-t.pos().x()*cosAth-t.pos().y()*sinAth, +t.pos().x()*sinAth-t.pos().y()*cosAth, Angle(-t.ori().dCast()) );
00198    directComposition(temp);*/
00199   }
00200   /*TODO implementare le composizioni di pose*/
00201   /*TODO implementare le inversioni di pose*/
00202   
00204   void directCompositionYaw(Pose3D &t){
00205         Decimal cosTh = cos( t.ori().yaw().dCast2Pi() );
00206         Decimal sinTh = sin( t.ori().yaw().dCast2Pi() );
00207         
00208         Position3D pos0(pos());
00209         Orientation3D ori0 = ori();
00210 
00211         (*this) = Pose3D(cosTh*pos0.x()-sinTh*pos0.y()+t.pos().x(), sinTh*pos0.x()+cosTh*pos0.y()+t.pos().y(),t.pos().z()+pos0.z(), ori()+t.ori());
00212   }
00214   void inverseCompositionYaw(Pose3D &t){
00215         Decimal cosAth = cos(t.ori().yaw().dCast2Pi());
00216         Decimal sinAth = sin(t.ori().yaw().dCast2Pi());
00217         Pose3D temp(-t.pos().x()*cosAth-t.pos().y()*sinAth, +t.pos().x()*sinAth-t.pos().y()*cosAth, t.pos().z(), Orientation3D(0.0,0.0,-t.ori().yaw().dCast2Pi()) );
00218         directCompositionYaw(temp);
00219   }  
00220   
00222   void setPos(Position3D &p){
00223    _pos = p;
00224   }
00225   
00227   void setOri(Orientation3D &o){
00228    _ori = o;
00229   }
00230   
00231   bool fromString(const string& s){
00232    string comma=",",xs,ys,zs,s2,s3,s4,s5,rs,ps,yaws;
00233    int commaPos;
00234    if((commaPos=s.find(comma))!=s.npos){
00235     xs = s.substr(0,commaPos);
00236     s2 = s.substr(commaPos+1,s.size()-commaPos-1);
00237     if((commaPos=s2.find(comma))!=s2.npos){
00238      ys = s2.substr(0,commaPos);
00239      s3 = s2.substr(commaPos+1,s2.size()-commaPos-1);
00240      if((commaPos=s3.find(comma))!=s3.npos){
00241       zs = s3.substr(0,commaPos);
00242       s4 = s3.substr(commaPos+1,s3.size()-commaPos-1);
00243       if((commaPos=s4.find(comma))!=s4.npos){
00244        rs = s4.substr(0,commaPos);
00245        s5 = s4.substr(commaPos+1,s4.size()-commaPos-1);
00246        if((commaPos=s5.find(comma))!=s5.npos){
00247         ps = s5.substr(0,commaPos);
00248         yaws = s5.substr(commaPos+1,s5.size()-commaPos-1);
00249        }else{
00250    //      MIP_STATIC_WARNING("Position3D","Second comma not present in Position3DOption.");
00251         return false;
00252        }
00253       }else{
00254   //      MIP_STATIC_WARNING("Position3D","Second comma not present in Position3DOption.");
00255        return false;
00256       }
00257      }else{
00258  //      MIP_STATIC_WARNING("Position3D","Second comma not present in Position3DOption.");
00259       return false;
00260      }
00261     }else{
00262 //      MIP_STATIC_WARNING("Position3D","Second comma not present in Position3DOption.");
00263      return false;
00264     }
00265     
00266     Decimal x, y, z, ro, pit, yaw;
00267     istringstream ssx(xs);
00268     ssx >> x;
00269     istringstream ssy(ys);
00270     ssy >> y;
00271     istringstream ssz(zs);
00272     ssz >> z;
00273     istringstream ssr(rs);
00274     ssx >> ro;
00275     istringstream ssp(ps);
00276     ssy >> pit;
00277     istringstream ssyaw(yaws);
00278     ssz >> yaw;
00279     
00280     _pos.setX(x);
00281     _pos.setY(y);
00282     _pos.setZ(z);
00283     
00284     _ori.setRoll(ro);
00285     _ori.setPitch(pit);
00286     _ori.setYaw(yaw);
00287    }else{
00288 //     MIP_STATIC_WARNING("Position3D","First comma not present in Position3DOption.");
00289     return false;
00290    }
00291    
00292    return true;
00293   }
00294   
00295   string toString() const{
00296    stringstream ss;
00297    ss << _pos.toString() << "," << _ori.toString();
00298    return ss.str();
00299   }
00300   
00301  private:
00302 #ifdef FOUND_BOOST_SER
00303   friend class boost::serialization::access;
00304   template<class Archive>
00305     void serialize(Archive & ar, const unsigned int version){
00306    ar & _pos; 
00307    ar & _ori;
00308    ar & _valid;
00309     }
00310 #endif
00311   
00312     Position3D    _pos;
00313     Orientation3D _ori;
00314     bool   _valid; 
00315 };
00316 
00322 class PoseFeature3D : public Association<Pose3D,int>{
00323  private:
00324   
00325 //   friend class boost::serialization::access;
00326 //   template<class Archive>
00327 //     void serialize(Archive & ar, const unsigned int version){
00328 //    ar & _pos;
00329 //    ar & _ori;
00330 //    ar & _valid;
00331 //   }
00332 
00333  public:
00334   
00335   
00338   PoseFeature3D() : Association<Pose3D,int>(){}
00339   
00343   PoseFeature3D (Pose3D p) : Association<Pose3D,int>(p){}
00344   
00349   PoseFeature3D (Pose3D p, int i) : Association<Pose3D,int>(p,i){}
00350 
00352   PoseFeature3D (const PoseFeature3D& f) : Association<Pose3D,int>(f){};
00353   
00355   using Association<Pose3D,int>::operator=;
00356   
00359   Pose3D getPose(void){
00360    return getObj();
00361   }
00364   int getId(void){
00365    return getAsso();
00366   }
00369   void setId(int a){
00370    associate(a);
00371   }
00372   
00375   string print(){
00376    stringstream s;
00377    if(associated()){
00378     s << "[" << getPose().print()  << "," << getId() << "]";
00379    }else{
00380     s << "[" << getPose().print()  << ",na]";
00381    }
00382    return s.str();
00383   }
00384 
00389   bool notEqual(PoseFeature3D &pose, Decimal toll){
00390    // Variable TEMP not used
00391 //    Decimal temp = getPose().pos().x() - pose.getPose().pos().x();
00392    if ( !DecimalUtilities::equal(getPose().pos().x(), pose.getPose().pos().x(), toll) ) return true;
00393    if ( !DecimalUtilities::equal(getPose().pos().y(), pose.getPose().pos().y(), toll) ) return true;
00394    if ( !DecimalUtilities::equal(getPose().pos().z(), pose.getPose().pos().z(), toll) ) return true;
00395    if ( !DecimalUtilities::equal(getPose().ori().roll().dCast2Pi(), pose.getPose().ori().roll().dCast2Pi(), toll) ) return true;
00396    if ( !DecimalUtilities::equal(getPose().ori().pitch().dCast2Pi(), pose.getPose().ori().pitch().dCast2Pi(), toll) ) return true;
00397    if ( !DecimalUtilities::equal(getPose().ori().yaw().dCast2Pi(), pose.getPose().ori().yaw().dCast2Pi(), toll) ) return true;
00398    if ( getId() != pose.getId() )           return true;
00399    return false;
00400   }
00401 };
00402 
00403 
00404 #endif
00405 
00406 
00407 

Generated on Mon Feb 20 07:01:07 2017 for MIP by  doxygen 1.5.6