Repository: gkjohnson/three-mesh-bvh (js)

Issue:
Fix validation function for use with "indirect" bvh

Pull request description:
Fix bounds validation for indirect bvh
Fix #629 

Patch:
diff --git a/src/debug/Debug.js b/src/debug/Debug.js
index 2bf8238..7495e3b 100644
--- a/src/debug/Debug.js
+++ b/src/debug/Debug.js
@@ -192,11 +192,20 @@ function validateBounds( bvh ) {
 		if ( isLeaf ) {
 
 			// check triangles
-			for ( let i = offset * 3, l = ( offset + count ) * 3; i < l; i += 3 ) {
+			for ( let i = offset, l = offset + count; i < l; i ++ ) {
 
-				const i0 = index.getX( i );
-				const i1 = index.getX( i + 1 );
-				const i2 = index.getX( i + 2 );
+				const triIndex = bvh.resolveTriangleIndex( i );
+				let i0 = 3 * triIndex;
+				let i1 = 3 * triIndex + 1;
+				let i2 = 3 * triIndex + 2;
+
+				if ( index ) {
+
+					i0 = index.getX( i0 );
+					i1 = index.getX( i1 );
+					i2 = index.getX( i2 );
+
+				}
 
 				let isContained;
 


Test patch:
diff --git a/test/MeshBVH.options.test.js b/test/MeshBVH.options.test.js
index b6f0b59..ac24b61 100644
--- a/test/MeshBVH.options.test.js
+++ b/test/MeshBVH.options.test.js
@@ -8,6 +8,7 @@ import {
 	MeshBVH,
 	getBVHExtremes,
 	acceleratedRaycast,
+	validateBounds,
 } from '../src/index.js';
 import { getMaxDepth } from './utils.js';
 
@@ -160,6 +161,13 @@ describe( 'Options', () => {
 
 		} );
 
+		it( 'should produce valid bounds when true.', () => {
+
+			const bvh = new MeshBVH( geometry, { indirect: true } );
+			expect( validateBounds( bvh ) ).toBeTruthy();
+
+		} );
+
 		it( 'should unpack to the same rearranged index buffer.', () => {
 
 			const geometry = new TorusGeometry( 5, 5, 40, 10 );