contrib/rpl/rgrl/rgrl_match_set.h
Go to the documentation of this file.
00001 #ifndef rgrl_match_set_h_
00002 #define rgrl_match_set_h_
00003 //:
00004 // \file
00005 // \brief  Represents a set of matches for a given feature type
00006 // \author Amitha Perera
00007 // \date 14 Nov 2002
00008 
00009 #include <vcl_vector.h>
00010 #include <vcl_cstddef.h>
00011 
00012 #include <rgrl/rgrl_feature_sptr.h>
00013 #include <rgrl/rgrl_feature_set.h>
00014 #include <rgrl/rgrl_object.h>
00015 #include <rgrl/rgrl_match_set_sptr.h>
00016 
00017 #include <vcl_iosfwd.h>
00018 
00019 class rgrl_transformation;
00020 
00021 // Forward declaration. Used internally by rgrl_match_set.
00022 class rgrl_match_set_from;
00023 
00024 // Iterators
00025 class rgrl_match_set_from_iterator;
00026 class rgrl_match_set_from_to_iterator;
00027 
00028 class rgrl_match_set_const_from_iterator;
00029 class rgrl_match_set_const_from_to_iterator;
00030 
00031 //:
00032 // For each feature in the "from" set, there is a set of features in
00033 // the "to" set. Each "from" point is indexed by an index \a i. For
00034 // each "from" point, there is a set of "to" points, indexed by \a j,
00035 // where the range of \a j depends on \a i. A specific
00036 // feature-to-feature correspondence is therefore given by (\a i, \a
00037 // j).  A "from" point could be repeated with a different index,
00038 // allowing a many-to-many matching.
00039 //
00040 // While one can request the set of "to" matches corresponding to a
00041 // given "from" match, there is currently no functionality to provide
00042 // the reverse. This functionality may be added in future, however, so
00043 // please use with this in mind.
00044 //
00045 class rgrl_match_set
00046   : public rgrl_object
00047 {
00048  public:
00049   typedef vcl_size_t                          size_type;
00050   typedef rgrl_match_set_from_iterator        from_iterator;
00051   typedef rgrl_match_set_const_from_iterator  const_from_iterator;
00052 
00053  public:
00054   //:  construct an empty match set
00055   rgrl_match_set( );
00056 
00057   //:  construct an empty match set, specifying the feature type
00058   rgrl_match_set( const vcl_type_info& feature_type );
00059 
00060   //:  construct an empty match set, specifying the feature type
00061   rgrl_match_set( const vcl_type_info& from_type,
00062                   const vcl_type_info& to_type,
00063                   const rgrl_feature_set_label& from_label = rgrl_feature_set_label(),
00064                   const rgrl_feature_set_label& to_label = rgrl_feature_set_label() );
00065 
00066   //: type of from features
00067   const vcl_type_info&
00068   from_type() const { return *from_type_; }
00069 
00070   //: type of from features
00071   const vcl_type_info&
00072   to_type() const { return *to_type_; }
00073 
00074   //: label of from features
00075   const rgrl_feature_set_label&
00076   from_label() const
00077   { return from_label_; }
00078 
00079   //: label of from features
00080   const rgrl_feature_set_label&
00081   to_label() const
00082   { return to_label_; }
00083 
00084   //: The number of "from" features.
00085   size_type from_size() const;
00086 
00087   const_from_iterator from_begin() const;
00088 
00089   const_from_iterator from_end() const;
00090 
00091   from_iterator from_begin();
00092 
00093   from_iterator from_end();
00094 
00095   //: Remove all the matches from this set.
00096   void clear();
00097 
00098   //: write out a match set
00099   void write( vcl_ostream& os ) const;
00100 
00101   //: write out a match set
00102   void write_sorted( vcl_ostream& os ) const;
00103 
00104   //: read in a match set
00105   bool read( vcl_istream& is );
00106 
00107   //: Add from feature, the transformed feature, and its matching "to" features.
00108   //
00109   void add_feature_and_matches( rgrl_feature_sptr                      from_feature,
00110                                 rgrl_feature_sptr                      mapped_feature,
00111                                 vcl_vector< rgrl_feature_sptr > const& matching_to );
00112 
00113   //: Add from feature, the transformed feature, and its matching "to" features, and the signature weight.
00114   //
00115   void add_feature_matches_and_weights( rgrl_feature_sptr                      from_feature,
00116                                         rgrl_feature_sptr                      mapped_feature,
00117                                         vcl_vector< rgrl_feature_sptr > const& matching_to,
00118                                         vcl_vector< double > const&            signature_weights );
00119 
00120   //: Add from feature, the transformed feature, and its matching "to" features, and ALL weights.
00121   //
00122   void add_feature_matches_and_weights( rgrl_feature_sptr                      from_feature,
00123                                         rgrl_feature_sptr                      mapped_feature,
00124                                         vcl_vector< rgrl_feature_sptr > const& matching_to,
00125                                         vcl_vector< double > const&            sig_wgts,
00126                                         vcl_vector< double > const&            geo_wgts,
00127                                         vcl_vector< double > const&            cum_wgts );
00128 
00129   //: Add from feature and its matching "to" feature.
00130   //
00131   // Convenience method for adding a one-to-one correspondence. The \a
00132   // wgt parameter is used to initialize all the weights for the
00133   // correspondence (geometric, signature, and cumulative weights).
00134   //
00135   void add_feature_and_match( rgrl_feature_sptr                      from_feature,
00136                               rgrl_feature_sptr                      mapped_feature,
00137                               rgrl_feature_sptr                      matching_to,
00138                               double                                 wgt = 1.0 );
00139 
00140   //: Update the cached mapped_from_feature using \a trans.
00141   //
00142   // This will remap each from_feature using \a trans and store the
00143   // result in \a mapped_from_feature.
00144   //
00145   void remap_from_features( rgrl_transformation const& trans );
00146 
00147   //: Update only the location of the cached mapped_from_feature using \a trans.
00148   //
00149   // This will remap the location of each from_feature using
00150   // \a trans and store the result in the already existing
00151   // \a mapped_from_feature.
00152   //
00153   void
00154   remap_only_location( rgrl_transformation const& trans );
00155 
00156   //  CS (9/20/2003): I am not at all sure that the idea of a single
00157   //  feature type in a match set is really a good idea, especially
00158   //  given the use of the error projector.
00159 
00160   //: to access feature types
00161   //
00162   const vcl_type_info& from_feature_type() const {return *from_type_;}
00163   const vcl_type_info& to_feature_type() const {return *to_type_;}
00164 
00165   unsigned int num_constraints_per_match() const;
00166 
00167   //: reserve space for a number of matches
00168   //
00169   void reserve( unsigned i ) {
00170     from_features_.reserve( i );
00171     xformed_from_features_.reserve(i);
00172     matches_and_weights_.reserve(i);
00173   }
00174 
00175  private:
00176   //: Holds the details of match.
00177   class match_info
00178   {
00179    public:
00180     //:
00181     // geometric and cumulative weights are set to -1 to allow
00182     //  a safety check on their usage.
00183     //
00184     match_info( rgrl_feature_sptr to_feat );
00185 
00186     //:
00187     // All the weights are initialized
00188     //
00189     match_info( rgrl_feature_sptr to_feat,
00190                 double geometric_wgt,
00191                 double signature_wgt,
00192                 double cumulative_wgt = 0);
00193 
00194     //:  Initialize the signature weight only
00195     //
00196     match_info( rgrl_feature_sptr to_feat,
00197                 double signature_wgt );
00198 
00199     rgrl_feature_sptr to_feature;
00200     double geometric_weight;
00201     double signature_weight;
00202     double cumulative_weight;
00203   };
00204 
00205   void set_num_constraints_per_match() const;
00206 
00207  private:
00208   friend class rgrl_match_set_from_iterator;
00209   friend class rgrl_match_set_const_from_iterator;
00210   friend class rgrl_match_set_from_to_iterator;
00211   friend class rgrl_match_set_const_from_to_iterator;
00212 
00213 
00214   const vcl_type_info* from_type_;
00215   const vcl_type_info* to_type_;
00216   rgrl_feature_set_label from_label_;
00217   rgrl_feature_set_label to_label_;
00218 
00219   mutable unsigned int num_constraints_per_match_;
00220   vcl_vector< rgrl_feature_sptr > from_features_;
00221   vcl_vector< rgrl_feature_sptr > xformed_from_features_;
00222   vcl_vector< vcl_vector< match_info > > matches_and_weights_;
00223 };
00224 
00225 //: stream output
00226 vcl_ostream&
00227 operator<< ( vcl_ostream& os, rgrl_match_set const& set );
00228 
00229 //: stream input
00230 vcl_istream&
00231 operator>> ( vcl_istream& is, rgrl_match_set& set );
00232 
00233 class rgrl_match_set_from_iterator
00234 {
00235  private:
00236   typedef rgrl_match_set::match_info                            match_info;
00237  public:
00238   typedef rgrl_match_set_from_to_iterator                       to_iterator;
00239   typedef vcl_vector< vcl_vector< match_info > >::size_type     size_type;
00240 
00241  public:
00242   //: Default constructor.
00243   // A default constructed iterator is, of course, invalid until it is
00244   // set to point into a rgrl_match_set.
00245   rgrl_match_set_from_iterator( );
00246 
00247   rgrl_match_set_from_iterator& operator++();
00248   rgrl_match_set_from_iterator operator+( int );
00249 
00250   bool operator==( const rgrl_match_set_from_iterator& other ) const;
00251   bool operator!=( const rgrl_match_set_from_iterator& other ) const;
00252 
00253   //: The beginning of the set of "to" matches for this "from".
00254   to_iterator begin() const;
00255 
00256   //: The end of the set of "to" matches for this "from".
00257   to_iterator end() const;
00258 
00259   //: The number of "to" matches for this "from"
00260   size_type size() const;
00261 
00262   //:
00263   bool empty() const;
00264 
00265   //:
00266   rgrl_feature_sptr from_feature() const;
00267 
00268   //:
00269   rgrl_feature_sptr mapped_from_feature() const;
00270 
00271  private:
00272   friend class rgrl_match_set;
00273   friend class rgrl_match_set_const_from_iterator;
00274 
00275   // For use by rgrl_match_set
00276   rgrl_match_set_from_iterator( rgrl_match_set* ms,
00277                                 vcl_vector< rgrl_feature_sptr >::size_type ind );
00278 
00279   rgrl_match_set* match_set_;
00280   vcl_vector< rgrl_feature_sptr >::size_type index_;
00281 };
00282 
00283 
00284 class rgrl_match_set_const_from_iterator
00285 {
00286  private:
00287   typedef rgrl_match_set::match_info                            match_info;
00288  public:
00289   typedef rgrl_match_set_const_from_to_iterator                 to_iterator;
00290   typedef vcl_vector< vcl_vector< match_info > >::size_type     size_type;
00291 
00292  public:
00293   //: Default constructor.
00294   // A default constructed iterator is, of course, invalid until it is
00295   // set to point into a rgrl_match_set.
00296   rgrl_match_set_const_from_iterator( );
00297 
00298   //: copy constructor
00299   //  it is used to convert a from_iterator into const type
00300   rgrl_match_set_const_from_iterator( rgrl_match_set_from_iterator const& from_iter );
00301 
00302   rgrl_match_set_const_from_iterator& operator++();
00303   rgrl_match_set_const_from_iterator operator++( int );
00304   rgrl_match_set_const_from_iterator operator+( int );
00305 
00306   bool operator==( const rgrl_match_set_const_from_iterator& other ) const;
00307   bool operator!=( const rgrl_match_set_const_from_iterator& other ) const;
00308 
00309   //: The beginning of the set of "to" matches for this "from".
00310   to_iterator begin() const;
00311 
00312   //: The end of the set of "to" matches for this "from".
00313   to_iterator end() const;
00314 
00315   //: The number of "to" matches for this "from"
00316   size_type size() const;
00317 
00318   //:
00319   bool empty() const;
00320 
00321   //:
00322   rgrl_feature_sptr const& from_feature() const;
00323 
00324   //:
00325   rgrl_feature_sptr const& mapped_from_feature() const;
00326 
00327  private:
00328   friend class rgrl_match_set;
00329 
00330   // For use by rgrl_match_set
00331   rgrl_match_set_const_from_iterator( rgrl_match_set const* ms,
00332                                       vcl_vector< rgrl_feature_sptr >::size_type ind );
00333 
00334   rgrl_match_set const* match_set_;
00335   vcl_vector< rgrl_feature_sptr >::size_type index_;
00336 };
00337 
00338 
00339 class rgrl_match_set_from_to_iterator
00340 {
00341  public:
00342   rgrl_match_set_from_to_iterator( );
00343 
00344   // default copy constructor and assignment operator does the correct
00345   // job
00346   // rgrl_match_set_from_to_iterator( rgrl_match_set_from_to_iterator const& );
00347 
00348   //:
00349   rgrl_match_set_from_to_iterator& operator++();
00350   rgrl_match_set_from_to_iterator operator+(int RHS);
00351 
00352   bool operator==( const rgrl_match_set_from_to_iterator& other ) const;
00353   bool operator!=( const rgrl_match_set_from_to_iterator& other ) const;
00354 
00355   //:
00356   rgrl_feature_sptr to_feature() const;
00357 
00358   //:
00359   double geometric_weight() const;
00360 
00361   //:
00362   double signature_weight() const;
00363 
00364   //:
00365   double cumulative_weight() const;
00366 
00367   //:
00368   void set_geometric_weight( double geom_wgt );
00369 
00370   //:
00371   void set_signature_weight( double sig_wgt );
00372 
00373   //:
00374   void set_cumulative_weight( double cum_wgt );
00375 
00376  protected:
00377 
00378   friend class rgrl_match_set_from_iterator;
00379   friend class rgrl_match_set_const_from_to_iterator;
00380 
00381   typedef rgrl_match_set::match_info match_info;
00382   typedef vcl_vector< match_info >::iterator MatchInfoIter;
00383   // for use by rgrl_match_set_from_iterator
00384   rgrl_match_set_from_to_iterator( MatchInfoIter const& itr );
00385 
00386   MatchInfoIter itr_;
00387 };
00388 
00389 
00390 class rgrl_match_set_const_from_to_iterator
00391 {
00392  public:
00393   typedef rgrl_match_set_const_from_to_iterator self_type;
00394  public:
00395   rgrl_match_set_const_from_to_iterator( );
00396 
00397   // default copy constructor and assignment operator does the correct
00398   // job
00399   // rgrl_match_set_from_to_iterator( rgrl_match_set_from_to_iterator const& );
00400 
00401   //: copy constructor
00402   //  it is used to convert from_to_iterator to const type
00403   rgrl_match_set_const_from_to_iterator( rgrl_match_set_from_to_iterator const& to_iter );
00404 
00405   //:
00406   self_type& operator++();
00407   self_type operator+(int RHS);
00408 
00409   bool operator==( const self_type& other ) const;
00410   bool operator!=( const self_type& other ) const;
00411 
00412   //:
00413   rgrl_feature_sptr const& to_feature() const;
00414 
00415   //:
00416   double geometric_weight() const;
00417 
00418   //:
00419   double signature_weight() const;
00420 
00421   //:
00422   double cumulative_weight() const;
00423 
00424  protected:
00425 
00426   friend class rgrl_match_set_const_from_iterator;
00427   typedef rgrl_match_set::match_info match_info;
00428   typedef vcl_vector< match_info >::const_iterator MatchInfoIter;
00429   // for use by rgrl_match_set_from_iterator
00430   rgrl_match_set_const_from_to_iterator( MatchInfoIter const& itr );
00431 
00432   MatchInfoIter itr_;
00433 };
00434 
00435 
00436 #endif // rgrl_match_set_h_