import { Box3, Vector3 } from 'three';
import { arrayToBox } from './Utils/ArrayBoxUtilities.js';
const box1 = new Box3();
const box2 = new Box3();
const vec = new Vector3();

export class MeshBVHDebug {

	constructor( bvh, geometry ) {

		this.bvh = bvh;
		this.geometry = geometry;

	}

	validateBounds() {

		const { bvh, geometry } = this;
		const depthStack = [];
		const index = geometry.index;
		const position = geometry.getAttribute( 'position' );
		let passes = true;

		bvh.traverse( ( depth, isLeaf, boundingData, offset, count ) => {

			const info = {
				depth,
				isLeaf,
				boundingData,
				offset,
				count,
			};
			depthStack[ depth ] = info;

			arrayToBox( boundingData, box1 );
			const parent = depthStack[ depth - 1 ];

			if ( isLeaf ) {

				// check triangles
				for ( let i = offset * 3, l = ( offset + count ) * 3; i < l; i += 3 ) {

					const i0 = index.getX( i );
					const i1 = index.getX( i + 1 );
					const i2 = index.getX( i + 2 );

					let isContained;

					vec.fromBufferAttribute( position, i0 );
					isContained = box1.containsPoint( vec );

					vec.fromBufferAttribute( position, i1 );
					isContained = isContained && box1.containsPoint( vec );

					vec.fromBufferAttribute( position, i2 );
					isContained = isContained && box1.containsPoint( vec );

					console.assert( isContained, 'Leaf bounds does not fully contain triangle.' );
					passes = passes && isContained;

				}

			}

			if ( parent ) {

				// check if my bounds fit in my parents
				arrayToBox( boundingData, box2 );

				const isContained = box2.containsBox( box1 );
				console.assert( isContained, 'Parent bounds does not fully contain child.' );
				passes = passes && isContained;

			}

		} );

		return passes;

	}

}

@@ -282,16 +282,735 @@
 y;%0A%0A%09%7D%0A%0A
+%09// Returns a simple, human readable object that represents the BVH.%0A%09getJSONStructure() %7B%0A%0A%09%09const %7B bvh %7D = this;%0A%09%09const depthStack = %5B%5D;%0A%0A%09%09bvh.traverse( ( depth, isLeaf, boundingData, offset, count ) =%3E %7B%0A%0A%09%09%09const info = %7B%0A%09%09%09%09bounds: arrayToBox( boundingData, new Box3() ),%0A%09%09%09%7D;%0A%0A%09%09%09if ( isLeaf ) %7B%0A%0A%09%09%09%09info.count = count;%0A%09%09%09%09info.offset = offset;%0A%0A%09%09%09%7D else %7B%0A%0A%09%09%09%09info.left = null;%0A%09%09%09%09info.right = null;%0A%0A%09%09%09%7D%0A%0A%09%09%09depthStack%5B depth %5D = info;%0A%0A%09%09%09// traversal hits the left then right node%0A%09%09%09const parent = depthStack%5B depth - 1 %5D;%0A%09%09%09if ( parent ) %7B%0A%0A%09%09%09%09if ( parent.left === null ) %7B%0A%0A%09%09%09%09%09parent.left = info;%0A%0A%09%09%09%09%7D else %7B%0A%0A%09%09%09%09%09parent.right = info;%0A%0A%09%09%09%09%7D%0A%0A%09%09%09%7D%0A%0A%09%09%7D );%0A%0A%09%09return depthStack%5B 0 %5D;%0A%0A%09%7D%0A%0A
 %09validat

add `getJSONStructure`