# BVH

class in `three-mesh-bvh`

```js
import { BVH } from 'three-mesh-bvh';
```

Abstract base class for BVH implementations. Provides core tree traversal and spatial query
methods. Subclasses implement primitive-specific logic by overriding `writePrimitiveBounds`
and related internal methods.

## Methods

### .shiftPrimitiveOffsets

```js
.shiftPrimitiveOffsets( offset: number )
```

Adjusts all primitive offsets stored in the BVH leaf nodes by the given value. Useful when
geometry buffers have been shifted or compacted (e.g. when merging geometries).

- `offset`, `number`

### .traverse

```js
.traverse( callback: function, rootIndex?: number )
```

Traverses all nodes of the BVH, invoking a callback for each node.

For leaf nodes the callback receives `( depth, isLeaf, boundingData, offset, count )`.
For internal nodes it receives `( depth, isLeaf, boundingData, splitAxis )` and may
return `true` to stop descending into that node's children.

- `callback`, `function`
- `rootIndex`, `number`, optional, default `0`

### .refit

```js
.refit(  )
```

Refits all BVH node bounds to reflect the current primitive positions. Faster than
rebuilding the BVH but produces a less optimal tree after large vertex deformations.

### .getBoundingBox

```js
.getBoundingBox( target: Box3 ): Box3
```

Computes the axis-aligned bounding box of all primitives in the BVH.

- `target`, `Box3`: Target box to write the result into.

### .shapecast

```js
.shapecast( callbacks: Object ): boolean
```

A generalized traversal function for performing spatial queries against the BVH. Returns
`true` as soon as a primitive has been reported as intersected. The tree is traversed
depth-first; `boundsTraverseOrder` controls which child is visited first. Returning
`CONTAINED` from `intersectsBounds` skips further child traversal and intersects all
primitives in that subtree immediately.

- `callbacks`, `Object`
  - `intersectsBounds`, `IntersectsBoundsCallback`
  - `intersectsRange`, `IntersectsRangeCallback`, optional
  - `boundsTraverseOrder`, `BoundsTraverseOrderCallback`, optional

### .bvhcast

```js
.bvhcast( otherBvh: BVH, matrixToLocal: Matrix4, callbacks: Object ): boolean
```

Simultaneously traverses two BVH structures to find intersecting primitive pairs. Returns
`true` as soon as any intersection is reported. Both trees are traversed depth-first with
alternating descent. `matrixToLocal` transforms `otherBvh` into the local space of this BVH.

- `otherBvh`, `BVH`
- `matrixToLocal`, `Matrix4`
- `callbacks`, `Object`
  - `intersectsRanges`, `IntersectsRangesCallback`
