@math.gl/culling 3.6.0-alpha.8 → 3.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/constants.d.ts +5 -5
  2. package/dist/constants.d.ts.map +1 -1
  3. package/dist/es5/constants.js +5 -7
  4. package/dist/es5/constants.js.map +1 -1
  5. package/dist/es5/lib/bounding-volumes/axis-aligned-bounding-box.js.map +1 -1
  6. package/dist/es5/lib/bounding-volumes/bounding-sphere.js.map +1 -1
  7. package/dist/es5/lib/bounding-volumes/oriented-bounding-box.js.map +1 -1
  8. package/dist/es5/lib/culling-volume.js.map +1 -1
  9. package/dist/esm/constants.js +5 -7
  10. package/dist/esm/constants.js.map +1 -1
  11. package/dist/esm/lib/bounding-volumes/axis-aligned-bounding-box.js.map +1 -1
  12. package/dist/esm/lib/bounding-volumes/bounding-sphere.js.map +1 -1
  13. package/dist/esm/lib/bounding-volumes/oriented-bounding-box.js.map +1 -1
  14. package/dist/esm/lib/culling-volume.js.map +1 -1
  15. package/dist/lib/bounding-volumes/axis-aligned-bounding-box.d.ts +1 -2
  16. package/dist/lib/bounding-volumes/axis-aligned-bounding-box.d.ts.map +1 -1
  17. package/dist/lib/bounding-volumes/bounding-sphere.d.ts +1 -2
  18. package/dist/lib/bounding-volumes/bounding-sphere.d.ts.map +1 -1
  19. package/dist/lib/bounding-volumes/bounding-volume.d.ts +1 -2
  20. package/dist/lib/bounding-volumes/bounding-volume.d.ts.map +1 -1
  21. package/dist/lib/bounding-volumes/oriented-bounding-box.d.ts +1 -2
  22. package/dist/lib/bounding-volumes/oriented-bounding-box.d.ts.map +1 -1
  23. package/dist/lib/culling-volume.d.ts +1 -2
  24. package/dist/lib/culling-volume.d.ts.map +1 -1
  25. package/package.json +3 -3
  26. package/src/constants.ts +5 -5
  27. package/src/lib/bounding-volumes/axis-aligned-bounding-box.ts +1 -1
  28. package/src/lib/bounding-volumes/bounding-sphere.ts +1 -1
  29. package/src/lib/bounding-volumes/bounding-volume.ts +1 -1
  30. package/src/lib/bounding-volumes/oriented-bounding-box.ts +1 -1
  31. package/src/lib/culling-volume.ts +2 -2
  32. package/dist/constants.js +0 -8
  33. package/dist/index.js +0 -13
  34. package/dist/lib/algorithms/bounding-box-from-points.js +0 -131
  35. package/dist/lib/algorithms/bounding-sphere-from-points.js +0 -140
  36. package/dist/lib/algorithms/compute-eigen-decomposition.js +0 -131
  37. package/dist/lib/bounding-volumes/axis-aligned-bounding-box.js +0 -111
  38. package/dist/lib/bounding-volumes/bounding-sphere.js +0 -118
  39. package/dist/lib/bounding-volumes/bounding-volume.js +0 -1
  40. package/dist/lib/bounding-volumes/oriented-bounding-box.js +0 -256
  41. package/dist/lib/culling-volume.js +0 -121
  42. package/dist/lib/perspective-frustum.js +0 -190
  43. package/dist/lib/perspective-off-center-frustum.js +0 -270
  44. package/dist/lib/plane.js +0 -63
@@ -1,131 +0,0 @@
1
- // This file is derived from the Cesium math library under Apache 2 license
2
- // See LICENSE.md and https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md
3
- import { Matrix3, _MathUtils } from '@math.gl/core';
4
- const scratchMatrix = new Matrix3();
5
- const scratchUnitary = new Matrix3();
6
- const scratchDiagonal = new Matrix3();
7
- const jMatrix = new Matrix3();
8
- const jMatrixTranspose = new Matrix3();
9
- /**
10
- * Computes the eigenvectors and eigenvalues of a symmetric matrix.
11
- *
12
- * - Returns a diagonal matrix and unitary matrix such that:
13
- * `matrix = unitary matrix * diagonal matrix * transpose(unitary matrix)`
14
- * - The values along the diagonal of the diagonal matrix are the eigenvalues. The columns
15
- * of the unitary matrix are the corresponding eigenvectors.
16
- * - This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
17
- * section 8.4.3 The Classical Jacobi Algorithm
18
- *
19
- * @param matrix The 3x3 matrix to decompose into diagonal and unitary matrix. Expected to be symmetric.
20
- * @param result Optional object with unitary and diagonal properties which are matrices onto which to store the result.
21
- * @returns An object with unitary and diagonal properties which are the unitary and diagonal matrices, respectively.
22
- *
23
- * @example
24
- * const a = //... symmetric matrix
25
- * const result = {
26
- * unitary : new Matrix3(),
27
- * diagonal : new Matrix3()
28
- * };
29
- * computeEigenDecomposition(a, result);
30
- *
31
- * const unitaryTranspose = Matrix3.transpose(result.unitary, new Matrix3());
32
- * const b = Matrix3.multiply(result.unitary, result.diagonal, new Matrix3());
33
- * Matrix3.multiply(b, unitaryTranspose, b); // b is now equal to a
34
- *
35
- * const lambda = result.diagonal.getColumn(0, new Vector3()).x; // first eigenvalue
36
- * const v = result.unitary.getColumn(0, new Vector3()); // first eigenvector
37
- * const c = v.multiplyByScalar(lambda); // equal to v.transformByMatrix3(a)
38
- */
39
- export default function computeEigenDecomposition(matrix,
40
- // @ts-expect-error accept empty object type
41
- result = {}) {
42
- const EIGEN_TOLERANCE = _MathUtils.EPSILON20;
43
- const EIGEN_MAX_SWEEPS = 10;
44
- let count = 0;
45
- let sweep = 0;
46
- const unitaryMatrix = scratchUnitary;
47
- const diagonalMatrix = scratchDiagonal;
48
- unitaryMatrix.identity();
49
- diagonalMatrix.copy(matrix);
50
- const epsilon = EIGEN_TOLERANCE * computeFrobeniusNorm(diagonalMatrix);
51
- while (sweep < EIGEN_MAX_SWEEPS && offDiagonalFrobeniusNorm(diagonalMatrix) > epsilon) {
52
- shurDecomposition(diagonalMatrix, jMatrix);
53
- jMatrixTranspose.copy(jMatrix).transpose();
54
- diagonalMatrix.multiplyRight(jMatrix);
55
- diagonalMatrix.multiplyLeft(jMatrixTranspose);
56
- unitaryMatrix.multiplyRight(jMatrix);
57
- if (++count > 2) {
58
- ++sweep;
59
- count = 0;
60
- }
61
- }
62
- result.unitary = unitaryMatrix.toTarget(result.unitary);
63
- result.diagonal = diagonalMatrix.toTarget(result.diagonal);
64
- return result;
65
- }
66
- function computeFrobeniusNorm(matrix) {
67
- let norm = 0.0;
68
- for (let i = 0; i < 9; ++i) {
69
- const temp = matrix[i];
70
- norm += temp * temp;
71
- }
72
- return Math.sqrt(norm);
73
- }
74
- const rowVal = [1, 0, 0];
75
- const colVal = [2, 2, 1];
76
- // Computes the "off-diagonal" Frobenius norm.
77
- // Assumes matrix is symmetric.
78
- function offDiagonalFrobeniusNorm(matrix) {
79
- let norm = 0.0;
80
- for (let i = 0; i < 3; ++i) {
81
- const temp = matrix[scratchMatrix.getElementIndex(colVal[i], rowVal[i])];
82
- norm += 2.0 * temp * temp;
83
- }
84
- return Math.sqrt(norm);
85
- }
86
- // The routine takes a matrix, which is assumed to be symmetric, and
87
- // finds the largest off-diagonal term, and then creates
88
- // a matrix (result) which can be used to help reduce it
89
- //
90
- // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
91
- // section 8.4.2 The 2by2 Symmetric Schur Decomposition.
92
- //
93
- // eslint-disable-next-line max-statements
94
- function shurDecomposition(matrix, result) {
95
- const tolerance = _MathUtils.EPSILON15;
96
- let maxDiagonal = 0.0;
97
- let rotAxis = 1;
98
- // find pivot (rotAxis) based on max diagonal of matrix
99
- for (let i = 0; i < 3; ++i) {
100
- const temp = Math.abs(matrix[scratchMatrix.getElementIndex(colVal[i], rowVal[i])]);
101
- if (temp > maxDiagonal) {
102
- rotAxis = i;
103
- maxDiagonal = temp;
104
- }
105
- }
106
- const p = rowVal[rotAxis];
107
- const q = colVal[rotAxis];
108
- let c = 1.0;
109
- let s = 0.0;
110
- if (Math.abs(matrix[scratchMatrix.getElementIndex(q, p)]) > tolerance) {
111
- const qq = matrix[scratchMatrix.getElementIndex(q, q)];
112
- const pp = matrix[scratchMatrix.getElementIndex(p, p)];
113
- const qp = matrix[scratchMatrix.getElementIndex(q, p)];
114
- const tau = (qq - pp) / 2.0 / qp;
115
- let t;
116
- if (tau < 0.0) {
117
- t = -1.0 / (-tau + Math.sqrt(1.0 + tau * tau));
118
- }
119
- else {
120
- t = 1.0 / (tau + Math.sqrt(1.0 + tau * tau));
121
- }
122
- c = 1.0 / Math.sqrt(1.0 + t * t);
123
- s = t * c;
124
- }
125
- // Copy into result
126
- Matrix3.IDENTITY.to(result);
127
- result[scratchMatrix.getElementIndex(p, p)] = result[scratchMatrix.getElementIndex(q, q)] = c;
128
- result[scratchMatrix.getElementIndex(q, p)] = s;
129
- result[scratchMatrix.getElementIndex(p, q)] = -s;
130
- return result;
131
- }
@@ -1,111 +0,0 @@
1
- import { Vector3 } from '@math.gl/core';
2
- import { INTERSECTION } from '../../constants';
3
- const scratchVector = new Vector3();
4
- const scratchNormal = new Vector3();
5
- /**
6
- * An axis aligned bounding box - aligned with coordinate axes
7
- * @see BoundingVolume
8
- * @see BoundingRectangle
9
- * @see OrientedBoundingBox
10
- */
11
- export default class AxisAlignedBoundingBox {
12
- /**
13
- * Creates an instance of an AxisAlignedBoundingBox from the minimum and maximum points along the x, y, and z axes.
14
- * @param minimum=[0, 0, 0] The minimum point along the x, y, and z axes.
15
- * @param maximum=[0, 0, 0] The maximum point along the x, y, and z axes.
16
- * @param center The center of the box; automatically computed if not supplied.
17
- */
18
- constructor(minimum = [0, 0, 0], maximum = [0, 0, 0], center) {
19
- // If center was not defined, compute it.
20
- center = center || scratchVector.copy(minimum).add(maximum).scale(0.5);
21
- this.center = new Vector3(center);
22
- this.halfDiagonal = new Vector3(maximum).subtract(this.center);
23
- /**
24
- * The minimum point defining the bounding box.
25
- * @type {Vector3}
26
- * @default {@link 0, 0, 0}
27
- */
28
- this.minimum = new Vector3(minimum);
29
- /**
30
- * The maximum point defining the bounding box.
31
- * @type {Vector3}
32
- * @default {@link 0, 0, 0}
33
- */
34
- this.maximum = new Vector3(maximum);
35
- }
36
- /**
37
- * Duplicates a AxisAlignedBoundingBox instance.
38
- *
39
- * @returns {AxisAlignedBoundingBox} A new AxisAlignedBoundingBox instance.
40
- */
41
- clone() {
42
- return new AxisAlignedBoundingBox(this.minimum, this.maximum, this.center);
43
- }
44
- /**
45
- * Compares the provided AxisAlignedBoundingBox componentwise and returns
46
- * <code>true</code> if they are equal, <code>false</code> otherwise.
47
- *
48
- * @param {AxisAlignedBoundingBox} [right] The second AxisAlignedBoundingBox to compare with.
49
- * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
50
- */
51
- equals(right) {
52
- return (this === right ||
53
- (Boolean(right) && this.minimum.equals(right.minimum) && this.maximum.equals(right.maximum)));
54
- }
55
- /**
56
- * Applies a 4x4 affine transformation matrix to a bounding sphere.
57
- * @param transform The transformation matrix to apply to the bounding sphere.
58
- * @returns itself, i.e. the modified BoundingVolume.
59
- */
60
- transform(transform) {
61
- this.center.transformAsPoint(transform);
62
- // TODO - this.halfDiagonal.transformAsVector(transform);
63
- this.halfDiagonal.transform(transform);
64
- this.minimum.transform(transform);
65
- this.maximum.transform(transform);
66
- return this;
67
- }
68
- /**
69
- * Determines which side of a plane a box is located.
70
- */
71
- intersectPlane(plane) {
72
- const { halfDiagonal } = this;
73
- const normal = scratchNormal.from(plane.normal);
74
- const e = halfDiagonal.x * Math.abs(normal.x) +
75
- halfDiagonal.y * Math.abs(normal.y) +
76
- halfDiagonal.z * Math.abs(normal.z);
77
- const s = this.center.dot(normal) + plane.distance; // signed distance from center
78
- if (s - e > 0) {
79
- return INTERSECTION.INSIDE;
80
- }
81
- if (s + e < 0) {
82
- // Not in front because normals point inward
83
- return INTERSECTION.OUTSIDE;
84
- }
85
- return INTERSECTION.INTERSECTING;
86
- }
87
- /** Computes the estimated distance from the closest point on a bounding box to a point. */
88
- distanceTo(point) {
89
- return Math.sqrt(this.distanceSquaredTo(point));
90
- }
91
- /** Computes the estimated distance squared from the closest point on a bounding box to a point. */
92
- distanceSquaredTo(point) {
93
- const offset = scratchVector.from(point).subtract(this.center);
94
- const { halfDiagonal } = this;
95
- let distanceSquared = 0.0;
96
- let d;
97
- d = Math.abs(offset.x) - halfDiagonal.x;
98
- if (d > 0) {
99
- distanceSquared += d * d;
100
- }
101
- d = Math.abs(offset.y) - halfDiagonal.y;
102
- if (d > 0) {
103
- distanceSquared += d * d;
104
- }
105
- d = Math.abs(offset.z) - halfDiagonal.z;
106
- if (d > 0) {
107
- distanceSquared += d * d;
108
- }
109
- return distanceSquared;
110
- }
111
- }
@@ -1,118 +0,0 @@
1
- // This file is derived from the Cesium math library under Apache 2 license
2
- // See LICENSE.md and https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md
3
- import { Vector3 } from '@math.gl/core';
4
- import * as mat4 from 'gl-matrix/mat4';
5
- import { INTERSECTION } from '../../constants';
6
- const scratchVector = new Vector3();
7
- const scratchVector2 = new Vector3();
8
- /** A BoundingSphere */
9
- export default class BoundingSphere {
10
- /** Creates a bounding sphere */
11
- constructor(center = [0, 0, 0], radius = 0.0) {
12
- this.radius = -0;
13
- this.center = new Vector3();
14
- this.fromCenterRadius(center, radius);
15
- }
16
- /** Sets the bounding sphere from `center` and `radius`. */
17
- fromCenterRadius(center, radius) {
18
- this.center.from(center);
19
- this.radius = radius;
20
- return this;
21
- }
22
- /**
23
- * Computes a bounding sphere from the corner points of an axis-aligned bounding box. The sphere
24
- * tightly and fully encompasses the box.
25
- */
26
- fromCornerPoints(corner, oppositeCorner) {
27
- oppositeCorner = scratchVector.from(oppositeCorner);
28
- this.center = new Vector3().from(corner).add(oppositeCorner).scale(0.5);
29
- this.radius = this.center.distance(oppositeCorner);
30
- return this;
31
- }
32
- /** Compares the provided BoundingSphere component wise */
33
- equals(right) {
34
- return (this === right ||
35
- (Boolean(right) && this.center.equals(right.center) && this.radius === right.radius));
36
- }
37
- /** Duplicates a BoundingSphere instance. */
38
- clone() {
39
- return new BoundingSphere(this.center, this.radius);
40
- }
41
- /** Computes a bounding sphere that contains both the left and right bounding spheres. */
42
- union(boundingSphere) {
43
- const leftCenter = this.center;
44
- const leftRadius = this.radius;
45
- const rightCenter = boundingSphere.center;
46
- const rightRadius = boundingSphere.radius;
47
- const toRightCenter = scratchVector.copy(rightCenter).subtract(leftCenter);
48
- const centerSeparation = toRightCenter.magnitude();
49
- if (leftRadius >= centerSeparation + rightRadius) {
50
- // Left sphere wins.
51
- return this.clone();
52
- }
53
- if (rightRadius >= centerSeparation + leftRadius) {
54
- // Right sphere wins.
55
- return boundingSphere.clone();
56
- }
57
- // There are two tangent points, one on far side of each sphere.
58
- const halfDistanceBetweenTangentPoints = (leftRadius + centerSeparation + rightRadius) * 0.5;
59
- // Compute the center point halfway between the two tangent points.
60
- scratchVector2
61
- .copy(toRightCenter)
62
- .scale((-leftRadius + halfDistanceBetweenTangentPoints) / centerSeparation)
63
- .add(leftCenter);
64
- this.center.copy(scratchVector2);
65
- this.radius = halfDistanceBetweenTangentPoints;
66
- return this;
67
- }
68
- /** Computes a bounding sphere by enlarging the provided sphere to contain the provided point. */
69
- expand(point) {
70
- const scratchPoint = scratchVector.from(point);
71
- const radius = scratchPoint.subtract(this.center).magnitude();
72
- if (radius > this.radius) {
73
- this.radius = radius;
74
- }
75
- return this;
76
- }
77
- // BoundingVolume interface
78
- /**
79
- * Applies a 4x4 affine transformation matrix to a bounding sphere.
80
- * @param sphere The bounding sphere to apply the transformation to.
81
- * @param transform The transformation matrix to apply to the bounding sphere.
82
- * @returns self.
83
- */
84
- transform(transform) {
85
- this.center.transform(transform);
86
- const scale = mat4.getScaling(scratchVector, transform);
87
- this.radius = Math.max(scale[0], Math.max(scale[1], scale[2])) * this.radius;
88
- return this;
89
- }
90
- /** Computes the estimated distance squared from the closest point on a bounding sphere to a point. */
91
- distanceSquaredTo(point) {
92
- const d = this.distanceTo(point);
93
- return d * d;
94
- }
95
- /** Computes the estimated distance from the closest point on a bounding sphere to a point. */
96
- distanceTo(point) {
97
- const scratchPoint = scratchVector.from(point);
98
- const delta = scratchPoint.subtract(this.center);
99
- return Math.max(0, delta.len() - this.radius);
100
- }
101
- /** Determines which side of a plane a sphere is located. */
102
- intersectPlane(plane) {
103
- const center = this.center;
104
- const radius = this.radius;
105
- const normal = plane.normal;
106
- const distanceToPlane = normal.dot(center) + plane.distance;
107
- // The center point is negative side of the plane normal
108
- if (distanceToPlane < -radius) {
109
- return INTERSECTION.OUTSIDE;
110
- }
111
- // The center point is positive side of the plane, but radius extends beyond it; partial overlap
112
- if (distanceToPlane < radius) {
113
- return INTERSECTION.INTERSECTING;
114
- }
115
- // The center point and radius is positive side of the plane
116
- return INTERSECTION.INSIDE;
117
- }
118
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,256 +0,0 @@
1
- // This file is derived from the Cesium math library under Apache 2 license
2
- // See LICENSE.md and https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md
3
- import { Vector3, Matrix3, Quaternion } from '@math.gl/core';
4
- import BoundingSphere from './bounding-sphere';
5
- import { INTERSECTION } from '../../constants';
6
- const scratchVector3 = new Vector3();
7
- const scratchOffset = new Vector3();
8
- const scratchVectorU = new Vector3();
9
- const scratchVectorV = new Vector3();
10
- const scratchVectorW = new Vector3();
11
- const scratchCorner = new Vector3();
12
- const scratchToCenter = new Vector3();
13
- const MATRIX3 = {
14
- COLUMN0ROW0: 0,
15
- COLUMN0ROW1: 1,
16
- COLUMN0ROW2: 2,
17
- COLUMN1ROW0: 3,
18
- COLUMN1ROW1: 4,
19
- COLUMN1ROW2: 5,
20
- COLUMN2ROW0: 6,
21
- COLUMN2ROW1: 7,
22
- COLUMN2ROW2: 8
23
- };
24
- /**
25
- * An OrientedBoundingBox of some object is a closed and convex cuboid.
26
- * It can provide a tighter bounding volume than `BoundingSphere` or
27
- * `AxisAlignedBoundingBox` in many cases.
28
- */
29
- export default class OrientedBoundingBox {
30
- constructor(center = [0, 0, 0], halfAxes = [0, 0, 0, 0, 0, 0, 0, 0, 0]) {
31
- this.center = new Vector3().from(center);
32
- this.halfAxes = new Matrix3(halfAxes);
33
- }
34
- /** Returns an array with three halfSizes for the bounding box */
35
- get halfSize() {
36
- const xAxis = this.halfAxes.getColumn(0);
37
- const yAxis = this.halfAxes.getColumn(1);
38
- const zAxis = this.halfAxes.getColumn(2);
39
- return [new Vector3(xAxis).len(), new Vector3(yAxis).len(), new Vector3(zAxis).len()];
40
- }
41
- /** Returns a quaternion describing the orientation of the bounding box */
42
- get quaternion() {
43
- const xAxis = this.halfAxes.getColumn(0);
44
- const yAxis = this.halfAxes.getColumn(1);
45
- const zAxis = this.halfAxes.getColumn(2);
46
- const normXAxis = new Vector3(xAxis).normalize();
47
- const normYAxis = new Vector3(yAxis).normalize();
48
- const normZAxis = new Vector3(zAxis).normalize();
49
- return new Quaternion().fromMatrix3(new Matrix3([...normXAxis, ...normYAxis, ...normZAxis]));
50
- }
51
- /**
52
- * Create OrientedBoundingBox from quaternion based OBB,
53
- */
54
- fromCenterHalfSizeQuaternion(center, halfSize, quaternion) {
55
- const quaternionObject = new Quaternion(quaternion);
56
- const directionsMatrix = new Matrix3().fromQuaternion(quaternionObject);
57
- directionsMatrix[0] = directionsMatrix[0] * halfSize[0];
58
- directionsMatrix[1] = directionsMatrix[1] * halfSize[0];
59
- directionsMatrix[2] = directionsMatrix[2] * halfSize[0];
60
- directionsMatrix[3] = directionsMatrix[3] * halfSize[1];
61
- directionsMatrix[4] = directionsMatrix[4] * halfSize[1];
62
- directionsMatrix[5] = directionsMatrix[5] * halfSize[1];
63
- directionsMatrix[6] = directionsMatrix[6] * halfSize[2];
64
- directionsMatrix[7] = directionsMatrix[7] * halfSize[2];
65
- directionsMatrix[8] = directionsMatrix[8] * halfSize[2];
66
- this.center = new Vector3().from(center);
67
- this.halfAxes = directionsMatrix;
68
- return this;
69
- }
70
- /** Duplicates a OrientedBoundingBox instance. */
71
- clone() {
72
- return new OrientedBoundingBox(this.center, this.halfAxes);
73
- }
74
- /** Compares the provided OrientedBoundingBox component wise and returns */
75
- equals(right) {
76
- return (this === right ||
77
- (Boolean(right) && this.center.equals(right.center) && this.halfAxes.equals(right.halfAxes)));
78
- }
79
- /** Computes a tight-fitting bounding sphere enclosing the provided oriented bounding box. */
80
- getBoundingSphere(result = new BoundingSphere()) {
81
- const halfAxes = this.halfAxes;
82
- const u = halfAxes.getColumn(0, scratchVectorU);
83
- const v = halfAxes.getColumn(1, scratchVectorV);
84
- const w = halfAxes.getColumn(2, scratchVectorW);
85
- // Calculate "corner" vector
86
- const cornerVector = scratchVector3.copy(u).add(v).add(w);
87
- result.center.copy(this.center);
88
- result.radius = cornerVector.magnitude();
89
- return result;
90
- }
91
- /** Determines which side of a plane the oriented bounding box is located. */
92
- intersectPlane(plane) {
93
- const center = this.center;
94
- const normal = plane.normal;
95
- const halfAxes = this.halfAxes;
96
- const normalX = normal.x;
97
- const normalY = normal.y;
98
- const normalZ = normal.z;
99
- // Plane is used as if it is its normal; the first three components are assumed to be normalized
100
- const radEffective = Math.abs(normalX * halfAxes[MATRIX3.COLUMN0ROW0] +
101
- normalY * halfAxes[MATRIX3.COLUMN0ROW1] +
102
- normalZ * halfAxes[MATRIX3.COLUMN0ROW2]) +
103
- Math.abs(normalX * halfAxes[MATRIX3.COLUMN1ROW0] +
104
- normalY * halfAxes[MATRIX3.COLUMN1ROW1] +
105
- normalZ * halfAxes[MATRIX3.COLUMN1ROW2]) +
106
- Math.abs(normalX * halfAxes[MATRIX3.COLUMN2ROW0] +
107
- normalY * halfAxes[MATRIX3.COLUMN2ROW1] +
108
- normalZ * halfAxes[MATRIX3.COLUMN2ROW2]);
109
- const distanceToPlane = normal.dot(center) + plane.distance;
110
- if (distanceToPlane <= -radEffective) {
111
- // The entire box is on the negative side of the plane normal
112
- return INTERSECTION.OUTSIDE;
113
- }
114
- else if (distanceToPlane >= radEffective) {
115
- // The entire box is on the positive side of the plane normal
116
- return INTERSECTION.INSIDE;
117
- }
118
- return INTERSECTION.INTERSECTING;
119
- }
120
- /** Computes the estimated distance from the closest point on a bounding box to a point. */
121
- distanceTo(point) {
122
- return Math.sqrt(this.distanceSquaredTo(point));
123
- }
124
- /**
125
- * Computes the estimated distance squared from the closest point
126
- * on a bounding box to a point.
127
- * See Geometric Tools for Computer Graphics 10.4.2
128
- */
129
- distanceSquaredTo(point) {
130
- // Computes the estimated distance squared from the
131
- // closest point on a bounding box to a point.
132
- // See Geometric Tools for Computer Graphics 10.4.2
133
- const offset = scratchOffset.from(point).subtract(this.center);
134
- const halfAxes = this.halfAxes;
135
- const u = halfAxes.getColumn(0, scratchVectorU);
136
- const v = halfAxes.getColumn(1, scratchVectorV);
137
- const w = halfAxes.getColumn(2, scratchVectorW);
138
- const uHalf = u.magnitude();
139
- const vHalf = v.magnitude();
140
- const wHalf = w.magnitude();
141
- u.normalize();
142
- v.normalize();
143
- w.normalize();
144
- let distanceSquared = 0.0;
145
- let d;
146
- d = Math.abs(offset.dot(u)) - uHalf;
147
- if (d > 0) {
148
- distanceSquared += d * d;
149
- }
150
- d = Math.abs(offset.dot(v)) - vHalf;
151
- if (d > 0) {
152
- distanceSquared += d * d;
153
- }
154
- d = Math.abs(offset.dot(w)) - wHalf;
155
- if (d > 0) {
156
- distanceSquared += d * d;
157
- }
158
- return distanceSquared;
159
- }
160
- /**
161
- * The distances calculated by the vector from the center of the bounding box
162
- * to position projected onto direction.
163
- *
164
- * - If you imagine the infinite number of planes with normal direction,
165
- * this computes the smallest distance to the closest and farthest planes
166
- * from `position` that intersect the bounding box.
167
- *
168
- * @param position The position to calculate the distance from.
169
- * @param direction The direction from position.
170
- * @param result An Interval (array of length 2) to store the nearest and farthest distances.
171
- * @returns Interval (array of length 2) with nearest and farthest distances
172
- * on the bounding box from position in direction.
173
- */
174
- // eslint-disable-next-line max-statements
175
- computePlaneDistances(position, direction, result = [-0, -0]) {
176
- let minDist = Number.POSITIVE_INFINITY;
177
- let maxDist = Number.NEGATIVE_INFINITY;
178
- const center = this.center;
179
- const halfAxes = this.halfAxes;
180
- const u = halfAxes.getColumn(0, scratchVectorU);
181
- const v = halfAxes.getColumn(1, scratchVectorV);
182
- const w = halfAxes.getColumn(2, scratchVectorW);
183
- // project first corner
184
- const corner = scratchCorner.copy(u).add(v).add(w).add(center);
185
- const toCenter = scratchToCenter.copy(corner).subtract(position);
186
- let mag = direction.dot(toCenter);
187
- minDist = Math.min(mag, minDist);
188
- maxDist = Math.max(mag, maxDist);
189
- // project second corner
190
- corner.copy(center).add(u).add(v).subtract(w);
191
- toCenter.copy(corner).subtract(position);
192
- mag = direction.dot(toCenter);
193
- minDist = Math.min(mag, minDist);
194
- maxDist = Math.max(mag, maxDist);
195
- // project third corner
196
- corner.copy(center).add(u).subtract(v).add(w);
197
- toCenter.copy(corner).subtract(position);
198
- mag = direction.dot(toCenter);
199
- minDist = Math.min(mag, minDist);
200
- maxDist = Math.max(mag, maxDist);
201
- // project fourth corner
202
- corner.copy(center).add(u).subtract(v).subtract(w);
203
- toCenter.copy(corner).subtract(position);
204
- mag = direction.dot(toCenter);
205
- minDist = Math.min(mag, minDist);
206
- maxDist = Math.max(mag, maxDist);
207
- // project fifth corner
208
- center.copy(corner).subtract(u).add(v).add(w);
209
- toCenter.copy(corner).subtract(position);
210
- mag = direction.dot(toCenter);
211
- minDist = Math.min(mag, minDist);
212
- maxDist = Math.max(mag, maxDist);
213
- // project sixth corner
214
- center.copy(corner).subtract(u).add(v).subtract(w);
215
- toCenter.copy(corner).subtract(position);
216
- mag = direction.dot(toCenter);
217
- minDist = Math.min(mag, minDist);
218
- maxDist = Math.max(mag, maxDist);
219
- // project seventh corner
220
- center.copy(corner).subtract(u).subtract(v).add(w);
221
- toCenter.copy(corner).subtract(position);
222
- mag = direction.dot(toCenter);
223
- minDist = Math.min(mag, minDist);
224
- maxDist = Math.max(mag, maxDist);
225
- // project eighth corner
226
- center.copy(corner).subtract(u).subtract(v).subtract(w);
227
- toCenter.copy(corner).subtract(position);
228
- mag = direction.dot(toCenter);
229
- minDist = Math.min(mag, minDist);
230
- maxDist = Math.max(mag, maxDist);
231
- result[0] = minDist;
232
- result[1] = maxDist;
233
- return result;
234
- }
235
- /**
236
- * Applies a 4x4 affine transformation matrix to a bounding sphere.
237
- * @param transform The transformation matrix to apply to the bounding sphere.
238
- * @returns itself, i.e. the modified BoundingVolume.
239
- */
240
- transform(transformation) {
241
- this.center.transformAsPoint(transformation);
242
- const xAxis = this.halfAxes.getColumn(0, scratchVectorU);
243
- xAxis.transformAsPoint(transformation);
244
- const yAxis = this.halfAxes.getColumn(1, scratchVectorV);
245
- yAxis.transformAsPoint(transformation);
246
- const zAxis = this.halfAxes.getColumn(2, scratchVectorW);
247
- zAxis.transformAsPoint(transformation);
248
- this.halfAxes = new Matrix3([...xAxis, ...yAxis, ...zAxis]);
249
- return this;
250
- }
251
- getTransform() {
252
- // const modelMatrix = Matrix4.fromRotationTranslation(this.boundingVolume.halfAxes, this.boundingVolume.center);
253
- // return modelMatrix;
254
- throw new Error('not implemented');
255
- }
256
- }