-
Notifications
You must be signed in to change notification settings - Fork 98
/
nanoflann.hpp
1323 lines (1137 loc) · 41.9 KB
/
nanoflann.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************************
* Software License Agreement (BSD License)
*
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
* Copyright 2011-2013 Jose Luis Blanco (joseluisblancoc@gmail.com).
* All rights reserved.
*
* THE BSD LICENSE
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*************************************************************************/
#ifndef NANOFLANN_HPP_
#define NANOFLANN_HPP_
#include <vector>
#include <cassert>
#include <algorithm>
#include <stdexcept>
#include <cstdio> // for fwrite()
#include <cmath> // for fabs(),...
#include <limits>
// Avoid conflicting declaration of min/max macros in windows headers
#if !defined(NOMINMAX) && (defined(_WIN32) || defined(_WIN32_) || defined(WIN32) || defined(_WIN64))
# define NOMINMAX
# ifdef max
# undef max
# undef min
# endif
#endif
namespace nanoflann
{
/** @addtogroup nanoflann_grp nanoflann C++ library for ANN
* @{ */
/** Library version: 0xMmP (M=Major,m=minor,P=path) */
#define NANOFLANN_VERSION 0x116
/** @addtogroup result_sets_grp Result set classes
* @{ */
template <typename DistanceType, typename IndexType = size_t, typename CountType = size_t>
class KNNResultSet
{
IndexType * indices;
DistanceType* dists;
CountType capacity;
CountType count;
public:
inline KNNResultSet(CountType capacity_) : capacity(capacity_), count(0)
{
}
inline void init(IndexType* indices_, DistanceType* dists_)
{
indices = indices_;
dists = dists_;
count = 0;
dists[capacity-1] = (std::numeric_limits<DistanceType>::max)();
}
inline CountType size() const
{
return count;
}
inline bool full() const
{
return count == capacity;
}
inline void addPoint(DistanceType dist, IndexType index)
{
CountType i;
for (i=count; i>0; --i) {
#ifdef NANOFLANN_FIRST_MATCH // If defined and two poins have the same distance, the one with the lowest-index will be returned first.
if ( (dists[i-1]>dist) || ((dist==dists[i-1])&&(indices[i-1]>index)) ) {
#else
if (dists[i-1]>dist) {
#endif
if (i<capacity) {
dists[i] = dists[i-1];
indices[i] = indices[i-1];
}
}
else break;
}
if (i<capacity) {
dists[i] = dist;
indices[i] = index;
}
if (count<capacity) count++;
}
inline DistanceType worstDist() const
{
return dists[capacity-1];
}
};
/**
* A result-set class used when performing a radius based search.
*/
template <typename DistanceType, typename IndexType = size_t>
class RadiusResultSet
{
public:
const DistanceType radius;
std::vector<std::pair<IndexType,DistanceType> >& m_indices_dists;
inline RadiusResultSet(DistanceType radius_, std::vector<std::pair<IndexType,DistanceType> >& indices_dists) : radius(radius_), m_indices_dists(indices_dists)
{
init();
}
inline ~RadiusResultSet() { }
inline void init() { clear(); }
inline void clear() { m_indices_dists.clear(); }
inline size_t size() const { return m_indices_dists.size(); }
inline bool full() const { return true; }
inline void addPoint(DistanceType dist, IndexType index)
{
if (dist<radius)
m_indices_dists.push_back(std::pair<IndexType,DistanceType>(index,dist));
}
inline DistanceType worstDist() const { return radius; }
/** Clears the result set and adjusts the search radius. */
inline void set_radius_and_clear( const DistanceType r )
{
radius = r;
clear();
}
/**
* Find the worst result (furtherest neighbor) without copying or sorting
* Pre-conditions: size() > 0
*/
std::pair<IndexType,DistanceType> worst_item() const
{
if (m_indices_dists.empty()) throw std::runtime_error("Cannot invoke RadiusResultSet::worst_item() on an empty list of results.");
typedef typename std::vector<std::pair<IndexType,DistanceType> >::const_iterator DistIt;
DistIt it = std::max_element(m_indices_dists.begin(), m_indices_dists.end());
return *it;
}
};
/** operator "<" for std::sort() */
struct IndexDist_Sorter
{
/** PairType will be typically: std::pair<IndexType,DistanceType> */
template <typename PairType>
inline bool operator()(const PairType &p1, const PairType &p2) const {
return p1.second < p2.second;
}
};
/** @} */
/** @addtogroup loadsave_grp Load/save auxiliary functions
* @{ */
template<typename T>
void save_value(FILE* stream, const T& value, size_t count = 1)
{
fwrite(&value, sizeof(value),count, stream);
}
template<typename T>
void save_value(FILE* stream, const std::vector<T>& value)
{
size_t size = value.size();
fwrite(&size, sizeof(size_t), 1, stream);
fwrite(&value[0], sizeof(T), size, stream);
}
template<typename T>
void load_value(FILE* stream, T& value, size_t count = 1)
{
size_t read_cnt = fread(&value, sizeof(value), count, stream);
if (read_cnt != count) {
throw std::runtime_error("Cannot read from file");
}
}
template<typename T>
void load_value(FILE* stream, std::vector<T>& value)
{
size_t size;
size_t read_cnt = fread(&size, sizeof(size_t), 1, stream);
if (read_cnt!=1) {
throw std::runtime_error("Cannot read from file");
}
value.resize(size);
read_cnt = fread(&value[0], sizeof(T), size, stream);
if (read_cnt!=size) {
throw std::runtime_error("Cannot read from file");
}
}
/** @} */
/** @addtogroup metric_grp Metric (distance) classes
* @{ */
template<typename T> inline T abs(T x) { return (x<0) ? -x : x; }
template<> inline int abs<int>(int x) { return ::abs(x); }
template<> inline float abs<float>(float x) { return fabsf(x); }
template<> inline double abs<double>(double x) { return fabs(x); }
template<> inline long double abs<long double>(long double x) { return fabsl(x); }
/** Manhattan distance functor (generic version, optimized for high-dimensionality data sets).
* Corresponding distance traits: nanoflann::metric_L1
* \tparam T Type of the elements (e.g. double, float, uint8_t)
* \tparam DistanceType Type of distance variables (must be signed) (e.g. float, double, int64_t)
*/
template<class T, class DataSource, typename _DistanceType = T>
struct L1_Adaptor
{
typedef T ElementType;
typedef _DistanceType DistanceType;
const DataSource &data_source;
L1_Adaptor(const DataSource &_data_source) : data_source(_data_source) { }
inline DistanceType operator()(const T* a, const size_t b_idx, size_t size, DistanceType worst_dist = -1) const
{
DistanceType result = DistanceType();
const T* last = a + size;
const T* lastgroup = last - 3;
size_t d = 0;
/* Process 4 items with each loop for efficiency. */
while (a < lastgroup) {
const DistanceType diff0 = nanoflann::abs(a[0] - data_source.kdtree_get_pt(b_idx,d++));
const DistanceType diff1 = nanoflann::abs(a[1] - data_source.kdtree_get_pt(b_idx,d++));
const DistanceType diff2 = nanoflann::abs(a[2] - data_source.kdtree_get_pt(b_idx,d++));
const DistanceType diff3 = nanoflann::abs(a[3] - data_source.kdtree_get_pt(b_idx,d++));
result += diff0 + diff1 + diff2 + diff3;
a += 4;
if ((worst_dist>0)&&(result>worst_dist)) {
return result;
}
}
/* Process last 0-3 components. Not needed for standard vector lengths. */
while (a < last) {
result += nanoflann::abs( *a++ - data_source.kdtree_get_pt(b_idx,d++) );
}
return result;
}
template <typename U, typename V>
inline DistanceType accum_dist(const U a, const V b, int ) const
{
return nanoflann::abs(a-b);
}
};
/** Squared Euclidean distance functor (generic version, optimized for high-dimensionality data sets).
* Corresponding distance traits: nanoflann::metric_L2
* \tparam T Type of the elements (e.g. double, float, uint8_t)
* \tparam DistanceType Type of distance variables (must be signed) (e.g. float, double, int64_t)
*/
template<class T, class DataSource, typename _DistanceType = T>
struct L2_Adaptor
{
typedef T ElementType;
typedef _DistanceType DistanceType;
const DataSource &data_source;
L2_Adaptor(const DataSource &_data_source) : data_source(_data_source) { }
inline DistanceType operator()(const T* a, const size_t b_idx, size_t size, DistanceType worst_dist = -1) const
{
DistanceType result = DistanceType();
const T* last = a + size;
const T* lastgroup = last - 3;
size_t d = 0;
/* Process 4 items with each loop for efficiency. */
while (a < lastgroup) {
const DistanceType diff0 = a[0] - data_source.kdtree_get_pt(b_idx,d++);
const DistanceType diff1 = a[1] - data_source.kdtree_get_pt(b_idx,d++);
const DistanceType diff2 = a[2] - data_source.kdtree_get_pt(b_idx,d++);
const DistanceType diff3 = a[3] - data_source.kdtree_get_pt(b_idx,d++);
result += diff0 * diff0 + diff1 * diff1 + diff2 * diff2 + diff3 * diff3;
a += 4;
if ((worst_dist>0)&&(result>worst_dist)) {
return result;
}
}
/* Process last 0-3 components. Not needed for standard vector lengths. */
while (a < last) {
const DistanceType diff0 = *a++ - data_source.kdtree_get_pt(b_idx,d++);
result += diff0 * diff0;
}
return result;
}
template <typename U, typename V>
inline DistanceType accum_dist(const U a, const V b, int ) const
{
return (a-b)*(a-b);
}
};
/** Squared Euclidean distance functor (suitable for low-dimensionality datasets, like 2D or 3D point clouds)
* Corresponding distance traits: nanoflann::metric_L2_Simple
* \tparam T Type of the elements (e.g. double, float, uint8_t)
* \tparam DistanceType Type of distance variables (must be signed) (e.g. float, double, int64_t)
*/
template<class T, class DataSource, typename _DistanceType = T>
struct L2_Simple_Adaptor
{
typedef T ElementType;
typedef _DistanceType DistanceType;
const DataSource &data_source;
L2_Simple_Adaptor(const DataSource &_data_source) : data_source(_data_source) { }
inline DistanceType operator()(const T* a, const size_t b_idx, size_t size) const {
return data_source.kdtree_distance(a,b_idx,size);
}
template <typename U, typename V>
inline DistanceType accum_dist(const U a, const V b, int ) const
{
return (a-b)*(a-b);
}
};
/** Metaprogramming helper traits class for the L1 (Manhattan) metric */
struct metric_L1 {
template<class T, class DataSource>
struct traits {
typedef L1_Adaptor<T,DataSource> distance_t;
};
};
/** Metaprogramming helper traits class for the L2 (Euclidean) metric */
struct metric_L2 {
template<class T, class DataSource>
struct traits {
typedef L2_Adaptor<T,DataSource> distance_t;
};
};
/** Metaprogramming helper traits class for the L2_simple (Euclidean) metric */
struct metric_L2_Simple {
template<class T, class DataSource>
struct traits {
typedef L2_Simple_Adaptor<T,DataSource> distance_t;
};
};
/** @} */
/** @addtogroup param_grp Parameter structs
* @{ */
/** Parameters (see http://code.google.com/p/nanoflann/ for help choosing the parameters)
*/
struct KDTreeSingleIndexAdaptorParams
{
KDTreeSingleIndexAdaptorParams(size_t _leaf_max_size = 10, int dim_ = -1) :
leaf_max_size(_leaf_max_size), dim(dim_)
{}
size_t leaf_max_size;
int dim;
};
/** Search options for KDTreeSingleIndexAdaptor::findNeighbors() */
struct SearchParams
{
/** Note: The first argument (checks_IGNORED_) is ignored, but kept for compatibility with the FLANN interface */
SearchParams(int checks_IGNORED_ = 32, float eps_ = 0, bool sorted_ = true ) :
checks(checks_IGNORED_), eps(eps_), sorted(sorted_) {}
int checks; //!< Ignored parameter (Kept for compatibility with the FLANN interface).
float eps; //!< search for eps-approximate neighbours (default: 0)
bool sorted; //!< only for radius search, require neighbours sorted by distance (default: true)
};
/** @} */
/** @addtogroup memalloc_grp Memory allocation
* @{ */
/**
* Allocates (using C's malloc) a generic type T.
*
* Params:
* count = number of instances to allocate.
* Returns: pointer (of type T*) to memory buffer
*/
template <typename T>
inline T* allocate(size_t count = 1)
{
T* mem = (T*) ::malloc(sizeof(T)*count);
return mem;
}
/**
* Pooled storage allocator
*
* The following routines allow for the efficient allocation of storage in
* small chunks from a specified pool. Rather than allowing each structure
* to be freed individually, an entire pool of storage is freed at once.
* This method has two advantages over just using malloc() and free(). First,
* it is far more efficient for allocating small objects, as there is
* no overhead for remembering all the information needed to free each
* object or consolidating fragmented memory. Second, the decision about
* how long to keep an object is made at the time of allocation, and there
* is no need to track down all the objects to free them.
*
*/
const size_t WORDSIZE=16;
const size_t BLOCKSIZE=8192;
class PooledAllocator
{
/* We maintain memory alignment to word boundaries by requiring that all
allocations be in multiples of the machine wordsize. */
/* Size of machine word in bytes. Must be power of 2. */
/* Minimum number of bytes requested at a time from the system. Must be multiple of WORDSIZE. */
size_t remaining; /* Number of bytes left in current block of storage. */
void* base; /* Pointer to base of current block of storage. */
void* loc; /* Current location in block to next allocate memory. */
size_t blocksize;
void internal_init()
{
remaining = 0;
base = NULL;
usedMemory = 0;
wastedMemory = 0;
}
public:
size_t usedMemory;
size_t wastedMemory;
/**
Default constructor. Initializes a new pool.
*/
PooledAllocator(const size_t blocksize_ = BLOCKSIZE) : blocksize(blocksize_) {
internal_init();
}
/**
* Destructor. Frees all the memory allocated in this pool.
*/
~PooledAllocator() {
free_all();
}
/** Frees all allocated memory chunks */
void free_all()
{
while (base != NULL) {
void *prev = *((void**) base); /* Get pointer to prev block. */
::free(base);
base = prev;
}
internal_init();
}
/**
* Returns a pointer to a piece of new memory of the given size in bytes
* allocated from the pool.
*/
void* malloc(const size_t req_size)
{
/* Round size up to a multiple of wordsize. The following expression
only works for WORDSIZE that is a power of 2, by masking last bits of
incremented size to zero.
*/
const size_t size = (req_size + (WORDSIZE - 1)) & ~(WORDSIZE - 1);
/* Check whether a new block must be allocated. Note that the first word
of a block is reserved for a pointer to the previous block.
*/
if (size > remaining) {
wastedMemory += remaining;
/* Allocate new storage. */
const size_t blocksize = (size + sizeof(void*) + (WORDSIZE-1) > BLOCKSIZE) ?
size + sizeof(void*) + (WORDSIZE-1) : BLOCKSIZE;
// use the standard C malloc to allocate memory
void* m = ::malloc(blocksize);
if (!m) {
fprintf(stderr,"Failed to allocate memory.\n");
return NULL;
}
/* Fill first word of new block with pointer to previous block. */
((void**) m)[0] = base;
base = m;
size_t shift = 0;
//int size_t = (WORDSIZE - ( (((size_t)m) + sizeof(void*)) & (WORDSIZE-1))) & (WORDSIZE-1);
remaining = blocksize - sizeof(void*) - shift;
loc = ((char*)m + sizeof(void*) + shift);
}
void* rloc = loc;
loc = (char*)loc + size;
remaining -= size;
usedMemory += size;
return rloc;
}
/**
* Allocates (using this pool) a generic type T.
*
* Params:
* count = number of instances to allocate.
* Returns: pointer (of type T*) to memory buffer
*/
template <typename T>
T* allocate(const size_t count = 1)
{
T* mem = (T*) this->malloc(sizeof(T)*count);
return mem;
}
};
/** @} */
/** @addtogroup kdtrees_grp KD-tree classes and adaptors
* @{ */
/** kd-tree index
*
* Contains the k-d trees and other information for indexing a set of points
* for nearest-neighbor matching.
*
* The class "DatasetAdaptor" must provide the following interface (can be non-virtual, inlined methods):
*
* \code
* // Must return the number of data points
* inline size_t kdtree_get_point_count() const { ... }
*
* // Must return the Euclidean (L2) distance between the vector "p1[0:size-1]" and the data point with index "idx_p2" stored in the class:
* inline DistanceType kdtree_distance(const T *p1, const size_t idx_p2,size_t size) const { ... }
*
* // Must return the dim'th component of the idx'th point in the class:
* inline T kdtree_get_pt(const size_t idx, int dim) const { ... }
*
* // Optional bounding-box computation: return false to default to a standard bbox computation loop.
* // Return true if the BBOX was already computed by the class and returned in "bb" so it can be avoided to redo it again.
* // Look at bb.size() to find out the expected dimensionality (e.g. 2 or 3 for point clouds)
* template <class BBOX>
* bool kdtree_get_bbox(BBOX &bb) const
* {
* bb[0].low = ...; bb[0].high = ...; // 0th dimension limits
* bb[1].low = ...; bb[1].high = ...; // 1st dimension limits
* ...
* return true;
* }
*
* \endcode
*
* \tparam IndexType Will be typically size_t or int
*/
template <typename Distance, class DatasetAdaptor,int DIM = -1, typename IndexType = size_t>
class KDTreeSingleIndexAdaptor
{
public:
typedef typename Distance::ElementType ElementType;
typedef typename Distance::DistanceType DistanceType;
protected:
/**
* Array of indices to vectors in the dataset.
*/
std::vector<IndexType> vind;
size_t m_leaf_max_size;
/**
* The dataset used by this index
*/
const DatasetAdaptor &dataset; //!< The source of our data
const KDTreeSingleIndexAdaptorParams index_params;
size_t m_size;
int dim; //!< Dimensionality of each data point
/*--------------------- Internal Data Structures --------------------------*/
struct Node
{
union {
struct
{
/**
* Indices of points in leaf node
*/
IndexType left, right;
} lr;
struct
{
/**
* Dimension used for subdivision.
*/
int divfeat;
/**
* The values used for subdivision.
*/
DistanceType divlow, divhigh;
} sub;
};
/**
* The child nodes.
*/
Node* child1, * child2;
};
typedef Node* NodePtr;
struct Interval
{
ElementType low, high;
};
typedef std::vector<Interval> BoundingBox;
/** This record represents a branch point when finding neighbors in
the tree. It contains a record of the minimum distance to the query
point, as well as the node at which the search resumes.
*/
template <typename T, typename DistanceType>
struct BranchStruct
{
T node; /* Tree node at which search resumes */
DistanceType mindist; /* Minimum distance to query for all nodes below. */
BranchStruct() {}
BranchStruct(const T& aNode, DistanceType dist) : node(aNode), mindist(dist) {}
inline bool operator<(const BranchStruct<T, DistanceType>& rhs) const
{
return mindist<rhs.mindist;
}
};
/**
* Array of k-d trees used to find neighbours.
*/
NodePtr root_node;
typedef BranchStruct<NodePtr, DistanceType> BranchSt;
typedef BranchSt* Branch;
BoundingBox root_bbox;
/**
* Pooled memory allocator.
*
* Using a pooled memory allocator is more efficient
* than allocating memory directly when there is a large
* number small of memory allocations.
*/
PooledAllocator pool;
public:
Distance distance;
/**
* KDTree constructor
*
* Params:
* inputData = dataset with the input features
* params = parameters passed to the kdtree algorithm (see http://code.google.com/p/nanoflann/ for help choosing the parameters)
*/
KDTreeSingleIndexAdaptor(const int dimensionality, const DatasetAdaptor& inputData, const KDTreeSingleIndexAdaptorParams& params = KDTreeSingleIndexAdaptorParams() ) :
dataset(inputData), index_params(params), root_node(NULL), distance(inputData)
{
m_size = dataset.kdtree_get_point_count();
dim = dimensionality;
if (DIM>0) dim=DIM;
else {
if (params.dim>0) dim = params.dim;
}
m_leaf_max_size = params.leaf_max_size;
// Create a permutable array of indices to the input vectors.
init_vind();
}
/**
* Standard destructor
*/
~KDTreeSingleIndexAdaptor()
{
}
/** Frees the previously-built index. Automatically called within buildIndex(). */
void freeIndex()
{
pool.free_all();
root_node=NULL;
}
/**
* Builds the index
*/
void buildIndex()
{
init_vind();
computeBoundingBox(root_bbox);
freeIndex();
root_node = divideTree(0, m_size, root_bbox ); // construct the tree
}
/**
* Returns size of index.
*/
size_t size() const
{
return m_size;
}
/**
* Returns the length of an index feature.
*/
size_t veclen() const
{
return static_cast<size_t>(DIM>0 ? DIM : dim);
}
/**
* Computes the inde memory usage
* Returns: memory used by the index
*/
size_t usedMemory() const
{
return pool.usedMemory+pool.wastedMemory+dataset.kdtree_get_point_count()*sizeof(IndexType); // pool memory and vind array memory
}
/** \name Query methods
* @{ */
/**
* Find set of nearest neighbors to vec[0:dim-1]. Their indices are stored inside
* the result object.
*
* Params:
* result = the result object in which the indices of the nearest-neighbors are stored
* vec = the vector for which to search the nearest neighbors
*
* \tparam RESULTSET Should be any ResultSet<DistanceType>
* \sa knnSearch, radiusSearch
*/
template <typename RESULTSET>
void findNeighbors(RESULTSET& result, const ElementType* vec, const SearchParams& searchParams) const
{
assert(vec);
if (!root_node) throw std::runtime_error("[nanoflann] findNeighbors() called before building the index.");
float epsError = 1+searchParams.eps;
std::vector<DistanceType> dists( (DIM>0 ? DIM : dim) ,0);
DistanceType distsq = computeInitialDistances(vec, dists);
searchLevel(result, vec, root_node, distsq, dists, epsError); // "count_leaf" parameter removed since was neither used nor returned to the user.
}
/**
* Find the "num_closest" nearest neighbors to the \a query_point[0:dim-1]. Their indices are stored inside
* the result object.
* \sa radiusSearch, findNeighbors
* \note nChecks_IGNORED is ignored but kept for compatibility with the original FLANN interface.
*/
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
{
nanoflann::KNNResultSet<DistanceType,IndexType> resultSet(num_closest);
resultSet.init(out_indices, out_distances_sq);
this->findNeighbors(resultSet, query_point, nanoflann::SearchParams());
}
/**
* Find all the neighbors to \a query_point[0:dim-1] within a maximum radius.
* The output is given as a vector of pairs, of which the first element is a point index and the second the corresponding distance.
* Previous contents of \a IndicesDists are cleared.
*
* If searchParams.sorted==true, the output list is sorted by ascending distances.
*
* For a better performance, it is advisable to do a .reserve() on the vector if you have any wild guess about the number of expected matches.
*
* \sa knnSearch, findNeighbors
* \return The number of points within the given radius (i.e. indices.size() or dists.size() )
*/
size_t radiusSearch(const ElementType *query_point,const DistanceType radius, std::vector<std::pair<IndexType,DistanceType> >& IndicesDists, const SearchParams& searchParams) const
{
RadiusResultSet<DistanceType,IndexType> resultSet(radius,IndicesDists);
this->findNeighbors(resultSet, query_point, searchParams);
if (searchParams.sorted)
std::sort(IndicesDists.begin(),IndicesDists.end(), IndexDist_Sorter() );
return resultSet.size();
}
/** @} */
private:
/** Make sure the auxiliary list \a vind has the same size than the current dataset, and re-generate if size has changed. */
void init_vind()
{
// Create a permutable array of indices to the input vectors.
m_size = dataset.kdtree_get_point_count();
if (vind.size()!=m_size) vind.resize(m_size);
for (size_t i = 0; i < m_size; i++) vind[i] = i;
}
/// Helper accessor to the dataset points:
inline ElementType dataset_get(size_t idx, int component) const {
return dataset.kdtree_get_pt(idx,component);
}
void save_tree(FILE* stream, NodePtr tree)
{
save_value(stream, *tree);
if (tree->child1!=NULL) {
save_tree(stream, tree->child1);
}
if (tree->child2!=NULL) {
save_tree(stream, tree->child2);
}
}
void load_tree(FILE* stream, NodePtr& tree)
{
tree = pool.allocate<Node>();
load_value(stream, *tree);
if (tree->child1!=NULL) {
load_tree(stream, tree->child1);
}
if (tree->child2!=NULL) {
load_tree(stream, tree->child2);
}
}
void computeBoundingBox(BoundingBox& bbox)
{
bbox.resize((DIM>0 ? DIM : dim));
if (dataset.kdtree_get_bbox(bbox))
{
// Done! It was implemented in derived class
}
else
{
for (int i=0; i<(DIM>0 ? DIM : dim); ++i) {
bbox[i].low =
bbox[i].high = dataset_get(0,i);
}
const size_t N = dataset.kdtree_get_point_count();
for (size_t k=1; k<N; ++k) {
for (int i=0; i<(DIM>0 ? DIM : dim); ++i) {
if (dataset_get(k,i)<bbox[i].low) bbox[i].low = dataset_get(k,i);
if (dataset_get(k,i)>bbox[i].high) bbox[i].high = dataset_get(k,i);
}
}
}
}
/**
* Create a tree node that subdivides the list of vecs from vind[first]
* to vind[last]. The routine is called recursively on each sublist.
* Place a pointer to this new tree node in the location pTree.
*
* Params: pTree = the new node to create
* first = index of the first vector
* last = index of the last vector
*/
NodePtr divideTree(const IndexType left, const IndexType right, BoundingBox& bbox)
{
NodePtr node = pool.allocate<Node>(); // allocate memory
/* If too few exemplars remain, then make this a leaf node. */
if ( (right-left) <= m_leaf_max_size) {
node->child1 = node->child2 = NULL; /* Mark as leaf node. */
node->lr.left = left;
node->lr.right = right;
// compute bounding-box of leaf points
for (int i=0; i<(DIM>0 ? DIM : dim); ++i) {
bbox[i].low = dataset_get(vind[left],i);
bbox[i].high = dataset_get(vind[left],i);
}
for (IndexType k=left+1; k<right; ++k) {
for (int i=0; i<(DIM>0 ? DIM : dim); ++i) {
if (bbox[i].low>dataset_get(vind[k],i)) bbox[i].low=dataset_get(vind[k],i);
if (bbox[i].high<dataset_get(vind[k],i)) bbox[i].high=dataset_get(vind[k],i);
}
}
}
else {
IndexType idx;
int cutfeat;
DistanceType cutval;
middleSplit_(&vind[0]+left, right-left, idx, cutfeat, cutval, bbox);
node->sub.divfeat = cutfeat;
BoundingBox left_bbox(bbox);
left_bbox[cutfeat].high = cutval;
node->child1 = divideTree(left, left+idx, left_bbox);
BoundingBox right_bbox(bbox);
right_bbox[cutfeat].low = cutval;
node->child2 = divideTree(left+idx, right, right_bbox);
node->sub.divlow = left_bbox[cutfeat].high;
node->sub.divhigh = right_bbox[cutfeat].low;
for (int i=0; i<(DIM>0 ? DIM : dim); ++i) {
bbox[i].low = std::min(left_bbox[i].low, right_bbox[i].low);
bbox[i].high = std::max(left_bbox[i].high, right_bbox[i].high);
}
}
return node;
}
void computeMinMax(IndexType* ind, IndexType count, int element, ElementType& min_elem, ElementType& max_elem)
{
min_elem = dataset_get(ind[0],element);
max_elem = dataset_get(ind[0],element);
for (IndexType i=1; i<count; ++i) {
ElementType val = dataset_get(ind[i],element);
if (val<min_elem) min_elem = val;
if (val>max_elem) max_elem = val;
}
}
void middleSplit(IndexType* ind, IndexType count, IndexType& index, int& cutfeat, DistanceType& cutval, const BoundingBox& bbox)
{
// find the largest span from the approximate bounding box
ElementType max_span = bbox[0].high-bbox[0].low;
cutfeat = 0;
cutval = (bbox[0].high+bbox[0].low)/2;
for (int i=1; i<(DIM>0 ? DIM : dim); ++i) {
ElementType span = bbox[i].low-bbox[i].low;
if (span>max_span) {
max_span = span;
cutfeat = i;
cutval = (bbox[i].high+bbox[i].low)/2;
}