@math.gl/culling 4.0.0-beta.1 → 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.
Files changed (55) hide show
  1. package/dist/constants.d.ts +0 -1
  2. package/dist/constants.js +5 -4
  3. package/dist/index.cjs +27 -45
  4. package/dist/index.cjs.map +7 -0
  5. package/dist/index.d.ts +11 -12
  6. package/dist/index.js +2 -1
  7. package/dist/lib/algorithms/bounding-box-from-points.d.ts +2 -3
  8. package/dist/lib/algorithms/bounding-box-from-points.js +113 -113
  9. package/dist/lib/algorithms/bounding-sphere-from-points.d.ts +1 -2
  10. package/dist/lib/algorithms/bounding-sphere-from-points.js +117 -103
  11. package/dist/lib/algorithms/compute-eigen-decomposition.d.ts +1 -2
  12. package/dist/lib/algorithms/compute-eigen-decomposition.js +113 -85
  13. package/dist/lib/bounding-volumes/axis-aligned-bounding-box.d.ts +2 -3
  14. package/dist/lib/bounding-volumes/axis-aligned-bounding-box.js +100 -80
  15. package/dist/lib/bounding-volumes/bounding-sphere.d.ts +2 -3
  16. package/dist/lib/bounding-volumes/bounding-sphere.js +106 -97
  17. package/dist/lib/bounding-volumes/bounding-volume.d.ts +1 -2
  18. package/dist/lib/bounding-volumes/bounding-volume.js +0 -1
  19. package/dist/lib/bounding-volumes/oriented-bounding-box.d.ts +3 -4
  20. package/dist/lib/bounding-volumes/oriented-bounding-box.js +238 -196
  21. package/dist/lib/culling-volume.d.ts +3 -4
  22. package/dist/lib/culling-volume.js +109 -90
  23. package/dist/lib/perspective-frustum.d.ts +2 -3
  24. package/dist/lib/perspective-frustum.js +184 -136
  25. package/dist/lib/perspective-off-center-frustum.d.ts +2 -3
  26. package/dist/lib/perspective-off-center-frustum.js +259 -158
  27. package/dist/lib/plane.d.ts +0 -1
  28. package/dist/lib/plane.js +58 -59
  29. package/package.json +5 -6
  30. package/dist/constants.d.ts.map +0 -1
  31. package/dist/constants.js.map +0 -1
  32. package/dist/index.d.ts.map +0 -1
  33. package/dist/index.js.map +0 -1
  34. package/dist/lib/algorithms/bounding-box-from-points.d.ts.map +0 -1
  35. package/dist/lib/algorithms/bounding-box-from-points.js.map +0 -1
  36. package/dist/lib/algorithms/bounding-sphere-from-points.d.ts.map +0 -1
  37. package/dist/lib/algorithms/bounding-sphere-from-points.js.map +0 -1
  38. package/dist/lib/algorithms/compute-eigen-decomposition.d.ts.map +0 -1
  39. package/dist/lib/algorithms/compute-eigen-decomposition.js.map +0 -1
  40. package/dist/lib/bounding-volumes/axis-aligned-bounding-box.d.ts.map +0 -1
  41. package/dist/lib/bounding-volumes/axis-aligned-bounding-box.js.map +0 -1
  42. package/dist/lib/bounding-volumes/bounding-sphere.d.ts.map +0 -1
  43. package/dist/lib/bounding-volumes/bounding-sphere.js.map +0 -1
  44. package/dist/lib/bounding-volumes/bounding-volume.d.ts.map +0 -1
  45. package/dist/lib/bounding-volumes/bounding-volume.js.map +0 -1
  46. package/dist/lib/bounding-volumes/oriented-bounding-box.d.ts.map +0 -1
  47. package/dist/lib/bounding-volumes/oriented-bounding-box.js.map +0 -1
  48. package/dist/lib/culling-volume.d.ts.map +0 -1
  49. package/dist/lib/culling-volume.js.map +0 -1
  50. package/dist/lib/perspective-frustum.d.ts.map +0 -1
  51. package/dist/lib/perspective-frustum.js.map +0 -1
  52. package/dist/lib/perspective-off-center-frustum.d.ts.map +0 -1
  53. package/dist/lib/perspective-off-center-frustum.js.map +0 -1
  54. package/dist/lib/plane.d.ts.map +0 -1
  55. 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
- export function computeEigenDecomposition(matrix, result = {}) {
8
- const EIGEN_TOLERANCE = _MathUtils.EPSILON20;
9
- const EIGEN_MAX_SWEEPS = 10;
10
- let count = 0;
11
- let sweep = 0;
12
- const unitaryMatrix = scratchUnitary;
13
- const diagonalMatrix = scratchDiagonal;
14
- unitaryMatrix.identity();
15
- diagonalMatrix.copy(matrix);
16
- const epsilon = EIGEN_TOLERANCE * computeFrobeniusNorm(diagonalMatrix);
17
-
18
- while (sweep < EIGEN_MAX_SWEEPS && offDiagonalFrobeniusNorm(diagonalMatrix) > epsilon) {
19
- shurDecomposition(diagonalMatrix, jMatrix);
20
- jMatrixTranspose.copy(jMatrix).transpose();
21
- diagonalMatrix.multiplyRight(jMatrix);
22
- diagonalMatrix.multiplyLeft(jMatrixTranspose);
23
- unitaryMatrix.multiplyRight(jMatrix);
24
-
25
- if (++count > 2) {
26
- ++sweep;
27
- count = 0;
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
- result.unitary = unitaryMatrix.toTarget(result.unitary);
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
- let norm = 0.0;
38
-
39
- for (let i = 0; i < 9; ++i) {
40
- const temp = matrix[i];
41
- norm += temp * temp;
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
- let norm = 0.0;
52
-
53
- for (let i = 0; i < 3; ++i) {
54
- const temp = matrix[scratchMatrix.getElementIndex(colVal[i], rowVal[i])];
55
- norm += 2.0 * temp * temp;
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
- const tolerance = _MathUtils.EPSILON15;
63
- let maxDiagonal = 0.0;
64
- let rotAxis = 1;
65
-
66
- for (let i = 0; i < 3; ++i) {
67
- const temp = Math.abs(matrix[scratchMatrix.getElementIndex(colVal[i], rowVal[i])]);
68
-
69
- if (temp > maxDiagonal) {
70
- rotAxis = i;
71
- maxDiagonal = temp;
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
- const p = rowVal[rotAxis];
76
- const q = colVal[rotAxis];
77
- let c = 1.0;
78
- let s = 0.0;
79
-
80
- if (Math.abs(matrix[scratchMatrix.getElementIndex(q, p)]) > tolerance) {
81
- const qq = matrix[scratchMatrix.getElementIndex(q, q)];
82
- const pp = matrix[scratchMatrix.getElementIndex(p, p)];
83
- const qp = matrix[scratchMatrix.getElementIndex(q, p)];
84
- const tau = (qq - pp) / 2.0 / qp;
85
- let t;
86
-
87
- if (tau < 0.0) {
88
- t = -1.0 / (-tau + Math.sqrt(1.0 + tau * tau));
89
- } else {
90
- t = 1.0 / (tau + Math.sqrt(1.0 + tau * tau));
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
- c = 1.0 / Math.sqrt(1.0 + t * t);
94
- s = t * c;
95
- }
96
-
97
- Matrix3.IDENTITY.to(result);
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 './bounding-volume';
1
+ import { BoundingVolume } from "./bounding-volume.js";
2
2
  import { Vector3 } from '@math.gl/core';
3
- import { Plane } from '../plane';
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
- constructor(minimum = [0, 0, 0], maximum = [0, 0, 0], center) {
8
- _defineProperty(this, "center", void 0);
9
-
10
- _defineProperty(this, "halfDiagonal", void 0);
11
-
12
- _defineProperty(this, "minimum", void 0);
13
-
14
- _defineProperty(this, "maximum", void 0);
15
-
16
- center = center || scratchVector.copy(minimum).add(maximum).scale(0.5);
17
- this.center = new Vector3(center);
18
- this.halfDiagonal = new Vector3(maximum).subtract(this.center);
19
- this.minimum = new Vector3(minimum);
20
- this.maximum = new Vector3(maximum);
21
- }
22
-
23
- clone() {
24
- return new AxisAlignedBoundingBox(this.minimum, this.maximum, this.center);
25
- }
26
-
27
- equals(right) {
28
- return this === right || Boolean(right) && this.minimum.equals(right.minimum) && this.maximum.equals(right.maximum);
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
- if (s + e < 0) {
52
- return INTERSECTION.OUTSIDE;
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
- return INTERSECTION.INTERSECTING;
56
- }
57
-
58
- distanceTo(point) {
59
- return Math.sqrt(this.distanceSquaredTo(point));
60
- }
61
-
62
- distanceSquaredTo(point) {
63
- const offset = scratchVector.from(point).subtract(this.center);
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
- d = Math.abs(offset.y) - halfDiagonal.y;
76
-
77
- if (d > 0) {
78
- distanceSquared += d * d;
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
- d = Math.abs(offset.z) - halfDiagonal.z;
82
-
83
- if (d > 0) {
84
- distanceSquared += d * d;
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 './bounding-volume';
3
- import { Plane } from '../plane';
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
- import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
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
- constructor(center = [0, 0, 0], radius = 0.0) {
8
- _defineProperty(this, "center", void 0);
9
-
10
- _defineProperty(this, "radius", void 0);
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
- if (rightRadius >= centerSeparation + leftRadius) {
51
- return boundingSphere.clone();
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
- const halfDistanceBetweenTangentPoints = (leftRadius + centerSeparation + rightRadius) * 0.5;
55
- scratchVector2.copy(toRightCenter).scale((-leftRadius + halfDistanceBetweenTangentPoints) / centerSeparation).add(leftCenter);
56
- this.center.copy(scratchVector2);
57
- this.radius = halfDistanceBetweenTangentPoints;
58
- return this;
59
- }
60
-
61
- expand(point) {
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
- return this;
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
- if (distanceToPlane < radius) {
101
- return INTERSECTION.INTERSECTING;
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 '../plane';
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