contrib/rpl/rgrl/rgrl_convergence_on_median_error.cxx
Go to the documentation of this file.
00001 #include "rgrl_convergence_on_median_error.h"
00002 
00003 #include <vcl_vector.h>
00004 #include <vcl_algorithm.h>
00005 #include <vcl_cassert.h>
00006 
00007 #include "rgrl_match_set.h"
00008 #include "rgrl_set_of.h"
00009 #include "rgrl_converge_status.h"
00010 #include "rgrl_view.h"
00011 #include "rgrl_util.h"
00012 
00013 rgrl_convergence_on_median_error::
00014 rgrl_convergence_on_median_error( double tol )
00015   : tolerance_( tol )
00016 {
00017 }
00018 
00019 
00020 rgrl_converge_status_sptr
00021 rgrl_convergence_on_median_error::
00022 compute_status( rgrl_converge_status_sptr               prev_status,
00023                 rgrl_view                        const& prev_view,
00024                 rgrl_view                        const& current_view,
00025                 rgrl_set_of<rgrl_match_set_sptr> const& current_match_sets,
00026                 rgrl_set_of<rgrl_scale_sptr>     const& /*current_scales*/,
00027                 bool                                    penalize_scaling) const
00028 {
00029   typedef rgrl_match_set::const_from_iterator from_iter;
00030   typedef from_iter::to_iterator              to_iter;
00031 
00032   // Step1: Compute the errors of all matches
00033   //
00034   //rgrl_transformation_sptr current_xform = current_view.xform_estimate();
00035   vcl_vector<double> errors;
00036 
00037   for ( unsigned ds=0; ds < current_match_sets.size(); ++ds ) {
00038     rgrl_match_set const& ms = *current_match_sets[ds];
00039     for ( from_iter fitr = ms.from_begin(); fitr != ms.from_end(); ++fitr ) {
00040       //rgrl_feature_sptr mapped = fitr.from_feature()->transform( *current_xform );
00041       rgrl_feature_sptr mapped = fitr.mapped_from_feature();
00042       for ( to_iter titr = fitr.begin(); titr != fitr.end(); ++titr ) {
00043         errors.push_back( titr.to_feature()->geometric_error( *mapped ) );
00044       }
00045     }
00046   }
00047 
00048   // Step2: Take the median of the sorted error vector as the
00049   //        objective value.  To avoid the low error given by an infeasible
00050   //        transformation, the error is scaled by the scaling of the spread
00051   //        of the transformed data points
00052   //
00053   assert ( errors.size() > 0 );
00054   vcl_vector<double>::iterator middle = errors.begin() + errors.size()/2;
00055   vcl_nth_element( errors.begin(), middle, errors.end() );
00056 
00057   double scaling = 1;
00058   if ( penalize_scaling ) {
00059     scaling =  rgrl_util_geometric_error_scaling( current_match_sets );
00060     DebugMacro(1, "geometric_error_scaling = "<<scaling<<'\n');
00061   }
00062 
00063   double new_error = *middle * scaling;
00064   bool good = new_error < tolerance_;
00065 
00066   return compute_status_helper( new_error, good, prev_status, prev_view, current_view );
00067 }