forked from flann-lib/flann
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflann_tests.h
722 lines (610 loc) · 22.9 KB
/
flann_tests.h
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
/***********************************************************************
* Software License Agreement (BSD License)
*
* Copyright 2008-2012 Marius Muja ([email protected]). All rights reserved.
* Copyright 2008-2012 David G. Lowe ([email protected]). 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.
*************************************************************************/
#ifndef FLANN_TESTS_H_
#define FLANN_TESTS_H_
#include <flann/util/matrix.h>
#include <vector>
#include <set>
template<typename T>
float compute_precision(const flann::Matrix<T>& match, const flann::Matrix<T>& indices)
{
int count = 0;
assert(match.rows == indices.rows);
size_t nn = std::min(match.cols, indices.cols);
for (size_t i=0; i<match.rows; ++i) {
for (size_t j=0;j<nn;++j) {
for (size_t k=0;k<nn;++k) {
if (match[i][j]==indices[i][k]) {
count ++;
}
}
}
}
return float(count)/(nn*match.rows);
}
/** @brief Compare the distances for match accuracies
* This is more precise: e.g. when you ask for the top 10 neighbors and they all get the same distance,
* you might have 100 other neighbors that are at the same distance and simply matching the indices is not the way to go
* @param gt_dists the ground truth best distances
* @param dists the distances of the computed nearest neighbors
* @param tol tolerance at which distanceare considered equal
* @return
*/
template<typename T>
float computePrecisionDiscrete(const flann::Matrix<T>& gt_dists, const flann::Matrix<T>& dists)
{
int count = 0;
assert(gt_dists.rows == dists.rows);
size_t nn = std::min(gt_dists.cols, dists.cols);
std::vector<T> gt_sorted_dists(nn), sorted_dists(nn), intersection(nn);
for (size_t i = 0; i < gt_dists.rows; ++i)
{
std::copy(gt_dists[i], gt_dists[i] + nn, gt_sorted_dists.begin());
std::sort(gt_sorted_dists.begin(), gt_sorted_dists.end());
std::copy(dists[i], dists[i] + nn, sorted_dists.begin());
std::sort(sorted_dists.begin(), sorted_dists.end());
typename std::vector<T>::iterator end = std::set_intersection(gt_sorted_dists.begin(), gt_sorted_dists.end(),
sorted_dists.begin(), sorted_dists.end(),
intersection.begin());
count += (end - intersection.begin());
}
return float(count) / (nn * gt_dists.rows);
}
const char* index_type_to_name(flann_algorithm_t index_type)
{
switch (index_type) {
case FLANN_INDEX_LINEAR: return "linear";
case FLANN_INDEX_KDTREE: return "randomized kd-tree";
case FLANN_INDEX_KMEANS: return "k-means";
case FLANN_INDEX_COMPOSITE: return "composite";
case FLANN_INDEX_KDTREE_SINGLE: return "single kd-tree";
case FLANN_INDEX_HIERARCHICAL: return "hierarchical";
case FLANN_INDEX_LSH: return "LSH";
#ifdef FLANN_USE_CUDA
case FLANN_INDEX_KDTREE_CUDA: return "kd-tree CUDA";
#endif
case FLANN_INDEX_SAVED: return "saved";
case FLANN_INDEX_AUTOTUNED: return "autotuned";
default: return "(unknown)";
}
}
class FLANNTestFixture : public ::testing::Test {
protected:
clock_t start_time_;
void start_timer(const std::string& message = "")
{
if (!message.empty()) {
printf("%s", message.c_str());
fflush(stdout);
}
start_time_ = clock();
}
double stop_timer()
{
return double(clock()-start_time_)/CLOCKS_PER_SEC;
}
template<typename Distance>
void TestSearch(const flann::Matrix<typename Distance::ElementType>& data,
const flann::IndexParams& index_params,
const flann::Matrix<typename Distance::ElementType>& query,
flann::Matrix<size_t>& indices,
flann::Matrix<typename Distance::ResultType>& dists,
size_t knn,
const flann::SearchParams& search_params,
float expected_precision,
const flann::Matrix<size_t>& gt_indices,
const flann::Matrix<typename Distance::ResultType>& gt_dists = flann::Matrix<typename Distance::ResultType>())
{
flann::seed_random(0);
Index<Distance> index(data, index_params);
char message[256];
const char* index_name = index_type_to_name(index.getType());
sprintf(message, "Building %s index... ", index_name);
start_timer( message );
index.buildIndex();
printf("done (%g seconds)\n", stop_timer());
start_timer("Searching KNN...");
index.knnSearch(query, indices, dists, knn, search_params );
printf("done (%g seconds)\n", stop_timer());
float precision;
if (gt_dists.ptr()==NULL) {
precision = compute_precision(gt_indices, indices);
}
else {
precision = computePrecisionDiscrete(gt_dists, dists);
}
EXPECT_GE(precision, expected_precision);
printf("Precision: %g\n", precision);
}
template<typename Distance>
void TestSearch2(const flann::Matrix<typename Distance::ElementType>& data,
const flann::IndexParams& index_params,
const flann::Matrix<typename Distance::ElementType>& query,
flann::Matrix<size_t>& indices,
flann::Matrix<typename Distance::ResultType>& dists,
size_t knn,
const flann::SearchParams& search_params,
float expected_precision,
const flann::Matrix<size_t>& gt_indices,
const flann::Matrix<typename Distance::ResultType>& gt_dists = flann::Matrix<typename Distance::ResultType>())
{
flann::seed_random(0);
Index<Distance> index(index_params);
char message[256];
const char* index_name = index_type_to_name(index.getType());
sprintf(message, "Building %s index... ", index_name);
start_timer( message );
index.buildIndex(data);
printf("done (%g seconds)\n", stop_timer());
start_timer("Searching KNN...");
index.knnSearch(query, indices, dists, knn, search_params );
printf("done (%g seconds)\n", stop_timer());
float precision;
if (gt_dists.ptr()==NULL) {
precision = compute_precision(gt_indices, indices);
}
else {
precision = computePrecisionDiscrete(gt_dists, dists);
}
EXPECT_GE(precision, expected_precision);
printf("Precision: %g\n", precision);
}
template<typename Distance>
void TestAddIncremental(const flann::Matrix<typename Distance::ElementType>& data,
const flann::IndexParams& index_params,
const flann::Matrix<typename Distance::ElementType>& query,
flann::Matrix<size_t>& indices,
flann::Matrix<typename Distance::ResultType>& dists,
size_t knn,
const flann::SearchParams& search_params,
float expected_precision,
flann::Matrix<size_t>& gt_indices,
const flann::Matrix<typename Distance::ResultType>& gt_dists = flann::Matrix<typename Distance::ResultType>())
{
flann::seed_random(0);
size_t size1 = data.rows/2-1;
size_t size2 = data.rows-size1;
Matrix<typename Distance::ElementType> data1(data[0], size1, data.cols);
Matrix<typename Distance::ElementType> data2(data[size1], size2, data.cols);
Index<Distance> index(data1, index_params);
char message[256];
const char* index_name = index_type_to_name(index.getType());
sprintf(message, "Building %s index... ", index_name);
start_timer( message );
index.buildIndex();
EXPECT_EQ(index.size(), data1.rows);
index.addPoints(data2);
printf("done (%g seconds)\n", stop_timer());
EXPECT_EQ(index.size(), data.rows);
start_timer("Searching KNN...");
index.knnSearch(query, indices, dists, knn, search_params);
printf("done (%g seconds)\n", stop_timer());
float precision;
if (gt_dists.ptr()==NULL) {
precision = compute_precision(gt_indices, indices);
}
else {
precision = computePrecisionDiscrete(gt_dists, dists);
}
EXPECT_GE(precision, expected_precision);
printf("Precision: %g\n", precision);
for (size_t i=0;i<indices.rows;++i) {
for (size_t j=0;j<indices.cols;++j) {
EXPECT_EQ(index.getPoint(indices[i][j]), data[indices[i][j]]);
}
}
// save and re-load the index and make sure everything still checks out
index.save("test_saved_index.idx");
Index<Distance > index2(data, flann::SavedIndexParams("test_saved_index.idx"));
index2.buildIndex();
EXPECT_EQ(index2.size(), data.rows);
flann::Matrix<size_t> indices2(new size_t[query.rows*knn], query.rows, knn);
flann::Matrix<typename Distance::ResultType> dists2(new typename Distance::ResultType[query.rows*knn], query.rows, knn);
start_timer("Searching KNN after saving and reloading index...");
index2.knnSearch(query, indices2, dists2, knn, search_params );
printf("done (%g seconds)\n", stop_timer());
for (size_t i=0;i<indices.rows;++i) {
for (size_t j=0;j<indices.cols;++j) {
EXPECT_EQ(indices[i][j], indices2[i][j]);
}
}
delete[] indices2.ptr();
delete[] dists2.ptr();
}
template<typename Distance>
void TestAddIncremental2(const flann::Matrix<typename Distance::ElementType>& data,
const flann::IndexParams& index_params,
const flann::Matrix<typename Distance::ElementType>& query,
flann::Matrix<size_t>& indices,
flann::Matrix<typename Distance::ResultType>& dists,
size_t knn,
const flann::SearchParams& search_params,
float expected_precision,
flann::Matrix<size_t>& gt_indices,
const flann::Matrix<typename Distance::ResultType>& gt_dists = flann::Matrix<typename Distance::ResultType>())
{
flann::seed_random(0);
size_t size1 = data.rows/2+1;
size_t size2 = data.rows-size1;
Matrix<typename Distance::ElementType> data1(data[0], size1, data.cols);
Matrix<typename Distance::ElementType> data2(data[size1], size2, data.cols);
Index<Distance> index(data1, index_params);
char message[256];
const char* index_name = index_type_to_name(index.getType());
sprintf(message, "Building %s index... ", index_name);
start_timer( message );
index.buildIndex();
EXPECT_EQ(index.size(), data1.rows);
index.addPoints(data2);
printf("done (%g seconds)\n", stop_timer());
EXPECT_EQ(index.size(), data.rows);
start_timer("Searching KNN...");
index.knnSearch(query, indices, dists, knn, search_params);
printf("done (%g seconds)\n", stop_timer());
float precision;
if (gt_dists.ptr()==NULL) {
precision = compute_precision(gt_indices, indices);
}
else {
precision = computePrecisionDiscrete(gt_dists, dists);
}
EXPECT_GE(precision, expected_precision);
printf("Precision: %g\n", precision);
for (size_t i=0;i<indices.rows;++i) {
for (size_t j=0;j<indices.cols;++j) {
EXPECT_EQ(index.getPoint(indices[i][j]), data[indices[i][j]]);
}
}
// save and re-load the index and make sure everything still checks out
index.save("test_saved_index.idx");
Index<Distance > index2(data, flann::SavedIndexParams("test_saved_index.idx"));
index2.buildIndex();
EXPECT_EQ(index2.size(), data.rows);
flann::Matrix<size_t> indices2(new size_t[query.rows*knn], query.rows, knn);
flann::Matrix<typename Distance::ResultType> dists2(new typename Distance::ResultType[query.rows*knn], query.rows, knn);
start_timer("Searching KNN after saving and reloading index...");
index2.knnSearch(query, indices2, dists2, knn, search_params );
printf("done (%g seconds)\n", stop_timer());
for (size_t i=0;i<indices.rows;++i) {
for (size_t j=0;j<indices.cols;++j) {
EXPECT_EQ(indices[i][j], indices2[i][j]);
}
}
delete[] indices2.ptr();
delete[] dists2.ptr();
}
template<typename Distance>
void TestSave(const flann::Matrix<typename Distance::ElementType>& data,
const flann::IndexParams& index_params,
const flann::Matrix<typename Distance::ElementType>& query,
flann::Matrix<size_t>& indices,
flann::Matrix<typename Distance::ResultType>& dists,
size_t knn,
const flann::SearchParams& search_params,
float expected_precision,
flann::Matrix<size_t>& gt_indices,
const flann::Matrix<typename Distance::ResultType>& gt_dists = flann::Matrix<typename Distance::ResultType>())
{
flann::seed_random(0);
Index<Distance> index(data, index_params);
char message[256];
const char* index_name = index_type_to_name(index.getType());
sprintf(message, "Building %s index... ", index_name);
start_timer( message );
index.buildIndex();
printf("done (%g seconds)\n", stop_timer());
EXPECT_EQ(index.size(), data.rows);
start_timer("Searching KNN...");
index.knnSearch(query, indices, dists, knn, search_params );
printf("done (%g seconds)\n", stop_timer());
float precision;
if (gt_dists.ptr()==NULL) {
precision = compute_precision(gt_indices, indices);
}
else {
precision = computePrecisionDiscrete(gt_dists, dists);
}
printf("Precision: %g\n", precision);
EXPECT_GE(precision, expected_precision);
printf("Saving index\n");
index.save("test_saved_index.idx");
printf("Loading index\n");
Index<Distance > index2(data, flann::SavedIndexParams("test_saved_index.idx"));
index2.buildIndex();
EXPECT_EQ(index2.size(), data.rows);
flann::Matrix<size_t> indices2(new size_t[query.rows*knn], query.rows, knn);
flann::Matrix<typename Distance::ResultType> dists2(new typename Distance::ResultType[query.rows*knn], query.rows, knn);
start_timer("Searching KNN after saving and reloading index...");
index2.knnSearch(query, indices2, dists2, knn, search_params );
printf("done (%g seconds)\n", stop_timer());
float precision2;
if (gt_dists.ptr()==NULL) {
precision2 = compute_precision(gt_indices, indices2);
}
else {
precision2 = computePrecisionDiscrete(gt_dists, dists2);
}
printf("Precision: %g\n", precision2);
EXPECT_EQ(precision, precision2);
for (size_t i=0;i<indices.rows;++i) {
for (size_t j=0;j<indices.cols;++j) {
EXPECT_EQ(indices[i][j], indices2[i][j]);
}
}
delete[] indices2.ptr();
delete[] dists2.ptr();
}
template<typename Distance>
void TestCopy(const flann::Matrix<typename Distance::ElementType>& data,
const flann::IndexParams& index_params,
const flann::Matrix<typename Distance::ElementType>& query,
flann::Matrix<size_t>& indices,
flann::Matrix<typename Distance::ResultType>& dists,
size_t knn,
const flann::SearchParams& search_params,
float expected_precision,
flann::Matrix<size_t>& gt_indices,
const flann::Matrix<typename Distance::ResultType>& gt_dists = flann::Matrix<typename Distance::ResultType>())
{
flann::seed_random(0);
Index<Distance> index(data, index_params);
char message[256];
const char* index_name = index_type_to_name(index.getType());
sprintf(message, "Building %s index... ", index_name);
start_timer( message );
index.buildIndex();
printf("done (%g seconds)\n", stop_timer());
start_timer("Searching KNN...");
index.knnSearch(query, indices, dists, knn, search_params );
printf("done (%g seconds)\n", stop_timer());
float precision;
if (gt_dists.ptr()==NULL) {
precision = compute_precision(gt_indices, indices);
}
else {
precision = computePrecisionDiscrete(gt_dists, dists);
}
printf("Precision: %g\n", precision);
EXPECT_GE(precision, expected_precision);
// test copy constructor
Index<Distance> index2(index);
start_timer("Searching KNN...");
index2.knnSearch(query, indices, dists, knn, search_params );
printf("done (%g seconds)\n", stop_timer());
float precision2;
if (gt_dists.ptr()==NULL) {
precision2 = compute_precision(gt_indices, indices);
}
else {
precision2 = computePrecisionDiscrete(gt_dists, dists);
}
printf("Precision: %g\n", precision2);
EXPECT_EQ(precision, precision2);
// test assignment operator
Index<Distance > index3(data, index_params);
index3 = index;
start_timer("Searching KNN...");
index3.knnSearch(query, indices, dists, knn, search_params );
printf("done (%g seconds)\n", stop_timer());
float precision3;
if (gt_dists.ptr()==NULL) {
precision3 = compute_precision(gt_indices, indices);
}
else {
precision3 = computePrecisionDiscrete(gt_dists, dists);
}
printf("Precision: %g\n", precision3);
EXPECT_EQ(precision, precision3);
}
template<typename Index>
void TestCopy2(const flann::Matrix<typename Index::ElementType>& data,
const flann::IndexParams& index_params,
const flann::Matrix<typename Index::ElementType>& query,
flann::Matrix<size_t>& indices,
flann::Matrix<typename Index::DistanceType>& dists,
size_t knn,
const flann::SearchParams& search_params,
float expected_precision,
flann::Matrix<size_t>& gt_indices,
const flann::Matrix<typename Index::DistanceType>& gt_dists = flann::Matrix<typename Index::DistanceType>())
{
flann::seed_random(0);
Index index(data, index_params);
char message[256];
const char* index_name = index_type_to_name(index.getType());
sprintf(message, "Building %s index... ", index_name);
start_timer( message );
index.buildIndex();
printf("done (%g seconds)\n", stop_timer());
start_timer("Searching KNN...");
index.knnSearch(query, indices, dists, knn, search_params );
printf("done (%g seconds)\n", stop_timer());
float precision;
if (gt_dists.ptr()==NULL) {
precision = compute_precision(gt_indices, indices);
}
else {
precision = computePrecisionDiscrete(gt_dists, dists);
}
printf("Precision: %g\n", precision);
EXPECT_GE(precision, expected_precision);
// test copy constructor
Index index2(index);
start_timer("Searching KNN...");
index2.knnSearch(query, indices, dists, knn, search_params );
printf("done (%g seconds)\n", stop_timer());
float precision2;
if (gt_dists.ptr()==NULL) {
precision2 = compute_precision(gt_indices, indices);
}
else {
precision2 = computePrecisionDiscrete(gt_dists, dists);
}
printf("Precision: %g\n", precision2);
EXPECT_EQ(precision, precision2);
// test assignment operator
Index index3(data, index_params);
index3 = index;
start_timer("Searching KNN...");
index3.knnSearch(query, indices, dists, knn, search_params );
printf("done (%g seconds)\n", stop_timer());
float precision3;
if (gt_dists.ptr()==NULL) {
precision3 = compute_precision(gt_indices, indices);
}
else {
precision3 = computePrecisionDiscrete(gt_dists, dists);
}
printf("Precision: %g\n", precision3);
EXPECT_EQ(precision, precision3);
}
template<typename Distance>
void TestRemove(const flann::Matrix<typename Distance::ElementType>& data,
const flann::IndexParams& index_params,
const flann::Matrix<typename Distance::ElementType>& query,
flann::Matrix<size_t>& indices,
flann::Matrix<typename Distance::ResultType>& dists,
size_t knn,
const flann::SearchParams& search_params)
{
flann::seed_random(0);
Index< Distance > index(data, index_params);
char message[256];
const char* index_name = index_type_to_name(index.getType());
sprintf(message, "Building %s index... ", index_name);
start_timer( message );
index.buildIndex();
printf("done (%g seconds)\n", stop_timer());
start_timer("Searching KNN before removing points...");
index.knnSearch(query, indices, dists, knn, search_params );
printf("done (%g seconds)\n", stop_timer());
EXPECT_EQ(index.size(), data.rows);
// remove about 50% of neighbours found
std::set<size_t> neighbors;
for (size_t i=0;i<indices.rows;++i) {
for (size_t j=0;j<indices.cols;++j) {
if (rand_double()<0.5) {
neighbors.insert(indices[i][j]);
}
}
}
flann::DynamicBitset removed(data.rows);
for (std::set<size_t>::iterator it = neighbors.begin(); it!=neighbors.end();++it) {
index.removePoint(*it);
removed.set(*it);
}
// also remove 10% of the initial points
size_t offset = data.rows/10;
for (size_t i=0;i<offset;++i) {
index.removePoint(i);
removed.set(i);
}
size_t new_size = 0;
for (size_t i=0;i<removed.size();++i) {
if (!removed.test(i)) ++new_size;
}
EXPECT_EQ(index.size(), new_size);
start_timer("Searching KNN after remove points...");
index.knnSearch(query, indices, dists, knn, search_params );
printf("done (%g seconds)\n", stop_timer());
for (size_t i=0;i<indices.rows;++i) {
for (size_t j=0;j<indices.cols;++j) {
EXPECT_GE(indices[i][j], offset);
EXPECT_TRUE(neighbors.find(indices[i][j])==neighbors.end());
EXPECT_EQ(index.getPoint(indices[i][j]), data[indices[i][j]]);
}
}
// save and re-load the index and make sure everything still checks out
index.save("test_saved_index.idx");
flann::seed_random(0);
Index< Distance > index2(data, flann::SavedIndexParams("test_saved_index.idx"));
index2.buildIndex();
EXPECT_EQ(index2.size(), new_size);
flann::Matrix<size_t> indices2(new size_t[query.rows*knn], query.rows, knn);
flann::Matrix<typename Distance::ResultType> dists2(new typename Distance::ResultType[query.rows*knn], query.rows, knn);
start_timer("Searching KNN after saving and reloading index...");
index2.knnSearch(query, indices2, dists2, knn, search_params );
printf("done (%g seconds)\n", stop_timer());
for (size_t i=0;i<indices.rows;++i) {
for (size_t j=0;j<indices.cols;++j) {
EXPECT_EQ(indices[i][j], indices2[i][j]);
}
}
delete[] indices2.ptr();
delete[] dists2.ptr();
// rebuild index
index.buildIndex();
EXPECT_EQ(index.size(), new_size);
start_timer("Searching KNN after remove points and rebuild index...");
index.knnSearch(query, indices, dists, knn, search_params );
printf("done (%g seconds)\n", stop_timer());
for (size_t i=0;i<indices.rows;++i) {
for (size_t j=0;j<indices.cols;++j) {
EXPECT_GE(indices[i][j], offset);
EXPECT_TRUE(neighbors.find(indices[i][j])==neighbors.end());
EXPECT_EQ(index.getPoint(indices[i][j]), data[indices[i][j]]);
}
}
}
};
template<typename ElementType, typename DistanceType>
class DatasetTestFixture : public FLANNTestFixture
{
protected:
std::string filename_;
flann::Matrix<ElementType> data;
flann::Matrix<ElementType> query;
flann::Matrix<size_t> gt_indices;
flann::Matrix<DistanceType> dists;
flann::Matrix<size_t> indices;
int knn;
DatasetTestFixture(const std::string& filename) : filename_(filename), knn(5)
{
}
void SetUp()
{
knn = 5;
printf("Reading test data...");
fflush(stdout);
flann::load_from_file(data, filename_.c_str(), "dataset");
flann::load_from_file(query, filename_.c_str(), "query");
flann::load_from_file(gt_indices, filename_.c_str(), "match");
dists = flann::Matrix<DistanceType>(new DistanceType[query.rows*knn], query.rows, knn);
indices = flann::Matrix<size_t>(new size_t[query.rows*knn], query.rows, knn);
printf("done\n");
}
void TearDown()
{
delete[] data.ptr();
delete[] query.ptr();
delete[] gt_indices.ptr();
delete[] dists.ptr();
delete[] indices.ptr();
}
};
#endif /* FLANN_TESTS_H_ */