> ## Documentation Index
> Fetch the complete documentation index at: https://docs.speckle.systems/llms.txt
> Use this file to discover all available pages before exploring further.

# AccelerationStructure

> Thin wrapper around the three-mesh-bvh library's MeshBVH class with some additional specific functionality.

The viewer is using [*three-mesh-bvh*](https://github.com/gkjohnson/three-mesh-bvh)
as the backbone for it's BVH implementation. The AccelerationStructure class is a
thin wrapper around the library's
[*MeshBVH*](https://threejs.org/docs/index.html?q=box#api/en/math/Box3) class
with some additional specific functionality.

The speckle viewer uses a dual level BVH for optimal acceleration. The
`AccelerationStructure` is the functional element of the *bottom-level* acceleration
structure. Each individual object will have it's own BVH, encapsulated by an
`AccelerationStructure` object.

## Constructors

### constructor

```typescript theme={null}
constructor(bvh: MeshBVH)
```

Populates/constructs this acceleration structure with the backing BVH.

**Parameters**

* **bvh**: The backing BVH as a [*MeshBVH*](https://github.com/gkjohnson/three-mesh-bvh/blob/master/src/core/MeshBVH.js)

## Accessors

### bvh

```typescript theme={null}
get bvh(): MeshBVH
```

Gets the backing BVH.

**Returns**: [*MeshBVH*](https://github.com/gkjohnson/three-mesh-bvh/blob/master/src/core/MeshBVH.js)

#### geometry

```typescript theme={null}
get geometry(): BufferGeometry
```

Gets the three.js geometry associated to the BVH.

<Tip>
  When building a BVH, three-mesh-bvh library needs a three.js geometry as
  input. This is that geometry. We don't use it for rendering.
</Tip>

**Returns**: [*BufferGeometry*](https://threejs.org/docs/index.html?q=buffer#api/en/core/BufferGeometry)

## Methods

### buildBVH

```typescript theme={null}
static buildBVH(
    indices: number[],
    position: Float32Array,
    options: BVHOptions = DefaultBVHOptions,
    transform?: Matrix4
): MeshBVH
```

Build a BVH using the provided geometry data.

**Parameters**

* **indices**: Geometry indices
* **position**: Geometry vertex positions
* **options**: [*BVHOptions*](/developers/viewer/acceleration-structure-api#bvhoptions)
* *optional* **transform**: A [*Matrix4*](https://threejs.org/docs/index.html?q=matri#api/en/math/Matrix4) that transforms the geometry data before building the BVH

**Returns**: [*MeshBVH*](https://github.com/gkjohnson/three-mesh-bvh/blob/master/src/core/MeshBVH.js)

### getBoundingBox

```typescript theme={null}
getBoundingBox(target?: Box3): Box3
```

Gets the aabb of the entire BVH.

**Parameters**

* *optional* **target**: [*Box3*](https://threejs.org/docs/index.html?q=box3#api/en/math/Box3)

**Returns**: [*Box3*](https://threejs.org/docs/index.html?q=box3#api/en/math/Box3)

### getVertexAtIndex

```typescript theme={null}
getVertexAtIndex(index: number): Vector3
```

Gets position value of a vertex at the given index inside the BVH vertex position array.

**Parameters**

* **index**: number

**Returns**: [*Vector3*](https://threejs.org/docs/index.html?q=vec#api/en/math/Vector3)

### raycast

```typescript theme={null}
raycast(
    ray: Ray,
    materialOrSide: Side | Material | Material[] = FrontSide
): Intersection<Object3D<Event>>[]
```

Wrapper over three-mesh-bvh raycast function. Keeps original behavior,but makes sure input and output spaces are correct.

**Parameters**

* **ray**: [*Ray*](https://threejs.org/docs/index.html?q=ray#api/en/math/Ray) to intersect with
* **materialOrSide**: [*Side*](https://threejs.org/docs/index.html?q=Materia#api/en/constants/Materials) | [*Material*](https://threejs.org/docs/index.html?q=Materia#api/en/materials/Material) | [*Material\[\]*](https://threejs.org/docs/index.html?q=Materia#api/en/materials/Material)

**Returns**: [*Intersection*](https://threejs.org/docs/index.html?q=rayc#api/en/core/Raycaster.intersectObject)

### raycastFirst

```typescript theme={null}
raycastFirst(
    ray: Ray,
    materialOrSide: Side | Material | Material[] = FrontSide
): Intersection<Object3D<Event>>[]
```

Identical to `raycast` but stops at first intersection found.

**Parameters**

* **ray**: [*Ray*](https://threejs.org/docs/index.html?q=ray#api/en/math/Ray) to intersect with
* **materialOrSide**: [*Side*](https://threejs.org/docs/index.html?q=Materia#api/en/constants/Materials) | [*Material*](https://threejs.org/docs/index.html?q=Materia#api/en/materials/Material) | [*Material\[\]*](https://threejs.org/docs/index.html?q=Materia#api/en/materials/Material)

**Returns**: [*Intersection*](https://threejs.org/docs/index.html?q=rayc#api/en/core/Raycaster.intersectObject)

### shapecast

```typescript theme={null}
shapecast(
    callbacks: {
      intersectsBounds: (
        box: Box3,
        isLeaf: boolean,
        score: number | undefined,
        depth: number,
        nodeIndex: number
      ) => ShapecastIntersection | boolean

      traverseBoundsOrder?: (box: Box3) => number
    } & (
      | {
          intersectsRange: (
            triangleOffset: number,
            triangleCount: number,
            contained: boolean,
            depth: number,
            nodeIndex: number,
            box: Box3
          ) => boolean
        }
      | {
          intersectsTriangle: (
            triangle: ExtendedTriangle,
            triangleIndex: number,
            contained: boolean,
            depth: number
          ) => boolean | void
        }
    )
  ): boolean
```

Generic mechanism to intersect the BVH with various shapes/objects. The callbacks provide granular access to several stages of the BVH intersection process.

**Parameters**

* **callbacks**: More details [*here*](https://github.com/gkjohnson/three-mesh-bvh/tree/master?tab=readme-ov-file#shapecast)

**Returns**: boolean

### transformInput

```typescript theme={null}
transformInput<T extends Vector3 | Ray | Box3>(input: T): T
```

Transform input vector, ray or box from world space into the acceleration structure's space.

<Warning>
  All the AccelerationStructure methods that deal with querying the BVH:
  [getBoundingBox](/developers/viewer/acceleration-structure-api#getboundingbox),
  [getVertexAtIndex](/developers/viewer/acceleration-structure-api#getvertexatindex),
  [raycast](/developers/viewer/acceleration-structure-api#raycast),
  [raycastFirst](/developers/viewer/acceleration-structure-api#raycastfirst),
  [shapecast](/developers/viewer/acceleration-structure-api#shapecast) already call
  this function implicitly.
</Warning>

**Parameters**

* **input**: [*Vector3*](https://threejs.org/docs/index.html?q=vec#api/en/math/Vector3) | [*Ray*](https://threejs.org/docs/index.html?q=ray#api/en/math/Ray) | [*Box3*](https://threejs.org/docs/index.html?q=box#api/en/math/Box3)

**Returns**: [*Vector3*](https://threejs.org/docs/index.html?q=vec#api/en/math/Vector3) | [*Ray*](https://threejs.org/docs/index.html?q=ray#api/en/math/Ray) | [*Box3*](https://threejs.org/docs/index.html?q=box#api/en/math/Box3)

<br />

### transformOutput

```typescript theme={null}
transformOutput<T extends Vector3 | Ray | Box3>(output: T): T
```

Transform input vector, ray or box from the acceleration structure's space into world space.

**Parameters**

* **input**: [*Vector3*](https://threejs.org/docs/index.html?q=vec#api/en/math/Vector3) | [*Ray*](https://threejs.org/docs/index.html?q=ray#api/en/math/Ray) | [*Box3*](https://threejs.org/docs/index.html?q=box#api/en/math/Box3)
  <Warning>
    All the AccelerationStructure methods that deal with querying the BVH:
    [getBoundingBox](/developers/viewer/acceleration-structure-api#getboundingbox),
    [getVertexAtIndex](/developers/viewer/acceleration-structure-api#getvertexatindex),
    [raycast](/developers/viewer/acceleration-structure-api#raycast),
    [raycastFirst](/developers/viewer/acceleration-structure-api#raycastfirst),
    [shapecast](/developers/viewer/acceleration-structure-api#shapecast) already call
    this function implicitly.
  </Warning>

**Returns**: [*Vector3*](https://threejs.org/docs/index.html?q=vec#api/en/math/Vector3) | [*Ray*](https://threejs.org/docs/index.html?q=ray#api/en/math/Ray) | [*Box3*](https://threejs.org/docs/index.html?q=box#api/en/math/Box3)

## Typedefs

### VectorLike

```typescript theme={null}
type VectorLike = { x: number; y: number; z?: number; w?: number };
```

Archtype for Vector2, Vector3 and Vector4.

### BVHOptions

```typescript theme={null}
interface BVHOptions {
  strategy: SplitStrategy;
  maxDepth: number;
  maxLeafTris: number;
  verbose: boolean;
  useSharedArrayBuffer: boolean;
  setBoundingBox: boolean;
  onProgress?: () => void;
  [SKIP_GENERATION]: boolean;
}
```

Based off the original options defined in [*three-mesh-bvh*](https://github.com/gkjohnson/three-mesh-bvh/blob/a3a7ca7b2b275606963a616c80ca1262a7e48d7f/src/core/MeshBVH.js#L29)
