@math.gl/geometry 5.0.0-beta.4 → 5.0.0-beta.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -0
- package/THIRD-PARTY-NOTICES.md +1 -1
- package/dist/color/color-lut-types.d.ts +16 -0
- package/dist/color/color-lut-types.d.ts.map +1 -0
- package/dist/color/color-lut-types.js +5 -0
- package/dist/color/color-lut-types.js.map +1 -0
- package/dist/color/index.cjs +132 -0
- package/dist/color/index.cjs.map +6 -0
- package/dist/color/index.d.ts +4 -0
- package/dist/color/index.d.ts.map +1 -0
- package/dist/color/index.js +5 -0
- package/dist/color/index.js.map +1 -0
- package/dist/color/sample-lut.d.ts +13 -0
- package/dist/color/sample-lut.d.ts.map +1 -0
- package/dist/color/sample-lut.js +132 -0
- package/dist/color/sample-lut.js.map +1 -0
- package/dist/culling/bounds.d.ts +20 -0
- package/dist/culling/bounds.d.ts.map +1 -0
- package/dist/culling/bounds.js +121 -0
- package/dist/culling/bounds.js.map +1 -0
- package/dist/culling/frustum-planes.d.ts +5 -0
- package/dist/culling/frustum-planes.d.ts.map +1 -0
- package/dist/culling/frustum-planes.js +37 -0
- package/dist/culling/frustum-planes.js.map +1 -0
- package/dist/culling/geometric-queries.d.ts +34 -0
- package/dist/culling/geometric-queries.d.ts.map +1 -0
- package/dist/culling/geometric-queries.js +208 -0
- package/dist/culling/geometric-queries.js.map +1 -0
- package/dist/culling/index.cjs +917 -514
- package/dist/culling/index.cjs.map +3 -3
- package/dist/culling/index.d.ts +5 -1
- package/dist/culling/index.d.ts.map +1 -1
- package/dist/culling/index.js +3 -0
- package/dist/culling/index.js.map +1 -1
- package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.d.ts +3 -3
- package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.d.ts.map +1 -1
- package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.js +25 -8
- package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.js.map +1 -1
- package/dist/culling/lib/culling-volume.d.ts +3 -0
- package/dist/culling/lib/culling-volume.d.ts.map +1 -1
- package/dist/culling/lib/culling-volume.js +7 -0
- package/dist/culling/lib/culling-volume.js.map +1 -1
- package/dist/culling/lib/ray.d.ts +5 -0
- package/dist/culling/lib/ray.d.ts.map +1 -1
- package/dist/culling/lib/ray.js +21 -0
- package/dist/culling/lib/ray.js.map +1 -1
- package/dist/curves/adaptive-sampling.d.ts +16 -0
- package/dist/curves/adaptive-sampling.d.ts.map +1 -0
- package/dist/curves/adaptive-sampling.js +59 -0
- package/dist/curves/adaptive-sampling.js.map +1 -0
- package/dist/curves/index.d.ts +4 -0
- package/dist/curves/index.d.ts.map +1 -1
- package/dist/curves/index.js +2 -0
- package/dist/curves/index.js.map +1 -1
- package/dist/curves/parallel-transport.d.ts +12 -0
- package/dist/curves/parallel-transport.d.ts.map +1 -0
- package/dist/curves/parallel-transport.js +83 -0
- package/dist/curves/parallel-transport.js.map +1 -0
- package/dist/index.cjs +1336 -699
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +10 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/dist/lighting/index.cjs +175 -0
- package/dist/lighting/index.cjs.map +6 -0
- package/dist/lighting/index.d.ts +4 -0
- package/dist/lighting/index.d.ts.map +1 -0
- package/dist/lighting/index.js +5 -0
- package/dist/lighting/index.js.map +1 -0
- package/dist/lighting/lighting-types.d.ts +16 -0
- package/dist/lighting/lighting-types.d.ts.map +1 -0
- package/dist/lighting/lighting-types.js +5 -0
- package/dist/lighting/lighting-types.js.map +1 -0
- package/dist/lighting/photometry.d.ts +36 -0
- package/dist/lighting/photometry.d.ts.map +1 -0
- package/dist/lighting/photometry.js +181 -0
- package/dist/lighting/photometry.js.map +1 -0
- package/dist/utils/project-bounds.d.ts +44 -0
- package/dist/utils/project-bounds.d.ts.map +1 -0
- package/dist/utils/project-bounds.js +93 -0
- package/dist/utils/project-bounds.js.map +1 -0
- package/dist/utils/project-texture-coordinates.d.ts +19 -0
- package/dist/utils/project-texture-coordinates.d.ts.map +1 -0
- package/dist/utils/project-texture-coordinates.js +49 -0
- package/dist/utils/project-texture-coordinates.js.map +1 -0
- package/package.json +15 -3
- package/src/color/color-lut-types.ts +19 -0
- package/src/color/index.ts +6 -0
- package/src/color/sample-lut.ts +175 -0
- package/src/culling/bounds.ts +152 -0
- package/src/culling/frustum-planes.ts +41 -0
- package/src/culling/geometric-queries.ts +299 -0
- package/src/culling/index.ts +37 -1
- package/src/culling/lib/bounding-volumes/axis-aligned-bounding-box.ts +26 -8
- package/src/culling/lib/culling-volume.ts +9 -1
- package/src/culling/lib/ray.ts +21 -0
- package/src/curves/adaptive-sampling.ts +85 -0
- package/src/curves/index.ts +4 -0
- package/src/curves/parallel-transport.ts +104 -0
- package/src/index.ts +44 -0
- package/src/lighting/index.ts +10 -0
- package/src/lighting/lighting-types.ts +20 -0
- package/src/lighting/photometry.ts +264 -0
- package/src/utils/project-bounds.ts +154 -0
- package/src/utils/project-texture-coordinates.ts +67 -0
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
import type {NumericArray} from '@math.gl/core';
|
|
5
|
+
import {validateQueryPoint, normalizeQueryDirection, type QueryPoint} from './queries';
|
|
6
|
+
|
|
7
|
+
type V = [number, number, number];
|
|
8
|
+
const sub = (a: QueryPoint, b: QueryPoint): V => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
|
|
9
|
+
const dot = (a: QueryPoint, b: QueryPoint) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
10
|
+
const cross = (a: QueryPoint, b: QueryPoint): V => [
|
|
11
|
+
a[1] * b[2] - a[2] * b[1],
|
|
12
|
+
a[2] * b[0] - a[0] * b[2],
|
|
13
|
+
a[0] * b[1] - a[1] * b[0]
|
|
14
|
+
];
|
|
15
|
+
const check = (...points: QueryPoint[]) => {
|
|
16
|
+
for (const p of points) validateQueryPoint(p, 3);
|
|
17
|
+
};
|
|
18
|
+
function output<T extends NumericArray>(out: T, values: QueryPoint): T {
|
|
19
|
+
if (out.length < values.length) throw new RangeError('Result buffer is too short');
|
|
20
|
+
const copy = Array.from(values);
|
|
21
|
+
for (let i = 0; i < copy.length; i++) out[i] = copy[i];
|
|
22
|
+
return out;
|
|
23
|
+
}
|
|
24
|
+
function direction(d: QueryPoint): void {
|
|
25
|
+
check(d);
|
|
26
|
+
if (Math.hypot(d[0], d[1], d[2]) === 0) throw new RangeError('Ray direction must be nonzero');
|
|
27
|
+
}
|
|
28
|
+
export function getRayPoint(
|
|
29
|
+
origin: QueryPoint,
|
|
30
|
+
dir: QueryPoint,
|
|
31
|
+
t: number,
|
|
32
|
+
out: NumericArray = [0, 0, 0]
|
|
33
|
+
): NumericArray {
|
|
34
|
+
check(origin);
|
|
35
|
+
direction(dir);
|
|
36
|
+
if (!Number.isFinite(t) || t < 0)
|
|
37
|
+
throw new RangeError('Ray parameter must be finite and nonnegative');
|
|
38
|
+
return output(out, [origin[0] + t * dir[0], origin[1] + t * dir[1], origin[2] + t * dir[2]]);
|
|
39
|
+
}
|
|
40
|
+
export function getClosestPointOnRay(
|
|
41
|
+
point: QueryPoint,
|
|
42
|
+
origin: QueryPoint,
|
|
43
|
+
dir: QueryPoint,
|
|
44
|
+
out: NumericArray = [0, 0, 0]
|
|
45
|
+
): NumericArray {
|
|
46
|
+
check(point, origin);
|
|
47
|
+
direction(dir);
|
|
48
|
+
const unit = normalizeQueryDirection(dir, 3);
|
|
49
|
+
const distance = Math.max(0, dot(sub(point, origin), unit));
|
|
50
|
+
return output(
|
|
51
|
+
out,
|
|
52
|
+
[0, 1, 2].map(i => origin[i] + distance * unit[i])
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
export function distanceToRay(point: QueryPoint, origin: QueryPoint, dir: QueryPoint): number {
|
|
56
|
+
const p = getClosestPointOnRay(point, origin, dir);
|
|
57
|
+
return Math.hypot(...sub(point, p));
|
|
58
|
+
}
|
|
59
|
+
export type RaySegmentClosestPoints = {
|
|
60
|
+
rayPoint: V;
|
|
61
|
+
segmentPoint: V;
|
|
62
|
+
rayParameter: number;
|
|
63
|
+
segmentFraction: number;
|
|
64
|
+
distance: number;
|
|
65
|
+
};
|
|
66
|
+
export function getClosestPointsRaySegment(
|
|
67
|
+
origin: QueryPoint,
|
|
68
|
+
dir: QueryPoint,
|
|
69
|
+
a: QueryPoint,
|
|
70
|
+
b: QueryPoint
|
|
71
|
+
): RaySegmentClosestPoints {
|
|
72
|
+
check(origin, a, b);
|
|
73
|
+
direction(dir);
|
|
74
|
+
const unit = normalizeQueryDirection(dir, 3);
|
|
75
|
+
const directionLength = Math.hypot(dir[0], dir[1], dir[2]);
|
|
76
|
+
const edge = sub(b, a),
|
|
77
|
+
offset = sub(origin, a);
|
|
78
|
+
const segmentLength = Math.hypot(...edge);
|
|
79
|
+
const segmentUnit = edge.map(x => (segmentLength ? x / segmentLength : 0));
|
|
80
|
+
const bb = dot(unit, segmentUnit),
|
|
81
|
+
dd = dot(unit, offset),
|
|
82
|
+
ee = dot(segmentUnit, offset);
|
|
83
|
+
const candidates: [number, number][] = [
|
|
84
|
+
[0, Math.max(0, Math.min(segmentLength, ee))],
|
|
85
|
+
[Math.max(0, -dd), 0],
|
|
86
|
+
[Math.max(0, bb * segmentLength - dd), segmentLength]
|
|
87
|
+
];
|
|
88
|
+
// Cross products retain the angle when nearly parallel dot products round to one.
|
|
89
|
+
const normal = cross(unit, segmentUnit);
|
|
90
|
+
const sine = Math.hypot(...normal);
|
|
91
|
+
if (segmentLength > 0 && sine > 0) {
|
|
92
|
+
const normalUnit = normal.map(value => value / sine);
|
|
93
|
+
const separation = sub(a, origin);
|
|
94
|
+
const t = dot(cross(separation, segmentUnit), normalUnit) / sine;
|
|
95
|
+
const u = dot(cross(separation, unit), normalUnit) / sine;
|
|
96
|
+
if (t >= 0 && u >= 0 && u <= segmentLength) candidates.push([t, u]);
|
|
97
|
+
}
|
|
98
|
+
let best: RaySegmentClosestPoints | undefined;
|
|
99
|
+
for (const [t, u] of candidates) {
|
|
100
|
+
const rayPoint = [
|
|
101
|
+
origin[0] + t * unit[0],
|
|
102
|
+
origin[1] + t * unit[1],
|
|
103
|
+
origin[2] + t * unit[2]
|
|
104
|
+
] as V;
|
|
105
|
+
const segmentPoint = [
|
|
106
|
+
a[0] + u * segmentUnit[0],
|
|
107
|
+
a[1] + u * segmentUnit[1],
|
|
108
|
+
a[2] + u * segmentUnit[2]
|
|
109
|
+
] as V;
|
|
110
|
+
const distance = Math.hypot(...sub(rayPoint, segmentPoint));
|
|
111
|
+
if (!best || distance < best.distance)
|
|
112
|
+
best = {
|
|
113
|
+
rayPoint,
|
|
114
|
+
segmentPoint,
|
|
115
|
+
rayParameter: t / directionLength,
|
|
116
|
+
segmentFraction: segmentLength ? u / segmentLength : 0,
|
|
117
|
+
distance
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
return best!;
|
|
121
|
+
}
|
|
122
|
+
/** Finite affine transforms only. The unnormalized output direction preserves the ray parameter. */
|
|
123
|
+
export function transformRay(
|
|
124
|
+
origin: QueryPoint,
|
|
125
|
+
dir: QueryPoint,
|
|
126
|
+
matrix: QueryPoint
|
|
127
|
+
): {origin: V; direction: V} {
|
|
128
|
+
check(origin);
|
|
129
|
+
direction(dir);
|
|
130
|
+
if (
|
|
131
|
+
matrix.length !== 16 ||
|
|
132
|
+
Array.from(matrix).some(x => !Number.isFinite(x)) ||
|
|
133
|
+
matrix[3] !== 0 ||
|
|
134
|
+
matrix[7] !== 0 ||
|
|
135
|
+
matrix[11] !== 0 ||
|
|
136
|
+
matrix[15] !== 1
|
|
137
|
+
)
|
|
138
|
+
throw new RangeError('Expected a finite affine matrix');
|
|
139
|
+
const apply = (p: QueryPoint, w: number): V =>
|
|
140
|
+
[0, 1, 2].map(
|
|
141
|
+
i => matrix[i] * p[0] + matrix[i + 4] * p[1] + matrix[i + 8] * p[2] + w * matrix[i + 12]
|
|
142
|
+
) as V;
|
|
143
|
+
const transformed = {origin: apply(origin, 1), direction: apply(dir, 0)};
|
|
144
|
+
check(transformed.origin);
|
|
145
|
+
direction(transformed.direction);
|
|
146
|
+
return transformed;
|
|
147
|
+
}
|
|
148
|
+
function triangle(a: QueryPoint, b: QueryPoint, c: QueryPoint) {
|
|
149
|
+
check(a, b, c);
|
|
150
|
+
const ab = sub(b, a),
|
|
151
|
+
ac = sub(c, a);
|
|
152
|
+
const scale = Math.max(Math.hypot(...ab), Math.hypot(...ac));
|
|
153
|
+
const u = ab.map(x => (scale ? x / scale : 0)) as V,
|
|
154
|
+
v = ac.map(x => (scale ? x / scale : 0)) as V;
|
|
155
|
+
return {u, v, scale, normal: cross(u, v)};
|
|
156
|
+
}
|
|
157
|
+
/** Barycentrics of the point's orthogonal projection; null for a degenerate triangle. */
|
|
158
|
+
export function getTriangleBarycentric(
|
|
159
|
+
point: QueryPoint,
|
|
160
|
+
a: QueryPoint,
|
|
161
|
+
b: QueryPoint,
|
|
162
|
+
c: QueryPoint,
|
|
163
|
+
out: NumericArray = [0, 0, 0]
|
|
164
|
+
): NumericArray | null {
|
|
165
|
+
check(point);
|
|
166
|
+
const {u, v, scale, normal} = triangle(a, b, c),
|
|
167
|
+
denominator = dot(normal, normal);
|
|
168
|
+
if (denominator === 0) return null;
|
|
169
|
+
const p = sub(point, a).map(x => x / scale);
|
|
170
|
+
const second = dot(cross(p, v), normal) / denominator;
|
|
171
|
+
const third = dot(cross(u, p), normal) / denominator;
|
|
172
|
+
return output(out, [1 - second - third, second, third]);
|
|
173
|
+
}
|
|
174
|
+
export function getTriangleArea(a: QueryPoint, b: QueryPoint, c: QueryPoint): number {
|
|
175
|
+
const {normal, scale} = triangle(a, b, c);
|
|
176
|
+
return (Math.hypot(...normal) * scale * scale) / 2;
|
|
177
|
+
}
|
|
178
|
+
export function getTriangleNormal(
|
|
179
|
+
a: QueryPoint,
|
|
180
|
+
b: QueryPoint,
|
|
181
|
+
c: QueryPoint,
|
|
182
|
+
out: NumericArray = [0, 0, 0]
|
|
183
|
+
): NumericArray {
|
|
184
|
+
const {normal} = triangle(a, b, c),
|
|
185
|
+
length = Math.hypot(...normal);
|
|
186
|
+
return output(
|
|
187
|
+
out,
|
|
188
|
+
normal.map(x => (length ? x / length : 0))
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
/** Front-facing means the oriented normal points against the viewing/ray direction. */
|
|
192
|
+
export function isTriangleFrontFacing(
|
|
193
|
+
a: QueryPoint,
|
|
194
|
+
b: QueryPoint,
|
|
195
|
+
c: QueryPoint,
|
|
196
|
+
dir: QueryPoint
|
|
197
|
+
): boolean {
|
|
198
|
+
direction(dir);
|
|
199
|
+
return dot(triangle(a, b, c).normal, dir) < 0;
|
|
200
|
+
}
|
|
201
|
+
/** Relative tolerance: plane distance is scaled by the longest edge from a. */
|
|
202
|
+
export function triangleContainsPoint(
|
|
203
|
+
point: QueryPoint,
|
|
204
|
+
a: QueryPoint,
|
|
205
|
+
b: QueryPoint,
|
|
206
|
+
c: QueryPoint,
|
|
207
|
+
tolerance = 1e-10
|
|
208
|
+
): boolean {
|
|
209
|
+
if (!Number.isFinite(tolerance) || tolerance < 0) throw new RangeError('Invalid tolerance');
|
|
210
|
+
const weights = getTriangleBarycentric(point, a, b, c);
|
|
211
|
+
if (!weights) return false;
|
|
212
|
+
const {normal, scale} = triangle(a, b, c);
|
|
213
|
+
const distance =
|
|
214
|
+
Math.abs(
|
|
215
|
+
dot(
|
|
216
|
+
sub(point, a).map(x => x / scale),
|
|
217
|
+
normal
|
|
218
|
+
)
|
|
219
|
+
) / Math.hypot(...normal);
|
|
220
|
+
return (
|
|
221
|
+
distance <= tolerance && Array.from(weights).every(x => x >= -tolerance && x <= 1 + tolerance)
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
export function interpolateTriangleAttribute(
|
|
225
|
+
weights: QueryPoint,
|
|
226
|
+
a: QueryPoint,
|
|
227
|
+
b: QueryPoint,
|
|
228
|
+
c: QueryPoint,
|
|
229
|
+
out: NumericArray = new Array(a.length)
|
|
230
|
+
): NumericArray {
|
|
231
|
+
check(weights);
|
|
232
|
+
if (
|
|
233
|
+
!a.length ||
|
|
234
|
+
a.length !== b.length ||
|
|
235
|
+
a.length !== c.length ||
|
|
236
|
+
[a, b, c].some(p => Array.from(p).some(x => !Number.isFinite(x)))
|
|
237
|
+
)
|
|
238
|
+
throw new RangeError('Expected matching finite attributes');
|
|
239
|
+
return output(
|
|
240
|
+
out,
|
|
241
|
+
Array.from(a, (x, i) => weights[0] * x + weights[1] * b[i] + weights[2] * c[i])
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
/** Closed segment; a coplanar segment returns its start, a disjoint segment returns null. */
|
|
245
|
+
export function intersectPlaneSegment(
|
|
246
|
+
normal: QueryPoint,
|
|
247
|
+
constant: number,
|
|
248
|
+
start: QueryPoint,
|
|
249
|
+
end: QueryPoint,
|
|
250
|
+
out: NumericArray = [0, 0, 0]
|
|
251
|
+
): NumericArray | null {
|
|
252
|
+
check(start, end);
|
|
253
|
+
direction(normal);
|
|
254
|
+
if (!Number.isFinite(constant)) throw new RangeError('Plane constant must be finite');
|
|
255
|
+
const a = dot(normal, start) + constant,
|
|
256
|
+
b = dot(normal, end) + constant;
|
|
257
|
+
if (a === 0) return output(out, start);
|
|
258
|
+
if (b === 0) return output(out, end);
|
|
259
|
+
if (a > 0 === b > 0) return null;
|
|
260
|
+
const t = a / (a - b);
|
|
261
|
+
return output(
|
|
262
|
+
out,
|
|
263
|
+
[0, 1, 2].map(i => start[i] + t * (end[i] - start[i]))
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
/** Separating-axis test for a closed AABB. Degenerate triangles reduce to segments/points. */
|
|
267
|
+
export function triangleIntersectsBounds(
|
|
268
|
+
a: QueryPoint,
|
|
269
|
+
b: QueryPoint,
|
|
270
|
+
c: QueryPoint,
|
|
271
|
+
minimum: QueryPoint,
|
|
272
|
+
maximum: QueryPoint
|
|
273
|
+
): boolean {
|
|
274
|
+
check(a, b, c, minimum, maximum);
|
|
275
|
+
if ([0, 1, 2].some(i => minimum[i] > maximum[i])) return false;
|
|
276
|
+
const center = [0, 1, 2].map(i => minimum[i] / 2 + maximum[i] / 2);
|
|
277
|
+
const extent = [0, 1, 2].map(i => maximum[i] / 2 - minimum[i] / 2);
|
|
278
|
+
const vertices = [a, b, c].map(p => sub(p, center));
|
|
279
|
+
const edges = [sub(b, a), sub(c, b), sub(a, c)];
|
|
280
|
+
const basis: V[] = [
|
|
281
|
+
[1, 0, 0],
|
|
282
|
+
[0, 1, 0],
|
|
283
|
+
[0, 0, 1]
|
|
284
|
+
];
|
|
285
|
+
const axes = [
|
|
286
|
+
...basis,
|
|
287
|
+
cross(edges[0], edges[1]),
|
|
288
|
+
...edges.flatMap(e => basis.map(axis => cross(e, axis)))
|
|
289
|
+
];
|
|
290
|
+
for (const axis of axes) {
|
|
291
|
+
const length = Math.hypot(...axis);
|
|
292
|
+
if (!length) continue;
|
|
293
|
+
const unit = axis.map(x => x / length);
|
|
294
|
+
const projections = vertices.map(v => dot(v, unit));
|
|
295
|
+
const radius = extent.reduce((sum, x, i) => sum + x * Math.abs(unit[i]), 0);
|
|
296
|
+
if (Math.min(...projections) > radius || Math.max(...projections) < -radius) return false;
|
|
297
|
+
}
|
|
298
|
+
return true;
|
|
299
|
+
}
|
package/src/culling/index.ts
CHANGED
|
@@ -11,7 +11,11 @@ export {CullingVolume} from './lib/culling-volume';
|
|
|
11
11
|
export {Plane} from './lib/plane';
|
|
12
12
|
export {Ray} from './lib/ray';
|
|
13
13
|
|
|
14
|
-
export type {
|
|
14
|
+
export type {
|
|
15
|
+
ImplicitShape,
|
|
16
|
+
ImplicitShape as Shape,
|
|
17
|
+
RayIntersection
|
|
18
|
+
} from './lib/shapes/implicit-shape';
|
|
15
19
|
export {BoxShape} from './lib/shapes/box-shape';
|
|
16
20
|
export type {BoxShapeProps} from './lib/shapes/box-shape';
|
|
17
21
|
export {CapsuleShape} from './lib/shapes/capsule-shape';
|
|
@@ -55,3 +59,35 @@ export {
|
|
|
55
59
|
getClosestPointsSegmentTriangle
|
|
56
60
|
} from './queries';
|
|
57
61
|
export type {SegmentClosestPoints, SegmentTriangleClosestPoints} from './queries';
|
|
62
|
+
|
|
63
|
+
export {extractFrustumPlanes} from './frustum-planes';
|
|
64
|
+
export {
|
|
65
|
+
createEmptyBounds,
|
|
66
|
+
isEmptyBounds,
|
|
67
|
+
boundsContainsPoint,
|
|
68
|
+
boundsContainsBounds,
|
|
69
|
+
boundsIntersect,
|
|
70
|
+
unionBounds,
|
|
71
|
+
intersectBounds,
|
|
72
|
+
expandBoundsByPoint,
|
|
73
|
+
expandBounds,
|
|
74
|
+
clampPointToBounds,
|
|
75
|
+
spheresIntersect,
|
|
76
|
+
boundsIntersectSphere
|
|
77
|
+
} from './bounds';
|
|
78
|
+
export {
|
|
79
|
+
getRayPoint,
|
|
80
|
+
getClosestPointOnRay,
|
|
81
|
+
distanceToRay,
|
|
82
|
+
getClosestPointsRaySegment,
|
|
83
|
+
transformRay,
|
|
84
|
+
getTriangleBarycentric,
|
|
85
|
+
getTriangleArea,
|
|
86
|
+
getTriangleNormal,
|
|
87
|
+
isTriangleFrontFacing,
|
|
88
|
+
triangleContainsPoint,
|
|
89
|
+
interpolateTriangleAttribute,
|
|
90
|
+
intersectPlaneSegment,
|
|
91
|
+
triangleIntersectsBounds
|
|
92
|
+
} from './geometric-queries';
|
|
93
|
+
export type {RaySegmentClosestPoints} from './geometric-queries';
|
|
@@ -83,16 +83,34 @@ export class AxisAlignedBoundingBox implements BoundingVolume {
|
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
/**
|
|
86
|
-
*
|
|
87
|
-
* @param transform The transformation matrix
|
|
88
|
-
* @returns itself, i.e. the modified
|
|
86
|
+
* Transforms an AABB by a finite affine matrix, rebuilding all bounds and extents.
|
|
87
|
+
* @param transform The affine transformation matrix.
|
|
88
|
+
* @returns itself, i.e. the modified bounding box.
|
|
89
89
|
*/
|
|
90
90
|
transform(transform: readonly number[]): this {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
91
|
+
if (
|
|
92
|
+
transform.length !== 16 ||
|
|
93
|
+
transform.some(x => !Number.isFinite(x)) ||
|
|
94
|
+
transform[3] !== 0 ||
|
|
95
|
+
transform[7] !== 0 ||
|
|
96
|
+
transform[11] !== 0 ||
|
|
97
|
+
transform[15] !== 1
|
|
98
|
+
) {
|
|
99
|
+
throw new RangeError('Expected a finite affine matrix');
|
|
100
|
+
}
|
|
101
|
+
const center = this.minimum.clone().add(this.maximum).scale(0.5);
|
|
102
|
+
const extent = this.maximum.clone().subtract(this.minimum).scale(0.5);
|
|
103
|
+
if (extent.some(x => x < 0)) throw new RangeError('Bounds must be ordered');
|
|
104
|
+
center.transformAsPoint(transform);
|
|
105
|
+
for (let row = 0; row < 3; row++) {
|
|
106
|
+
this.halfDiagonal[row] =
|
|
107
|
+
Math.abs(transform[row]) * extent[0] +
|
|
108
|
+
Math.abs(transform[row + 4]) * extent[1] +
|
|
109
|
+
Math.abs(transform[row + 8]) * extent[2];
|
|
110
|
+
}
|
|
111
|
+
this.center.copy(center);
|
|
112
|
+
this.minimum.copy(center).subtract(this.halfDiagonal);
|
|
113
|
+
this.maximum.copy(center).add(this.halfDiagonal);
|
|
96
114
|
return this;
|
|
97
115
|
}
|
|
98
116
|
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
// See ../../../LICENSE for the retained Cesium notice and Apache-2.0 terms.
|
|
9
9
|
|
|
10
10
|
/* eslint-disable */
|
|
11
|
-
import {Vector3, assert} from '@math.gl/core';
|
|
11
|
+
import {Vector3, assert, type ProjectionDepthOptions} from '@math.gl/core';
|
|
12
|
+
import {extractFrustumPlanes} from '../frustum-planes';
|
|
12
13
|
import type {CullingResult} from '../constants';
|
|
13
14
|
import {Plane} from './plane';
|
|
14
15
|
import type {BoundingVolume} from './bounding-volumes/bounding-volume';
|
|
@@ -94,6 +95,13 @@ export class CullingVolume {
|
|
|
94
95
|
return this;
|
|
95
96
|
}
|
|
96
97
|
|
|
98
|
+
/** Replace planes from a view-projection matrix; validates before mutating. */
|
|
99
|
+
fromViewProjection(matrix: ArrayLike<number>, options: ProjectionDepthOptions = {}): this {
|
|
100
|
+
const planes = extractFrustumPlanes(matrix, options);
|
|
101
|
+
this.planes.splice(0, this.planes.length, ...planes);
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
|
|
97
105
|
/** Determines whether a bounding volume intersects the culling volume. */
|
|
98
106
|
computeVisibility(boundingVolume: BoundingVolume): CullingResult {
|
|
99
107
|
// const planes = this.planes;
|
package/src/culling/lib/ray.ts
CHANGED
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
// See ../../../LICENSE for the retained Cesium notice and Apache-2.0 terms.
|
|
9
9
|
|
|
10
10
|
import {Vector3} from '@math.gl/core';
|
|
11
|
+
import {normalizeQueryDirection} from '../queries';
|
|
12
|
+
import {getRayPoint, getClosestPointOnRay, distanceToRay, transformRay} from '../geometric-queries';
|
|
11
13
|
|
|
12
14
|
/* Represents a ray that extends infinitely from the provided origin in the provided direction. */
|
|
13
15
|
export class Ray {
|
|
@@ -30,4 +32,23 @@ export class Ray {
|
|
|
30
32
|
this.origin = origin;
|
|
31
33
|
this.direction = direction;
|
|
32
34
|
}
|
|
35
|
+
at(t: number, result = new Vector3()): Vector3 {
|
|
36
|
+
getRayPoint(this.origin, this.direction, t, result);
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
closestPointTo(point: readonly number[], result = new Vector3()): Vector3 {
|
|
40
|
+
getClosestPointOnRay(point, this.origin, this.direction, result);
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
distanceTo(point: readonly number[]): number {
|
|
44
|
+
return distanceToRay(point, this.origin, this.direction);
|
|
45
|
+
}
|
|
46
|
+
/** Transform and renormalize: subsequent parameters are distances in the new space. */
|
|
47
|
+
transform(matrix: readonly number[]): this {
|
|
48
|
+
const ray = transformRay(this.origin, this.direction, matrix);
|
|
49
|
+
const normalized = normalizeQueryDirection(ray.direction, 3);
|
|
50
|
+
this.origin.copy(ray.origin);
|
|
51
|
+
this.direction.copy(normalized);
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
33
54
|
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
import {Curve} from './curve';
|
|
5
|
+
|
|
6
|
+
export type AdaptiveCurveOptions = {
|
|
7
|
+
tolerance: number;
|
|
8
|
+
maxDepth?: number;
|
|
9
|
+
maxPoints?: number;
|
|
10
|
+
};
|
|
11
|
+
export type AdaptiveCurveSamples = {
|
|
12
|
+
parameters: Float64Array;
|
|
13
|
+
positions: Float64Array;
|
|
14
|
+
/** False means a work limit prevented meeting the sampled chord-error criterion. */
|
|
15
|
+
converged: boolean;
|
|
16
|
+
maxSampledError: number;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** Quarter/midpoint chord-error sampling. This is not a certified bound for arbitrary curves. */
|
|
20
|
+
export function sampleCurveAdaptive(
|
|
21
|
+
curve: Curve,
|
|
22
|
+
options: AdaptiveCurveOptions
|
|
23
|
+
): AdaptiveCurveSamples {
|
|
24
|
+
const {tolerance, maxDepth = 16, maxPoints = 65536} = options;
|
|
25
|
+
if (
|
|
26
|
+
!Number.isFinite(tolerance) ||
|
|
27
|
+
tolerance <= 0 ||
|
|
28
|
+
!Number.isInteger(maxDepth) ||
|
|
29
|
+
maxDepth < 0 ||
|
|
30
|
+
maxDepth > 30 ||
|
|
31
|
+
!Number.isSafeInteger(maxPoints) ||
|
|
32
|
+
maxPoints < 2
|
|
33
|
+
)
|
|
34
|
+
throw new RangeError('Invalid adaptive sampling budget');
|
|
35
|
+
const parameters = [0],
|
|
36
|
+
positions = [...curve.getPoint(0)];
|
|
37
|
+
let converged = true,
|
|
38
|
+
maxSampledError = 0;
|
|
39
|
+
const stack = [{t0: 0, t1: 1, a: curve.getPoint(0), b: curve.getPoint(1), depth: 0}];
|
|
40
|
+
while (stack.length) {
|
|
41
|
+
const segment = stack.pop()!;
|
|
42
|
+
const {t0, t1, a, b, depth} = segment;
|
|
43
|
+
const localEdge = b.map((x, i) => x - a[i]);
|
|
44
|
+
const samples = [0.25, 0.5, 0.75].map(f => curve.getPoint(t0 + (t1 - t0) * f));
|
|
45
|
+
const localSamples = samples.map(point => point.map((x, i) => x - a[i]));
|
|
46
|
+
const local = [...localEdge, ...localSamples.flat()].every(Number.isFinite);
|
|
47
|
+
const scale = local
|
|
48
|
+
? Math.max(...localEdge.map(Math.abs), ...localSamples.flat().map(Math.abs), Number.MIN_VALUE)
|
|
49
|
+
: Math.max(...a.map(Math.abs), ...b.map(Math.abs), ...samples.flat().map(Math.abs));
|
|
50
|
+
const edge = local ? localEdge.map(x => x / scale) : b.map((x, i) => x / scale - a[i] / scale);
|
|
51
|
+
const squared = edge.reduce((sum, x) => sum + x * x, 0);
|
|
52
|
+
const measuredError = Math.max(
|
|
53
|
+
...samples.map((p, sampleIndex) => {
|
|
54
|
+
const offset = local
|
|
55
|
+
? localSamples[sampleIndex].map(x => x / scale)
|
|
56
|
+
: p.map((x, i) => x / scale - a[i] / scale);
|
|
57
|
+
const t = squared
|
|
58
|
+
? Math.max(0, Math.min(1, edge.reduce((sum, x, i) => sum + x * offset[i], 0) / squared))
|
|
59
|
+
: 0;
|
|
60
|
+
return Math.hypot(...offset.map((x, i) => x - t * edge[i])) * scale;
|
|
61
|
+
})
|
|
62
|
+
);
|
|
63
|
+
const error = Number.isFinite(measuredError) ? measuredError : Infinity;
|
|
64
|
+
if (
|
|
65
|
+
error > tolerance &&
|
|
66
|
+
depth < maxDepth &&
|
|
67
|
+
parameters.length + stack.length + 2 <= maxPoints
|
|
68
|
+
) {
|
|
69
|
+
const mid = (t0 + t1) / 2;
|
|
70
|
+
stack.push({t0: mid, t1, a: samples[1], b, depth: depth + 1});
|
|
71
|
+
stack.push({t0, t1: mid, a, b: samples[1], depth: depth + 1});
|
|
72
|
+
} else {
|
|
73
|
+
if (error > tolerance) converged = false;
|
|
74
|
+
maxSampledError = Math.max(maxSampledError, error);
|
|
75
|
+
parameters.push(t1);
|
|
76
|
+
positions.push(...b);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
parameters: new Float64Array(parameters),
|
|
81
|
+
positions: new Float64Array(positions),
|
|
82
|
+
converged,
|
|
83
|
+
maxSampledError
|
|
84
|
+
};
|
|
85
|
+
}
|
package/src/curves/index.ts
CHANGED
|
@@ -13,3 +13,7 @@ export {HermiteCurve} from './hermite-curve';
|
|
|
13
13
|
export {CatmullRomCurve} from './catmull-rom-curve';
|
|
14
14
|
export type {CatmullRomCurveOptions} from './catmull-rom-curve';
|
|
15
15
|
export {CurveArcLength} from './curve-arc-length';
|
|
16
|
+
export {sampleCurveAdaptive} from './adaptive-sampling';
|
|
17
|
+
export type {AdaptiveCurveOptions, AdaptiveCurveSamples} from './adaptive-sampling';
|
|
18
|
+
export {computeParallelTransportFrames} from './parallel-transport';
|
|
19
|
+
export type {CurveFrames} from './parallel-transport';
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
import type {CurvePoint} from './curve';
|
|
5
|
+
|
|
6
|
+
export type CurveFrames = {tangents: Float64Array; normals: Float64Array; binormals: Float64Array};
|
|
7
|
+
type V = number[];
|
|
8
|
+
const sub = (a: V, b: V) => a.map((x, i) => x - b[i]);
|
|
9
|
+
const dot = (a: V, b: V) => a.reduce((sum, x, i) => sum + x * b[i], 0);
|
|
10
|
+
const cross = (a: V, b: V): V => [
|
|
11
|
+
a[1] * b[2] - a[2] * b[1],
|
|
12
|
+
a[2] * b[0] - a[0] * b[2],
|
|
13
|
+
a[0] * b[1] - a[1] * b[0]
|
|
14
|
+
];
|
|
15
|
+
const unit = (a: V) => {
|
|
16
|
+
const length = Math.hypot(...a);
|
|
17
|
+
if (!length) throw new RangeError('Cannot construct a frame for a stationary path');
|
|
18
|
+
return a.map(x => x / length);
|
|
19
|
+
};
|
|
20
|
+
function rotate(v: V, axis: V, angle: number): V {
|
|
21
|
+
const c = Math.cos(angle),
|
|
22
|
+
s = Math.sin(angle),
|
|
23
|
+
d = dot(axis, v),
|
|
24
|
+
k = cross(axis, v);
|
|
25
|
+
return v.map((x, i) => x * c + k[i] * s + axis[i] * d * (1 - c));
|
|
26
|
+
}
|
|
27
|
+
/** Frames on a 3D polyline; repeated points inherit a neighboring direction. Closed paths repeat the first point. */
|
|
28
|
+
export function computeParallelTransportFrames(
|
|
29
|
+
points: readonly CurvePoint[],
|
|
30
|
+
options: {closed?: boolean; initialNormal?: CurvePoint} = {}
|
|
31
|
+
): CurveFrames {
|
|
32
|
+
if (
|
|
33
|
+
points.length < 2 ||
|
|
34
|
+
points.some(p => p.length !== 3 || Array.from(p).some(x => !Number.isFinite(x)))
|
|
35
|
+
)
|
|
36
|
+
throw new RangeError('Expected at least two finite 3D points');
|
|
37
|
+
const data = points.map(p => Array.from(p)),
|
|
38
|
+
n = data.length,
|
|
39
|
+
closed = options.closed ?? false;
|
|
40
|
+
if (closed && (n < 4 || Math.hypot(...sub(data[0], data[n - 1])) !== 0))
|
|
41
|
+
throw new RangeError('A closed path must repeat its first point');
|
|
42
|
+
// Cache adjacent nonzero directions, then propagate backwards across equal runs.
|
|
43
|
+
// This keeps repeated-point paths linear rather than rescanning each suffix.
|
|
44
|
+
const tangent: V[] = new Array(n);
|
|
45
|
+
const edges: (V | undefined)[] = new Array(n - 1);
|
|
46
|
+
let last: V | undefined;
|
|
47
|
+
for (let i = 0; i < n - 1; i++) {
|
|
48
|
+
const edge = sub(data[i + 1], data[i]);
|
|
49
|
+
if (Math.hypot(...edge) > 0) last = edges[i] = unit(edge);
|
|
50
|
+
}
|
|
51
|
+
if (!last) throw new RangeError('Cannot construct a frame for a stationary path');
|
|
52
|
+
tangent[n - 1] = last;
|
|
53
|
+
for (let i = n - 2; i >= 0; i--) {
|
|
54
|
+
last = edges[i] ?? last;
|
|
55
|
+
tangent[i] = last;
|
|
56
|
+
}
|
|
57
|
+
if (closed) tangent[n - 1] = [...tangent[0]];
|
|
58
|
+
const t = tangent[0];
|
|
59
|
+
let initial = options.initialNormal ? Array.from(options.initialNormal) : [0, 0, 0];
|
|
60
|
+
if (options.initialNormal && (initial.length !== 3 || initial.some(x => !Number.isFinite(x))))
|
|
61
|
+
throw new RangeError('Invalid initial normal');
|
|
62
|
+
// Select an axis by absolute alignment (including negative tangent components).
|
|
63
|
+
if (!options.initialNormal) {
|
|
64
|
+
initial = [0, 0, 0];
|
|
65
|
+
initial[t.map(Math.abs).indexOf(Math.min(...t.map(Math.abs)))] = 1;
|
|
66
|
+
}
|
|
67
|
+
initial = unit(initial);
|
|
68
|
+
const perpendicular = cross(t, initial);
|
|
69
|
+
if (Math.hypot(...perpendicular) <= 1e-12) {
|
|
70
|
+
throw new RangeError('Initial normal must not be parallel to the tangent');
|
|
71
|
+
}
|
|
72
|
+
// A double cross product avoids normalizing a cancellation residual along t.
|
|
73
|
+
const normals: V[] = [unit(cross(perpendicular, t))];
|
|
74
|
+
for (let i = 1; i < n; i++) {
|
|
75
|
+
const axis = cross(tangent[i - 1], tangent[i]),
|
|
76
|
+
length = Math.hypot(...axis),
|
|
77
|
+
cosine = Math.max(-1, Math.min(1, dot(tangent[i - 1], tangent[i])));
|
|
78
|
+
let normal = normals[i - 1];
|
|
79
|
+
if (length > 1e-12)
|
|
80
|
+
normal = rotate(
|
|
81
|
+
normal,
|
|
82
|
+
axis.map(x => x / length),
|
|
83
|
+
Math.atan2(length, cosine)
|
|
84
|
+
);
|
|
85
|
+
// At an exact reversal the previous normal defines the deterministic 180-degree axis.
|
|
86
|
+
normals.push(unit(normal.map((x, j) => x - dot(normal, tangent[i]) * tangent[i][j])));
|
|
87
|
+
}
|
|
88
|
+
if (closed) {
|
|
89
|
+
const mismatch = Math.atan2(
|
|
90
|
+
dot(tangent[0], cross(normals[n - 1], normals[0])),
|
|
91
|
+
dot(normals[n - 1], normals[0])
|
|
92
|
+
);
|
|
93
|
+
const lengths = [0];
|
|
94
|
+
for (let i = 1; i < n; i++)
|
|
95
|
+
lengths.push(lengths[i - 1] + Math.hypot(...sub(data[i], data[i - 1])));
|
|
96
|
+
for (let i = 1; i < n; i++)
|
|
97
|
+
normals[i] = rotate(normals[i], tangent[i], (mismatch * lengths[i]) / lengths[n - 1]);
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
tangents: new Float64Array(tangent.flat()),
|
|
101
|
+
normals: new Float64Array(normals.flat()),
|
|
102
|
+
binormals: new Float64Array(normals.flatMap((normal, i) => cross(tangent[i], normal)))
|
|
103
|
+
};
|
|
104
|
+
}
|