31 #ifndef RTABMAP_FLANN_RESULTSET_H 32 #define RTABMAP_FLANN_RESULTSET_H 49 template <
typename T,
typename DistanceType>
56 BranchStruct(
const T& aNode, DistanceType dist) : node(aNode), mindist(dist) {}
58 bool operator<(const BranchStruct<T, DistanceType>& rhs)
const 60 return mindist<rhs.mindist;
65 template <
typename DistanceType>
69 dist_(dist), index_(index)
74 return (dist_ < dist_index.
dist_) || ((dist_ == dist_index.
dist_) && index_ < dist_index.
index_);
81 template <
typename DistanceType>
87 virtual bool full()
const = 0;
89 virtual void addPoint(DistanceType dist,
size_t index) = 0;
91 virtual DistanceType worstDist()
const = 0;
100 template <
typename DistanceType>
124 dist_index_[capacity_-1].dist_ = worst_distance_;
143 return count_==capacity_;
153 if (dist>=worst_distance_)
return;
155 if (count_ < capacity_) ++count_;
157 for (i=count_-1; i>0; --i) {
158 #ifdef FLANN_FIRST_MATCH 159 if ( (dist_index_[i-1].dist_>dist) || ((dist==dist_index_[i-1].dist_)&&(dist_index_[i-1].index_>index)) )
161 if (dist_index_[i-1].dist_>dist)
164 dist_index_[i] = dist_index_[i-1];
168 dist_index_[i].dist_ = dist;
169 dist_index_[i].index_ = index;
170 worst_distance_ = dist_index_[capacity_-1].dist_;
180 void copy(
size_t* indices, DistanceType* dists,
size_t num_elements,
bool sorted =
true)
182 size_t n =
std::min(count_, num_elements);
183 for (
size_t i=0; i<n; ++i) {
184 *indices++ = dist_index_[i].index_;
185 *dists++ = dist_index_[i].dist_;
191 return worst_distance_;
204 template <
typename DistanceType>
228 dist_index_[capacity_-1].dist_ = worst_distance_;
239 return count_ == capacity_;
245 if (dist >= worst_distance_)
return;
247 for (i = count_; i > 0; --i) {
248 #ifdef FLANN_FIRST_MATCH 249 if ( (dist_index_[i-1].dist_<=dist) && ((dist!=dist_index_[i-1].dist_)||(dist_index_[i-1].index_<=index)) )
251 if (dist_index_[i-1].dist_<=dist)
255 for (
size_t j = i - 1; dist_index_[j].dist_ == dist && j--;) {
256 if (dist_index_[j].index_ == index) {
263 if (count_ < capacity_) ++count_;
264 for (
size_t j = count_-1; j > i; --j) {
265 dist_index_[j] = dist_index_[j-1];
267 dist_index_[i].dist_ = dist;
268 dist_index_[i].index_ = index;
269 worst_distance_ = dist_index_[capacity_-1].dist_;
279 void copy(
size_t* indices, DistanceType* dists,
size_t num_elements,
bool sorted =
true)
281 size_t n =
std::min(count_, num_elements);
282 for (
size_t i=0; i<n; ++i) {
283 *indices++ = dist_index_[i].index_;
284 *dists++ = dist_index_[i].dist_;
290 return worst_distance_;
303 template <
typename DistanceType>
313 dist_index_.reserve(capacity_);
337 return dist_index_.size();
357 if (dist>=worst_dist_)
return;
359 if (dist_index_.size()==capacity_) {
361 std::pop_heap(dist_index_.begin(), dist_index_.end());
362 dist_index_.pop_back();
366 dist_index_.push_back(DistIndex(dist,index));
368 std::push_heap(dist_index_.begin(), dist_index_.end());
371 if (dist_index_.size()==capacity_) {
373 std::make_heap(dist_index_.begin(), dist_index_.end());
377 worst_dist_ = dist_index_[0].dist_;
388 void copy(
size_t* indices, DistanceType* dists,
size_t num_elements,
bool sorted =
true)
393 std::sort(dist_index_.begin(), dist_index_.end());
396 if (num_elements<size()) {
397 std::nth_element(dist_index_.begin(), dist_index_.begin()+num_elements, dist_index_.end());
401 size_t n =
std::min(dist_index_.size(), num_elements);
402 for (
size_t i=0; i<n; ++i) {
403 *indices++ = dist_index_[i].index_;
404 *dists++ = dist_index_[i].dist_;
425 template <
typename DistanceType>
435 dist_index_.reserve(1024);
457 return dist_index_.size();
479 dist_index_.push_back(DistIndex(dist,index));
490 void copy(
size_t* indices, DistanceType* dists,
size_t num_elements,
bool sorted =
true)
495 std::sort(dist_index_.begin(), dist_index_.end());
498 if (num_elements<size()) {
499 std::nth_element(dist_index_.begin(), dist_index_.begin()+num_elements, dist_index_.end());
503 size_t n =
std::min(dist_index_.size(), num_elements);
504 for (
size_t i=0; i<n; ++i) {
505 *indices++ = dist_index_[i].index_;
506 *dists++ = dist_index_[i].dist_;
526 template <
typename DistanceType>
533 radius_(radius_), capacity_(capacity_)
536 dist_index_.reserve(capacity_);
550 worst_dist_ = radius_;
560 return dist_index_.size();
580 if (dist>=worst_dist_)
return;
582 if (dist_index_.size()==capacity_) {
584 std::pop_heap(dist_index_.begin(), dist_index_.end());
585 dist_index_.pop_back();
589 dist_index_.push_back(DistIndex(dist,index));
591 std::push_heap(dist_index_.begin(), dist_index_.end());
594 if (dist_index_.size()==capacity_) {
597 std::make_heap(dist_index_.begin(), dist_index_.end());
601 worst_dist_ = dist_index_[0].dist_;
612 void copy(
size_t* indices, DistanceType* dists,
size_t num_elements,
bool sorted =
true)
617 std::sort(dist_index_.begin(), dist_index_.end());
620 if (num_elements<size()) {
621 std::nth_element(dist_index_.begin(), dist_index_.begin()+num_elements, dist_index_.end());
625 size_t n =
std::min(dist_index_.size(), num_elements);
626 for (
size_t i=0; i<n; ++i) {
627 *indices++ = dist_index_[i].index_;
628 *dists++ = dist_index_[i].dist_;
651 template <
typename DistanceType>
703 template<
typename DistanceType>
710 dist_(dist), index_(index)
715 return (dist_ < dist_index.
dist_) || ((dist_ == dist_index.
dist_) && index_ < dist_index.
index_);
723 worst_distance_(
std::numeric_limits<DistanceType>::
max())
740 void copy(
size_t* indices, DistanceType* dist,
int n_neighbors,
bool sorted =
true)
742 if (n_neighbors<0) n_neighbors = dist_indices_.size();
744 typedef typename std::set<DistIndex>::const_iterator Iterator;
745 for (Iterator dist_index = dist_indices_.begin(), dist_index_end =
746 dist_indices_.end(); (dist_index != dist_index_end) && (i < n_neighbors); ++dist_index, ++indices, ++dist, ++i) {
747 *indices = dist_index->index_;
748 *dist = dist_index->dist_;
757 return dist_indices_.size();
766 return worst_distance_;
784 template<
typename DistanceType>
793 this->is_full_ =
false;
801 inline void addPoint(DistanceType dist,
size_t index)
804 if (dist >= worst_distance_)
return;
805 dist_indices_.insert(
DistIndex(dist, index));
808 if (dist_indices_.size() > capacity_) {
809 dist_indices_.erase(*dist_indices_.rbegin());
810 worst_distance_ = dist_indices_.rbegin()->dist_;
813 else if (dist_indices_.size() == capacity_) {
815 worst_distance_ = dist_indices_.rbegin()->dist_;
823 dist_indices_.clear();
843 template<
typename DistanceType>
862 if (dist < radius_) dist_indices_.insert(
DistIndex(dist, index));
869 dist_indices_.clear();
902 template<
typename DistanceType>
911 this->radius_ = radius;
919 dist_indices_.clear();
920 worst_distance_ = radius_;
933 #endif //FLANN_RESULTSET_H
DistanceIndex< DistanceType > DistIndex
void addPoint(DistanceType dist, size_t index)
DistIndex(DistanceType dist, unsigned int index)
void copy(size_t *indices, DistanceType *dist, int n_neighbors, bool sorted=true)
DistanceType worstDist() const
KNNUniqueResultSet(unsigned int capacity)
RadiusUniqueResultSet(DistanceType radius)
void copy(size_t *indices, DistanceType *dists, size_t num_elements, bool sorted=true)
DistanceType worstDist() const
void copy(size_t *indices, DistanceType *dists, size_t num_elements, bool sorted=true)
GLM_FUNC_DECL genType min(genType const &x, genType const &y)
DistanceType worst_distance_
void addPoint(DistanceType dist, size_t index)
std::set< DistIndex > dist_indices_
DistanceIndex< DistanceType > DistIndex
DistanceType worst_distance_
KNNRadiusUniqueResultSet(DistanceType radius, size_t capacity)
KNNSimpleResultSet(size_t capacity_)
DistanceIndex< DistanceType > DistIndex
RadiusResultSet(DistanceType radius_)
CountRadiusResultSet(DistanceType radius_)
void addPoint(DistanceType dist, size_t index)
std::vector< DistIndex > dist_index_
BranchStruct(const T &aNode, DistanceType dist)
std::vector< DistIndex > dist_index_
DistanceType worstDist() const
KNNResultSet(int capacity)
DistanceIndex< DistanceType > DistIndex
DistanceType worstDist() const
DistanceType worstDist() const
std::vector< DistIndex > dist_index_
void addPoint(DistanceType dist, size_t index)
KNNResultSet2(size_t capacity_)
void addPoint(DistanceType dist, size_t index)
void addPoint(DistanceType dist, size_t index)
DistanceIndex< DistanceType > DistIndex
void copy(size_t *indices, DistanceType *dists, size_t num_elements, bool sorted=true)
UniqueResultSet< DistanceType >::DistIndex DistIndex
void copy(size_t *indices, DistanceType *dists, size_t num_elements, bool sorted=true)
std::vector< DistIndex > dist_index_
bool operator<(const DistIndex dist_index) const
GLM_FUNC_DECL genType max(genType const &x, genType const &y)
UniqueResultSet< DistanceType >::DistIndex DistIndex
DistanceType worstDist() const
void addPoint(DistanceType dist, size_t index)
KNNRadiusResultSet(DistanceType radius_, size_t capacity_)
DistanceType worstDist() const
void addPoint(DistanceType dist, size_t index)
void copy(size_t *indices, DistanceType *dists, size_t num_elements, bool sorted=true)
std::vector< DistIndex > dist_index_
DistanceType worstDist() const
bool operator<(const DistanceIndex &dist_index) const
DistanceIndex(DistanceType dist, size_t index)
DistanceType worst_distance_