-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuadTree.m
More file actions
160 lines (128 loc) · 4.55 KB
/
QuadTree.m
File metadata and controls
160 lines (128 loc) · 4.55 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
//
// QuadTree.m
// QuadTree
//
// Created by Aaron Dean Bikis on 3/1/17.
// Copyright © 2017 7apps. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "QuadTree.h"
#pragma mark - Constructors
QuadTreeNodeData QuadTreeNodeDataMake(double x, double y, void* data)
{
QuadTreeNodeData d; d.x = x; d.y = y; d.data = data;
return d;
}
BoundingBox BoundingBoxMake(double x0, double y0, double xf, double yf)
{
BoundingBox bb; bb.x0 = x0; bb.y0 = y0; bb.xf = xf; bb.yf = yf;
return bb;
}
QuadTreeNode* QuadTreeNodeMake(BoundingBox boundary, int bucketCapacity)
{
QuadTreeNode* node = malloc(sizeof(QuadTreeNode));
node->northWest = NULL;
node->northEast = NULL;
node->southWest = NULL;
node->southEast = NULL;
node->boundary = boundary;
node->bucketCapacity = bucketCapacity;
node->count = 0;
node->points = malloc(sizeof(QuadTreeNodeData) * bucketCapacity);
return node;
}
#pragma mark - Bounding Box Functions
bool BoundingBoxContainsData(BoundingBox box, QuadTreeNodeData data)
{
bool containsX = box.x0 <= data.x && data.x <= box.xf;
bool containsY = box.y0 <= data.y && data.y <= box.yf;
return containsX && containsY;
}
bool BoundingBoxIntersectsBoundingBox(BoundingBox b1, BoundingBox b2)
{
return (b1.x0 <= b2.xf && b1.xf >= b2.x0 && b1.y0 <= b2.yf && b1.yf >= b2.y0);
}
#pragma mark - Quad Tree Functions
void QuadTreeNodeSubdivide(QuadTreeNode* node)
{
BoundingBox box = node->boundary;
double xMid = (box.xf + box.x0) / 2.0;
double yMid = (box.yf + box.y0) / 2.0;
BoundingBox northWest = BoundingBoxMake(box.x0, box.y0, xMid, yMid);
node->northWest = QuadTreeNodeMake(northWest, node->bucketCapacity);
BoundingBox northEast = BoundingBoxMake(xMid, box.y0, box.xf, yMid);
node->northEast = QuadTreeNodeMake(northEast, node->bucketCapacity);
BoundingBox southWest = BoundingBoxMake(box.x0, yMid, xMid, box.yf);
node->southWest = QuadTreeNodeMake(southWest, node->bucketCapacity);
BoundingBox southEast = BoundingBoxMake(xMid, yMid, box.xf, box.yf);
node->southEast = QuadTreeNodeMake(southEast, node->bucketCapacity);
}
bool QuadTreeNodeInsertData(QuadTreeNode* node, QuadTreeNodeData data)
{
if (!BoundingBoxContainsData(node->boundary, data)) {
return false;
}
if (node->count < node->bucketCapacity) {
node->points[node->count] = data;
node->count++;
return true;
}
if (node->northWest == NULL) {
QuadTreeNodeSubdivide(node);
}
if (QuadTreeNodeInsertData(node->northWest, data)) return true;
if (QuadTreeNodeInsertData(node->northEast, data)) return true;
if (QuadTreeNodeInsertData(node->southWest, data)) return true;
if (QuadTreeNodeInsertData(node->southEast, data)) return true;
return false;
}
void QuadTreeGatherDataInRange(QuadTreeNode* node, BoundingBox range, DataReturnBlock block)
{
if (!BoundingBoxIntersectsBoundingBox(node->boundary, range)) {
return;
}
for (int i = 0; i < node->count; i++) {
if (BoundingBoxContainsData(range, node->points[i])) {
block(node->points[i]);
}
}
if (node->northWest == NULL) {
return;
}
QuadTreeGatherDataInRange(node->northWest, range, block);
QuadTreeGatherDataInRange(node->northEast, range, block);
QuadTreeGatherDataInRange(node->southWest, range, block);
QuadTreeGatherDataInRange(node->southEast, range, block);
}
void QuadTreeTraverse(QuadTreeNode* node, QuadTreeTraverseBlock block)
{
block(node);
if (node->northWest == NULL) {
return;
}
QuadTreeTraverse(node->northWest, block);
QuadTreeTraverse(node->northEast, block);
QuadTreeTraverse(node->southWest, block);
QuadTreeTraverse(node->southEast, block);
}
//UNUSED
QuadTreeNode* QuadTreeBuildWithData(QuadTreeNodeData *data, int count, BoundingBox boundingBox, int capacity)
{
QuadTreeNode* root = QuadTreeNodeMake(boundingBox, capacity);
for (int i = 0; i < count; i++) {
QuadTreeNodeInsertData(root, data[i]);
}
return root;
}
void FreeQuadTreeNode(QuadTreeNode* node)
{
if (node->northWest != NULL) FreeQuadTreeNode(node->northWest);
if (node->northEast != NULL) FreeQuadTreeNode(node->northEast);
if (node->southWest != NULL) FreeQuadTreeNode(node->southWest);
if (node->southEast != NULL) FreeQuadTreeNode(node->southEast);
for (int i=0; i < node->count; i++) {
free(node->points[i].data);
}
free(node->points);
free(node);
}