@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,93 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
/** Projects a lattice covering corners, edges, faces and the interior of a 2D/3D box.
|
|
5
|
+
* Sampling alone cannot establish an enclosure for a nonlinear transform. When qualified
|
|
6
|
+
* global Lipschitz constants are supplied, inflate by the lattice covering radius instead
|
|
7
|
+
* of claiming corner/sample extrema are conservative. Split seams and domains beforehand.
|
|
8
|
+
*/
|
|
9
|
+
export function projectBounds(sourceBounds, options) {
|
|
10
|
+
const [minimum, maximum] = sourceBounds;
|
|
11
|
+
const size = minimum.length;
|
|
12
|
+
const { transform, targetSize = size, subdivisions = 8, maxSamples = 262144, lipschitz } = options;
|
|
13
|
+
if ((size !== 2 && size !== 3) ||
|
|
14
|
+
maximum.length !== size ||
|
|
15
|
+
(targetSize !== 2 && targetSize !== 3)) {
|
|
16
|
+
throw new RangeError('Bounds and target dimension must be 2 or 3');
|
|
17
|
+
}
|
|
18
|
+
if (!Number.isSafeInteger(subdivisions) ||
|
|
19
|
+
subdivisions < 1 ||
|
|
20
|
+
!Number.isSafeInteger(maxSamples) ||
|
|
21
|
+
maxSamples < 1) {
|
|
22
|
+
throw new RangeError('Subdivision and sample budgets must be positive safe integers');
|
|
23
|
+
}
|
|
24
|
+
if (lipschitz &&
|
|
25
|
+
(lipschitz.length !== targetSize ||
|
|
26
|
+
!Array.from(lipschitz).every(value => Number.isFinite(value) && value >= 0))) {
|
|
27
|
+
throw new RangeError('Lipschitz constants must be finite, nonnegative and match target size');
|
|
28
|
+
}
|
|
29
|
+
const widths = [];
|
|
30
|
+
const counts = [];
|
|
31
|
+
let count = 1;
|
|
32
|
+
for (let axis = 0; axis < size; axis++) {
|
|
33
|
+
const width = maximum[axis] - minimum[axis];
|
|
34
|
+
if (!Number.isFinite(minimum[axis]) ||
|
|
35
|
+
!Number.isFinite(maximum[axis]) ||
|
|
36
|
+
!Number.isFinite(width) ||
|
|
37
|
+
width < 0) {
|
|
38
|
+
throw new RangeError('Source bounds must have finite ordered coordinates and extents');
|
|
39
|
+
}
|
|
40
|
+
widths.push(width);
|
|
41
|
+
counts.push(width === 0 ? 1 : subdivisions + 1);
|
|
42
|
+
count *= counts[axis];
|
|
43
|
+
if (!Number.isSafeInteger(count) || count > maxSamples) {
|
|
44
|
+
throw new RangeError('Projected bounds exceed the sample budget');
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const lower = new Array(targetSize).fill(Infinity);
|
|
48
|
+
const upper = new Array(targetSize).fill(-Infinity);
|
|
49
|
+
for (let index = 0; index < count; index++) {
|
|
50
|
+
let remaining = index;
|
|
51
|
+
const source = [];
|
|
52
|
+
for (let axis = 0; axis < size; axis++) {
|
|
53
|
+
const step = remaining % counts[axis];
|
|
54
|
+
remaining = Math.floor(remaining / counts[axis]);
|
|
55
|
+
source.push(step === 0
|
|
56
|
+
? minimum[axis]
|
|
57
|
+
: step === subdivisions
|
|
58
|
+
? maximum[axis]
|
|
59
|
+
: minimum[axis] + widths[axis] * (step / subdivisions));
|
|
60
|
+
}
|
|
61
|
+
const projected = transform(source);
|
|
62
|
+
if (!projected || projected.length < targetSize) {
|
|
63
|
+
return { status: 'invalid', bounds: null, sampleCount: index + 1 };
|
|
64
|
+
}
|
|
65
|
+
for (let axis = 0; axis < targetSize; axis++) {
|
|
66
|
+
if (!Number.isFinite(projected[axis]))
|
|
67
|
+
return { status: 'invalid', bounds: null, sampleCount: index + 1 };
|
|
68
|
+
lower[axis] = Math.min(lower[axis], projected[axis]);
|
|
69
|
+
upper[axis] = Math.max(upper[axis], projected[axis]);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (lipschitz) {
|
|
73
|
+
const radius = Math.hypot(...widths.map((width, axis) => width / subdivisions / 2 +
|
|
74
|
+
4 * Number.EPSILON * Math.max(Math.abs(minimum[axis]), Math.abs(maximum[axis]))));
|
|
75
|
+
for (let axis = 0; axis < targetSize; axis++) {
|
|
76
|
+
const displacement = lipschitz[axis] * radius;
|
|
77
|
+
const roundoff = 8 *
|
|
78
|
+
Number.EPSILON *
|
|
79
|
+
Math.max(1, Math.abs(lower[axis]), Math.abs(upper[axis]), displacement);
|
|
80
|
+
lower[axis] -= displacement + roundoff;
|
|
81
|
+
upper[axis] += displacement + roundoff;
|
|
82
|
+
if (!Number.isFinite(lower[axis]) || !Number.isFinite(upper[axis])) {
|
|
83
|
+
return { status: 'invalid', bounds: null, sampleCount: count };
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
status: lipschitz ? 'conservative' : 'sampled',
|
|
89
|
+
bounds: [lower, upper],
|
|
90
|
+
sampleCount: count
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=project-bounds.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AAC,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,KAAK,EAAC,CAAC;YAC/D,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO;QACL,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,SAAS;QAC9C,MAAM,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC;QACtB,WAAW,EAAE,KAAK;KACnB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Projects packed source vertices into an explicitly located image rectangle. */
|
|
2
|
+
export type ProjectTextureCoordinatesOptions = {
|
|
3
|
+
/** Prepared synchronous source-to-image transform. Receives a fresh source array.
|
|
4
|
+
* Null or nonfinite XY rejects the operation; exceptions propagate unchanged.
|
|
5
|
+
*/
|
|
6
|
+
transform: (position: readonly number[]) => ArrayLike<number> | null;
|
|
7
|
+
/** Image rectangle [left, bottom, right, top], in the transform's output coordinates. */
|
|
8
|
+
bounds: readonly [number, number, number, number];
|
|
9
|
+
/** Number of packed source components per vertex. Default 3. */
|
|
10
|
+
size?: 2 | 3;
|
|
11
|
+
/** Texture origin. Default 'top-left'; bottom-left makes V increase with image Y. */
|
|
12
|
+
origin?: 'top-left' | 'bottom-left';
|
|
13
|
+
};
|
|
14
|
+
/** Generates detached float64 UVs without changing positions, indices or vertex order.
|
|
15
|
+
* Outside-image coordinates remain outside [0, 1]. This performs no subdivision,
|
|
16
|
+
* seam inference, domain clipping, GPU readback, or asynchronous preparation.
|
|
17
|
+
*/
|
|
18
|
+
export declare function projectTextureCoordinates(positions: ArrayLike<number>, options: ProjectTextureCoordinatesOptions): Float64Array;
|
|
19
|
+
//# sourceMappingURL=project-texture-coordinates.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"project-texture-coordinates.d.ts","sourceRoot":"","sources":["../../src/utils/project-texture-coordinates.ts"],"names":[],"mappings":"AAIA,kFAAkF;AAClF,MAAM,MAAM,gCAAgC,GAAG;IAC7C;;OAEG;IACH,SAAS,EAAE,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,KAAK,SAAS,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC;IACrE,yFAAyF;IACzF,MAAM,EAAE,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;IAClD,gEAAgE;IAChE,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC;IACb,qFAAqF;IACrF,MAAM,CAAC,EAAE,UAAU,GAAG,aAAa,CAAC;CACrC,CAAC;AAEF;;;GAGG;AACH,wBAAgB,yBAAyB,CACvC,SAAS,EAAE,SAAS,CAAC,MAAM,CAAC,EAC5B,OAAO,EAAE,gCAAgC,GACxC,YAAY,CAyCd"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
/** Generates detached float64 UVs without changing positions, indices or vertex order.
|
|
5
|
+
* Outside-image coordinates remain outside [0, 1]. This performs no subdivision,
|
|
6
|
+
* seam inference, domain clipping, GPU readback, or asynchronous preparation.
|
|
7
|
+
*/
|
|
8
|
+
export function projectTextureCoordinates(positions, options) {
|
|
9
|
+
const { transform, bounds, size = 3, origin = 'top-left' } = options;
|
|
10
|
+
if (size !== 2 && size !== 3)
|
|
11
|
+
throw new RangeError('Source size must be 2 or 3');
|
|
12
|
+
if (!Number.isSafeInteger(positions.length) || positions.length < 0 || positions.length % size) {
|
|
13
|
+
throw new RangeError('Positions must contain complete packed vertices');
|
|
14
|
+
}
|
|
15
|
+
if (origin !== 'top-left' && origin !== 'bottom-left') {
|
|
16
|
+
throw new RangeError('Invalid texture origin');
|
|
17
|
+
}
|
|
18
|
+
if (bounds.length !== 4 || !bounds.every(Number.isFinite)) {
|
|
19
|
+
throw new RangeError('Image bounds must contain four finite values');
|
|
20
|
+
}
|
|
21
|
+
const [left, bottom, right, top] = bounds;
|
|
22
|
+
const width = right - left;
|
|
23
|
+
const height = top - bottom;
|
|
24
|
+
if (!(width > 0) || !(height > 0) || !Number.isFinite(width) || !Number.isFinite(height)) {
|
|
25
|
+
throw new RangeError('Image bounds must have finite positive extents');
|
|
26
|
+
}
|
|
27
|
+
const count = positions.length / size;
|
|
28
|
+
const result = new Float64Array(count * 2);
|
|
29
|
+
for (let index = 0; index < count; index++) {
|
|
30
|
+
const source = Array.from({ length: size }, (_, axis) => positions[index * size + axis]);
|
|
31
|
+
if (!source.every(Number.isFinite))
|
|
32
|
+
throw new RangeError(`Invalid source vertex ${index}`);
|
|
33
|
+
const projected = transform(source);
|
|
34
|
+
if (!projected ||
|
|
35
|
+
projected.length < 2 ||
|
|
36
|
+
!Number.isFinite(projected[0]) ||
|
|
37
|
+
!Number.isFinite(projected[1])) {
|
|
38
|
+
throw new RangeError(`Invalid projected vertex ${index}`);
|
|
39
|
+
}
|
|
40
|
+
const u = (projected[0] - left) / width;
|
|
41
|
+
const v = origin === 'top-left' ? (top - projected[1]) / height : (projected[1] - bottom) / height;
|
|
42
|
+
if (!Number.isFinite(u) || !Number.isFinite(v))
|
|
43
|
+
throw new RangeError(`Invalid UV at vertex ${index}`);
|
|
44
|
+
result[index * 2] = u;
|
|
45
|
+
result[index * 2 + 1] = v;
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=project-texture-coordinates.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"project-texture-coordinates.js","sourceRoot":"","sources":["../../src/utils/project-texture-coordinates.ts"],"names":[],"mappings":"AAAA,UAAU;AACV,+BAA+B;AAC/B,4DAA4D;AAgB5D;;;GAGG;AACH,MAAM,UAAU,yBAAyB,CACvC,SAA4B,EAC5B,OAAyC;IAEzC,MAAM,EAAC,SAAS,EAAE,MAAM,EAAE,IAAI,GAAG,CAAC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D,MAAM,CAAC,KAAK,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;QACtB,MAAM,CAAC,KAAK,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;IAC5B,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/package.json
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|
|
8
8
|
},
|
|
9
|
-
"version": "5.0.0-beta.
|
|
9
|
+
"version": "5.0.0-beta.6",
|
|
10
10
|
"keywords": [
|
|
11
11
|
"geometry",
|
|
12
12
|
"gltf",
|
|
@@ -32,17 +32,29 @@
|
|
|
32
32
|
"types": "./dist/culling/index.d.ts",
|
|
33
33
|
"import": "./dist/culling/index.js",
|
|
34
34
|
"require": "./dist/culling/index.cjs"
|
|
35
|
+
},
|
|
36
|
+
"./lighting": {
|
|
37
|
+
"types": "./dist/lighting/index.d.ts",
|
|
38
|
+
"import": "./dist/lighting/index.js",
|
|
39
|
+
"require": "./dist/lighting/index.cjs"
|
|
40
|
+
},
|
|
41
|
+
"./color": {
|
|
42
|
+
"types": "./dist/color/index.d.ts",
|
|
43
|
+
"import": "./dist/color/index.js",
|
|
44
|
+
"require": "./dist/color/index.cjs"
|
|
35
45
|
}
|
|
36
46
|
},
|
|
37
47
|
"files": [
|
|
38
48
|
"dist",
|
|
39
49
|
"src",
|
|
50
|
+
"!src/**/README.md",
|
|
51
|
+
"./README.md",
|
|
40
52
|
"LICENSE",
|
|
41
53
|
"THIRD-PARTY-NOTICES.md"
|
|
42
54
|
],
|
|
43
55
|
"dependencies": {
|
|
44
|
-
"@math.gl/core": "5.0.0-beta.
|
|
56
|
+
"@math.gl/core": "5.0.0-beta.6"
|
|
45
57
|
},
|
|
46
58
|
"sideEffects": false,
|
|
47
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "c0b536175257abee1b30f687a3ca8cf5c8ca7bb9"
|
|
48
60
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
/** Numerical RGB lookup table, independent of its file encoding or renderer. */
|
|
6
|
+
export type ColorLUT = {
|
|
7
|
+
/** Independent 1D curves or one 3D RGB mapping. */
|
|
8
|
+
dimension: 1 | 3;
|
|
9
|
+
/** Number of samples along each axis. */
|
|
10
|
+
size: number;
|
|
11
|
+
/** Interleaved RGB values, red coordinate varying fastest for 3D tables. */
|
|
12
|
+
data: Float32Array;
|
|
13
|
+
/** Minimum input coordinate for each channel. */
|
|
14
|
+
domainMinimum: number[];
|
|
15
|
+
/** Maximum input coordinate for each channel. */
|
|
16
|
+
domainMaximum: number[];
|
|
17
|
+
/** Optional strictly increasing normalized knots from 0 through 1. */
|
|
18
|
+
inputGrid?: Float64Array;
|
|
19
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
export {createColorLUTSampler, applyColorLUT} from './sample-lut';
|
|
5
|
+
export type {ColorLUTSampler, ApplyColorLUTOptions} from './sample-lut';
|
|
6
|
+
export type {ColorLUT} from './color-lut-types';
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
import type {ColorLUT} from './color-lut-types';
|
|
5
|
+
|
|
6
|
+
/** Reusable RGB sampler; inputs use the authored domain and outputs retain signed/HDR values. */
|
|
7
|
+
export type ColorLUTSampler = (
|
|
8
|
+
red: number,
|
|
9
|
+
green: number,
|
|
10
|
+
blue: number
|
|
11
|
+
) => [number, number, number];
|
|
12
|
+
|
|
13
|
+
/** Options for interleaved floating-point RGB or RGBA buffers. */
|
|
14
|
+
export type ApplyColorLUTOptions = {
|
|
15
|
+
/** Number of interleaved components; default 3. Alpha is copied at float32 precision. */
|
|
16
|
+
components?: 3 | 4;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** Creates a snapshot sampler with per-channel 1D linear or red-fastest 3D trilinear interpolation. */
|
|
20
|
+
export function createColorLUTSampler(lut: ColorLUT): ColorLUTSampler {
|
|
21
|
+
const writeSample = createColorLUTWriter(lut);
|
|
22
|
+
return (red, green, blue) => {
|
|
23
|
+
const result: [number, number, number] = [0, 0, 0];
|
|
24
|
+
writeSample(red, green, blue, result, 0);
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Creates a validated snapshot writer with reusable per-sampler scratch storage. */
|
|
30
|
+
function createColorLUTWriter(lut: ColorLUT) {
|
|
31
|
+
const {dimension, size} = lut;
|
|
32
|
+
if (
|
|
33
|
+
![1, 3].includes(dimension) ||
|
|
34
|
+
!Number.isSafeInteger(size) ||
|
|
35
|
+
size < 2 ||
|
|
36
|
+
!Number.isSafeInteger(size ** dimension * 3) ||
|
|
37
|
+
lut.data.length !== size ** dimension * 3 ||
|
|
38
|
+
lut.domainMinimum.length !== 3 ||
|
|
39
|
+
lut.domainMaximum.length !== 3
|
|
40
|
+
) {
|
|
41
|
+
throw new Error('LUT: invalid calculation table');
|
|
42
|
+
}
|
|
43
|
+
const minimum = Array.from(lut.domainMinimum);
|
|
44
|
+
const maximum = Array.from(lut.domainMaximum);
|
|
45
|
+
for (let channel = 0; channel < 3; channel++) {
|
|
46
|
+
if (
|
|
47
|
+
!Number.isFinite(minimum[channel]) ||
|
|
48
|
+
!Number.isFinite(maximum[channel]) ||
|
|
49
|
+
!Number.isFinite(maximum[channel] - minimum[channel]) ||
|
|
50
|
+
maximum[channel] <= minimum[channel]
|
|
51
|
+
)
|
|
52
|
+
throw new Error('LUT: invalid calculation domain');
|
|
53
|
+
}
|
|
54
|
+
if (!lut.data.every(Number.isFinite)) throw new Error('LUT: invalid calculation samples');
|
|
55
|
+
const data = lut.data.slice();
|
|
56
|
+
const grid = lut.inputGrid?.slice();
|
|
57
|
+
if (
|
|
58
|
+
grid &&
|
|
59
|
+
(grid.length !== size ||
|
|
60
|
+
grid[0] !== 0 ||
|
|
61
|
+
grid[size - 1] !== 1 ||
|
|
62
|
+
grid.some(
|
|
63
|
+
(value, index) => !Number.isFinite(value) || (index > 0 && value <= grid[index - 1])
|
|
64
|
+
))
|
|
65
|
+
) {
|
|
66
|
+
throw new Error('LUT: invalid nonuniform input grid');
|
|
67
|
+
}
|
|
68
|
+
const coordinates = [0, 0, 0];
|
|
69
|
+
const indices = [0, 0, 0];
|
|
70
|
+
const weights = [0, 0, 0];
|
|
71
|
+
const accumulated = [0, 0, 0];
|
|
72
|
+
return (
|
|
73
|
+
red: number,
|
|
74
|
+
green: number,
|
|
75
|
+
blue: number,
|
|
76
|
+
result: [number, number, number] | Float32Array,
|
|
77
|
+
resultOffset: number
|
|
78
|
+
): void => {
|
|
79
|
+
coordinates[0] = red;
|
|
80
|
+
coordinates[1] = green;
|
|
81
|
+
coordinates[2] = blue;
|
|
82
|
+
for (let channel = 0; channel < 3; channel++) {
|
|
83
|
+
const value = coordinates[channel];
|
|
84
|
+
if (!Number.isFinite(value)) throw new Error('LUT: input colors must be finite');
|
|
85
|
+
const normalized =
|
|
86
|
+
value <= minimum[channel]
|
|
87
|
+
? 0
|
|
88
|
+
: value >= maximum[channel]
|
|
89
|
+
? 1
|
|
90
|
+
: (value - minimum[channel]) / (maximum[channel] - minimum[channel]);
|
|
91
|
+
writeLUTInterval(normalized, size, grid, indices, weights, channel);
|
|
92
|
+
}
|
|
93
|
+
accumulated[0] = 0;
|
|
94
|
+
accumulated[1] = 0;
|
|
95
|
+
accumulated[2] = 0;
|
|
96
|
+
if (dimension === 1) {
|
|
97
|
+
for (let channel = 0; channel < 3; channel++) {
|
|
98
|
+
const offset = indices[channel] * 3 + channel;
|
|
99
|
+
accumulated[channel] =
|
|
100
|
+
data[offset] * (1 - weights[channel]) + data[offset + 3] * weights[channel];
|
|
101
|
+
}
|
|
102
|
+
} else {
|
|
103
|
+
for (let blueCorner = 0; blueCorner < 2; blueCorner++) {
|
|
104
|
+
for (let greenCorner = 0; greenCorner < 2; greenCorner++) {
|
|
105
|
+
for (let redCorner = 0; redCorner < 2; redCorner++) {
|
|
106
|
+
const weight =
|
|
107
|
+
(redCorner ? weights[0] : 1 - weights[0]) *
|
|
108
|
+
(greenCorner ? weights[1] : 1 - weights[1]) *
|
|
109
|
+
(blueCorner ? weights[2] : 1 - weights[2]);
|
|
110
|
+
const offset =
|
|
111
|
+
(((indices[2] + blueCorner) * size + indices[1] + greenCorner) * size +
|
|
112
|
+
indices[0] +
|
|
113
|
+
redCorner) *
|
|
114
|
+
3;
|
|
115
|
+
for (let channel = 0; channel < 3; channel++)
|
|
116
|
+
accumulated[channel] += data[offset + channel] * weight;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
for (let channel = 0; channel < 3; channel++)
|
|
122
|
+
result[resultOffset + channel] = accumulated[channel];
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Applies a LUT to floating-point pixels without changing the source or applying transfer functions. */
|
|
127
|
+
export function applyColorLUT(
|
|
128
|
+
lut: ColorLUT,
|
|
129
|
+
colors: Float32Array | Float64Array,
|
|
130
|
+
options: ApplyColorLUTOptions = {}
|
|
131
|
+
): Float32Array {
|
|
132
|
+
const {components = 3} = options;
|
|
133
|
+
if (![3, 4].includes(components) || colors.length % components !== 0)
|
|
134
|
+
throw new Error('LUT: invalid pixel component count');
|
|
135
|
+
const writeSample = createColorLUTWriter(lut);
|
|
136
|
+
const result = new Float32Array(colors.length);
|
|
137
|
+
for (let offset = 0; offset < colors.length; offset += components) {
|
|
138
|
+
writeSample(colors[offset], colors[offset + 1], colors[offset + 2], result, offset);
|
|
139
|
+
if (components === 4) {
|
|
140
|
+
const alpha = colors[offset + 3];
|
|
141
|
+
result[offset + 3] = alpha;
|
|
142
|
+
if (!Number.isFinite(alpha) || !Number.isFinite(result[offset + 3]))
|
|
143
|
+
throw new Error('LUT: alpha cannot be represented as float32');
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return result;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Finds normalized uniform/nonuniform knots, retaining the final endpoint in the last interval. */
|
|
150
|
+
function writeLUTInterval(
|
|
151
|
+
value: number,
|
|
152
|
+
size: number,
|
|
153
|
+
grid: Float64Array | undefined,
|
|
154
|
+
indices: number[],
|
|
155
|
+
weights: number[],
|
|
156
|
+
channel: number
|
|
157
|
+
): void {
|
|
158
|
+
if (!grid) {
|
|
159
|
+
const coordinate = value * (size - 1);
|
|
160
|
+
const lower = Math.min(Math.floor(coordinate), size - 2);
|
|
161
|
+
indices[channel] = lower;
|
|
162
|
+
weights[channel] = coordinate - lower;
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
let lower = 0;
|
|
166
|
+
let upper = size - 1;
|
|
167
|
+
while (upper - lower > 1) {
|
|
168
|
+
const midpoint = Math.floor((lower + upper) / 2);
|
|
169
|
+
if (value < grid[midpoint]) upper = midpoint;
|
|
170
|
+
else lower = midpoint;
|
|
171
|
+
}
|
|
172
|
+
lower = Math.min(lower, size - 2);
|
|
173
|
+
indices[channel] = lower;
|
|
174
|
+
weights[channel] = (value - grid[lower]) / (grid[lower + 1] - grid[lower]);
|
|
175
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
import type {Bounds, Bounds2D, Bounds3D, NumericArray} from '@math.gl/core';
|
|
5
|
+
|
|
6
|
+
type ReadonlyBounds = readonly [ArrayLike<number>, ArrayLike<number>];
|
|
7
|
+
function dimension(bounds: ReadonlyBounds): number {
|
|
8
|
+
const n = bounds[0].length;
|
|
9
|
+
if (
|
|
10
|
+
(n !== 2 && n !== 3) ||
|
|
11
|
+
bounds[1].length !== n ||
|
|
12
|
+
[...Array.from(bounds[0]), ...Array.from(bounds[1])].some(Number.isNaN)
|
|
13
|
+
) {
|
|
14
|
+
throw new RangeError('Expected 2D or 3D bounds without NaN');
|
|
15
|
+
}
|
|
16
|
+
const empty = Array.from({length: n}, (_, i) => bounds[0][i] > bounds[1][i]).some(Boolean);
|
|
17
|
+
if (
|
|
18
|
+
!empty &&
|
|
19
|
+
[...Array.from(bounds[0]), ...Array.from(bounds[1])].some(x => !Number.isFinite(x))
|
|
20
|
+
) {
|
|
21
|
+
throw new RangeError('Nonempty bounds must be finite');
|
|
22
|
+
}
|
|
23
|
+
return n;
|
|
24
|
+
}
|
|
25
|
+
function pair(a: ReadonlyBounds, b: ReadonlyBounds): number {
|
|
26
|
+
const n = dimension(a);
|
|
27
|
+
if (dimension(b) !== n) throw new RangeError('Bounds dimensions must match');
|
|
28
|
+
return n;
|
|
29
|
+
}
|
|
30
|
+
function point(p: ArrayLike<number>, n: number): void {
|
|
31
|
+
if (p.length !== n || Array.from(p).some(x => !Number.isFinite(x)))
|
|
32
|
+
throw new RangeError('Expected a finite point of matching dimension');
|
|
33
|
+
}
|
|
34
|
+
export function createEmptyBounds(dimension: 2): Bounds2D;
|
|
35
|
+
export function createEmptyBounds(dimension: 3): Bounds3D;
|
|
36
|
+
export function createEmptyBounds(dimension: 2 | 3): Bounds;
|
|
37
|
+
export function createEmptyBounds(dimension: 2 | 3): Bounds {
|
|
38
|
+
if (dimension !== 2 && dimension !== 3) throw new RangeError('Expected dimension 2 or 3');
|
|
39
|
+
return [new Array(dimension).fill(Infinity), new Array(dimension).fill(-Infinity)] as Bounds;
|
|
40
|
+
}
|
|
41
|
+
/** Any inverted axis denotes empty bounds. Equal endpoints describe a nonempty point. */
|
|
42
|
+
export function isEmptyBounds(bounds: ReadonlyBounds): boolean {
|
|
43
|
+
return Array.from({length: dimension(bounds)}, (_, i) => bounds[0][i] > bounds[1][i]).some(
|
|
44
|
+
Boolean
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
export function boundsContainsPoint(bounds: ReadonlyBounds, p: ArrayLike<number>): boolean {
|
|
48
|
+
point(p, dimension(bounds));
|
|
49
|
+
return (
|
|
50
|
+
!isEmptyBounds(bounds) && Array.from(p).every((x, i) => x >= bounds[0][i] && x <= bounds[1][i])
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
export function boundsContainsBounds(a: ReadonlyBounds, b: ReadonlyBounds): boolean {
|
|
54
|
+
const n = pair(a, b);
|
|
55
|
+
return (
|
|
56
|
+
isEmptyBounds(b) ||
|
|
57
|
+
(!isEmptyBounds(a) &&
|
|
58
|
+
Array.from({length: n}, (_, i) => a[0][i] <= b[0][i] && a[1][i] >= b[1][i]).every(Boolean))
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
export function boundsIntersect(a: ReadonlyBounds, b: ReadonlyBounds): boolean {
|
|
62
|
+
const n = pair(a, b);
|
|
63
|
+
return (
|
|
64
|
+
!isEmptyBounds(a) &&
|
|
65
|
+
!isEmptyBounds(b) &&
|
|
66
|
+
Array.from({length: n}, (_, i) => a[0][i] <= b[1][i] && b[0][i] <= a[1][i]).every(Boolean)
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
function store(values: Bounds, out: Bounds = createEmptyBounds(values[0].length as 2 | 3)): Bounds {
|
|
70
|
+
if (out[0].length !== values[0].length || out[1].length !== values[0].length) {
|
|
71
|
+
throw new RangeError('Output bounds dimensions must match');
|
|
72
|
+
}
|
|
73
|
+
for (let j = 0; j < 2; j++) for (let i = 0; i < values[j].length; i++) out[j][i] = values[j][i];
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
export function unionBounds(a: ReadonlyBounds, b: ReadonlyBounds, out?: Bounds): Bounds {
|
|
77
|
+
const n = pair(a, b);
|
|
78
|
+
const values = createEmptyBounds(n as 2 | 3);
|
|
79
|
+
const ea = isEmptyBounds(a),
|
|
80
|
+
eb = isEmptyBounds(b);
|
|
81
|
+
for (let i = 0; i < n; i++) {
|
|
82
|
+
values[0][i] = Math.min(ea ? Infinity : a[0][i], eb ? Infinity : b[0][i]);
|
|
83
|
+
values[1][i] = Math.max(ea ? -Infinity : a[1][i], eb ? -Infinity : b[1][i]);
|
|
84
|
+
}
|
|
85
|
+
return store(values, out);
|
|
86
|
+
}
|
|
87
|
+
export function intersectBounds(a: ReadonlyBounds, b: ReadonlyBounds, out?: Bounds): Bounds {
|
|
88
|
+
const n = pair(a, b),
|
|
89
|
+
values = createEmptyBounds(n as 2 | 3);
|
|
90
|
+
if (boundsIntersect(a, b))
|
|
91
|
+
for (let i = 0; i < n; i++) {
|
|
92
|
+
values[0][i] = Math.max(a[0][i], b[0][i]);
|
|
93
|
+
values[1][i] = Math.min(a[1][i], b[1][i]);
|
|
94
|
+
}
|
|
95
|
+
return store(values, out);
|
|
96
|
+
}
|
|
97
|
+
export function expandBoundsByPoint(
|
|
98
|
+
bounds: ReadonlyBounds,
|
|
99
|
+
p: ArrayLike<number>,
|
|
100
|
+
out?: Bounds
|
|
101
|
+
): Bounds {
|
|
102
|
+
point(p, dimension(bounds));
|
|
103
|
+
return unionBounds(bounds, [p, p], out);
|
|
104
|
+
}
|
|
105
|
+
/** Negative padding shrinks bounds; an inverted result becomes canonical empty bounds. */
|
|
106
|
+
export function expandBounds(bounds: ReadonlyBounds, padding: number, out?: Bounds): Bounds {
|
|
107
|
+
if (!Number.isFinite(padding)) throw new RangeError('Padding must be finite');
|
|
108
|
+
const n = dimension(bounds),
|
|
109
|
+
values = createEmptyBounds(n as 2 | 3);
|
|
110
|
+
if (!isEmptyBounds(bounds))
|
|
111
|
+
for (let i = 0; i < n; i++) {
|
|
112
|
+
values[0][i] = bounds[0][i] - padding;
|
|
113
|
+
values[1][i] = bounds[1][i] + padding;
|
|
114
|
+
}
|
|
115
|
+
return store(isEmptyBounds(values) ? createEmptyBounds(n as 2 | 3) : values, out);
|
|
116
|
+
}
|
|
117
|
+
export function clampPointToBounds(
|
|
118
|
+
bounds: ReadonlyBounds,
|
|
119
|
+
p: ArrayLike<number>,
|
|
120
|
+
out: NumericArray = new Array(p.length)
|
|
121
|
+
): NumericArray | null {
|
|
122
|
+
point(p, dimension(bounds));
|
|
123
|
+
if (out.length < p.length) throw new RangeError('Result buffer is too short');
|
|
124
|
+
if (isEmptyBounds(bounds)) return null;
|
|
125
|
+
const result = Array.from(p, (x, i) => Math.max(bounds[0][i], Math.min(bounds[1][i], x)));
|
|
126
|
+
for (let i = 0; i < result.length; i++) out[i] = result[i];
|
|
127
|
+
return out;
|
|
128
|
+
}
|
|
129
|
+
function radius(r: number): void {
|
|
130
|
+
if (!Number.isFinite(r) || r < 0) throw new RangeError('Radius must be finite and nonnegative');
|
|
131
|
+
}
|
|
132
|
+
export function spheresIntersect(
|
|
133
|
+
a: ArrayLike<number>,
|
|
134
|
+
ar: number,
|
|
135
|
+
b: ArrayLike<number>,
|
|
136
|
+
br: number
|
|
137
|
+
): boolean {
|
|
138
|
+
point(a, 3);
|
|
139
|
+
point(b, 3);
|
|
140
|
+
radius(ar);
|
|
141
|
+
radius(br);
|
|
142
|
+
return Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]) <= ar + br;
|
|
143
|
+
}
|
|
144
|
+
export function boundsIntersectSphere(
|
|
145
|
+
bounds: ReadonlyBounds,
|
|
146
|
+
center: ArrayLike<number>,
|
|
147
|
+
r: number
|
|
148
|
+
): boolean {
|
|
149
|
+
radius(r);
|
|
150
|
+
const closest = clampPointToBounds(bounds, center);
|
|
151
|
+
return closest !== null && Math.hypot(...Array.from(center, (x, i) => x - closest[i])) <= r;
|
|
152
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
import {getProjectionDepthRange, type ProjectionDepthOptions} from '@math.gl/core';
|
|
5
|
+
import {Plane} from './lib/plane';
|
|
6
|
+
|
|
7
|
+
/** Inward planes: left, right, bottom, top, near, far. An infinite far plane is omitted. */
|
|
8
|
+
export function extractFrustumPlanes(
|
|
9
|
+
matrix: ArrayLike<number>,
|
|
10
|
+
options: ProjectionDepthOptions = {}
|
|
11
|
+
): Plane[] {
|
|
12
|
+
if (matrix.length !== 16 || Array.from(matrix).some(x => !Number.isFinite(x)))
|
|
13
|
+
throw new RangeError('Expected a finite view-projection matrix');
|
|
14
|
+
const [near, far] = getProjectionDepthRange(options);
|
|
15
|
+
const low = Math.min(near, far),
|
|
16
|
+
high = Math.max(near, far);
|
|
17
|
+
const row = (i: number) => [matrix[i], matrix[i + 4], matrix[i + 8], matrix[i + 12]];
|
|
18
|
+
const x = row(0),
|
|
19
|
+
y = row(1),
|
|
20
|
+
z = row(2),
|
|
21
|
+
w = row(3);
|
|
22
|
+
const combine = (a: number[], b: number[], factor: number) => a.map((v, i) => v + factor * b[i]);
|
|
23
|
+
const lo = combine(z, w, -low),
|
|
24
|
+
hi = w.map((v, i) => high * v - z[i]);
|
|
25
|
+
const coefficients = [
|
|
26
|
+
combine(w, x, 1),
|
|
27
|
+
combine(w, x, -1),
|
|
28
|
+
combine(w, y, 1),
|
|
29
|
+
combine(w, y, -1),
|
|
30
|
+
...(options.reversedDepth ? [hi, lo] : [lo, hi])
|
|
31
|
+
];
|
|
32
|
+
const planes: Plane[] = [];
|
|
33
|
+
for (const [a, b, c, d] of coefficients) {
|
|
34
|
+
const length = Math.hypot(a, b, c);
|
|
35
|
+
if (length === 0) {
|
|
36
|
+
if (d < 0) throw new RangeError('Empty clipping volume');
|
|
37
|
+
} else planes.push(new Plane([a / length, b / length, c / length], d / length));
|
|
38
|
+
}
|
|
39
|
+
if (planes.length < 4) throw new RangeError('Degenerate clipping volume');
|
|
40
|
+
return planes;
|
|
41
|
+
}
|