|
|
@@ -1,16 +1,51 @@
|
|
|
import { Vector3 } from './Vector3.js';
|
|
|
|
|
|
+/**
|
|
|
+ * Represents an axis-aligned bounding box (AABB) in 3D space.
|
|
|
+ */
|
|
|
class Box3 {
|
|
|
|
|
|
+ /**
|
|
|
+ * Constructs a new bounding box.
|
|
|
+ *
|
|
|
+ * @param {Vector3} [min=(Infinity,Infinity,Infinity)] - A vector representing the lower boundary of the box.
|
|
|
+ * @param {Vector3} [max=(-Infinity,-Infinity,-Infinity)] - A vector representing the upper boundary of the box.
|
|
|
+ */
|
|
|
constructor( min = new Vector3( + Infinity, + Infinity, + Infinity ), max = new Vector3( - Infinity, - Infinity, - Infinity ) ) {
|
|
|
|
|
|
+ /**
|
|
|
+ * This flag can be used for type testing.
|
|
|
+ *
|
|
|
+ * @type {boolean}
|
|
|
+ * @readonly
|
|
|
+ * @default true
|
|
|
+ */
|
|
|
this.isBox3 = true;
|
|
|
|
|
|
+ /**
|
|
|
+ * The lower boundary of the box.
|
|
|
+ *
|
|
|
+ * @type {Vector3}
|
|
|
+ */
|
|
|
this.min = min;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The upper boundary of the box.
|
|
|
+ *
|
|
|
+ * @type {Vector3}
|
|
|
+ */
|
|
|
this.max = max;
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Sets the lower and upper boundaries of this box.
|
|
|
+ * Please note that this method only copies the values from the given objects.
|
|
|
+ *
|
|
|
+ * @param {Vector3} min - The lower boundary of the box.
|
|
|
+ * @param {Vector3} max - The upper boundary of the box.
|
|
|
+ * @return {Box3} A reference to this bounding box.
|
|
|
+ */
|
|
|
set( min, max ) {
|
|
|
|
|
|
this.min.copy( min );
|
|
|
@@ -20,6 +55,13 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Sets the upper and lower bounds of this box so it encloses the position data
|
|
|
+ * in the given array.
|
|
|
+ *
|
|
|
+ * @param {Array<number>} array - An array holding 3D position data.
|
|
|
+ * @return {Box3} A reference to this bounding box.
|
|
|
+ */
|
|
|
setFromArray( array ) {
|
|
|
|
|
|
this.makeEmpty();
|
|
|
@@ -34,6 +76,13 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Sets the upper and lower bounds of this box so it encloses the position data
|
|
|
+ * in the given buffer attribute.
|
|
|
+ *
|
|
|
+ * @param {BufferAttribute} attribute - A buffer attribute holding 3D position data.
|
|
|
+ * @return {Box3} A reference to this bounding box.
|
|
|
+ */
|
|
|
setFromBufferAttribute( attribute ) {
|
|
|
|
|
|
this.makeEmpty();
|
|
|
@@ -48,6 +97,13 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Sets the upper and lower bounds of this box so it encloses the position data
|
|
|
+ * in the given array.
|
|
|
+ *
|
|
|
+ * @param {Array<Vector3>} points - An array holding 3D position data as instances of {@link Vector3}.
|
|
|
+ * @return {Box3} A reference to this bounding box.
|
|
|
+ */
|
|
|
setFromPoints( points ) {
|
|
|
|
|
|
this.makeEmpty();
|
|
|
@@ -62,6 +118,14 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Centers this box on the given center vector and sets this box's width, height and
|
|
|
+ * depth to the given size values.
|
|
|
+ *
|
|
|
+ * @param {Vector3} center - The center of the box.
|
|
|
+ * @param {Vector3} size - The x, y and z dimensions of the box.
|
|
|
+ * @return {Box3} A reference to this bounding box.
|
|
|
+ */
|
|
|
setFromCenterAndSize( center, size ) {
|
|
|
|
|
|
const halfSize = _vector.copy( size ).multiplyScalar( 0.5 );
|
|
|
@@ -73,6 +137,16 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Computes the world-axis-aligned bounding box of an [page:Object3D]
|
|
|
+ * (including its children), accounting for the object's, and children's,
|
|
|
+ * world transforms. The function may result in a larger box than strictly necessary.
|
|
|
+ *
|
|
|
+ * @param {Object3D} object - The 3D object to compute the bounding box for.
|
|
|
+ * @param {boolean} [precise=false] - If set to `true`, the method computes the smallest
|
|
|
+ * world-axis-aligned bounding box at the expense of more computation.
|
|
|
+ * @return {Box3} A reference to this bounding box.
|
|
|
+ */
|
|
|
setFromObject( object, precise = false ) {
|
|
|
|
|
|
this.makeEmpty();
|
|
|
@@ -81,12 +155,23 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns a new box with copied values from this instance.
|
|
|
+ *
|
|
|
+ * @return {Box3} A clone of this instance.
|
|
|
+ */
|
|
|
clone() {
|
|
|
|
|
|
return new this.constructor().copy( this );
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Copies the values of the given box to this instance.
|
|
|
+ *
|
|
|
+ * @param {Box3} box - The box to copy.
|
|
|
+ * @return {Box3} A reference to this bounding box.
|
|
|
+ */
|
|
|
copy( box ) {
|
|
|
|
|
|
this.min.copy( box.min );
|
|
|
@@ -96,6 +181,11 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Makes this box empty which means in encloses a zero space in 3D.
|
|
|
+ *
|
|
|
+ * @return {Box3} A reference to this bounding box.
|
|
|
+ */
|
|
|
makeEmpty() {
|
|
|
|
|
|
this.min.x = this.min.y = this.min.z = + Infinity;
|
|
|
@@ -105,6 +195,13 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns true if this box includes zero points within its bounds.
|
|
|
+ * Note that a box with equal lower and upper bounds still includes one
|
|
|
+ * point, the one both bounds share.
|
|
|
+ *
|
|
|
+ * @return {boolean} Whether this box is empty or not.
|
|
|
+ */
|
|
|
isEmpty() {
|
|
|
|
|
|
// this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
|
|
|
@@ -113,18 +210,36 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns the center point of this box.
|
|
|
+ *
|
|
|
+ * @param {Vector3} target - The target vector that is used to store the method's result.
|
|
|
+ * @return {Vector3} The center point.
|
|
|
+ */
|
|
|
getCenter( target ) {
|
|
|
|
|
|
return this.isEmpty() ? target.set( 0, 0, 0 ) : target.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns the dimensions of this box.
|
|
|
+ *
|
|
|
+ * @param {Vector3} target - The target vector that is used to store the method's result.
|
|
|
+ * @return {Vector3} The size.
|
|
|
+ */
|
|
|
getSize( target ) {
|
|
|
|
|
|
return this.isEmpty() ? target.set( 0, 0, 0 ) : target.subVectors( this.max, this.min );
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Expands the boundaries of this box to include the given point.
|
|
|
+ *
|
|
|
+ * @param {Vector3} point - The point that should be included by the bounding box.
|
|
|
+ * @return {Box3} A reference to this bounding box.
|
|
|
+ */
|
|
|
expandByPoint( point ) {
|
|
|
|
|
|
this.min.min( point );
|
|
|
@@ -134,6 +249,16 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Expands this box equilaterally by the given vector. The width of this
|
|
|
+ * box will be expanded by the x component of the vector in both
|
|
|
+ * directions. The height of this box will be expanded by the y component of
|
|
|
+ * the vectorin both directions. The depth of this box will be
|
|
|
+ * expanded by the z component of the vector in both directions.
|
|
|
+ *
|
|
|
+ * @param {Vector3} vector - The vector that should expand the bounding box.
|
|
|
+ * @return {Box3} A reference to this bounding box.
|
|
|
+ */
|
|
|
expandByVector( vector ) {
|
|
|
|
|
|
this.min.sub( vector );
|
|
|
@@ -143,6 +268,13 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Expands each dimension of the box by the given scalar. If negative, the
|
|
|
+ * dimensions of the box will be contracted.
|
|
|
+ *
|
|
|
+ * @param {number} scalar - The scalar value that should expand the bounding box.
|
|
|
+ * @return {Box3} A reference to this bounding box.
|
|
|
+ */
|
|
|
expandByScalar( scalar ) {
|
|
|
|
|
|
this.min.addScalar( - scalar );
|
|
|
@@ -152,6 +284,17 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Expands the boundaries of this box to include [page:Object3D object] and
|
|
|
+ * its children, accounting for the object's, and children's, world
|
|
|
+ * transforms. The function may result in a larger box than strictly
|
|
|
+ * necessary (unless the precise parameter is set to true).
|
|
|
+ *
|
|
|
+ * @param {Object3D} object - The 3D object that should expand the bounding box.
|
|
|
+ * @param {boolean} precise - If set to `true`, the method expands the bounding box
|
|
|
+ * as little as necessary at the expense of more computation.
|
|
|
+ * @return {Box3} A reference to this bounding box.
|
|
|
+ */
|
|
|
expandByObject( object, precise = false ) {
|
|
|
|
|
|
// Computes the world-axis-aligned bounding box of an object (including its children),
|
|
|
@@ -236,6 +379,12 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns `true` if the given point lies within or on the boundaries of this box.
|
|
|
+ *
|
|
|
+ * @param {Vector3} point - The point to test.
|
|
|
+ * @return {boolean} Whether the bounding box contains the given point or not.
|
|
|
+ */
|
|
|
containsPoint( point ) {
|
|
|
|
|
|
return point.x >= this.min.x && point.x <= this.max.x &&
|
|
|
@@ -244,6 +393,13 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns `true` if this bounding box includes the entirety of the given bounding box.
|
|
|
+ * If this box and the given one are identical, this function also returns `true`.
|
|
|
+ *
|
|
|
+ * @param {Box3} box - The bounding box to test.
|
|
|
+ * @return {boolean} Whether the bounding box contains the given bounding box or not.
|
|
|
+ */
|
|
|
containsBox( box ) {
|
|
|
|
|
|
return this.min.x <= box.min.x && box.max.x <= this.max.x &&
|
|
|
@@ -252,6 +408,13 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns a point as a proportion of this box's width, height and depth.
|
|
|
+ *
|
|
|
+ * @param {Vector3} point - A point in 3D space.
|
|
|
+ * @param {Vector3} target - The target vector that is used to store the method's result.
|
|
|
+ * @return {Vector3} A point as a proportion of this box's width, height and depth.
|
|
|
+ */
|
|
|
getParameter( point, target ) {
|
|
|
|
|
|
// This can potentially have a divide by zero if the box
|
|
|
@@ -265,6 +428,12 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns `true` if the given bounding box intersects with this bounding box.
|
|
|
+ *
|
|
|
+ * @param {Box3} box - The bounding box to test.
|
|
|
+ * @return {boolean} Whether the given bounding box intersects with this bounding box.
|
|
|
+ */
|
|
|
intersectsBox( box ) {
|
|
|
|
|
|
// using 6 splitting planes to rule out intersections.
|
|
|
@@ -274,6 +443,12 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns `true` if the given bounding sphere intersects with this bounding box.
|
|
|
+ *
|
|
|
+ * @param {Sphere} sphere - The bounding sphere to test.
|
|
|
+ * @return {boolean} Whether the given bounding sphere intersects with this bounding box.
|
|
|
+ */
|
|
|
intersectsSphere( sphere ) {
|
|
|
|
|
|
// Find the point on the AABB closest to the sphere center.
|
|
|
@@ -284,6 +459,12 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns `true` if the given plane intersects with this bounding box.
|
|
|
+ *
|
|
|
+ * @param {Plane} plane - The plane to test.
|
|
|
+ * @return {boolean} Whether the given plane intersects with this bounding box.
|
|
|
+ */
|
|
|
intersectsPlane( plane ) {
|
|
|
|
|
|
// We compute the minimum and maximum dot product values. If those values
|
|
|
@@ -331,6 +512,12 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns `true` if the given triangle intersects with this bounding box.
|
|
|
+ *
|
|
|
+ * @param {Triangle} triangle - The triangle to test.
|
|
|
+ * @return {boolean} Whether the given triangle intersects with this bounding box.
|
|
|
+ */
|
|
|
intersectsTriangle( triangle ) {
|
|
|
|
|
|
if ( this.isEmpty() ) {
|
|
|
@@ -384,18 +571,38 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Clamps the given point within the bounds of this box.
|
|
|
+ *
|
|
|
+ * @param {Vector3} point - The point to clamp.
|
|
|
+ * @param {Vector3} target - The target vector that is used to store the method's result.
|
|
|
+ * @return {Vector3} The clamped point.
|
|
|
+ */
|
|
|
clampPoint( point, target ) {
|
|
|
|
|
|
return target.copy( point ).clamp( this.min, this.max );
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns the euclidean distance from any edge of this box to the specified point. If
|
|
|
+ * the given point lies inside of this box, the distance will be `0`.
|
|
|
+ *
|
|
|
+ * @param {Vector3} point - The point to compute the distance to.
|
|
|
+ * @return {number} The euclidean distance.
|
|
|
+ */
|
|
|
distanceToPoint( point ) {
|
|
|
|
|
|
return this.clampPoint( point, _vector ).distanceTo( point );
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns a bounding sphere that encloses this bounding box.
|
|
|
+ *
|
|
|
+ * @param {Sphere} target - The target sphere that is used to store the method's result.
|
|
|
+ * @return {Sphere} The bounding sphere that encloses this bounding box.
|
|
|
+ */
|
|
|
getBoundingSphere( target ) {
|
|
|
|
|
|
if ( this.isEmpty() ) {
|
|
|
@@ -414,6 +621,15 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Computes the intersection of this bounding box and the given one, setting the upper
|
|
|
+ * bound of this box to the lesser of the two boxes' upper bounds and the
|
|
|
+ * lower bound of this box to the greater of the two boxes' lower bounds. If
|
|
|
+ * there's no overlap, makes this box empty.
|
|
|
+ *
|
|
|
+ * @param {Box3} box - The bounding box to intersect with.
|
|
|
+ * @return {Box3} A reference to this bounding box.
|
|
|
+ */
|
|
|
intersect( box ) {
|
|
|
|
|
|
this.min.max( box.min );
|
|
|
@@ -426,6 +642,14 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Computes the union of this box and [page:Box3 box], setting the upper
|
|
|
+ * bound of this box to the greater of the two boxes' upper bounds and the
|
|
|
+ * lower bound of this box to the lesser of the two boxes' lower bounds.
|
|
|
+ *
|
|
|
+ * @param {Box3} box - The bounding box that will be unioned with this instance.
|
|
|
+ * @return {Box3} A reference to this bounding box.
|
|
|
+ */
|
|
|
union( box ) {
|
|
|
|
|
|
this.min.min( box.min );
|
|
|
@@ -435,6 +659,12 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Transforms this bounding box by the given 4x4 transformation matrix.
|
|
|
+ *
|
|
|
+ * @param {Matrix4} matrix - The transformation matrix.
|
|
|
+ * @return {Box3} A reference to this bounding box.
|
|
|
+ */
|
|
|
applyMatrix4( matrix ) {
|
|
|
|
|
|
// transform of empty box is an empty box.
|
|
|
@@ -456,6 +686,13 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Adds the given offset to both the upper and lower bounds of this bounding box,
|
|
|
+ * effectively moving it in 3D space.
|
|
|
+ *
|
|
|
+ * @param {Vector3} offset - The offset that should be used to translate the bounding box.
|
|
|
+ * @return {Box3} A reference to this bounding box.
|
|
|
+ */
|
|
|
translate( offset ) {
|
|
|
|
|
|
this.min.add( offset );
|
|
|
@@ -465,6 +702,12 @@ class Box3 {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns `true` if this bounding box is equal with the given one.
|
|
|
+ *
|
|
|
+ * @param {Box3} box - The box to test for equality.
|
|
|
+ * @return {boolean} Whether this bounding box is equal with the given one.
|
|
|
+ */
|
|
|
equals( box ) {
|
|
|
|
|
|
return box.min.equals( this.min ) && box.max.equals( this.max );
|