@math.gl/culling 3.5.6 → 3.6.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. package/dist/constants.d.ts +6 -0
  2. package/dist/constants.d.ts.map +1 -0
  3. package/dist/constants.js +8 -0
  4. package/dist/es5/constants.js +7 -5
  5. package/dist/es5/constants.js.map +1 -1
  6. package/dist/es5/index.js +0 -12
  7. package/dist/es5/index.js.map +1 -1
  8. package/dist/es5/lib/algorithms/bounding-box-from-points.js.map +1 -1
  9. package/dist/es5/lib/algorithms/bounding-sphere-from-points.js.map +1 -1
  10. package/dist/es5/lib/algorithms/compute-eigen-decomposition.js.map +1 -1
  11. package/dist/es5/lib/bounding-volumes/axis-aligned-bounding-box.js +12 -6
  12. package/dist/es5/lib/bounding-volumes/axis-aligned-bounding-box.js.map +1 -1
  13. package/dist/es5/lib/bounding-volumes/bounding-sphere.js +8 -4
  14. package/dist/es5/lib/bounding-volumes/bounding-sphere.js.map +1 -1
  15. package/dist/es5/lib/bounding-volumes/bounding-volume.js +2 -0
  16. package/dist/es5/lib/bounding-volumes/bounding-volume.js.map +1 -0
  17. package/dist/es5/lib/bounding-volumes/oriented-bounding-box.js +4 -0
  18. package/dist/es5/lib/bounding-volumes/oriented-bounding-box.js.map +1 -1
  19. package/dist/es5/lib/culling-volume.js +6 -20
  20. package/dist/es5/lib/culling-volume.js.map +1 -1
  21. package/dist/es5/lib/perspective-frustum.js +17 -7
  22. package/dist/es5/lib/perspective-frustum.js.map +1 -1
  23. package/dist/es5/lib/perspective-off-center-frustum.js +17 -5
  24. package/dist/es5/lib/perspective-off-center-frustum.js.map +1 -1
  25. package/dist/es5/lib/plane.js +5 -1
  26. package/dist/es5/lib/plane.js.map +1 -1
  27. package/dist/esm/constants.js +7 -5
  28. package/dist/esm/constants.js.map +1 -1
  29. package/dist/esm/index.js +0 -2
  30. package/dist/esm/index.js.map +1 -1
  31. package/dist/esm/lib/algorithms/bounding-box-from-points.js.map +1 -1
  32. package/dist/esm/lib/algorithms/bounding-sphere-from-points.js.map +1 -1
  33. package/dist/esm/lib/algorithms/compute-eigen-decomposition.js.map +1 -1
  34. package/dist/esm/lib/bounding-volumes/axis-aligned-bounding-box.js +15 -6
  35. package/dist/esm/lib/bounding-volumes/axis-aligned-bounding-box.js.map +1 -1
  36. package/dist/esm/lib/bounding-volumes/bounding-sphere.js +9 -4
  37. package/dist/esm/lib/bounding-volumes/bounding-sphere.js.map +1 -1
  38. package/dist/esm/lib/bounding-volumes/bounding-volume.js +2 -0
  39. package/dist/esm/lib/bounding-volumes/bounding-volume.js.map +1 -0
  40. package/dist/esm/lib/bounding-volumes/oriented-bounding-box.js +5 -0
  41. package/dist/esm/lib/bounding-volumes/oriented-bounding-box.js.map +1 -1
  42. package/dist/esm/lib/culling-volume.js +10 -16
  43. package/dist/esm/lib/culling-volume.js.map +1 -1
  44. package/dist/esm/lib/perspective-frustum.js +31 -5
  45. package/dist/esm/lib/perspective-frustum.js.map +1 -1
  46. package/dist/esm/lib/perspective-off-center-frustum.js +31 -3
  47. package/dist/esm/lib/perspective-off-center-frustum.js.map +1 -1
  48. package/dist/esm/lib/plane.js +6 -1
  49. package/dist/esm/lib/plane.js.map +1 -1
  50. package/dist/index.d.ts +12 -0
  51. package/dist/index.d.ts.map +1 -0
  52. package/dist/index.js +13 -0
  53. package/{src → dist}/lib/algorithms/bounding-box-from-points.d.ts +3 -10
  54. package/dist/lib/algorithms/bounding-box-from-points.d.ts.map +1 -0
  55. package/dist/lib/algorithms/bounding-box-from-points.js +131 -0
  56. package/dist/{esm/lib → lib}/algorithms/bounding-sphere-from-points.d.ts +2 -5
  57. package/dist/lib/algorithms/bounding-sphere-from-points.d.ts.map +1 -0
  58. package/dist/lib/algorithms/bounding-sphere-from-points.js +140 -0
  59. package/dist/{es5/lib → lib}/algorithms/compute-eigen-decomposition.d.ts +7 -9
  60. package/dist/lib/algorithms/compute-eigen-decomposition.d.ts.map +1 -0
  61. package/dist/lib/algorithms/compute-eigen-decomposition.js +131 -0
  62. package/dist/lib/bounding-volumes/axis-aligned-bounding-box.d.ts +56 -0
  63. package/dist/lib/bounding-volumes/axis-aligned-bounding-box.d.ts.map +1 -0
  64. package/dist/lib/bounding-volumes/axis-aligned-bounding-box.js +111 -0
  65. package/dist/lib/bounding-volumes/bounding-sphere.d.ts +40 -0
  66. package/dist/lib/bounding-volumes/bounding-sphere.d.ts.map +1 -0
  67. package/dist/lib/bounding-volumes/bounding-sphere.js +118 -0
  68. package/dist/lib/bounding-volumes/bounding-volume.d.ts +30 -0
  69. package/dist/lib/bounding-volumes/bounding-volume.d.ts.map +1 -0
  70. package/dist/lib/bounding-volumes/bounding-volume.js +1 -0
  71. package/dist/lib/bounding-volumes/oriented-bounding-box.d.ts +67 -0
  72. package/dist/lib/bounding-volumes/oriented-bounding-box.d.ts.map +1 -0
  73. package/dist/lib/bounding-volumes/oriented-bounding-box.js +256 -0
  74. package/dist/lib/culling-volume.d.ts +46 -0
  75. package/dist/lib/culling-volume.d.ts.map +1 -0
  76. package/dist/lib/culling-volume.js +121 -0
  77. package/dist/lib/perspective-frustum.d.ts +140 -0
  78. package/dist/lib/perspective-frustum.d.ts.map +1 -0
  79. package/dist/lib/perspective-frustum.js +205 -0
  80. package/dist/lib/perspective-off-center-frustum.d.ts +122 -0
  81. package/dist/lib/perspective-off-center-frustum.d.ts.map +1 -0
  82. package/dist/lib/perspective-off-center-frustum.js +305 -0
  83. package/dist/lib/plane.d.ts +26 -0
  84. package/dist/lib/plane.d.ts.map +1 -0
  85. package/dist/lib/plane.js +63 -0
  86. package/package.json +5 -5
  87. package/src/constants.ts +8 -0
  88. package/src/{index.js → index.ts} +0 -4
  89. package/src/lib/algorithms/{bounding-box-from-points.js → bounding-box-from-points.ts} +17 -4
  90. package/src/lib/algorithms/{bounding-sphere-from-points.js → bounding-sphere-from-points.ts} +15 -3
  91. package/src/lib/algorithms/{compute-eigen-decomposition.js → compute-eigen-decomposition.ts} +40 -1
  92. package/src/lib/bounding-volumes/{axis-aligned-bounding-box.js → axis-aligned-bounding-box.ts} +45 -14
  93. package/src/lib/bounding-volumes/{bounding-sphere.js → bounding-sphere.ts} +44 -29
  94. package/src/lib/bounding-volumes/{bounding-volume.d.ts → bounding-volume.ts} +7 -7
  95. package/src/lib/bounding-volumes/{oriented-bounding-box.js → oriented-bounding-box.ts} +72 -14
  96. package/src/lib/{culling-volume.js → culling-volume.ts} +42 -34
  97. package/src/lib/{perspective-frustum.js → perspective-frustum.ts} +48 -66
  98. package/src/lib/{perspective-off-center-frustum.js → perspective-off-center-frustum.ts} +24 -14
  99. package/src/lib/{plane.js → plane.ts} +24 -17
  100. package/dist/es5/constants.d.ts +0 -6
  101. package/dist/es5/index.d.ts +0 -20
  102. package/dist/es5/lib/algorithms/bounding-box-from-points.d.ts +0 -22
  103. package/dist/es5/lib/algorithms/bounding-sphere-from-points.d.ts +0 -17
  104. package/dist/es5/lib/bounding-volumes/axis-aligned-bounding-box.d.ts +0 -51
  105. package/dist/es5/lib/bounding-volumes/bounding-sphere.d.ts +0 -52
  106. package/dist/es5/lib/bounding-volumes/bounding-volume.d.ts +0 -32
  107. package/dist/es5/lib/bounding-volumes/oriented-bounding-box.d.ts +0 -83
  108. package/dist/es5/lib/culling-volume.d.ts +0 -55
  109. package/dist/es5/lib/plane.d.ts +0 -38
  110. package/dist/esm/constants.d.ts +0 -6
  111. package/dist/esm/index.d.ts +0 -20
  112. package/dist/esm/lib/algorithms/bounding-box-from-points.d.ts +0 -22
  113. package/dist/esm/lib/algorithms/compute-eigen-decomposition.d.ts +0 -39
  114. package/dist/esm/lib/bounding-volumes/axis-aligned-bounding-box.d.ts +0 -51
  115. package/dist/esm/lib/bounding-volumes/bounding-sphere.d.ts +0 -52
  116. package/dist/esm/lib/bounding-volumes/bounding-volume.d.ts +0 -32
  117. package/dist/esm/lib/bounding-volumes/oriented-bounding-box.d.ts +0 -83
  118. package/dist/esm/lib/culling-volume.d.ts +0 -55
  119. package/dist/esm/lib/plane.d.ts +0 -38
  120. package/src/constants.d.ts +0 -6
  121. package/src/constants.js +0 -8
  122. package/src/index.d.ts +0 -20
  123. package/src/lib/algorithms/bounding-sphere-from-points.d.ts +0 -17
  124. package/src/lib/algorithms/compute-eigen-decomposition.d.ts +0 -39
  125. package/src/lib/bounding-volumes/axis-aligned-bounding-box.d.ts +0 -51
  126. package/src/lib/bounding-volumes/bounding-sphere.d.ts +0 -52
  127. package/src/lib/bounding-volumes/oriented-bounding-box.d.ts +0 -83
  128. package/src/lib/culling-volume.d.ts +0 -55
  129. package/src/lib/plane.d.ts +0 -38
@@ -0,0 +1,256 @@
1
+ // This file is derived from the Cesium math library under Apache 2 license
2
+ // See LICENSE.md and https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md
3
+ import { Vector3, Matrix3, Quaternion } from '@math.gl/core';
4
+ import BoundingSphere from './bounding-sphere';
5
+ import { INTERSECTION } from '../../constants';
6
+ const scratchVector3 = new Vector3();
7
+ const scratchOffset = new Vector3();
8
+ const scratchVectorU = new Vector3();
9
+ const scratchVectorV = new Vector3();
10
+ const scratchVectorW = new Vector3();
11
+ const scratchCorner = new Vector3();
12
+ const scratchToCenter = new Vector3();
13
+ const MATRIX3 = {
14
+ COLUMN0ROW0: 0,
15
+ COLUMN0ROW1: 1,
16
+ COLUMN0ROW2: 2,
17
+ COLUMN1ROW0: 3,
18
+ COLUMN1ROW1: 4,
19
+ COLUMN1ROW2: 5,
20
+ COLUMN2ROW0: 6,
21
+ COLUMN2ROW1: 7,
22
+ COLUMN2ROW2: 8
23
+ };
24
+ /**
25
+ * An OrientedBoundingBox of some object is a closed and convex cuboid.
26
+ * It can provide a tighter bounding volume than `BoundingSphere` or
27
+ * `AxisAlignedBoundingBox` in many cases.
28
+ */
29
+ export default class OrientedBoundingBox {
30
+ constructor(center = [0, 0, 0], halfAxes = [0, 0, 0, 0, 0, 0, 0, 0, 0]) {
31
+ this.center = new Vector3().from(center);
32
+ this.halfAxes = new Matrix3(halfAxes);
33
+ }
34
+ /** Returns an array with three halfSizes for the bounding box */
35
+ get halfSize() {
36
+ const xAxis = this.halfAxes.getColumn(0);
37
+ const yAxis = this.halfAxes.getColumn(1);
38
+ const zAxis = this.halfAxes.getColumn(2);
39
+ return [new Vector3(xAxis).len(), new Vector3(yAxis).len(), new Vector3(zAxis).len()];
40
+ }
41
+ /** Returns a quaternion describing the orientation of the bounding box */
42
+ get quaternion() {
43
+ const xAxis = this.halfAxes.getColumn(0);
44
+ const yAxis = this.halfAxes.getColumn(1);
45
+ const zAxis = this.halfAxes.getColumn(2);
46
+ const normXAxis = new Vector3(xAxis).normalize();
47
+ const normYAxis = new Vector3(yAxis).normalize();
48
+ const normZAxis = new Vector3(zAxis).normalize();
49
+ return new Quaternion().fromMatrix3(new Matrix3([...normXAxis, ...normYAxis, ...normZAxis]));
50
+ }
51
+ /**
52
+ * Create OrientedBoundingBox from quaternion based OBB,
53
+ */
54
+ fromCenterHalfSizeQuaternion(center, halfSize, quaternion) {
55
+ const quaternionObject = new Quaternion(quaternion);
56
+ const directionsMatrix = new Matrix3().fromQuaternion(quaternionObject);
57
+ directionsMatrix[0] = directionsMatrix[0] * halfSize[0];
58
+ directionsMatrix[1] = directionsMatrix[1] * halfSize[0];
59
+ directionsMatrix[2] = directionsMatrix[2] * halfSize[0];
60
+ directionsMatrix[3] = directionsMatrix[3] * halfSize[1];
61
+ directionsMatrix[4] = directionsMatrix[4] * halfSize[1];
62
+ directionsMatrix[5] = directionsMatrix[5] * halfSize[1];
63
+ directionsMatrix[6] = directionsMatrix[6] * halfSize[2];
64
+ directionsMatrix[7] = directionsMatrix[7] * halfSize[2];
65
+ directionsMatrix[8] = directionsMatrix[8] * halfSize[2];
66
+ this.center = new Vector3().from(center);
67
+ this.halfAxes = directionsMatrix;
68
+ return this;
69
+ }
70
+ /** Duplicates a OrientedBoundingBox instance. */
71
+ clone() {
72
+ return new OrientedBoundingBox(this.center, this.halfAxes);
73
+ }
74
+ /** Compares the provided OrientedBoundingBox component wise and returns */
75
+ equals(right) {
76
+ return (this === right ||
77
+ (Boolean(right) && this.center.equals(right.center) && this.halfAxes.equals(right.halfAxes)));
78
+ }
79
+ /** Computes a tight-fitting bounding sphere enclosing the provided oriented bounding box. */
80
+ getBoundingSphere(result = new BoundingSphere()) {
81
+ const halfAxes = this.halfAxes;
82
+ const u = halfAxes.getColumn(0, scratchVectorU);
83
+ const v = halfAxes.getColumn(1, scratchVectorV);
84
+ const w = halfAxes.getColumn(2, scratchVectorW);
85
+ // Calculate "corner" vector
86
+ const cornerVector = scratchVector3.copy(u).add(v).add(w);
87
+ result.center.copy(this.center);
88
+ result.radius = cornerVector.magnitude();
89
+ return result;
90
+ }
91
+ /** Determines which side of a plane the oriented bounding box is located. */
92
+ intersectPlane(plane) {
93
+ const center = this.center;
94
+ const normal = plane.normal;
95
+ const halfAxes = this.halfAxes;
96
+ const normalX = normal.x;
97
+ const normalY = normal.y;
98
+ const normalZ = normal.z;
99
+ // Plane is used as if it is its normal; the first three components are assumed to be normalized
100
+ const radEffective = Math.abs(normalX * halfAxes[MATRIX3.COLUMN0ROW0] +
101
+ normalY * halfAxes[MATRIX3.COLUMN0ROW1] +
102
+ normalZ * halfAxes[MATRIX3.COLUMN0ROW2]) +
103
+ Math.abs(normalX * halfAxes[MATRIX3.COLUMN1ROW0] +
104
+ normalY * halfAxes[MATRIX3.COLUMN1ROW1] +
105
+ normalZ * halfAxes[MATRIX3.COLUMN1ROW2]) +
106
+ Math.abs(normalX * halfAxes[MATRIX3.COLUMN2ROW0] +
107
+ normalY * halfAxes[MATRIX3.COLUMN2ROW1] +
108
+ normalZ * halfAxes[MATRIX3.COLUMN2ROW2]);
109
+ const distanceToPlane = normal.dot(center) + plane.distance;
110
+ if (distanceToPlane <= -radEffective) {
111
+ // The entire box is on the negative side of the plane normal
112
+ return INTERSECTION.OUTSIDE;
113
+ }
114
+ else if (distanceToPlane >= radEffective) {
115
+ // The entire box is on the positive side of the plane normal
116
+ return INTERSECTION.INSIDE;
117
+ }
118
+ return INTERSECTION.INTERSECTING;
119
+ }
120
+ /** Computes the estimated distance from the closest point on a bounding box to a point. */
121
+ distanceTo(point) {
122
+ return Math.sqrt(this.distanceSquaredTo(point));
123
+ }
124
+ /**
125
+ * Computes the estimated distance squared from the closest point
126
+ * on a bounding box to a point.
127
+ * See Geometric Tools for Computer Graphics 10.4.2
128
+ */
129
+ distanceSquaredTo(point) {
130
+ // Computes the estimated distance squared from the
131
+ // closest point on a bounding box to a point.
132
+ // See Geometric Tools for Computer Graphics 10.4.2
133
+ const offset = scratchOffset.from(point).subtract(this.center);
134
+ const halfAxes = this.halfAxes;
135
+ const u = halfAxes.getColumn(0, scratchVectorU);
136
+ const v = halfAxes.getColumn(1, scratchVectorV);
137
+ const w = halfAxes.getColumn(2, scratchVectorW);
138
+ const uHalf = u.magnitude();
139
+ const vHalf = v.magnitude();
140
+ const wHalf = w.magnitude();
141
+ u.normalize();
142
+ v.normalize();
143
+ w.normalize();
144
+ let distanceSquared = 0.0;
145
+ let d;
146
+ d = Math.abs(offset.dot(u)) - uHalf;
147
+ if (d > 0) {
148
+ distanceSquared += d * d;
149
+ }
150
+ d = Math.abs(offset.dot(v)) - vHalf;
151
+ if (d > 0) {
152
+ distanceSquared += d * d;
153
+ }
154
+ d = Math.abs(offset.dot(w)) - wHalf;
155
+ if (d > 0) {
156
+ distanceSquared += d * d;
157
+ }
158
+ return distanceSquared;
159
+ }
160
+ /**
161
+ * The distances calculated by the vector from the center of the bounding box
162
+ * to position projected onto direction.
163
+ *
164
+ * - If you imagine the infinite number of planes with normal direction,
165
+ * this computes the smallest distance to the closest and farthest planes
166
+ * from `position` that intersect the bounding box.
167
+ *
168
+ * @param position The position to calculate the distance from.
169
+ * @param direction The direction from position.
170
+ * @param result An Interval (array of length 2) to store the nearest and farthest distances.
171
+ * @returns Interval (array of length 2) with nearest and farthest distances
172
+ * on the bounding box from position in direction.
173
+ */
174
+ // eslint-disable-next-line max-statements
175
+ computePlaneDistances(position, direction, result = [-0, -0]) {
176
+ let minDist = Number.POSITIVE_INFINITY;
177
+ let maxDist = Number.NEGATIVE_INFINITY;
178
+ const center = this.center;
179
+ const halfAxes = this.halfAxes;
180
+ const u = halfAxes.getColumn(0, scratchVectorU);
181
+ const v = halfAxes.getColumn(1, scratchVectorV);
182
+ const w = halfAxes.getColumn(2, scratchVectorW);
183
+ // project first corner
184
+ const corner = scratchCorner.copy(u).add(v).add(w).add(center);
185
+ const toCenter = scratchToCenter.copy(corner).subtract(position);
186
+ let mag = direction.dot(toCenter);
187
+ minDist = Math.min(mag, minDist);
188
+ maxDist = Math.max(mag, maxDist);
189
+ // project second corner
190
+ corner.copy(center).add(u).add(v).subtract(w);
191
+ toCenter.copy(corner).subtract(position);
192
+ mag = direction.dot(toCenter);
193
+ minDist = Math.min(mag, minDist);
194
+ maxDist = Math.max(mag, maxDist);
195
+ // project third corner
196
+ corner.copy(center).add(u).subtract(v).add(w);
197
+ toCenter.copy(corner).subtract(position);
198
+ mag = direction.dot(toCenter);
199
+ minDist = Math.min(mag, minDist);
200
+ maxDist = Math.max(mag, maxDist);
201
+ // project fourth corner
202
+ corner.copy(center).add(u).subtract(v).subtract(w);
203
+ toCenter.copy(corner).subtract(position);
204
+ mag = direction.dot(toCenter);
205
+ minDist = Math.min(mag, minDist);
206
+ maxDist = Math.max(mag, maxDist);
207
+ // project fifth corner
208
+ center.copy(corner).subtract(u).add(v).add(w);
209
+ toCenter.copy(corner).subtract(position);
210
+ mag = direction.dot(toCenter);
211
+ minDist = Math.min(mag, minDist);
212
+ maxDist = Math.max(mag, maxDist);
213
+ // project sixth corner
214
+ center.copy(corner).subtract(u).add(v).subtract(w);
215
+ toCenter.copy(corner).subtract(position);
216
+ mag = direction.dot(toCenter);
217
+ minDist = Math.min(mag, minDist);
218
+ maxDist = Math.max(mag, maxDist);
219
+ // project seventh corner
220
+ center.copy(corner).subtract(u).subtract(v).add(w);
221
+ toCenter.copy(corner).subtract(position);
222
+ mag = direction.dot(toCenter);
223
+ minDist = Math.min(mag, minDist);
224
+ maxDist = Math.max(mag, maxDist);
225
+ // project eighth corner
226
+ center.copy(corner).subtract(u).subtract(v).subtract(w);
227
+ toCenter.copy(corner).subtract(position);
228
+ mag = direction.dot(toCenter);
229
+ minDist = Math.min(mag, minDist);
230
+ maxDist = Math.max(mag, maxDist);
231
+ result[0] = minDist;
232
+ result[1] = maxDist;
233
+ return result;
234
+ }
235
+ /**
236
+ * Applies a 4x4 affine transformation matrix to a bounding sphere.
237
+ * @param transform The transformation matrix to apply to the bounding sphere.
238
+ * @returns itself, i.e. the modified BoundingVolume.
239
+ */
240
+ transform(transformation) {
241
+ this.center.transformAsPoint(transformation);
242
+ const xAxis = this.halfAxes.getColumn(0, scratchVectorU);
243
+ xAxis.transformAsPoint(transformation);
244
+ const yAxis = this.halfAxes.getColumn(1, scratchVectorV);
245
+ yAxis.transformAsPoint(transformation);
246
+ const zAxis = this.halfAxes.getColumn(2, scratchVectorW);
247
+ zAxis.transformAsPoint(transformation);
248
+ this.halfAxes = new Matrix3([...xAxis, ...yAxis, ...zAxis]);
249
+ return this;
250
+ }
251
+ getTransform() {
252
+ // const modelMatrix = Matrix4.fromRotationTranslation(this.boundingVolume.halfAxes, this.boundingVolume.center);
253
+ // return modelMatrix;
254
+ throw new Error('not implemented');
255
+ }
256
+ }
@@ -0,0 +1,46 @@
1
+ import { INTERSECTION } from '../constants';
2
+ import Plane from './plane';
3
+ import type { BoundingVolume } from './bounding-volumes/bounding-volume';
4
+ import type BoundingSphere from './bounding-volumes/bounding-sphere';
5
+ /** A culling volume defined by planes. */
6
+ export default class CullingVolume {
7
+ /**
8
+ * For plane masks (as used in {@link CullingVolume#computeVisibilityWithPlaneMask}), this special value
9
+ * represents the case where the object bounding volume is entirely outside the culling volume.
10
+ */
11
+ static MASK_OUTSIDE: number;
12
+ /**
13
+ * For plane masks (as used in {@link CullingVolume.prototype.computeVisibilityWithPlaneMask}), this value
14
+ * represents the case where the object bounding volume is entirely inside the culling volume.
15
+ */
16
+ static MASK_INSIDE: number;
17
+ /**
18
+ * For plane masks (as used in {@link CullingVolume.prototype.computeVisibilityWithPlaneMask}), this value
19
+ * represents the case where the object bounding volume (may) intersect all planes of the culling volume.
20
+ */
21
+ static MASK_INDETERMINATE: number;
22
+ /** Array of clipping planes. */
23
+ readonly planes: Plane[];
24
+ /**
25
+ * Create a new `CullingVolume` bounded by an array of clipping planed
26
+ * @param planes Array of clipping planes.
27
+ * */
28
+ constructor(planes?: Plane[]);
29
+ /**
30
+ * Constructs a culling volume from a bounding sphere. Creates six planes that create a box containing the sphere.
31
+ * The planes are aligned to the x, y, and z axes in world coordinates.
32
+ */
33
+ fromBoundingSphere(boundingSphere: BoundingSphere): CullingVolume;
34
+ /** Determines whether a bounding volume intersects the culling volume. */
35
+ computeVisibility(boundingVolume: BoundingVolume): INTERSECTION;
36
+ /**
37
+ * Determines whether a bounding volume intersects the culling volume.
38
+ *
39
+ * @param parentPlaneMask A bit mask from the boundingVolume's parent's check against the same culling
40
+ * volume, such that if (planeMask & (1 << planeIndex) === 0), for k < 31, then
41
+ * the parent (and therefore this) volume is completely inside plane[planeIndex]
42
+ * and that plane check can be skipped.
43
+ */
44
+ computeVisibilityWithPlaneMask(boundingVolume: BoundingVolume, parentPlaneMask: number): number;
45
+ }
46
+ //# sourceMappingURL=culling-volume.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"culling-volume.d.ts","sourceRoot":"","sources":["../../src/lib/culling-volume.ts"],"names":[],"mappings":"AAKA,OAAO,EAAC,YAAY,EAAC,MAAM,cAAc,CAAC;AAC1C,OAAO,KAAK,MAAM,SAAS,CAAC;AAC5B,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,oCAAoC,CAAC;AACvE,OAAO,KAAK,cAAc,MAAM,oCAAoC,CAAC;AASrE,0CAA0C;AAC1C,MAAM,CAAC,OAAO,OAAO,aAAa;IAChC;;;OAGG;IACH,MAAM,CAAC,YAAY,SAAc;IAEjC;;;OAGG;IACH,MAAM,CAAC,WAAW,SAAc;IAEhC;;;OAGG;IACH,MAAM,CAAC,kBAAkB,SAAc;IAEvC,gCAAgC;IAChC,QAAQ,CAAC,MAAM,EAAE,KAAK,EAAE,CAAC;IAEzB;;;SAGK;gBACO,MAAM,GAAE,KAAK,EAAO;IAIhC;;;OAGG;IACH,kBAAkB,CAAC,cAAc,EAAE,cAAc,GAAG,aAAa;IAsCjE,0EAA0E;IAC1E,iBAAiB,CAAC,cAAc,EAAE,cAAc,GAAG,YAAY;IAsB/D;;;;;;;OAOG;IACH,8BAA8B,CAAC,cAAc,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,GAAG,MAAM;CAmChG"}
@@ -0,0 +1,121 @@
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
+ /* eslint-disable */
4
+ import { Vector3, assert } from '@math.gl/core';
5
+ import { INTERSECTION } from '../constants';
6
+ import Plane from './plane';
7
+ // X, Y, Z Unit vectors
8
+ const faces = [new Vector3([1, 0, 0]), new Vector3([0, 1, 0]), new Vector3([0, 0, 1])];
9
+ const scratchPlaneCenter = new Vector3();
10
+ const scratchPlaneNormal = new Vector3();
11
+ const scratchPlane = new Plane(new Vector3(1.0, 0.0, 0.0), 0.0);
12
+ /** A culling volume defined by planes. */
13
+ export default class CullingVolume {
14
+ /**
15
+ * Create a new `CullingVolume` bounded by an array of clipping planed
16
+ * @param planes Array of clipping planes.
17
+ * */
18
+ constructor(planes = []) {
19
+ this.planes = planes;
20
+ }
21
+ /**
22
+ * Constructs a culling volume from a bounding sphere. Creates six planes that create a box containing the sphere.
23
+ * The planes are aligned to the x, y, and z axes in world coordinates.
24
+ */
25
+ fromBoundingSphere(boundingSphere) {
26
+ this.planes.length = 2 * faces.length;
27
+ const center = boundingSphere.center;
28
+ const radius = boundingSphere.radius;
29
+ let planeIndex = 0;
30
+ for (const faceNormal of faces) {
31
+ let plane0 = this.planes[planeIndex];
32
+ let plane1 = this.planes[planeIndex + 1];
33
+ if (!plane0) {
34
+ plane0 = this.planes[planeIndex] = new Plane();
35
+ }
36
+ if (!plane1) {
37
+ plane1 = this.planes[planeIndex + 1] = new Plane();
38
+ }
39
+ const plane0Center = scratchPlaneCenter.copy(faceNormal).scale(-radius).add(center);
40
+ const plane0Distance = -faceNormal.dot(plane0Center);
41
+ plane0.fromPointNormal(plane0Center, faceNormal);
42
+ const plane1Center = scratchPlaneCenter.copy(faceNormal).scale(radius).add(center);
43
+ const negatedFaceNormal = scratchPlaneNormal.copy(faceNormal).negate();
44
+ const plane1Distance = -negatedFaceNormal.dot(plane1Center);
45
+ plane1.fromPointNormal(plane1Center, negatedFaceNormal);
46
+ planeIndex += 2;
47
+ }
48
+ return this;
49
+ }
50
+ /** Determines whether a bounding volume intersects the culling volume. */
51
+ computeVisibility(boundingVolume) {
52
+ // const planes = this.planes;
53
+ let intersect = INTERSECTION.INSIDE;
54
+ for (const plane of this.planes) {
55
+ const result = boundingVolume.intersectPlane(plane);
56
+ switch (result) {
57
+ case INTERSECTION.OUTSIDE:
58
+ // We are done
59
+ return INTERSECTION.OUTSIDE;
60
+ case INTERSECTION.INTERSECTING:
61
+ // If no other intersection is outside, return INTERSECTING
62
+ intersect = INTERSECTION.INTERSECTING;
63
+ break;
64
+ default:
65
+ }
66
+ }
67
+ return intersect;
68
+ }
69
+ /**
70
+ * Determines whether a bounding volume intersects the culling volume.
71
+ *
72
+ * @param parentPlaneMask A bit mask from the boundingVolume's parent's check against the same culling
73
+ * volume, such that if (planeMask & (1 << planeIndex) === 0), for k < 31, then
74
+ * the parent (and therefore this) volume is completely inside plane[planeIndex]
75
+ * and that plane check can be skipped.
76
+ */
77
+ computeVisibilityWithPlaneMask(boundingVolume, parentPlaneMask) {
78
+ assert(Number.isFinite(parentPlaneMask), 'parentPlaneMask is required.');
79
+ if (parentPlaneMask === CullingVolume.MASK_OUTSIDE ||
80
+ parentPlaneMask === CullingVolume.MASK_INSIDE) {
81
+ // parent is completely outside or completely inside, so this child is as well.
82
+ return parentPlaneMask;
83
+ }
84
+ // Start with MASK_INSIDE (all zeros) so that after the loop, the return value can be compared with MASK_INSIDE.
85
+ // (Because if there are fewer than 31 planes, the upper bits wont be changed.)
86
+ let mask = CullingVolume.MASK_INSIDE;
87
+ const planes = this.planes;
88
+ for (let k = 0; k < this.planes.length; ++k) {
89
+ // For k greater than 31 (since 31 is the maximum number of INSIDE/INTERSECTING bits we can store), skip the optimization.
90
+ const flag = k < 31 ? 1 << k : 0;
91
+ if (k < 31 && (parentPlaneMask & flag) === 0) {
92
+ // boundingVolume is known to be INSIDE this plane.
93
+ continue;
94
+ }
95
+ const plane = planes[k];
96
+ const result = boundingVolume.intersectPlane(plane);
97
+ if (result === INTERSECTION.OUTSIDE) {
98
+ return CullingVolume.MASK_OUTSIDE;
99
+ }
100
+ else if (result === INTERSECTION.INTERSECTING) {
101
+ mask |= flag;
102
+ }
103
+ }
104
+ return mask;
105
+ }
106
+ }
107
+ /**
108
+ * For plane masks (as used in {@link CullingVolume#computeVisibilityWithPlaneMask}), this special value
109
+ * represents the case where the object bounding volume is entirely outside the culling volume.
110
+ */
111
+ CullingVolume.MASK_OUTSIDE = 0xffffffff;
112
+ /**
113
+ * For plane masks (as used in {@link CullingVolume.prototype.computeVisibilityWithPlaneMask}), this value
114
+ * represents the case where the object bounding volume is entirely inside the culling volume.
115
+ */
116
+ CullingVolume.MASK_INSIDE = 0x00000000;
117
+ /**
118
+ * For plane masks (as used in {@link CullingVolume.prototype.computeVisibilityWithPlaneMask}), this value
119
+ * represents the case where the object bounding volume (may) intersect all planes of the culling volume.
120
+ */
121
+ CullingVolume.MASK_INDETERMINATE = 0x7fffffff;
@@ -0,0 +1,140 @@
1
+ import { Matrix4 } from '@math.gl/core';
2
+ import PerspectiveOffCenterFrustum from './perspective-off-center-frustum';
3
+ /**
4
+ * The viewing frustum is defined by 6 planes.
5
+ * Each plane is represented by a {@link Vector4} object, where the x, y, and z components
6
+ * define the unit vector normal to the plane, and the w component is the distance of the
7
+ * plane from the origin/camera position.
8
+ *
9
+ * @alias PerspectiveFrustum
10
+ * @constructor
11
+ *
12
+ * @param {Object} [options] An object with the following properties:
13
+ * @param {Number} [options.fov] The angle of the field of view (FOV), in radians.
14
+ * @param {Number} [options.aspectRatio] The aspect ratio of the frustum's width to it's height.
15
+ * @param {Number} [options.near=1.0] The distance of the near plane.
16
+ * @param {Number} [options.far=500000000.0] The distance of the far plane.
17
+ * @param {Number} [options.xOffset=0.0] The offset in the x direction.
18
+ * @param {Number} [options.yOffset=0.0] The offset in the y direction.
19
+ *
20
+ * @example
21
+ * var frustum = new PerspectiveFrustum({
22
+ * fov : Math.PI_OVER_THREE,
23
+ * aspectRatio : canvas.clientWidth / canvas.clientHeight
24
+ * near : 1.0,
25
+ * far : 1000.0
26
+ * });
27
+ *
28
+ * @see PerspectiveOffCenterFrustum
29
+ */
30
+ export default class PerspectiveFrustum {
31
+ _offCenterFrustum: PerspectiveOffCenterFrustum;
32
+ /**
33
+ * The angle of the field of view (FOV), in radians. This angle will be used
34
+ * as the horizontal FOV if the width is greater than the height, otherwise
35
+ * it will be the vertical FOV.
36
+ */
37
+ fov: number;
38
+ _fov: any;
39
+ _fovy: any;
40
+ _sseDenominator: any;
41
+ /**
42
+ * The aspect ratio of the frustum's width to it's height.
43
+ */
44
+ aspectRatio: number;
45
+ _aspectRatio: any;
46
+ /**
47
+ * The distance of the near plane.
48
+ * @default 1.0
49
+ */
50
+ near: number;
51
+ _near: any;
52
+ /**
53
+ * The distance of the far plane.
54
+ * @default 500000000.0
55
+ */
56
+ far: number;
57
+ _far: any;
58
+ /**
59
+ * Offsets the frustum in the x direction.
60
+ * @default 0.0
61
+ */
62
+ xOffset: number;
63
+ _xOffset: any;
64
+ /**
65
+ * Offsets the frustum in the y direction.
66
+ * @default 0.0
67
+ */
68
+ yOffset: number;
69
+ _yOffset: any;
70
+ constructor(options?: Record<string, any>);
71
+ /**
72
+ * Returns a duplicate of a PerspectiveFrustum instance.
73
+ */
74
+ clone(): PerspectiveFrustum;
75
+ /**
76
+ * Compares the provided PerspectiveFrustum componentwise and returns
77
+ * <code>true</code> if they are equal, <code>false</code> otherwise.
78
+ */
79
+ equals(other: PerspectiveFrustum): boolean;
80
+ /**
81
+ * Gets the perspective projection matrix computed from the view frustum.
82
+ */
83
+ get projectionMatrix(): Matrix4;
84
+ /**
85
+ * The perspective projection matrix computed from the view frustum with an infinite far plane.
86
+ */
87
+ get infiniteProjectionMatrix(): Matrix4;
88
+ /**
89
+ * Gets the angle of the vertical field of view, in radians.
90
+ */
91
+ get fovy(): number;
92
+ /**
93
+ * @private
94
+ */
95
+ get sseDenominator(): number;
96
+ /**
97
+ * Creates a culling volume for this frustum.
98
+ *
99
+ * @param {Vector3} position The eye position.
100
+ * @param {Vector3} direction The view direction.
101
+ * @param {Vector3} up The up direction.
102
+ * @returns {CullingVolume} A culling volume at the given position and orientation.
103
+ *
104
+ * @example
105
+ * // Check if a bounding volume intersects the frustum.
106
+ * var cullingVolume = frustum.computeCullingVolume(cameraPosition, cameraDirection, cameraUp);
107
+ * var intersect = cullingVolume.computeVisibility(boundingVolume);
108
+ */
109
+ computeCullingVolume(position: any, direction: any, up: any): import("./culling-volume").default;
110
+ /**
111
+ * Returns the pixel's width and height in meters.
112
+ *
113
+ * @param {Number} drawingBufferWidth The width of the drawing buffer.
114
+ * @param {Number} drawingBufferHeight The height of the drawing buffer.
115
+ * @param {Number} distance The distance to the near plane in meters.
116
+ * @param {Vector2} result The object onto which to store the result.
117
+ * @returns {Vector2} The modified result parameter or a new instance of {@link Vector2} with the pixel's width and height in the x and y properties, respectively.
118
+ *
119
+ * @exception {DeveloperError} drawingBufferWidth must be greater than zero.
120
+ * @exception {DeveloperError} drawingBufferHeight must be greater than zero.
121
+ *
122
+ * @example
123
+ * // Example 1
124
+ * // Get the width and height of a pixel.
125
+ * var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 1.0, new Vector2());
126
+ *
127
+ * @example
128
+ * // Example 2
129
+ * // Get the width and height of a pixel if the near plane was set to 'distance'.
130
+ * // For example, get the size of a pixel of an image on a billboard.
131
+ * var position = camera.position;
132
+ * var direction = camera.direction;
133
+ * var toCenter = Vector3.subtract(primitive.boundingVolume.center, position, new Vector3()); // vector from camera to a primitive
134
+ * var toCenterProj = Vector3.multiplyByScalar(direction, Vector3.dot(direction, toCenter), new Vector3()); // project vector onto camera direction vector
135
+ * var distance = Vector3.magnitude(toCenterProj);
136
+ * var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, distance, new Vector2());
137
+ */
138
+ getPixelDimensions(drawingBufferWidth: any, drawingBufferHeight: any, distance: any, result: any): any;
139
+ }
140
+ //# sourceMappingURL=perspective-frustum.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"perspective-frustum.d.ts","sourceRoot":"","sources":["../../src/lib/perspective-frustum.ts"],"names":[],"mappings":"AASA,OAAO,EAAS,OAAO,EAAC,MAAM,eAAe,CAAC;AAC9C,OAAO,2BAA2B,MAAM,kCAAkC,CAAC;AAI3E;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,CAAC,OAAO,OAAO,kBAAkB;IACrC,iBAAiB,8BAAqC;IACtD;;;;OAIG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,MAAC;IACL,KAAK,MAAC;IACN,eAAe,MAAC;IAChB;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,MAAC;IACb;;;OAGG;IACH,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,MAAC;IACN;;;OAGG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,MAAC;IACL;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,MAAC;IACT;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,MAAC;gBAEG,OAAO,GAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAM;IA0B7C;;OAEG;IACH,KAAK,IAAI,kBAAkB;IAS3B;;;OAGG;IACH,MAAM,CAAC,KAAK,EAAE,kBAAkB,GAAG,OAAO;IAiB1C;;OAEG;IACH,IAAI,gBAAgB,IAAI,OAAO,CAG9B;IAED;;OAEG;IACH,IAAI,wBAAwB,IAAI,OAAO,CAGtC;IAED;;OAEG;IACH,IAAI,IAAI,IAAI,MAAM,CAGjB;IAED;;OAEG;IACH,IAAI,cAAc,IAAI,MAAM,CAG3B;IAED;;;;;;;;;;;;OAYG;IACH,oBAAoB,CAAC,QAAQ,KAAA,EAAE,SAAS,KAAA,EAAE,EAAE,KAAA;IAK5C;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACH,kBAAkB,CAAC,kBAAkB,KAAA,EAAE,mBAAmB,KAAA,EAAE,QAAQ,KAAA,EAAE,MAAM,KAAA;CAS7E"}