-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathQuadTree.js
More file actions
256 lines (238 loc) · 7.84 KB
/
QuadTree.js
File metadata and controls
256 lines (238 loc) · 7.84 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
import Rect from '../graphics/structs/Rect';
/**
* Creates Quad Tree data structure. It distributes points into equal quadrants.
* So it is equivalent to 2 dimensional binary search tree.
* @class QuadTree
*
* @param {number} minimalSize Defines minimal size of the quadrant. This protects structure against unnecessary depth.
* @returns {QuadTree} Returns Quad Tree data structure.
*/
export default function QuadTree(minimalSize) {
var _minimalScale = Math.max(1, scale(minimalSize)),
_rootScale = 8,
_rootSize = 256,
_rootCell = null;
// Create root cell
_rootCell = new Cell(0, 0, _rootScale, _rootSize);
function Cell(x, y, scale, size) {
this.x = x;
this.y = y;
this.scale = scale;
this.size = size;
this.quadrants = [];
this.points = [];
}
Cell.prototype.notEnclosed = function (rect) {
if (this.x < rect.x || this.x + this.size > rect.x + rect.width || this.y < rect.y || this.y + this.size > rect.y + rect.height) {
return true;
}
return false;
};
Cell.prototype.overlaps = function (rect) {
if (this.x + this.size < rect.x || rect.x + rect.width < this.x || this.y + this.size < rect.y || rect.y + rect.height < this.y) {
return false;
}
return true;
};
Cell.prototype.getQuadrantIndex = function (x, y) {
var shift = this.scale - 1;
return ((x >> shift) & 1) | (((y >> shift) & 1) << 1);
};
function scale(value) {
return Math.floor(Math.log(value) / Math.log(2));
}
/**
* Adds point
*
* @param {Point} point Point
*/
function addPoint(point) {
var x = Math.floor(point.x),
y = Math.floor(point.y),
size = Math.max(x, y);
while (_rootSize <= size) {
_rootScale += 1;
_rootSize *= 2;
var parent = new Cell(0, 0, _rootScale, _rootSize);
_splitCell(parent);
parent.quadrants[0] = _rootCell;
_rootCell = parent;
}
_addPoint(point);
}
function _addPoint(point) {
var x = Math.floor(point.x),
y = Math.floor(point.y),
cell = _rootCell;
if (x < 0 || y < 0) {
throw "Negative values are not supported in the quad tree.";
}
while (cell.points == null || cell.points.length > 0) {
if (cell.scale == _minimalScale && cell.points != null) {
break;
}
if (cell.points != null && cell.points.length > 0) {
_splitCell(cell);
}
cell = cell.quadrants[cell.getQuadrantIndex(x, y)];
}
cell.points.push(point);
}
function _splitCell(parent) {
var size = parent.size / 2;
parent.quadrants = [
new Cell(parent.x, parent.y, parent.scale - 1, size),
new Cell(parent.x + size, parent.y, parent.scale - 1, size),
new Cell(parent.x, parent.y + size, parent.scale - 1, size),
new Cell(parent.x + size, parent.y + size, parent.scale - 1, size)
];
for (var index = 0, len = parent.points.length; index < len; index += 1) {
var point = parent.points[index],
x = Math.floor(point.x),
y = Math.floor(point.y);
parent.quadrants[parent.getQuadrantIndex(x, y)].points.push(point);
}
// indicates that cell has quadrants
parent.points = null;
}
/**
* Callback function for iteration of points
*
* @callback onQuadTreePointCallback
* @param {Point} point Rectangle
* @returns {boolean} Returns true to break iteration process.
*/
/**
* Loops rectangular area of quad tree structure
*
* @param {object} thisArg The callback function invocation context
* @param {Rect} rect Rectangular search area
* @param {onQuadTreePointCallback} onItem Callback function to call for every point within the search area
*/
function loopArea(thisArg, rect, onItem) {
var cell,
index, len;
if (onItem != null) {
var check = [_rootCell],
nocheck = [];
while (check.length > 0 || nocheck.length > 0) {
var newCheck = [],
newNocheck = [];
for (index = 0, len = check.length; index < len; index += 1) {
cell = check[index];
if (cell.overlaps(rect)) {
if (cell.notEnclosed(rect)) {
if (cell.points == null) {
for (var quadrantIndex = 0; quadrantIndex < 4; quadrantIndex += 1) {
newCheck.push(cell.quadrants[quadrantIndex]);
}
} else {
for (var pointIndex = 0, pointsLen = cell.points.length; pointIndex < pointsLen; pointIndex += 1) {
var point = cell.points[pointIndex];
if (rect.contains(point)) {
if (onItem.call(thisArg, point)) {
return;
}
}
}
}
} else {
nocheck.push(cell);
}
}
}
for (index = 0, len = nocheck.length; index < len; index += 1) {
cell = nocheck[index];
if (cell.points == null) {
for (quadrantIndex = 0; quadrantIndex < 4; quadrantIndex += 1) {
newNocheck.push(cell.quadrants[quadrantIndex]);
}
} else {
for (pointIndex = 0, pointsLen = cell.points.length; pointIndex < pointsLen; pointIndex += 1) {
if (onItem.call(thisArg, cell.points[pointIndex])) {
return;
}
}
}
}
check = newCheck;
nocheck = newNocheck;
}
}
}
/**
* Validates internal data consistency of quad tree data structure
*
* @returns {boolean} Returns true if structure pass validation
*/
function validate() {
var level = [_rootCell];
while (level.length > 0) {
var newLevel = [];
for (var index = 0, len = level.length; index < len; index += 1) {
var cell = level[index];
var rect = new Rect(cell.x, cell.y, cell.size, cell.size);
if (cell.points != null && cell.quadrants.length > 0) {
return false;
}
if (cell.points == null) {
for (var quadrantIndex = 0; quadrantIndex < 4; quadrantIndex += 1) {
newLevel.push(cell.quadrants[quadrantIndex]);
}
} else {
for (var pointIndex = 0, pointsLen = cell.points.length; pointIndex < pointsLen; pointIndex += 1) {
var point = cell.points[pointIndex];
if (!rect.contains(point)) {
return false;
}
}
}
}
level = newLevel;
}
return true;
}
/**
* Returns collection of quadrants created in the data structure
* Quadrants exists only when elements exists in them.
* This method is used for visual debugging of the structure.
*
* @param {React} selection Rectangular test area to highlight quadrants
* @returns {Rect[]} Returns collection of available quadrants.
* Quadrants containing points within selection area have context.highlight property set to true.
*/
function getPositions(selection) {
var result = [];
var count = 0;
var level = [_rootCell];
while (level.length > 0) {
var newLevel = [];
for (var index = 0, len = level.length; index < len; index += 1) {
var cell = level[index];
var rect = new Rect(cell.x, cell.y, cell.size, cell.size);
rect.context = {
isHighlighted: false
};
count += 1;
if (selection != null && selection.overlaps(rect) && cell.points != null && cell.points.length > 0) {
rect.context.isHighlighted = true;
}
result.push(rect);
for (var quadrantIndex = 0; quadrantIndex < 4; quadrantIndex += 1) {
var quadrant = cell.quadrants[quadrantIndex];
if (quadrant != null) {
newLevel.push(quadrant);
}
}
}
level = newLevel;
}
return result;
}
return {
addPoint: addPoint,
loopArea: loopArea,
validate: validate,
getPositions: getPositions
};
};