1 #ifndef rgrl_data_manager_h
2 #define rgrl_data_manager_h
3 //:
4 // \file
5 // \brief Class to represent the collection of feature sets available for use by the registration engine.
6 // \author Charlene Tsai
7 // \date   Feb 2004
8 
9 #include <iostream>
10 #include <map>
11 #include <string>
12 #include <utility>
13 #include <vector>
14 #include <rgrl/rgrl_estimator.h>
15 #include <rgrl/rgrl_estimator_sptr.h>
16 #include <rgrl/rgrl_feature_set.h>
17 #include <rgrl/rgrl_feature_set_sptr.h>
18 #include <rgrl/rgrl_matcher.h>
19 #include <rgrl/rgrl_matcher_sptr.h>
20 #include <rgrl/rgrl_object.h>
21 #include <rgrl/rgrl_scale_estimator.h>
22 #include <rgrl/rgrl_scale_estimator_sptr.h>
23 #include <rgrl/rgrl_weighter.h>
24 #include <rgrl/rgrl_weighter_sptr.h>
25 
26 #ifdef _MSC_VER
27 #  include <vcl_msvc_warnings.h>
28 #endif
29 
30 // ---------------------------------------------------------------------------
31 //                                                                   data item
32 //
33 
34 //: An object to store the moving_feature_set, its matching fixed_feature_set, and other components
35 //  (that work on the two feature sets during registration).
36 //
37 class rgrl_data_manager_data_item
38 {
39  public:
40   rgrl_data_manager_data_item( rgrl_feature_set_sptr              in_from_set,
41                                rgrl_feature_set_sptr              in_to_set,
42                                rgrl_matcher_sptr                  in_matcher,
43                                rgrl_weighter_sptr                 in_weighter,
44                                rgrl_scale_estimator_unwgted_sptr  in_unwgted_scale_est,
45                                rgrl_scale_estimator_wgted_sptr    in_wgted_scale_est,
46                                std::string                   in_label = std::string() )
from_set(in_from_set)47     : from_set( in_from_set ),
48       to_set( in_to_set ),
49       matcher( in_matcher ),
50       unwgted_scale_est( in_unwgted_scale_est ),
51       wgted_scale_est( in_wgted_scale_est ),
52       weighter( in_weighter ),
53       label(std::move( in_label ))
54     {
55     }
56 
57   rgrl_feature_set_sptr             from_set;
58   rgrl_feature_set_sptr             to_set;
59   rgrl_matcher_sptr                 matcher;
60   rgrl_scale_estimator_unwgted_sptr unwgted_scale_est;
61   rgrl_scale_estimator_wgted_sptr   wgted_scale_est;
62   rgrl_weighter_sptr                weighter;
63   std::string                        label;
64 };
65 
66 // ---------------------------------------------------------------------------
67 //                                                                data storage
68 //
69 
70 //: A collection of data in stages, where each stage can store multiple data items and multiple estimators.
71 //
72 class rgrl_data_manager_data_storage
73 {
74  public:
75   typedef std::vector< rgrl_data_manager_data_item >  data_vector;
76   typedef std::map< unsigned, data_vector >  data_map;
77   typedef std::vector< rgrl_estimator_sptr >  estimator_vector;
78   typedef std::map< unsigned, estimator_vector >  estimator_map;
79 
80   //: same as std::map[]
81   data_vector& operator[]( unsigned i )
82   {
83     return map_[i];
84   }
85 
86   //: Assumes the key is in the map.
87   data_vector const& operator[]( unsigned i ) const
88   {
89     return map_.find( i )->second;
90   }
91 
92   //: true iff the key is in the map.
has(unsigned i)93   bool has(unsigned i ) const
94   {
95     return map_.find( i ) != map_.end();
96   }
97 
98   //:
add_estimator(unsigned i,rgrl_estimator_sptr estimator)99   void add_estimator( unsigned i, rgrl_estimator_sptr estimator)
100   {
101     estimators_[i].push_back( estimator );
102   }
103 
104   //: true iff the key is in the map
has_estimator_hierarchy(unsigned i)105   bool has_estimator_hierarchy(unsigned i ) const
106   {
107     return estimators_.find( i ) != estimators_.end();
108   }
109 
110   //: Assume the key is in the map
estimator_hierarchy(unsigned i)111   estimator_vector const& estimator_hierarchy(unsigned i) const
112   {
113     return estimators_.find( i )->second;
114   }
115 
116   //: Set dimension increase to go from the current stage to the next
set_dimension_increase_for_next_stage(unsigned i,double rate)117   void set_dimension_increase_for_next_stage( unsigned i, double rate)
118   {
119     dim_increase_for_next_stage_[i] = rate;
120   }
121 
122   //: Get dim_increase_for_next_stage
dimension_increase_for_next_stage(unsigned i)123   double dimension_increase_for_next_stage(unsigned i) const
124   {
125     return dim_increase_for_next_stage_.find( i )->second;
126   }
127 
128  private:
129   data_map map_;
130   estimator_map estimators_;
131   std::map< unsigned, double >  dim_increase_for_next_stage_;
132 };
133 
134 // ---------------------------------------------------------------------------
135 //                                                  data manager
136 //
137 
138 class rgrl_data_manager: public rgrl_object
139 {
140  public:
141   //: Constructor
142   //
143   //  If multi_stage set to true, the data manager allows data stored
144   //  in multiple stages. The registration engine processes data from
145   //  the highest stage to the 0th stage. In the framework of
146   //  multi-resolution, the stage index i corresponds to an effective
147   //  sigma value of 2^i.
148   rgrl_data_manager( bool multi_stage = false );
149 
150   ~rgrl_data_manager() override;
151 
152   //:  Add a data item to a multi-stage data storage.
153   //
154   //  The data item for registration takes at least two feature
155   //  sets. Other components are set to the techniques commonly used
156   //  in the literature, if not given. In such case, best performance
157   //  is not guaranteed. To optimize the registration results, all
158   //  components should be specified.
159   //
160   //  The matcher is ICP, the weighter is m_estimator (Beaton-Tukey),
161   //  and the unwgted_scale_estimator is MAD (Median Absolute
162   //  Deviation).  wgted_scale_est keeps the original value, since it
163   //  is not an essential component.
164   //
165   void add_data( unsigned stage,
166                  const rgrl_feature_set_sptr&                    from_set,
167                  const rgrl_feature_set_sptr&                    to_set,
168                  rgrl_matcher_sptr                        matcher = nullptr,
169                  rgrl_weighter_sptr                       weighter = nullptr,
170                  rgrl_scale_estimator_unwgted_sptr        unwgted_scale_est = nullptr,
171                  const rgrl_scale_estimator_wgted_sptr&          wgted_scale_est = nullptr,
172                  const std::string&                        label = std::string() );
173 
174   //: Add a data item to a single-stage data storage.
175   //
176   //  The stage is assumed to be 0.
177   //
178   void add_data( const rgrl_feature_set_sptr&                    from_set,
179                  const rgrl_feature_set_sptr&                    to_set,
180                  const rgrl_matcher_sptr&                        matcher = nullptr,
181                  const rgrl_weighter_sptr&                       weighter = nullptr,
182                  const rgrl_scale_estimator_unwgted_sptr&        unwgted_scale_est = nullptr,
183                  const rgrl_scale_estimator_wgted_sptr&          wgted_scale_est = nullptr,
184                  const std::string&                        label = std::string() );
185 
186   //: Add an estimator to a multi-stage data storage.
187   void add_estimator( unsigned                           stage,
188                       const rgrl_estimator_sptr&                estimator);
189 
190   //: Add an estimator to a single-stage data storage.
191   //
192   //  The stage is assumed to be 0
193   //
194   void add_estimator( const rgrl_estimator_sptr&                estimator);
195 
196   //: Set the dimension increase to go from the current stage to the next
197   //
198   //  If the function is never called for stage \a stage, the
199   //  dimension increase for \a stage is 1.
200   void set_dimension_increase_for_next_stage( unsigned stage, double rate);
201 
202   //: Return \a dimension_increase_for_next_stage_ at stage \a stage
203   double dimension_increase_for_next_stage(unsigned stage) const;
204 
205   //: Return true if there exists at least one stage containing multiple data_items
206   //
207   bool is_multi_feature() const;
208 
209   //: Return the set of data_items and estimators from a multi-stage data storage.
210   //
211   void
212   get_data_at_stage( unsigned stage,
213                      std::vector<rgrl_feature_set_sptr>             & from_sets,
214                      std::vector<rgrl_feature_set_sptr>             & to_sets,
215                      std::vector<rgrl_matcher_sptr>                 & matchers,
216                      std::vector<rgrl_weighter_sptr>                & weighters,
217                      std::vector<rgrl_scale_estimator_unwgted_sptr> & unwgted_scale_ests,
218                      std::vector<rgrl_scale_estimator_wgted_sptr>   & wgted_scale_ests,
219                      std::vector<rgrl_estimator_sptr>               & estimators) const;
220 
221   //: Return the a single data_item and the set of estimators from a multi-stage data storage.
222   //
223   void
224   get_data_at_stage( unsigned stage,
225                      rgrl_feature_set_sptr             & from_sets,
226                      rgrl_feature_set_sptr             & to_sets,
227                      rgrl_matcher_sptr                 & matchers,
228                      rgrl_weighter_sptr                & weighters,
229                      rgrl_scale_estimator_unwgted_sptr & unwgted_scale_ests,
230                      rgrl_scale_estimator_wgted_sptr   & wgted_scale_ests,
231                      std::vector<rgrl_estimator_sptr>   & estimators) const;
232 
233   //: Return the set of data_items and estimators from a single-stage data storage.
234   //
235   //  The stage is assumed to be 0.
236   void
237   get_data( std::vector<rgrl_feature_set_sptr>             & from_sets,
238             std::vector<rgrl_feature_set_sptr>             & to_sets,
239             std::vector<rgrl_matcher_sptr>                 & matchers,
240             std::vector<rgrl_weighter_sptr>                & weighters,
241             std::vector<rgrl_scale_estimator_unwgted_sptr> & unwgted_scale_ests,
242             std::vector<rgrl_scale_estimator_wgted_sptr>   & wgted_scale_ests,
243             std::vector<rgrl_estimator_sptr>               & estimators) const;
244 
245   //: Return the a single data_item and the set of estimators from a single-stage data storage.
246   //
247   //  The stage is assumed to be 0.
248   void
249   get_data( rgrl_feature_set_sptr             & from_sets,
250             rgrl_feature_set_sptr             & to_sets,
251             rgrl_matcher_sptr                 & matchers,
252             rgrl_weighter_sptr                & weighters,
253             rgrl_scale_estimator_unwgted_sptr & unwgted_scale_ests,
254             rgrl_scale_estimator_wgted_sptr   & wgted_scale_ests,
255             std::vector<rgrl_estimator_sptr>   & estimators) const;
256 
257   //: Return labels
258   void get_label( unsigned stage, std::vector<std::string>& labels ) const;
259 
260   //: Return labels
261   void get_label( std::vector<std::string>& labels ) const;
262 
263   //: Return true if certain stage exists with data
264   bool has_stage(unsigned i ) const;
265 
266   // Defines type-related functions
267   rgrl_type_macro( rgrl_data_manager, rgrl_object );
268 
269  private:
270   void generate_defaults(rgrl_matcher_sptr&                  matcher,
271                          rgrl_weighter_sptr&                 weighter,
272                          rgrl_scale_estimator_unwgted_sptr&  unwgted_scale_est );
273 
274  private:
275   rgrl_data_manager_data_storage   data_;
276   bool                             multi_stage_;
277   bool multi_feature_{false};
278 };
279 
280 #endif
281