-
Notifications
You must be signed in to change notification settings - Fork 4
/
Nearby.js
249 lines (214 loc) · 5.78 KB
/
Nearby.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
var Nearby = function (width, height, depth, binSize){
this.limitBox = this.createBox(0, 0, 0, width, height, depth);
this.binSize = binSize;
this.bin = new Map();
this.reusableResultMap = new Map();
}
Nearby.prototype.createBox = function(x, y, z, width, height, depth){
var bb = {};
bb.containsBox = function(box){
return this.minX <= box.minX && box.maxX <= this.maxX &&
this.minY <= box.minY && box.maxY <= this.maxY &&
this.minZ <= box.minZ && box.maxZ <= this.maxZ;
};
bb.setFromCenterAndSize = function(x, y, z, width, height, depth){
var halfWidth = width / 2;
var halfHeight = height / 2;
var halfDepth = depth / 2;
this.minX = x - halfWidth;
this.maxX = x + halfWidth;
this.minY = y - halfHeight;
this.maxY = y + halfHeight;
this.minZ = z - halfDepth;
this.maxZ = z + halfDepth;
};
bb.setFromCenterAndSize(x, y, z, width, height, depth);
return bb;
}
Nearby.prototype.createObject = function(id, box){
var self = this;
var obj = {
id: id,
box: box,
binInfo: new Map(),
};
return obj;
}
Nearby.prototype.insert = function(obj){
if (!this.limitBox.containsBox(obj.box)){
return;
}
var BIN_SIZE = this.binSize;
var box = obj.box;
var minX = box.minX;
var minY = box.minY;
var minZ = box.minZ;
var maxX = box.maxX;
var maxY = box.maxY;
var maxZ = box.maxZ;
var round = Math.round(minX / BIN_SIZE) * BIN_SIZE;
var minXLower, minXUpper;
if (round <= minX){
minXLower = round;
minXUpper = minXLower + BIN_SIZE;
}else{
minXUpper = round;
minXLower = round - BIN_SIZE;
}
round = Math.round(maxX / BIN_SIZE) * BIN_SIZE;
var maxXLower, maxXUpper;
if (round < maxX){
maxXLower = round;
maxXUpper = maxXLower + BIN_SIZE;
}else{
maxXUpper = round;
maxXLower = round - BIN_SIZE;
}
if (minXLower > maxXLower){
maxXLower = minXLower;
}
round = Math.round(minY/BIN_SIZE) * BIN_SIZE;
var minYLower, minYUpper;
if (round <= minY){
minYLower = round;
minYUpper = minYLower + BIN_SIZE;
}else{
minYUpper = round;
minYLower = round - BIN_SIZE;
}
round = Math.round(maxY/BIN_SIZE) * BIN_SIZE;
var maxYLower, maxYUpper;
if (round < maxY){
maxYLower = round;
maxYUpper = maxYLower + BIN_SIZE;
}else{
maxYUpper = round;
maxYLower = round - BIN_SIZE;
}
if (minYLower > maxYLower){
maxYLower = minYLower;
}
round = Math.round(minZ/BIN_SIZE) * BIN_SIZE;
var minZLower, minZUpper;
if (round <= minZ){
minZLower = round;
minZUpper = minZLower + BIN_SIZE;
}else{
minZUpper = round;
minZLower = round - BIN_SIZE;
}
round = Math.round(maxZ/BIN_SIZE) * BIN_SIZE;
var maxZLower, maxZUpper;
if (round < maxZ){
maxZLower = round;
maxZUpper = maxZLower + BIN_SIZE;
}else{
maxZUpper = round;
maxZLower = round - BIN_SIZE;
}
if (minZLower > maxZLower){
maxZLower = minZLower;
}
for (var x = minXLower; x<= maxXLower; x+= BIN_SIZE){
if (!this.bin.has(x)){
this.bin.set(x, new Map());
}
if (!obj.binInfo.has(x)){
obj.binInfo.set(x, new Map());
}
for (var y = minYLower; y<= maxYLower; y+= BIN_SIZE){
if (!this.bin.get(x).has(y)){
this.bin.get(x).set(y, new Map());
}
if (!obj.binInfo.get(x).has(y)){
obj.binInfo.get(x).set(y, new Map());
}
for (var z = minZLower; z <= maxZLower; z+= BIN_SIZE){
if (!this.bin.get(x).get(y).has(z)){
this.bin.get(x).get(y).set(z, new Map());
}
this.bin.get(x).get(y).get(z).set(obj, true);
obj.binInfo.get(x).get(y).set(z, true);
}
}
}
}
Nearby.prototype.query = function(x, y, z){
var BIN_SIZE = this.binSize;
var rX = Math.round(x / BIN_SIZE) * BIN_SIZE;
var rY = Math.round(y / BIN_SIZE) * BIN_SIZE;
var rZ = Math.round(z / BIN_SIZE) * BIN_SIZE;
var minX, maxX;
if (rX <= x){
minX = rX;
maxX = rX + BIN_SIZE;
}else{
maxX = rX;
minX = rX - BIN_SIZE;
}
var minY, maxY;
if (rY <= y){
minY = rY;
maxY = rY + BIN_SIZE;
}else{
maxY = rY;
minY = rY - BIN_SIZE;
}
var minZ, maxZ;
if (rZ <= z){
minZ = rZ;
maxZ = rZ + BIN_SIZE;
}else{
maxZ = rZ;
minZ = rZ - BIN_SIZE;
}
var result = this.reusableResultMap;
result.clear();
for (var xDiff = -BIN_SIZE; xDiff <= BIN_SIZE; xDiff += BIN_SIZE){
var keyX = (minX + xDiff);
for (var yDiff = -BIN_SIZE; yDiff <= BIN_SIZE; yDiff += BIN_SIZE){
var keyY = (minY + yDiff);
for (var zDiff = -BIN_SIZE; zDiff <= BIN_SIZE; zDiff += BIN_SIZE){
var keyZ = (minZ + zDiff);
if (this.bin.has(keyX) && this.bin.get(keyX).has(keyY)){
var res = this.bin.get(keyX).get(keyY).get(keyZ);
if (!res) continue;
for (var obj of res.keys()){
result.set(obj, true);
}
}
}
}
}
return result;
}
Nearby.prototype.delete = function(obj){
var binInfo = obj.binInfo;
for (var x of binInfo.keys()){
for (var y of binInfo.get(x).keys()){
for (var z of binInfo.get(x).get(y).keys()){
if (this.bin.has(x) && this.bin.get(x).has(y) && this.bin.get(x).get(y).has(z)){
this.bin.get(x).get(y).get(z).delete(obj);
if (this.bin.get(x).get(y).get(z).size == 0){
this.bin.get(x).get(y).delete(z);
}
if (this.bin.get(x).get(y).size == 0){
this.bin.get(x).delete(y);
}
if (this.bin.get(x).size == 0){
this.bin.delete(x);
}
}
}
}
}
for (var x of binInfo.keys()){
binInfo.delete(x);
}
}
Nearby.prototype.update = function(obj, x, y, z, width, height, depth){
obj.box.setFromCenterAndSize(x, y, z, width, height, depth);
this.delete(obj);
this.insert(obj);
}
export default Nearby;