[26cac0]: KDTree.c Maximize Restore History

Download this file

KDTree.c    926 lines (756 with data), 24.0 kB

  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
/* autopano-sift, Automatic panorama image creation
* Copyright (C) 2004 -- Sebastian Nowozin
*
* This program is free software released under the GNU General Public
* License, which is included in this software package (doc/LICENSE).
*/
/* KDTree.cs
*
* A vanilla k-d tree implementation.
*
* (C) Copyright 2004 -- Sebastian Nowozin (nowozin@cs.tu-berlin.de)
*
* Based on "An introductory tutorial on kd-trees" by Andrew W. Moore,
* available at http://www.ri.cmu.edu/pubs/pub_2818.html
*/
#include "AutoPanoSift.h"
/* SortedLimitedList replacement by Eric Engle
*
* Changes:
* Modified Add(), and implemented Set to handle node setting semantics
* not provided by the .NET library.
*
* Performance notes:
* This routine uses a simple insertion sort to handle calls to Add.
* Each element is compared from right to left. If obj is smaller than the
* current array object, that object is slid to the right. Otherwise the hole
* from the last slide operation is used to hold obj.
* Most of the calls to Add() will return -1, i.e. in the normal case only a
* fraction of the items will be smaller than the largest item on the list.
* This common case is recognized in the first comparison. Iteration occurs
* only for those items that belong on the list, so for the normal case self
* operation is faster than it's strictly linear performance would suggest.
*/
SortedLimitedList* SortedLimitedList_new0 ()
{
SortedLimitedList* self = (SortedLimitedList*)malloc(sizeof(SortedLimitedList));
return self;
}
void SortedLimitedList_delete (SortedLimitedList* self)
{
ArrayList_delete(&self->base);
}
SortedLimitedList* SortedLimitedList_new (int maxElements, void* deletefn)
{
SortedLimitedList* self = SortedLimitedList_new0();
ArrayList_init((ArrayList*)self, deletefn);
self->max = maxElements;
self->deletefn = deletefn;
return self;
}
// Sets the argument index to the argument object.
// Replaces the node if it already exists,
// adds a new node if at the end of the list,
// does nothing otherwise.
void SortedLimitedList_SetItem (SortedLimitedList* self, int idx, void* value)
{
if (idx < ArrayList_Count(&self->base)) {
ArrayList_SetItem(&self->base, idx, value);
} else if (idx == ArrayList_Count(&self->base)) { // TODO: should check for max?
ArrayList_AddItem(&self->base, value);
} else {
if (self->deletefn) {
self->deletefn(value);
}
}
}
int SortedLimitedList_Count (SortedLimitedList* self)
{
return ArrayList_Count(&self->base);
}
void* SortedLimitedList_GetItem (SortedLimitedList* self, int i)
{
return ArrayList_GetItem(&self->base, i);
}
void SortedLimitedList_RemoveAt (SortedLimitedList* self, int i)
{
return ArrayList_RemoveAt(&self->base, i);
}
// Processes list from right to left, sliding each node that is greater
// than 'self' to the right. The loop ends when either the first node is
// reached, meaning obj is a new minimum, or it's proper sorted position
// in the list is reached.
// Returns position of obj or -1 if obj was not placed.
int SortedLimitedList_AddItem (SortedLimitedList* self, void* value)
{
int pos = ArrayList_Count(&self->base);
while (pos > 0 && self->comparator.compareTo(&self->comparator, ArrayList_GetItem(&self->base, pos-1), value) >= 0) {
if (pos < self->max) {
if (pos==self->max-1) {
if (ArrayList_Count(&self->base) == self->max) {
if (self->deletefn) {
self->deletefn(ArrayList_GetItem(&self->base, pos));
}
}
}
SortedLimitedList_SetItem(self, pos, ArrayList_GetItem(&self->base, pos-1));
}
pos --;
}
if (pos < self->max) {
if (pos==self->max-1) {
if (ArrayList_Count(&self->base) == self->max) {
if (self->deletefn) {
self->deletefn(ArrayList_GetItem(&self->base, pos));
}
}
}
SortedLimitedList_SetItem(self, pos, value);
} else {
if (self->deletefn) {
self->deletefn(value);
}
pos = -1;
}
return pos;
}
int IKDTreeDomain_GetDimensionCount(IKDTreeDomain* self)
{
return self->getDimensionCount(self);
}
int IKDTreeDomain_GetDimensionElement(IKDTreeDomain* self, int dim)
{
return self->getDimensionElement(self, dim);
}
KDTree* KDTree_new0 ()
{
KDTree* self = (KDTree*)malloc(sizeof(KDTree));
self->left = NULL;
self->right = NULL;
self->dr = NULL;
return self;
}
void KDTree_delete(KDTree* self)
{
if (self) {
KDTree_delete(self->left);
KDTree_delete(self->right);
// TODO: KDTreeDomain_delete(self->dr)?
free(self);
}
}
bool KDTreeBestEntry_Equals (KDTreeBestEntry* be1, KDTreeBestEntry* be2)
{
return (be1->neighbour == be2->neighbour);
}
KDTreeBestEntry* KDTreeBestEntry_new0()
{
KDTreeBestEntry* self = (KDTreeBestEntry*)malloc(sizeof(KDTreeBestEntry));
return self;
}
void KDTreeBestEntry_delete(KDTreeBestEntry* self)
{
if (self) {
free(self);
}
}
KDTreeBestEntry* KDTreeBestEntry_new3 (IKDTreeDomain* neighbour, int distanceSq, bool squared)
{
KDTreeBestEntry* self= KDTreeBestEntry_new0();
self->neighbour = neighbour;
self->distanceSq = distanceSq;
self->squared = true;
return self;
}
KDTreeBestEntry* KDTreeBestEntry_new (IKDTreeDomain* neighbour, double dist)
{
KDTreeBestEntry* self= KDTreeBestEntry_new0();
self->neighbour = neighbour;
self->distance = dist;
self->squared = false;
return self;
}
IKDTreeDomain* KDTreeBestEntry_Neighbour (KDTreeBestEntry* self)
{
return self->neighbour;
}
int KDTreeBestEntry_CompareTo (IComparator* self, KDTreeBestEntry* be1, KDTreeBestEntry* be2)
{
if (be1->squared) {
if (be1->distanceSq < be2->distanceSq)
return (-1);
else if (be1->distanceSq > be2->distanceSq)
return (1);
return (0);
} else {
if (be1->distance < be2->distance)
return (-1);
else if (be1->distance > be2->distance)
return (1);
return (0);
}
}
KDTreeHREntry* KDTreeHREntry_new0()
{
KDTreeHREntry* self = (KDTreeHREntry*)malloc(sizeof(KDTreeHREntry));
self->rect = NULL;
self->tree = NULL;
self->pivot = NULL;
return self;
}
void KDTreeHREntry_delete(KDTreeHREntry* self)
{
if (self) {
HyperRectangle_unref(self->rect);
//KDTree_delete(self->tree);
//IKDTreeDomain_delete(self->pivot);
free(self);
}
}
KDTreeHREntry* KDTreeHREntry_new (HyperRectangle* rect, KDTree* tree, IKDTreeDomain* pivot,
double dist)
{
KDTreeHREntry* self = KDTreeHREntry_new0();
self->rect = HyperRectangle_ref(rect);
self->tree = tree;
self->pivot = pivot;
self->dist = dist;
return self;
}
int KDTreeHREntry_CompareTo (IComparator* self, KDTreeHREntry* hre1, KDTreeHREntry* hre2)
{
if (hre1->dist < hre2->dist)
return (-1);
else if (hre1->dist > hre2->dist)
return (1);
return (0);
}
HyperRectangle* HyperRectangle_new0()
{
HyperRectangle* self = (HyperRectangle*)malloc(sizeof(HyperRectangle));
self->ref = 0;
return self;
}
void HyperRectangle_delete (HyperRectangle* self)
{
if (self) {
if (self->leftTop) {
free(self->leftTop);
self->leftTop = NULL;
}
if (self->rightBottom) {
free(self->rightBottom);
self->rightBottom = NULL;
}
self->dim = 0;
free(self);
}
}
HyperRectangle* HyperRectangle_ref (HyperRectangle* self)
{
self->ref++;
return self;
}
void HyperRectangle_unref (HyperRectangle* self)
{
self->ref--;
if (self->ref == 0) {
//HyperRectangle_delete(self);
}
}
HyperRectangle* HyperRectangle_new (int dim)
{
HyperRectangle* self = HyperRectangle_new0();
self->dim = dim;
self->leftTop = (int*)malloc(dim*sizeof(int));
self->rightBottom = (int*)malloc(dim*sizeof(int));
return self;
}
HyperRectangle* HyperRectangle_clone (HyperRectangle* self)
{
HyperRectangle* rec = HyperRectangle_new (self->dim);
int n;
for ( n = 0 ; n < self->dim ; ++n) {
rec->leftTop[n] = self->leftTop[n];
rec->rightBottom[n] = self->rightBottom[n];
}
return (rec);
}
HyperRectangle* HyperRectangle_CreateUniverseRectangle (int dim)
{
HyperRectangle* rec = HyperRectangle_new (dim);
int n;
for ( n = 0 ; n < dim ; ++n) {
rec->leftTop[n] = INT_MIN;
rec->rightBottom[n] = INT_MAX;
}
return (rec);
}
HyperRectangle* HyperRectangle_SplitAt (HyperRectangle* self, int splitDim, int splitVal)
{
if (self->leftTop[splitDim] >= splitVal || self->rightBottom[splitDim] < splitVal)
FatalError("SplitAt with splitpoint outside rec");
HyperRectangle* r2 = HyperRectangle_clone(self);
self->rightBottom[splitDim] = splitVal;
r2->leftTop[splitDim] = splitVal;
return (r2);
}
bool HyperRectangle_IsIn (HyperRectangle* self, IKDTreeDomain* target)
{
if (IKDTreeDomain_GetDimensionCount(target) != self->dim)
FatalError ("IsIn dimension mismatch");
int n;
for (n = 0 ; n < self->dim ; ++n) {
int targD = IKDTreeDomain_GetDimensionElement(target, n);
if (targD < self->leftTop[n] || targD >= self->rightBottom[n])
return (false);
}
return (true);
}
// Return true if any part of this HR is reachable from target by no
// more than 'distRad', false otherwise.
// The algorithm is specified in the kd-tree paper mentioned at the
// top of this file, in section 6-7. But there is a mistake in the
// third distinct case, which should read "hrMax" instead of "hrMin".
bool HyperRectangle_IsInReach (HyperRectangle* self, IKDTreeDomain* target, double distRad)
{
return (HyperRectangle_Distance (self, target) < distRad);
}
// Return the distance from the nearest point from within the HR to
// the target point.
double HyperRectangle_Distance (HyperRectangle* self, IKDTreeDomain* target)
{
int closestPointN;
int distance = 0;
// first compute the closest point within hr to the target. if
// this point is within reach of target, then there is an
// intersection between the hypersphere around target with radius
// 'dist' and this hyperrectangle.
int n;
for ( n = 0 ; n < self->dim ; ++n) {
int tI = IKDTreeDomain_GetDimensionElement (target, n);
int hrMin = self->leftTop[n];
int hrMax = self->rightBottom[n];
closestPointN = 0;
if (tI <= hrMin) {
closestPointN = hrMin;
} else if (tI > hrMin && tI < hrMax) {
closestPointN = tI;
} else if (tI >= hrMax) {
closestPointN = hrMax;
}
int dimDist = tI - closestPointN;
distance += dimDist * dimDist;
}
return (sqrt ((double) distance));
}
// Find the nearest neighbour to the hyperspace point 'target' within the
// kd-tree. After return 'resDist' contains the absolute distance from the
// target point. The nearest neighbour is returned.
IKDTreeDomain* KDTree_NearestNeighbour (KDTree* self, IKDTreeDomain* target, double* resDist)
{
ArrayList* hrl = ArrayList_new0(HyperRectangle_delete);
HyperRectangle* hr =
HyperRectangle_CreateUniverseRectangle (IKDTreeDomain_GetDimensionCount(target));
ArrayList_AddItem(hrl, hr);
IKDTreeDomain* nearest = KDTree_NearestNeighbourI (self, target, hr,
Double_PositiveInfinity, resDist, hrl);
*resDist = sqrt (*resDist);
ArrayList_delete(hrl);
return (nearest);
}
// Internal recursive algorithm for the kd-tree nearest neighbour search.
IKDTreeDomain* KDTree_NearestNeighbourI (KDTree* self, IKDTreeDomain* target, HyperRectangle* hr,
double maxDistSq, double* resDistSq, ArrayList* hrl)
{
//WriteLine ("C NearestNeighbourI called");
*resDistSq = Double_PositiveInfinity;
IKDTreeDomain* pivot = self->dr;
HyperRectangle* leftHr = hr;
HyperRectangle* rightHr = HyperRectangle_SplitAt(leftHr, self->splitDim,
IKDTreeDomain_GetDimensionElement (pivot, self->splitDim));
ArrayList_AddItem(hrl, rightHr);
HyperRectangle* nearerHr = NULL;
HyperRectangle* furtherHr =NULL;
KDTree* nearerKd = NULL;
KDTree* furtherKd = NULL;
// step 5-7
if (IKDTreeDomain_GetDimensionElement (target, self->splitDim) <=
IKDTreeDomain_GetDimensionElement (pivot, self->splitDim))
{
nearerKd = self->left;
nearerHr = leftHr;
furtherKd = self->right;
furtherHr = rightHr;
} else {
nearerKd = self->right;
nearerHr = rightHr;
furtherKd = self->left;
furtherHr = leftHr;
}
// step 8
IKDTreeDomain* nearest = NULL;
double distSq;
if (nearerKd == NULL) {
distSq = Double_PositiveInfinity;
} else {
nearest = KDTree_NearestNeighbourI (nearerKd, target, nearerHr,
maxDistSq, &distSq, hrl);
}
// step 9
maxDistSq = min (maxDistSq, distSq);
// step 10
if (HyperRectangle_IsInReach (furtherHr, target, sqrt (maxDistSq))) {
double ptDistSq = KDTree_DistanceSq (pivot, target);
if (ptDistSq < distSq) {
// steps 10.1.1 to 10.1.3
nearest = pivot;
distSq = ptDistSq;
maxDistSq = distSq;
}
// step 10.2
double tempDistSq;
IKDTreeDomain* tempNearest = NULL;
if (furtherKd == NULL) {
tempDistSq = Double_PositiveInfinity;
} else {
tempNearest = KDTree_NearestNeighbourI (furtherKd, target,
furtherHr, maxDistSq, & tempDistSq, hrl);
}
// step 10.3
if (tempDistSq < distSq) {
nearest = tempNearest;
distSq = tempDistSq;
}
}
*resDistSq = distSq;
return (nearest);
}
SortedLimitedList* KDTree_NearestNeighbourList (KDTree* self, IKDTreeDomain* target,
double* resDist, int q)
{
ArrayList* hrl = ArrayList_new0(HyperRectangle_delete);
HyperRectangle* hr =
HyperRectangle_CreateUniverseRectangle (IKDTreeDomain_GetDimensionCount(target));
ArrayList_AddItem(hrl, hr);
SortedLimitedList* best = SortedLimitedList_new (q, KDTreeBestEntry_delete);
best->comparator.compareTo = (void*)KDTreeBestEntry_CompareTo;
/*IKDTreeDomain* nearest = */KDTree_NearestNeighbourListI (self, best, q, target, hr,
Double_PositiveInfinity, resDist, hrl);
*resDist = sqrt (*resDist);
int i;
for(i=0; i<SortedLimitedList_Count(best); i++) {
KDTreeBestEntry* be = SortedLimitedList_GetItem(best, i);
be->distance = sqrt (be->distance);
}
ArrayList_delete(hrl);
return (best);
}
IKDTreeDomain* KDTree_NearestNeighbourListI (KDTree* self, SortedLimitedList* best,
int q, IKDTreeDomain* target, HyperRectangle* hr, double maxDistSq,
double* resDistSq, ArrayList* hrl)
{
//WriteLine ("C NearestNeighbourI called");
*resDistSq = Double_PositiveInfinity;
IKDTreeDomain* pivot = self->dr;
KDTreeBestEntry* be = KDTreeBestEntry_new (self->dr, KDTree_DistanceSq (target, self->dr));
SortedLimitedList_AddItem (best, be);
HyperRectangle* leftHr = hr;
HyperRectangle* rightHr = HyperRectangle_SplitAt (leftHr, self->splitDim,
IKDTreeDomain_GetDimensionElement (pivot, self->splitDim));
ArrayList_AddItem(hrl, rightHr);
HyperRectangle* nearerHr = NULL;
HyperRectangle* furtherHr = NULL;
KDTree* nearerKd = NULL;
KDTree* furtherKd = NULL;
// step 5-7
if (IKDTreeDomain_GetDimensionElement (target, self->splitDim) <=
IKDTreeDomain_GetDimensionElement (pivot, self->splitDim))
{
nearerKd = self->left;
nearerHr = leftHr;
furtherKd = self->right;
furtherHr = rightHr;
} else {
nearerKd = self->right;
nearerHr = rightHr;
furtherKd = self->left;
furtherHr = leftHr;
}
// step 8
IKDTreeDomain* nearest = NULL;
double distSq;
// No child, bottom reached!
if (nearerKd == NULL) {
distSq = Double_PositiveInfinity;
} else {
nearest = KDTree_NearestNeighbourListI (nearerKd, best, q, target, nearerHr,
maxDistSq, &distSq, hrl);
}
// step 9
//maxDistSq = Math.Min (maxDistSq, distanceSq);
if (SortedLimitedList_Count(best) >= q)
maxDistSq = ((KDTreeBestEntry*) SortedLimitedList_GetItem(best, q - 1))->distance;
else
maxDistSq = Double_PositiveInfinity;
// step 10
if (HyperRectangle_IsInReach (furtherHr, target, sqrt (maxDistSq))) {
double ptDistSq = KDTree_DistanceSq (pivot, target);
if (ptDistSq < distSq) {
// steps 10.1.1 to 10.1.3
nearest = pivot;
distSq = ptDistSq;
// TODO: use k-element list
/*
best.Add (new BestEntry (pivot, ptDistSq));
best.Sort ();
*/
maxDistSq = distSq;
}
// step 10.2
double tempDistSq;
IKDTreeDomain* tempNearest = NULL;
if (furtherKd == NULL) {
tempDistSq = Double_PositiveInfinity;
} else {
tempNearest = KDTree_NearestNeighbourListI (furtherKd, best, q, target,
furtherHr, maxDistSq, &tempDistSq, hrl);
}
// step 10.3
if (tempDistSq < distSq) {
nearest = tempNearest;
distSq = tempDistSq;
}
}
*resDistSq = distSq;
return (nearest);
}
// Limited Best-Bin-First k-d-tree nearest neighbour search.
//
// (Using the algorithm described in the paper "Shape indexing using
// approximate nearest-neighbour search in high-dimensional spaces",
// available at http://www.cs.ubc.ca/spider/lowe/papers/cvpr97-abs.html)
//
// Find the approximate nearest neighbour to the hyperspace point 'target'
// within the kd-tree using 'searchSteps' tail recursions at most (each
// recursion deciding about one neighbours' fitness).
//
// After return 'resDist' contains the absolute distance of the
// approximate nearest neighbour from the target point. The approximate
// nearest neighbour is returned.
SortedLimitedList* KDTree_NearestNeighbourListBBF (KDTree* self, IKDTreeDomain* target,
int q, int searchSteps)
{
ArrayList* hrl = ArrayList_new0(HyperRectangle_delete);
HyperRectangle* hr =
HyperRectangle_CreateUniverseRectangle (IKDTreeDomain_GetDimensionCount(target));
ArrayList_AddItem(hrl, hr);
SortedLimitedList* best = SortedLimitedList_new (q, KDTreeBestEntry_delete);
best->comparator.compareTo = (void*)KDTreeBestEntry_CompareTo;
SortedLimitedList* searchHr = SortedLimitedList_new (searchSteps, KDTreeHREntry_delete);
searchHr->comparator.compareTo = (void*)KDTreeHREntry_CompareTo;
int dummyDist;
/*IKDTreeDomain* nearest = */KDTree_NearestNeighbourListBBFI (self, best, q, target, hr,
INT_MAX, &dummyDist, searchHr, &searchSteps,
hrl);
SortedLimitedList_delete(searchHr);
int i;
for(i=0; i<SortedLimitedList_Count(best); i++) {
KDTreeBestEntry* be = SortedLimitedList_GetItem(best, i);
be->distance = sqrt (be->distanceSq);
}
ArrayList_delete(hrl);
return (best);
}
IKDTreeDomain* KDTree_NearestNeighbourListBBFI (KDTree* self, SortedLimitedList* best,
int q, IKDTreeDomain* target, HyperRectangle* hr, int maxDistSq,
int* resDistSq, SortedLimitedList* searchHr, int* searchSteps,
ArrayList* hrl)
{
//WriteLine ("C NearestNeighbourI called");
*resDistSq = INT_MAX;
IKDTreeDomain* pivot = self->dr;
KDTreeBestEntry* be = KDTreeBestEntry_new3 (self->dr, KDTree_DistanceSq (target, self->dr), true);
SortedLimitedList_AddItem(best, be);
HyperRectangle* leftHr = hr;
HyperRectangle* rightHr = HyperRectangle_SplitAt (leftHr, self->splitDim,
IKDTreeDomain_GetDimensionElement (pivot, self->splitDim));
ArrayList_AddItem(hrl, rightHr);
HyperRectangle* nearerHr = NULL;
HyperRectangle* furtherHr = NULL;
KDTree* nearerKd = NULL;
KDTree* furtherKd = NULL;
// step 5-7
if (IKDTreeDomain_GetDimensionElement (target, self->splitDim) <=
IKDTreeDomain_GetDimensionElement (pivot, self->splitDim))
{
nearerKd = self->left;
nearerHr = leftHr;
furtherKd = self->right;
furtherHr = rightHr;
} else {
nearerKd = self->right;
nearerHr = rightHr;
furtherKd = self->left;
furtherHr = leftHr;
}
// step 8
IKDTreeDomain* nearest = NULL;
int distSq;
KDTreeHREntry* hre = KDTreeHREntry_new (furtherHr, furtherKd, pivot,
HyperRectangle_Distance (furtherHr, target));
SortedLimitedList_AddItem (searchHr, hre);
// No child, bottom reached!
if (nearerKd == NULL) {
distSq = INT_MAX;
} else {
nearest = KDTree_NearestNeighbourListBBFI (nearerKd, best, q, target, nearerHr,
maxDistSq, &distSq, searchHr, searchSteps, hrl);
}
// step 9
if (SortedLimitedList_Count(best) >= q) {
maxDistSq = ((KDTreeBestEntry*) SortedLimitedList_GetItem(best,q - 1))->distanceSq;
} else
maxDistSq = INT_MAX;
if (SortedLimitedList_Count(searchHr) > 0) {
KDTreeHREntry* hre = SortedLimitedList_GetItem(searchHr, 0);
SortedLimitedList_RemoveAt (searchHr, 0);
furtherHr = hre->rect;
furtherKd = hre->tree;
pivot = hre->pivot;
KDTreeHREntry_delete(hre);
}
// step 10
*searchSteps -= 1;
if (*searchSteps > 0 &&
HyperRectangle_IsInReach (furtherHr, target, sqrt (maxDistSq)))
{
int ptDistSq = KDTree_DistanceSq (pivot, target);
if (ptDistSq < distSq) {
// steps 10.1.1 to 10.1.3
nearest = pivot;
distSq = ptDistSq;
maxDistSq = distSq;
}
// step 10.2
int tempDistSq;
IKDTreeDomain* tempNearest = NULL;
if (furtherKd == NULL) {
tempDistSq = INT_MAX;
} else {
tempNearest = KDTree_NearestNeighbourListBBFI (furtherKd, best, q,
target, furtherHr, maxDistSq, &tempDistSq, searchHr,
searchSteps, hrl);
}
// step 10.3
if (tempDistSq < distSq) {
nearest = tempNearest;
distSq = tempDistSq;
}
}
*resDistSq = distSq;
return (nearest);
}
int KDTree_DistanceSq (IKDTreeDomain* t1, IKDTreeDomain* t2)
{
int distance = 0;
int n;
for ( n = 0 ; n < IKDTreeDomain_GetDimensionCount(t1) ; ++n) {
int dimDist = IKDTreeDomain_GetDimensionElement (t1, n) -
IKDTreeDomain_GetDimensionElement (t2, n);
distance += dimDist * dimDist;
}
return (distance);
}
IKDTreeDomain* KDTree_GoodCandidate (ArrayList* exset, int* splitDim)
{
IKDTreeDomain* first = ArrayList_GetItem(exset, 0);
if (first == NULL) {
FatalError ("Not of type IKDTreeDomain (TODO: custom exception)");
}
int dim = IKDTreeDomain_GetDimensionCount(first);
// initialize temporary hr search min/max values
double* minHr = (double*)malloc(sizeof(double)* dim);
double* maxHr = (double*)malloc(sizeof(double)* dim);
int i;
for (i = 0 ; i < dim ; ++i) {
minHr[i] = Double_PositiveInfinity;
maxHr[i] = Double_NegativeInfinity;
}
int j;
for(j=0; j<ArrayList_Count(exset); j++) {
IKDTreeDomain* dom = ArrayList_GetItem(exset, j);
int k;
for (k = 0 ; k < dim ; ++k) {
double dimE = IKDTreeDomain_GetDimensionElement (dom, k);
if (dimE < minHr[k])
minHr[k] = dimE;
if (dimE > maxHr[k])
maxHr[k] = dimE;
}
}
// find the maximum range dimension
double* diffHr = (double*)malloc(sizeof(double)* dim);
int maxDiffDim = 0;
double maxDiff = 0.0;
int k;
for (k = 0 ; k < dim ; ++k) {
diffHr[k] = maxHr[k] - minHr[k];
if (diffHr[k] > maxDiff) {
maxDiff = diffHr[k];
maxDiffDim = k;
}
}
free(maxHr); maxHr = NULL;
// the splitting dimension is maxDiffDim
// now find a exemplar as close to the arithmetic middle as possible
double middle = (maxDiff / 2.0) + minHr[maxDiffDim];
IKDTreeDomain* exemplar = NULL;
double exemMinDiff = Double_PositiveInfinity;
free(minHr); minHr=NULL;
int l;
for(l=0; l<ArrayList_Count(exset); l++) {
IKDTreeDomain* dom = ArrayList_GetItem(exset, l);
double curDiff = abs (IKDTreeDomain_GetDimensionElement (dom, maxDiffDim) - middle);
if (curDiff < exemMinDiff) {
exemMinDiff = curDiff;
exemplar = dom;
}
}
free(diffHr); diffHr = NULL;
// return the values
*splitDim = maxDiffDim;
return (exemplar);
}
// Build a kd-tree from a list of elements. All elements must implement
// the IKDTreeDomain interface and must have the same dimensionality.
KDTree* KDTree_CreateKDTree (ArrayList* exset)
{
if (ArrayList_Count(exset) == 0)
return (NULL);
KDTree* cur = KDTree_new0 ();
cur->dr = KDTree_GoodCandidate (exset, &cur->splitDim);
ArrayList* leftElems = ArrayList_new0 (NULL);
ArrayList* rightElems = ArrayList_new0 (NULL);
// split the exemplar set into left/right elements relative to the
// splitting dimension
double bound = IKDTreeDomain_GetDimensionElement (cur->dr, cur->splitDim);
int i;
for(i=0; i<ArrayList_Count(exset); i++) {
IKDTreeDomain* dom = ArrayList_GetItem(exset, i);
// ignore the current element
if (dom == cur->dr)
continue;
if (IKDTreeDomain_GetDimensionElement (dom, cur->splitDim) <= bound) {
ArrayList_AddItem(leftElems, dom);
} else {
ArrayList_AddItem(rightElems, dom);
}
}
// recurse
cur->left = KDTree_CreateKDTree (leftElems);
cur->right = KDTree_CreateKDTree (rightElems);
ArrayList_delete(leftElems);
ArrayList_delete(rightElems);
return (cur);
}