@math.gl/culling 4.0.0 → 4.0.1
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.
- package/dist/constants.d.ts +0 -1
- package/dist/constants.js +5 -4
- package/dist/index.cjs +27 -45
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +11 -12
- package/dist/index.js +2 -1
- package/dist/lib/algorithms/bounding-box-from-points.d.ts +2 -3
- package/dist/lib/algorithms/bounding-box-from-points.js +113 -113
- package/dist/lib/algorithms/bounding-sphere-from-points.d.ts +1 -2
- package/dist/lib/algorithms/bounding-sphere-from-points.js +117 -103
- package/dist/lib/algorithms/compute-eigen-decomposition.d.ts +1 -2
- package/dist/lib/algorithms/compute-eigen-decomposition.js +113 -85
- package/dist/lib/bounding-volumes/axis-aligned-bounding-box.d.ts +2 -3
- package/dist/lib/bounding-volumes/axis-aligned-bounding-box.js +100 -80
- package/dist/lib/bounding-volumes/bounding-sphere.d.ts +2 -3
- package/dist/lib/bounding-volumes/bounding-sphere.js +106 -97
- package/dist/lib/bounding-volumes/bounding-volume.d.ts +1 -2
- package/dist/lib/bounding-volumes/bounding-volume.js +0 -1
- package/dist/lib/bounding-volumes/oriented-bounding-box.d.ts +3 -4
- package/dist/lib/bounding-volumes/oriented-bounding-box.js +238 -196
- package/dist/lib/culling-volume.d.ts +3 -4
- package/dist/lib/culling-volume.js +109 -90
- package/dist/lib/perspective-frustum.d.ts +2 -3
- package/dist/lib/perspective-frustum.js +184 -136
- package/dist/lib/perspective-off-center-frustum.d.ts +2 -3
- package/dist/lib/perspective-off-center-frustum.js +259 -158
- package/dist/lib/plane.d.ts +0 -1
- package/dist/lib/plane.js +58 -59
- package/package.json +5 -6
- package/dist/constants.d.ts.map +0 -1
- package/dist/constants.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/lib/algorithms/bounding-box-from-points.d.ts.map +0 -1
- package/dist/lib/algorithms/bounding-box-from-points.js.map +0 -1
- package/dist/lib/algorithms/bounding-sphere-from-points.d.ts.map +0 -1
- package/dist/lib/algorithms/bounding-sphere-from-points.js.map +0 -1
- package/dist/lib/algorithms/compute-eigen-decomposition.d.ts.map +0 -1
- package/dist/lib/algorithms/compute-eigen-decomposition.js.map +0 -1
- package/dist/lib/bounding-volumes/axis-aligned-bounding-box.d.ts.map +0 -1
- package/dist/lib/bounding-volumes/axis-aligned-bounding-box.js.map +0 -1
- package/dist/lib/bounding-volumes/bounding-sphere.d.ts.map +0 -1
- package/dist/lib/bounding-volumes/bounding-sphere.js.map +0 -1
- package/dist/lib/bounding-volumes/bounding-volume.d.ts.map +0 -1
- package/dist/lib/bounding-volumes/bounding-volume.js.map +0 -1
- package/dist/lib/bounding-volumes/oriented-bounding-box.d.ts.map +0 -1
- package/dist/lib/bounding-volumes/oriented-bounding-box.js.map +0 -1
- package/dist/lib/culling-volume.d.ts.map +0 -1
- package/dist/lib/culling-volume.js.map +0 -1
- package/dist/lib/perspective-frustum.d.ts.map +0 -1
- package/dist/lib/perspective-frustum.js.map +0 -1
- package/dist/lib/perspective-off-center-frustum.d.ts.map +0 -1
- package/dist/lib/perspective-off-center-frustum.js.map +0 -1
- package/dist/lib/plane.d.ts.map +0 -1
- package/dist/lib/plane.js.map +0 -1
|
@@ -1,103 +1,131 @@
|
|
|
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
|
|
1
3
|
import { Matrix3, _MathUtils } from '@math.gl/core';
|
|
2
4
|
const scratchMatrix = new Matrix3();
|
|
3
5
|
const scratchUnitary = new Matrix3();
|
|
4
6
|
const scratchDiagonal = new Matrix3();
|
|
5
7
|
const jMatrix = new Matrix3();
|
|
6
8
|
const jMatrixTranspose = new Matrix3();
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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 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
|
+
}
|
|
28
61
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
result.diagonal = diagonalMatrix.toTarget(result.diagonal);
|
|
33
|
-
return result;
|
|
62
|
+
result.unitary = unitaryMatrix.toTarget(result.unitary);
|
|
63
|
+
result.diagonal = diagonalMatrix.toTarget(result.diagonal);
|
|
64
|
+
return result;
|
|
34
65
|
}
|
|
35
|
-
|
|
36
66
|
function computeFrobeniusNorm(matrix) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return Math.sqrt(norm);
|
|
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);
|
|
45
73
|
}
|
|
46
|
-
|
|
47
74
|
const rowVal = [1, 0, 0];
|
|
48
75
|
const colVal = [2, 2, 1];
|
|
49
|
-
|
|
76
|
+
// Computes the "off-diagonal" Frobenius norm.
|
|
77
|
+
// Assumes matrix is symmetric.
|
|
50
78
|
function offDiagonalFrobeniusNorm(matrix) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return Math.sqrt(norm);
|
|
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);
|
|
59
85
|
}
|
|
60
|
-
|
|
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
|
|
61
94
|
function shurDecomposition(matrix, result) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
+
}
|
|
72
105
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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;
|
|
91
124
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
result[scratchMatrix.getElementIndex(p, p)] = result[scratchMatrix.getElementIndex(q, q)] = c;
|
|
99
|
-
result[scratchMatrix.getElementIndex(q, p)] = s;
|
|
100
|
-
result[scratchMatrix.getElementIndex(p, q)] = -s;
|
|
101
|
-
return result;
|
|
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;
|
|
102
131
|
}
|
|
103
|
-
//# sourceMappingURL=compute-eigen-decomposition.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { BoundingVolume } from
|
|
1
|
+
import { BoundingVolume } from "./bounding-volume.js";
|
|
2
2
|
import { Vector3 } from '@math.gl/core';
|
|
3
|
-
import { Plane } from
|
|
3
|
+
import { Plane } from "../plane.js";
|
|
4
4
|
/**
|
|
5
5
|
* An axis aligned bounding box - aligned with coordinate axes
|
|
6
6
|
* @see BoundingVolume
|
|
@@ -52,4 +52,3 @@ export declare class AxisAlignedBoundingBox implements BoundingVolume {
|
|
|
52
52
|
/** Computes the estimated distance squared from the closest point on a bounding box to a point. */
|
|
53
53
|
distanceSquaredTo(point: readonly number[]): number;
|
|
54
54
|
}
|
|
55
|
-
//# sourceMappingURL=axis-aligned-bounding-box.d.ts.map
|
|
@@ -1,91 +1,111 @@
|
|
|
1
|
-
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
2
1
|
import { Vector3 } from '@math.gl/core';
|
|
3
2
|
import { INTERSECTION } from "../../constants.js";
|
|
4
3
|
const scratchVector = new Vector3();
|
|
5
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
|
+
*/
|
|
6
11
|
export class AxisAlignedBoundingBox {
|
|
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
|
-
transform(transform) {
|
|
32
|
-
this.center.transformAsPoint(transform);
|
|
33
|
-
this.halfDiagonal.transform(transform);
|
|
34
|
-
this.minimum.transform(transform);
|
|
35
|
-
this.maximum.transform(transform);
|
|
36
|
-
return this;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
intersectPlane(plane) {
|
|
40
|
-
const {
|
|
41
|
-
halfDiagonal
|
|
42
|
-
} = this;
|
|
43
|
-
const normal = scratchNormal.from(plane.normal);
|
|
44
|
-
const e = halfDiagonal.x * Math.abs(normal.x) + halfDiagonal.y * Math.abs(normal.y) + halfDiagonal.z * Math.abs(normal.z);
|
|
45
|
-
const s = this.center.dot(normal) + plane.distance;
|
|
46
|
-
|
|
47
|
-
if (s - e > 0) {
|
|
48
|
-
return INTERSECTION.INSIDE;
|
|
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);
|
|
49
35
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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);
|
|
53
43
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const {
|
|
65
|
-
halfDiagonal
|
|
66
|
-
} = this;
|
|
67
|
-
let distanceSquared = 0.0;
|
|
68
|
-
let d;
|
|
69
|
-
d = Math.abs(offset.x) - halfDiagonal.x;
|
|
70
|
-
|
|
71
|
-
if (d > 0) {
|
|
72
|
-
distanceSquared += d * d;
|
|
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)));
|
|
73
54
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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;
|
|
79
67
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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;
|
|
85
110
|
}
|
|
86
|
-
|
|
87
|
-
return distanceSquared;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
111
|
}
|
|
91
|
-
//# sourceMappingURL=axis-aligned-bounding-box.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { NumericArray, Vector3 } from '@math.gl/core';
|
|
2
|
-
import { BoundingVolume } from
|
|
3
|
-
import { Plane } from
|
|
2
|
+
import { BoundingVolume } from "./bounding-volume.js";
|
|
3
|
+
import { Plane } from "../plane.js";
|
|
4
4
|
/** A BoundingSphere */
|
|
5
5
|
export declare class BoundingSphere implements BoundingVolume {
|
|
6
6
|
center: Vector3;
|
|
@@ -36,4 +36,3 @@ export declare class BoundingSphere implements BoundingVolume {
|
|
|
36
36
|
/** Determines which side of a plane a sphere is located. */
|
|
37
37
|
intersectPlane(plane: Plane): number;
|
|
38
38
|
}
|
|
39
|
-
//# sourceMappingURL=bounding-sphere.d.ts.map
|
|
@@ -1,108 +1,117 @@
|
|
|
1
|
-
|
|
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
|
|
2
3
|
import { Vector3, mat4 } from '@math.gl/core';
|
|
3
4
|
import { INTERSECTION } from "../../constants.js";
|
|
4
5
|
const scratchVector = new Vector3();
|
|
5
6
|
const scratchVector2 = new Vector3();
|
|
7
|
+
/** A BoundingSphere */
|
|
6
8
|
export class BoundingSphere {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
this.radius = -0;
|
|
13
|
-
this.center = new Vector3();
|
|
14
|
-
this.fromCenterRadius(center, radius);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
fromCenterRadius(center, radius) {
|
|
18
|
-
this.center.from(center);
|
|
19
|
-
this.radius = radius;
|
|
20
|
-
return this;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
fromCornerPoints(corner, oppositeCorner) {
|
|
24
|
-
oppositeCorner = scratchVector.from(oppositeCorner);
|
|
25
|
-
this.center = new Vector3().from(corner).add(oppositeCorner).scale(0.5);
|
|
26
|
-
this.radius = this.center.distance(oppositeCorner);
|
|
27
|
-
return this;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
equals(right) {
|
|
31
|
-
return this === right || Boolean(right) && this.center.equals(right.center) && this.radius === right.radius;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
clone() {
|
|
35
|
-
return new BoundingSphere(this.center, this.radius);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
union(boundingSphere) {
|
|
39
|
-
const leftCenter = this.center;
|
|
40
|
-
const leftRadius = this.radius;
|
|
41
|
-
const rightCenter = boundingSphere.center;
|
|
42
|
-
const rightRadius = boundingSphere.radius;
|
|
43
|
-
const toRightCenter = scratchVector.copy(rightCenter).subtract(leftCenter);
|
|
44
|
-
const centerSeparation = toRightCenter.magnitude();
|
|
45
|
-
|
|
46
|
-
if (leftRadius >= centerSeparation + rightRadius) {
|
|
47
|
-
return this.clone();
|
|
9
|
+
/** Creates a bounding sphere */
|
|
10
|
+
constructor(center = [0, 0, 0], radius = 0.0) {
|
|
11
|
+
this.radius = -0;
|
|
12
|
+
this.center = new Vector3();
|
|
13
|
+
this.fromCenterRadius(center, radius);
|
|
48
14
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
15
|
+
/** Sets the bounding sphere from `center` and `radius`. */
|
|
16
|
+
fromCenterRadius(center, radius) {
|
|
17
|
+
this.center.from(center);
|
|
18
|
+
this.radius = radius;
|
|
19
|
+
return this;
|
|
52
20
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const scratchPoint = scratchVector.from(point);
|
|
63
|
-
const radius = scratchPoint.subtract(this.center).magnitude();
|
|
64
|
-
|
|
65
|
-
if (radius > this.radius) {
|
|
66
|
-
this.radius = radius;
|
|
21
|
+
/**
|
|
22
|
+
* Computes a bounding sphere from the corner points of an axis-aligned bounding box. The sphere
|
|
23
|
+
* tightly and fully encompasses the box.
|
|
24
|
+
*/
|
|
25
|
+
fromCornerPoints(corner, oppositeCorner) {
|
|
26
|
+
oppositeCorner = scratchVector.from(oppositeCorner);
|
|
27
|
+
this.center = new Vector3().from(corner).add(oppositeCorner).scale(0.5);
|
|
28
|
+
this.radius = this.center.distance(oppositeCorner);
|
|
29
|
+
return this;
|
|
67
30
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
transform(transform) {
|
|
73
|
-
this.center.transform(transform);
|
|
74
|
-
const scale = mat4.getScaling(scratchVector, transform);
|
|
75
|
-
this.radius = Math.max(scale[0], Math.max(scale[1], scale[2])) * this.radius;
|
|
76
|
-
return this;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
distanceSquaredTo(point) {
|
|
80
|
-
const d = this.distanceTo(point);
|
|
81
|
-
return d * d;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
distanceTo(point) {
|
|
85
|
-
const scratchPoint = scratchVector.from(point);
|
|
86
|
-
const delta = scratchPoint.subtract(this.center);
|
|
87
|
-
return Math.max(0, delta.len() - this.radius);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
intersectPlane(plane) {
|
|
91
|
-
const center = this.center;
|
|
92
|
-
const radius = this.radius;
|
|
93
|
-
const normal = plane.normal;
|
|
94
|
-
const distanceToPlane = normal.dot(center) + plane.distance;
|
|
95
|
-
|
|
96
|
-
if (distanceToPlane < -radius) {
|
|
97
|
-
return INTERSECTION.OUTSIDE;
|
|
31
|
+
/** Compares the provided BoundingSphere component wise */
|
|
32
|
+
equals(right) {
|
|
33
|
+
return (this === right ||
|
|
34
|
+
(Boolean(right) && this.center.equals(right.center) && this.radius === right.radius));
|
|
98
35
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
36
|
+
/** Duplicates a BoundingSphere instance. */
|
|
37
|
+
clone() {
|
|
38
|
+
return new BoundingSphere(this.center, this.radius);
|
|
39
|
+
}
|
|
40
|
+
/** Computes a bounding sphere that contains both the left and right bounding spheres. */
|
|
41
|
+
union(boundingSphere) {
|
|
42
|
+
const leftCenter = this.center;
|
|
43
|
+
const leftRadius = this.radius;
|
|
44
|
+
const rightCenter = boundingSphere.center;
|
|
45
|
+
const rightRadius = boundingSphere.radius;
|
|
46
|
+
const toRightCenter = scratchVector.copy(rightCenter).subtract(leftCenter);
|
|
47
|
+
const centerSeparation = toRightCenter.magnitude();
|
|
48
|
+
if (leftRadius >= centerSeparation + rightRadius) {
|
|
49
|
+
// Left sphere wins.
|
|
50
|
+
return this.clone();
|
|
51
|
+
}
|
|
52
|
+
if (rightRadius >= centerSeparation + leftRadius) {
|
|
53
|
+
// Right sphere wins.
|
|
54
|
+
return boundingSphere.clone();
|
|
55
|
+
}
|
|
56
|
+
// There are two tangent points, one on far side of each sphere.
|
|
57
|
+
const halfDistanceBetweenTangentPoints = (leftRadius + centerSeparation + rightRadius) * 0.5;
|
|
58
|
+
// Compute the center point halfway between the two tangent points.
|
|
59
|
+
scratchVector2
|
|
60
|
+
.copy(toRightCenter)
|
|
61
|
+
.scale((-leftRadius + halfDistanceBetweenTangentPoints) / centerSeparation)
|
|
62
|
+
.add(leftCenter);
|
|
63
|
+
this.center.copy(scratchVector2);
|
|
64
|
+
this.radius = halfDistanceBetweenTangentPoints;
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
/** Computes a bounding sphere by enlarging the provided sphere to contain the provided point. */
|
|
68
|
+
expand(point) {
|
|
69
|
+
const scratchPoint = scratchVector.from(point);
|
|
70
|
+
const radius = scratchPoint.subtract(this.center).magnitude();
|
|
71
|
+
if (radius > this.radius) {
|
|
72
|
+
this.radius = radius;
|
|
73
|
+
}
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
// BoundingVolume interface
|
|
77
|
+
/**
|
|
78
|
+
* Applies a 4x4 affine transformation matrix to a bounding sphere.
|
|
79
|
+
* @param sphere The bounding sphere to apply the transformation to.
|
|
80
|
+
* @param transform The transformation matrix to apply to the bounding sphere.
|
|
81
|
+
* @returns self.
|
|
82
|
+
*/
|
|
83
|
+
transform(transform) {
|
|
84
|
+
this.center.transform(transform);
|
|
85
|
+
const scale = mat4.getScaling(scratchVector, transform);
|
|
86
|
+
this.radius = Math.max(scale[0], Math.max(scale[1], scale[2])) * this.radius;
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
/** Computes the estimated distance squared from the closest point on a bounding sphere to a point. */
|
|
90
|
+
distanceSquaredTo(point) {
|
|
91
|
+
const d = this.distanceTo(point);
|
|
92
|
+
return d * d;
|
|
93
|
+
}
|
|
94
|
+
/** Computes the estimated distance from the closest point on a bounding sphere to a point. */
|
|
95
|
+
distanceTo(point) {
|
|
96
|
+
const scratchPoint = scratchVector.from(point);
|
|
97
|
+
const delta = scratchPoint.subtract(this.center);
|
|
98
|
+
return Math.max(0, delta.len() - this.radius);
|
|
99
|
+
}
|
|
100
|
+
/** Determines which side of a plane a sphere is located. */
|
|
101
|
+
intersectPlane(plane) {
|
|
102
|
+
const center = this.center;
|
|
103
|
+
const radius = this.radius;
|
|
104
|
+
const normal = plane.normal;
|
|
105
|
+
const distanceToPlane = normal.dot(center) + plane.distance;
|
|
106
|
+
// The center point is negative side of the plane normal
|
|
107
|
+
if (distanceToPlane < -radius) {
|
|
108
|
+
return INTERSECTION.OUTSIDE;
|
|
109
|
+
}
|
|
110
|
+
// The center point is positive side of the plane, but radius extends beyond it; partial overlap
|
|
111
|
+
if (distanceToPlane < radius) {
|
|
112
|
+
return INTERSECTION.INTERSECTING;
|
|
113
|
+
}
|
|
114
|
+
// The center point and radius is positive side of the plane
|
|
115
|
+
return INTERSECTION.INSIDE;
|
|
102
116
|
}
|
|
103
|
-
|
|
104
|
-
return INTERSECTION.INSIDE;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
117
|
}
|
|
108
|
-
//# sourceMappingURL=bounding-sphere.js.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Plane } from
|
|
1
|
+
import { Plane } from "../plane.js";
|
|
2
2
|
/**
|
|
3
3
|
* Common interface for BoundingVolumes
|
|
4
4
|
* including BoundingSphere, AxisAlignedBoundingBox and OrientedBoundingBox
|
|
@@ -26,4 +26,3 @@ export interface BoundingVolume {
|
|
|
26
26
|
*/
|
|
27
27
|
intersectPlane(plane: Plane): number;
|
|
28
28
|
}
|
|
29
|
-
//# sourceMappingURL=bounding-volume.d.ts.map
|