00001
00002
00003 #ifndef _GFM_MATCHING_RESULT_HPP_
00004 #define _GFM_MATCHING_RESULT_HPP_
00005
00006 #include <boost/serialization/nvp.hpp>
00007 #include <boost/serialization/utility.hpp>
00008 #include <boost/serialization/version.hpp>
00009 #include <boost/serialization/vector.hpp>
00010 #include <boost/shared_ptr.hpp>
00011
00012 #include "fdetect/InterestFeature.hpp"
00013 #include "fdetect/DetectionResult.hpp"
00014
00015 namespace jafar {
00016 namespace gfm {
00017 struct Match {
00018 Match(fdetect::InterestFeature* p_r, fdetect::InterestFeature* p_m, int i) :
00019 point_ref(p_r), point_match(p_m), index(i)
00020 {
00021 }
00022 fdetect::InterestFeature* point_ref;
00023 fdetect::InterestFeature* point_match;
00024 int index;
00025
00026
00027
00028
00029
00030
00031
00032 };
00033 typedef std::vector<Match> vMatches;
00034 typedef std::vector<Match>::iterator vMatches_it;
00039 class MatchingResult {
00040 struct Shared;
00041 public:
00042
00043 MatchingResult();
00047 MatchingResult(const jafar::fdetect::DetectionResult& pr, const jafar::fdetect::DetectionResult& pm );
00051 MatchingResult(const MatchingResult&);
00056 MatchingResult& operator=(const MatchingResult&);
00057
00058
00059
00060
00061
00062
00063
00064 ~MatchingResult();
00065 public:
00067 void addMatch(fdetect::InterestFeature* ip1, fdetect::InterestFeature* ip2);
00069 void merge( jafar::gfm::vMatches m);
00071 void propageId();
00073 void propageStereoIndex();
00075 void propagateId( unsigned int &idVal );
00077 std::size_t nbMatches() const;
00079 const jafar::fdetect::DetectionResult& pointsRef() const;
00081 const jafar::fdetect::DetectionResult& pointsMatch() const;
00082 vMatches& matches() const;
00084 bool valid() const { return (m_shared!=NULL); }
00085 private:
00086 void unref();
00087 Shared* m_shared;
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 };
00117 }
00118
00119 namespace gfm_v2 {
00120 template<typename InterestFeatureT>
00121 struct Match {
00122 Match(InterestFeatureT* p_r,
00123 InterestFeatureT* p_m, int i) :
00124 point_ref(p_r), point_match(p_m), index(i)
00125 {
00126 }
00127 InterestFeatureT* point_ref;
00128 InterestFeatureT* point_match;
00129 int index;
00130 template<class Archive>
00131 void serialize(Archive &ar, const unsigned int version)
00132 {
00133 ar & BOOST_SERIALIZATION_NVP(point_ref);
00134 ar & BOOST_SERIALIZATION_NVP(point_match);
00135 ar & index;
00136 }
00137 };
00142 template<typename InterestFeatureT>
00143 class MatchingResult {
00144 public:
00145 typedef typename gfm_v2::Match< InterestFeatureT > MatchType;
00146 typedef std::vector< MatchType > vMatches;
00147 typedef typename std::vector< MatchType >::iterator iterator;
00148 typedef typename std::vector< MatchType >::const_iterator const_iterator;
00149 typedef fdetect_v2::DetectionResult<InterestFeatureT> DetectionResult;
00150
00151 private:
00152 struct Shared {
00153 Shared() {};
00154 Shared(const DetectionResult& pr,
00155 const DetectionResult& pm )
00156 : points_ref(pr), points_match(pm) { }
00157 vMatches matches;
00158 DetectionResult points_ref;
00159 DetectionResult points_match;
00160 };
00161
00162 boost::shared_ptr<Shared> m_shared;
00163
00164 public:
00165 MatchingResult() : m_shared(new Shared()) {};
00169 MatchingResult(const DetectionResult& pr,
00170 const DetectionResult& pm ) :
00171 m_shared(new Shared(pr,pm)) {};
00172 MatchingResult(const MatchingResult& mr) :
00173 m_shared(mr.m_shared) {};
00174
00175 ~MatchingResult() {};
00176 public:
00178 void addMatch(InterestFeatureT* ip1,
00179 InterestFeatureT* ip2)
00180 {
00181 m_shared->matches.push_back(Match<InterestFeatureT>(ip1, ip2, m_shared->matches.size()));
00182 }
00184 void push_back(InterestFeatureT* ip1,
00185 InterestFeatureT* ip2)
00186 {
00187 m_shared->matches.push_back(MatchType(ip1, ip2, m_shared->matches.size()));
00188 }
00190 iterator begin()
00191 {
00192 return m_shared->matches.begin();
00193 }
00195 iterator end()
00196 {
00197 return m_shared->matches.end();
00198 }
00200 const_iterator begin() const
00201 {
00202 return m_shared->matches.begin();
00203 }
00205 const_iterator end() const
00206 {
00207 return m_shared->matches.end();
00208 }
00210 size_t size() const
00211 {
00212 return m_shared->matches.size();
00213 }
00215 void merge( const vMatches &m)
00216 {
00217 for(iterator it = m.begin(); it != m.end(); ++it)
00218 {
00219 Match<InterestFeatureT> m = *it;
00220 m.index = m_shared->matches.size();
00221 m_shared->matches.push_back(m);
00222 }
00223 }
00225 void propageId()
00226 {
00227 for( iterator it = m_shared->matches.begin();
00228 it != m_shared->matches.end();
00229 ++it)
00230 {
00231 it->point_match->setId( it->point_ref->id() );
00232 }
00233 }
00235 void propageStereoIndex()
00236 {
00237 for( iterator it = m_shared->matches.begin();
00238 it != m_shared->matches.end();
00239 ++it)
00240 {
00241 it->point_match->setStereoIndex(it->point_ref->index());
00242 it->point_ref->setStereoIndex(it->point_match->index());
00243 }
00244 }
00248 void propagateId( unsigned int &idVal )
00249 {
00250 for( iterator it = m_shared->matches.begin();
00251 it != m_shared->matches.end();
00252 ++it)
00253 {
00254 if (it->point_ref->id() == geom::InterestPoint::NO_ID)
00255 {
00256 if (it->point_ref->id() == geom::InterestPoint::NO_ID)
00257 {
00258 it->point_match->setId( idVal );
00259 it->point_ref->setId( idVal );
00260 ++idVal;
00261 }
00262 else
00263 {
00264 it->point_match->setId( it->point_ref->id() );
00265 }
00266 }
00267 else
00268 {
00269 it->point_match->setId( it->point_ref->id() );
00270 }
00271 }
00272 }
00274 std::size_t nbMatches() const
00275 {
00276 return m_shared->matches.size();
00277 }
00279 const DetectionResult& pointsRef() const
00280 {
00281 return m_shared->points_ref;
00282 }
00284 const DetectionResult& pointsMatch() const
00285 {
00286 return m_shared->points_match;
00287 }
00289 vMatches& matches() const
00290 {
00291 return m_shared->matches;
00292 }
00294 bool valid() const
00295 {
00296 return (m_shared!=NULL);
00297 }
00299 void clear()
00300 {
00301 m_shared->matches.clear();
00302 }
00304 void erase(size_t pos)
00305 {
00306 iterator it = begin();
00307 it+=pos;
00308 m_shared->matches.erase(it);
00309 }
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338 };
00339 template<typename T>
00340 std::ostream& operator <<(std::ostream& s, const MatchingResult<T> &mr)
00341 {
00342 using namespace std;
00343 for(typename MatchingResult<T>::const_iterator mit = mr.begin();
00344 mit != mr.end();
00345 ++mit) {
00346 s << "ref "<< mit->point_ref->parent_id << "," << mit->point_ref->index();
00347 s << "\tmatch "<< mit->point_match->parent_id << "," << mit->point_match->index();
00348 s << endl;
00349 }
00350 return s;
00351 }
00352 }
00353 }
00354
00355 #endif