-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathocttree.ts
More file actions
366 lines (317 loc) · 9.71 KB
/
octtree.ts
File metadata and controls
366 lines (317 loc) · 9.71 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
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
export const CHUNK_DEPTH = 10;
export const CHUNK_SIZE = 1 << CHUNK_DEPTH;
const RIGHT = 1;
const TOP = 2;
const BACK = 4;
type DistArgs = [x: number, y: number, z: number] | [p: Point];
// a point in space
export class Point {
x: number;
y: number;
z: number;
constructor(x: number, y: number, z: number) {
this.x = x;
this.y = y;
this.z = z;
}
static rect(
x: number,
y: number,
z: number,
w: number,
h: number,
d: number
) {
return [
new Point(x - w / 2, y - h / 2, z - d / 2),
new Point(x + w / 2, y + h / 2, z + d / 2),
];
}
// get a chunk from a point
getChunk() {
return new Point(
Math.floor(this.x / CHUNK_SIZE),
Math.floor(this.y / CHUNK_SIZE),
Math.floor(this.z / CHUNK_SIZE)
);
}
// returns true if this point is between the other two
in(min: Point, max: Point) {
return (
min.x <= this.x &&
this.x <= max.x &&
min.y <= this.y &&
this.y <= max.y &&
min.z <= this.z &&
this.z <= max.z
);
}
// get the octant the point should be in for a node
getOctant(child: Point, depth: number) {
if (depth === 0) return 0;
return (
((child.x - this.x) >> (depth - 1) > 0 ? RIGHT : 0) |
((child.y - this.y) >> (depth - 1) > 0 ? TOP : 0) |
((child.z - this.z) >> (depth - 1) > 0 ? BACK : 0)
);
}
// get the middle of a chunk
getChunkMidpoint() {
return new Point(
this.x * CHUNK_SIZE,
this.y * CHUNK_SIZE,
this.z * CHUNK_SIZE
);
}
// compare points
eq(point: Point) {
return this.x == point.x && this.y == point.y && this.z == point.z;
}
// return a copy of this point shifted
shifted(x: number, y: number, z: number) {
return new Point(this.x + x, this.y + y, this.z + z);
}
dist(...args: DistArgs) {
const [x, y, z] = args.length === 3 ? args : args[0].arr();
return Math.hypot(this.x - x, this.y - y, this.z - z);
}
arr() {
return [this.x, this.y, this.z];
}
// stringified points are <x, y, z>
toString() {
return `<${this.x}, ${this.y}, ${this.z}>`;
}
}
type OctNodeValue<T> =
// | { unchanged: true } // unused, for diffing
{ value: T | null } | { nodes: OctNode<T>[] };
// a node in the tree
export class OctNode<T> {
pos: Point;
depth: number;
value: OctNodeValue<T>;
chunk?: Point;
constructor(pos: Point, depth: number, value: T | null) {
this.pos = pos;
this.depth = depth;
this.value = { value };
}
// true if this node is contained by the bounds
isInside(min: Point, max: Point) {
const size = 1 << this.depth;
// check if this bounds are entirely within
return (
this.pos.x >= min.x &&
this.pos.x + size <= max.x &&
this.pos.y >= min.y &&
this.pos.y + size <= max.y &&
this.pos.z >= min.z &&
this.pos.z + size <= max.z
);
}
isOutside(min: Point, max: Point) {
const size = 1 << this.depth;
return (
this.pos.x + size <= min.x ||
this.pos.x >= max.x ||
this.pos.y + size <= min.y ||
this.pos.y >= max.y ||
this.pos.z + size <= min.z ||
this.pos.z >= max.z
);
}
// if every child node has the same value, delete them
reduce() {
if (!('nodes' in this.value)) return;
// reduce all the nodes to values
// check if the other 7 nodes have the same value as the first one
for (let i = 0; i < 8; i++) {
// attempt to reduce this node
this.value.nodes[i].reduce();
if (
!('value' in this.value.nodes[i].value) ||
this.value.nodes[0].value['value'] !==
this.value.nodes[i].value['value']
) {
return;
}
}
// delete the old nodes
this.value = this.value.nodes[0].value;
}
// insert an area into the tree
insert(value: T, minBound: Point, maxBound: Point) {
if (this.isInside(minBound, maxBound)) {
this.value = { value };
return;
}
if (this.depth === 0) return;
// populate nodes if it's empty
if ('value' in this.value) {
// decrease depth
const depth = this.depth - 1;
// the shift is half of the child's size
const shift = 1 << depth;
// create new nodes in each 8 child octants of this node
const { value } = this.value;
this.value = {
nodes: [
new OctNode(this.pos.shifted(0, 0, 0), depth, value),
new OctNode(this.pos.shifted(shift, 0, 0), depth, value),
new OctNode(this.pos.shifted(0, shift, 0), depth, value),
new OctNode(this.pos.shifted(shift, shift, 0), depth, value),
new OctNode(this.pos.shifted(0, 0, shift), depth, value),
new OctNode(this.pos.shifted(shift, 0, shift), depth, value),
new OctNode(this.pos.shifted(0, shift, shift), depth, value),
new OctNode(this.pos.shifted(shift, shift, shift), depth, value),
],
};
}
if ('nodes' in this.value) {
for (const n of this.value.nodes) {
if (!n.isOutside(minBound, maxBound))
n.insert(value, minBound, maxBound);
}
}
}
// search an area
search(minBound: Point, maxBound: Point, set: Set<T>) {
set ??= new Set();
// if there's no nodes...
if ('value' in this.value) {
// add this value, this node would only have come up if it was within bounds
set.add(this.value.value);
return;
}
if ('nodes' in this.value) {
// search children
for (const n of this.value.nodes) {
// if the bounds are not outside of this node, search
if (!n.isOutside(minBound, maxBound)) {
n.search(minBound, maxBound, set);
}
}
}
}
// get the value at this point
get(point: Point): T | null {
if ('value' in this.value) return this.value.value;
if ('nodes' in this.value)
return this.value.nodes[this.pos.getOctant(point, this.depth)].get(point);
return null;
}
}
export default class ChunkTree<T> {
chunks: OctNode<T>[];
fill: T;
constructor(fill = null) {
this.chunks = [];
// empty nodes have this value
this.fill = fill;
}
// reduce all chunks
reduce() {
for (const chunk of this.chunks) {
chunk.reduce();
}
}
// iterate across chunks with bounds and run the fn with those bounds
iterChunksFromBounds(
minBound: Point,
maxBound: Point,
fn: (min: Point, max: Point) => void
) {
// if the boundaries are in different chunks, split the area up by chunk
const minChunk = minBound.getChunk();
const maxChunk = maxBound.shifted(-1, -1, -1).getChunk();
if (
minChunk.x > maxChunk.x ||
minChunk.y > maxChunk.y ||
minChunk.z > maxChunk.z
)
throw 'max chunk too small';
if (!minChunk.eq(maxChunk)) {
/*// insert in all chunks that overlap
for (let x = minChunk.x; x <= maxChunk.x; ++x) {
for (let y = minChunk.y; y <= maxChunk.y; ++y) {
for (let z = minChunk.z; z <= maxChunk.z; ++z) {
fn(minBound, maxBound);
}
}
}*/
for (let x = minChunk.x; x <= maxChunk.x; ++x) {
// determine the min and max bounds for this chunk
// these should always be within the same chunk
// and should cap out at the max bound's position
const minX = x === minChunk.x ? minBound.x : x * CHUNK_SIZE;
const maxX = Math.min(
Math.floor(minX / CHUNK_SIZE + 1) * CHUNK_SIZE,
maxBound.x
);
for (let y = minChunk.y; y <= maxChunk.y; ++y) {
// same thing as above but for y
const minY = y === minChunk.y ? minBound.y : y * CHUNK_SIZE;
const maxY = Math.min(
Math.floor(minY / CHUNK_SIZE + 1) * CHUNK_SIZE,
maxBound.y
);
for (let z = minChunk.z; z <= maxChunk.z; ++z) {
// same thing as above but for z
const minZ = z === minChunk.z ? minBound.z : z * CHUNK_SIZE;
const maxZ = Math.min(
Math.floor(minZ / CHUNK_SIZE + 1) * CHUNK_SIZE,
maxBound.z
);
// run the fn on the new single-chunk bounds
fn(new Point(minX, minY, minZ), new Point(maxX, maxY, maxZ));
}
}
}
return;
} // otherwise the boundaries are in the same chunk so it's okay to run the function
fn(minBound, maxBound);
}
// get a chunk at a point
getChunkAt(point: Point, create = false) {
const chunkPos = point.getChunk();
// find the the corresponding chunk octtree
let chunk = this.chunks.find(c => c.chunk.eq(chunkPos));
if (!chunk && create) {
// create a new chunk because one does not exist
chunk = new OctNode(chunkPos.getChunkMidpoint(), CHUNK_DEPTH, this.fill);
chunk.chunk = chunkPos;
this.chunks.push(chunk);
}
return chunk;
}
// search all values in an area
search(minBound: Point, maxBound: Point) {
// the set of all result values
const results = new Set<T>();
// run the following code in the chunks covered by these boundaries:
this.iterChunksFromBounds(minBound, maxBound, (min, max) => {
const chunk = this.getChunkAt(min);
if (chunk) {
// search the chunk if it exists
chunk.search(min, max, results);
}
});
// remove the empty item1
results.delete(this.fill);
return results;
}
// get the value at a point
get(point: Point) {
const chunk = this.getChunkAt(point);
return chunk ? chunk.get(point) : this.fill;
}
// insert an area into chunks
insert(value: T, minBound: Point, maxBound: Point) {
// run the following code in the chunks covered by these boundaries:
this.iterChunksFromBounds(minBound, maxBound, (min, max) => {
// insert the value into the oct tree
this.getChunkAt(min, true).insert(value, min, max);
});
}
}