-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuadTree.swift
executable file
·356 lines (287 loc) · 11 KB
/
QuadTree.swift
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
//
// QuadTree.swift
// DIRStage
//
// Created by Adi Mathew on 8/7/14.
// Copyright (c) 2014 RCPD. All rights reserved.
//
import CoreLocation
enum QTRNodeQuadrant: Int {
case NE = 0, SE, SW, NW
}
//Basic Conversion functions.From DSAlgorithm.
func radianToDegrees(radians: Double) -> Double
{
return (radians * (180/M_PI))
}
func degreesToRadians(degrees: Double) -> Double
{
return (degrees * (M_PI/180))
}
///Returns 1/-1 depending on sign of value.
func sgn(value: Double) -> Int
{
return Int(abs(value)/value)
}
private func bboxAroundCoordinate(coordinate: CLLocationCoordinate2D, withDistance distance: CLLocationDistance) -> [CLLocationDegrees]
{
let MIN_LAT = -M_PI_2
let MAX_LAT = M_PI_2
let MIN_LONG = -M_PI
let MAX_LONG = M_PI
let R: Double = 6378137
let r = distance/R
if CLLocationCoordinate2DIsValid(coordinate) {
let latRadian = degreesToRadians(coordinate.latitude)
let longRadian = degreesToRadians(coordinate.longitude)
var latMin = latRadian - r
var latMax = latRadian + r
var longMin = 0.0
var longMax = 0.0
if latMin > MIN_LAT && latMax < MAX_LAT {
let deltaLong = asin(sin(r)/cos(latRadian))
longMin = longRadian - deltaLong
if longMin < MIN_LONG {
longMin += 2.0 * M_PI
}
longMax = longRadian + deltaLong
if longMax > MAX_LONG {
longMax -= 2.0 * M_PI
}
}
else {
latMin = max(latMin, MIN_LAT)
latMax = min(latMax, MAX_LAT)
longMin = MIN_LONG
longMax = MAX_LONG
}
return [longMin, latMin, longMax, latMax].map{
(var rads) -> CLLocationDegrees in
return radianToDegrees(rads)
}
}
return []
}
struct QTRSpan {
var longitudeDelta: CLLocationDegrees
var latitudeDelta: CLLocationDegrees
init (_ longitudeDelta: CLLocationDegrees, _ latitudeDelta: CLLocationDegrees) {
self.latitudeDelta = latitudeDelta
self.longitudeDelta = longitudeDelta
}
init (_ bbox: [CLLocationDegrees]) {
self.latitudeDelta = bbox[3] - bbox[1]
self.longitudeDelta = bbox[2] - bbox[0]
}
}
class QTRNodePoint {
var latitude: Double?
var longitude: Double?
var name: String
var coordinate2D: CLLocationCoordinate2D {
get {
return CLLocationCoordinate2DMake(self.latitude!, self.longitude!)
}
}
init (_ latitude: Double, _ longitude: Double, _ name: String) {
self.latitude = latitude
self.longitude = longitude
self.name = name
// super.init()
}
init (_ coordinate: CLLocationCoordinate2D, _ name: String) {
self.latitude = coordinate.latitude
self.longitude = coordinate.longitude
self.name = name
}
init(_ coordinate: [CLLocationDegrees]) {
self.latitude = coordinate[1]
self.longitude = coordinate[0]
self.name = "Unknown"
}
}
class QTRBBox {
var lowLatitude: Double
var highLatitude: Double
var lowLongitude: Double
var highLongitude: Double
var center: CLLocationCoordinate2D {
get {
return centerOfBoundingBox([self.lowLongitude, self.lowLatitude, self.highLongitude, self.highLatitude])
}
}
var span: QTRSpan
init (_ lowLongitude: Double, _ lowLatitude: Double, _ highLongitude: Double, _ highLatitude: Double) {
self.lowLatitude = lowLatitude
self.highLatitude = highLatitude
self.lowLongitude = lowLongitude
self.highLongitude = highLongitude
self.span = QTRSpan([lowLongitude, lowLatitude, highLongitude, highLatitude])
// super.init()
}
init (_ bbox: [CLLocationDegrees]) {
self.lowLatitude = bbox[1]
self.highLatitude = bbox[3]
self.lowLongitude = bbox[0]
self.highLongitude = bbox[2]
self.span = QTRSpan(bbox)
}
convenience init (_ midpoint: CLLocationCoordinate2D, _ radius: Double) {
self.init(bboxAroundCoordinate(midpoint, withDistance: radius))
}
private func centerOfBoundingBox(bbox: [CLLocationDegrees]) -> CLLocationCoordinate2D
{
// let upperRight = CLLocationCoordinate2DMake(bbox[3], bbox[2])
// let lowerLeft = CLLocationCoordinate2DMake(bbox[1], bbox[0])
// if (CLLocationCoordinate2DIsValid(upperRight) && CLLocationCoordinate2DIsValid(lowerLeft) ){
// //Using C functions. Try Swift's trig functions too. USE Radians.
// if (upperRight.latitude - lowerLeft.latitude >= 3.0) || (upperRight.longitude - lowerLeft.longitude >= 3.0) {
// let longitudeDelta = degreesToRadians((upperRight.longitude - lowerLeft.longitude))
//
// let bX = cos(degreesToRadians(upperRight.latitude)) * cos(longitudeDelta)
// let bY = cos(degreesToRadians(upperRight.latitude)) * sin(longitudeDelta)
//
// let midLatitude = atan2(sin(degreesToRadians(lowerLeft.latitude)) + sin(degreesToRadians(upperRight.latitude)), sqrt(pow(cos(degreesToRadians(lowerLeft.latitude)) + bX, 2) + pow(bY, 2)))
// let midLongitude = degreesToRadians(lowerLeft.longitude) + atan2(bY, cos(degreesToRadians(lowerLeft.latitude)) + bX)
//
// return CLLocationCoordinate2DMake(radianToDegrees(midLatitude), radianToDegrees(midLongitude))
// }
// }
return CLLocationCoordinate2DMake((bbox[3] + bbox[1])/2.0, (bbox[2] + bbox[0])/2.0)
}
private func asArray() -> [CLLocationDegrees] {
return [self.lowLongitude, self.lowLatitude, self.highLongitude, self.highLatitude]
}
private func containsCoordinate(coordinate: CLLocationCoordinate2D ) -> Bool
{
var isWithinLongitudes: Bool = false
var isWithinLatitudes: Bool = false
//for Latitudes
if self.lowLatitude < coordinate.latitude && coordinate.latitude <= self.highLatitude {
isWithinLatitudes = true
}
//for Longitudes
if sgn(self.highLongitude) == sgn(self.lowLongitude) || self.lowLongitude < self.highLongitude {
if self.lowLongitude < coordinate.longitude && coordinate.longitude <= self.highLongitude {
isWithinLongitudes = true
}
} else {
if abs(self.highLongitude) <= abs(coordinate.longitude) && abs(coordinate.longitude) > abs(self.lowLongitude) {
isWithinLongitudes = true
}
}
return isWithinLatitudes && isWithinLongitudes
}
private func intersects(boundingBox bbox: QTRBBox) -> Bool
{
if !(sgn(self.highLongitude) == sgn(self.lowLongitude)) || !(self.lowLongitude < self.highLongitude) {
if !(sgn(bbox.highLongitude) == sgn(bbox.lowLongitude)) || !(bbox.lowLongitude < bbox.highLongitude) {
return false
}
}
if self.lowLatitude <= bbox.highLatitude && self.highLatitude > bbox.lowLatitude {
if self.lowLongitude <= bbox.highLongitude && self.highLongitude > bbox.lowLongitude {
return true
}
}
return false
}
}
class QTRNode {
var ne: QTRNode?
var se: QTRNode?
var sw: QTRNode?
var nw: QTRNode?
var bbox: QTRBBox
var bucketCapacity: Int
var points: Array<QTRNodePoint>
var size: Int {
return self.points.count
}
init (_ bbox: QTRBBox, _ bucketCapacity: Int) {
self.bbox = bbox
self.points = []
self.bucketCapacity = bucketCapacity
}
convenience init (_ points: [QTRNodePoint], _ bbox: QTRBBox, _ bucketCapacity: Int) {
self.init(bbox, bucketCapacity)
for p: QTRNodePoint in points {
self.insert(p)
}
}
private func split() {
let box = self.bbox
let c = box.center
let ne = QTRBBox(c.longitude, c.latitude, box.highLongitude, box.highLatitude)
self.ne = QTRNode(ne, self.bucketCapacity)
let se = QTRBBox(c.longitude, box.lowLatitude, box.highLongitude, c.latitude)
self.se = QTRNode(se, self.bucketCapacity)
let sw = QTRBBox(box.lowLongitude, box.lowLatitude, c.longitude, c.latitude)
self.sw = QTRNode(sw, self.bucketCapacity)
let nw = QTRBBox(box.lowLongitude, c.latitude, c.longitude, box.highLatitude)
self.nw = QTRNode(nw, self.bucketCapacity)
}
private func insert(point: QTRNodePoint) -> Bool {
if !self.bbox.containsCoordinate(point.coordinate2D) {
return false
}
if self.size < self.bucketCapacity {
self.points.append(point)
return true
}
if self.ne? == nil {
self.split()
}
if self.ne!.insert(point) { return true }
if self.se!.insert(point) { return true }
if self.sw!.insert(point) { return true }
if self.nw!.insert(point) { return true }
return false
}
func get(pointsIn range: QTRBBox, andApply map: (QTRNodePoint) -> ()) {
if !self.bbox.intersects(boundingBox: range) {
return
}
for p in self.points {
if range.containsCoordinate(p.coordinate2D) {
map(p)
}
}
if self.ne? == nil {
return
}
self.ne?.get(pointsIn: range, andApply: map)
self.se?.get(pointsIn: range, andApply: map)
self.sw?.get(pointsIn: range, andApply: map)
self.nw?.get(pointsIn: range, andApply: map)
}
func traverse(andApply map: (QTRNode) -> ()) {
map(self)
if self.ne? == nil {
return
}
self.ne?.traverse(andApply: map)
self.se?.traverse(andApply: map)
self.sw?.traverse(andApply: map)
self.nw?.traverse(andApply: map)
}
deinit {
print("DeInitializing")
self.ne = nil
self.se = nil
self.sw = nil
self.nw = nil
}
}
func generateRandomPoint(inRange bbox: QTRBBox) -> CLLocationCoordinate2D {
let p = Double(arc4random_uniform(250))*(bbox.highLatitude - bbox.lowLatitude)/100 + bbox.lowLatitude
let q = Double(arc4random_uniform(250))*(bbox.highLongitude - bbox.lowLongitude)/100 + bbox.lowLongitude
return CLLocationCoordinate2DMake(p, q)
}
func generateQTRPointArray(ofLength length: Int, inRange bbox: QTRBBox) -> [QTRNodePoint] {
var returnArray = [QTRNodePoint]()
for i in 0..<length {
returnArray.append(QTRNodePoint(generateRandomPoint(inRange: bbox), "RandomPoint_" + "\(i)"))
}
return returnArray
}