-
Notifications
You must be signed in to change notification settings - Fork 0
/
outerQuadTree.js
431 lines (346 loc) · 8.73 KB
/
outerQuadTree.js
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
The MIT License
Copyright (c) 2011 Mike Chambers
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
* A QuadTree implementation in JavaScript, a 2d spatial subdivision algorithm.
* @module QuadTree
**/
(function(window) {
/****************** QuadTree ****************/
/**
* QuadTree data structure.
* @class QuadTree
* @constructor
* @param {Object} An object representing the bounds of the top level of the QuadTree. The object
* should contain the following properties : x, y, width, height
* @param {Boolean} pointQuad Whether the QuadTree will contain points (true), or items with bounds
* (width / height)(false). Default value is false.
* @param {Number} maxDepth The maximum number of levels that the quadtree will create. Default is 4.
* @param {Number} maxChildren The maximum number of children that a node can contain before it is split into sub-nodes.
**/
function QuadTree(bounds, pointQuad, maxDepth, maxChildren)
{
var node;
if(pointQuad)
{
node = new Node(bounds, 0, maxDepth, maxChildren);
}
else
{
node = new BoundsNode(bounds, 0, maxDepth, maxChildren);
}
this.root = node;
}
/**
* The root node of the QuadTree which covers the entire area being segmented.
* @property root
* @type Node
**/
QuadTree.prototype.root = null;
/**
* Inserts an item into the QuadTree.
* @method insert
* @param {Object|Array} item The item or Array of items to be inserted into the QuadTree. The item should expose x, y
* properties that represents its position in 2D space.
**/
QuadTree.prototype.insert = function(item)
{
if(item instanceof Array)
{
var len = item.length;
for(var i = 0; i < len; i++)
{
this.root.insert(item[i]);
}
}
else
{
this.root.insert(item);
}
}
/**
* Clears all nodes and children from the QuadTree
* @method clear
**/
QuadTree.prototype.clear = function()
{
this.root.clear();
}
/**
* Retrieves all items / points in the same node as the specified item / point. If the specified item
* overlaps the bounds of a node, then all children in both nodes will be returned.
* @method retrieve
* @param {Object} item An object representing a 2D coordinate point (with x, y properties), or a shape
* with dimensions (x, y, width, height) properties.
**/
QuadTree.prototype.retrieve = function(item)
{
//get a copy of the array of items
var out = this.root.retrieve(item).slice(0);
return out;
}
/************** Node ********************/
function Node(bounds, depth, maxDepth, maxChildren)
{
this._bounds = bounds;
this.children = [];
this.nodes = [];
if(maxChildren)
{
this._maxChildren = maxChildren;
}
if(maxDepth)
{
this._maxDepth = maxDepth;
}
if(depth)
{
this._depth = depth;
}
}
//subnodes
Node.prototype.nodes = null;
Node.prototype._classConstructor = Node;
//children contained directly in the node
Node.prototype.children = null;
Node.prototype._bounds = null;
//read only
Node.prototype._depth = 0;
Node.prototype._maxChildren = 4;
Node.prototype._maxDepth = 4;
Node.TOP_LEFT = 0;
Node.TOP_RIGHT = 1;
Node.BOTTOM_LEFT = 2;
Node.BOTTOM_RIGHT = 3;
Node.prototype.insert = function(item)
{
if(this.nodes.length)
{
var index = this._findIndex(item);
this.nodes[index].insert(item);
return;
}
this.children.push(item);
var len = this.children.length;
if(!(this._depth >= this._maxDepth) &&
len > this._maxChildren)
{
this.subdivide();
for(var i = 0; i < len; i++)
{
this.insert(this.children[i]);
}
this.children.length = 0;
}
}
Node.prototype.retrieve = function(item)
{
if(this.nodes.length)
{
var index = this._findIndex(item);
return this.nodes[index].retrieve(item);
}
return this.children;
}
Node.prototype._findIndex = function(item)
{
var b = this._bounds;
var left = (item.x > b.x + b.width / 2)? false : true;
var top = (item.y > b.y + b.height / 2)? false : true;
//top left
var index = Node.TOP_LEFT;
if(left)
{
//left side
if(!top)
{
//bottom left
index = Node.BOTTOM_LEFT;
}
}
else
{
//right side
if(top)
{
//top right
index = Node.TOP_RIGHT;
}
else
{
//bottom right
index = Node.BOTTOM_RIGHT;
}
}
return index;
}
Node.prototype.subdivide = function()
{
var depth = this._depth + 1;
var bx = this._bounds.x;
var by = this._bounds.y;
//floor the values
var b_w_h = (this._bounds.width / 2)|0;
var b_h_h = (this._bounds.height / 2)|0;
var bx_b_w_h = bx + b_w_h;
var by_b_h_h = by + b_h_h;
//top left
this.nodes[Node.TOP_LEFT] = new this._classConstructor({
x:bx,
y:by,
width:b_w_h,
height:b_h_h
},
depth);
//top right
this.nodes[Node.TOP_RIGHT] = new this._classConstructor({
x:bx_b_w_h,
y:by,
width:b_w_h,
height:b_h_h
},
depth);
//bottom left
this.nodes[Node.BOTTOM_LEFT] = new this._classConstructor({
x:bx,
y:by_b_h_h,
width:b_w_h,
height:b_h_h
},
depth);
//bottom right
this.nodes[Node.BOTTOM_RIGHT] = new this._classConstructor({
x:bx_b_w_h,
y:by_b_h_h,
width:b_w_h,
height:b_h_h
},
depth);
}
Node.prototype.clear = function()
{
this.children.length = 0;
var len = this.nodes.length;
for(var i = 0; i < len; i++)
{
this.nodes[i].clear();
}
this.nodes.length = 0;
}
/******************** BoundsQuadTree ****************/
function BoundsNode(bounds, depth, maxChildren, maxDepth)
{
Node.call(this, bounds, depth, maxChildren, maxDepth);
this._stuckChildren = [];
}
BoundsNode.prototype = new Node();
BoundsNode.prototype._classConstructor = BoundsNode;
BoundsNode.prototype._stuckChildren = null;
//we use this to collect and conctenate items being retrieved. This way
//we dont have to continuously create new Array instances.
//Note, when returned from QuadTree.retrieve, we then copy the array
BoundsNode.prototype._out = [];
BoundsNode.prototype.insert = function(item)
{
if(this.nodes.length)
{
var index = this._findIndex(item);
var node = this.nodes[index];
//todo: make _bounds bounds
if(item.x >= node._bounds.x &&
item.x + item.width <= node._bounds.x + node._bounds.width &&
item.y >= node._bounds.y &&
item.y + item.height <= node._bounds.y + node._bounds.height)
{
this.nodes[index].insert(item);
}
else
{
this._stuckChildren.push(item);
}
return;
}
this.children.push(item);
var len = this.children.length;
if(!(this._depth >= this._maxDepth) &&
len > this._maxChildren)
{
this.subdivide();
for(var i = 0; i < len; i++)
{
this.insert(this.children[i]);
}
this.children.length = 0;
}
}
BoundsNode.prototype.getChildren = function()
{
return this.children.concat(this._stuckChildren);
}
BoundsNode.prototype.retrieve = function(item)
{
var out = this._out;
out.length = 0;
if(this.nodes.length)
{
var index = this._findIndex(item);
out.push.apply(out, this.nodes[index].retrieve(item));
}
out.push.apply(out, this._stuckChildren);
out.push.apply(out, this.children);
return out;
}
BoundsNode.prototype.clear = function()
{
this._stuckChildren.length = 0;
//array
this.children.length = 0;
var len = this.nodes.length;
if(!len)
{
return;
}
for(var i = 0; i < len; i++)
{
this.nodes[i].clear();
}
//array
this.nodes.length = 0;
//we could call the super clear function but for now, im just going to inline it
//call the hidden super.clear, and make sure its called with this = this instance
//Object.getPrototypeOf(BoundsNode.prototype).clear.call(this);
}
BoundsNode.prototype.getChildCount
window.QuadTree = QuadTree;
/*
//http://ejohn.org/blog/objectgetprototypeof/
if ( typeof Object.getPrototypeOf !== "function" ) {
if ( typeof "test".__proto__ === "object" ) {
Object.getPrototypeOf = function(object){
return object.__proto__;
};
} else {
Object.getPrototypeOf = function(object){
// May break if the constructor has been tampered with
return object.constructor.prototype;
};
}
}
*/
}(window));