contrib/rpl/rgrl/rgrl_estimator.cxx
Go to the documentation of this file.
00001 #include "rgrl_estimator.h"
00002 //:
00003 // \file
00004 // \author Amitha Perera
00005 // \date   Feb 2003
00006 
00007 #include <rgrl/rgrl_set_of.h>
00008 #include <rgrl/rgrl_match_set_sptr.h>
00009 #include <rgrl/rgrl_match_set.h>
00010 
00011 rgrl_estimator::
00012 rgrl_estimator( unsigned int param_dof )
00013   : dof_( param_dof )
00014 {
00015 }
00016 
00017 rgrl_estimator::
00018 rgrl_estimator()
00019   : dof_( 0 )
00020 {
00021 }
00022 
00023 rgrl_estimator::
00024 ~rgrl_estimator()
00025 {
00026 }
00027 
00028 
00029 rgrl_transformation_sptr
00030 rgrl_estimator::
00031 estimate( rgrl_match_set_sptr matches,
00032           rgrl_transformation const& cur_transform ) const
00033 {
00034   rgrl_set_of<rgrl_match_set_sptr> set;
00035   set.push_back( matches );
00036   return estimate( set, cur_transform );
00037 }
00038 
00039 //: Determine the weighted centres of the From and To points
00040 //
00041 bool
00042 rgrl_est_compute_weighted_centres( rgrl_set_of<rgrl_match_set_sptr> const& matches,
00043                                    vnl_vector<double>& from_centre,
00044                                    vnl_vector<double>& to_centre )
00045 {
00046   typedef rgrl_match_set::const_from_iterator FIter;
00047   typedef FIter::to_iterator TIter;
00048 
00049   vnl_vector<double> from_pt;
00050   vnl_vector<double> to_pt;
00051   double sum_wgt = 0.0;
00052 
00053   // initialize centres to zero based on point dimension
00054   bool need_init = true;
00055   for ( unsigned ms=0; ms < matches.size() && need_init; ++ms ) {
00056 
00057     rgrl_match_set const& match_set = *matches[ms];
00058     for ( FIter fi = match_set.from_begin(); fi != match_set.from_end() && need_init; ++fi )
00059       for ( TIter ti = fi.begin(); ti != fi.end() && need_init ; ++ti ) {
00060 
00061         from_centre.set_size( fi.from_feature()->dim() );
00062         from_centre.fill( double(0) );
00063 
00064         to_centre.set_size( ti.to_feature()->dim() );
00065         to_centre.fill( double(0) );
00066 
00067         //done
00068         need_init = false;
00069       }
00070   }
00071 
00072   // sum of weighted point location
00073   for ( unsigned ms=0; ms < matches.size(); ++ms ) {
00074 
00075     rgrl_match_set const& match_set = *matches[ms];
00076     for ( FIter fi = match_set.from_begin(); fi != match_set.from_end(); ++fi )
00077       for ( TIter ti = fi.begin(); ti != fi.end(); ++ti ) {
00078         double const wgt = ti.cumulative_weight();
00079         from_pt = fi.from_feature()->location();
00080         from_pt *= wgt;
00081         from_centre += from_pt;
00082         to_pt = ti.to_feature()->location();
00083         to_pt *= wgt;
00084         to_centre += to_pt;
00085         sum_wgt += wgt;
00086       }
00087   }
00088 
00089   // if the weight is too small or zero,
00090   // that means there is no good match
00091   if ( sum_wgt < 1e-8 ) {
00092     vcl_cerr << "Sum of weights is too small for centre computation.\n";
00093     return false;
00094   }
00095 
00096   from_centre /= sum_wgt;
00097   to_centre /= sum_wgt;
00098 
00099   return true;
00100 }
00101 
00102 unsigned
00103 rgrl_est_matches_residual_number(rgrl_set_of<rgrl_match_set_sptr> const& matches)
00104 {
00105   // count the number of constraints/residuals
00106   typedef rgrl_match_set::const_from_iterator FIter;
00107   typedef FIter::to_iterator TIter;
00108   unsigned int tot_num = 0;
00109   for ( unsigned ms = 0; ms<matches.size(); ++ms )
00110     if ( matches[ms] ) { // if pointer is valid
00111 
00112       rgrl_match_set const& one_set = *(matches[ms]);
00113       for ( FIter fi=one_set.from_begin(); fi!=one_set.from_end(); ++fi )
00114         if ( fi.size() ) {
00115           tot_num += fi.size() * fi.begin().to_feature()->dim();  // each point provides two constraints
00116         }
00117     }
00118 
00119   return tot_num;
00120 }
00121