PoseCv.hh
Go to the documentation of this file.
00001 
00007 #ifndef P_POSE_CV_HH
00008 #define P_POSE_CV_HH
00009 
00010 #include <limits.h>
00011 #include <map>
00012 //#include <opencv/cv.h>
00013 //#include <opencv/cxcore.h>
00014 #include <opencv2/opencv.hpp>
00015 #include <iostream>
00016 #include <blort/Recognizer3D/PNamespace.hh>
00017 #include <blort/Recognizer3D/Array.hh>
00018 #include <opencv2/core/core.hpp>
00019 
00020 namespace P
00021 {
00022 
00023 
00024 class PoseCv
00025 {
00026 private:    
00027   
00028 
00029 public:
00030   CvMat *R;
00031   CvMat *t;
00032   CvMat *n;
00033 
00034   PoseCv();
00035   PoseCv(cv::Mat translation, cv::Mat rotation);
00036   ~PoseCv();
00037 };
00038 
00039 void InitializePoseCv(PoseCv &pose);
00040 void DeletePoseCv(Array<PoseCv*> &ps);
00041 
00042 inline void CopyPoseCv(PoseCv &in, PoseCv &out);
00043 inline void Rot2Quat(CvMat *M, double *q);
00044 inline void Rot2Vec3(CvMat *R, double *d);
00045 inline void Quat2Rot(double *q, CvMat *M);
00046 inline void Vec32Rot(double *d, CvMat *R);
00047 inline void InvPoseCv(PoseCv &in, PoseCv &out);
00048 
00049 
00050 
00051 /*********************************** INLINE *********************************/
00052 
00053 inline void CopyPoseCv(PoseCv &in, PoseCv &out)
00054 { 
00055   cvCopy(in.R, out.R);
00056   cvCopy(in.t, out.t);
00057   cvCopy(in.n, out.n);
00058 }
00059 
00060 inline void Rot2Quat(CvMat *M, double *q)
00061 {
00062   float *R = M->data.fl;
00063 
00064   double tmp[4];
00065   double mag;
00066   unsigned maxpos;
00067   tmp[0]=1.0 + R[0] + R[4] + R[8];
00068   tmp[1]=1.0 + R[0] - R[4] - R[8];
00069   tmp[2]=1.0 - R[0] + R[4] - R[8];
00070   tmp[3]=1.0 - R[0] - R[4] + R[8];
00071 
00072   mag=-1.0;
00073   for(unsigned i=0; i<4; i++){
00074     if(tmp[i]>mag){
00075       mag=tmp[i];
00076       maxpos=i;
00077     }
00078   }
00079   if(maxpos==0){
00080     q[0]=sqrt(tmp[0])*0.5;
00081     q[1]=(R[7] - R[5])/(4.0*q[0]);
00082     q[2]=(R[2] - R[6])/(4.0*q[0]);
00083     q[3]=(R[3] - R[1])/(4.0*q[0]);
00084   }
00085   else if(maxpos==1){
00086     q[1]=sqrt(tmp[1])*0.5;
00087     q[0]=(R[7] - R[5])/(4.0*q[1]);
00088     q[2]=(R[3] + R[1])/(4.0*q[1]);
00089     q[3]=(R[2] + R[6])/(4.0*q[1]);
00090   }
00091   else if(maxpos==2){
00092     q[2]=sqrt(tmp[2])*0.5;
00093     q[0]=(R[2] - R[6])/(4.0*q[2]);
00094     q[1]=(R[3] + R[1])/(4.0*q[2]);
00095     q[3]=(R[7] + R[5])/(4.0*q[2]);
00096   }
00097   else if(maxpos==3){
00098     q[3]=sqrt(tmp[3])*0.5;
00099     q[0]=(R[3] - R[1])/(4.0*q[3]);
00100     q[1]=(R[2] + R[6])/(4.0*q[3]);
00101     q[2]=(R[7] + R[5])/(4.0*q[3]);
00102   }
00103   else
00104   {
00105     cout<<"komisch"<<endl;
00106   }
00107   // enforce unit length
00108   mag=q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3];
00109 
00110   if(!IsZero(mag-1.))
00111   {
00112 
00113     mag=1.0/sqrt(mag);
00114     q[0]*=mag; q[1]*=mag; q[2]*=mag; q[3]*=mag;
00115   }
00116 }
00117 
00118 inline void Rot2Vec3(CvMat *R, double *d)
00119 {
00120   double t[4];
00121   Rot2Quat(R,t);
00122 
00123   double mag=sqrt(Sqr(t[0]) + Sqr(t[1]) + Sqr(t[2]) + Sqr(t[3]));
00124   double sg=(t[0]>=0.0)? 1.0 : -1.0;
00125   mag=sg/mag;
00126   d[0] = t[1]*mag;
00127   d[1] = t[2]*mag;
00128   d[2] = t[3]*mag;
00129 }
00130 
00131 inline void Quat2Rot(double *q, CvMat *M)
00132 {
00133   float *R = M->data.fl;
00134 
00135   // ensure unit length
00136   double mag = q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3];
00137   if(!IsZero(mag-1.0))
00138   {
00139     mag=1.0/sqrt(mag);
00140     q[0]*=mag; q[1]*=mag; q[2]*=mag; q[3]*=mag;
00141   }
00142 
00143   R[0]=q[0]*q[0]+q[1]*q[1]-q[2]*q[2]-q[3]*q[3];
00144   R[1]=2*(q[1]*q[2]-q[0]*q[3]);
00145   R[2]=2*(q[1]*q[3]+q[0]*q[2]);
00146 
00147   R[3]=2*(q[1]*q[2]+q[0]*q[3]);
00148   R[4]=q[0]*q[0]+q[2]*q[2]-q[1]*q[1]-q[3]*q[3];
00149   R[5]=2*(q[2]*q[3]-q[0]*q[1]);
00150 
00151   R[6]=2*(q[1]*q[3]-q[0]*q[2]);
00152   R[7]=2*(q[2]*q[3]+q[0]*q[1]);
00153   R[8]=q[0]*q[0]+q[3]*q[3]-q[1]*q[1]-q[2]*q[2];
00154 }
00155 
00156 inline void Vec32Rot(double *d, CvMat *R)
00157 {
00158   double q[4];
00159   q[0] = sqrt(1.0 - Sqr(d[0]) - Sqr(d[1])- Sqr(d[2]));
00160   q[1] = d[0];
00161   q[2] = d[1];
00162   q[3] = d[2];
00163 
00164   Quat2Rot(q,R);  
00165 }
00166 
00167 inline void InvPoseCv(PoseCv &in, PoseCv &out)
00168 {
00169   cvTranspose( in.R, out.R );
00170   cvGEMM( out.R, in.t, -1, 0, 0, out.t, 0 );
00171 }
00172 
00173 
00174 } //--THE END--
00175 
00176 #endif
00177 


blort
Author(s): Michael Zillich, Thomas Mörwald, Johann Prankl, Andreas Richtsfeld, Bence Magyar (ROS version)
autogenerated on Thu Jan 2 2014 11:38:25