Jafar
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
bundleVariables.hpp
00001 #ifndef BUNDLE_BUNDLEVARIABLES_HPP
00002 #define BUNDLE_BUNDLEVARIABLES_HPP
00003 
00004 #include <iostream>
00005 #include <fstream>
00006 #include <string>
00007 #include <vector>
00008 
00009 #include "kernel/jafarMacro.hpp"
00010 #include "jmath/jblas.hpp"
00011 
00012 #include "camera/cameraBarreto.hpp"
00013 #include "camera/cameraPinhole.hpp"
00014 
00015 #include "bundle/bundleDimension.hpp"
00016 #include "bundle/bundleParameters.hpp"
00017 
00018 #include "geom/t3dEuler.hpp"
00019 #include "geom/t3dPointTools.hpp"
00020 
00021 
00022 namespace jafar {
00023 
00024   namespace bundle {
00025 
00034     template <typename Sensor>
00035     class bundleVariables {
00036       
00038       bundleDimension pbDim;    
00039       
00041       Sensor Cam;
00042       
00044       std::vector<jafar::geom::T3DEuler> vecT3D;
00045       
00047       std::vector<jblas::vec3> obj3dinCamFrame;
00048 
00050       jblas::vec VoV; // vector of variables 
00051 
00053       std::vector<jblas::vec_range*> oneCamVov;
00054       
00056       std::vector<jblas::vec_range*> oneObjVov;
00057            
00059       jblas::vec_range motPartVov;
00060       
00062       jblas::vec_range strPartVov;
00063 
00065       jafar::geom::T3DEuler Rob2Cam; 
00066 
00067 
00068     public :
00069       //
00070       // Constructors
00071       //
00072       //==============
00073 
00079       bundleVariables (bundleParameters const &bP) :
00080   pbDim(bP.bDim),
00081   Cam (),
00082   vecT3D(pbDim.nbCam),
00083   obj3dinCamFrame(pbDim.nbCam*pbDim.nbObj3d),
00084   VoV(pbDim.nbCam*pbDim.cpdvSize+3*pbDim.nbObj3d),
00085   oneCamVov(pbDim.nbCam),
00086   oneObjVov(pbDim.nbObj3d),
00087   motPartVov(VoV,ublas::range(0,pbDim.nbCam*pbDim.cpdvSize)),
00088   strPartVov(VoV,ublas::range(pbDim.nbCam*pbDim.cpdvSize,VoV.size())),
00089   Rob2Cam()
00090       {
00091   double pR2C[vecT3D[0].size()];
00092   
00093   // 
00094   // Extended Intrinsic Parameters
00095   //
00096   Cam.load(bP.camParamFile);
00097 
00098   // Open bP.robToCamFile
00099   std::ifstream infile(bP.robToCamFile.c_str());
00100   
00101   if (infile.is_open())
00102     {
00103       // get info from bP.robToCamFIle
00104       infile.read((char*)pR2C,vecT3D[0].size()*sizeof(double));
00105       
00106       // Close file
00107       infile.close();                
00108       
00109       // Set Values of Rob2Cam
00110       Rob2Cam.setValue(pR2C[0],pR2C[1],pR2C[2],pR2C[3],pR2C[4],pR2C[5]);
00111           
00112     }
00113   else
00114     {
00115       JFR_WARNING("Rob2Cam is set to default because of unreachable file");
00116       Rob2Cam.setValue(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
00117     }
00118 
00119   //
00120   // Init vector of pointers on vec_range
00121   // 
00122   for (unsigned int cpt=0; cpt<pbDim.nbCam; cpt++)
00123     oneCamVov[cpt] = new jblas::vec_range(VoV,ublas::range(cpt*pbDim.cpdvSize,(cpt+1)*pbDim.cpdvSize));
00124   
00125   unsigned int decalage = pbDim.nbCam*pbDim.cpdvSize;
00126   
00127   for (unsigned int cpt=0; cpt<pbDim.nbObj3d; cpt++)
00128     oneObjVov[cpt] = new jblas::vec_range(VoV,ublas::range(decalage+3*cpt,decalage+3*(cpt+1)));   
00129   
00130       };
00131       
00136       bundleVariables (bundleVariables const& bv_) :
00137   pbDim(bv_.pbDim),
00138   Cam(bv_.Cam),
00139   vecT3D(bv_.vecT3D),
00140   obj3dinCamFrame(bv_.obj3dinCamFrame),
00141   VoV(bv_.VoV),
00142   oneCamVov(pbDim.nbCam),
00143   oneObjVov(pbDim.nbObj3d),
00144   motPartVov(VoV,ublas::range(0,pbDim.nbCam*pbDim.cpdvSize)),
00145   strPartVov(VoV,ublas::range(pbDim.nbCam*pbDim.cpdvSize,VoV.size())),
00146   Rob2Cam(bv_.Rob2Cam)  
00147       {
00148   //
00149   // Init vector of vec_range
00150   // 
00151   for (unsigned int cpt=0; cpt<pbDim.nbCam; cpt++)
00152     oneCamVov[cpt] = new jblas::vec_range(VoV,ublas::range(cpt*pbDim.cpdvSize,(cpt+1)*pbDim.cpdvSize));
00153   
00154   unsigned int decalage = pbDim.nbCam*pbDim.cpdvSize;
00155   
00156   for (unsigned int cpt=0; cpt<pbDim.nbObj3d; cpt++)
00157     oneObjVov[cpt] = new jblas::vec_range(VoV,ublas::range(decalage+3*cpt,decalage+3*(cpt+1)));   
00158       };
00159 
00160       //
00161       // Accessors
00162       //
00163       //===========
00164 
00168       bool getCPDV (unsigned int indCam, jblas::vec &cpdvVector)
00169       {
00170   JFR_PRECOND(indCam<vecT3D.size(),"bundleVariables::getCPDV - indCam is out of range of vecT3D");
00171   JFR_PRECOND(cpdvVector.size()==vecT3D[0].size(),"bundleVariables::getCPDV - cpdvVector has an invalid size");
00172   
00173   cpdvVector.assign(vecT3D[indCam].getX());
00174 
00175   // Manque un test pour comparer contenu de la T3D et de VoV!
00176   
00177   return(true);
00178       }
00179 
00183       bool getObj3D (unsigned int indObj3D, jblas::vec &objVector)
00184       {
00185   JFR_PRECOND(indObj3D<pbDim.nbObj3d,"bundleVariables::getObj3D - Out of range");
00186   JFR_PRECOND(objVector.size()==3,"bundleVariables::getObj3D - The objVector size is not 3");
00187 
00188   
00189   objVector.assign(*(oneObjVov[indObj3D]));
00190   return true;
00191       };
00192 
00193  
00194       //
00195       // Mutators
00196       //
00197       //==========      
00198 
00204       void update (jblas::vec const& dp)
00205       {
00206   JFR_PRECOND(dp.size()==VoV.size(),"bundleVariables::update - deltap has an invalid size");
00207   
00208   unsigned int cptCam;
00209   
00210   // First step : add dp to VoV
00211   // WARNING : if rotation parametrization uses quaternion
00212   //           we can use a simple addition
00213   
00214   VoV += dp;
00215   
00216   // Second step : update the T3DEuler
00217   // WARNING : For Quaternion, it will be different
00218   for (cptCam=0;cptCam<pbDim.nbCam;cptCam++)
00219     vecT3D[cptCam].setValue((*(oneCamVov[cptCam]))[0],(*(oneCamVov[cptCam]))[1],(*(oneCamVov[cptCam]))[2],
00220           (*(oneCamVov[cptCam]))[3],(*(oneCamVov[cptCam]))[4],(*(oneCamVov[cptCam]))[5]);   
00221       };
00222 
00223 
00227       void update (bundleVariables const& bv_)
00228       {
00229   JFR_PRECOND(bv_.pbDim.nbCam == pbDim.nbCam,"bundleVariables::update - Invalid number of cameras");
00230   JFR_PRECOND(bv_.pbDim.nbObj3d == pbDim.nbObj3d,"bundleVariables::update - Invalid number of 3D objects");
00231   JFR_PRECOND(bv_.VoV.size() == VoV.size(),"bundleVariables:update - Invalid sizw of the new state vector");  
00232   
00233   unsigned int cptCam = 0;
00234   
00235   // Copy of T3D
00236   for (cptCam=0;cptCam<pbDim.nbCam;cptCam++)
00237     vecT3D[cptCam].assign(bv_.vecT3D[cptCam]);
00238   
00239   // Copy of data vector
00240   VoV.assign(bv_.VoV);
00241   
00242       };
00243 
00244 
00248       void initFromFile (bundle::bundleParameters const &bP)
00249       {
00250   
00251   std::ifstream infile;
00252   double vtemp[pbDim.cpdvSize], vtemp2[3];
00253   int numIma;
00254   
00255   // Init CPDV  
00256   infile.open(bP.initCpdvFile.c_str(),std::ios::binary | std::ios::in);
00257     
00258   if (infile.is_open())
00259     {     
00260       for (unsigned int cpt=0 ; cpt<pbDim.nbCam ; cpt++)
00261         {
00262     infile.read((char*)&numIma,sizeof(unsigned int));
00263     infile.read((char*)vtemp,pbDim.cpdvSize*sizeof(double));      
00264     
00265     vecT3D[cpt].setValue(vtemp[0],vtemp[1],vtemp[2],vtemp[3],vtemp[4],vtemp[5]);
00266     
00267     for (unsigned int cptt=0 ; cptt<pbDim.cpdvSize ; ++cptt)
00268       (*(oneCamVov[cpt]))[cptt] = vtemp[cptt];        
00269     
00270         }
00271     }
00272 
00273   infile.close();
00274 
00275 
00276   // Init Structure
00277   infile.open(bP.initObj3dFile.c_str(),std::ios::binary | std::ios::in);
00278   
00279   if (infile.is_open())
00280     {
00281       for (unsigned int cpt=0 ; cpt<pbDim.nbObj3d ; cpt++)
00282         {
00283     infile.read((char*)&numIma,sizeof(unsigned int));
00284     infile.read((char*)vtemp2,3*sizeof(double));
00285 
00286     for (unsigned int cptt=0 ; cptt<3 ; ++cptt)
00287       (*(oneObjVov[cpt]))[cptt] = vtemp2[cptt];   
00288         }
00289     }
00290   
00291   
00292   
00293   infile.close();
00294       };      
00295 
00296 
00300       double L2normP ()
00301       {
00302   return ublas::norm_2(VoV);
00303       }
00304       
00305 
00306       //
00307       // Projection and Jacobian computing methods
00308       // 
00309       //===========================================
00310       
00314       void projection (unsigned int const n, unsigned int const k, jblas::vec &projnk)
00315       {
00316   JFR_PRECOND(projnk.size()==2,"bundleVariables::projection - Invalid output vector size. Must be equal 2");
00317   JFR_PRECOND(n<pbDim.nbObj3d,"bundleVariables::projection - You try to project an unknown 3D features");
00318   JFR_PRECOND(k<pbDim.nbCam,"bundleVariables::projection - You try to project a 3D features on an unknown camera.");
00319   
00320   jblas::vec3 vtmp1, vtmp2;
00321   
00322   vtmp1 = (*oneObjVov[n]);
00323 
00324 
00325   /* First  Step : From World Frame to Robot Frame */
00326   geom::t3d::pointToFrame<jblas::vec3,jblas::vec3>(vecT3D[k],vtmp1,vtmp2);
00327 
00328   
00329   /* Second Step : From Robot Frame to Camera Frame */
00330   geom::t3d::pointToFrame<jblas::vec3,jblas::vec3>(Rob2Cam,vtmp2,vtmp1);          
00331   obj3dinCamFrame[n*pbDim.nbCam+k].assign(vtmp1);
00332 
00333     
00334   /* Third  Step : Central projection */
00335   Cam.project(vtmp1,projnk);
00336   
00337       };
00338 
00342       void projectionJac (unsigned int const n, unsigned int const k, jblas::mat &jacCPDVnk, jblas::mat &jacOBJ3Dnk)
00343       {
00344   JFR_PRECOND(jacCPDVnk.size1()==2 && jacCPDVnk.size2()==vecT3D[0].size(),"bundleVariables::projectionJac - jacCPDVnk has an invalid size.");
00345   JFR_PRECOND(jacOBJ3Dnk.size1()==2 && jacOBJ3Dnk.size2()==3,"bundleVariables::projectionJac - jacOBJ3Dnk has an invalid size.");
00346          
00347 
00348   jblas::mat matTMP1(3,6), matTMP2(3,3);
00349   jblas::mat matCPDV(3,6), matOBJ3D(3,3);       
00350   jblas::mat mat2D(2,3);
00351   
00352   jblas::mat33 Rr2c, tRr2c; // pour stocker la matrice de rotation de la transfo Rob2Cam
00353   
00354   /* First Step  : Jacobian matrix X|rob w.r.t. X|w and CPDV */ 
00355   geom::t3d::pointToFrameJacEuler(vecT3D[k].getX(),*(oneObjVov[n]),matTMP1,matTMP2);
00356   
00357   /* Second Step : Combinaison with Rr2c */
00358   Rr2c = Rob2Cam.getR();
00359   tRr2c = trans(Rr2c);
00360   matCPDV = prod(tRr2c,matTMP1);
00361   matOBJ3D = prod(tRr2c,matTMP2);
00362 
00363   /* Third Step  : Combinaison with Jacobian matrix */
00364   Cam.projectJac(obj3dinCamFrame[n*pbDim.nbCam+k],mat2D);
00365   jacCPDVnk  = prod(mat2D,matCPDV);
00366   jacOBJ3Dnk = prod(mat2D,matOBJ3D);
00367 
00368       };
00369 
00373       void exportInFile (bundleParameters bP)
00374       {
00375   
00376   std::ofstream outfile;
00377 
00378   // Export 3D position
00379   outfile.open(bP.outObj3dFile.c_str(),std::ios::binary | std::ios::out);
00380   
00381   if (outfile.is_open())      
00382     {
00383       for (unsigned int cptObj=0 ; cptObj<pbDim.nbObj3d ; ++cptObj)
00384         {
00385     outfile.write((char*)&cptObj,sizeof(unsigned int));
00386     
00387     for (unsigned int k=0;k<3;k++)
00388       outfile.write((char*)&((*(oneObjVov[cptObj]))[k]),sizeof(double));
00389     
00390         }
00391       outfile.close();
00392     }
00393   
00394   // Export extrinsic parameters
00395   outfile.open(bP.outCpdvFile.c_str(),std::ios::binary | std::ios::out);
00396   
00397   if (outfile.is_open())      
00398     {
00399       for (unsigned int cptCam=0 ; cptCam<pbDim.nbCam ; ++cptCam)
00400         {
00401     outfile.write((char*)&cptCam,sizeof(unsigned int));
00402 
00403     for (unsigned int k=0;k<pbDim.cpdvSize;k++)
00404       outfile.write((char*)&((*(oneCamVov[cptCam]))[k]),sizeof(double));
00405 
00406         }
00407       outfile.close();   
00408     }
00409   
00410       };
00411       
00412 
00413     };
00414 
00415   } // namespace bundle
00416 
00417 } // namespace jafar 
00418 
00419 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Wed Oct 15 2014 00:37:17 for Jafar by doxygen 1.7.6.1
LAAS-CNRS