31 #ifndef OPENCV_FLANN_RESULTSET_H
32 #define OPENCV_FLANN_RESULTSET_H
43 #include "opencv2/core/base.hpp"
44 #include "opencv2/core/cvdef.h"
54 template <
typename T,
typename DistanceType>
61 BranchStruct(
const T& aNode, DistanceType dist) : node(aNode), mindist(dist) {}
63 bool operator<(
const BranchStruct<T, DistanceType>& rhs)
const
65 return mindist<rhs.mindist;
70 template <
typename DistanceType>
74 virtual ~ResultSet() {}
76 virtual bool full()
const = 0;
78 virtual void addPoint(DistanceType dist,
int index) = 0;
80 virtual DistanceType worstDist()
const = 0;
89 template <
typename DistanceType>
90 class KNNSimpleResultSet :
public ResultSet<DistanceType>
96 DistanceType worst_distance_;
99 KNNSimpleResultSet(
int capacity_) : capacity(capacity_),
count(0)
103 void init(
int* indices_, DistanceType* dists_)
109 dists[capacity-1] = worst_distance_;
119 return count == capacity;
125 if (dist >= worst_distance_)
return;
127 for (i=
count; i>0; --i) {
128 #ifdef FLANN_FIRST_MATCH
129 if ( (dists[i-1]>dist) || ((dist==dists[i-1])&&(indices[i-1]>
index)) )
135 dists[i] = dists[i-1];
136 indices[i] = indices[i-1];
144 worst_distance_ = dists[capacity-1];
149 return worst_distance_;
156 template <
typename DistanceType>
157 class KNNResultSet :
public ResultSet<DistanceType>
163 DistanceType worst_distance_;
166 KNNResultSet(
int capacity_)
167 : indices(NULL), dists(NULL), capacity(capacity_),
count(0), worst_distance_(0)
171 void init(
int* indices_, DistanceType* dists_)
177 dists[capacity-1] = worst_distance_;
187 return count == capacity;
195 if (dist >= worst_distance_)
return;
197 for (i =
count; i > 0; --i) {
198 #ifdef FLANN_FIRST_MATCH
199 if ( (dists[i-1]<=dist) && ((dist!=dists[i-1])||(indices[i-1]<=
index)) )
201 if (dists[i-1]<=dist)
205 for (
int j = i; dists[j] == dist && j--;) {
206 if (indices[j] ==
index) {
215 for (
int j =
count-1; j > i; --j) {
216 dists[j] = dists[j-1];
217 indices[j] = indices[j-1];
221 worst_distance_ = dists[capacity-1];
226 return worst_distance_;
234 template <
typename DistanceType>
235 class RadiusResultSet :
public ResultSet<DistanceType>
244 RadiusResultSet(DistanceType radius_,
int* indices_, DistanceType* dists_,
int capacity_) :
245 radius(radius_), indices(indices_), dists(dists_), capacity(capacity_)
269 void addPoint(DistanceType dist,
int index)
272 if ((capacity>0)&&(
count < capacity)) {
280 DistanceType worstDist()
const
292 template<
typename DistanceType>
293 class UniqueResultSet :
public ResultSet<DistanceType>
298 DistIndex(DistanceType dist,
unsigned int index) :
299 dist_(dist), index_(
index)
302 bool operator<(
const DistIndex dist_index)
const
304 return (dist_ < dist_index.dist_) || ((dist_ == dist_index.dist_) && index_ < dist_index.index_);
312 is_full_(false), worst_distance_(
std::numeric_limits<DistanceType>::
max())
326 virtual void clear() = 0;
333 virtual void copy(
int* indices, DistanceType* dist,
int n_neighbors = -1)
const
335 if (n_neighbors < 0) {
337 dist_indices_.end(); dist_index != dist_index_end; ++dist_index, ++indices, ++dist) {
338 *indices = dist_index->index_;
339 *dist = dist_index->dist_;
345 dist_indices_.end(); (dist_index != dist_index_end) && (i < n_neighbors); ++dist_index, ++indices, ++dist, ++i) {
346 *indices = dist_index->index_;
347 *dist = dist_index->dist_;
357 virtual void sortAndCopy(
int* indices, DistanceType* dist,
int n_neighbors = -1)
const
359 copy(indices, dist, n_neighbors);
366 return dist_indices_.size();
374 return worst_distance_;
381 DistanceType worst_distance_;
392 template<
typename DistanceType>
393 class KNNUniqueResultSet :
public UniqueResultSet<DistanceType>
399 KNNUniqueResultSet(
unsigned int capacity) : capacity_(capacity)
401 this->is_full_ =
false;
412 if (dist >= worst_distance_)
return;
413 dist_indices_.insert(DistIndex(dist,
index));
416 if (dist_indices_.size() > capacity_) {
417 dist_indices_.erase(*dist_indices_.rbegin());
418 worst_distance_ = dist_indices_.rbegin()->dist_;
421 else if (dist_indices_.size() == capacity_) {
423 worst_distance_ = dist_indices_.rbegin()->dist_;
431 dist_indices_.clear();
437 typedef typename UniqueResultSet<DistanceType>::DistIndex DistIndex;
438 using UniqueResultSet<DistanceType>::is_full_;
439 using UniqueResultSet<DistanceType>::worst_distance_;
440 using UniqueResultSet<DistanceType>::dist_indices_;
443 unsigned int capacity_;
451 template<
typename DistanceType>
452 class RadiusUniqueResultSet :
public UniqueResultSet<DistanceType>
458 RadiusUniqueResultSet(DistanceType
radius) :
470 if (dist <= radius_) dist_indices_.insert(DistIndex(dist,
index));
477 dist_indices_.clear();
497 typedef typename UniqueResultSet<DistanceType>::DistIndex DistIndex;
498 using UniqueResultSet<DistanceType>::dist_indices_;
499 using UniqueResultSet<DistanceType>::is_full_;
502 DistanceType radius_;
509 template<
typename DistanceType>
510 class KNNRadiusUniqueResultSet :
public KNNUniqueResultSet<DistanceType>
517 KNNRadiusUniqueResultSet(
unsigned int capacity, DistanceType
radius)
519 this->capacity_ = capacity;
521 this->dist_indices_.reserve(capacity_);
529 dist_indices_.clear();
530 worst_distance_ = radius_;
534 using KNNUniqueResultSet<DistanceType>::dist_indices_;
535 using KNNUniqueResultSet<DistanceType>::is_full_;
536 using KNNUniqueResultSet<DistanceType>::worst_distance_;
539 unsigned int capacity_;
542 DistanceType radius_;
InputArrayOfArrays InputArrayOfArrays InputOutputArray InputOutputArray InputOutputArray InputOutputArray Size InputOutputArray InputOutputArray T
Definition: calib3d.hpp:1867
int index
Definition: core_c.h:634
CvSize size
Definition: core_c.h:112
int count
Definition: core_c.h:1413
softfloat max(const softfloat &a, const softfloat &b)
Definition: softfloat.hpp:440
#define CV_OVERRIDE
Definition: cvdef.h:792
#define CV_DbgAssert(expr)
Definition: base.hpp:375
CvPoint2D32f float * radius
Definition: imgproc_c.h:534