contrib/rpl/rgrl/rgrl_estimator.h
Go to the documentation of this file.
00001 #ifndef rgrl_estimator_h_
00002 #define rgrl_estimator_h_
00003 //:
00004 // \file
00005 // \author Amitha Perera
00006 // \date   Feb 2003
00007 //
00008 // \verbatim
00009 //  Modifications:
00010 //   2003-09 Charlene Tsai. Added the functions to support random sampling.
00011 // \endverbatim
00012 //
00013 
00014 #include <vcl_cassert.h>
00015 
00016 #include "rgrl_set_of.h"
00017 #include "rgrl_match_set_sptr.h"
00018 #include "rgrl_transformation.h"
00019 #include "rgrl_object.h"
00020 
00021 #include "rgrl_estimator_sptr.h"
00022 
00023 //: Interface for the transform estimators
00024 //
00025 // An estimator creates a transform object from a set of matches.
00026 //
00027 class rgrl_estimator
00028   : public rgrl_object
00029 {
00030  public:
00031   //: Default constructor
00032   //
00033   //  Does nothing.
00034   rgrl_estimator();
00035 
00036   //: Constructor.
00037   //
00038   // See the comments for param_dof(). The parameter is required by
00039   // some algorithms such as random sampling and DBICP.
00040   rgrl_estimator( unsigned int param_dof );
00041 
00042   virtual
00043   ~rgrl_estimator();
00044 
00045   //: whether this method is iterative or non-iterative
00046   virtual
00047   bool
00048   is_iterative_method() const = 0;
00049 
00050 
00051   //: Estimate the transform
00052   //
00053   // Given a collection of match sets in \a matches and the current
00054   // transform estimate (from the previous iteration, for example),
00055   // this function will estimate the transformation parameters and
00056   // return a transform object that captures the estimated transform.
00057   //
00058   virtual
00059   rgrl_transformation_sptr
00060   estimate( rgrl_set_of<rgrl_match_set_sptr> const& matches,
00061             rgrl_transformation const& cur_transform ) const = 0;
00062 
00063 
00064   //: Estimate the transform
00065   //
00066   // Given a set of matches in \a matches and the current transform
00067   // estimate (from the previous iteration, for example), this
00068   // function will estimate the transformation parameters and return a
00069   // transform object that captures the estimated transform.
00070   //
00071   // The default implementation in the abstract base class will simply
00072   // construct a size one collection of match sets and call the other
00073   // estimate function.
00074   //
00075   virtual
00076   rgrl_transformation_sptr
00077   estimate( rgrl_match_set_sptr matches,
00078             rgrl_transformation const& cur_transform ) const;
00079 
00080   //: The degrees of freedom in the parameter set.
00081   unsigned int param_dof() const { assert (dof_); return dof_; }
00082 
00083   //: Set the degrees of freedom
00084   void set_param_dof(unsigned int dof) { dof_ = dof; }
00085 
00086   //: The degrees of freedom in the residual.
00087   //
00088   //  Most of the time, this would be 1 since the residual comes from
00089   //  a single random variable. In some problems, however, the error
00090   //  is the combination of more than one random variable. (For
00091   //  example, if the residual is a 2d Euclidean distance with
00092   //  possible error in both coordinates, the degrees of freedom in
00093   //  the error will be 2.)
00094   virtual unsigned int residual_dof() const { return 1; }
00095 
00096   //: Type of transformation estimated by this estimator.
00097   virtual
00098   const vcl_type_info& transformation_type() const = 0;
00099 
00100   //: Name of transformation estimated by this estimator.
00101   //  It is more useful when a transformation/estimator pair
00102   //  is capable of storing/estimating several models,
00103   //  usually differing in dof
00104   virtual
00105   const vcl_string transformation_name() const
00106   { return this->transformation_type().name(); }
00107 
00108   // Defines type-related functions
00109   rgrl_type_macro( rgrl_estimator, rgrl_object );
00110 
00111  protected:
00112   //: Determine the weighted centres of the From and To points
00113   //
00114 
00115  private:
00116   unsigned int dof_;
00117 };
00118 
00119 // ===================================================================
00120 //
00121 //: Interface for linear transform estimators
00122 //
00123 // An estimator creates a transform object from a set of matches.
00124 //
00125 class rgrl_linear_estimator
00126   : public rgrl_estimator
00127 {
00128  public:
00129   //: Default constructor
00130   //
00131   //  Does nothing.
00132   rgrl_linear_estimator()
00133    : rgrl_estimator()
00134   { }
00135 
00136   //: Constructor.
00137   //
00138   // See the comments for param_dof(). The parameter is required by
00139   // some algorithms such as random sampling and DBICP.
00140   rgrl_linear_estimator( unsigned int param_dof )
00141    : rgrl_estimator( param_dof )
00142   { }
00143 
00144   virtual
00145   ~rgrl_linear_estimator()
00146   { }
00147 
00148   //: Linear estimator is non-iterative
00149   //
00150   virtual
00151   bool
00152   is_iterative_method() const
00153   { return false; }
00154 };
00155 
00156 // ===================================================================
00157 //
00158 //: Interface for non-linear transform estimators
00159 //
00160 // An estimator creates a transform object from a set of matches.
00161 //
00162 class rgrl_nonlinear_estimator
00163   : public rgrl_estimator
00164 {
00165  public:
00166   //: Default constructor
00167   //
00168   //  Does nothing.
00169   rgrl_nonlinear_estimator()
00170    : rgrl_estimator(),
00171      max_num_iterations_(0),
00172      relative_threshold_(1e-8)
00173   { }
00174 
00175   //: Constructor.
00176   //
00177   // See the comments for param_dof(). The parameter is required by
00178   // some algorithms such as random sampling and DBICP.
00179   rgrl_nonlinear_estimator( unsigned int /*param_dof*/ )
00180    : rgrl_estimator(),
00181      max_num_iterations_(0),
00182      relative_threshold_(1e-8)
00183   { }
00184 
00185   virtual
00186   ~rgrl_nonlinear_estimator()
00187   { }
00188 
00189   //: Linear estimator is non-iterative
00190   //
00191   virtual
00192   bool
00193   is_iterative_method() const
00194   { return true; }
00195 
00196   //: set max number of iterations
00197   void set_max_num_iter( int max )
00198   { max_num_iterations_ = max; }
00199 
00200   //: return max number of iterations
00201   int max_num_iter() const
00202   { return max_num_iterations_; }
00203 
00204   //: set relative threshold for parameters change
00205   void set_rel_thres( double thres )
00206   { relative_threshold_ = thres; }
00207 
00208   //: relative threshold
00209   double rel_thres() const
00210   { return relative_threshold_; }
00211 
00212  protected:
00213   //: specify the maximum number of iterations for this estimator
00214   int max_num_iterations_;
00215 
00216   //: The threshold for relative parameter change before termination
00217   double relative_threshold_;
00218 };
00219 
00220 bool
00221 rgrl_est_compute_weighted_centres( rgrl_set_of<rgrl_match_set_sptr> const& matches,
00222                                    vnl_vector<double>& from_centre,
00223                                    vnl_vector<double>& to_centre );
00224 unsigned
00225 rgrl_est_matches_residual_number(rgrl_set_of<rgrl_match_set_sptr> const& matches);
00226 
00227 
00228 #endif // rgrl_estimator_h_