Follow this link to skip to the main content

a_camera_model.h

Go to the documentation of this file.
00001 // -*-c++-*- 
00002 //---------------------------< /-/ CLARAty /-/ >------------------------------
00003 /** 
00004  * @file  a_camera_model.h
00005  *
00006  * A stub for a camera model that define concrete functions for the pure
00007  * virtual methods
00008  *
00009  * <br>@b Designer(s):  Issa A.D. Nesnas
00010  * <br>@b Author(s):    Issa A.D. Nesnas
00011  * <br>@b Date:         February 26, 2007
00012  *
00013  * <b>Software License:</b><br>
00014  * <code>  http://claraty.jpl.nasa.gov/license/open_src/  or
00015  *         file: license/open_src.txt  </code>
00016  *
00017  * &copy; 2007, Jet Propulsion Laboratory, California Institute of Technology<br>
00018  *
00019  * $Revision: 1.3 $
00020  */ 
00021 //-----------------------------------------------------------------------------
00022 
00023 #ifndef A_CAMERA_MODEL_H
00024 #define A_CAMERA_MODEL_H
00025 
00026 #include "claraty/camera_model.h"
00027 #include "claraty/point.h"
00028 #include "claraty/2d_point.h"
00029 #include <sstream>
00030 #include "claraty/fdm_parse_tree.h"
00031 
00032 namespace claraty {
00033 
00034 /**
00035  * @ingroup vision_data_structure
00036  */
00037 
00038 /**
00039  * Most basic pin-hole camera model with no lens distortion
00040  *  
00041  *                     Map_Op (one-to-one)
00042  *        Camera_Model 1  -------------->  Camera_Model 2 (mapped)
00043  *       (unmapped coord)                  (mapped_coord)
00044  *
00045  */
00046 
00047 class A_Camera_Model : public Camera_Model {
00048 
00049 private:
00050   int _image_width;
00051   int _image_height;
00052 
00053 public:
00054   // @{ @name Frame constructor/destructor 
00055   A_Camera_Model() {};
00056   A_Camera_Model(int width, int height, Frame * frame) 
00057     : Camera_Model (frame),
00058       _image_width(width),
00059       _image_height(height) {}; 
00060   //@}
00061 
00062   // A_Camera_Model& operator=(const A_Camera_Model& rhs) :;
00063   Camera_Model *   clone() const  {return new A_Camera_Model(*this); }
00064   static Camera_Model * build()   {return new A_Camera_Model; }
00065 
00066   int get_image_width()  { return _image_width; }
00067   int get_image_height() { return _image_height; }
00068 
00069   void ray_to_pixel (const Ray & vector, 
00070                      Pixel_Coord & pixel_coord) const;
00071   void pixel_to_ray (const Pixel_Coord & pixel_coord,
00072                      Ray & vector) const;
00073 
00074   std::string   get_typename () const {return "A_Camera_Model";}
00075 
00076   bool map_model (Camera_Model & mapped_model,
00077                   Map_Op       & map_op) const {return false;}
00078 
00079   bool io(FDM_Map m);
00080 
00081 };
00082 
00083 //-----------------------------------------------------------------------------
00084 /**
00085  * Computes the 2D pixel location in the image from a 3D ray described
00086  * by the pointing 'vector' with respect to the model's coordinate frame
00087  */
00088 void A_Camera_Model::
00089 ray_to_pixel(const Ray & ray, 
00090              Pixel_Coord & pixel_coord) const 
00091 {
00092   pixel_coord(0) = ray(0) / ray(2);
00093   pixel_coord(1) = ray(1) / ray(2);
00094 
00095   pixel_coord += 0.01; // Adding DC offset for noise
00096 };
00097 
00098 //-----------------------------------------------------------------------------
00099 /**
00100  * Computes the 3D ray 'vector' (not necessarily a unit vector) with
00101  * respect to the camera model's reference that passes through the
00102  * given image pixel
00103  */
00104 void A_Camera_Model::
00105 pixel_to_ray(const Pixel_Coord & pixel_coord,
00106              Ray & ray) const
00107 { 
00108   float x = pixel_coord(0);
00109   float y = pixel_coord(1);
00110 
00111   float _fx = 1.0, _fy = 1.0;
00112 
00113   ray(0) = (x - _image_width/2)  / _fx;
00114   ray(1) = (y - _image_height/2) / _fy;
00115   ray(2) = 1.0;
00116 
00117   ray.normalize();
00118 };
00119   
00120 //-----------------------------------------------------------------------------
00121 
00122 bool io_object(FDM_Map map, std::auto_ptr<A_Camera_Model>& model);
00123 
00124 //-----------------------------------------------------------------------------
00125 
00126 std::istream& operator >> (std::istream& str,       A_Camera_Model & m);
00127 std::ostream& operator << (std::ostream& str, const A_Camera_Model & m);
00128 
00129 //-----------------------------------------------------------------------------
00130 /**
00131  */
00132 inline bool A_Camera_Model::
00133 io(FDM_Map m)
00134 {
00135   bool ok = Camera_Model::io(m);
00136 
00137   double dummy;
00138 
00139   if (m.is_read()) {
00140     std::string distString;
00141 
00142     ok &= m.field("distortion", distString);
00143 
00144     std::istringstream dist(distString);
00145 
00146     for (int i = 0; i < 4; i++)
00147       dist >> dummy;
00148     return ok;
00149   }
00150   ok &= m.field("width",  _image_width);
00151   ok &= m.field("height", _image_height);
00152   return ok;
00153 }
00154 //-----------------------------------------------------------------------------
00155 /**
00156  */
00157 inline
00158 std::ostream& operator<<(std::ostream& os, const A_Camera_Model& model)
00159 {
00160   FDM_Parse_Tree::write_object(model, os);
00161   return(os);
00162 } 
00163 //-----------------------------------------------------------------------------
00164 /**
00165  */
00166 inline 
00167 std::istream& operator>>(std::istream& is, A_Camera_Model& model)
00168 {
00169   FDM_Parse_Tree::read_object(model, is);
00170   return(is);
00171 }
00172 
00173 //-----------------------------------------------------------------------------
00174 
00175 //DECLARE_BUILDER_INIT(Camera_Model, A_Camera_Model, factory, A_Camera_Model::build);
00176 
00177 } // namespace claraty
00178 
00179 #endif // CAMERA_MODEL_H