11 #ifndef MATCHONETOONE_HH_
12 #define MATCHONETOONE_HH_
19 struct matchOneToOne_MatchInfo
25 inline bool operator<(
const matchOneToOne_MatchInfo& r)
const
27 return distance < r.distance;
29 inline bool operator>(
const matchOneToOne_MatchInfo& r)
const
31 return distance > r.distance;
43 template <
class T1,
class T2,
class DistanceCalculator>
44 unsigned matchOneToOne(
const std::vector<T1>& v1,
const std::vector<T2>& v2,
45 const unsigned n1,
const unsigned n2,
46 const DistanceCalculator& calc,
47 std::vector<int>* matchFrom1To2,
48 const double maxMatchingDistance = DBL_MAX)
51 matchFrom1To2->clear();
55 matchFrom1To2->reserve(n1);
56 for (
unsigned i1=0; i1<n1; ++i1)
57 matchFrom1To2->push_back(-1);
61 const unsigned nmatches = n1*n2;
62 std::vector<matchOneToOne_MatchInfo> distanceTable(nmatches);
63 std::vector<int> taken2(n2);
65 for (
unsigned i2=0; i2<n2; ++i2)
68 matchOneToOne_MatchInfo* m;
69 for (
unsigned i1=0; i1<n1; ++i1)
70 for (
unsigned i2=0; i2<n2; ++i2)
72 m = &distanceTable[i1*n2+i2];
73 m->distance = calc(v1[i1], v2[i2]);
78 std::sort(distanceTable.begin(), distanceTable.end());
79 for (
unsigned i=0; i<nmatches && nused<n1 && nused<n2; ++i)
81 m = &distanceTable[i];
82 if (m->distance > maxMatchingDistance)
84 if ((*matchFrom1To2)[m->i1] < 0 && !taken2[m->i2])
86 (*matchFrom1To2)[m->i1] =
static_cast<int>(m->i2);
98 #endif // MATCHONETOONE_HH_