00001
00002
00003 #ifndef _BUNDLER_ADAPTER_HPP
00004 #define _BUNDLER_ADAPTER_HPP
00005
00006 #include "kernel/jafarException.hpp"
00007 #include "bundler/viewsManager.hpp"
00008 #include "bundler/bundlerException.hpp"
00009 #include "geom/Declarations.hpp"
00010 #include "geom/Point.hpp"
00011 #include "premodeler/ifManager.hpp"
00012 #include "camera/cameraContainer.hpp"
00013 #include "geom/t3d.hpp"
00014 #include <set>
00015 namespace jafar {
00016
00017 namespace bundler {
00018 using namespace std;
00019 using namespace jafar;
00020 using namespace bundler;
00021 using namespace camera;
00022 using namespace geom;
00023
00024 template<class TRACKERS>
00032 class Adapter {
00033
00034 protected :
00035 ViewsManager* vManager;
00036 TRACKERS m_trackers;
00037 struct Point3DCompare{
00038 bool operator() (const geom::Point3D& lhs, const geom::Point3D& rhs){
00039 return lhs.id() < rhs.id();
00040 }
00041 };
00042 std::set<geom::Point3D, Point3DCompare> objects;
00043 std::map<int, jblas::vec3> points;
00044 std::map<int, double> errors;
00045 double mean_error;
00046
00047 public :
00051 Adapter()
00052 {
00053 vManager = new ViewsManager();
00054 };
00060 Adapter(const camera::CameraType& camType,
00061 const std::string& camCalibFile)
00062 {
00063 vManager = new ViewsManager(camCalibFile,
00064 camType);
00065 };
00072 Adapter(const camera::CameraType& camType,
00073 const std::string& camCalibFile,
00074 const TRACKERS& trackers) : m_trackers(trackers)
00075 {
00076 vManager = new ViewsManager(camCalibFile,
00077 camType);
00078 };
00086 Adapter(const jafar::camera::CameraType& camType,
00087 const std::string& camCalibFile,
00088 const std::map<int, geom::T3D*>& toOriginFrames,
00089 const TRACKERS& trackers) : m_trackers(trackers)
00090 {
00091 vManager = new ViewsManager(camCalibFile,
00092 camType,
00093 toOriginFrames);
00094 }
00096 virtual ~Adapter()
00097 {
00098 delete vManager;
00099 }
00101 void setCamera(const camera::CameraType& camType,
00102 const std::string& camCalibFile) {
00103 vManager->setCamera(camType, camCalibFile);
00104 }
00106 void setTrackers(const TRACKERS& trackers) {
00107 m_trackers = trackers;
00108 }
00110 inline void setReferences(const std::map<int, geom::T3D*>& toOriginFrames) {
00111 vManager->setToOrigineReferences(toOriginFrames);
00112 }
00114 inline void insertReference(const int& index,
00115 T3D* sensorToOrigin)
00116 {
00117 vManager->insertToOriginReference(index, sensorToOrigin);
00118 }
00120 void computeMeanReprojectionError()
00121 {
00122 cout << ">>> compute mean reprojection errors"<<endl;
00123 mean_error=0;
00124 std::map<int, double>::iterator it;
00125 for (it = errors.begin();
00126 it != errors.end();
00127 ++it) {
00128 mean_error+=it->second;
00129 }
00130 mean_error/=errors.size();
00131 cout << "<<< compute mean reprojection errors "<<mean_error<<endl;
00132 }
00134 inline double meanReprojectionError()
00135 {
00136 return mean_error;
00137 }
00143 void removeOutliers(const double& k = 1.5)
00144 {
00145 cout << ">>> remove outliers"<<endl;
00146 JFR_PRED_ERROR(k>=1.0,
00147 BundlerException,
00148 BundlerException::ADAPTER_INVALID_CONSTRAINT,
00149 "outliers control constrant must be >=1.0");
00150 computeMeanReprojectionError();
00151 int counter=0;
00152 std::map<int, Point3D>::iterator point;
00153 std::map<int, double>::iterator error;
00154 for (point = objects.begin();
00155 point != objects.end();
00156 ++point) {
00157 error = errors.find(point->first);
00158 if (error->second > k * mean_error){
00159 objects.erase(point);
00160 ++counter;
00161 }
00162 }
00163 cout << "<<< remove outliers "<<counter<<" points removed"<<endl;
00164 }
00166 inline std::set<geom::Point3D, Point3DCompare> trackedObjects()
00167 {
00168 return objects;
00169 }
00171 inline std::map<int, jblas::vec3> trackedPoints()
00172 {
00173 return points;
00174 }
00176 void writeToFile(const string& fullFileName)
00177 {
00178 ofstream file;
00179 file.open(fullFileName.c_str(), ios::out);
00180 if(!file.bad()){
00181 file << trackedObjects.size() <<endl;
00182 std::map<int, Point3D>::iterator it;
00183 for (it = objects.begin();
00184 it != objects.end();
00185 it++) {
00186 file << (it->second).toString() <<endl;
00187 }
00188 } else {
00189
00190
00191 JFR_ERROR(BundlerException,
00192 BundlerException::FILESYSTEM_ERROR,
00193 "can not open " + fullFileName + " in write mode!");
00194 }
00195 file.close();
00196 }
00198 inline ViewsManager* viewsManager() const
00199 {
00200 return vManager;
00201 }
00203 inline std::vector<jafar::bundler::View*> viewsList() const
00204 {
00205 return vManager->viewsList();
00206 }
00207
00208 protected:
00213 virtual void process() {};
00217 virtual void processTracker() {};
00221 virtual void loadTrackerViews() {};
00225 void addTrackedObject(const geom::Point3D& p)
00226 {
00227 objects.insert(p);
00228 }
00232 void addTrackedObject(const geom::Point3D& p,
00233 const double& e)
00234 {
00235 objects.insert(p);
00236 errors[p.id()] = e;
00237 }
00241 void addTrackedPoint(const int& id,
00242 const jblas::vec3& p)
00243 {
00244 points[id] = p;
00245 }
00250 void addTrackedPoint(const int& id,
00251 const jblas::vec3& p,
00252 const double& e)
00253 {
00254 points[id] = p;
00255 errors[id] = e;
00256 }
00257
00258 };
00259 }
00260 }
00261 #endif