OrientedBoundingBox
An OrientedBoundingBox is a closed and convex cuboid. It can provide a tighter bounding volume than a bounding sphere or an axis aligned bounding box in many cases.
The class support two representations of an oriented bounding box:
- A half-axes based representation. 3 half axes vectors (
halfAxes: Matrix3) describe size and orientation of a bounding box. This approach is used in the 3DTiles specification (https://github.com/CesiumGS/3d-tiles/tree/master/specification#box) - A half-size-quaternion based representation. A
halfSize: number[3]array describes size, aquaternion: Quaterniondescribes orientation of a bounding box. This approach is used in the Indexed 3d Scene Layer (I3S) specification (https://github.com/Esri/i3s-spec/blob/master/docs/1.7/obb.cmn.md).
Usage
Create an OrientedBoundingBox using a transformation matrix, a position where the box will be translated, and a scale.
import {Vector3} from '@math.gl/core/matrix';
import {OrientedBoundingBox} from '@math.gl/geometry/culling';
const center = new Vector3(1.0, 0.0, 0.0);
const halfAxes = new Matrix3().fromScale([1.0, 3.0, 2.0]);
const box = new OrientedBoundingBox(center, halfAxes);
Sort bounding boxes from back to front
boxes.sort(
(boxA, boxB) =>
boxB.distanceSquaredTo(camera.positionWC) - boxA.distanceSquaredTo(camera.positionWC)
);
Compute an oriented bounding box enclosing two points.
import {makeOrientedBoundingBoxFromPoints} from '@math.gl/geometry/culling';
const box = makeOrientedBoundingBoxFromPoints([
[2, 0, 0],
[-2, 0, 0]
]);
Inheritanceโ
class OrientedBoundingBox implements BoundingVolume.
Global Functionsโ
makeOrientedBoundingBoxFromPoints(positions : Array[3][], result? : OrientedBoundingBox) : OrientedBoundingBoxโ
Computes an instance of an OrientedBoundingBox of the given positions.
This is an implementation of Stefan Gottschalk's Collision Queries using Oriented Bounding Boxes (PHD thesis).
positionsList ofVector3points that the bounding box will enclose.resultOptional object onto which to store the result.
getOrientedBoundingBoxQuaternion(box): Quaternionโ
Standalone export from @math.gl/geometry/culling. Returns the quaternion describing
an oriented bounding box, excluding the scale of its half axes.
makeOrientedBoundingBoxFromQuaternion(center, halfSize, quaternion, result?)โ
Standalone export from @math.gl/geometry/culling. Constructs an oriented bounding
box from its center, half sizes and quaternion. Returns a new box or the supplied
result box. Inputs are copied; the result owns its center and half axes.
Fieldsโ
center: Vector3โ
The center position of the box.
halfAxes: Matrix3โ
The transformation matrix, to rotate the box to the right position.
readonly halfSize: number[]โ
The array with three half-sizes for the bounding box
Methodsโ
constructor(center = [0, 0, 0], halfAxes = [0, 0, 0, 0, 0, 0, 0, 0, 0])โ
constructorโ
center=Vector3.ZEROThe center of the box.halfAxes=Matrix3.ZEROThe three orthogonal half-axes of the bounding box. Equivalently, the transformation matrix, to rotate and scale a cube centered at the origin.
clone() : OrientedBoundingBoxโ
Duplicates a OrientedBoundingBox instance.
Returns
- A new
OrientedBoundingBoxinstance.
equals(right: OrientedBoundingBox) : Booleanโ
Compares the provided OrientedBoundingBox componentwise and returns true if they are equal, false otherwise.
rightThe secondOrientedBoundingBox
Returns
trueif left and right are equal,falseotherwise.
intersectPlane(plane : Plane) : CullingResultโ
Determines which side of a plane the oriented bounding box is located.
planeThe plane to test against.
Returns
'inside'if the entire box is on the side of the plane the normal is pointing'outside'if the entire box is on the opposite side, and'intersecting'if the box intersects the plane.
distanceTo(point : Number[3]) : Numberโ
Computes the estimated distance from the closest point on a bounding box to a point.
pointThe point
Returns
- The estimated distance from the bounding sphere to the point.
distanceSquaredTo(point : Number[3]) : Numberโ
Computes the estimated distance squared from the closest point on a bounding box to a point.
pointThe point
Returns
- The estimated distance squared from the bounding sphere to the point.
computePlaneDistances(position : Number[3], direction : Number[3], result : Number[2]]) : Number[2]โ
The distances calculated by the vector from the center of the bounding box to position projected onto direction.
If you imagine the infinite number of planes with normal direction, this computes the smallest distance to the closest and farthest planes from position that intersect the bounding box.
positionThe position to calculate the distance from.directionThe direction from position.resultAn optional Interval to store the nearest and farthest distances.
Returns
- The nearest and farthest distances on the bounding box from position in direction.
Attributionโ
This class was ported from Cesium under the Apache 2 License.
See Attributions for the project-wide source inventory and acknowledgments.