# AccelerationStructure

The viewer is using three-mesh-bvh (opens new window) as the backbone for it's BVH implementation. The AccelerationStructure class is a thin wrapper around the library's MeshBVH (opens new window) 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

#

Accessors

bvh geometry

#

Methods

buildBVH getBoundingBox getVertexAtIndex raycast
raycastFirst shapecast transformInput transformOutput

#

Typedefs

VectorLike

#

Constructors

#

constructor

constructor(bvh: MeshBVH)
1

Populates/constructs this acceleration structure with the backing BVH.

Parameters

#

Accessors

# bvh

get bvh(): MeshBVH
1

Gets the backing BVH.

Returns: MeshBVH (opens new window)

# geometry

get geometry(): BufferGeometry
1

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.

Returns: BufferGeometry (opens new window)

#

Methods

# buildBVH

static buildBVH(
    indices: number[],
    position: Float32Array,
    options: BVHOptions = DefaultBVHOptions,
    transform?: Matrix4
): MeshBVH
1
2
3
4
5
6

Build a BVH using the provided geometry data.

Parameters

  • indices: Geometry indices
  • position: Geometry vertex positions
  • options: BVHOptions
  • optional transform: A Matrix4 (opens new window) that transforms the geometry data before building the BVH

Returns: MeshBVH (opens new window)

# getBoundingBox

getBoundingBox(target?: Box3): Box3
1

Gets the aabb of the entire BVH.

Parameters

Returns: Box3 (opens new window)

# getVertexAtIndex

getVertexAtIndex(index: number): Vector3
1

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

Parameters

  • index: number

Returns: Vector3 (opens new window)

# raycast

raycast(
    ray: Ray,
    materialOrSide: Side | Material | Material[] = FrontSide
): Intersection<Object3D<Event>>[]
1
2
3
4

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

Parameters

Returns: Intersection (opens new window)

# raycastFirst

raycastFirst(
    ray: Ray,
    materialOrSide: Side | Material | Material[] = FrontSide
): Intersection<Object3D<Event>>[]
1
2
3
4

Identical to raycast but stops at first intersection found.

Parameters

Returns: Intersection (opens new window)

# shapecast

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
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

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

Parameters

Returns: boolean

# transformInput

transformInput<T extends Vector3 | Ray | Box3>(input: T): T
1

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, getVertexAtIndex, raycast, raycastFirst, shapecast already call this function implicitly.

Parameters

Returns: Vector3 (opens new window) | Ray (opens new window) | Box3 (opens new window)


# transformOutput

transformOutput<T extends Vector3 | Ray | Box3>(output: T): T
1

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

Parameters

Returns: Vector3 (opens new window) | Ray (opens new window) | Box3 (opens new window)

#

Typedefs

# VectorLike

type VectorLike = { x: number; y: number; z?: number; w?: number };
1

Archtype for Vector2, Vector3 and Vector4.