contrib/rpl/rgrl/rgrl_matcher.cxx
Go to the documentation of this file.
00001 #include "rgrl_matcher.h"
00002 //:
00003 // \file
00004 #include <rgrl/rgrl_view.h>
00005 #include <rgrl/rgrl_feature.h>
00006 #include <rgrl/rgrl_match_set.h>
00007 #include <vcl_algorithm.h>
00008 
00009 // less than operator
00010 bool
00011 rgrl_matcher::flip_node::
00012 operator<( flip_node const& other ) const
00013 {
00014   return this->to_.as_pointer() < other.to_.as_pointer();
00015 }
00016 
00017 rgrl_matcher::
00018 ~rgrl_matcher()
00019 {
00020 }
00021 
00022 rgrl_match_set_sptr
00023 rgrl_matcher::
00024 compute_matches( rgrl_feature_set const&    from_features,
00025                  rgrl_feature_set const&    to_features,
00026                  rgrl_view const&           current_view,
00027                  rgrl_scale const&          current_scale,
00028                  rgrl_match_set_sptr const& old_matches )
00029 {
00030   return compute_matches( from_features, to_features, current_view,
00031                           *current_view.xform_estimate(), current_scale, old_matches );
00032 }
00033 
00034 rgrl_match_set_sptr
00035 rgrl_matcher::
00036 compute_matches( rgrl_feature_set const&    from_features,
00037                  rgrl_feature_set const&    to_features,
00038                  rgrl_transformation const& current_xform,
00039                  rgrl_mask_box const&       from_region,
00040                  rgrl_mask_box const&       to_region,
00041                  rgrl_scale const&          current_scale,
00042                  rgrl_match_set_sptr const& old_matches )
00043 {
00044   rgrl_mask_sptr from_roi = new rgrl_mask_box( from_region.x0(), from_region.x1() );
00045   rgrl_mask_sptr to_roi = new rgrl_mask_box( to_region.x0(), to_region.x1() );
00046   rgrl_view view( from_roi, to_roi, from_region, from_region, 0, 0, 0);
00047 
00048   return this->compute_matches(from_features,
00049                                to_features,
00050                                view,
00051                                current_xform,
00052                                current_scale,
00053                                old_matches);
00054 }
00055 
00056 void
00057 rgrl_matcher::
00058 add_one_flipped_match( rgrl_match_set_sptr&      inv_set,
00059                        rgrl_view          const& current_view,
00060                        nodes_vec_iterator const& begin_iter,
00061                        nodes_vec_iterator const& end_iter )
00062 {
00063   const unsigned int size = unsigned( end_iter - begin_iter );
00064   rgrl_transformation_sptr const& inverse_xform = current_view.inverse_xform_estimate();
00065 
00066   rgrl_feature_sptr from = begin_iter->to_;
00067   rgrl_feature_sptr mapped = from->transform( *inverse_xform );
00068 
00069   // setup structure
00070   vcl_vector< rgrl_feature_sptr > matching_tos;
00071   vcl_vector< double >            sig_wgts;
00072   matching_tos.reserve( size );
00073   sig_wgts.reserve( size );
00074 
00075   // copy features
00076   for ( nodes_vec_iterator itr = begin_iter; itr!=end_iter; ++itr ) {
00077     matching_tos.push_back( itr->from_ );
00078     sig_wgts.push_back( itr->sig_wgt_ );
00079   }
00080 
00081   // add matches
00082   inv_set->add_feature_matches_and_weights( from, mapped, matching_tos, sig_wgts );
00083 }
00084 
00085 // default behavior when inverting a set of matches
00086 rgrl_match_set_sptr
00087 rgrl_matcher::
00088 invert_matches( rgrl_match_set const&    current_set,
00089                 rgrl_view const&         current_view )
00090 {
00091   typedef rgrl_match_set::const_from_iterator from_iter;
00092   typedef from_iter::to_iterator        to_iter;
00093 
00094   rgrl_match_set_sptr inv_set
00095     = new rgrl_match_set( current_set.to_feature_type(),
00096                           current_set.from_feature_type(),
00097                           current_set.to_label(),
00098                           current_set.from_label() );
00099 
00100   inv_set->reserve( 3*current_set.from_size() );
00101   vcl_vector< flip_node >  matches;
00102   matches.reserve( 5*current_set.from_size() );
00103 
00104   flip_node tmp;
00105   for ( from_iter fitr = current_set.from_begin(); fitr != current_set.from_end(); ++fitr )
00106   {
00107     if ( fitr.size() == 0 )  continue;
00108 
00109     for ( to_iter titr = fitr.begin(); titr != fitr.end(); ++titr )
00110     {
00111       tmp.from_    = fitr.from_feature();
00112       tmp.to_      = titr.to_feature();
00113       tmp.sig_wgt_ = titr.signature_weight();
00114 
00115       matches.push_back( tmp );
00116       //rgrl_feature_sptr mapped = from->transform( *reverse_xform );
00117       //vcl_vector<rgrl_feature_sptr> to_vec(1, to );
00118       //vcl_vector<double>            sig_vec(1, titr.signature_weight() );
00119       //inv_set->add_feature_matches_and_weights( from, mapped, to_vec, sig_vec );
00120     }
00121   }
00122 
00123   // empty match set
00124   if ( matches.empty() )
00125     return inv_set;
00126 
00127   // sort the matches according to To feature pointer
00128   vcl_sort( matches.begin(), matches.end() );
00129 
00130   vcl_vector<flip_node>::const_iterator begin_iter, end_iter;
00131   for ( begin_iter=matches.begin(), end_iter=matches.begin()+1;
00132         end_iter!=matches.end(); ++end_iter ) {
00133     if ( end_iter->to_ == begin_iter->to_ )
00134       continue;
00135 
00136     // everything between [begin_iter, end_iter) has the same To feature pointer
00137     add_one_flipped_match( inv_set, current_view, begin_iter, end_iter );
00138     // adjust iterator
00139     begin_iter = end_iter;
00140   }
00141 
00142   // the final match
00143   add_one_flipped_match( inv_set, current_view, begin_iter, end_iter );
00144 
00145   return inv_set;
00146 }