contrib/rpl/rgrl/rgrl_data_manager.h
Go to the documentation of this file.
00001 #ifndef rgrl_data_manager_h
00002 #define rgrl_data_manager_h
00003 //:
00004 // \file
00005 // \brief Class to represent the collection of feature sets available for use by the registration engine.
00006 // \author Charlene Tsai
00007 // \date   Feb 2004
00008 
00009 #include <rgrl/rgrl_estimator_sptr.h>
00010 #include <rgrl/rgrl_feature_set_sptr.h>
00011 #include <rgrl/rgrl_matcher_sptr.h>
00012 #include <rgrl/rgrl_scale_estimator_sptr.h>
00013 #include <rgrl/rgrl_weighter_sptr.h>
00014 #include <rgrl/rgrl_estimator.h>
00015 #include <rgrl/rgrl_feature_set.h>
00016 #include <rgrl/rgrl_matcher.h>
00017 #include <rgrl/rgrl_scale_estimator.h>
00018 #include <rgrl/rgrl_weighter.h>
00019 #include <rgrl/rgrl_object.h>
00020 
00021 #include <vcl_vector.h>
00022 #include <vcl_map.h>
00023 #include <vcl_string.h>
00024 
00025 // ---------------------------------------------------------------------------
00026 //                                                                   data item
00027 //
00028 
00029 //: An object to store the moving_feature_set, its matching fixed_feature_set, and other components
00030 //  (that work on the two feature sets during registration).
00031 //
00032 class rgrl_data_manager_data_item
00033 {
00034  public:
00035   rgrl_data_manager_data_item( rgrl_feature_set_sptr              in_from_set,
00036                                rgrl_feature_set_sptr              in_to_set,
00037                                rgrl_matcher_sptr                  in_matcher,
00038                                rgrl_weighter_sptr                 in_weighter,
00039                                rgrl_scale_estimator_unwgted_sptr  in_unwgted_scale_est,
00040                                rgrl_scale_estimator_wgted_sptr    in_wgted_scale_est,
00041                                const vcl_string&                  in_label = vcl_string() )
00042     : from_set( in_from_set ),
00043       to_set( in_to_set ),
00044       matcher( in_matcher ),
00045       unwgted_scale_est( in_unwgted_scale_est ),
00046       wgted_scale_est( in_wgted_scale_est ),
00047       weighter( in_weighter ),
00048       label( in_label )
00049     {
00050     }
00051 
00052   rgrl_feature_set_sptr             from_set;
00053   rgrl_feature_set_sptr             to_set;
00054   rgrl_matcher_sptr                 matcher;
00055   rgrl_scale_estimator_unwgted_sptr unwgted_scale_est;
00056   rgrl_scale_estimator_wgted_sptr   wgted_scale_est;
00057   rgrl_weighter_sptr                weighter;
00058   vcl_string                        label;
00059 };
00060 
00061 // ---------------------------------------------------------------------------
00062 //                                                                data storage
00063 //
00064 
00065 //: A collection of data in stages, where each stage can store multiple data items and multiple estimators.
00066 //
00067 class rgrl_data_manager_data_storage
00068 {
00069  public:
00070   typedef vcl_vector< rgrl_data_manager_data_item >  data_vector;
00071   typedef vcl_map< unsigned, data_vector >  data_map;
00072   typedef vcl_vector< rgrl_estimator_sptr >  estimator_vector;
00073   typedef vcl_map< unsigned, estimator_vector >  estimator_map;
00074 
00075   //: same as std::map[]
00076   data_vector& operator[]( unsigned i )
00077   {
00078     return map_[i];
00079   }
00080 
00081   //: Assumes the key is in the map.
00082   data_vector const& operator[]( unsigned i ) const
00083   {
00084     return map_.find( i )->second;
00085   }
00086 
00087   //: true iff the key is in the map.
00088   bool has(unsigned i ) const
00089   {
00090     return map_.find( i ) != map_.end();
00091   }
00092 
00093   //:
00094   void add_estimator( unsigned i, rgrl_estimator_sptr estimator)
00095   {
00096     estimators_[i].push_back( estimator );
00097   }
00098 
00099   //: true iff the key is in the map
00100   bool has_estimator_hierarchy(unsigned i ) const
00101   {
00102     return estimators_.find( i ) != estimators_.end();
00103   }
00104 
00105   //: Assume the key is in the map
00106   estimator_vector const& estimator_hierarchy(unsigned i) const
00107   {
00108     return estimators_.find( i )->second;
00109   }
00110 
00111   //: Set dimension increase to go from the current stage to the next
00112   void set_dimension_increase_for_next_stage( unsigned i, double rate)
00113   {
00114     dim_increase_for_next_stage_[i] = rate;
00115   }
00116 
00117   //: Get dim_increase_for_next_stage
00118   double dimension_increase_for_next_stage(unsigned i) const
00119   {
00120     return dim_increase_for_next_stage_.find( i )->second;
00121   }
00122 
00123  private:
00124   data_map map_;
00125   estimator_map estimators_;
00126   vcl_map< unsigned, double >  dim_increase_for_next_stage_;
00127 };
00128 
00129 // ---------------------------------------------------------------------------
00130 //                                                  data manager
00131 //
00132 
00133 class rgrl_data_manager: public rgrl_object
00134 {
00135  public:
00136   //: Constructor
00137   //
00138   //  If multi_stage set to true, the data manager allows data stored
00139   //  in multiple stages. The registration engine processes data from
00140   //  the highest stage to the 0th stage. In the framework of
00141   //  multi-resolution, the stage index i corresponds to an effective
00142   //  sigma value of 2^i.
00143   rgrl_data_manager( bool multi_stage = false );
00144 
00145   ~rgrl_data_manager();
00146 
00147   //:  Add a data item to a multi-stage data storage.
00148   //
00149   //  The data item for registration takes at least two feature
00150   //  sets. Other components are set to the techniques commonly used
00151   //  in the literature, if not given. In such case, best performance
00152   //  is not guaranteed. To optimize the registration results, all
00153   //  components should be specified.
00154   //
00155   //  The matcher is ICP, the weighter is m_estimator (Beaton-Tukey),
00156   //  and the unwgted_scale_estimator is MAD (Median Absolute
00157   //  Deviation).  wgted_scale_est keeps the original value, since it
00158   //  is not an essential component.
00159   //
00160   void add_data( unsigned stage,
00161                  rgrl_feature_set_sptr                    from_set,
00162                  rgrl_feature_set_sptr                    to_set,
00163                  rgrl_matcher_sptr                        matcher = 0,
00164                  rgrl_weighter_sptr                       weighter = 0,
00165                  rgrl_scale_estimator_unwgted_sptr        unwgted_scale_est = 0,
00166                  rgrl_scale_estimator_wgted_sptr          wgted_scale_est = 0,
00167                  const vcl_string&                        label = vcl_string() );
00168 
00169   //: Add a data item to a single-stage data storage.
00170   //
00171   //  The stage is assumed to be 0.
00172   //
00173   void add_data( rgrl_feature_set_sptr                    from_set,
00174                  rgrl_feature_set_sptr                    to_set,
00175                  rgrl_matcher_sptr                        matcher = 0,
00176                  rgrl_weighter_sptr                       weighter = 0,
00177                  rgrl_scale_estimator_unwgted_sptr        unwgted_scale_est = 0,
00178                  rgrl_scale_estimator_wgted_sptr          wgted_scale_est = 0,
00179                  const vcl_string&                        label = vcl_string() );
00180 
00181   //: Add an estimator to a multi-stage data storage.
00182   void add_estimator( unsigned                           stage,
00183                       rgrl_estimator_sptr                estimator);
00184 
00185   //: Add an estimator to a single-stage data storage.
00186   //
00187   //  The stage is assumed to be 0
00188   //
00189   void add_estimator( rgrl_estimator_sptr                estimator);
00190 
00191   //: Set the dimension increase to go from the current stage to the next
00192   //
00193   //  If the function is never called for stage \a stage, the
00194   //  dimension increase for \a stage is 1.
00195   void set_dimension_increase_for_next_stage( unsigned stage, double rate);
00196 
00197   //: Return \a dimension_increase_for_next_stage_ at stage \a stage
00198   double dimension_increase_for_next_stage(unsigned stage) const;
00199 
00200   //: Return true if there exists at least one stage containing multiple data_items
00201   //
00202   bool is_multi_feature() const;
00203 
00204   //: Return the set of data_items and estimators from a multi-stage data storage.
00205   //
00206   void
00207   get_data_at_stage( unsigned stage,
00208                      vcl_vector<rgrl_feature_set_sptr>             & from_sets,
00209                      vcl_vector<rgrl_feature_set_sptr>             & to_sets,
00210                      vcl_vector<rgrl_matcher_sptr>                 & matchers,
00211                      vcl_vector<rgrl_weighter_sptr>                & weighters,
00212                      vcl_vector<rgrl_scale_estimator_unwgted_sptr> & unwgted_scale_ests,
00213                      vcl_vector<rgrl_scale_estimator_wgted_sptr>   & wgted_scale_ests,
00214                      vcl_vector<rgrl_estimator_sptr>               & estimators) const;
00215 
00216   //: Return the a single data_item and the set of estimators from a multi-stage data storage.
00217   //
00218   void
00219   get_data_at_stage( unsigned stage,
00220                      rgrl_feature_set_sptr             & from_sets,
00221                      rgrl_feature_set_sptr             & to_sets,
00222                      rgrl_matcher_sptr                 & matchers,
00223                      rgrl_weighter_sptr                & weighters,
00224                      rgrl_scale_estimator_unwgted_sptr & unwgted_scale_ests,
00225                      rgrl_scale_estimator_wgted_sptr   & wgted_scale_ests,
00226                      vcl_vector<rgrl_estimator_sptr>   & estimators) const;
00227 
00228   //: Return the set of data_items and estimators from a single-stage data storage.
00229   //
00230   //  The stage is assumed to be 0.
00231   void
00232   get_data( vcl_vector<rgrl_feature_set_sptr>             & from_sets,
00233             vcl_vector<rgrl_feature_set_sptr>             & to_sets,
00234             vcl_vector<rgrl_matcher_sptr>                 & matchers,
00235             vcl_vector<rgrl_weighter_sptr>                & weighters,
00236             vcl_vector<rgrl_scale_estimator_unwgted_sptr> & unwgted_scale_ests,
00237             vcl_vector<rgrl_scale_estimator_wgted_sptr>   & wgted_scale_ests,
00238             vcl_vector<rgrl_estimator_sptr>               & estimators) const;
00239 
00240   //: Return the a single data_item and the set of estimators from a single-stage data storage.
00241   //
00242   //  The stage is assumed to be 0.
00243   void
00244   get_data( rgrl_feature_set_sptr             & from_sets,
00245             rgrl_feature_set_sptr             & to_sets,
00246             rgrl_matcher_sptr                 & matchers,
00247             rgrl_weighter_sptr                & weighters,
00248             rgrl_scale_estimator_unwgted_sptr & unwgted_scale_ests,
00249             rgrl_scale_estimator_wgted_sptr   & wgted_scale_ests,
00250             vcl_vector<rgrl_estimator_sptr>   & estimators) const;
00251 
00252   //: Return labels
00253   void get_label( unsigned stage, vcl_vector<vcl_string>& labels ) const;
00254 
00255   //: Return labels
00256   void get_label( vcl_vector<vcl_string>& labels ) const;
00257 
00258   //: Return true if certain stage exists with data
00259   bool has_stage(unsigned i ) const;
00260 
00261   // Defines type-related functions
00262   rgrl_type_macro( rgrl_data_manager, rgrl_object );
00263 
00264  private:
00265   void generate_defaults(rgrl_matcher_sptr&                  matcher,
00266                          rgrl_weighter_sptr&                 weighter,
00267                          rgrl_scale_estimator_unwgted_sptr&  unwgted_scale_est );
00268 
00269  private:
00270   rgrl_data_manager_data_storage   data_;
00271   bool                             multi_stage_;
00272   bool                             multi_feature_;
00273 };
00274 
00275 #endif