Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Camera.h
Go to the documentation of this file.
00001 /********************************************************************
00002 **                Image Component Library (ICL)                    **
00003 **                                                                 **
00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld          **
00005 **                         Neuroinformatics Group                  **
00006 ** Website: www.iclcv.org and                                      **
00007 **          http://opensource.cit-ec.de/projects/icl               **
00008 **                                                                 **
00009 ** File   : ICLGeom/src/ICLGeom/Camera.h                           **
00010 ** Module : ICLGeom                                                **
00011 ** Authors: Erik Weitnauer, Christof Elbrechter                    **
00012 **                                                                 **
00013 **                                                                 **
00014 ** GNU LESSER GENERAL PUBLIC LICENSE                               **
00015 ** This file may be used under the terms of the GNU Lesser General **
00016 ** Public License version 3.0 as published by the                  **
00017 **                                                                 **
00018 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
00019 ** included in the packaging of this file.  Please review the      **
00020 ** following information to ensure the license requirements will   **
00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt                **
00022 **                                                                 **
00023 ** The development of this software was supported by the           **
00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology.    **
00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche       **
00026 ** Forschungsgemeinschaft (DFG) in the context of the German       **
00027 ** Excellence Initiative.                                          **
00028 **                                                                 **
00029 ********************************************************************/
00030 
00031 #pragma once
00032 
00033 #include <ICLUtils/CompatMacros.h>
00034 #include <ICLUtils/Size.h>
00035 #include <ICLUtils/Point32f.h>
00036 #include <ICLUtils/Rect.h>
00037 #include <ICLUtils/Rect32f.h>
00038 #include <ICLUtils/Exception.h>
00039 #include <ICLUtils/Array2D.h>
00040 #include <ICLGeom/PlaneEquation.h>
00041 #include <ICLGeom/ViewRay.h>
00042 
00043 
00044 // the icl namespace
00045 namespace icl {
00046   namespace geom{
00048 
00132     class ICLGeom_API Camera {
00133       public:
00134   
00135       struct ICLGeom_API RenderParams {
00136         utils::Size chipSize;    
00137         float clipZNear;  
00138         float clipZFar;   
00139         utils::Rect viewport;    
00140         float viewportZMin, viewportZMax; 
00141   
00142         RenderParams(const utils::Size &chipSize=utils::Size::VGA, float clipZNear=1.0, float clipZFar=10000.,
00143           const utils::Rect &viewport=utils::Rect(0,0,640,480), float viewportZMin=0.,
00144           float viewportZMax=1.):
00145           chipSize(chipSize), clipZNear(clipZNear), clipZFar(clipZFar), viewport(viewport),
00146           viewportZMin(viewportZMin), viewportZMax(viewportZMax) {}
00147       };
00148   
00150       struct NotEnoughDataPointsException  : public utils::ICLException{
00151         NotEnoughDataPointsException():utils::ICLException(__FUNCTION__){}
00152       };
00153   
00155       typedef math::FixedMatrix<icl32f,3,3> Mat3x3;
00156   
00159 
00160       Camera(const Vec &pos=Vec(0,0,10,1),
00161                const Vec &norm=Vec(0,0,-1,1),
00162                const Vec &up=Vec(1,0,0,1),
00163                float f=3,
00164                const utils::Point32f &principalPointOffset=utils::Point32f(320,240),
00165                float sampling_res_x = 200,
00166                float sampling_res_y = 200,
00167                float skew = 0,
00168                const RenderParams &renderParams = RenderParams()): m_pos(pos), m_norm(norm), m_up(up), m_f(f),
00169              m_px(principalPointOffset.x), m_py(principalPointOffset.y),
00170              m_mx(sampling_res_x), m_my(sampling_res_y), m_skew(skew), m_renderParams(renderParams) {}
00171   
00173 
00177       Camera(const std::string &filename, const std::string &prefix="config.")  throw (utils::ParseException);
00179 
00183       Camera(std::istream &configDataStream, const std::string &prefix="config.")  throw (utils::ParseException);
00184   
00185   
00187 
00188       static Camera createFromProjectionMatrix(const math::FixedMatrix<icl32f,4,3> &Q, float focalLength=1);
00189   
00191 
00199       static Camera calibrate(std::vector<Vec> Xws, std::vector<utils::Point32f> xis, float focalLength=1)
00200       throw (NotEnoughDataPointsException,math::SingularMatrixException);
00201   
00203 
00205       static Camera calibrate_pinv(std::vector<Vec> Xws, std::vector<utils::Point32f> xis, float focalLength=1)
00206       throw (NotEnoughDataPointsException,math::SingularMatrixException);
00207   
00210       // projections normal
00212       utils::Point32f project(const Vec &Xw) const;
00214       void project(const std::vector<Vec> &Xws, std::vector<utils::Point32f> &dst) const;
00216       const std::vector<utils::Point32f> project(const std::vector<Vec> &Xws) const;
00217   
00218   
00219   
00220       // projections OpenGL
00222       Vec projectGL(const Vec &Xw) const;
00224       void projectGL(const std::vector<Vec> &Xws, std::vector<Vec> &dst) const;
00226       const std::vector<Vec> projectGL(const std::vector<Vec> &Xws) const;
00227   
00228   
00229       // projection magic
00231       ViewRay getViewRay(const utils::Point32f &pixel) const;
00232   
00234 
00236       std::vector<ViewRay> getViewRays(const std::vector<utils::Point32f> &pixels) const;
00237   
00239 
00241       utils::Array2D<ViewRay> getAllViewRays() const;
00242       
00244       ViewRay getViewRay(const Vec &Xw) const;
00246       Vec estimate3DPosition(const utils::Point32f &pixel, const PlaneEquation &plane) const throw (utils::ICLException);
00248 
00257       static Vec getIntersection(const ViewRay &v,
00258                                  const PlaneEquation &plane) throw (utils::ICLException);
00259   
00262       // setters and getters
00264       void setRotation(const Mat3x3 &rot);
00266       void setRotation(const Vec &rot);
00267   
00269 
00273       void setTransformation(const Mat &m);
00274 
00276 
00278       void setWorldTransformation(const Mat &m);
00279       
00280       
00282 
00284       void setWorldFrame(const Mat &m);
00285 
00287       Mat getCSTransformationMatrix() const;
00288       
00290       Mat getInvCSTransformationMatrix() const;
00291       
00293       Mat getCSTransformationMatrixGL() const;
00295       Mat getProjectionMatrix() const;
00297       Mat getProjectionMatrixGL() const;
00298       Mat getViewportMatrixGL() const;
00299   
00301       math::FixedMatrix<icl32f,4,3> getQMatrix() const;
00302       
00304       math::FixedMatrix<icl32f,3,4> getInvQMatrix() const;
00305   
00307       inline void translate(const Vec &d) { m_pos += d; }
00308   
00309       std::string toString() const;
00310   
00313       const std::string &getName() const { return m_name; }
00314       const Vec &getPosition() const { return m_pos; }
00315       const Vec &getNorm() const { return m_norm; }
00316       const Vec &getUp() const { return m_up; }
00317       Vec getHoriz() const { return cross(m_up, m_norm); }
00318       float getFocalLength() const { return m_f; }
00319       utils::Point32f getPrincipalPointOffset() const { return utils::Point32f(m_px, m_py); }
00320       float getPrincipalPointOffsetX() const { return m_px; }
00321       float getPrincipalPointOffsetY() const { return m_py; }
00322       float getSamplingResolutionX() const { return m_mx; }
00323       float getSamplingResolutionY() const { return m_my; }
00324       float getSkew() const { return m_skew; }
00325       const RenderParams &getRenderParams() const { return m_renderParams; }
00326       RenderParams &getRenderParams() { return m_renderParams; }
00327   
00330       void setName(const std::string &name) { m_name = name; }
00331       void setPosition(const Vec &pos) { m_pos = pos; }
00332       void setNorm(const Vec &norm) { m_norm = norm; m_norm[3] = 0; m_norm.normalize(); m_norm[3] = 1; } 
00333       void setUp(const Vec &up) { m_up = up; m_up[3] = 0; m_up.normalize(); m_up[3] = 1; } 
00334       void setFocalLength(float value) { m_f = value; }
00335       void setPrincipalPointOffset(float px, float py) { m_px = px; m_py = py; }
00336       void setPrincipalPointOffset(const utils::Point32f &p) { m_px = p.x; m_py = p.y; }
00337       void setSamplingResolutionX(float value) { m_mx = value; }
00338       void setSamplingResolutionY(float value) { m_my = value; }
00339       void setSamplingResolution(float x, float y) { m_mx = x; m_my = y; }
00340       void setSkew(float value) { m_skew = value; }
00341       void setRenderParams(const RenderParams &rp) { m_renderParams = rp; }
00342   
00343   
00346 
00347 
00351       void setResolution(const utils::Size &newScreenSize);
00352   
00354 
00357       void setResolution(const utils::Size &newScreenSize, const utils::Point &newPrincipalPointOffset);
00358   
00360       inline const utils::Size &getResolution() const { return m_renderParams.chipSize; }
00363 
00364 
00442       static Vec estimate_3D(const std::vector<Camera*> cams,
00443                              const std::vector<utils::Point32f> &UVs,
00444                              bool removeInvalidPoints=false) throw (utils::ICLException);
00445   
00447 
00449       static Vec estimate_3D_svd(const std::vector<Camera*> cams,
00450                                  const std::vector<utils::Point32f> &UVs);
00451   
00454       protected:
00455       static Mat createTransformationMatrix(const Vec &norm, const Vec &up, const Vec &pos);
00456   
00457       private:
00458       // General Parameters
00459       std::string m_name;   
00460   
00461       // External Parameters
00462       Vec m_pos;        
00463       Vec m_norm;       
00464       Vec m_up;         
00465   
00466       // Internal Parameters
00467       float m_f;        
00468       float m_px, m_py; 
00469       float m_mx, m_my; 
00470       float m_skew;     
00471   
00472       // Rendering Parameters
00473       RenderParams m_renderParams;
00474   
00477   
00479       static void checkAndFixPoints(std::vector<Vec> &worldPoints, std::vector<utils::Point32f> &imagePoints)
00480         throw (NotEnoughDataPointsException);
00482       static void load_camera_from_stream(std::istream &is,
00483                                           const std::string &prefix,
00484                                           Camera &cam);
00485     };
00486   
00488     ICLGeom_API std::ostream &operator<<(std::ostream &os, const Camera &cam);
00489   
00491     ICLGeom_API std::istream &operator>>(std::istream &is, Camera &cam) throw (utils::ParseException);
00492   
00493   } // namespace geom
00494 } // namespace icl
00495 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines