-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflann.cpp
1215 lines (1067 loc) · 40.9 KB
/
flann.cpp
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.
*
* 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.
*************************************************************************/
#define FLANN_FIRST_MATCH
#include "params.h"
#include "flann.h"
#include <map>
#include <cstring>
#include "any.h"
#include "lz4.h"
#include "lz4hc.h"
using namespace std;
/*
struct FLANNParameters DEFAULT_FLANN_PARAMETERS = {
FLANN_INDEX_KDTREE,
32, 0.0f,
0, -1, 0,
4, 4
32, 11, FLANN_CENTERS_RANDOM, 0.2f,
0.9f, 0.01f, 0, 0.1f,
FLANN_LOG_NONE, 0
};*/
using namespace flann;
//typedef map<string, any> IndexParams;
flann::IndexParams create_parameters(FLANNParameters* p)
{
IndexParams params;
params["algorithm"] = p->algorithm;
params["checks"] = p->checks;
params["eps"] = p->eps;
if (p->algorithm == FLANN_INDEX_LSH) {
params["table_number"] = p->table_number_;
params["key_size"] = p->key_size_;
params["multi_probe_level"] = p->multi_probe_level_;
}
params["log_level"] = p->log_level;
params["random_seed"] = p->random_seed;
return params;
}
flann::SearchParams create_search_params(FLANNParameters* p)
{
flann::SearchParams params;
params.checks = p->checks;
params.eps = p->eps;
params.sorted = p->sorted;
params.max_neighbors = p->max_neighbors;
params.cores = p->cores;
return params;
}
flann_distance_t flann_distance_type = FLANN_DIST_EUCLIDEAN;
int flann_distance_order = 3;
void flann_set_distance_type(flann_distance_t distance_type, int order)
{
flann_distance_type = distance_type;
flann_distance_order = order;
}
flann_distance_t flann_get_distance_type() {
return flann_distance_type;
}
int flann_get_distance_order() {
return flann_distance_order;
}
template<typename Distance>
flann_index_t __flann_build_index(typename Distance::ElementType* dataset, int rows, int cols, float* speedup,
FLANNParameters* flann_params, Distance d = Distance())
{
typedef typename Distance::ElementType ElementType;
try {
init_flann_parameters(flann_params);
if (flann_params == NULL) {
throw FLANNException("The flann_params argument must be non-null");
}
IndexParams params = create_parameters(flann_params);
Index<Distance>* index = new Index<Distance>(Matrix<ElementType>(dataset, rows, cols), params, d);
index->buildIndex();
return index;
}
catch (std::runtime_error& e) {
//Logger::error("Caught exception: %s\n", e.what());
return NULL;
}
}
template<typename T>
flann_index_t _flann_build_index(T* dataset, int rows, int cols, float* speedup, FLANNParameters* flann_params)
{
if (flann_distance_type == FLANN_DIST_EUCLIDEAN) {
return __flann_build_index<L2<T> >(dataset, rows, cols, speedup, flann_params);
}
else if (flann_distance_type == FLANN_DIST_MANHATTAN) {
return __flann_build_index<L1<T> >(dataset, rows, cols, speedup, flann_params);
}
else if (flann_distance_type == FLANN_DIST_MINKOWSKI) {
return __flann_build_index<MinkowskiDistance<T> >(dataset, rows, cols, speedup, flann_params, MinkowskiDistance<T>(flann_distance_order));
}
else if (flann_distance_type == FLANN_DIST_HIST_INTERSECT) {
return __flann_build_index<HistIntersectionDistance<T> >(dataset, rows, cols, speedup, flann_params);
}
else if (flann_distance_type == FLANN_DIST_HELLINGER) {
return __flann_build_index<HellingerDistance<T> >(dataset, rows, cols, speedup, flann_params);
}
else if (flann_distance_type == FLANN_DIST_CHI_SQUARE) {
return __flann_build_index<ChiSquareDistance<T> >(dataset, rows, cols, speedup, flann_params);
}
else if (flann_distance_type == FLANN_DIST_KULLBACK_LEIBLER) {
return __flann_build_index<KL_Divergence<T> >(dataset, rows, cols, speedup, flann_params);
}
else {
// //Logger::error("Distance type unsupported in the C bindings, use the C++ bindings instead\n");
return NULL;
}
}
flann_index_t flann_build_index(float* dataset, int rows, int cols, float* speedup, FLANNParameters* flann_params)
{
return _flann_build_index<float>(dataset, rows, cols, speedup, flann_params);
}
flann_index_t flann_build_index_float(float* dataset, int rows, int cols, float* speedup, FLANNParameters* flann_params)
{
return _flann_build_index<float>(dataset, rows, cols, speedup, flann_params);
}
flann_index_t flann_build_index_double(double* dataset, int rows, int cols, float* speedup, FLANNParameters* flann_params)
{
return _flann_build_index<double>(dataset, rows, cols, speedup, flann_params);
}
flann_index_t flann_build_index_byte(unsigned char* dataset, int rows, int cols, float* speedup, FLANNParameters* flann_params)
{
return _flann_build_index<unsigned char>(dataset, rows, cols, speedup, flann_params);
}
flann_index_t flann_build_index_int(int* dataset, int rows, int cols, float* speedup, FLANNParameters* flann_params)
{
return _flann_build_index<int>(dataset, rows, cols, speedup, flann_params);
}
template <typename Distance>
int __flann_add_points(flann_index_t index_ptr,
typename Distance::ElementType* points, int rows, int columns,
float rebuild_threshold) {
typedef typename Distance::ElementType ElementType;
try {
if (index_ptr == NULL) {
throw FLANNException("Invalid index");
}
Index<Distance>* index = (Index<Distance>*)index_ptr;
index->addPoints(Matrix<ElementType>(points, rows, columns),
rebuild_threshold);
return 0;
}
catch (std::runtime_error& e) {
////Logger::error("Caught exception: %s\n", e.what());
return -1;
}
return -1;
}
template <typename T>
int _flann_add_points(flann_index_t index_ptr, T* points, int rows, int columns,
float rebuild_threshold) {
if (flann_distance_type == FLANN_DIST_EUCLIDEAN) {
return __flann_add_points<L2<T> >(index_ptr, points, rows, columns, rebuild_threshold);
}
else if (flann_distance_type == FLANN_DIST_MANHATTAN) {
return __flann_add_points<L1<T> >(index_ptr, points, rows, columns, rebuild_threshold);
}
else if (flann_distance_type == FLANN_DIST_MINKOWSKI) {
return __flann_add_points<MinkowskiDistance<T> >(index_ptr, points, rows, columns, rebuild_threshold);
}
else if (flann_distance_type == FLANN_DIST_HIST_INTERSECT) {
return __flann_add_points<HistIntersectionDistance<T> >(index_ptr, points, rows, columns, rebuild_threshold);
}
else if (flann_distance_type == FLANN_DIST_HELLINGER) {
return __flann_add_points<HellingerDistance<T> >(index_ptr, points, rows, columns, rebuild_threshold);
}
else if (flann_distance_type == FLANN_DIST_CHI_SQUARE) {
return __flann_add_points<ChiSquareDistance<T> >(index_ptr, points, rows, columns, rebuild_threshold);
}
else if (flann_distance_type == FLANN_DIST_KULLBACK_LEIBLER) {
return __flann_add_points<KL_Divergence<T> >(index_ptr, points, rows, columns, rebuild_threshold);
}
else {
// //Logger::error("Distance type unsupported in the C bindings, use the C++ bindings instead\n");
return 0;
}
}
int flann_add_points(flann_index_t index_ptr, float* points, int rows, int columns, float rebuild_threshold)
{
return _flann_add_points<float>(index_ptr, points, rows, columns, rebuild_threshold);
}
int flann_add_points_float(flann_index_t index_ptr, float* points, int rows, int columns, float rebuild_threshold)
{
return _flann_add_points<float>(index_ptr, points, rows, columns, rebuild_threshold);
}
int flann_add_points_double(flann_index_t index_ptr, double* points, int rows, int columns, float rebuild_threshold)
{
return _flann_add_points<double>(index_ptr, points, rows, columns, rebuild_threshold);
}
int flann_add_points_byte(flann_index_t index_ptr, unsigned char* points, int rows, int columns, float rebuild_threshold)
{
return _flann_add_points<unsigned char>(index_ptr, points, rows, columns, rebuild_threshold);
}
int flann_add_points_int(flann_index_t index_ptr, int* points, int rows, int columns, float rebuild_threshold)
{
return _flann_add_points<int>(index_ptr, points, rows, columns, rebuild_threshold);
}
template <typename Distance>
int __flann_remove_point(flann_index_t index_ptr, unsigned int point_id_uint) {
size_t point_id(point_id_uint);
try {
if (index_ptr == NULL) {
throw FLANNException("Invalid index");
}
Index<Distance>* index = (Index<Distance>*)index_ptr;
index->removePoint(point_id);
return 0;
}
catch (std::runtime_error& e) {
////Logger::error("Caught exception: %s\n", e.what());
return -1;
}
return -1;
}
template <typename T>
int _flann_remove_point(flann_index_t index_ptr, unsigned int point_id) {
if (flann_distance_type == FLANN_DIST_EUCLIDEAN) {
return __flann_remove_point<L2<T> >(index_ptr, point_id);
}
else if (flann_distance_type == FLANN_DIST_MANHATTAN) {
return __flann_remove_point<L1<T> >(index_ptr, point_id);
}
else if (flann_distance_type == FLANN_DIST_MINKOWSKI) {
return __flann_remove_point<MinkowskiDistance<T> >(index_ptr, point_id);
}
else if (flann_distance_type == FLANN_DIST_HIST_INTERSECT) {
return __flann_remove_point<HistIntersectionDistance<T> >(index_ptr, point_id);
}
else if (flann_distance_type == FLANN_DIST_HELLINGER) {
return __flann_remove_point<HellingerDistance<T> >(index_ptr, point_id);
}
else if (flann_distance_type == FLANN_DIST_CHI_SQUARE) {
return __flann_remove_point<ChiSquareDistance<T> >(index_ptr, point_id);
}
else if (flann_distance_type == FLANN_DIST_KULLBACK_LEIBLER) {
return __flann_remove_point<KL_Divergence<T> >(index_ptr, point_id);
}
else {
// //Logger::error("Distance type unsupported in the C bindings, use the C++ bindings instead\n");
return 0;
}
}
int flann_remove_point(flann_index_t index_ptr, unsigned int point_id)
{
return _flann_remove_point<float>(index_ptr, point_id);
}
int flann_remove_point_float(flann_index_t index_ptr, unsigned int point_id)
{
return _flann_remove_point<float>(index_ptr, point_id);
}
int flann_remove_point_double(flann_index_t index_ptr, unsigned int point_id)
{
return _flann_remove_point<double>(index_ptr, point_id);
}
int flann_remove_point_byte(flann_index_t index_ptr, unsigned int point_id)
{
return _flann_remove_point<unsigned char>(index_ptr, point_id);
}
int flann_remove_point_int(flann_index_t index_ptr, unsigned int point_id)
{
return _flann_remove_point<int>(index_ptr, point_id);
}
template <typename Distance>
typename Distance::ElementType* __flann_get_point(flann_index_t index_ptr,
unsigned int point_id_uint) {
size_t point_id(point_id_uint);
try {
if (index_ptr == NULL) {
throw FLANNException("Invalid index");
}
Index<Distance>* index = (Index<Distance>*)index_ptr;
return index->getPoint(point_id);
}
catch (std::runtime_error& e) {
//Logger::error("Caught exception: %s\n", e.what());
return NULL;
}
}
template <typename T>
T* _flann_get_point(flann_index_t index_ptr, unsigned int point_id) {
if (flann_distance_type == FLANN_DIST_EUCLIDEAN) {
return __flann_get_point<L2<T> >(index_ptr, point_id);
}
else if (flann_distance_type == FLANN_DIST_MANHATTAN) {
return __flann_get_point<L1<T> >(index_ptr, point_id);
}
else if (flann_distance_type == FLANN_DIST_MINKOWSKI) {
return __flann_get_point<MinkowskiDistance<T> >(index_ptr, point_id);
}
else if (flann_distance_type == FLANN_DIST_HIST_INTERSECT) {
return __flann_get_point<HistIntersectionDistance<T> >(index_ptr, point_id);
}
else if (flann_distance_type == FLANN_DIST_HELLINGER) {
return __flann_get_point<HellingerDistance<T> >(index_ptr, point_id);
}
else if (flann_distance_type == FLANN_DIST_CHI_SQUARE) {
return __flann_get_point<ChiSquareDistance<T> >(index_ptr, point_id);
}
else if (flann_distance_type == FLANN_DIST_KULLBACK_LEIBLER) {
return __flann_get_point<KL_Divergence<T> >(index_ptr, point_id);
}
else {
// //Logger::error("Distance type unsupported in the C bindings, use the C++ bindings instead\n");
return NULL;
}
}
float* flann_get_point(flann_index_t index_ptr, unsigned int point_id)
{
return _flann_get_point<float>(index_ptr, point_id);
}
float* flann_get_point_float(flann_index_t index_ptr, unsigned int point_id)
{
return _flann_get_point<float>(index_ptr, point_id);
}
double* flann_get_point_double(flann_index_t index_ptr, unsigned int point_id)
{
return _flann_get_point<double>(index_ptr, point_id);
}
unsigned char* flann_get_point_byte(flann_index_t index_ptr, unsigned int point_id)
{
return _flann_get_point<unsigned char>(index_ptr, point_id);
}
int* flann_get_point_int(flann_index_t index_ptr, unsigned int point_id)
{
return _flann_get_point<int>(index_ptr, point_id);
}
template <typename Distance>
unsigned int __flann_veclen(flann_index_t index_ptr) {
try {
if (index_ptr == NULL) {
throw FLANNException("Invalid index");
}
Index<Distance>* index = (Index<Distance>*)index_ptr;
return index->veclen();
}
catch (std::runtime_error& e) {
////Logger::error("Caught exception: %s\n", e.what());
return 0;
}
}
template <typename T>
unsigned int _flann_veclen(flann_index_t index_ptr) {
if (flann_distance_type == FLANN_DIST_EUCLIDEAN) {
return __flann_veclen<L2<T> >(index_ptr);
}
else if (flann_distance_type == FLANN_DIST_MANHATTAN) {
return __flann_veclen<L1<T> >(index_ptr);
}
else if (flann_distance_type == FLANN_DIST_MINKOWSKI) {
return __flann_veclen<MinkowskiDistance<T> >(index_ptr);
}
else if (flann_distance_type == FLANN_DIST_HIST_INTERSECT) {
return __flann_veclen<HistIntersectionDistance<T> >(index_ptr);
}
else if (flann_distance_type == FLANN_DIST_HELLINGER) {
return __flann_veclen<HellingerDistance<T> >(index_ptr);
}
else if (flann_distance_type == FLANN_DIST_CHI_SQUARE) {
return __flann_veclen<ChiSquareDistance<T> >(index_ptr);
}
else if (flann_distance_type == FLANN_DIST_KULLBACK_LEIBLER) {
return __flann_veclen<KL_Divergence<T> >(index_ptr);
}
else {
////Logger::error("Distance type unsupported in the C bindings, use the C++ bindings instead\n");
return 0;
}
}
unsigned int flann_veclen(flann_index_t index_ptr)
{
return _flann_veclen<float>(index_ptr);
}
unsigned int flann_veclen_float(flann_index_t index_ptr)
{
return _flann_veclen<float>(index_ptr);
}
unsigned int flann_veclen_double(flann_index_t index_ptr)
{
return _flann_veclen<double>(index_ptr);
}
unsigned int flann_veclen_byte(flann_index_t index_ptr)
{
return _flann_veclen<unsigned char>(index_ptr);
}
unsigned int flann_veclen_int(flann_index_t index_ptr)
{
return _flann_veclen<int>(index_ptr);
}
template <typename Distance>
unsigned int __flann_size(flann_index_t index_ptr) {
try {
if (index_ptr == NULL) {
throw FLANNException("Invalid index");
}
Index<Distance>* index = (Index<Distance>*)index_ptr;
return index->size();
}
catch (std::runtime_error& e) {
////Logger::error("Caught exception: %s\n", e.what());
return 0;
}
}
template <typename T>
unsigned int _flann_size(flann_index_t index_ptr) {
if (flann_distance_type == FLANN_DIST_EUCLIDEAN) {
return __flann_size<L2<T> >(index_ptr);
}
else if (flann_distance_type == FLANN_DIST_MANHATTAN) {
return __flann_size<L1<T> >(index_ptr);
}
else if (flann_distance_type == FLANN_DIST_MINKOWSKI) {
return __flann_size<MinkowskiDistance<T> >(index_ptr);
}
else if (flann_distance_type == FLANN_DIST_HIST_INTERSECT) {
return __flann_size<HistIntersectionDistance<T> >(index_ptr);
}
else if (flann_distance_type == FLANN_DIST_HELLINGER) {
return __flann_size<HellingerDistance<T> >(index_ptr);
}
else if (flann_distance_type == FLANN_DIST_CHI_SQUARE) {
return __flann_size<ChiSquareDistance<T> >(index_ptr);
}
else if (flann_distance_type == FLANN_DIST_KULLBACK_LEIBLER) {
return __flann_size<KL_Divergence<T> >(index_ptr);
}
else {
//Logger::error("Distance type unsupported in the C bindings, use the C++ bindings instead\n");
return 0;
}
}
unsigned int flann_size(flann_index_t index_ptr)
{
return _flann_size<float>(index_ptr);
}
unsigned int flann_size_float(flann_index_t index_ptr)
{
return _flann_size<float>(index_ptr);
}
unsigned int flann_size_double(flann_index_t index_ptr)
{
return _flann_size<double>(index_ptr);
}
unsigned int flann_size_byte(flann_index_t index_ptr)
{
return _flann_size<unsigned char>(index_ptr);
}
unsigned int flann_size_int(flann_index_t index_ptr)
{
return _flann_size<int>(index_ptr);
}
template <typename Distance>
int __flann_used_memory(flann_index_t index_ptr) {
try {
if (index_ptr == NULL) {
throw FLANNException("Invalid index");
}
Index<Distance>* index = (Index<Distance>*)index_ptr;
return index->usedMemory();
}
catch (std::runtime_error& e) {
//Logger::error("Caught exception: %s\n", e.what());
return 0;
}
}
template <typename T>
int _flann_used_memory(flann_index_t index_ptr) {
if (flann_distance_type == FLANN_DIST_EUCLIDEAN) {
return __flann_used_memory<L2<T> >(index_ptr);
}
else if (flann_distance_type == FLANN_DIST_MANHATTAN) {
return __flann_used_memory<L1<T> >(index_ptr);
}
else if (flann_distance_type == FLANN_DIST_MINKOWSKI) {
return __flann_used_memory<MinkowskiDistance<T> >(index_ptr);
}
else if (flann_distance_type == FLANN_DIST_HIST_INTERSECT) {
return __flann_used_memory<HistIntersectionDistance<T> >(index_ptr);
}
else if (flann_distance_type == FLANN_DIST_HELLINGER) {
return __flann_used_memory<HellingerDistance<T> >(index_ptr);
}
else if (flann_distance_type == FLANN_DIST_CHI_SQUARE) {
return __flann_used_memory<ChiSquareDistance<T> >(index_ptr);
}
else if (flann_distance_type == FLANN_DIST_KULLBACK_LEIBLER) {
return __flann_used_memory<KL_Divergence<T> >(index_ptr);
}
else {
//Logger::error("Distance type unsupported in the C bindings, use the C++ bindings instead\n");
return 0;
}
}
int flann_used_memory(flann_index_t index_ptr)
{
return _flann_used_memory<float>(index_ptr);
}
int flann_used_memory_float(flann_index_t index_ptr)
{
return _flann_used_memory<float>(index_ptr);
}
int flann_used_memory_double(flann_index_t index_ptr)
{
return _flann_used_memory<double>(index_ptr);
}
int flann_used_memory_byte(flann_index_t index_ptr)
{
return _flann_used_memory<unsigned char>(index_ptr);
}
int flann_used_memory_int(flann_index_t index_ptr)
{
return _flann_used_memory<int>(index_ptr);
}
template<typename Distance>
int __flann_save_index(flann_index_t index_ptr, char* filename)
{
try {
if (index_ptr == NULL) {
throw FLANNException("Invalid index");
}
Index<Distance>* index = (Index<Distance>*)index_ptr;
index->save(filename);
return 0;
}
catch (std::runtime_error& e) {
//Logger::error("Caught exception: %s\n", e.what());
return -1;
}
}
template<typename T>
int _flann_save_index(flann_index_t index_ptr, char* filename)
{
if (flann_distance_type == FLANN_DIST_EUCLIDEAN) {
return __flann_save_index<L2<T> >(index_ptr, filename);
}
else if (flann_distance_type == FLANN_DIST_MANHATTAN) {
return __flann_save_index<L1<T> >(index_ptr, filename);
}
else if (flann_distance_type == FLANN_DIST_MINKOWSKI) {
return __flann_save_index<MinkowskiDistance<T> >(index_ptr, filename);
}
else if (flann_distance_type == FLANN_DIST_HIST_INTERSECT) {
return __flann_save_index<HistIntersectionDistance<T> >(index_ptr, filename);
}
else if (flann_distance_type == FLANN_DIST_HELLINGER) {
return __flann_save_index<HellingerDistance<T> >(index_ptr, filename);
}
else if (flann_distance_type == FLANN_DIST_CHI_SQUARE) {
return __flann_save_index<ChiSquareDistance<T> >(index_ptr, filename);
}
else if (flann_distance_type == FLANN_DIST_KULLBACK_LEIBLER) {
return __flann_save_index<KL_Divergence<T> >(index_ptr, filename);
}
else {
//Logger::error("Distance type unsupported in the C bindings, use the C++ bindings instead\n");
return -1;
}
}
int flann_save_index(flann_index_t index_ptr, char* filename)
{
return _flann_save_index<float>(index_ptr, filename);
}
int flann_save_index_float(flann_index_t index_ptr, char* filename)
{
return _flann_save_index<float>(index_ptr, filename);
}
int flann_save_index_double(flann_index_t index_ptr, char* filename)
{
return _flann_save_index<double>(index_ptr, filename);
}
int flann_save_index_byte(flann_index_t index_ptr, char* filename)
{
return _flann_save_index<unsigned char>(index_ptr, filename);
}
int flann_save_index_int(flann_index_t index_ptr, char* filename)
{
return _flann_save_index<int>(index_ptr, filename);
}
template<typename Distance>
flann_index_t __flann_load_index(char* filename, typename Distance::ElementType* dataset, int rows, int cols,
Distance d = Distance())
{
try {
Index<Distance>* index = new Index<Distance>(Matrix<typename Distance::ElementType>(dataset, rows, cols), SavedIndexParams(filename), d);
return index;
}
catch (std::runtime_error& e) {
//Logger::error("Caught exception: %s\n", e.what());
return NULL;
}
}
template<typename T>
flann_index_t _flann_load_index(char* filename, T* dataset, int rows, int cols)
{
if (flann_distance_type == FLANN_DIST_EUCLIDEAN) {
return __flann_load_index<L2<T> >(filename, dataset, rows, cols);
}
else if (flann_distance_type == FLANN_DIST_MANHATTAN) {
return __flann_load_index<L1<T> >(filename, dataset, rows, cols);
}
else if (flann_distance_type == FLANN_DIST_MINKOWSKI) {
return __flann_load_index<MinkowskiDistance<T> >(filename, dataset, rows, cols, MinkowskiDistance<T>(flann_distance_order));
}
else if (flann_distance_type == FLANN_DIST_HIST_INTERSECT) {
return __flann_load_index<HistIntersectionDistance<T> >(filename, dataset, rows, cols);
}
else if (flann_distance_type == FLANN_DIST_HELLINGER) {
return __flann_load_index<HellingerDistance<T> >(filename, dataset, rows, cols);
}
else if (flann_distance_type == FLANN_DIST_CHI_SQUARE) {
return __flann_load_index<ChiSquareDistance<T> >(filename, dataset, rows, cols);
}
else if (flann_distance_type == FLANN_DIST_KULLBACK_LEIBLER) {
return __flann_load_index<KL_Divergence<T> >(filename, dataset, rows, cols);
}
else {
//Logger::error("Distance type unsupported in the C bindings, use the C++ bindings instead\n");
return NULL;
}
}
flann_index_t flann_load_index(char* filename, float* dataset, int rows, int cols)
{
return _flann_load_index<float>(filename, dataset, rows, cols);
}
flann_index_t flann_load_index_float(char* filename, float* dataset, int rows, int cols)
{
return _flann_load_index<float>(filename, dataset, rows, cols);
}
flann_index_t flann_load_index_double(char* filename, double* dataset, int rows, int cols)
{
return _flann_load_index<double>(filename, dataset, rows, cols);
}
flann_index_t flann_load_index_byte(char* filename, unsigned char* dataset, int rows, int cols)
{
return _flann_load_index<unsigned char>(filename, dataset, rows, cols);
}
flann_index_t flann_load_index_int(char* filename, int* dataset, int rows, int cols)
{
return _flann_load_index<int>(filename, dataset, rows, cols);
}
template<typename Distance>
int __flann_find_nearest_neighbors(typename Distance::ElementType* dataset, int rows, int cols, typename Distance::ElementType* testset, int tcount,
int* result, typename Distance::ResultType* dists, int nn, FLANNParameters* flann_params, Distance d = Distance())
{
typedef typename Distance::ElementType ElementType;
typedef typename Distance::ResultType DistanceType;
try {
init_flann_parameters(flann_params);
IndexParams params = create_parameters(flann_params);
Index<Distance>* index = new Index<Distance>(Matrix<ElementType>(dataset, rows, cols), params, d);
index->buildIndex();
Matrix<int> m_indices(result, tcount, nn);
Matrix<DistanceType> m_dists(dists, tcount, nn);
SearchParams search_params = create_search_params(flann_params);
index->knnSearch(Matrix<ElementType>(testset, tcount, index->veclen()),
m_indices,
m_dists, nn, search_params);
delete index;
return 0;
}
catch (std::runtime_error& e) {
//Logger::error("Caught exception: %s\n", e.what());
return -1;
}
return -1;
}
template<typename T, typename R>
int _flann_find_nearest_neighbors(T* dataset, int rows, int cols, T* testset, int tcount,
int* result, R* dists, int nn, FLANNParameters* flann_params)
{
if (flann_distance_type == FLANN_DIST_EUCLIDEAN) {
return __flann_find_nearest_neighbors<L2<T> >(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params);
}
else if (flann_distance_type == FLANN_DIST_MANHATTAN) {
return __flann_find_nearest_neighbors<L1<T> >(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params);
}
else if (flann_distance_type == FLANN_DIST_MINKOWSKI) {
return __flann_find_nearest_neighbors<MinkowskiDistance<T> >(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params, MinkowskiDistance<T>(flann_distance_order));
}
else if (flann_distance_type == FLANN_DIST_HIST_INTERSECT) {
return __flann_find_nearest_neighbors<HistIntersectionDistance<T> >(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params);
}
else if (flann_distance_type == FLANN_DIST_HELLINGER) {
return __flann_find_nearest_neighbors<HellingerDistance<T> >(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params);
}
else if (flann_distance_type == FLANN_DIST_CHI_SQUARE) {
return __flann_find_nearest_neighbors<ChiSquareDistance<T> >(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params);
}
else if (flann_distance_type == FLANN_DIST_KULLBACK_LEIBLER) {
return __flann_find_nearest_neighbors<KL_Divergence<T> >(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params);
}
else {
//Logger::error("Distance type unsupported in the C bindings, use the C++ bindings instead\n");
return -1;
}
}
int flann_find_nearest_neighbors(float* dataset, int rows, int cols, float* testset, int tcount, int* result, float* dists, int nn, FLANNParameters* flann_params)
{
return _flann_find_nearest_neighbors(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params);
}
int flann_find_nearest_neighbors_float(float* dataset, int rows, int cols, float* testset, int tcount, int* result, float* dists, int nn, FLANNParameters* flann_params)
{
return _flann_find_nearest_neighbors(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params);
}
int flann_find_nearest_neighbors_double(double* dataset, int rows, int cols, double* testset, int tcount, int* result, double* dists, int nn, FLANNParameters* flann_params)
{
return _flann_find_nearest_neighbors(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params);
}
int flann_find_nearest_neighbors_byte(unsigned char* dataset, int rows, int cols, unsigned char* testset, int tcount, int* result, float* dists, int nn, FLANNParameters* flann_params)
{
return _flann_find_nearest_neighbors(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params);
}
int flann_find_nearest_neighbors_int(int* dataset, int rows, int cols, int* testset, int tcount, int* result, float* dists, int nn, FLANNParameters* flann_params)
{
return _flann_find_nearest_neighbors(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params);
}
template<typename Distance>
int __flann_find_nearest_neighbors_index(flann_index_t index_ptr, typename Distance::ElementType* testset, int tcount,
int* result, typename Distance::ResultType* dists, int nn, FLANNParameters* flann_params)
{
typedef typename Distance::ElementType ElementType;
typedef typename Distance::ResultType DistanceType;
try {
init_flann_parameters(flann_params);
if (index_ptr == NULL) {
throw FLANNException("Invalid index");
}
Index<Distance>* index = (Index<Distance>*)index_ptr;
Matrix<int> m_indices(result, tcount, nn);
Matrix<DistanceType> m_dists(dists, tcount, nn);
SearchParams search_params = create_search_params(flann_params);
index->knnSearch(Matrix<ElementType>(testset, tcount, index->veclen()),
m_indices,
m_dists, nn, search_params);
return 0;
}
catch (std::runtime_error& e) {
//Logger::error("Caught exception: %s\n", e.what());
return -1;
}
return -1;
}
template<typename T, typename R>
int _flann_find_nearest_neighbors_index(flann_index_t index_ptr, T* testset, int tcount,
int* result, R* dists, int nn, FLANNParameters* flann_params)
{
if (flann_distance_type == FLANN_DIST_EUCLIDEAN) {
return __flann_find_nearest_neighbors_index<L2<T> >(index_ptr, testset, tcount, result, dists, nn, flann_params);
}
else if (flann_distance_type == FLANN_DIST_MANHATTAN) {
return __flann_find_nearest_neighbors_index<L1<T> >(index_ptr, testset, tcount, result, dists, nn, flann_params);
}
else if (flann_distance_type == FLANN_DIST_MINKOWSKI) {
return __flann_find_nearest_neighbors_index<MinkowskiDistance<T> >(index_ptr, testset, tcount, result, dists, nn, flann_params);
}
else if (flann_distance_type == FLANN_DIST_HIST_INTERSECT) {
return __flann_find_nearest_neighbors_index<HistIntersectionDistance<T> >(index_ptr, testset, tcount, result, dists, nn, flann_params);
}
else if (flann_distance_type == FLANN_DIST_HELLINGER) {
return __flann_find_nearest_neighbors_index<HellingerDistance<T> >(index_ptr, testset, tcount, result, dists, nn, flann_params);
}
else if (flann_distance_type == FLANN_DIST_CHI_SQUARE) {
return __flann_find_nearest_neighbors_index<ChiSquareDistance<T> >(index_ptr, testset, tcount, result, dists, nn, flann_params);
}
else if (flann_distance_type == FLANN_DIST_KULLBACK_LEIBLER) {
return __flann_find_nearest_neighbors_index<KL_Divergence<T> >(index_ptr, testset, tcount, result, dists, nn, flann_params);
}
else {
//Logger::error("Distance type unsupported in the C bindings, use the C++ bindings instead\n");
return -1;
}
}
int flann_find_nearest_neighbors_index(flann_index_t index_ptr, float* testset, int tcount, int* result, float* dists, int nn, FLANNParameters* flann_params)
{
return _flann_find_nearest_neighbors_index(index_ptr, testset, tcount, result, dists, nn, flann_params);
}
int flann_find_nearest_neighbors_index_float(flann_index_t index_ptr, float* testset, int tcount, int* result, float* dists, int nn, FLANNParameters* flann_params)
{
return _flann_find_nearest_neighbors_index(index_ptr, testset, tcount, result, dists, nn, flann_params);
}
int flann_find_nearest_neighbors_index_double(flann_index_t index_ptr, double* testset, int tcount, int* result, double* dists, int nn, FLANNParameters* flann_params)
{
return _flann_find_nearest_neighbors_index(index_ptr, testset, tcount, result, dists, nn, flann_params);
}
int flann_find_nearest_neighbors_index_byte(flann_index_t index_ptr, unsigned char* testset, int tcount, int* result, float* dists, int nn, FLANNParameters* flann_params)
{
return _flann_find_nearest_neighbors_index(index_ptr, testset, tcount, result, dists, nn, flann_params);
}
int flann_find_nearest_neighbors_index_int(flann_index_t index_ptr, int* testset, int tcount, int* result, float* dists, int nn, FLANNParameters* flann_params)
{
return _flann_find_nearest_neighbors_index(index_ptr, testset, tcount, result, dists, nn, flann_params);
}
template<typename Distance>
int __flann_radius_search(flann_index_t index_ptr,
typename Distance::ElementType* query,
int* indices,
typename Distance::ResultType* dists,
int max_nn,
float radius,
FLANNParameters* flann_params)
{
typedef typename Distance::ElementType ElementType;
typedef typename Distance::ResultType DistanceType;
try {
init_flann_parameters(flann_params);
if (index_ptr == NULL) {
throw FLANNException("Invalid index");
}
Index<Distance>* index = (Index<Distance>*)index_ptr;
Matrix<int> m_indices(indices, 1, max_nn);
Matrix<DistanceType> m_dists(dists, 1, max_nn);
SearchParams search_params = create_search_params(flann_params);
int count = index->radiusSearch(Matrix<ElementType>(query, 1, index->veclen()),
m_indices,
m_dists, radius, search_params);
return count;
}
catch (std::runtime_error& e) {
//Logger::error("Caught exception: %s\n", e.what());
return -1;
}
}
template<typename T, typename R>
int _flann_radius_search(flann_index_t index_ptr,
T* query,
int* indices,
R* dists,
int max_nn,
float radius,
FLANNParameters* flann_params)
{
if (flann_distance_type == FLANN_DIST_EUCLIDEAN) {
return __flann_radius_search<L2<T> >(index_ptr, query, indices, dists, max_nn, radius, flann_params);
}
else if (flann_distance_type == FLANN_DIST_MANHATTAN) {
return __flann_radius_search<L1<T> >(index_ptr, query, indices, dists, max_nn, radius, flann_params);
}
else if (flann_distance_type == FLANN_DIST_MINKOWSKI) {
return __flann_radius_search<MinkowskiDistance<T> >(index_ptr, query, indices, dists, max_nn, radius, flann_params);
}
else if (flann_distance_type == FLANN_DIST_HIST_INTERSECT) {
return __flann_radius_search<HistIntersectionDistance<T> >(index_ptr, query, indices, dists, max_nn, radius, flann_params);
}
else if (flann_distance_type == FLANN_DIST_HELLINGER) {
return __flann_radius_search<HellingerDistance<T> >(index_ptr, query, indices, dists, max_nn, radius, flann_params);
}
else if (flann_distance_type == FLANN_DIST_CHI_SQUARE) {
return __flann_radius_search<ChiSquareDistance<T> >(index_ptr, query, indices, dists, max_nn, radius, flann_params);
}
else if (flann_distance_type == FLANN_DIST_KULLBACK_LEIBLER) {
return __flann_radius_search<KL_Divergence<T> >(index_ptr, query, indices, dists, max_nn, radius, flann_params);
}
else {