forked from alejandrojapkin/CocoaLUT
/
KDNode.m
executable file
·97 lines (79 loc) · 3.44 KB
/
KDNode.m
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
//
// KDNode.m
// KDTree
//
// Created by Bronson Brown-deVost on 9/16/13.
// Copyright (c) 2013 Bronson Brown-deVost. All rights reserved.
//
#import "KDNode.h"
@implementation KDNode
@synthesize splitPoint, dimension, right, left, rightLeaf, leftLeaf;
# pragma mark -
# pragma mark Initialize
-(id)initWithArray:(NSArray*)array withDimensions:(NSUInteger)dimensions andWithCurrentDimension:(NSUInteger)currentDimension {
if (self = [super init]) {
//Check to make sure we stay within the coordinate dimensions
if (currentDimension >= dimensions) {
currentDimension = 0;
}
dimension = currentDimension;
//Sort the array along the current dimension and find the center point
array = [self sortArray:array inDimension:currentDimension];
int centerPoint = [self findCenter:array inDimension:currentDimension];
//Set the mean value for this node
splitPoint = [self findMean:array inLayer:currentDimension];
//create children till we reach the end of the array (i.e. the array only holds one or two objects).
if ([array count] > 2) {
//Move to the next dimension
currentDimension++;
//Split off a left array and star building nodes on it.
NSRange leftRange = NSMakeRange(0, centerPoint);
NSArray *leftArray = [array subarrayWithRange: leftRange];
left = [[KDNode alloc] initWithArray:leftArray withDimensions:dimensions andWithCurrentDimension:currentDimension];
NSUInteger offsetLength = [array count] - centerPoint;
//Split off the right array and start building nodes on it.
NSRange rightRange = NSMakeRange(centerPoint, offsetLength);
NSArray *rightArray = [array subarrayWithRange: rightRange];
right = [[KDNode alloc] initWithArray:rightArray withDimensions:dimensions andWithCurrentDimension:currentDimension];
} else {
leftLeaf = [[KDLeaf alloc] initWithArray:[array objectAtIndex:0]];
if ([array count] > 1) {
rightLeaf = [[KDLeaf alloc] initWithArray:[array objectAtIndex:1]];
}
}
}
return self;
}
-(id)initWithArray:(NSArray*)array withDimensions:(NSUInteger)dimensions {
return [self initWithArray:array withDimensions:dimensions andWithCurrentDimension:0];
}
-(id)initWithArray:(NSArray*)array {
return [self initWithArray:array withDimensions:[[array objectAtIndex:0] count]-1];
}
# pragma mark -
# pragma mark Sorting and Processing
//This sorts an array along a specified dimension
-(NSArray*)sortArray:(NSArray*)array inDimension:(NSUInteger)layer {
array = [array sortedArrayUsingComparator:^(id a, id b) {
NSNumber *coordinateA = [a objectAtIndex:layer];
NSNumber *coordinateB = [b objectAtIndex:layer];
return [coordinateA compare:coordinateB];
}];
return array;
}
//This finds the mean in a given array
-(CGFloat)findMean:(NSArray*)array inLayer:(NSUInteger)layer{
CGFloat mean = 0;
for (NSArray *currentValue in array){
mean += [[currentValue objectAtIndex:layer] doubleValue];
}
mean /= [array count];
return mean;
}
//This passes back the index of the center of the array (always rounded down)
-(int)findCenter:(NSArray*)array inDimension:(NSUInteger)dimension {
int center;
center = (int)[array count]/2;
return center;
}
@end