@math.gl/culling 4.1.0 → 4.2.0-alpha.5

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 (54) hide show
  1. package/LICENSE +51 -0
  2. package/README.md +8 -1
  3. package/dist/index.cjs +660 -83
  4. package/dist/index.cjs.map +3 -4
  5. package/dist/index.d.ts +12 -0
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +6 -0
  8. package/dist/index.js.map +1 -1
  9. package/dist/lib/bounding-volumes/oriented-bounding-box.d.ts +1 -1
  10. package/dist/lib/bounding-volumes/oriented-bounding-box.d.ts.map +1 -1
  11. package/dist/lib/bounding-volumes/oriented-bounding-box.js.map +1 -1
  12. package/dist/lib/plane.d.ts +9 -0
  13. package/dist/lib/plane.d.ts.map +1 -1
  14. package/dist/lib/plane.js +22 -1
  15. package/dist/lib/plane.js.map +1 -1
  16. package/dist/lib/ray.d.ts +13 -0
  17. package/dist/lib/ray.d.ts.map +1 -0
  18. package/dist/lib/ray.js +28 -0
  19. package/dist/lib/ray.js.map +1 -0
  20. package/dist/lib/shapes/box-shape.d.ts +19 -0
  21. package/dist/lib/shapes/box-shape.d.ts.map +1 -0
  22. package/dist/lib/shapes/box-shape.js +69 -0
  23. package/dist/lib/shapes/box-shape.js.map +1 -0
  24. package/dist/lib/shapes/capsule-shape.d.ts +23 -0
  25. package/dist/lib/shapes/capsule-shape.d.ts.map +1 -0
  26. package/dist/lib/shapes/capsule-shape.js +151 -0
  27. package/dist/lib/shapes/capsule-shape.js.map +1 -0
  28. package/dist/lib/shapes/cylinder-shape.d.ts +22 -0
  29. package/dist/lib/shapes/cylinder-shape.d.ts.map +1 -0
  30. package/dist/lib/shapes/cylinder-shape.js +99 -0
  31. package/dist/lib/shapes/cylinder-shape.js.map +1 -0
  32. package/dist/lib/shapes/implicit-shape.d.ts +55 -0
  33. package/dist/lib/shapes/implicit-shape.d.ts.map +1 -0
  34. package/dist/lib/shapes/implicit-shape.js +143 -0
  35. package/dist/lib/shapes/implicit-shape.js.map +1 -0
  36. package/dist/lib/shapes/plane-shape.d.ts +26 -0
  37. package/dist/lib/shapes/plane-shape.d.ts.map +1 -0
  38. package/dist/lib/shapes/plane-shape.js +72 -0
  39. package/dist/lib/shapes/plane-shape.js.map +1 -0
  40. package/dist/lib/shapes/sphere-shape.d.ts +18 -0
  41. package/dist/lib/shapes/sphere-shape.d.ts.map +1 -0
  42. package/dist/lib/shapes/sphere-shape.js +43 -0
  43. package/dist/lib/shapes/sphere-shape.js.map +1 -0
  44. package/package.json +4 -4
  45. package/src/index.ts +13 -0
  46. package/src/lib/bounding-volumes/oriented-bounding-box.ts +2 -1
  47. package/src/lib/plane.ts +28 -1
  48. package/src/lib/ray.ts +31 -0
  49. package/src/lib/shapes/box-shape.ts +78 -0
  50. package/src/lib/shapes/capsule-shape.ts +232 -0
  51. package/src/lib/shapes/cylinder-shape.ts +133 -0
  52. package/src/lib/shapes/implicit-shape.ts +209 -0
  53. package/src/lib/shapes/plane-shape.ts +78 -0
  54. package/src/lib/shapes/sphere-shape.ts +60 -0
@@ -0,0 +1,232 @@
1
+ // math.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {
6
+ closestDistanceToCircleArc2D,
7
+ closestDistanceToSegment2D,
8
+ ImplicitShapeBase,
9
+ solveQuadratic,
10
+ type ImplicitShape,
11
+ type LocalRayIntersection
12
+ } from './implicit-shape';
13
+
14
+ const EPSILON = 1e-6;
15
+
16
+ export type CapsuleShapeProps = {
17
+ height?: number;
18
+ radiusBottom?: number;
19
+ radiusTop?: number;
20
+ matrix?: readonly number[];
21
+ };
22
+
23
+ /** Analytic glTF capsule: the convex hull of two spheres on the Y axis. */
24
+ export class CapsuleShape extends ImplicitShapeBase {
25
+ readonly type = 'capsule' as const;
26
+ readonly height: number;
27
+ readonly radiusBottom: number;
28
+ readonly radiusTop: number;
29
+
30
+ constructor(props: CapsuleShapeProps = {}) {
31
+ super(props.matrix);
32
+ this.height = props.height ?? 1;
33
+ this.radiusBottom = props.radiusBottom ?? 0.5;
34
+ this.radiusTop = props.radiusTop ?? 0.5;
35
+ if (!Number.isFinite(this.height) || this.height <= 0)
36
+ throw new Error('Capsule height must be greater than zero');
37
+ if (![this.radiusBottom, this.radiusTop].every(value => Number.isFinite(value) && value >= 0))
38
+ throw new Error('Capsule radii must be non-negative');
39
+ if (this.radiusBottom === 0 && this.radiusTop === 0)
40
+ throw new Error('At least one capsule radius must be greater than zero');
41
+ }
42
+
43
+ clone(): CapsuleShape {
44
+ return new CapsuleShape({
45
+ height: this.height,
46
+ radiusBottom: this.radiusBottom,
47
+ radiusTop: this.radiusTop,
48
+ matrix: this.matrix
49
+ });
50
+ }
51
+ override equals(shape: ImplicitShape): boolean {
52
+ return (
53
+ shape instanceof CapsuleShape &&
54
+ super.equals(shape) &&
55
+ this.height === shape.height &&
56
+ this.radiusBottom === shape.radiusBottom &&
57
+ this.radiusTop === shape.radiusTop
58
+ );
59
+ }
60
+ containsLocalPoint(point: readonly number[]): boolean {
61
+ const profile = this.getProfile();
62
+ const radial = Math.hypot(point[0], point[2]);
63
+ if (profile.sphere)
64
+ return (
65
+ Math.hypot(radial, point[1] - profile.sphere.center) <= profile.sphere.radius + EPSILON
66
+ );
67
+ if (point[1] < profile.bottom.y)
68
+ return Math.hypot(radial, point[1] + this.height / 2) <= this.radiusBottom + EPSILON;
69
+ if (point[1] > profile.top.y)
70
+ return Math.hypot(radial, point[1] - this.height / 2) <= this.radiusTop + EPSILON;
71
+ const t = (point[1] - profile.bottom.y) / (profile.top.y - profile.bottom.y);
72
+ return (
73
+ radial <= profile.bottom.radius + (profile.top.radius - profile.bottom.radius) * t + EPSILON
74
+ );
75
+ }
76
+ distanceToLocalPoint(point: readonly number[]): number {
77
+ if (this.containsLocalPoint(point)) return 0;
78
+ const profile = this.getProfile();
79
+ const radial = Math.hypot(point[0], point[2]);
80
+ if (profile.sphere)
81
+ return Math.max(
82
+ 0,
83
+ Math.hypot(radial, point[1] - profile.sphere.center) - profile.sphere.radius
84
+ );
85
+ const delta = this.radiusTop - this.radiusBottom;
86
+ const sideNormalY = -delta / this.height;
87
+ const bottomArc = closestDistanceToCircleArc2D(
88
+ radial,
89
+ point[1],
90
+ -this.height / 2,
91
+ this.radiusBottom,
92
+ -1,
93
+ sideNormalY
94
+ );
95
+ const topArc = closestDistanceToCircleArc2D(
96
+ radial,
97
+ point[1],
98
+ this.height / 2,
99
+ this.radiusTop,
100
+ sideNormalY,
101
+ 1
102
+ );
103
+ const side = closestDistanceToSegment2D(
104
+ radial,
105
+ point[1],
106
+ profile.bottom.radius,
107
+ profile.bottom.y,
108
+ profile.top.radius,
109
+ profile.top.y
110
+ );
111
+ return Math.min(bottomArc, topArc, side);
112
+ }
113
+ supportLocal(direction: readonly number[]): readonly number[] {
114
+ const length = Math.hypot(...direction);
115
+ const unit = length ? direction.map(value => value / length) : [1, 0, 0];
116
+ const bottom = [
117
+ unit[0] * this.radiusBottom,
118
+ -this.height / 2 + unit[1] * this.radiusBottom,
119
+ unit[2] * this.radiusBottom
120
+ ];
121
+ const top = [
122
+ unit[0] * this.radiusTop,
123
+ this.height / 2 + unit[1] * this.radiusTop,
124
+ unit[2] * this.radiusTop
125
+ ];
126
+ return dot(top, direction) >= dot(bottom, direction) ? top : bottom;
127
+ }
128
+ intersectLocalRay(
129
+ origin: readonly number[],
130
+ direction: readonly number[]
131
+ ): LocalRayIntersection | undefined {
132
+ const profile = this.getProfile();
133
+ if (profile.sphere)
134
+ return intersectSphere(origin, direction, profile.sphere.center, profile.sphere.radius);
135
+ const candidates: LocalRayIntersection[] = [];
136
+ const slope = (profile.top.radius - profile.bottom.radius) / (profile.top.y - profile.bottom.y);
137
+ const intercept = profile.bottom.radius - slope * profile.bottom.y;
138
+ const radialAtOrigin = intercept + slope * origin[1];
139
+ const radialAlongRay = slope * direction[1];
140
+ for (const distance of solveQuadratic(
141
+ direction[0] ** 2 + direction[2] ** 2 - radialAlongRay ** 2,
142
+ 2 * (origin[0] * direction[0] + origin[2] * direction[2] - radialAtOrigin * radialAlongRay),
143
+ origin[0] ** 2 + origin[2] ** 2 - radialAtOrigin ** 2
144
+ )) {
145
+ const y = origin[1] + distance * direction[1];
146
+ if (distance >= 0 && y >= profile.bottom.y - EPSILON && y <= profile.top.y + EPSILON) {
147
+ const x = origin[0] + distance * direction[0];
148
+ const z = origin[2] + distance * direction[2];
149
+ const radial = Math.hypot(x, z) || 1;
150
+ const normal = [x / radial, -slope, z / radial];
151
+ const length = Math.hypot(...normal);
152
+ candidates.push({distance, normal: normal.map(value => value / length)});
153
+ }
154
+ }
155
+ for (const bottomHit of intersectSphereRoots(
156
+ origin,
157
+ direction,
158
+ -this.height / 2,
159
+ this.radiusBottom
160
+ )) {
161
+ const y = origin[1] + bottomHit.distance * direction[1];
162
+ if (y <= profile.bottom.y + EPSILON) candidates.push(bottomHit);
163
+ }
164
+ for (const topHit of intersectSphereRoots(origin, direction, this.height / 2, this.radiusTop)) {
165
+ const y = origin[1] + topHit.distance * direction[1];
166
+ if (y >= profile.top.y - EPSILON) candidates.push(topHit);
167
+ }
168
+ return candidates.sort((a, b) => a.distance - b.distance)[0];
169
+ }
170
+
171
+ private getProfile(): {
172
+ sphere?: {center: number; radius: number};
173
+ bottom: {radius: number; y: number};
174
+ top: {radius: number; y: number};
175
+ } {
176
+ const delta = this.radiusTop - this.radiusBottom;
177
+ if (Math.abs(delta) >= this.height) {
178
+ const topWins = this.radiusTop >= this.radiusBottom;
179
+ return {
180
+ sphere: {
181
+ center: topWins ? this.height / 2 : -this.height / 2,
182
+ radius: topWins ? this.radiusTop : this.radiusBottom
183
+ },
184
+ bottom: {radius: 0, y: 0},
185
+ top: {radius: 0, y: 0}
186
+ };
187
+ }
188
+ const sinSlope = delta / this.height;
189
+ const radialNormal = Math.sqrt(1 - sinSlope * sinSlope);
190
+ const normalY = -sinSlope;
191
+ return {
192
+ bottom: {
193
+ radius: this.radiusBottom * radialNormal,
194
+ y: -this.height / 2 + this.radiusBottom * normalY
195
+ },
196
+ top: {radius: this.radiusTop * radialNormal, y: this.height / 2 + this.radiusTop * normalY}
197
+ };
198
+ }
199
+ }
200
+
201
+ function intersectSphere(
202
+ origin: readonly number[],
203
+ direction: readonly number[],
204
+ centerY: number,
205
+ radius: number
206
+ ): LocalRayIntersection | undefined {
207
+ return intersectSphereRoots(origin, direction, centerY, radius)[0];
208
+ }
209
+
210
+ function intersectSphereRoots(
211
+ origin: readonly number[],
212
+ direction: readonly number[],
213
+ centerY: number,
214
+ radius: number
215
+ ): LocalRayIntersection[] {
216
+ if (radius === 0) return [];
217
+ const relative = [origin[0], origin[1] - centerY, origin[2]];
218
+ return solveQuadratic(
219
+ dot(direction, direction),
220
+ 2 * dot(relative, direction),
221
+ dot(relative, relative) - radius * radius
222
+ )
223
+ .filter(distance => distance >= 0)
224
+ .map(distance => ({
225
+ distance,
226
+ normal: relative.map((value, i) => (value + distance * direction[i]) / radius)
227
+ }));
228
+ }
229
+
230
+ function dot(a: readonly number[], b: readonly number[]): number {
231
+ return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
232
+ }
@@ -0,0 +1,133 @@
1
+ // math.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {
6
+ closestDistanceToSegment2D,
7
+ ImplicitShapeBase,
8
+ solveQuadratic,
9
+ type ImplicitShape,
10
+ type LocalRayIntersection
11
+ } from './implicit-shape';
12
+
13
+ const EPSILON = 1e-6;
14
+
15
+ export type CylinderShapeProps = {
16
+ height?: number;
17
+ radiusBottom?: number;
18
+ radiusTop?: number;
19
+ matrix?: readonly number[];
20
+ };
21
+
22
+ /** Analytic closed, Y-aligned glTF cylinder shape, including tapered cylinders. */
23
+ export class CylinderShape extends ImplicitShapeBase {
24
+ readonly type = 'cylinder' as const;
25
+ readonly height: number;
26
+ readonly radiusBottom: number;
27
+ readonly radiusTop: number;
28
+
29
+ constructor(props: CylinderShapeProps = {}) {
30
+ super(props.matrix);
31
+ this.height = props.height ?? 2;
32
+ this.radiusBottom = props.radiusBottom ?? 0.5;
33
+ this.radiusTop = props.radiusTop ?? 0.5;
34
+ if (!Number.isFinite(this.height) || this.height <= 0)
35
+ throw new Error('Cylinder height must be greater than zero');
36
+ if (![this.radiusBottom, this.radiusTop].every(value => Number.isFinite(value) && value >= 0))
37
+ throw new Error('Cylinder radii must be non-negative');
38
+ if (this.radiusBottom === 0 && this.radiusTop === 0)
39
+ throw new Error('At least one cylinder radius must be greater than zero');
40
+ }
41
+
42
+ clone(): CylinderShape {
43
+ return new CylinderShape({
44
+ height: this.height,
45
+ radiusBottom: this.radiusBottom,
46
+ radiusTop: this.radiusTop,
47
+ matrix: this.matrix
48
+ });
49
+ }
50
+ override equals(shape: ImplicitShape): boolean {
51
+ return (
52
+ shape instanceof CylinderShape &&
53
+ super.equals(shape) &&
54
+ this.height === shape.height &&
55
+ this.radiusBottom === shape.radiusBottom &&
56
+ this.radiusTop === shape.radiusTop
57
+ );
58
+ }
59
+ containsLocalPoint(point: readonly number[]): boolean {
60
+ const half = this.height / 2;
61
+ if (point[1] < -half - EPSILON || point[1] > half + EPSILON) return false;
62
+ const t = (point[1] + half) / this.height;
63
+ const radius = this.radiusBottom + (this.radiusTop - this.radiusBottom) * t;
64
+ return Math.hypot(point[0], point[2]) <= radius + EPSILON;
65
+ }
66
+ distanceToLocalPoint(point: readonly number[]): number {
67
+ if (this.containsLocalPoint(point)) return 0;
68
+ const radial = Math.hypot(point[0], point[2]);
69
+ const half = this.height / 2;
70
+ const side = closestDistanceToSegment2D(
71
+ radial,
72
+ point[1],
73
+ this.radiusBottom,
74
+ -half,
75
+ this.radiusTop,
76
+ half
77
+ );
78
+ const bottom = closestDistanceToSegment2D(radial, point[1], 0, -half, this.radiusBottom, -half);
79
+ const top = closestDistanceToSegment2D(radial, point[1], 0, half, this.radiusTop, half);
80
+ return Math.min(side, bottom, top);
81
+ }
82
+ supportLocal(direction: readonly number[]): readonly number[] {
83
+ const radialLength = Math.hypot(direction[0], direction[2]);
84
+ const x = radialLength ? direction[0] / radialLength : 1;
85
+ const z = radialLength ? direction[2] / radialLength : 0;
86
+ const half = this.height / 2;
87
+ const bottomValue = radialLength * this.radiusBottom - direction[1] * half;
88
+ const topValue = radialLength * this.radiusTop + direction[1] * half;
89
+ return topValue >= bottomValue
90
+ ? [x * this.radiusTop, half, z * this.radiusTop]
91
+ : [x * this.radiusBottom, -half, z * this.radiusBottom];
92
+ }
93
+ intersectLocalRay(
94
+ origin: readonly number[],
95
+ direction: readonly number[]
96
+ ): LocalRayIntersection | undefined {
97
+ const half = this.height / 2;
98
+ const slope = (this.radiusTop - this.radiusBottom) / this.height;
99
+ const intercept = (this.radiusTop + this.radiusBottom) / 2;
100
+ const radialAtOrigin = intercept + slope * origin[1];
101
+ const radialAlongRay = slope * direction[1];
102
+ const candidates: LocalRayIntersection[] = [];
103
+ const roots = solveQuadratic(
104
+ direction[0] ** 2 + direction[2] ** 2 - radialAlongRay ** 2,
105
+ 2 * (origin[0] * direction[0] + origin[2] * direction[2] - radialAtOrigin * radialAlongRay),
106
+ origin[0] ** 2 + origin[2] ** 2 - radialAtOrigin ** 2
107
+ );
108
+ for (const distance of roots) {
109
+ const y = origin[1] + distance * direction[1];
110
+ if (distance >= 0 && y >= -half - EPSILON && y <= half + EPSILON) {
111
+ const x = origin[0] + distance * direction[0];
112
+ const z = origin[2] + distance * direction[2];
113
+ const radial = Math.hypot(x, z) || 1;
114
+ const normal = [x / radial, -slope, z / radial];
115
+ const length = Math.hypot(...normal);
116
+ candidates.push({distance, normal: normal.map(value => value / length)});
117
+ }
118
+ }
119
+ if (Math.abs(direction[1]) > 1e-15) {
120
+ for (const [y, radius, normalY] of [
121
+ [-half, this.radiusBottom, -1],
122
+ [half, this.radiusTop, 1]
123
+ ]) {
124
+ const distance = (y - origin[1]) / direction[1];
125
+ const x = origin[0] + distance * direction[0];
126
+ const z = origin[2] + distance * direction[2];
127
+ if (distance >= 0 && Math.hypot(x, z) <= radius + EPSILON)
128
+ candidates.push({distance, normal: [0, normalY, 0]});
129
+ }
130
+ }
131
+ return candidates.sort((a, b) => a.distance - b.distance)[0];
132
+ }
133
+ }
@@ -0,0 +1,209 @@
1
+ // math.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {Matrix4, Vector3} from '@math.gl/core';
6
+ import {INTERSECTION} from '../../constants';
7
+ import {AxisAlignedBoundingBox} from '../bounding-volumes/axis-aligned-bounding-box';
8
+ import {BoundingSphere} from '../bounding-volumes/bounding-sphere';
9
+ import type {BoundingVolume} from '../bounding-volumes/bounding-volume';
10
+ import type {Plane} from '../plane';
11
+ import type {Ray} from '../ray';
12
+
13
+ export type RayIntersection = {
14
+ distance: number;
15
+ point: Vector3;
16
+ normal: Vector3;
17
+ entering: boolean;
18
+ };
19
+
20
+ export interface ImplicitShape extends BoundingVolume {
21
+ readonly type: 'box' | 'capsule' | 'cylinder' | 'plane' | 'sphere';
22
+ readonly matrix: Matrix4;
23
+ clone(): ImplicitShape;
24
+ equals(shape: ImplicitShape): boolean;
25
+ containsPoint(point: readonly number[]): boolean;
26
+ intersectRay(ray: Ray): RayIntersection | undefined;
27
+ getAxisAlignedBoundingBox(): AxisAlignedBoundingBox | undefined;
28
+ getBoundingSphere(): BoundingSphere | undefined;
29
+ }
30
+
31
+ export type LocalRayIntersection = {distance: number; normal: readonly number[]};
32
+
33
+ /** Shared affine transform, support mapping and query implementation for finite convex shapes. */
34
+ export abstract class ImplicitShapeBase implements ImplicitShape {
35
+ abstract readonly type: ImplicitShape['type'];
36
+ readonly matrix: Matrix4;
37
+ protected inverseMatrix: Matrix4;
38
+
39
+ constructor(matrix?: readonly number[]) {
40
+ this.matrix = new Matrix4();
41
+ if (matrix) this.matrix.copy(matrix);
42
+ validateAffineMatrix(this.matrix);
43
+ this.inverseMatrix = this.matrix.clone().invert();
44
+ }
45
+
46
+ abstract clone(): ImplicitShapeBase;
47
+ abstract containsLocalPoint(point: readonly number[]): boolean;
48
+ abstract distanceToLocalPoint(point: readonly number[]): number;
49
+ abstract supportLocal(direction: readonly number[]): readonly number[];
50
+ abstract intersectLocalRay(
51
+ origin: readonly number[],
52
+ direction: readonly number[]
53
+ ): LocalRayIntersection | undefined;
54
+
55
+ equals(shape: ImplicitShape): boolean {
56
+ if (this.type !== shape.type) return false;
57
+ for (let i = 0; i < 16; i++) if (this.matrix[i] !== shape.matrix[i]) return false;
58
+ return true;
59
+ }
60
+
61
+ transform(transform: readonly number[]): this {
62
+ const next = new Matrix4().copy(transform);
63
+ validateAffineMatrix(next);
64
+ this.matrix.multiplyLeft(next);
65
+ validateAffineMatrix(this.matrix);
66
+ this.inverseMatrix.copy(this.matrix).invert();
67
+ return this;
68
+ }
69
+
70
+ containsPoint(point: readonly number[]): boolean {
71
+ return this.containsLocalPoint(this.inverseMatrix.transformAsPoint(point) as readonly number[]);
72
+ }
73
+
74
+ distanceSquaredTo(point: readonly number[]): number {
75
+ const distance = this.distanceTo(point);
76
+ return distance * distance;
77
+ }
78
+
79
+ /** Exact for rigid/uniform transforms; conservative estimate for non-uniform scale or shear. */
80
+ distanceTo(point: readonly number[]): number {
81
+ const localPoint = this.inverseMatrix.transformAsPoint(point);
82
+ const localDistance = this.distanceToLocalPoint(localPoint as readonly number[]);
83
+ const scales = this.matrix.getScale();
84
+ return localDistance * Math.min(scales[0], scales[1], scales[2]);
85
+ }
86
+
87
+ intersectPlane(plane: Plane): number {
88
+ const maximum = this.getSupportPoint(plane.normal);
89
+ const minimum = this.getSupportPoint([-plane.normal[0], -plane.normal[1], -plane.normal[2]]);
90
+ const maxDistance = plane.getPointDistance(maximum);
91
+ const minDistance = plane.getPointDistance(minimum);
92
+ if (minDistance > 0) return INTERSECTION.INSIDE;
93
+ if (maxDistance < 0) return INTERSECTION.OUTSIDE;
94
+ return INTERSECTION.INTERSECTING;
95
+ }
96
+
97
+ intersectRay(ray: Ray): RayIntersection | undefined {
98
+ const localOrigin = this.inverseMatrix.transformAsPoint(ray.origin);
99
+ const localDirection = this.inverseMatrix.transformAsVector(ray.direction);
100
+ const hit = this.intersectLocalRay(
101
+ localOrigin as readonly number[],
102
+ localDirection as readonly number[]
103
+ );
104
+ if (!hit || hit.distance < 0) return undefined;
105
+ const point = new Vector3(ray.direction).scale(hit.distance).add(ray.origin);
106
+ const inverse = this.inverseMatrix;
107
+ const normal = new Vector3(
108
+ inverse[0] * hit.normal[0] + inverse[1] * hit.normal[1] + inverse[2] * hit.normal[2],
109
+ inverse[4] * hit.normal[0] + inverse[5] * hit.normal[1] + inverse[6] * hit.normal[2],
110
+ inverse[8] * hit.normal[0] + inverse[9] * hit.normal[1] + inverse[10] * hit.normal[2]
111
+ ).normalize();
112
+ return {distance: hit.distance, point, normal, entering: ray.direction.dot(normal) < 0};
113
+ }
114
+
115
+ getAxisAlignedBoundingBox(): AxisAlignedBoundingBox | undefined {
116
+ const minimum = new Vector3();
117
+ const maximum = new Vector3();
118
+ for (let axis = 0; axis < 3; axis++) {
119
+ const direction = [0, 0, 0];
120
+ direction[axis] = 1;
121
+ maximum[axis] = this.getSupportPoint(direction)[axis];
122
+ direction[axis] = -1;
123
+ minimum[axis] = this.getSupportPoint(direction)[axis];
124
+ }
125
+ return new AxisAlignedBoundingBox(minimum, maximum);
126
+ }
127
+
128
+ getBoundingSphere(): BoundingSphere | undefined {
129
+ const box = this.getAxisAlignedBoundingBox();
130
+ if (!box) return undefined;
131
+ return new BoundingSphere().fromCornerPoints(box.minimum, box.maximum);
132
+ }
133
+
134
+ protected getSupportPoint(worldDirection: readonly number[]): Vector3 {
135
+ const matrix = this.matrix;
136
+ const localDirection = [
137
+ matrix[0] * worldDirection[0] + matrix[1] * worldDirection[1] + matrix[2] * worldDirection[2],
138
+ matrix[4] * worldDirection[0] + matrix[5] * worldDirection[1] + matrix[6] * worldDirection[2],
139
+ matrix[8] * worldDirection[0] + matrix[9] * worldDirection[1] + matrix[10] * worldDirection[2]
140
+ ];
141
+ return new Vector3(this.supportLocal(localDirection)).transform(this.matrix);
142
+ }
143
+ }
144
+
145
+ export function validateAffineMatrix(matrix: readonly number[]): void {
146
+ if (matrix.length !== 16 || matrix.some(value => !Number.isFinite(value))) {
147
+ throw new Error('Shape matrix must contain 16 finite values');
148
+ }
149
+ if (
150
+ Math.abs(matrix[3]) > 1e-12 ||
151
+ Math.abs(matrix[7]) > 1e-12 ||
152
+ Math.abs(matrix[11]) > 1e-12 ||
153
+ Math.abs(matrix[15] - 1) > 1e-12
154
+ ) {
155
+ throw new Error('Shape matrix must be affine');
156
+ }
157
+ if (Math.abs(new Matrix4().copy(matrix).determinant()) < 1e-15)
158
+ throw new Error('Shape matrix must be invertible');
159
+ }
160
+
161
+ export function solveQuadratic(a: number, b: number, c: number): number[] {
162
+ if (Math.abs(a) < 1e-15) return Math.abs(b) < 1e-15 ? [] : [-c / b];
163
+ const discriminant = b * b - 4 * a * c;
164
+ if (discriminant < 0) return [];
165
+ const root = Math.sqrt(Math.max(0, discriminant));
166
+ return [(-b - root) / (2 * a), (-b + root) / (2 * a)].sort((left, right) => left - right);
167
+ }
168
+
169
+ export function closestDistanceToSegment2D(
170
+ px: number,
171
+ py: number,
172
+ ax: number,
173
+ ay: number,
174
+ bx: number,
175
+ by: number
176
+ ): number {
177
+ const dx = bx - ax;
178
+ const dy = by - ay;
179
+ const denominator = dx * dx + dy * dy;
180
+ const t = denominator
181
+ ? Math.max(0, Math.min(1, ((px - ax) * dx + (py - ay) * dy) / denominator))
182
+ : 0;
183
+ return Math.hypot(px - (ax + t * dx), py - (ay + t * dy));
184
+ }
185
+
186
+ /** Distance in the radial/Y half-plane to a spherical profile arc selected by normal Y range. */
187
+ export function closestDistanceToCircleArc2D(
188
+ px: number,
189
+ py: number,
190
+ centerY: number,
191
+ radius: number,
192
+ minimumNormalY: number,
193
+ maximumNormalY: number
194
+ ): number {
195
+ if (radius === 0) return Math.hypot(px, py - centerY);
196
+ const dx = px;
197
+ const dy = py - centerY;
198
+ const length = Math.hypot(dx, dy);
199
+ const normalY = length ? dy / length : 0;
200
+ if (normalY >= minimumNormalY && normalY <= maximumNormalY) {
201
+ return Math.abs(length - radius);
202
+ }
203
+ const minimumRadius = radius * Math.sqrt(Math.max(0, 1 - minimumNormalY ** 2));
204
+ const maximumRadius = radius * Math.sqrt(Math.max(0, 1 - maximumNormalY ** 2));
205
+ return Math.min(
206
+ Math.hypot(px - minimumRadius, py - (centerY + radius * minimumNormalY)),
207
+ Math.hypot(px - maximumRadius, py - (centerY + radius * maximumNormalY))
208
+ );
209
+ }
@@ -0,0 +1,78 @@
1
+ // math.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {Vector3} from '@math.gl/core';
6
+ import {INTERSECTION} from '../../constants';
7
+ import {ImplicitShapeBase, type ImplicitShape, type LocalRayIntersection} from './implicit-shape';
8
+ import type {AxisAlignedBoundingBox} from '../bounding-volumes/axis-aligned-bounding-box';
9
+ import type {BoundingSphere} from '../bounding-volumes/bounding-sphere';
10
+ import type {Plane} from '../plane';
11
+
12
+ export type PlaneShapeProps = {sizeX?: number; sizeZ?: number; matrix?: readonly number[]};
13
+
14
+ /** Analytic glTF plane. The surface faces +Y and the negative-Y half-space is inside. */
15
+ export class PlaneShape extends ImplicitShapeBase {
16
+ readonly type = 'plane' as const;
17
+ readonly sizeX?: number;
18
+ readonly sizeZ?: number;
19
+ constructor(props: PlaneShapeProps = {}) {
20
+ super(props.matrix);
21
+ this.sizeX = props.sizeX;
22
+ this.sizeZ = props.sizeZ;
23
+ if (this.sizeX !== undefined && (!Number.isFinite(this.sizeX) || this.sizeX <= 0))
24
+ throw new Error('Plane sizeX must be greater than zero');
25
+ if (this.sizeZ !== undefined && (!Number.isFinite(this.sizeZ) || this.sizeZ <= 0))
26
+ throw new Error('Plane sizeZ must be greater than zero');
27
+ }
28
+ clone(): PlaneShape {
29
+ return new PlaneShape({sizeX: this.sizeX, sizeZ: this.sizeZ, matrix: this.matrix});
30
+ }
31
+ override equals(shape: ImplicitShape): boolean {
32
+ return (
33
+ shape instanceof PlaneShape &&
34
+ super.equals(shape) &&
35
+ this.sizeX === shape.sizeX &&
36
+ this.sizeZ === shape.sizeZ
37
+ );
38
+ }
39
+ containsLocalPoint(point: readonly number[]): boolean {
40
+ return point[1] <= 1e-12;
41
+ }
42
+ distanceToLocalPoint(point: readonly number[]): number {
43
+ return Math.max(0, point[1]);
44
+ }
45
+ supportLocal(): readonly number[] {
46
+ throw new Error('An unbounded plane half-space has no finite support point');
47
+ }
48
+ intersectLocalRay(
49
+ origin: readonly number[],
50
+ direction: readonly number[]
51
+ ): LocalRayIntersection | undefined {
52
+ if (Math.abs(direction[1]) < 1e-15) return undefined;
53
+ const distance = -origin[1] / direction[1];
54
+ if (distance < 0) return undefined;
55
+ const x = origin[0] + distance * direction[0];
56
+ const z = origin[2] + distance * direction[2];
57
+ if (this.sizeX !== undefined && Math.abs(x) > this.sizeX / 2 + 1e-12) return undefined;
58
+ if (this.sizeZ !== undefined && Math.abs(z) > this.sizeZ / 2 + 1e-12) return undefined;
59
+ return {distance, normal: [0, 1, 0]};
60
+ }
61
+ override intersectPlane(plane: Plane): number {
62
+ const boundaryPoint = new Vector3(0, 0, 0).transform(this.matrix);
63
+ const inverse = this.inverseMatrix;
64
+ const boundaryNormal = new Vector3(inverse[1], inverse[5], inverse[9]).normalize();
65
+ const alignment = boundaryNormal.dot(plane.normal);
66
+ if (Math.abs(Math.abs(alignment) - 1) > 1e-10) return INTERSECTION.INTERSECTING;
67
+ const boundaryDistance = plane.getPointDistance(boundaryPoint);
68
+ if (alignment < 0 && boundaryDistance > 0) return INTERSECTION.INSIDE;
69
+ if (alignment > 0 && boundaryDistance < 0) return INTERSECTION.OUTSIDE;
70
+ return INTERSECTION.INTERSECTING;
71
+ }
72
+ override getAxisAlignedBoundingBox(): AxisAlignedBoundingBox | undefined {
73
+ return undefined;
74
+ }
75
+ override getBoundingSphere(): BoundingSphere | undefined {
76
+ return undefined;
77
+ }
78
+ }