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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/** @import { Color } from 'three' */
/** @import { GeometryBVH } from '../core/GeometryBVH.js' */
import { LineBasicMaterial, BufferAttribute, Box3, Group, MeshBasicMaterial, Object3D, BufferGeometry, Mesh, Matrix4, Vector3 } from 'three';
import { arrayToBox } from '../utils/ArrayBoxUtilities.js';
import { MeshBVH } from '../core/MeshBVH.js';

const boundingBox = /* @__PURE__ */ new Box3();
const matrix = /* @__PURE__ */ new Matrix4();
const vec = /* @__PURE__ */ new Vector3();

class BVHRootHelper extends Object3D {

	get isMesh() {

		return ! this.displayEdges;

	}

	get isLineSegments() {

		return this.displayEdges;

	}

	get isLine() {

		return this.displayEdges;

	}

	getVertexPosition( ...args ) {

		// implement this function so it works with Box3.setFromObject
		return Mesh.prototype.getVertexPosition.call( this, ...args );

	}

	constructor( bvh, material, depth = 10, group = 0 ) {

		super();

		this.material = material;
		this.geometry = new BufferGeometry();
		this.name = 'BVHRootHelper';
		this.depth = depth;
		this.displayParents = false;
		this.bvh = bvh;
		this.displayEdges = true;
		this._group = group;

	}

	raycast() {}

	update() {

		const boundsTree = this.bvh;
		this.geometry.dispose();
		this.visible = false;
		if ( boundsTree ) {

			this.geometry = this.getGeometry( boundsTree );
			this.visible = true;

		}

	}

	getGeometry( boundsTree ) {

		const group = this._group;

		// fill in the position buffer with the bounds corners
		let positionArray = null;
		if ( group !== - 1 ) {

			positionArray = this.getBVHBoundPositions( boundsTree, group );

		} else {

			const positionArrays = boundsTree._roots.map( ( r, i ) => this.getBVHBoundPositions( boundsTree, i ) );
			const total = positionArrays.reduce( ( v, arr ) => v + arr.length, 0 );
			positionArray = new Float32Array( total );

			let offset = 0;
			positionArrays.forEach( arr => {

				positionArray.set( arr, offset );
				offset += arr.length;

			} );

		}

		const indexArray = this.getBVHBoundIndices( positionArray );

		// update the geometry
		const geometry = new BufferGeometry();
		geometry.setIndex( new BufferAttribute( indexArray, 1, false ) );
		geometry.setAttribute( 'position', new BufferAttribute( positionArray, 3, false ) );
		return geometry;

	}

	getBVHBoundIndices( positionArray ) {

		const boundsCount = positionArray.length / ( 8 * 3 );
		let indexArray;
		let indices;
		if ( this.displayEdges ) {

			// fill in the index buffer to point to the corner points
			indices = new Uint8Array( [
				// x axis
				0, 4,
				1, 5,
				2, 6,
				3, 7,

				// y axis
				0, 2,
				1, 3,
				4, 6,
				5, 7,

				// z axis
				0, 1,
				2, 3,
				4, 5,
				6, 7,
			] );

		} else {

			indices = new Uint8Array( [

				// X-, X+
				0, 1, 2,
				2, 1, 3,

				4, 6, 5,
				6, 7, 5,

				// Y-, Y+
				1, 4, 5,
				0, 4, 1,

				2, 3, 6,
				3, 7, 6,

				// Z-, Z+
				0, 2, 4,
				2, 6, 4,

				1, 5, 3,
				3, 5, 7,

			] );

		}

		if ( positionArray.length > 65535 ) {

			indexArray = new Uint32Array( indices.length * boundsCount );

		} else {

			indexArray = new Uint16Array( indices.length * boundsCount );

		}

		const indexLength = indices.length;
		for ( let i = 0; i < boundsCount; i ++ ) {

			const posOffset = i * 8;
			const indexOffset = i * indexLength;
			for ( let j = 0; j < indexLength; j ++ ) {

				indexArray[ indexOffset + j ] = posOffset + indices[ j ];

			}

		}

		return indexArray;

	}

	getBVHBoundPositions( bvh, group = 0, matrix = null ) {

		// count the number of bounds required
		const targetDepth = this.depth - 1;
		const displayParents = this.displayParents;
		let boundsCount = 0;
		bvh.traverse( ( depth, isLeaf ) => {

			if ( depth >= targetDepth || isLeaf ) {

				boundsCount ++;
				return true;

			} else if ( displayParents ) {

				boundsCount ++;

			}

		}, group );

		// fill in the position buffer with the bounds corners
		let posIndex = 0;
		const positionArray = new Float32Array( 8 * 3 * boundsCount );
		bvh.traverse( ( depth, isLeaf, boundingData ) => {

			const terminate = depth >= targetDepth || isLeaf;
			if ( terminate || displayParents ) {

				arrayToBox( 0, boundingData, boundingBox );

				const { min, max } = boundingBox;
				for ( let x = - 1; x <= 1; x += 2 ) {

					const xVal = x < 0 ? min.x : max.x;
					for ( let y = - 1; y <= 1; y += 2 ) {

						const yVal = y < 0 ? min.y : max.y;
						for ( let z = - 1; z <= 1; z += 2 ) {

							const zVal = z < 0 ? min.z : max.z;
							vec.set( xVal, yVal, zVal );
							if ( matrix ) {

								vec.applyMatrix4( matrix );

							}

							vec.toArray( positionArray, posIndex );
							posIndex += 3;

						}

					}

				}

				return terminate;

			}

		}, group );

		return positionArray;

	}

}

/**
 * A `THREE.Group` that visualizes a BVH as wireframe bounding boxes or solid
 * face overlays. Attach it as a sibling of the mesh in the scene graph and
 * call `update()` whenever the mesh's BVH or world transform changes.
 *
 * @param {Object3D | GeometryBVH | null} [mesh=null] - The mesh whose `geometry.boundsTree`
 *   should be displayed, or a `GeometryBVH` to display directly.
 * @param {GeometryBVH | number | null} [bvh=null] - The BVH to visualize. When the first
 *   argument is a `GeometryBVH`, this argument is interpreted as `depth`.
 * @param {number} [depth=10] - Maximum tree depth to display.
 * @extends Group
 */
export class BVHHelper extends Group {

	/**
	 * Shortcut to `edgeMaterial.color`.
	 * @type {Color}
	 * @readonly
	 */
	get color() {

		return this.edgeMaterial.color;

	}

	/**
	 * Opacity applied to both edge and mesh materials.
	 * @type {number}
	 */
	get opacity() {

		return this.edgeMaterial.opacity;

	}

	set opacity( v ) {

		this.edgeMaterial.opacity = v;
		this.meshMaterial.opacity = v;

	}

	get objectIndex() {

		console.warn( 'BVHHelper: "objectIndex" has been renamed "instanceId".' );
		return this.instanceId;

	}

	set objectIndex( v ) {

		console.warn( 'BVHHelper: "objectIndex" has been renamed "instanceId".' );
		this.instanceId = v;

	}

	constructor( mesh = null, bvh = null, depth = 10 ) {

		// handle bvh, depth signature
		if ( mesh instanceof MeshBVH ) {

			depth = bvh || 10;
			bvh = mesh;
			mesh = null;

		}

		// handle mesh, depth signature
		if ( typeof bvh === 'number' ) {

			depth = bvh;
			bvh = null;

		}

		super();

		this.name = 'BVHHelper';

		/** @type {number} */
		this.depth = depth;

		/** @type {Object3D | null} */
		this.mesh = mesh;

		/** @type {GeometryBVH | null} */
		this.bvh = bvh;

		/** @type {boolean} */
		this.displayParents = false;

		/** @type {boolean} */
		this.displayEdges = true;

		/** @type {number} */
		this.instanceId = 0;

		this._roots = [];

		const edgeMaterial = new LineBasicMaterial( {
			color: 0x00FF88,
			transparent: true,
			opacity: 0.3,
			depthWrite: false,
		} );

		const meshMaterial = new MeshBasicMaterial( {
			color: 0x00FF88,
			transparent: true,
			opacity: 0.3,
			depthWrite: false,
		} );

		meshMaterial.color = edgeMaterial.color;

		/**
		 * Material used when rendering in wireframe edge mode.
		 * @type {LineBasicMaterial}
		 */
		this.edgeMaterial = edgeMaterial;

		/**
		 * Material used when rendering in solid face mode.
		 * @type {MeshBasicMaterial}
		 */
		this.meshMaterial = meshMaterial;

		this.update();

	}

	/**
	 * Rebuilds the helper's display geometry from the current BVH state. Must
	 * be called after changes to the BVH, `depth`, `displayParents`, or
	 * `displayEdges`.
	 */
	update() {

		const mesh = this.mesh;
		const instanceId = this.instanceId;
		let bvh = this.bvh || mesh.boundsTree || mesh.geometry && mesh.geometry.boundsTree || null;
		if ( mesh && mesh.isBatchedMesh && mesh.boundsTrees && ! bvh && instanceId >= 0 ) {

			// get the bvh from a batchedMesh if not provided
			// TODO: we should have an official way to get the geometry index cleanly
			const drawInfo = mesh._drawInfo[ instanceId ];
			if ( drawInfo ) {

				bvh = mesh.boundsTrees[ drawInfo.geometryIndex ] || bvh;

			}

		}

		const totalRoots = bvh ? bvh._roots.length : 0;
		while ( this._roots.length > totalRoots ) {

			const root = this._roots.pop();
			root.geometry.dispose();
			this.remove( root );

		}

		for ( let i = 0; i < totalRoots; i ++ ) {

			const { depth, edgeMaterial, meshMaterial, displayParents, displayEdges } = this;

			if ( i >= this._roots.length ) {

				const root = new BVHRootHelper( bvh, edgeMaterial, depth, i );
				this.add( root );
				this._roots.push( root );

			}

			const root = this._roots[ i ];
			root.bvh = bvh;
			root.depth = depth;
			root.displayParents = displayParents;
			root.displayEdges = displayEdges;
			root.material = displayEdges ? edgeMaterial : meshMaterial;
			root.update();

		}

	}

	updateMatrixWorld( ...args ) {

		const mesh = this.mesh;
		const parent = this.parent;
		const instanceId = this.instanceId;

		if ( mesh !== null ) {

			mesh.updateWorldMatrix( true, false );

			if ( parent ) {

				this.matrix
					.copy( parent.matrixWorld )
					.invert()
					.multiply( mesh.matrixWorld );

			} else {

				this.matrix
					.copy( mesh.matrixWorld );

			}

			// handle batched and instanced mesh bvhs
			if ( ( mesh.isInstancedMesh || mesh.isBatchedMesh ) && instanceId >= 0 ) {

				mesh.getMatrixAt( instanceId, matrix );
				this.matrix.multiply( matrix );

			}

			this.matrix.decompose(
				this.position,
				this.quaternion,
				this.scale,
			);

		}

		super.updateMatrixWorld( ...args );

	}

	copy( source ) {

		this.depth = source.depth;
		this.mesh = source.mesh;
		this.bvh = source.bvh;
		this.opacity = source.opacity;
		this.color.copy( source.color );

	}

	clone() {

		return new BVHHelper().copy( this );

	}

	/**
	 * Disposes of the materials and geometries used by the helper.
	 */
	dispose() {

		this.edgeMaterial.dispose();
		this.meshMaterial.dispose();

		const children = this.children;
		for ( let i = 0, l = children.length; i < l; i ++ ) {

			children[ i ].geometry.dispose();

		}

	}

}

export class MeshBVHHelper extends BVHHelper {

	constructor( ...args ) {

		console.warn( 'MeshBVHHelper: Class has been deprecated. Use BVHHelper instead.' );
		super( ...args );

	}

}