contrib/gel/mrc/vpgl/vpgl_radial_distortion.h
Go to the documentation of this file.
00001 // This is gel/mrc/vpgl/vpgl_radial_distortion.h
00002 #ifndef vpgl_radial_distortion_h_
00003 #define vpgl_radial_distortion_h_
00004 //:
00005 // \file
00006 // \brief An abstract base class for radial lens distortions.
00007 // \author Matt Leotta
00008 // \date August 19, 2005
00009 //
00010 //   A radial lens distortion is a 2D warping of the image plane that is radial symmetric
00011 //   about some center of distortion.  It is assumed that the map is bijective,
00012 //   though a closed form solution for the inverse may not exist in general.
00013 //   A default iterative solver is implemented to solve this numerically.
00014 
00015 #include "vpgl_lens_distortion.h"
00016 #include <vgl/vgl_point_2d.h>
00017 #include <vgl/vgl_vector_2d.h>
00018 #include <vgl/vgl_homg_point_2d.h>
00019 
00020 //: A base class for radial lens distortions
00021 template <class T>
00022 class vpgl_radial_distortion : public vpgl_lens_distortion<T>
00023 {
00024  public:
00025   //: Constructor
00026   vpgl_radial_distortion(const vgl_point_2d<T>& center, bool has_deriv=false)
00027    : center_(center), distorted_center_(center), has_derivative_(has_deriv) {}
00028 
00029   //: Constructor
00030   vpgl_radial_distortion(const vgl_point_2d<T>& center,
00031                          const vgl_point_2d<T>& new_center, bool has_deriv=false)
00032    : center_(center), distorted_center_(new_center), has_derivative_(has_deriv) {}
00033 
00034   //: Distort a projected point on the image plane
00035   //  Calls the pure virtual radial distortion function
00036   virtual vgl_homg_point_2d<T> distort( const vgl_homg_point_2d<T>& point ) const;
00037 
00038   //: Return the original point that was distorted to this location (inverse of distort)
00039   // \param init is an initial guess at the solution for the iterative solver
00040   // if \p init is NULL then \p point is used as the initial guess
00041   // calls the radial undistortion function
00042   virtual vgl_homg_point_2d<T> undistort( const vgl_homg_point_2d<T>& point,
00043                                           const vgl_homg_point_2d<T>* init=0) const;
00044 
00045   //: Distort a radial length
00046   // \retval a scale factor such that
00047   // \code
00048   //   distort_pt = center + distort_radius(radius)*(pt - center)
00049   // \endcode
00050   virtual T distort_radius( T radius ) const = 0;
00051 
00052   //: Return the inverse of distort function
00053   // \param init is an initial guess at the solution for the iterative solver
00054   // if \p init is NULL then \p radius is used as the initial guess
00055   virtual T undistort_radius( T radius, const T* init=0) const;
00056 
00057   //: Compute the derivative of the distort_radius function
00058   // \note implementing this function is optional but it may improve the convergence
00059   //       rate of the undistort function if iterative solving is used
00060   // Set \p has_derivative_ to true if you define this function
00061   virtual T distort_radius_deriv( T radius ) const
00062   {
00063     T eps = T(0.001);
00064     return (distort_radius(radius) - distort_radius(radius-eps)) / eps;
00065   }
00066 
00067   //: Set a translation to apply before of after distortion
00068   // This is needed when distorting an image to translate the resulting image
00069   // such that all points have positive indices
00070   virtual void set_translation(const vgl_vector_2d<T>& offset, bool after = true)
00071   {
00072     if (after)
00073       distorted_center_ += offset;
00074     else
00075       center_ += offset;
00076   }
00077 
00078   //: Returns the center of distortion
00079   vgl_point_2d<T> center() const { return center_; }
00080   //: Returns the center of distortion in the distorted image
00081   vgl_point_2d<T> distorted_center() const { return distorted_center_; }
00082 
00083   //: Set the center of distortion
00084   void set_center(const vgl_point_2d<T>& c) { center_ = c; }
00085   //: Set the center of distortion in the distorted image
00086   void set_distorted_center(const vgl_point_2d<T>& dc) { distorted_center_ = dc; }
00087 
00088  protected:
00089   //: The center of distortion
00090   vgl_point_2d<T> center_;
00091 
00092   //: The center of distortion in the distorted space
00093   vgl_point_2d<T> distorted_center_;
00094 
00095   bool has_derivative_;
00096 };
00097 
00098 #endif // vpgl_radial_distortion_h_