33 #ifndef NANOFLANN_HPP_ 34 #define NANOFLANN_HPP_ 45 #if !defined(NOMINMAX) && (defined(_WIN32) || defined(_WIN32_) || defined(WIN32) || defined(_WIN64)) 59 #define NANOFLANN_VERSION 0x113 63 template <
typename DistanceType,
typename IndexType =
size_t,
typename CountType =
size_t>
72 inline KNNResultSet(CountType capacity_) : capacity(capacity_), count(0)
76 inline void init(IndexType* indices_, DistanceType* dists_)
81 dists[capacity-1] = (std::numeric_limits<DistanceType>::max)();
84 inline CountType
size()
const 95 inline void addPoint(DistanceType dist, IndexType index)
98 for (i=count; i>0; --i) {
99 #ifdef NANOFLANN_FIRST_MATCH // If defined and two poins have the same distance, the one with the lowest-index will be returned first. 100 if ( (dists[i-1]>dist) || ((dist==dists[i-1])&&(indices[i-1]>index)) ) {
102 if (dists[i-1]>dist) {
105 dists[i] = dists[i-1];
106 indices[i] = indices[i-1];
115 if (count<capacity) count++;
120 return dists[capacity-1];
128 template <
typename DistanceType,
typename IndexType =
size_t>
136 inline RadiusResultSet(DistanceType radius_, std::vector<std::pair<IndexType,DistanceType> >& indices_dists) : radius(radius_), m_indices_dists(indices_dists)
143 inline void init() { clear(); }
144 inline void clear() { m_indices_dists.clear(); }
146 inline size_t size()
const {
return m_indices_dists.size(); }
148 inline bool full()
const {
return true; }
150 inline void addPoint(DistanceType dist, IndexType index)
153 m_indices_dists.push_back(std::make_pair<IndexType,DistanceType>(index,dist));
156 inline DistanceType
worstDist()
const {
return radius; }
171 if (m_indices_dists.empty())
throw std::runtime_error(
"Cannot invoke RadiusResultSet::worst_item() on an empty list of results.");
172 typedef typename std::vector<std::pair<IndexType,DistanceType> >::const_iterator DistIt;
173 DistIt it = std::max_element(m_indices_dists.begin(), m_indices_dists.end());
182 template <
typename PairType>
183 inline bool operator()(
const PairType &p1,
const PairType &p2)
const {
184 return p1.second < p2.second;
194 void save_value(FILE* stream,
const T& value,
size_t count = 1)
196 fwrite(&value,
sizeof(value),count, stream);
202 size_t size = value.size();
203 fwrite(&size,
sizeof(
size_t), 1, stream);
204 fwrite(&value[0],
sizeof(T), size, stream);
210 size_t read_cnt = fread(&value,
sizeof(value), count, stream);
211 if (read_cnt != count) {
212 throw std::runtime_error(
"Cannot read from file");
221 size_t read_cnt = fread(&size,
sizeof(
size_t), 1, stream);
223 throw std::runtime_error(
"Cannot read from file");
226 read_cnt = fread(&value[0],
sizeof(T), size, stream);
227 if (read_cnt!=size) {
228 throw std::runtime_error(
"Cannot read from file");
237 template<
typename T>
inline T
abs(T x) {
return (x<0) ? -x : x; }
239 template<>
inline float abs<float>(
float x) {
return fabsf(x); }
240 template<>
inline double abs<double>(
double x) {
return fabs(x); }
248 template<
class T,
class DataSource,
typename _DistanceType = T>
256 L1_Adaptor(
const DataSource &_data_source) : data_source(_data_source) { }
258 inline DistanceType
operator()(
const T* a,
const size_t b_idx,
size_t size, DistanceType worst_dist = -1)
const 260 DistanceType result = DistanceType();
261 const T* last = a +
size;
262 const T* lastgroup = last - 3;
266 while (a < lastgroup) {
267 const DistanceType diff0 =
nanoflann::abs(a[0] - data_source.kdtree_get_pt(b_idx,d++));
268 const DistanceType diff1 =
nanoflann::abs(a[1] - data_source.kdtree_get_pt(b_idx,d++));
269 const DistanceType diff2 =
nanoflann::abs(a[2] - data_source.kdtree_get_pt(b_idx,d++));
270 const DistanceType diff3 =
nanoflann::abs(a[3] - data_source.kdtree_get_pt(b_idx,d++));
271 result += diff0 + diff1 + diff2 + diff3;
273 if ((worst_dist>0)&&(result>worst_dist)) {
279 result +=
nanoflann::abs( *a++ - data_source.kdtree_get_pt(b_idx,d++) );
284 template <
typename U,
typename V>
285 inline DistanceType
accum_dist(
const U a,
const V b,
int dim)
const 296 template<
class T,
class DataSource,
typename _DistanceType = T>
304 L2_Adaptor(
const DataSource &_data_source) : data_source(_data_source) { }
306 inline DistanceType
operator()(
const T* a,
const size_t b_idx,
size_t size, DistanceType worst_dist = -1)
const 308 DistanceType result = DistanceType();
309 const T* last = a +
size;
310 const T* lastgroup = last - 3;
314 while (a < lastgroup) {
315 const DistanceType diff0 = a[0] - data_source.kdtree_get_pt(b_idx,d++);
316 const DistanceType diff1 = a[1] - data_source.kdtree_get_pt(b_idx,d++);
317 const DistanceType diff2 = a[2] - data_source.kdtree_get_pt(b_idx,d++);
318 const DistanceType diff3 = a[3] - data_source.kdtree_get_pt(b_idx,d++);
319 result += diff0 * diff0 + diff1 * diff1 + diff2 * diff2 + diff3 * diff3;
321 if ((worst_dist>0)&&(result>worst_dist)) {
327 const DistanceType diff0 = *a++ - data_source.kdtree_get_pt(b_idx,d++);
328 result += diff0 * diff0;
333 template <
typename U,
typename V>
334 inline DistanceType
accum_dist(
const U a,
const V b,
int dim)
const 345 template<
class T,
class DataSource,
typename _DistanceType = T>
355 inline DistanceType
operator()(
const T* a,
const size_t b_idx,
size_t size)
const {
356 return data_source.kdtree_distance(a,b_idx,size);
359 template <
typename U,
typename V>
360 inline DistanceType
accum_dist(
const U a,
const V b,
int dim)
const 368 template<
class T,
class DataSource>
375 template<
class T,
class DataSource>
382 template<
class T,
class DataSource>
400 leaf_max_size(_leaf_max_size), dim(dim_)
411 SearchParams(
int checks_IGNORED_ = 32,
float eps_ = 0,
bool sorted_ =
true ) :
412 eps(eps_), sorted(sorted_) {}
431 template <
typename T>
434 T* mem = (T*) ::malloc(
sizeof(T)*
count);
480 this->blocksize = blocksize;
493 while (base !=
NULL) {
494 void *prev = *((
void**) base);
510 const size_t size = (req_size + (WORDSIZE - 1)) & ~(WORDSIZE - 1);
515 if (size > remaining) {
517 wastedMemory += remaining;
520 const size_t blocksize = (size +
sizeof(
void*) + (WORDSIZE-1) >
BLOCKSIZE) ?
521 size +
sizeof(
void*) + (WORDSIZE-1) : BLOCKSIZE;
524 void* m = ::malloc(blocksize);
526 fprintf(stderr,
"Failed to allocate memory.\n");
531 ((
void**) m)[0] = base;
537 remaining = blocksize -
sizeof(
void*) - shift;
538 loc = ((
char*)m +
sizeof(
void*) + shift);
541 loc = (
char*)loc + size;
556 template <
typename T>
559 T* mem = (T*) this->malloc(
sizeof(T)*
count);
603 template <
typename Distance,
class DatasetAdaptor,
int DIM = -1,
typename IndexType =
size_t>
673 template <
typename T,
typename DistanceType>
680 BranchStruct(
const T& aNode, DistanceType dist) : node(aNode), mindist(dist) {}
682 inline bool operator<(const BranchStruct<T, DistanceType>& rhs)
const 684 return mindist<rhs.mindist;
718 dataset(inputData), index_params(params), distance(inputData)
720 m_size = dataset.kdtree_get_point_count();
721 dim = dimensionality;
724 if (params.dim>0) dim = params.
dim;
726 m_leaf_max_size = params.leaf_max_size;
745 computeBoundingBox(root_bbox);
746 root_node = divideTree(0, m_size, root_bbox );
762 return static_cast<size_t>(DIM>0 ? DIM : dim);
788 template <
typename RESULTSET>
792 float epsError = 1+searchParams.
eps;
794 std::vector<DistanceType>
dists( (DIM>0 ? DIM : dim) ,0);
795 DistanceType distsq = computeInitialDistances(vec, dists);
796 searchLevel(result, vec, root_node, distsq, dists, epsError);
805 inline void knnSearch(
const ElementType *query_point,
const size_t num_closest, IndexType *out_indices, DistanceType *out_distances_sq,
const int nChecks_IGNORED = 10)
const 808 resultSet.
init(out_indices, out_distances_sq);
824 size_t radiusSearch(
const ElementType *query_point,
const DistanceType radius, std::vector<std::pair<IndexType,DistanceType> >& IndicesDists,
const SearchParams& searchParams)
const 827 this->findNeighbors(resultSet, query_point, searchParams);
832 return resultSet.
size();
842 m_size = dataset.kdtree_get_point_count();
843 if (vind.size()!=m_size)
846 for (
size_t i = 0; i < m_size; i++) vind[i] = i;
852 return dataset.kdtree_get_pt(idx,component);
860 save_tree(stream, tree->
child1);
863 save_tree(stream, tree->
child2);
873 load_tree(stream, tree->
child1);
876 load_tree(stream, tree->
child2);
883 bbox.resize((DIM>0 ? DIM : dim));
884 if (dataset.kdtree_get_bbox(bbox))
890 for (
int i=0; i<(DIM>0 ? DIM : dim); ++i) {
892 bbox[i].high = dataset_get(0,i);
894 const size_t N = dataset.kdtree_get_point_count();
895 for (
size_t k=1; k<N; ++k) {
896 for (
int i=0; i<(DIM>0 ? DIM : dim); ++i) {
897 if (dataset_get(k,i)<bbox[i].low) bbox[i].low = dataset_get(k,i);
898 if (dataset_get(k,i)>bbox[i].high) bbox[i].high = dataset_get(k,i);
914 NodePtr
divideTree(
const IndexType left,
const IndexType right, BoundingBox& bbox)
919 if ( (right-left) <= m_leaf_max_size) {
921 node->
lr.left = left;
922 node->
lr.right = right;
925 for (
int i=0; i<(DIM>0 ? DIM : dim); ++i) {
926 bbox[i].low = dataset_get(vind[left],i);
927 bbox[i].high = dataset_get(vind[left],i);
929 for (IndexType k=left+1; k<right; ++k) {
930 for (
int i=0; i<(DIM>0 ? DIM : dim); ++i) {
931 if (bbox[i].low>dataset_get(vind[k],i)) bbox[i].low=dataset_get(vind[k],i);
932 if (bbox[i].high<dataset_get(vind[k],i)) bbox[i].high=dataset_get(vind[k],i);
940 middleSplit_(&vind[0]+left, right-left, idx, cutfeat, cutval, bbox);
942 node->
sub.divfeat = cutfeat;
944 BoundingBox left_bbox(bbox);
945 left_bbox[cutfeat].high = cutval;
946 node->
child1 = divideTree(left, left+idx, left_bbox);
948 BoundingBox right_bbox(bbox);
949 right_bbox[cutfeat].low = cutval;
950 node->
child2 = divideTree(left+idx, right, right_bbox);
952 node->
sub.divlow = left_bbox[cutfeat].high;
953 node->
sub.divhigh = right_bbox[cutfeat].low;
955 for (
int i=0; i<(DIM>0 ? DIM : dim); ++i) {
956 bbox[i].low = std::min(left_bbox[i].low, right_bbox[i].low);
957 bbox[i].high = std::max(left_bbox[i].high, right_bbox[i].high);
964 void computeMinMax(IndexType* ind, IndexType count,
int element, ElementType& min_elem, ElementType& max_elem)
966 min_elem = dataset_get(ind[0],element);
967 max_elem = dataset_get(ind[0],element);
968 for (IndexType i=1; i<
count; ++i) {
969 ElementType val = dataset_get(ind[i],element);
970 if (val<min_elem) min_elem = val;
971 if (val>max_elem) max_elem = val;
975 void middleSplit(IndexType* ind, IndexType count, IndexType& index,
int& cutfeat, DistanceType& cutval,
const BoundingBox& bbox)
978 ElementType max_span = bbox[0].high-bbox[0].low;
980 cutval = (bbox[0].high+bbox[0].low)/2;
981 for (
int i=1; i<(DIM>0 ? DIM : dim); ++i) {
982 ElementType span = bbox[i].low-bbox[i].low;
986 cutval = (bbox[i].high+bbox[i].low)/2;
991 ElementType min_elem, max_elem;
992 computeMinMax(ind, count, cutfeat, min_elem, max_elem);
993 cutval = (min_elem+max_elem)/2;
994 max_span = max_elem - min_elem;
998 for (
size_t i=0; i<(DIM>0 ? DIM : dim); ++i) {
1000 ElementType span = bbox[i].high-bbox[i].low;
1001 if (span>max_span) {
1002 computeMinMax(ind, count, i, min_elem, max_elem);
1003 span = max_elem - min_elem;
1004 if (span>max_span) {
1007 cutval = (min_elem+max_elem)/2;
1011 IndexType lim1, lim2;
1012 planeSplit(ind, count, cutfeat, cutval, lim1, lim2);
1014 if (lim1>count/2) index = lim1;
1015 else if (lim2<count/2) index = lim2;
1016 else index = count/2;
1020 void middleSplit_(IndexType* ind, IndexType count, IndexType& index,
int& cutfeat, DistanceType& cutval,
const BoundingBox& bbox)
1022 const DistanceType EPS=
static_cast<DistanceType
>(0.00001);
1023 ElementType max_span = bbox[0].high-bbox[0].low;
1024 for (
int i=1; i<(DIM>0 ? DIM : dim); ++i) {
1025 ElementType span = bbox[i].high-bbox[i].low;
1026 if (span>max_span) {
1030 ElementType max_spread = -1;
1032 for (
int i=0; i<(DIM>0 ? DIM : dim); ++i) {
1033 ElementType span = bbox[i].high-bbox[i].low;
1034 if (span>(1-EPS)*max_span) {
1035 ElementType min_elem, max_elem;
1036 computeMinMax(ind, count, cutfeat, min_elem, max_elem);
1037 ElementType spread = max_elem-min_elem;;
1038 if (spread>max_spread) {
1040 max_spread = spread;
1045 DistanceType split_val = (bbox[cutfeat].low+bbox[cutfeat].high)/2;
1046 ElementType min_elem, max_elem;
1047 computeMinMax(ind, count, cutfeat, min_elem, max_elem);
1049 if (split_val<min_elem) cutval = min_elem;
1050 else if (split_val>max_elem) cutval = max_elem;
1051 else cutval = split_val;
1053 IndexType lim1, lim2;
1054 planeSplit(ind, count, cutfeat, cutval, lim1, lim2);
1056 if (lim1>count/2) index = lim1;
1057 else if (lim2<count/2) index = lim2;
1058 else index = count/2;
1071 void planeSplit(IndexType* ind,
const IndexType count,
int cutfeat, DistanceType cutval, IndexType& lim1, IndexType& lim2)
1075 IndexType right = count-1;
1077 while (left<=right && dataset_get(ind[left],cutfeat)<cutval) ++left;
1078 while (right && left<=right && dataset_get(ind[right],cutfeat)>=cutval) --right;
1079 if (left>right || !right)
break;
1090 while (left<=right && dataset_get(ind[left],cutfeat)<=cutval) ++left;
1091 while (right && left<=right && dataset_get(ind[right],cutfeat)>cutval) --right;
1092 if (left>right || !right)
break;
1103 DistanceType distsq = 0.0;
1105 for (
int i = 0; i < (DIM>0 ? DIM : dim); ++i) {
1106 if (vec[i] < root_bbox[i].low) {
1107 dists[i] = distance.accum_dist(vec[i], root_bbox[i].low, i);
1110 if (vec[i] > root_bbox[i].high) {
1111 dists[i] = distance.accum_dist(vec[i], root_bbox[i].high, i);
1123 template <
class RESULTSET>
1124 void searchLevel(RESULTSET& result_set,
const ElementType* vec,
const NodePtr node, DistanceType mindistsq,
1125 std::vector<DistanceType>& dists,
const float epsError)
const 1130 DistanceType worst_dist = result_set.worstDist();
1131 for (IndexType i=node->
lr.left; i<node->lr.right; ++i) {
1132 const IndexType index = vind[i];
1133 DistanceType dist = distance(vec, index, (DIM>0 ? DIM : dim));
1134 if (dist<worst_dist) {
1135 result_set.addPoint(dist,vind[i]);
1142 int idx = node->
sub.divfeat;
1143 ElementType val = vec[idx];
1144 DistanceType diff1 = val - node->
sub.divlow;
1145 DistanceType diff2 = val - node->
sub.divhigh;
1149 DistanceType cut_dist;
1150 if ((diff1+diff2)<0) {
1151 bestChild = node->
child1;
1152 otherChild = node->
child2;
1153 cut_dist = distance.accum_dist(val, node->
sub.divhigh, idx);
1156 bestChild = node->
child2;
1157 otherChild = node->
child1;
1158 cut_dist = distance.accum_dist( val, node->
sub.divlow, idx);
1162 searchLevel(result_set, vec, bestChild, mindistsq, dists, epsError);
1164 DistanceType dst = dists[idx];
1165 mindistsq = mindistsq + cut_dist - dst;
1166 dists[idx] = cut_dist;
1167 if (mindistsq*epsError<=result_set.worstDist()) {
1168 searchLevel(result_set, vec, otherChild, mindistsq, dists, epsError);
1181 save_tree(stream, root_node);
1191 load_tree(stream, root_node);
1216 template <
class MatrixType,
int DIM = -1,
class Distance =
nanoflann::metric_L2,
typename IndexType =
size_t>
1220 typedef typename MatrixType::Scalar
num_t;
1221 typedef typename Distance::template traits<num_t,self_t>::distance_t
metric_t;
1229 const size_t dims = mat.cols();
1230 if (DIM>0 && static_cast<int>(dims)!=DIM)
1231 throw std::runtime_error(
"Data set dimensionality does not match the 'DIM' template argument");
1247 inline void query(
const num_t *query_point,
const size_t num_closest, IndexType *out_indices, num_t *out_distances_sq,
const int nChecks_IGNORED = 10)
const 1250 resultSet.
init(out_indices, out_distances_sq);
1266 return m_data_matrix.rows();
1273 for (
size_t i=0; i<
size; i++) {
1274 const num_t d= p1[i]-m_data_matrix.coeff(idx_p2,i);
1282 return m_data_matrix.coeff(idx,dim);
1288 template <
class BBOX>
long double abs< long double >(long double x)
void findNeighbors(RESULTSET &result, const ElementType *vec, const SearchParams &searchParams) const
const MatrixType & m_data_matrix
std::vector< IndexType > vind
void computeBoundingBox(BoundingBox &bbox)
const DistanceType radius
void planeSplit(IndexType *ind, const IndexType count, int cutfeat, DistanceType cutval, IndexType &lim1, IndexType &lim2)
L2_Adaptor< T, DataSource > distance_t
double abs< double >(double x)
Distance::ElementType ElementType
L1_Adaptor< T, DataSource > distance_t
NodePtr divideTree(const IndexType left, const IndexType right, BoundingBox &bbox)
void computeMinMax(IndexType *ind, IndexType count, int element, ElementType &min_elem, ElementType &max_elem)
KDTreeSingleIndexAdaptor(const int dimensionality, const DatasetAdaptor &inputData, const KDTreeSingleIndexAdaptorParams ¶ms=KDTreeSingleIndexAdaptorParams())
std::pair< IndexType, DistanceType > worst_item() const
L2_Simple_Adaptor(const DataSource &_data_source)
RadiusResultSet(DistanceType radius_, std::vector< std::pair< IndexType, DistanceType > > &indices_dists)
Distance::template traits< num_t, self_t >::distance_t metric_t
L1_Adaptor(const DataSource &_data_source)
void middleSplit_(IndexType *ind, IndexType count, IndexType &index, int &cutfeat, DistanceType &cutval, const BoundingBox &bbox)
DistanceType accum_dist(const U a, const V b, int dim) const
num_t kdtree_get_pt(const size_t idx, int dim) const
KDTreeEigenMatrixAdaptor< MatrixType, DIM, Distance > self_t
~KDTreeSingleIndexAdaptor()
void addPoint(DistanceType dist, IndexType index)
const DataSource & data_source
void load_value(FILE *stream, T &value, size_t count=1)
void addPoint(DistanceType dist, IndexType index)
const DatasetAdaptor & dataset
The source of our data.
size_t kdtree_get_point_count() const
__kf_hdevice__ void swap(T &a, T &b)
void save_tree(FILE *stream, NodePtr tree)
void saveIndex(FILE *stream)
DistanceType accum_dist(const U a, const V b, int dim) const
int checks
Ignored parameter (Kept for compatibility with the FLANN interface).
void loadIndex(FILE *stream)
void middleSplit(IndexType *ind, IndexType count, IndexType &index, int &cutfeat, DistanceType &cutval, const BoundingBox &bbox)
ElementType dataset_get(size_t idx, int component) const
Helper accessor to the dataset points:
const DataSource & data_source
~KDTreeEigenMatrixAdaptor()
int dim
Dimensionality of each data point.
BranchStruct(const T &aNode, DistanceType dist)
T * allocate(const size_t count=1)
DistanceType operator()(const T *a, const size_t b_idx, size_t size, DistanceType worst_dist=-1) const
void load_tree(FILE *stream, NodePtr &tree)
void searchLevel(RESULTSET &result_set, const ElementType *vec, const NodePtr node, DistanceType mindistsq, std::vector< DistanceType > &dists, const float epsError) const
T * allocate(size_t count=1)
void query(const num_t *query_point, const size_t num_closest, IndexType *out_indices, num_t *out_distances_sq, const int nChecks_IGNORED=10) const
void init(IndexType *indices_, DistanceType *dists_)
DistanceType operator()(const T *a, const size_t b_idx, size_t size, DistanceType worst_dist=-1) const
void set_radius_and_clear(const DistanceType r)
struct nanoflann::KDTreeSingleIndexAdaptor::Node::@15::@17 lr
_DistanceType DistanceType
_DistanceType DistanceType
DistanceType computeInitialDistances(const ElementType *vec, std::vector< DistanceType > &dists) const
const KDTreeSingleIndexAdaptorParams index_params
bool kdtree_get_bbox(BBOX &bb) const
_DistanceType DistanceType
L2_Adaptor(const DataSource &_data_source)
void * malloc(const size_t req_size)
KDTreeSingleIndexAdaptor< metric_t, self_t, DIM, IndexType > index_t
struct nanoflann::KDTreeSingleIndexAdaptor::Node::@15::@18 sub
L2_Simple_Adaptor< T, DataSource > distance_t
std::vector< Interval > BoundingBox
PooledAllocator(const size_t blocksize=BLOCKSIZE)
const self_t & derived() const
DistanceType operator()(const T *a, const size_t b_idx, size_t size) const
DistanceType worstDist() const
const DataSource & data_source
size_t usedMemory() const
std::vector< std::pair< IndexType, DistanceType > > & m_indices_dists
void save_value(FILE *stream, const T &value, size_t count=1)
bool sorted
only for radius search, require neighbours sorted by distance (default: true)
SearchParams(int checks_IGNORED_=32, float eps_=0, bool sorted_=true)
KDTreeEigenMatrixAdaptor(const int dimensionality, const MatrixType &mat, const int leaf_max_size=10)
The kd-tree index for the user to call its methods as usual with any other FLANN index.
bool operator()(const PairType &p1, const PairType &p2) const
KDTreeSingleIndexAdaptorParams(size_t _leaf_max_size=10, int dim_=-1)
Distance::DistanceType DistanceType
KNNResultSet(CountType capacity_)
float abs< float >(float x)
float eps
search for eps-approximate neighbours (default: 0)
DistanceType accum_dist(const U a, const V b, int dim) const
DistanceType worstDist() const
num_t kdtree_distance(const num_t *p1, const size_t idx_p2, size_t size) const
size_t radiusSearch(const ElementType *query_point, const DistanceType radius, std::vector< std::pair< IndexType, DistanceType > > &IndicesDists, const SearchParams &searchParams) const
void knnSearch(const ElementType *query_point, const size_t num_closest, IndexType *out_indices, DistanceType *out_distances_sq, const int nChecks_IGNORED=10) const
BranchStruct< NodePtr, DistanceType > BranchSt