core/vpgl/vpgl_generic_camera.h
Go to the documentation of this file.
00001 // This is core/vpgl/vpgl_generic_camera.h
00002 #ifndef vpgl_generic_camera_h_
00003 #define vpgl_generic_camera_h_
00004 //:
00005 // \file
00006 // \brief The generic camera
00007 // \author J.L. Mundy
00008 // \date April 10, 2011
00009 //
00010 //   A generic form of camera projection is defined by a set of 3-d rays
00011 //   and a mapping between rays and image coordinates. This model has been
00012 //   used to unify various camera types such as fisheye, cadadioptric, etc.
00013 //   Although it is possible to encounter cameras where the rays are
00014 //   general curves, this class restricts the geometry of a ray to a 3-d line.
00015 //   In the generic camera it is not necessary that all the rays intersect
00016 //   at a single point as in the projective camera. In this implementation,
00017 //   the image-to-ray map is restricted to a 2-d array, even though in general
00018 //   the map can be more complex. It is assumed that the ray field is smooth
00019 //   so that local derivatives are defined
00020 //
00021 // \verbatim
00022 //  Modifications <none>
00023 // \endverbatim
00024 
00025 #include <vbl/vbl_array_2d.h>
00026 #include <vgl/vgl_ray_3d.h>
00027 #include <vgl/vgl_point_3d.h>
00028 #include <vpgl/vpgl_camera.h>
00029 #include <vcl_iosfwd.h>
00030 #include <vcl_string.h>
00031 
00032 template <class T>
00033 class vpgl_generic_camera : public vpgl_camera<T>
00034 {
00035  public:
00036 
00037   vpgl_generic_camera();
00038   vpgl_generic_camera( vbl_array_2d<vgl_ray_3d<T> > const& rays);
00039 
00040   virtual ~vpgl_generic_camera() {}
00041 
00042   virtual vcl_string type_name() const { return "vpgl_generic_camera"; }
00043 
00044   //: The generic camera interface. u represents image column, v image row. Finds projection using a pyramid search over the rays and so not particularly efficient.
00045   virtual void project(const T x, const T y, const T z, T& u, T& v) const;
00046 
00047   //: the number of columns (u coordinate) in the ray image
00048   unsigned cols(int level) const {return rays_[level].cols();}
00049   unsigned cols() const { return rays_[0].cols();}
00050 
00051   //: the number of rows (v coordinate) in the ray image
00052   unsigned rows(int level) const {return rays_[level].rows();}
00053   unsigned rows() const { return rays_[0].rows();}
00054 
00055   //: the number of pyramid levels
00056   unsigned n_levels() {return static_cast<unsigned>(n_levels_);}
00057 
00058   //: the ray corresponding to a given pixel
00059   vgl_ray_3d<T> ray(const T u, const T v) const;
00060 
00061   //: a ray passing through a given 3-d point
00062   vgl_ray_3d<T> ray(vgl_point_3d<T> const& p) const;
00063 
00064   //: the ray index at a given level
00065   vbl_array_2d<vgl_ray_3d<T> >& rays(int level) { return rays_[level];}
00066 
00067   //: the nearest ray origin to the coordinate origin
00068   vgl_point_3d<T> min_ray_origin() {return min_ray_origin_;}
00069   vgl_vector_3d<T> min_ray_direction() {return min_ray_direction_;}
00070 
00071   //: the furthest ray origin from the coordinate origin
00072   vgl_point_3d<T> max_ray_origin() {return max_ray_origin_;}
00073   vgl_vector_3d<T> max_ray_direction() {return max_ray_direction_;}
00074 
00075   //: debug function
00076   void print_orig(int level);
00077 
00078   //: visualization
00079   void print_to_vrml(int level, vcl_ostream& os);
00080 
00081  protected:
00082   void nearest_ray_to_point(vgl_point_3d<T> const& p,
00083                             int& nearest_r, int& nearest_c) const;
00084   //: nearest ray at level
00085   void nearest_ray(int level, vgl_point_3d<T> const& p,
00086                    int start_r, int end_r, int start_c, int end_c,
00087                    int& nearest_r, int& nearest_c) const;
00088 
00089   //: refine the projection to sub pixel
00090   void refine_projection(int nearest_c, int nearest_r,
00091                          vgl_point_3d<T> const& p, T& u, T& v) const;
00092 
00093   //: refine ray
00094   void refine_ray_at_point(int nearest_c, int nearest_r,
00095                            vgl_point_3d<T> const& p,
00096                            vgl_ray_3d<T>& ray) const;
00097 
00098   // === members ===
00099 
00100   //: ray origin bound to support occlusion reasoning
00101   vgl_point_3d<T> min_ray_origin_;
00102   vgl_vector_3d<T> min_ray_direction_;
00103   //: ray origin bound to support occlusion reasoning
00104   vgl_point_3d<T> max_ray_origin_;
00105   vgl_vector_3d<T> max_ray_direction_;
00106 
00107   //: a pyramid data structure for the rays to support efficient projection
00108   // (level == 0 is the highest resolution)
00109   int n_levels_;
00110   //: num rows at each resolution level
00111   vcl_vector<int> nr_;
00112   //: num cols at each resolution level
00113   vcl_vector<int> nc_;
00114   //: the pyramid
00115   vcl_vector<vbl_array_2d<vgl_ray_3d<T> > > rays_;
00116 };
00117 
00118 #endif // vpgl_generic_camera_h_