-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkdtree2.cpp
819 lines (673 loc) · 18.8 KB
/
kdtree2.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
//
// (c) Matthew B. Kennel, Institute for Nonlinear Science, UCSD (2004)
//
// Licensed under the Academic Free License version 1.1 found in file LICENSE
// with additional provisions in that same file.
#include "kdtree2.hpp"
#include <algorithm>
#include <iostream>
using namespace std;
namespace kdtree2 {
// utility
inline float squared(const float x) {
return(x*x);
}
inline void swap(int& a, int&b) {
int tmp;
tmp = a;
a = b;
b = tmp;
}
inline void swap(float& a, float&b) {
float tmp;
tmp = a;
a = b;
b = tmp;
}
//
// KDTREERESULT implementation
//
inline bool operator<(const KDTreeResult& e1, const KDTreeResult& e2) {
return (e1.dis < e2.dis);
}
//
// KDTREE2_RESULT_VECTOR implementation
//
float KDTreeResultVector::max_value() {
return( (*begin()).dis ); // very first element
}
void KDTreeResultVector::push_element_and_heapify(KDTreeResult& e) {
push_back(e); // what a vector does.
push_heap( begin(), end() ); // and now heapify it, with the new elt.
}
float KDTreeResultVector::replace_maxpri_elt_return_new_maxpri(KDTreeResult& e) {
// remove the maximum priority element on the queue and replace it
// with 'e', and return its priority.
//
// here, it means replacing the first element [0] with e, and re heapifying.
pop_heap( begin(), end() );
pop_back();
push_back(e); // insert new
push_heap(begin(), end() ); // and heapify.
return( (*this)[0].dis );
}
//
// KDTREE2 implementation
//
// constructor
KDTree::KDTree(KDTreeArray& data_in,bool rearrange_in,int dim_in)
: the_data(data_in),
N ( data_in.shape()[0] ),
dim( data_in.shape()[1] ),
sort_results(false),
rearrange(rearrange_in),
root(NULL),
data(NULL),
ind(N)
{
//
// initialize the constant references using this unusual C++
// feature.
//
if (dim_in > 0)
dim = dim_in;
build_tree();
if (rearrange) {
// if we have a rearranged tree.
// allocate the memory for it.
printf("rearranging\n");
rearranged_data.resize( boost::extents[N][dim] );
// permute the data for it.
for (int i=0; i<N; i++) {
for (int j=0; j<dim; j++) {
rearranged_data[i][j] = the_data[ind[i]][j];
// wouldn't F90 be nice here?
}
}
data = &rearranged_data;
} else {
data = &the_data;
}
}
// destructor
KDTree::~KDTree() {
delete root;
}
// building routines
void KDTree::build_tree() {
for (int i=0; i<N; i++)
{ ind[i] = i; }
root = build_tree_for_range(0,N-1,NULL);
}
KDTreeNode* KDTree::build_tree_for_range(int l, int u, KDTreeNode* parent) {
// recursive function to build
KDTreeNode* node = new KDTreeNode(dim);
// the newly created node.
if (u<l) {
return(NULL); // no data in this node.
}
if ((u-l) <= bucketsize) {
// create a terminal node.
// always compute true bounding box for terminal node.
for (int i=0;i<dim;i++) {
spread_in_coordinate(i,l,u,node->box[i]);
}
node->cut_dim = 0;
node->cut_val = 0.0;
node->l = l;
node->u = u;
node->left = node->right = NULL;
} else {
//
// Compute an APPROXIMATE bounding box for this node.
// if parent == NULL, then this is the root node, and
// we compute for all dimensions.
// Otherwise, we copy the bounding box from the parent for
// all coordinates except for the parent's cut dimension.
// That, we recompute ourself.
//
int c =-1;
float maxspread = 0.0;
int m;
//cout << "dim="<<dim <<endl;
for (int i=0;i<dim;i++) {
if ((parent == NULL) || (parent->cut_dim == i)) {
spread_in_coordinate(i,l,u,node->box[i]);
} else {
node->box[i] = parent->box[i];
}
float spread = node->box[i].upper - node->box[i].lower;
if (spread>maxspread) {
maxspread = spread;
c=i;
}
//cout << spread <<" -> "<<maxspread << "\t";
}
//cout<<"c="<< c <<endl;
//
// now, c is the identity of which coordinate has the greatest spread
//
if (c == -1) {
m = (l+u)/2;
c=0;
select_on_coordinate(c,m,l,u);
} else {
float sum;
float average;
if (true) {
sum = 0.0;
//cout<< c <<endl;
for (int k=l; k <= u; k++) {
sum += the_data[ind[k]][c];
}
average = sum / static_cast<float> (u-l+1);
} else {
// average of top and bottom nodes.
average = (node->box[c].upper + node->box[c].lower)*0.5;
}
m = select_on_coordinate_value(c,average,l,u);
}
// move the indices around to cut on dim 'c'.
node->cut_dim=c;
node->l = l;
node->u = u;
node->left = build_tree_for_range(l,m,node);
node->right = build_tree_for_range(m+1,u,node);
if (node->right == NULL) {
for (int i=0; i<dim; i++)
node->box[i] = node->left->box[i];
node->cut_val = node->left->box[c].upper;
node->cut_val_left = node->cut_val_right = node->cut_val;
} else if (node->left == NULL) {
for (int i=0; i<dim; i++)
node->box[i] = node->right->box[i];
node->cut_val = node->right->box[c].upper;
node->cut_val_left = node->cut_val_right = node->cut_val;
} else {
node->cut_val_right = node->right->box[c].lower;
node->cut_val_left = node->left->box[c].upper;
node->cut_val = (node->cut_val_left + node->cut_val_right) / 2.0;
//
// now recompute true bounding box as union of subtree boxes.
// This is now faster having built the tree, being logarithmic in
// N, not linear as would be from naive method.
//
for (int i=0; i<dim; i++) {
node->box[i].upper = std::max(node->left->box[i].upper,
node->right->box[i].upper);
node->box[i].lower = std::min(node->left->box[i].lower,
node->right->box[i].lower);
}
}
}
return(node);
}
void KDTree:: spread_in_coordinate(int c, int l, int u, interval& interv)
{
// return the minimum and maximum of the indexed data between l and u in
// smin_out and smax_out.
float smin, smax;
float lmin, lmax;
int i;
smin = the_data[ind[l]][c];
smax = smin;
// process two at a time.
for (i=l+2; i<= u; i+=2) {
lmin = the_data[ind[i-1]] [c];
lmax = the_data[ind[i] ] [c];
if (lmin > lmax) {
swap(lmin,lmax);
// float t = lmin;
// lmin = lmax;
// lmax = t;
}
if (smin > lmin) smin = lmin;
if (smax <lmax) smax = lmax;
}
// is there one more element?
if (i == u+1) {
float last = the_data[ind[u]] [c];
if (smin>last) smin = last;
if (smax<last) smax = last;
}
interv.lower = smin;
interv.upper = smax;
// printf("Spread in coordinate %d=[%f,%f]\n",c,smin,smax);
}
void KDTree::select_on_coordinate(int c, int k, int l, int u) {
//
// Move indices in ind[l..u] so that the elements in [l .. k]
// are less than the [k+1..u] elmeents, viewed across dimension 'c'.
//
while (l < u) {
int t = ind[l];
int m = l;
for (int i=l+1; i<=u; i++) {
if ( the_data[ ind[i] ] [c] < the_data[t][c]) {
m++;
swap(ind[i],ind[m]);
}
} // for i
swap(ind[l],ind[m]);
if (m <= k) l = m+1;
if (m >= k) u = m-1;
} // while loop
}
int KDTree::select_on_coordinate_value(int c, float alpha, int l, int u) {
//
// Move indices in ind[l..u] so that the elements in [l .. return]
// are <= alpha, and hence are less than the [return+1..u]
// elmeents, viewed across dimension 'c'.
//
int lb = l, ub = u;
while (lb < ub) {
if (the_data[ind[lb]][c] <= alpha) {
lb++; // good where it is.
} else {
swap(ind[lb],ind[ub]);
ub--;
}
}
// here ub=lb
if (the_data[ind[lb]][c] <= alpha)
return(lb);
else
return(lb-1);
}
// void KDTree::dump_data() {
// int upper1, upper2;
// upper1 = N;
// upper2 = dim;
// printf("Rearrange=%d\n",rearrange);
// printf("N=%d, dim=%d\n", upper1, upper2);
// for (int i=0; i<upper1; i++) {
// printf("the_data[%d][*]=",i);
// for (int j=0; j<upper2; j++)
// printf("%f,",the_data[i][j]);
// printf("\n");
// }
// for (int i=0; i<upper1; i++)
// printf("Indexes[%d]=%d\n",i,ind[i]);
// for (int i=0; i<upper1; i++) {
// printf("data[%d][*]=",i);
// for (int j=0; j<upper2; j++)
// printf("%f,",(*data)[i][j]);
// printf("\n");
// }
// }
//
// search record substructure
//
// one of these is created for each search.
// this holds useful information to be used
// during the search
static const float infinity = 1.0e38;
class SearchRecord {
private:
friend class KDTree;
friend class KDTreeNode;
std::vector<float>& qv;
int dim;
bool rearrange;
unsigned int nn; // , nfound;
float ballsize;
int centeridx, correltime;
KDTreeResultVector& result; // results
const KDTreeArray* data;
const std::vector<int>& ind;
// constructor
public:
SearchRecord(std::vector<float>& qv_in, KDTree& tree_in,
KDTreeResultVector& result_in) :
qv(qv_in),
result(result_in),
data(tree_in.data),
ind(tree_in.ind)
{
dim = tree_in.dim;
rearrange = tree_in.rearrange;
ballsize = infinity;
nn = 0;
};
};
void KDTree::n_nearest_brute_force(std::vector<float>& qv, int nn, KDTreeResultVector& result) {
result.clear();
for (int i=0; i<N; i++) {
float dis = 0.0;
KDTreeResult e;
for (int j=0; j<dim; j++) {
dis += squared( the_data[i][j] - qv[j]);
}
e.dis = dis;
e.idx = i;
result.push_back(e);
}
sort(result.begin(), result.end() );
}
void KDTree::n_nearest(std::vector<float>& qv, int nn, KDTreeResultVector& result) {
SearchRecord sr(qv,*this,result);
std::vector<float> vdiff(dim,0.0);
result.clear();
sr.centeridx = -1;
sr.correltime = 0;
sr.nn = nn;
root->search(sr);
if (sort_results) sort(result.begin(), result.end());
}
// search for n nearest to a given query vector 'qv'.
void KDTree::n_nearest_around_point(int idxin, int correltime, int nn,
KDTreeResultVector& result) {
std::vector<float> qv(dim); // query vector
result.clear();
for (int i=0; i<dim; i++) {
qv[i] = the_data[idxin][i];
}
// copy the query vector.
{
SearchRecord sr(qv, *this, result);
// construct the search record.
sr.centeridx = idxin;
sr.correltime = correltime;
sr.nn = nn;
root->search(sr);
}
if (sort_results) sort(result.begin(), result.end());
}
void KDTree::r_nearest(std::vector<float>& qv, float r2, KDTreeResultVector& result) {
// search for all within a ball of a certain radius
SearchRecord sr(qv,*this,result);
std::vector<float> vdiff(dim,0.0);
result.clear();
sr.centeridx = -1;
sr.correltime = 0;
sr.nn = 0;
sr.ballsize = r2;
root->search(sr);
if (sort_results) sort(result.begin(), result.end());
}
int KDTree::r_count(std::vector<float>& qv, float r2) {
// search for all within a ball of a certain radius
{
KDTreeResultVector result;
SearchRecord sr(qv,*this,result);
sr.centeridx = -1;
sr.correltime = 0;
sr.nn = 0;
sr.ballsize = r2;
root->search(sr);
return(result.size());
}
}
void KDTree::r_nearest_around_point(int idxin, int correltime, float r2,
KDTreeResultVector& result) {
std::vector<float> qv(dim); // query vector
result.clear();
for (int i=0; i<dim; i++) {
qv[i] = the_data[idxin][i];
}
// copy the query vector.
{
SearchRecord sr(qv, *this, result);
// construct the search record.
sr.centeridx = idxin;
sr.correltime = correltime;
sr.ballsize = r2;
sr.nn = 0;
root->search(sr);
}
if (sort_results) sort(result.begin(), result.end());
}
int KDTree::r_count_around_point(int idxin, int correltime, float r2)
{
std::vector<float> qv(dim); // query vector
for (int i=0; i<dim; i++) {
qv[i] = the_data[idxin][i];
}
// copy the query vector.
{
KDTreeResultVector result;
SearchRecord sr(qv, *this, result);
// construct the search record.
sr.centeridx = idxin;
sr.correltime = correltime;
sr.ballsize = r2;
sr.nn = 0;
root->search(sr);
return(result.size());
}
}
//
// KDTREE_NODE implementation
//
// constructor
KDTreeNode::KDTreeNode(int dim) : box(dim) {
left = right = NULL;
//
// all other construction is handled for real in the
// KDTree building operations.
//
}
// destructor
KDTreeNode::~KDTreeNode() {
if (left != NULL) delete left;
if (right != NULL) delete right;
// maxbox and minbox
// will be automatically deleted in their own destructors.
}
void KDTreeNode::search(SearchRecord& sr) {
// the core search routine.
// This uses true distance to bounding box as the
// criterion to search the secondary node.
//
// This results in somewhat fewer searches of the secondary nodes
// than 'search', which uses the vdiff vector, but as this
// takes more computational time, the overall performance may not
// be improved in actual run time.
//
if ( (left == NULL) && (right == NULL)) {
// we are on a terminal node
if (sr.nn == 0) {
process_terminal_node_fixedball(sr);
} else {
process_terminal_node(sr);
}
} else {
KDTreeNode *ncloser, *nfarther;
float extra;
float qval = sr.qv[cut_dim];
// value of the wall boundary on the cut dimension.
if (qval < cut_val) {
ncloser = left;
nfarther = right;
extra = cut_val_right-qval;
} else {
ncloser = right;
nfarther = left;
extra = qval-cut_val_left;
};
if (ncloser != NULL) ncloser->search(sr);
if ((nfarther != NULL) && (squared(extra) < sr.ballsize)) {
// first cut
if (nfarther->box_in_search_range(sr)) {
nfarther->search(sr);
}
}
}
}
inline float dis_from_bnd(float x, float amin, float amax) {
if (x > amax) {
return(x-amax);
} else if (x < amin)
return (amin-x);
else
return 0.0;
}
inline bool KDTreeNode::box_in_search_range(SearchRecord& sr) {
//
// does the bounding box, represented by minbox[*],maxbox[*]
// have any point which is within 'sr.ballsize' to 'sr.qv'??
//
int dim = sr.dim;
float dis2 =0.0;
float ballsize = sr.ballsize;
for (int i=0; i<dim;i++) {
dis2 += squared(dis_from_bnd(sr.qv[i],box[i].lower,box[i].upper));
if (dis2 > ballsize)
return(false);
}
return(true);
}
void KDTreeNode::process_terminal_node(SearchRecord& sr) {
int centeridx = sr.centeridx;
int correltime = sr.correltime;
unsigned int nn = sr.nn;
int dim = sr.dim;
float ballsize = sr.ballsize;
//
bool rearrange = sr.rearrange;
const KDTreeArray& data = *sr.data;
const bool debug = false;
if (debug) {
printf("Processing terminal node %d, %d\n",l,u);
std::cout << "Query vector = [";
for (int i=0; i<dim; i++) std::cout << sr.qv[i] << ',';
std::cout << "]\n";
std::cout << "nn = " << nn << '\n';
check_query_in_bound(sr);
}
for (int i=l; i<=u;i++) {
int indexofi; // sr.ind[i];
float dis;
bool early_exit;
if (rearrange) {
early_exit = false;
dis = 0.0;
for (int k=0; k<dim; k++) {
dis += squared(data[i][k] - sr.qv[k]);
if (dis > ballsize) {
early_exit=true;
break;
}
}
if(early_exit) continue; // next iteration of mainloop
// why do we do things like this? because if we take an early
// exit (due to distance being too large) which is common, then
// we need not read in the actual point index, thus saving main
// memory bandwidth. If the distance to point is less than the
// ballsize, though, then we need the index.
//
indexofi = sr.ind[i];
} else {
//
// but if we are not using the rearranged data, then
// we must always
indexofi = sr.ind[i];
early_exit = false;
dis = 0.0;
for (int k=0; k<dim; k++) {
dis += squared(data[indexofi][k] - sr.qv[k]);
if (dis > ballsize) {
early_exit= true;
break;
}
}
if(early_exit) continue; // next iteration of mainloop
} // end if rearrange.
if (centeridx > 0) {
// we are doing decorrelation interval
if (abs(indexofi-centeridx) < correltime) continue; // skip this point.
}
// here the point must be added to the list.
//
// two choices for any point. The list so far is either
// undersized, or it is not.
//
if (sr.result.size() < nn) {
KDTreeResult e;
e.idx = indexofi;
e.dis = dis;
sr.result.push_element_and_heapify(e);
if (debug) std::cout << "unilaterally pushed dis=" << dis;
if (sr.result.size() == nn) ballsize = sr.result.max_value();
// Set the ball radius to the largest on the list (maximum priority).
if (debug) {
std::cout << " ballsize = " << ballsize << "\n";
std::cout << "sr.result.size() = " << sr.result.size() << '\n';
}
} else {
//
// if we get here then the current node, has a squared
// distance smaller
// than the last on the list, and belongs on the list.
//
KDTreeResult e;
e.idx = indexofi;
e.dis = dis;
ballsize = sr.result.replace_maxpri_elt_return_new_maxpri(e);
if (debug) {
std::cout << "Replaced maximum dis with dis=" << dis <<
" new ballsize =" << ballsize << '\n';
}
}
} // main loop
sr.ballsize = ballsize;
}
void KDTreeNode::process_terminal_node_fixedball(SearchRecord& sr) {
int centeridx = sr.centeridx;
int correltime = sr.correltime;
int dim = sr.dim;
float ballsize = sr.ballsize;
//
bool rearrange = sr.rearrange;
const KDTreeArray& data = *sr.data;
for (int i=l; i<=u;i++) {
int indexofi = sr.ind[i];
float dis;
bool early_exit;
if (rearrange) {
early_exit = false;
dis = 0.0;
for (int k=0; k<dim; k++) {
dis += squared(data[i][k] - sr.qv[k]);
if (dis > ballsize) {
early_exit=true;
break;
}
}
if(early_exit) continue; // next iteration of mainloop
// why do we do things like this? because if we take an early
// exit (due to distance being too large) which is common, then
// we need not read in the actual point index, thus saving main
// memory bandwidth. If the distance to point is less than the
// ballsize, though, then we need the index.
//
indexofi = sr.ind[i];
} else {
//
// but if we are not using the rearranged data, then
// we must always
indexofi = sr.ind[i];
early_exit = false;
dis = 0.0;
for (int k=0; k<dim; k++) {
dis += squared(data[indexofi][k] - sr.qv[k]);
if (dis > ballsize) {
early_exit= true;
break;
}
}
if(early_exit) continue; // next iteration of mainloop
} // end if rearrange.
if (centeridx > 0) {
// we are doing decorrelation interval
if (abs(indexofi-centeridx) < correltime) continue; // skip this point.
}
{
KDTreeResult e;
e.idx = indexofi;
e.dis = dis;
sr.result.push_back(e);
}
}
}
} // namespace kdtee2