@math.gl/culling 3.5.6 → 3.6.0-alpha.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 +6 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +8 -0
- package/dist/es5/constants.js +7 -5
- package/dist/es5/constants.js.map +1 -1
- package/dist/es5/index.js +12 -24
- package/dist/es5/index.js.map +1 -1
- package/dist/es5/lib/algorithms/bounding-box-from-points.js +72 -123
- package/dist/es5/lib/algorithms/bounding-box-from-points.js.map +1 -1
- package/dist/es5/lib/algorithms/bounding-sphere-from-points.js +81 -109
- package/dist/es5/lib/algorithms/bounding-sphere-from-points.js.map +1 -1
- package/dist/es5/lib/algorithms/compute-eigen-decomposition.js +36 -37
- package/dist/es5/lib/algorithms/compute-eigen-decomposition.js.map +1 -1
- package/dist/es5/lib/bounding-volumes/axis-aligned-bounding-box.js +68 -74
- package/dist/es5/lib/bounding-volumes/axis-aligned-bounding-box.js.map +1 -1
- package/dist/es5/lib/bounding-volumes/bounding-sphere.js +93 -110
- package/dist/es5/lib/bounding-volumes/bounding-sphere.js.map +1 -1
- package/dist/es5/lib/bounding-volumes/bounding-volume.js +2 -0
- package/dist/es5/lib/bounding-volumes/bounding-volume.js.map +1 -0
- package/dist/es5/lib/bounding-volumes/oriented-bounding-box.js +190 -211
- package/dist/es5/lib/bounding-volumes/oriented-bounding-box.js.map +1 -1
- package/dist/es5/lib/culling-volume.js +80 -131
- package/dist/es5/lib/culling-volume.js.map +1 -1
- package/dist/es5/lib/perspective-frustum.js +73 -84
- package/dist/es5/lib/perspective-frustum.js.map +1 -1
- package/dist/es5/lib/perspective-off-center-frustum.js +114 -117
- package/dist/es5/lib/perspective-off-center-frustum.js.map +1 -1
- package/dist/es5/lib/plane.js +55 -69
- package/dist/es5/lib/plane.js.map +1 -1
- package/dist/esm/constants.js +7 -5
- package/dist/esm/constants.js.map +1 -1
- package/dist/esm/index.js +0 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/lib/algorithms/bounding-box-from-points.js.map +1 -1
- package/dist/esm/lib/algorithms/bounding-sphere-from-points.js.map +1 -1
- package/dist/esm/lib/algorithms/compute-eigen-decomposition.js.map +1 -1
- package/dist/esm/lib/bounding-volumes/axis-aligned-bounding-box.js +15 -6
- package/dist/esm/lib/bounding-volumes/axis-aligned-bounding-box.js.map +1 -1
- package/dist/esm/lib/bounding-volumes/bounding-sphere.js +9 -4
- package/dist/esm/lib/bounding-volumes/bounding-sphere.js.map +1 -1
- package/dist/esm/lib/bounding-volumes/bounding-volume.js +2 -0
- package/dist/esm/lib/bounding-volumes/bounding-volume.js.map +1 -0
- package/dist/esm/lib/bounding-volumes/oriented-bounding-box.js +5 -0
- package/dist/esm/lib/bounding-volumes/oriented-bounding-box.js.map +1 -1
- package/dist/esm/lib/culling-volume.js +10 -16
- package/dist/esm/lib/culling-volume.js.map +1 -1
- package/dist/esm/lib/perspective-frustum.js +31 -5
- package/dist/esm/lib/perspective-frustum.js.map +1 -1
- package/dist/esm/lib/perspective-off-center-frustum.js +31 -3
- package/dist/esm/lib/perspective-off-center-frustum.js.map +1 -1
- package/dist/esm/lib/plane.js +6 -1
- package/dist/esm/lib/plane.js.map +1 -1
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/{src → dist}/lib/algorithms/bounding-box-from-points.d.ts +3 -10
- package/dist/lib/algorithms/bounding-box-from-points.d.ts.map +1 -0
- package/dist/lib/algorithms/bounding-box-from-points.js +131 -0
- package/dist/{esm/lib → lib}/algorithms/bounding-sphere-from-points.d.ts +2 -5
- package/dist/lib/algorithms/bounding-sphere-from-points.d.ts.map +1 -0
- package/dist/lib/algorithms/bounding-sphere-from-points.js +142 -0
- package/dist/{es5/lib → lib}/algorithms/compute-eigen-decomposition.d.ts +7 -9
- package/dist/lib/algorithms/compute-eigen-decomposition.d.ts.map +1 -0
- package/dist/lib/algorithms/compute-eigen-decomposition.js +131 -0
- package/dist/lib/bounding-volumes/axis-aligned-bounding-box.d.ts +56 -0
- package/dist/lib/bounding-volumes/axis-aligned-bounding-box.d.ts.map +1 -0
- package/dist/lib/bounding-volumes/axis-aligned-bounding-box.js +111 -0
- package/dist/lib/bounding-volumes/bounding-sphere.d.ts +40 -0
- package/dist/lib/bounding-volumes/bounding-sphere.d.ts.map +1 -0
- package/dist/lib/bounding-volumes/bounding-sphere.js +118 -0
- package/dist/lib/bounding-volumes/bounding-volume.d.ts +30 -0
- package/dist/lib/bounding-volumes/bounding-volume.d.ts.map +1 -0
- package/dist/lib/bounding-volumes/bounding-volume.js +1 -0
- package/dist/lib/bounding-volumes/oriented-bounding-box.d.ts +67 -0
- package/dist/lib/bounding-volumes/oriented-bounding-box.d.ts.map +1 -0
- package/dist/lib/bounding-volumes/oriented-bounding-box.js +256 -0
- package/dist/lib/culling-volume.d.ts +46 -0
- package/dist/lib/culling-volume.d.ts.map +1 -0
- package/dist/lib/culling-volume.js +121 -0
- package/dist/lib/perspective-frustum.d.ts +140 -0
- package/dist/lib/perspective-frustum.d.ts.map +1 -0
- package/dist/lib/perspective-frustum.js +205 -0
- package/dist/lib/perspective-off-center-frustum.d.ts +122 -0
- package/dist/lib/perspective-off-center-frustum.d.ts.map +1 -0
- package/dist/lib/perspective-off-center-frustum.js +305 -0
- package/dist/lib/plane.d.ts +26 -0
- package/dist/lib/plane.d.ts.map +1 -0
- package/dist/lib/plane.js +63 -0
- package/package.json +4 -4
- package/src/constants.ts +8 -0
- package/src/{index.js → index.ts} +0 -4
- package/src/lib/algorithms/{bounding-box-from-points.js → bounding-box-from-points.ts} +17 -4
- package/src/lib/algorithms/{bounding-sphere-from-points.js → bounding-sphere-from-points.ts} +15 -1
- package/src/lib/algorithms/{compute-eigen-decomposition.js → compute-eigen-decomposition.ts} +40 -1
- package/src/lib/bounding-volumes/{axis-aligned-bounding-box.js → axis-aligned-bounding-box.ts} +45 -14
- package/src/lib/bounding-volumes/{bounding-sphere.js → bounding-sphere.ts} +44 -29
- package/src/lib/bounding-volumes/{bounding-volume.d.ts → bounding-volume.ts} +7 -7
- package/src/lib/bounding-volumes/{oriented-bounding-box.js → oriented-bounding-box.ts} +72 -14
- package/src/lib/{culling-volume.js → culling-volume.ts} +42 -34
- package/src/lib/{perspective-frustum.js → perspective-frustum.ts} +48 -66
- package/src/lib/{perspective-off-center-frustum.js → perspective-off-center-frustum.ts} +24 -14
- package/src/lib/{plane.js → plane.ts} +24 -17
- package/dist/es5/constants.d.ts +0 -6
- package/dist/es5/index.d.ts +0 -20
- package/dist/es5/lib/algorithms/bounding-box-from-points.d.ts +0 -22
- package/dist/es5/lib/algorithms/bounding-sphere-from-points.d.ts +0 -17
- package/dist/es5/lib/bounding-volumes/axis-aligned-bounding-box.d.ts +0 -51
- package/dist/es5/lib/bounding-volumes/bounding-sphere.d.ts +0 -52
- package/dist/es5/lib/bounding-volumes/bounding-volume.d.ts +0 -32
- package/dist/es5/lib/bounding-volumes/oriented-bounding-box.d.ts +0 -83
- package/dist/es5/lib/culling-volume.d.ts +0 -55
- package/dist/es5/lib/plane.d.ts +0 -38
- package/dist/esm/constants.d.ts +0 -6
- package/dist/esm/index.d.ts +0 -20
- package/dist/esm/lib/algorithms/bounding-box-from-points.d.ts +0 -22
- package/dist/esm/lib/algorithms/compute-eigen-decomposition.d.ts +0 -39
- package/dist/esm/lib/bounding-volumes/axis-aligned-bounding-box.d.ts +0 -51
- package/dist/esm/lib/bounding-volumes/bounding-sphere.d.ts +0 -52
- package/dist/esm/lib/bounding-volumes/bounding-volume.d.ts +0 -32
- package/dist/esm/lib/bounding-volumes/oriented-bounding-box.d.ts +0 -83
- package/dist/esm/lib/culling-volume.d.ts +0 -55
- package/dist/esm/lib/plane.d.ts +0 -38
- package/src/constants.d.ts +0 -6
- package/src/constants.js +0 -8
- package/src/index.d.ts +0 -20
- package/src/lib/algorithms/bounding-sphere-from-points.d.ts +0 -17
- package/src/lib/algorithms/compute-eigen-decomposition.d.ts +0 -39
- package/src/lib/bounding-volumes/axis-aligned-bounding-box.d.ts +0 -51
- package/src/lib/bounding-volumes/bounding-sphere.d.ts +0 -52
- package/src/lib/bounding-volumes/oriented-bounding-box.d.ts +0 -83
- package/src/lib/culling-volume.d.ts +0 -55
- package/src/lib/plane.d.ts +0 -38
|
@@ -0,0 +1,142 @@
|
|
|
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 BoundingSphere from '../bounding-volumes/bounding-sphere';
|
|
5
|
+
/* eslint-disable */
|
|
6
|
+
const fromPointsXMin = new Vector3();
|
|
7
|
+
const fromPointsYMin = new Vector3();
|
|
8
|
+
const fromPointsZMin = new Vector3();
|
|
9
|
+
const fromPointsXMax = new Vector3();
|
|
10
|
+
const fromPointsYMax = new Vector3();
|
|
11
|
+
const fromPointsZMax = new Vector3();
|
|
12
|
+
const fromPointsCurrentPos = new Vector3();
|
|
13
|
+
const fromPointsScratch = new Vector3();
|
|
14
|
+
const fromPointsRitterCenter = new Vector3();
|
|
15
|
+
const fromPointsMinBoxPt = new Vector3();
|
|
16
|
+
const fromPointsMaxBoxPt = new Vector3();
|
|
17
|
+
const fromPointsNaiveCenterScratch = new Vector3();
|
|
18
|
+
const volumeConstant = (4.0 / 3.0) * Math.PI;
|
|
19
|
+
/**
|
|
20
|
+
* Computes a tight-fitting bounding sphere enclosing a list of 3D Cartesian points.
|
|
21
|
+
*
|
|
22
|
+
* The bounding sphere is computed by running two algorithms, a naive algorithm and
|
|
23
|
+
* Ritter's algorithm. The smaller of the two spheres is used to ensure a tight fit.
|
|
24
|
+
* Bounding sphere computation article http://blogs.agi.com/insight3d/index.php/2008/02/04/a-bounding
|
|
25
|
+
*
|
|
26
|
+
* @param positions An array of points that the bounding sphere will enclose.
|
|
27
|
+
* @param result Optional object onto which to store the result.
|
|
28
|
+
* @returns The modified result parameter or a new `BoundingSphere` instance if not provided.
|
|
29
|
+
*/
|
|
30
|
+
export default function makeBoundingSphereFromPoints(positions, result = new BoundingSphere()) {
|
|
31
|
+
if (!positions || positions.length === 0) {
|
|
32
|
+
return result.fromCenterRadius([0, 0, 0], 0);
|
|
33
|
+
}
|
|
34
|
+
const currentPos = fromPointsCurrentPos.copy(positions[0]);
|
|
35
|
+
const xMin = fromPointsXMin.copy(currentPos);
|
|
36
|
+
const yMin = fromPointsYMin.copy(currentPos);
|
|
37
|
+
const zMin = fromPointsZMin.copy(currentPos);
|
|
38
|
+
const xMax = fromPointsXMax.copy(currentPos);
|
|
39
|
+
const yMax = fromPointsYMax.copy(currentPos);
|
|
40
|
+
const zMax = fromPointsZMax.copy(currentPos);
|
|
41
|
+
for (const position of positions) {
|
|
42
|
+
currentPos.copy(position);
|
|
43
|
+
const x = currentPos.x;
|
|
44
|
+
const y = currentPos.y;
|
|
45
|
+
const z = currentPos.z;
|
|
46
|
+
// Store points containing the the smallest and largest components
|
|
47
|
+
if (x < xMin.x) {
|
|
48
|
+
xMin.copy(currentPos);
|
|
49
|
+
}
|
|
50
|
+
if (x > xMax.x) {
|
|
51
|
+
xMax.copy(currentPos);
|
|
52
|
+
}
|
|
53
|
+
if (y < yMin.y) {
|
|
54
|
+
yMin.copy(currentPos);
|
|
55
|
+
}
|
|
56
|
+
if (y > yMax.y) {
|
|
57
|
+
yMax.copy(currentPos);
|
|
58
|
+
}
|
|
59
|
+
if (z < zMin.z) {
|
|
60
|
+
zMin.copy(currentPos);
|
|
61
|
+
}
|
|
62
|
+
if (z > zMax.z) {
|
|
63
|
+
zMax.copy(currentPos);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// Compute x-, y-, and z-spans (Squared distances b/n each component's min. and max.).
|
|
67
|
+
const xSpan = fromPointsScratch.copy(xMax).subtract(xMin).magnitudeSquared();
|
|
68
|
+
const ySpan = fromPointsScratch.copy(yMax).subtract(yMin).magnitudeSquared();
|
|
69
|
+
const zSpan = fromPointsScratch.copy(zMax).subtract(zMin).magnitudeSquared();
|
|
70
|
+
// Set the diameter endpoints to the largest span.
|
|
71
|
+
let diameter1 = xMin;
|
|
72
|
+
let diameter2 = xMax;
|
|
73
|
+
let maxSpan = xSpan;
|
|
74
|
+
if (ySpan > maxSpan) {
|
|
75
|
+
maxSpan = ySpan;
|
|
76
|
+
diameter1 = yMin;
|
|
77
|
+
diameter2 = yMax;
|
|
78
|
+
}
|
|
79
|
+
if (zSpan > maxSpan) {
|
|
80
|
+
maxSpan = zSpan;
|
|
81
|
+
diameter1 = zMin;
|
|
82
|
+
diameter2 = zMax;
|
|
83
|
+
}
|
|
84
|
+
// Calculate the center of the initial sphere found by Ritter's algorithm
|
|
85
|
+
const ritterCenter = fromPointsRitterCenter;
|
|
86
|
+
ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
|
|
87
|
+
ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
|
|
88
|
+
ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
|
|
89
|
+
// Calculate the radius of the initial sphere found by Ritter's algorithm
|
|
90
|
+
let radiusSquared = fromPointsScratch.copy(diameter2).subtract(ritterCenter).magnitudeSquared();
|
|
91
|
+
let ritterRadius = Math.sqrt(radiusSquared);
|
|
92
|
+
// Find the center of the sphere found using the Naive method.
|
|
93
|
+
const minBoxPt = fromPointsMinBoxPt;
|
|
94
|
+
minBoxPt.x = xMin.x;
|
|
95
|
+
minBoxPt.y = yMin.y;
|
|
96
|
+
minBoxPt.z = zMin.z;
|
|
97
|
+
const maxBoxPt = fromPointsMaxBoxPt;
|
|
98
|
+
maxBoxPt.x = xMax.x;
|
|
99
|
+
maxBoxPt.y = yMax.y;
|
|
100
|
+
maxBoxPt.z = zMax.z;
|
|
101
|
+
const naiveCenter = fromPointsNaiveCenterScratch
|
|
102
|
+
.copy(minBoxPt)
|
|
103
|
+
.add(maxBoxPt)
|
|
104
|
+
.multiplyByScalar(0.5);
|
|
105
|
+
// Begin 2nd pass to find naive radius and modify the ritter sphere.
|
|
106
|
+
let naiveRadius = 0;
|
|
107
|
+
for (const position of positions) {
|
|
108
|
+
currentPos.copy(position);
|
|
109
|
+
// Find the furthest point from the naive center to calculate the naive radius.
|
|
110
|
+
const r = fromPointsScratch.copy(currentPos).subtract(naiveCenter).magnitude();
|
|
111
|
+
if (r > naiveRadius) {
|
|
112
|
+
naiveRadius = r;
|
|
113
|
+
}
|
|
114
|
+
// Make adjustments to the Ritter Sphere to include all points.
|
|
115
|
+
const oldCenterToPointSquared = fromPointsScratch
|
|
116
|
+
.copy(currentPos)
|
|
117
|
+
.subtract(ritterCenter)
|
|
118
|
+
.magnitudeSquared();
|
|
119
|
+
if (oldCenterToPointSquared > radiusSquared) {
|
|
120
|
+
const oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
|
|
121
|
+
// Calculate new radius to include the point that lies outside
|
|
122
|
+
ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
|
|
123
|
+
radiusSquared = ritterRadius * ritterRadius;
|
|
124
|
+
// Calculate center of new Ritter sphere
|
|
125
|
+
const oldToNew = oldCenterToPoint - ritterRadius;
|
|
126
|
+
ritterCenter.x = (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) / oldCenterToPoint;
|
|
127
|
+
ritterCenter.y = (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) / oldCenterToPoint;
|
|
128
|
+
ritterCenter.z = (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) / oldCenterToPoint;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (ritterRadius < naiveRadius) {
|
|
132
|
+
ritterCenter.to(result.center);
|
|
133
|
+
// @ts-ignore TS2540: Cannot assign to 'radius' because it is a read-only property.
|
|
134
|
+
result.radius = ritterRadius;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
naiveCenter.to(result.center);
|
|
138
|
+
// @ts-ignore TS2540: Cannot assign to 'radius' because it is a read-only property.
|
|
139
|
+
result.radius = naiveRadius;
|
|
140
|
+
}
|
|
141
|
+
return result;
|
|
142
|
+
}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import {Matrix3} from '@math.gl/core';
|
|
2
|
-
|
|
1
|
+
import { Matrix3 } from '@math.gl/core';
|
|
2
|
+
export declare type EigenDecomposition = {
|
|
3
|
+
unitary: Matrix3;
|
|
4
|
+
diagonal: Matrix3;
|
|
5
|
+
};
|
|
3
6
|
/**
|
|
4
7
|
* Computes the eigenvectors and eigenvalues of a symmetric matrix.
|
|
5
8
|
*
|
|
@@ -30,10 +33,5 @@ import {Matrix3} from '@math.gl/core';
|
|
|
30
33
|
* const v = result.unitary.getColumn(0, new Vector3()); // first eigenvector
|
|
31
34
|
* const c = v.multiplyByScalar(lambda); // equal to v.transformByMatrix3(a)
|
|
32
35
|
*/
|
|
33
|
-
export default function computeEigenDecomposition(
|
|
34
|
-
|
|
35
|
-
result?: object
|
|
36
|
-
): {
|
|
37
|
-
unitary: Matrix3;
|
|
38
|
-
diagonal: Matrix3;
|
|
39
|
-
};
|
|
36
|
+
export default function computeEigenDecomposition(matrix: number[], result?: EigenDecomposition): EigenDecomposition;
|
|
37
|
+
//# sourceMappingURL=compute-eigen-decomposition.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compute-eigen-decomposition.d.ts","sourceRoot":"","sources":["../../../src/lib/algorithms/compute-eigen-decomposition.ts"],"names":[],"mappings":"AAGA,OAAO,EAAC,OAAO,EAAa,MAAM,eAAe,CAAC;AASlD,oBAAY,kBAAkB,GAAG;IAC/B,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,CAAC,OAAO,UAAU,yBAAyB,CAC/C,MAAM,EAAE,MAAM,EAAE,EAEhB,MAAM,GAAE,kBAAuB,GAC9B,kBAAkB,CAkCpB"}
|
|
@@ -0,0 +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
|
|
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
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { BoundingVolume } from './bounding-volume';
|
|
2
|
+
import { Vector3 } from '@math.gl/core';
|
|
3
|
+
import Plane from '../plane';
|
|
4
|
+
import { INTERSECTION } from '../../constants';
|
|
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 implements BoundingVolume {
|
|
12
|
+
/** The center point of the bounding box. */
|
|
13
|
+
readonly center: Vector3;
|
|
14
|
+
/** The positive half diagonal of the bounding box. */
|
|
15
|
+
readonly halfDiagonal: Vector3;
|
|
16
|
+
/** The minimum point defining the bounding box. [0, 0, 0] for empty box */
|
|
17
|
+
readonly minimum: Vector3;
|
|
18
|
+
/** The maximum point defining the bounding box. [0, 0, 0] for empty box */
|
|
19
|
+
readonly maximum: Vector3;
|
|
20
|
+
/**
|
|
21
|
+
* Creates an instance of an AxisAlignedBoundingBox from the minimum and maximum points along the x, y, and z axes.
|
|
22
|
+
* @param minimum=[0, 0, 0] The minimum point along the x, y, and z axes.
|
|
23
|
+
* @param maximum=[0, 0, 0] The maximum point along the x, y, and z axes.
|
|
24
|
+
* @param center The center of the box; automatically computed if not supplied.
|
|
25
|
+
*/
|
|
26
|
+
constructor(minimum?: readonly number[], maximum?: readonly number[], center?: readonly number[]);
|
|
27
|
+
/**
|
|
28
|
+
* Duplicates a AxisAlignedBoundingBox instance.
|
|
29
|
+
*
|
|
30
|
+
* @returns {AxisAlignedBoundingBox} A new AxisAlignedBoundingBox instance.
|
|
31
|
+
*/
|
|
32
|
+
clone(): AxisAlignedBoundingBox;
|
|
33
|
+
/**
|
|
34
|
+
* Compares the provided AxisAlignedBoundingBox componentwise and returns
|
|
35
|
+
* <code>true</code> if they are equal, <code>false</code> otherwise.
|
|
36
|
+
*
|
|
37
|
+
* @param {AxisAlignedBoundingBox} [right] The second AxisAlignedBoundingBox to compare with.
|
|
38
|
+
* @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
|
|
39
|
+
*/
|
|
40
|
+
equals(right: any): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Applies a 4x4 affine transformation matrix to a bounding sphere.
|
|
43
|
+
* @param transform The transformation matrix to apply to the bounding sphere.
|
|
44
|
+
* @returns itself, i.e. the modified BoundingVolume.
|
|
45
|
+
*/
|
|
46
|
+
transform(transform: readonly number[]): this;
|
|
47
|
+
/**
|
|
48
|
+
* Determines which side of a plane a box is located.
|
|
49
|
+
*/
|
|
50
|
+
intersectPlane(plane: Plane): INTERSECTION;
|
|
51
|
+
/** Computes the estimated distance from the closest point on a bounding box to a point. */
|
|
52
|
+
distanceTo(point: readonly number[]): number;
|
|
53
|
+
/** Computes the estimated distance squared from the closest point on a bounding box to a point. */
|
|
54
|
+
distanceSquaredTo(point: readonly number[]): number;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=axis-aligned-bounding-box.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"axis-aligned-bounding-box.d.ts","sourceRoot":"","sources":["../../../src/lib/bounding-volumes/axis-aligned-bounding-box.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAC,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAC,OAAO,EAAC,MAAM,eAAe,CAAC;AACtC,OAAO,KAAK,MAAM,UAAU,CAAC;AAC7B,OAAO,EAAC,YAAY,EAAC,MAAM,iBAAiB,CAAC;AAK7C;;;;;GAKG;AACH,MAAM,CAAC,OAAO,OAAO,sBAAuB,YAAW,cAAc;IACnE,4CAA4C;IAC5C,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,sDAAsD;IACtD,QAAQ,CAAC,YAAY,EAAE,OAAO,CAAC;IAC/B,2EAA2E;IAC3E,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,2EAA2E;IAC3E,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAE1B;;;;;OAKG;gBAED,OAAO,GAAE,SAAS,MAAM,EAAc,EACtC,OAAO,GAAE,SAAS,MAAM,EAAc,EACtC,MAAM,CAAC,EAAE,SAAS,MAAM,EAAE;IAsB5B;;;;OAIG;IACH,KAAK;IAIL;;;;;;OAMG;IACH,MAAM,CAAC,KAAK,KAAA;IAOZ;;;;OAIG;IACH,SAAS,CAAC,SAAS,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI;IAS7C;;OAEG;IACH,cAAc,CAAC,KAAK,EAAE,KAAK,GAAG,YAAY;IAqB1C,2FAA2F;IAC3F,UAAU,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM;IAI5C,mGAAmG;IACnG,iBAAiB,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM;CAwBpD"}
|
|
@@ -0,0 +1,111 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { NumericArray, Vector3 } from '@math.gl/core';
|
|
2
|
+
import { INTERSECTION } from '../../constants';
|
|
3
|
+
import { BoundingVolume } from './bounding-volume';
|
|
4
|
+
import Plane from '../plane';
|
|
5
|
+
/** A BoundingSphere */
|
|
6
|
+
export default class BoundingSphere implements BoundingVolume {
|
|
7
|
+
center: Vector3;
|
|
8
|
+
radius: number;
|
|
9
|
+
/** Creates a bounding sphere */
|
|
10
|
+
constructor(center?: readonly number[], radius?: number);
|
|
11
|
+
/** Sets the bounding sphere from `center` and `radius`. */
|
|
12
|
+
fromCenterRadius(center: readonly number[], radius: number): this;
|
|
13
|
+
/**
|
|
14
|
+
* Computes a bounding sphere from the corner points of an axis-aligned bounding box. The sphere
|
|
15
|
+
* tightly and fully encompasses the box.
|
|
16
|
+
*/
|
|
17
|
+
fromCornerPoints(corner: readonly number[], oppositeCorner: readonly number[]): this;
|
|
18
|
+
/** Compares the provided BoundingSphere component wise */
|
|
19
|
+
equals(right: BoundingSphere): boolean;
|
|
20
|
+
/** Duplicates a BoundingSphere instance. */
|
|
21
|
+
clone(): BoundingSphere;
|
|
22
|
+
/** Computes a bounding sphere that contains both the left and right bounding spheres. */
|
|
23
|
+
union(boundingSphere: BoundingSphere): BoundingSphere;
|
|
24
|
+
/** Computes a bounding sphere by enlarging the provided sphere to contain the provided point. */
|
|
25
|
+
expand(point: readonly number[]): this;
|
|
26
|
+
/**
|
|
27
|
+
* Applies a 4x4 affine transformation matrix to a bounding sphere.
|
|
28
|
+
* @param sphere The bounding sphere to apply the transformation to.
|
|
29
|
+
* @param transform The transformation matrix to apply to the bounding sphere.
|
|
30
|
+
* @returns self.
|
|
31
|
+
*/
|
|
32
|
+
transform(transform: readonly number[]): this;
|
|
33
|
+
/** Computes the estimated distance squared from the closest point on a bounding sphere to a point. */
|
|
34
|
+
distanceSquaredTo(point: Readonly<NumericArray>): number;
|
|
35
|
+
/** Computes the estimated distance from the closest point on a bounding sphere to a point. */
|
|
36
|
+
distanceTo(point: Readonly<NumericArray>): number;
|
|
37
|
+
/** Determines which side of a plane a sphere is located. */
|
|
38
|
+
intersectPlane(plane: Plane): INTERSECTION;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=bounding-sphere.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bounding-sphere.d.ts","sourceRoot":"","sources":["../../../src/lib/bounding-volumes/bounding-sphere.ts"],"names":[],"mappings":"AAGA,OAAO,EAAC,YAAY,EAAE,OAAO,EAAC,MAAM,eAAe,CAAC;AAEpD,OAAO,EAAC,YAAY,EAAC,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAC,cAAc,EAAC,MAAM,mBAAmB,CAAC;AACjD,OAAO,KAAK,MAAM,UAAU,CAAC;AAK7B,uBAAuB;AACvB,MAAM,CAAC,OAAO,OAAO,cAAe,YAAW,cAAc;IAC3D,MAAM,EAAE,OAAO,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IAEf,gCAAgC;gBACpB,MAAM,GAAE,SAAS,MAAM,EAAc,EAAE,MAAM,GAAE,MAAY;IAMvE,2DAA2D;IAC3D,gBAAgB,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI;IAMjE;;;OAGG;IACH,gBAAgB,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,EAAE,cAAc,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI;IAOpF,0DAA0D;IAC1D,MAAM,CAAC,KAAK,EAAE,cAAc,GAAG,OAAO;IAOtC,4CAA4C;IAC5C,KAAK,IAAI,cAAc;IAIvB,yFAAyF;IACzF,KAAK,CAAC,cAAc,EAAE,cAAc,GAAG,cAAc;IAkCrD,iGAAiG;IACjG,MAAM,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI;IAWtC;;;;;OAKG;IACH,SAAS,CAAC,SAAS,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI;IAO7C,sGAAsG;IACtG,iBAAiB,CAAC,KAAK,EAAE,QAAQ,CAAC,YAAY,CAAC,GAAG,MAAM;IAKxD,8FAA8F;IAC9F,UAAU,CAAC,KAAK,EAAE,QAAQ,CAAC,YAAY,CAAC,GAAG,MAAM;IAMjD,4DAA4D;IAC5D,cAAc,CAAC,KAAK,EAAE,KAAK,GAAG,YAAY;CAiB3C"}
|
|
@@ -0,0 +1,118 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { INTERSECTION } from '../../constants';
|
|
2
|
+
import Plane from '../plane';
|
|
3
|
+
/**
|
|
4
|
+
* Common interface for BoundingVolumes
|
|
5
|
+
* including BoundingSphere, AxisAlignedBoundingBox and OrientedBoundingBox
|
|
6
|
+
*/
|
|
7
|
+
export interface BoundingVolume {
|
|
8
|
+
/**
|
|
9
|
+
* Applies a 4x4 affine transformation matrix to a bounding sphere.
|
|
10
|
+
* @param sphere The bounding sphere to apply the transformation to.
|
|
11
|
+
* @param transform The transformation matrix to apply to the bounding sphere.
|
|
12
|
+
* @returns itself, i.e. the modified BoundingVolume.
|
|
13
|
+
*/
|
|
14
|
+
transform(transform: readonly number[]): this;
|
|
15
|
+
/** Computes the estimated distance squared from the closest point on a bounding sphere to a point. */
|
|
16
|
+
distanceSquaredTo(point: readonly number[]): number;
|
|
17
|
+
/** Computes the estimated distance from the closest point on a bounding sphere to a point. */
|
|
18
|
+
distanceTo(point: readonly number[]): number;
|
|
19
|
+
/**
|
|
20
|
+
* Determines which side of a plane the oriented bounding box is located.
|
|
21
|
+
*
|
|
22
|
+
* @param plane The plane to test against.
|
|
23
|
+
* @returns
|
|
24
|
+
* - `INTERSECTION.INSIDE` if the entire box is on the side of the plane the normal is pointing.
|
|
25
|
+
* - `INTERSECTION.OUTSIDE` if the entire box is on the opposite side.
|
|
26
|
+
* - `INTERSECTION.INTERSECTING` if the box intersects the plane.
|
|
27
|
+
*/
|
|
28
|
+
intersectPlane(plane: Plane): INTERSECTION;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=bounding-volume.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bounding-volume.d.ts","sourceRoot":"","sources":["../../../src/lib/bounding-volumes/bounding-volume.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAC,MAAM,iBAAiB,CAAC;AAC7C,OAAO,KAAK,MAAM,UAAU,CAAC;AAE7B;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B;;;;;OAKG;IACH,SAAS,CAAC,SAAS,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI,CAAC;IAE9C,sGAAsG;IACtG,iBAAiB,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAAC;IACpD,8FAA8F;IAC9F,UAAU,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAAC;IAE7C;;;;;;;;OAQG;IACH,cAAc,CAAC,KAAK,EAAE,KAAK,GAAG,YAAY,CAAC;CAC5C"}
|