00001 #ifndef BUNDLE_BUNDLE_HPP
00002 #define BUNDLE_BUNDLE_HPP
00003
00004 #include <iostream>
00005 #include <string>
00006 #include <cstdlib>
00007
00008 #include "bundle/bundleDimension.hpp"
00009 #include "bundle/bundleParameters.hpp"
00010 #include "bundle/linearsys.hpp"
00011
00012 #include "camera/cameraPinhole.hpp"
00013 #include "camera/cameraBarreto.hpp"
00014
00015 #include "boost/timer.hpp"
00016
00017 namespace jafar {
00018
00019 namespace bundle {
00020
00028 template <typename CamType>
00029 void computeResidual (observationsContainer &obs, bundleVariables<CamType> &variables)
00030 {
00031
00032 jblas::vec v2tmp(2), v3tmp(3), v6tmp(6);
00033
00034
00035 for (unsigned int cpt =0; cpt<obs.obs2D.size() ; cpt++)
00036 {
00037 variables.projection((*(obs.obs2D[cpt])).idFeature,(*(obs.obs2D[cpt])).idView,v2tmp);
00038 (*(obs.obs2D[cpt])).setResidual(v2tmp);
00039 }
00040
00041
00042 for (unsigned int cpt =0; cpt<obs.obs3D.size() ; cpt++)
00043 {
00044 variables.getObj3D((*(obs.obs3D[cpt])).idFeature,v3tmp);
00045 (*(obs.obs3D[cpt])).setResidual(v3tmp);
00046 }
00047
00048
00049 for (unsigned int cpt =0; cpt<obs.obsCPDV.size() ; cpt++)
00050 {
00051 variables.getCPDV((*(obs.obsCPDV[cpt])).idView,v6tmp);
00052 (*(obs.obsCPDV[cpt])).setResidual(v6tmp);
00053 }
00054
00055 }
00056
00057
00064 template <typename CamType>
00065 void bundleGN (std::string paramFile)
00066 {
00067
00068 boost::timer t1, t2;
00069
00070 bool stop=false;
00071 double currentcritvalue, tps;
00072 double DPnorm = 0.0, Pnorm = 0.0, Ginfnorm = 0.0;
00073 int LmIterCpt=0;
00074 std::stringstream msg;
00075
00076
00077
00078
00079 bundleParameters bparam;
00080 bparam.load(paramFile);
00081 bparam.bDim.cpdvSize = 6;
00082 bparam.check();
00083
00084
00085
00086
00087 bundleVariables<CamType> Bvar(bparam);
00088 Bvar.initFromFile(bparam);
00089
00090
00091
00092
00093 observationsContainer Bobs(bparam.bDim);
00094 Bobs.initFromFile(bparam);
00095
00096
00097
00098
00099
00100 computeResidual<CamType>(Bobs,Bvar);
00101 currentcritvalue = Bobs.residualWL2Norm();
00102 bundleLinearSys BLS(Bobs);
00103
00104 t2.restart();
00105
00106 std::cout.setf(std::ios_base::dec);
00107 std::cout.unsetf(std::ios_base::showbase);
00108 std::cout << std::endl;
00109 std::cout << std::endl;
00110 std::cout << "| # ";
00111 std::cout << "| critere ";
00112 std::cout << "| normL2(gradient) ";
00113 std::cout << "| normL2(deltap) ";
00114 std::cout << "| Tps execution |" << std::endl;
00115 std::cout << "|-----|---------------|------------------|------------------|---------------|" << std::endl;
00116
00117 for ( LmIterCpt=1 ; (!stop) && (LmIterCpt<=bparam.nbIterLmMax) ; LmIterCpt++)
00118 {
00119
00120 t1.restart();
00121 BLS.processData(Bobs,Bvar);
00122 if (ublas::norm_inf(BLS.gradient)<=bparam.eps1)
00123 {
00124 msg << " Gradient infinite norm is too small : " << Ginfnorm << " for " << bparam.eps1 << std::endl;
00125 stop = true;
00126 break;
00127 }
00128
00129 BLS.solveByGC();
00130
00131
00132 DPnorm = ublas::norm_2(BLS.deltap);
00133 Pnorm = Bvar.L2normP();
00134
00135 Bvar.update(BLS.deltap);
00136 computeResidual<CamType>(Bobs,Bvar);
00137
00138 tps = t1.elapsed();
00139
00140
00141
00142 std::cout << "| ";
00143 std::cout.width(3);
00144 std::cout.setf(std::ios_base::right);
00145 std::cout.setf(std::ios_base::scientific);
00146 std::cout.precision(7);
00147 std::cout << LmIterCpt <<" | ";
00148
00149 std::cout.width(13);
00150 std::cout << currentcritvalue << " | ";
00151
00152 std::cout.width(15);
00153 std::cout.precision(10);
00154 std::cout << ublas::norm_2(BLS.gradient) << " | ";
00155
00156 std::cout.precision(10);
00157 std::cout << DPnorm << " | ";
00158
00159 std::cout.setf(std::ios_base::fixed);
00160 std::cout.width(9);
00161 std::cout << tps << " sec |" << std::endl;
00162 std::cout.unsetf(std::ios_base::fixed);
00163
00164 currentcritvalue = Bobs.residualWL2Norm();
00165
00166 if ( DPnorm <= (bparam.eps2*Pnorm) )
00167 {
00168 msg << " Change in p is too small : " << DPnorm << " for " << bparam.eps2*Pnorm << std::endl;
00169 stop = true;
00170 break;
00171 }
00172
00173 }
00174
00175 tps = t2.elapsed();
00176
00177 std::cout << "|-----|---------------|------------------|------------------|---------------|\n" << std::endl;
00178
00179 std::cout << msg.str() << std::endl;
00180
00181 std::cout.setf(std::ios_base::fixed);
00182 std::cout.width(9);
00183 std::cout << " Optimization time : " << tps << " sec." << std::endl;
00184 std::cout.unsetf(std::ios_base::fixed);
00185
00186
00187 Bvar.exportInFile(bparam);
00188
00189 };
00190
00198 template <typename CamType>
00199 void bundleLM (std::string paramFile)
00200 {
00201
00202 boost::timer t1;
00203 boost::timer t2;
00204
00205 bool stop = false;
00206 double rho, nu = 2.0, mu =0.0, currentcritvalue, newcritvalue, rhoDenom, rhoNum, test1, tau = 1.0e-3;
00207 double nuMax = pow(2.0,15)+1.0;
00208 double DPnorm = 0.0, Pnorm = 0.0, tps;
00209 double Ginfnorm = 0.0;
00210 int LmIterCpt=0;
00211
00212 std::stringstream msg;
00213
00214
00215
00216
00217 bundleParameters bparam;
00218 bparam.load(paramFile);
00219 bparam.bDim.cpdvSize = 6;
00220 bparam.check();
00221
00222
00223
00224
00225 bundleVariables<CamType> Bvar(bparam);
00226 Bvar.initFromFile(bparam);
00227 bundleVariables<CamType> BvarNew(Bvar);
00228
00229
00230
00231
00232 observationsContainer Bobs(bparam.bDim);
00233 Bobs.initFromFile(bparam);
00234
00235
00236
00237 bundleLinearSys BLS(Bobs);
00238
00239
00240 jblas::vec mudpg(BLS.deltap.size());
00241
00242
00243
00244 computeResidual<CamType>(Bobs,Bvar);
00245 currentcritvalue = Bobs.residualWL2Norm();
00246
00247 t2.restart();
00248
00249 std::cout.setf(std::ios_base::dec);
00250 std::cout.unsetf(std::ios_base::showbase);
00251 std::cout << std::endl;
00252 std::cout << std::endl;
00253 std::cout << "| # ";
00254 std::cout << "| critere ";
00255 std::cout << "| normL2(gradient) ";
00256 std::cout << "| normL2(deltap) ";
00257 std::cout << "| Tps execution |" << std::endl;
00258 std::cout << "|-----|---------------|------------------|------------------|---------------|" << std::endl;
00259
00260
00261 for ( LmIterCpt=1 ; (LmIterCpt<=bparam.nbIterLmMax) && (!stop) ; LmIterCpt++ )
00262 {
00263
00264
00265 t1.restart();
00266 BLS.processData(Bobs,Bvar);
00267 Ginfnorm = ublas::norm_inf(BLS.gradient);
00268 if (Ginfnorm<=bparam.eps1)
00269 {
00270 msg << " Gradient infinite norm is too small : " << Ginfnorm << " for " << bparam.eps1 << std::endl;
00271 stop = true;
00272 break;
00273 }
00274
00275 if ( LmIterCpt==1 )
00276 mu = tau*BLS.maxDiagA();
00277
00278
00279 while (true)
00280 {
00281 BLS.solveByGC(mu);
00282
00283
00284 DPnorm = ublas::norm_2(BLS.deltap);
00285 Pnorm = Bvar.L2normP();
00286
00287
00288
00289
00290 if ( DPnorm <= (bparam.eps2*Pnorm) )
00291 {
00292 msg << " Change in p is too small : " << DPnorm << " for " << bparam.eps2*Pnorm << std::endl;
00293 stop = true;
00294 break;
00295 }
00296
00297
00298 BvarNew.update(BLS.deltap);
00299
00300
00301 computeResidual<CamType>(Bobs,BvarNew);
00302
00303
00304 newcritvalue = Bobs.residualWL2Norm();
00305
00306
00307 rhoNum = currentcritvalue-newcritvalue;
00308 mudpg = mu*BLS.deltap;
00309 mudpg += BLS.gradient;
00310 rhoDenom = ublas::inner_prod(BLS.deltap,mudpg);
00311
00312 if ( (rhoNum>0.0) && (rhoDenom>0.0) )
00313 {
00314
00315 rho = rhoNum/rhoDenom;
00316
00317
00318 test1 = 1.0-pow((2.0*rho-1.0),3.0);
00319 mu = (0.33333>test1)?mu/3.0:mu*test1;
00320 nu = 2.0;
00321
00322
00323 Bvar.update(BvarNew);
00324
00325
00326 tps = t1.elapsed();
00327 std::cout << "| ";
00328 std::cout.width(3);
00329 std::cout.setf(std::ios_base::right);
00330 std::cout.setf(std::ios_base::scientific);
00331 std::cout.precision(7);
00332 std::cout << LmIterCpt <<" | ";
00333
00334 std::cout.width(13);
00335 std::cout << currentcritvalue << " | ";
00336
00337 std::cout.width(15);
00338 std::cout.precision(10);
00339 std::cout << ublas::norm_2(BLS.gradient) << " | ";
00340
00341 std::cout.precision(10);
00342 std::cout << DPnorm << " | ";
00343
00344 std::cout.setf(std::ios_base::fixed);
00345 std::cout.width(9);
00346 std::cout << tps << " sec |" << std::endl;
00347 std::cout.unsetf(std::ios_base::fixed);
00348
00349
00350 currentcritvalue = newcritvalue;
00351
00352
00353
00354 break;
00355 }
00356
00357 mu *= nu;
00358 nu *= 2.0;
00359
00360 if (nu>=nuMax)
00361 {
00362 msg << "Too many attemps to increase the damping factor" << std::endl;
00363 stop = true;
00364 break;
00365 }
00366
00367 }
00368
00369 }
00370
00371 tps = t2.elapsed();
00372
00373 std::cout << "|-----|---------------|------------------|------------------|---------------|\n" << std::endl;
00374
00375 std::cout << msg.str() << std::endl;
00376
00377 std::cout.setf(std::ios_base::fixed);
00378 std::cout.width(9);
00379 std::cout << " Optimization time : " << tps << " sec." << std::endl;
00380 std::cout.unsetf(std::ios_base::fixed);
00381
00382
00383 Bvar.exportInFile(bparam);
00384
00385 };
00386
00387 }
00388
00389 }
00390
00391
00392
00393 #endif