00001
00002
00003 #ifndef BUNDLE_SBA_HPP
00004 #define BUNDLE_SBA_HPP
00005
00006 #include <iostream>
00007 #include <string>
00008 #include <cstdlib>
00009
00010 #include "kernel/csvFile.hpp"
00011 #include "bundle/bundleException.hpp"
00012 #include "geom/t3dQuaternion.hpp"
00013 #include "camera/cameraPinhole.hpp"
00014 #include "premodeler/ifManager.hpp"
00015 #include "jmath/jblas.hpp"
00016 #include "jmath/ublasExtra.hpp"
00017
00018
00019 namespace jafar {
00020
00021 namespace bundle {
00022
00023 class SBACamera;
00024 std::ostream& operator<<(std::ostream& s, const jafar::bundle::SBACamera& c);
00025
00026 class SBAObservations;
00027
00035 class SBAObservation {
00036 private:
00038 jblas::vec3 point;
00040 std::map<int, jblas::vec2> projections;
00041 int nbFramesIn;
00042 public:
00044 SBAObservation();
00046 SBAObservation(const SBAObservation& obs);
00048 void setPoint(const jblas::vec3& _point);
00050 jblas::vec3 getPoint() const;
00052 int nbOfFramesIn() const;
00054 jblas::vec2 getProjection(const int& frameIndex);
00056 void insertProjection(const int& frameIndex, const jblas::vec2& _projection);
00058 typedef std::map<int, jblas::vec2>::const_iterator ProjectionsIterator;
00059 inline std::map<int, jblas::vec2>::const_iterator projectionsBegin() const {
00060 return (this->projections).begin();
00061 };
00062 inline std::map<int, jblas::vec2>::const_iterator projectionsEnd() const {
00063 return (this->projections).end();
00064 };
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 inline size_t projectionsSize() const {return (this->projections).size();};
00075 friend class SBAObservations;
00076 };
00077
00078 class SBAObservations : public jafar::kernel::CSVFileSaveLoad {
00079 private:
00080 static int nbProjections;
00081 std::map<int, SBAObservation> observations;
00082 protected:
00083 void loadCSVFile(kernel::CSVFile& csvFile);
00084 void saveCSVFile(kernel::CSVFile& csvFile);
00085 public:
00087 SBAObservations() : CSVFileSaveLoad() { nbProjections = 0; };
00089 typedef std::map<int, SBAObservation>::const_iterator ObservationsIterator;
00091 void insertObservation(const int& obsId, const SBAObservation& obs);
00093 SBAObservation getObservation(const int& obsId);
00095 inline std::map<int, SBAObservation>::const_iterator begin(){
00096 return observations.begin();
00097 };
00099 inline std::map<int, SBAObservation>::const_iterator end(){
00100 return observations.end();
00101 };
00103 inline size_t size(){
00104 return observations.size();
00105 };
00107 inline int nbOfProjections() {
00108 return (int)nbProjections;
00109 }
00111 int maxProjectionsNumber();
00112 };
00113
00114 class SBACameras;
00115
00122 class SBACamera {
00123 private:
00125 jafar::camera::CameraPinhole camera;
00127 std::map<int, jafar::geom::T3DQuaternion> cameraMotion;
00128 public:
00130 SBACamera();
00132 SBACamera(const jafar::camera::CameraPinhole& _camera);
00134 SBACamera(const SBACamera& sbaCam);
00136 void setPinholeCamera(const jafar::camera::CameraPinhole& _camera);
00138 jafar::camera::CameraPinhole getPinholeCamera();
00140 void insertPose(const int& poseIndex, const jafar::geom::T3D& pose);
00142 jafar::geom::T3DQuaternion getPose(const int& poseIndex);
00144 inline int motionSize() const {return this->cameraMotion.size();};
00146 typedef std::map<int, geom::T3DQuaternion>::const_iterator MotionsIterator;
00148 inline std::map<int, jafar::geom::T3DQuaternion>::const_iterator motionBegin() const {
00149 return cameraMotion.begin();
00150 }
00152 inline std::map<int, jafar::geom::T3DQuaternion>::const_iterator motionEnd() const {
00153 return cameraMotion.end();
00154 }
00156 static void toTsaiNotation(const bundle::SBACamera& cam, double* jafarIntrinsics);
00158 static void toJafarNotation(bundle::SBACamera& cam, double* tsaiIntrinsics);
00160 void toVector(jblas::vec& v, const int& poseIndex);
00162 void toVector(jblas::vec& v, jafar::geom::T3DQuaternion pose);
00164 static SBACamera fromVector(jblas::vec& v, const int& poseId);
00166 static SBACamera fromVector(const jblas::vec& intrinsics,
00167 const jblas::vec& extrinsics,
00168 const int& poseId);
00170 void projectPointOnFrame(const jblas::vec3& inWorldCoordinates,
00171 const jafar::geom::T3DQuaternion& pose,
00172 jblas::vec2& projection,
00173 jblas::mat& transformJac,
00174 jblas::mat& projectionJac);
00176 void projectPointOnFrame(const jblas::vec3& inWorldCoordinates,
00177 const jafar::geom::T3DQuaternion& pose,
00178 jblas::vec2& projection);
00179 friend class SBACameras;
00180 friend std::ostream& operator<<(std::ostream& s, const jafar::bundle::SBACamera& c);
00181
00182 };
00183
00184 class SBACameras : public jafar::kernel::CSVFileSaveLoad {
00185 private:
00186 std::map<int, SBACamera> cameras;
00187 std::map<int, geom::T3DQuaternion*> poses;
00188 std::map<int, int> framesCameras;
00189 static size_t nframes;
00190 protected:
00191 void loadCSVFile(kernel::CSVFile& csvFile);
00192 void saveCSVFile(kernel::CSVFile& csvFile);
00193 public:
00194 SBACameras() : CSVFileSaveLoad() { nframes = 0; };
00196 typedef std::map<int, SBACamera>::const_iterator CamerasIterator;
00198 void insertCamera(const int& camId, const SBACamera& cam);
00200 SBACamera getCamera(const int& camId);
00202 inline std::map<int, SBACamera>::const_iterator begin(){
00203 return cameras.begin();
00204 };
00206 inline std::map<int, SBACamera>::const_iterator end(){
00207 return cameras.end();
00208 };
00210 inline size_t size(){
00211 return cameras.size();
00212 };
00214 size_t nbFrames();
00215 void insertPose(const int& poseIndex, jafar::geom::T3D* pose);
00216 jafar::geom::T3DQuaternion* getPose(const int& poseIndex);
00217
00218
00219 void setCameraIntrinsics(const int& camIndex, const jafar::camera::CameraPinhole& pCamera);
00220 std::map<int, geom::T3DQuaternion*> getPoses();
00221 };
00222
00229 class SparseBundleAdjustment {
00230 private:
00231 SBACameras cams;
00232 SBAObservations obs;
00233 std::string obsFileName;
00234 std::string camFileName;
00235 std::map<int,int> frameCameraMap;
00236 int whichCameraId(const int& frameId);
00237
00238 ublas::vector<jblas::vec> P;
00239 ublas::vector<jblas::vec> deltaP;
00240 ublas::vector<jblas::vec> PdeltaP;
00241 ublas::vector<jblas::vec> eab;
00242
00243
00244
00245
00246
00247
00249 int nbCams;
00251 int nbObs;
00253 int nbProj;
00255 int nbVars;
00257 int stop;
00259 int iteration;
00260
00261 public:
00262 static const double SBA_EPSILON;
00263 static const double SBA_EPSILON_SQ;
00264 static const double SBA_ONE_THIRD;
00265 static const double SBA_INIT_MU;
00266 static const int SBA_ITERATION_MAX;
00267
00268 SparseBundleAdjustment();
00269
00270 SparseBundleAdjustment(const SBACameras& _cams,
00271 const SBAObservations& _obs);
00272
00273 SparseBundleAdjustment(const std::string& camFileName,
00274 const std::string& obsFileName);
00275
00276 SparseBundleAdjustment(const std::string& camFileName,
00277 const std::string& calFileName,
00278 const std::string& obsFileName);
00279
00280 SparseBundleAdjustment(const std::map<int, jblas::vec3>& _trackedObjects,
00281 const jafar::premodeler::IFLists& _globalTracker,
00282 std::map<int, jafar::geom::T3D *> _toOriginReferences,
00283 const jafar::camera::CameraPinhole& pCamera);
00284
00285 void loadCameras(const std::string& camFileName);
00286
00287 void loadObservations(const std::string& obsFileName);
00288
00289 void loadCalibration(const std::string& calFileName);
00290
00291 void saveFiles(const std::string& _obsFileName,
00292 const std::string& _camFileName);
00293
00294 void bundleAll();
00295
00296 void explicitStopReason();
00297
00298
00299
00300 ublas::vector<jblas::vec> getOptimized();
00301 ublas::vector<jblas::vec3> getOptimizedPoints();
00302 int getNbVariables();
00303
00304 int getNbObseravtions();
00305
00306 int getNbProjections();
00307
00308 int getNbFrames();
00309 };
00310
00311 std::ostream& operator<<(std::ostream& s, const jafar::bundle::SBAObservation& o);
00312
00313 }
00314 }
00315 #endif