forked from WWDCScholars/iOS-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQNode.m
executable file
·307 lines (263 loc) · 8.8 KB
/
QNode.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
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
//
// This file is subject to the terms and conditions defined in
// file 'LICENSE.md', which is part of this source code package.
//
#import "QNode.h"
#import "QTreeGeometryUtils.h"
static const CLLocationDistance MinDistinguishableMetersDistance = 5;
static CLLocationDegrees DegreesMetric(CLLocationCoordinate2D c1, CLLocationCoordinate2D c2)
{
return sqrt(pow(c1.latitude - c2.latitude, 2) + pow(c1.longitude - c2.longitude, 2));
}
static CLLocationCoordinate2D MeanCoordinate(NSArray* insertableObjects)
{
CLLocationDegrees meanLatitude = 0;
CLLocationDegrees meanLongitude = 0;
for( id<QTreeInsertable> object in insertableObjects ) {
meanLongitude += object.coordinate.longitude;
meanLatitude += object.coordinate.latitude;
}
meanLatitude /= insertableObjects.count;
meanLongitude /= insertableObjects.count;
return CLLocationCoordinate2DMake(meanLatitude, meanLongitude);
}
static CLLocationDegrees CircumscribedDegreesRadius(NSArray* insertableObjects, CLLocationCoordinate2D center)
{
CLLocationDegrees radius = 0;
for( id<QTreeInsertable> object in insertableObjects ) {
radius = MAX(radius, DegreesMetric(object.coordinate, center));
}
return radius;
}
@interface QNode()
@property(nonatomic, assign) MKCoordinateRegion region;
@property(nonatomic, strong) id<QTreeInsertable> leadObject;
@property(nonatomic, strong) NSMutableSet* satellites;
@property(nonatomic, assign) NSUInteger count;
@property(nonatomic, strong) QCluster* cachedCluster;
@property(nonatomic, retain) QNode* upLeft;
@property(nonatomic, retain) QNode* upRight;
@property(nonatomic, retain) QNode* downLeft;
@property(nonatomic, retain) QNode* downRight;
@end
@implementation QNode
+(instancetype)nodeWithRegion:(MKCoordinateRegion)region
{
return [[QNode alloc] initWithRegion:region];
}
-(id)initWithRegion:(MKCoordinateRegion)region
{
self = [super init];
if( !self ) {
return nil;
}
self.region = region;
return self;
}
-(CLLocationDegrees)centerLatitude
{
return self.region.center.latitude;
}
-(CLLocationDegrees)centerLongitude
{
return self.region.center.longitude;
}
-(BOOL)isLeaf
{
return !self.upLeft && !self.downLeft && !self.upRight && !self.downRight;
}
-(BOOL)insertObject:(id<QTreeInsertable>)insertableObject
{
if( self.leadObject ) {
if( CLMetersBetweenCoordinates(self.leadObject.coordinate, insertableObject.coordinate) >= MinDistinguishableMetersDistance ) {
// Move self objects deeper
NSAssert([self isLeaf], @"Node containing objects should be a leaf");
[self insertLeadObject:self.leadObject withSatellites:self.satellites];
self.leadObject = nil;
self.satellites = nil;
} else {
if( ![self.leadObject isEqual:insertableObject] ) {
self.count += 1;
self.cachedCluster = nil;
if( !self.satellites ) {
self.satellites = [NSMutableSet set];
}
[self.satellites addObject:insertableObject];
return YES;
} else {
// Can't distinguish two objects
return NO;
}
}
}
if( [self insertLeadObject:insertableObject withSatellites:nil] ) {
self.count += 1;
return YES;
} else {
return NO;
}
}
-(BOOL)removeObject:(id<QTreeInsertable>)insertableObject
{
if( self.leadObject ) {
if( [self.satellites containsObject:insertableObject] ) {
[self.satellites removeObject:insertableObject];
self.cachedCluster = nil;
self.count -= 1;
return YES;
} else if( [self.leadObject isEqual:insertableObject] ) {
self.leadObject = [self.satellites anyObject];
if( self.leadObject ) {
[self.satellites removeObject:self.leadObject];
} // else should delete this node then
self.cachedCluster = nil;
self.count -= 1;
return YES;
} else {
return NO;
}
}
QNode* __strong *pNode = [self childNodeForObject:insertableObject];
if( *pNode ) {
BOOL result = [*pNode removeObject:insertableObject];
if( result ) {
self.cachedCluster = nil;
self.count -= 1;
if( (*pNode).count == 0 ) {
*pNode = nil;
}
QNode* __strong *pChild = [self theOnlyChildNode];
if( pChild != nil && [(*pChild) isLeaf] ) {
self.leadObject = (*pChild).leadObject;
self.satellites = (*pChild).satellites;
NSAssert(self.count == (*pChild).count, @"Should be in sync already");
*pChild = nil;
}
}
return result;
} else {
return NO;
}
}
-(QNode* __strong *)childNodeForObject:(id<QTreeInsertable>)insertableObject
{
QNode* __strong *pNode = nil;
const BOOL down = insertableObject.coordinate.latitude < self.centerLatitude;
const BOOL left = insertableObject.coordinate.longitude < self.centerLongitude;
if( down ) {
if( left ) {
pNode = &_downLeft;
} else {
pNode = &_downRight;
}
} else {
if( left ) {
pNode = &_upLeft;
} else {
pNode = &_upRight;
}
}
return pNode;
}
// returns nil if there are more than one child
-(QNode* __strong *)theOnlyChildNode
{
QNode* __strong *pChild = nil;
while( YES ) {
if( self.upLeft ) {
pChild = &_upLeft;
}
if( self.downLeft ) {
if( pChild ) {
pChild = nil;
break;
}
pChild = &_downLeft;
}
if( self.upRight ) {
if( pChild ) {
pChild = nil;
break;
}
pChild = &_upRight;
}
if( self.downRight ) {
if( pChild ) {
pChild = nil;
break;
}
pChild = &_downRight;
}
break;
}
return pChild;
}
-(BOOL)insertLeadObject:(id<QTreeInsertable>)leadObject withSatellites:(NSSet*)satellites
{
self.cachedCluster = nil;
QNode* __strong *pNode = [self childNodeForObject:leadObject];
if( !*pNode ) {
const BOOL down = leadObject.coordinate.latitude < self.centerLatitude;
const BOOL left = leadObject.coordinate.longitude < self.centerLongitude;
const CLLocationDegrees latDeltaBy2 = self.region.span.latitudeDelta / 2;
const CLLocationDegrees newLat = self.centerLatitude + latDeltaBy2 * (down ? -1 : +1) / 2;
const CLLocationDegrees lngDeltaBy2 = self.region.span.longitudeDelta / 2;
const CLLocationDegrees newLng = self.centerLongitude + lngDeltaBy2 * (left ? -1 : +1) / 2;
const CLLocationCoordinate2D newCenter = CLLocationCoordinate2DMake(newLat, newLng);
const MKCoordinateSpan newSpan = MKCoordinateSpanMake(latDeltaBy2, lngDeltaBy2);
QNode* newNode = [QNode nodeWithRegion:MKCoordinateRegionMake(newCenter, newSpan)];
newNode.leadObject = leadObject;
newNode.satellites = [satellites mutableCopy];
newNode.count = 1 + satellites.count;
*pNode = newNode;
return YES;
} else {
NSAssert(!satellites, @"Satellites should be non-nil only when moving objects deeper");
return [*pNode insertObject:leadObject];
}
}
-(NSArray*)getObjectsInRegion:(MKCoordinateRegion)region minNonClusteredSpan:(CLLocationDegrees)span
{
if( !MKCoordinateRegionIntersectsRegion(self.region, region) ) {
return @[];
}
NSMutableArray* result = [NSMutableArray array];
if( self.leadObject ) {
if( MKCoordinateRegionContainsCoordinate(region, self.leadObject.coordinate) ) {
[result addObject:self.leadObject];
[result addObjectsFromArray:self.satellites.allObjects];
}
} else if( MIN(self.region.span.latitudeDelta, self.region.span.longitudeDelta) >= span ) {
[result addObjectsFromArray:[self.upLeft getObjectsInRegion:region minNonClusteredSpan:span]];
[result addObjectsFromArray:[self.upRight getObjectsInRegion:region minNonClusteredSpan:span]];
[result addObjectsFromArray:[self.downLeft getObjectsInRegion:region minNonClusteredSpan:span]];
[result addObjectsFromArray:[self.downRight getObjectsInRegion:region minNonClusteredSpan:span]];
} else {
if( !self.cachedCluster ) {
QCluster* cluster = [[QCluster alloc] init];
NSArray* allChildren = [self getObjectsInRegion:self.region minNonClusteredSpan:0];
CLLocationCoordinate2D meanCenter = MeanCoordinate(allChildren);
cluster.coordinate = meanCenter;
cluster.objectsCount = allChildren.count;
cluster.radius = CircumscribedDegreesRadius(allChildren, meanCenter);
self.cachedCluster = cluster;
}
[result addObject:self.cachedCluster];
}
return result;
}
-(QNode*)childNodeForLocation:(CLLocationCoordinate2D)location
{
if( self.downRight && MKCoordinateRegionContainsCoordinate(self.downRight.region, location) ) {
return self.downRight;
} else if( self.downLeft && MKCoordinateRegionContainsCoordinate(self.downLeft.region, location) ) {
return self.downLeft;
} else if( self.upRight && MKCoordinateRegionContainsCoordinate(self.upRight.region, location) ) {
return self.upRight;
} else if( self.upLeft && MKCoordinateRegionContainsCoordinate(self.upLeft.region, location) ) {
return self.upLeft;
} else {
return nil;
}
}
@end