-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTreeBucket.js
232 lines (203 loc) · 6.08 KB
/
TreeBucket.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
'use strict'
/*
MIT License
Copyright (c) 2021 Andrew S ([email protected])
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.
*/
/**
* TreeBucket.
*
* The class that actually contains the data
*/
export default class TreeBucket {
/**
* Constructor for TreeBucket
* @param {number} bucketX
* @param {number} bucketY
* @param {number} bucketSize
*/
constructor (bucketX, bucketY, bucketSize) {
this.PARENT = undefined
this.CHILDREN = [undefined, undefined,
undefined, undefined]
this.BUCKET_SIZE = bucketSize
this.COUNTER = 0
this.BUCKET_X = bucketX
this.BUCKET_Y = bucketY
this.X = bucketX * bucketSize
this.Y = bucketY * bucketSize
this.MAX_X = this.X + bucketSize
this.MAX_Y = this.Y + bucketSize
this.HALFX = this.X + bucketSize / 2
this.HALFY = this.Y + bucketSize / 2
this.QUAD_CACHE = [[], [], [], [], [], [], [], [], []]
this.ITEM_LIST = []
}
/**
* Update QuadCache with appropriate child buckets.
*/
updateQuadCache () {
TreeBucket.QUADS.forEach((quad, i) => {
const arr = []
this.QUAD_CACHE[i] = arr
quad.forEach((index) => {
const child = this.CHILDREN[index]
if (child !== undefined) {
arr.push(child)
}
})
})
}
/**
* Increments a counter and propagates it upwards.
*/
add () {
if (this.COUNTER === 0 && this.PARENT !== undefined) this.PARENT.add()
++this.COUNTER
}
/**
* Decrements a counter and propagates it upwards.
*/
subtract () {
--this.COUNTER
if (this.COUNTER === 0 && this.PARENT !== undefined) this.PARENT.subtract()
}
/**
* Returns the quads that collide with the bounding box. Returns -1 if bounds is completely enclosing bucket.
* @param {Bounds} bounds
* @returns {number}
*/
getQuad (bounds) {
if (this.QUAD_CACHE[0].length <= 1) {
return 0
}
const minX = bounds.minX
const minY = bounds.minY
const maxX = bounds.maxX
const maxY = bounds.maxY
const minX2 = this.X
const minY2 = this.Y
const maxX2 = this.MAX_X
const maxY2 = this.MAX_Y
const halfX = this.HALFX
const halfY = this.HALFY
if (maxY <= halfY) {
if (maxX <= halfX) return 1
else if (minX >= halfX) return 2
return 5
} else if (minY >= halfY) {
if (maxX <= halfX) return 3
else if (minX >= halfX) return 4
return 6
}
if (maxX <= halfX) {
return 7
} else if (minX >= halfX) {
return 8
}
if (bounds.width < this.BUCKET_SIZE || bounds.height < this.BUCKET_SIZE || minX > minX2 || maxX < maxX2 || minY > minY2 || maxY < maxY2) {
return 0
}
return -1 // too big
}
/**
* Internal method that iterates through the items contained in the bucket while filtering non-unique entries.
*
* Similar to Array.every();
* @param {EveryCallback} call
* @param {number} QID
* @returns {boolean}
*/
_every (call, QID) {
return this.ITEM_LIST.every((item) => {
if (item._HashBoundsTempCheck !== QID) {
item._HashBoundsTempCheck = QID
return call(item)
}
return true
})
}
/**
* Recursive method that iterates through entries that may collide with the specified bounds.
* @param {Bounds} bounds
* @param {EveryCallback} call
* @param {number} QID
* @returns {boolean}
*/
every (bounds, call, QID) {
if (this.COUNTER === 0) return true
const quads = this.getQuad(bounds)
if (quads === -1) return this.everyAll(call, QID)
if (!this._every(call, QID)) return false
return this.QUAD_CACHE[quads].every((child) => {
return child.every(bounds, call, QID)
})
}
/**
* Recursive method that iterates through all entries contained in this bucket and its children.
* @param {EveryCallback} call
* @param {number} QID
* @returns {boolean}
*/
everyAll (call, QID) {
if (this.COUNTER === 0) return true
if (!this._every(call, QID)) return false
return this.QUAD_CACHE[0].every((child) => {
return child.everyAll(call, QID)
})
}
/**
* Removes a entry
* @param {EntryCache} entryCache
* @param {number} indexKey
*/
remove (entryCache, indexKey) {
const index = entryCache.indexes[indexKey]
const len1 = this.ITEM_LIST.length - 1
if (index !== len1) {
const swap = this.ITEM_LIST[index] = this.ITEM_LIST[len1]
const cache2 = swap._HashBounds[entryCache.hashID]
const swapKey = (this.BUCKET_X - cache2.k1x) * (cache2.k2y - cache2.k1y + 1) + this.BUCKET_Y - cache2.k1y
cache2.indexes[swapKey] = index
}
this.ITEM_LIST.pop()
if (len1 === 0) this.subtract()
}
/**
* Sets a entry
* @param {Entry} entry
* @param {EntryCache} entryCache
* @param {number} indexKey
*/
set (entry, entryCache, indexKey) {
const len = this.ITEM_LIST.length
entryCache.indexes[indexKey] = len
this.ITEM_LIST.push(entry)
if (len === 0) this.add()
}
}
TreeBucket.QUADS = [
[0, 1, 2, 3],
[0],
[1],
[2],
[3],
[0, 1],
[2, 3],
[0, 2],
[1, 3]
]