@math.gl/geometry 5.0.0-beta.1 → 5.0.0-beta.10
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 +944 -508
- package/dist/culling/index.cjs.map +3 -3
- package/dist/culling/index.d.ts +7 -1
- package/dist/culling/index.d.ts.map +1 -1
- package/dist/culling/index.js +4 -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/culling/screen-ray.d.ts +22 -0
- package/dist/culling/screen-ray.d.ts.map +1 -0
- package/dist/culling/screen-ray.js +55 -0
- package/dist/culling/screen-ray.js.map +1 -0
- 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 +1922 -817
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +15 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1
- 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/polygon/earcut.d.ts.map +1 -1
- package/dist/polygon/earcut.js +100 -34
- package/dist/polygon/earcut.js.map +1 -1
- package/dist/polygon/index.d.ts +2 -0
- package/dist/polygon/index.d.ts.map +1 -1
- package/dist/polygon/index.js +1 -0
- package/dist/polygon/index.js.map +1 -1
- package/dist/polygon/pole-of-inaccessibility.d.ts +21 -0
- package/dist/polygon/pole-of-inaccessibility.d.ts.map +1 -0
- package/dist/polygon/pole-of-inaccessibility.js +285 -0
- package/dist/polygon/pole-of-inaccessibility.js.map +1 -0
- package/dist/polygon/subdivide-triangle-mesh.d.ts +10 -0
- package/dist/polygon/subdivide-triangle-mesh.d.ts.map +1 -1
- package/dist/polygon/subdivide-triangle-mesh.js +13 -3
- package/dist/polygon/subdivide-triangle-mesh.js.map +1 -1
- package/dist/utils/compose-attribute-provenance.d.ts +13 -0
- package/dist/utils/compose-attribute-provenance.d.ts.map +1 -0
- package/dist/utils/compose-attribute-provenance.js +63 -0
- package/dist/utils/compose-attribute-provenance.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/dist/utils/qualify-surface-transition.d.ts +21 -0
- package/dist/utils/qualify-surface-transition.d.ts.map +1 -0
- package/dist/utils/qualify-surface-transition.js +140 -0
- package/dist/utils/qualify-surface-transition.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 +39 -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/culling/screen-ray.ts +75 -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 +51 -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/polygon/earcut.ts +98 -37
- package/src/polygon/index.ts +6 -0
- package/src/polygon/pole-of-inaccessibility.ts +330 -0
- package/src/polygon/subdivide-triangle-mesh.ts +22 -2
- package/src/utils/compose-attribute-provenance.ts +74 -0
- package/src/utils/project-bounds.ts +154 -0
- package/src/utils/project-texture-coordinates.ts +67 -0
- package/src/utils/qualify-surface-transition.ts +169 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
import type {AttributeInterpolationProvenance} from './interpolate-packed-attributes';
|
|
5
|
+
|
|
6
|
+
/** Compose child→intermediate and intermediate→original barycentric provenance.
|
|
7
|
+
* Interpolate attributes only after composition, avoiding repeated integer rounding.
|
|
8
|
+
* More than three distinct contributors rejects: this contract describes source
|
|
9
|
+
* triangles, not arbitrary sparse blending. Zero-weight indices are ignored.
|
|
10
|
+
*/
|
|
11
|
+
export function composeAttributeProvenance(
|
|
12
|
+
parent: AttributeInterpolationProvenance,
|
|
13
|
+
child: AttributeInterpolationProvenance,
|
|
14
|
+
options: {maxVertices?: number} = {}
|
|
15
|
+
): {sourceVertexIndices: Uint32Array; sourceVertexWeights: Float64Array} {
|
|
16
|
+
const limit = options.maxVertices ?? 65536;
|
|
17
|
+
if (!Number.isSafeInteger(limit) || limit < 0 || limit > 0xffffffff)
|
|
18
|
+
throw new RangeError('Invalid provenance vertex limit');
|
|
19
|
+
validate(parent);
|
|
20
|
+
validate(child, parent.sourceVertexIndices.length / 3);
|
|
21
|
+
if (child.sourceVertexIndices.length / 3 > limit)
|
|
22
|
+
throw new RangeError('Provenance composition exceeded maxVertices');
|
|
23
|
+
const indices = new Uint32Array(child.sourceVertexIndices.length);
|
|
24
|
+
const weights = new Float64Array(indices.length);
|
|
25
|
+
for (let vertex = 0; vertex < indices.length / 3; vertex++) {
|
|
26
|
+
const contributors = new Map<number, number>();
|
|
27
|
+
for (let j = 0; j < 3; j++) {
|
|
28
|
+
const weight = child.sourceVertexWeights[3 * vertex + j];
|
|
29
|
+
if (!weight) continue;
|
|
30
|
+
const intermediate = child.sourceVertexIndices[3 * vertex + j];
|
|
31
|
+
for (let k = 0; k < 3; k++) {
|
|
32
|
+
const w = weight * parent.sourceVertexWeights[3 * intermediate + k];
|
|
33
|
+
if (!w) continue;
|
|
34
|
+
const index = parent.sourceVertexIndices[3 * intermediate + k];
|
|
35
|
+
contributors.set(index, (contributors.get(index) ?? 0) + w);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (contributors.size > 3)
|
|
39
|
+
throw new RangeError('Composed vertex spans multiple source triangles');
|
|
40
|
+
const sum = Array.from(contributors.values()).reduce((a, b) => a + b, 0);
|
|
41
|
+
let slot = 3 * vertex;
|
|
42
|
+
for (const [index, weight] of contributors) {
|
|
43
|
+
indices[slot] = index;
|
|
44
|
+
weights[slot++] = weight / sum;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return {sourceVertexIndices: indices, sourceVertexWeights: weights};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function validate(p: AttributeInterpolationProvenance, count = 0x100000000) {
|
|
51
|
+
if (
|
|
52
|
+
!Number.isSafeInteger(p.sourceVertexIndices.length) ||
|
|
53
|
+
p.sourceVertexIndices.length < 0 ||
|
|
54
|
+
p.sourceVertexIndices.length % 3 ||
|
|
55
|
+
p.sourceVertexIndices.length !== p.sourceVertexWeights.length
|
|
56
|
+
)
|
|
57
|
+
throw new RangeError('Invalid provenance lengths');
|
|
58
|
+
for (let i = 0; i < p.sourceVertexIndices.length; i += 3) {
|
|
59
|
+
let sum = 0;
|
|
60
|
+
for (let j = 0; j < 3; j++) {
|
|
61
|
+
const w = p.sourceVertexWeights[i + j],
|
|
62
|
+
index = p.sourceVertexIndices[i + j];
|
|
63
|
+
if (
|
|
64
|
+
!Number.isFinite(w) ||
|
|
65
|
+
w < 0 ||
|
|
66
|
+
w > 1 ||
|
|
67
|
+
(w > 0 && (!Number.isSafeInteger(index) || index < 0 || index >= count))
|
|
68
|
+
)
|
|
69
|
+
throw new RangeError('Invalid provenance contributor');
|
|
70
|
+
sum += w;
|
|
71
|
+
}
|
|
72
|
+
if (Math.abs(sum - 1) > 1e-10) throw new RangeError('Provenance weights must sum to one');
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
/** A finite, continuous source box with ordered minimum and maximum coordinates. */
|
|
6
|
+
export type ProjectionSourceBounds = readonly [ArrayLike<number>, ArrayLike<number>];
|
|
7
|
+
|
|
8
|
+
/** Controls bounded sampling of a prepared synchronous coordinate transform. */
|
|
9
|
+
export type ProjectBoundsOptions = {
|
|
10
|
+
/** Receives a fresh source array. Null/nonfinite output rejects the whole result.
|
|
11
|
+
* Exceptions propagate unchanged; no projection engine is imported by this helper.
|
|
12
|
+
*/
|
|
13
|
+
transform: (position: readonly number[]) => ArrayLike<number> | null;
|
|
14
|
+
/** Output dimension, 2 or 3. Defaults to the source dimension. */
|
|
15
|
+
targetSize?: 2 | 3;
|
|
16
|
+
/** Equal subdivisions on each nonzero source axis. Positive integer; default 8. */
|
|
17
|
+
subdivisions?: number;
|
|
18
|
+
/** Maximum transform calls. Positive safe integer; default 262144. */
|
|
19
|
+
maxSamples?: number;
|
|
20
|
+
/** Optional caller-qualified global Lipschitz constant for EACH output component,
|
|
21
|
+
* measured against Euclidean source distance over the ENTIRE source box.
|
|
22
|
+
* Requires a finite continuous transform throughout the box, not merely at samples.
|
|
23
|
+
* This contract cannot be inferred or verified from point samples.
|
|
24
|
+
*/
|
|
25
|
+
lipschitz?: readonly number[];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** Detached bounds plus an explicit qualification; invalid results never expose partial bounds. */
|
|
29
|
+
export type ProjectedBoundsResult =
|
|
30
|
+
| {
|
|
31
|
+
/** 'sampled' is for approximate fitting only; 'conservative' requires the supplied contract. */
|
|
32
|
+
status: 'sampled' | 'conservative';
|
|
33
|
+
/** Output minimum and maximum vectors; safe for culling only in conservative mode. */
|
|
34
|
+
bounds: [number[], number[]];
|
|
35
|
+
/** Number of transform calls. */
|
|
36
|
+
sampleCount: number;
|
|
37
|
+
}
|
|
38
|
+
| {
|
|
39
|
+
/** A null/nonfinite transform sample or unrepresentable enclosure was encountered. */
|
|
40
|
+
status: 'invalid';
|
|
41
|
+
/** No partial result is safe to use. */
|
|
42
|
+
bounds: null;
|
|
43
|
+
/** Number of transform calls, including an invalid sample if applicable. */
|
|
44
|
+
sampleCount: number;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** Projects a lattice covering corners, edges, faces and the interior of a 2D/3D box.
|
|
48
|
+
* Sampling alone cannot establish an enclosure for a nonlinear transform. When qualified
|
|
49
|
+
* global Lipschitz constants are supplied, inflate by the lattice covering radius instead
|
|
50
|
+
* of claiming corner/sample extrema are conservative. Split seams and domains beforehand.
|
|
51
|
+
*/
|
|
52
|
+
export function projectBounds(
|
|
53
|
+
sourceBounds: ProjectionSourceBounds,
|
|
54
|
+
options: ProjectBoundsOptions
|
|
55
|
+
): ProjectedBoundsResult {
|
|
56
|
+
const [minimum, maximum] = sourceBounds;
|
|
57
|
+
const size = minimum.length;
|
|
58
|
+
const {transform, targetSize = size, subdivisions = 8, maxSamples = 262144, lipschitz} = options;
|
|
59
|
+
if (
|
|
60
|
+
(size !== 2 && size !== 3) ||
|
|
61
|
+
maximum.length !== size ||
|
|
62
|
+
(targetSize !== 2 && targetSize !== 3)
|
|
63
|
+
) {
|
|
64
|
+
throw new RangeError('Bounds and target dimension must be 2 or 3');
|
|
65
|
+
}
|
|
66
|
+
if (
|
|
67
|
+
!Number.isSafeInteger(subdivisions) ||
|
|
68
|
+
subdivisions < 1 ||
|
|
69
|
+
!Number.isSafeInteger(maxSamples) ||
|
|
70
|
+
maxSamples < 1
|
|
71
|
+
) {
|
|
72
|
+
throw new RangeError('Subdivision and sample budgets must be positive safe integers');
|
|
73
|
+
}
|
|
74
|
+
if (
|
|
75
|
+
lipschitz &&
|
|
76
|
+
(lipschitz.length !== targetSize ||
|
|
77
|
+
!Array.from(lipschitz).every(value => Number.isFinite(value) && value >= 0))
|
|
78
|
+
) {
|
|
79
|
+
throw new RangeError('Lipschitz constants must be finite, nonnegative and match target size');
|
|
80
|
+
}
|
|
81
|
+
const widths: number[] = [];
|
|
82
|
+
const counts: number[] = [];
|
|
83
|
+
let count = 1;
|
|
84
|
+
for (let axis = 0; axis < size; axis++) {
|
|
85
|
+
const width = maximum[axis] - minimum[axis];
|
|
86
|
+
if (
|
|
87
|
+
!Number.isFinite(minimum[axis]) ||
|
|
88
|
+
!Number.isFinite(maximum[axis]) ||
|
|
89
|
+
!Number.isFinite(width) ||
|
|
90
|
+
width < 0
|
|
91
|
+
) {
|
|
92
|
+
throw new RangeError('Source bounds must have finite ordered coordinates and extents');
|
|
93
|
+
}
|
|
94
|
+
widths.push(width);
|
|
95
|
+
counts.push(width === 0 ? 1 : subdivisions + 1);
|
|
96
|
+
count *= counts[axis];
|
|
97
|
+
if (!Number.isSafeInteger(count) || count > maxSamples) {
|
|
98
|
+
throw new RangeError('Projected bounds exceed the sample budget');
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const lower = new Array<number>(targetSize).fill(Infinity);
|
|
102
|
+
const upper = new Array<number>(targetSize).fill(-Infinity);
|
|
103
|
+
for (let index = 0; index < count; index++) {
|
|
104
|
+
let remaining = index;
|
|
105
|
+
const source: number[] = [];
|
|
106
|
+
for (let axis = 0; axis < size; axis++) {
|
|
107
|
+
const step = remaining % counts[axis];
|
|
108
|
+
remaining = Math.floor(remaining / counts[axis]);
|
|
109
|
+
source.push(
|
|
110
|
+
step === 0
|
|
111
|
+
? minimum[axis]
|
|
112
|
+
: step === subdivisions
|
|
113
|
+
? maximum[axis]
|
|
114
|
+
: minimum[axis] + widths[axis] * (step / subdivisions)
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
const projected = transform(source);
|
|
118
|
+
if (!projected || projected.length < targetSize) {
|
|
119
|
+
return {status: 'invalid', bounds: null, sampleCount: index + 1};
|
|
120
|
+
}
|
|
121
|
+
for (let axis = 0; axis < targetSize; axis++) {
|
|
122
|
+
if (!Number.isFinite(projected[axis]))
|
|
123
|
+
return {status: 'invalid', bounds: null, sampleCount: index + 1};
|
|
124
|
+
lower[axis] = Math.min(lower[axis], projected[axis]);
|
|
125
|
+
upper[axis] = Math.max(upper[axis], projected[axis]);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (lipschitz) {
|
|
129
|
+
const radius = Math.hypot(
|
|
130
|
+
...widths.map(
|
|
131
|
+
(width, axis) =>
|
|
132
|
+
width / subdivisions / 2 +
|
|
133
|
+
4 * Number.EPSILON * Math.max(Math.abs(minimum[axis]), Math.abs(maximum[axis]))
|
|
134
|
+
)
|
|
135
|
+
);
|
|
136
|
+
for (let axis = 0; axis < targetSize; axis++) {
|
|
137
|
+
const displacement = lipschitz[axis] * radius;
|
|
138
|
+
const roundoff =
|
|
139
|
+
8 *
|
|
140
|
+
Number.EPSILON *
|
|
141
|
+
Math.max(1, Math.abs(lower[axis]), Math.abs(upper[axis]), displacement);
|
|
142
|
+
lower[axis] -= displacement + roundoff;
|
|
143
|
+
upper[axis] += displacement + roundoff;
|
|
144
|
+
if (!Number.isFinite(lower[axis]) || !Number.isFinite(upper[axis])) {
|
|
145
|
+
return {status: 'invalid', bounds: null, sampleCount: count};
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return {
|
|
150
|
+
status: lipschitz ? 'conservative' : 'sampled',
|
|
151
|
+
bounds: [lower, upper],
|
|
152
|
+
sampleCount: count
|
|
153
|
+
};
|
|
154
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
/** Projects packed source vertices into an explicitly located image rectangle. */
|
|
6
|
+
export type ProjectTextureCoordinatesOptions = {
|
|
7
|
+
/** Prepared synchronous source-to-image transform. Receives a fresh source array.
|
|
8
|
+
* Null or nonfinite XY rejects the operation; exceptions propagate unchanged.
|
|
9
|
+
*/
|
|
10
|
+
transform: (position: readonly number[]) => ArrayLike<number> | null;
|
|
11
|
+
/** Image rectangle [left, bottom, right, top], in the transform's output coordinates. */
|
|
12
|
+
bounds: readonly [number, number, number, number];
|
|
13
|
+
/** Number of packed source components per vertex. Default 3. */
|
|
14
|
+
size?: 2 | 3;
|
|
15
|
+
/** Texture origin. Default 'top-left'; bottom-left makes V increase with image Y. */
|
|
16
|
+
origin?: 'top-left' | 'bottom-left';
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** Generates detached float64 UVs without changing positions, indices or vertex order.
|
|
20
|
+
* Outside-image coordinates remain outside [0, 1]. This performs no subdivision,
|
|
21
|
+
* seam inference, domain clipping, GPU readback, or asynchronous preparation.
|
|
22
|
+
*/
|
|
23
|
+
export function projectTextureCoordinates(
|
|
24
|
+
positions: ArrayLike<number>,
|
|
25
|
+
options: ProjectTextureCoordinatesOptions
|
|
26
|
+
): Float64Array {
|
|
27
|
+
const {transform, bounds, size = 3, origin = 'top-left'} = options;
|
|
28
|
+
if (size !== 2 && size !== 3) throw new RangeError('Source size must be 2 or 3');
|
|
29
|
+
if (!Number.isSafeInteger(positions.length) || positions.length < 0 || positions.length % size) {
|
|
30
|
+
throw new RangeError('Positions must contain complete packed vertices');
|
|
31
|
+
}
|
|
32
|
+
if (origin !== 'top-left' && origin !== 'bottom-left') {
|
|
33
|
+
throw new RangeError('Invalid texture origin');
|
|
34
|
+
}
|
|
35
|
+
if (bounds.length !== 4 || !bounds.every(Number.isFinite)) {
|
|
36
|
+
throw new RangeError('Image bounds must contain four finite values');
|
|
37
|
+
}
|
|
38
|
+
const [left, bottom, right, top] = bounds;
|
|
39
|
+
const width = right - left;
|
|
40
|
+
const height = top - bottom;
|
|
41
|
+
if (!(width > 0) || !(height > 0) || !Number.isFinite(width) || !Number.isFinite(height)) {
|
|
42
|
+
throw new RangeError('Image bounds must have finite positive extents');
|
|
43
|
+
}
|
|
44
|
+
const count = positions.length / size;
|
|
45
|
+
const result = new Float64Array(count * 2);
|
|
46
|
+
for (let index = 0; index < count; index++) {
|
|
47
|
+
const source = Array.from({length: size}, (_, axis) => positions[index * size + axis]);
|
|
48
|
+
if (!source.every(Number.isFinite)) throw new RangeError(`Invalid source vertex ${index}`);
|
|
49
|
+
const projected = transform(source);
|
|
50
|
+
if (
|
|
51
|
+
!projected ||
|
|
52
|
+
projected.length < 2 ||
|
|
53
|
+
!Number.isFinite(projected[0]) ||
|
|
54
|
+
!Number.isFinite(projected[1])
|
|
55
|
+
) {
|
|
56
|
+
throw new RangeError(`Invalid projected vertex ${index}`);
|
|
57
|
+
}
|
|
58
|
+
const u = (projected[0] - left) / width;
|
|
59
|
+
const v =
|
|
60
|
+
origin === 'top-left' ? (top - projected[1]) / height : (projected[1] - bottom) / height;
|
|
61
|
+
if (!Number.isFinite(u) || !Number.isFinite(v))
|
|
62
|
+
throw new RangeError(`Invalid UV at vertex ${index}`);
|
|
63
|
+
result[index * 2] = u;
|
|
64
|
+
result[index * 2 + 1] = v;
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
import type {TriangleMesh} from '../polygon/subdivide-triangle-mesh';
|
|
5
|
+
|
|
6
|
+
export type SurfaceTransitionQualification = {
|
|
7
|
+
/** A fixed indexed disk stays a graph over XY throughout linear vertex interpolation. */
|
|
8
|
+
scope: 'convex-xy-disk-linear';
|
|
9
|
+
minimumProjectedDoubleArea: number;
|
|
10
|
+
boundary: Uint32Array;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/** Qualify a conservative subset of 3D mesh morphs, without time sampling.
|
|
14
|
+
* Input is an oriented, connected triangulated disk. Both endpoints share indices.
|
|
15
|
+
* Every triangle must retain positive XY orientation and the boundary must stay
|
|
16
|
+
* convex throughout linear vertex motion. Quadratic extrema test the full [0,1].
|
|
17
|
+
* This excludes holes, folded surfaces, most global globe meshes and curved vertex
|
|
18
|
+
* trajectories. It qualifies the piecewise-linear mesh, not its underlying smooth
|
|
19
|
+
* projection or a camera's approximation error. Floating-point margins reject
|
|
20
|
+
* near-degeneracy; this is not a formal interval-arithmetic certificate.
|
|
21
|
+
*/
|
|
22
|
+
export function qualifySurfaceTransition(
|
|
23
|
+
mesh: TriangleMesh,
|
|
24
|
+
targetPositions: ArrayLike<number>,
|
|
25
|
+
options: {minimumDoubleArea?: number; maxChecks?: number} = {}
|
|
26
|
+
): SurfaceTransitionQualification {
|
|
27
|
+
const from = Array.from(mesh.positions),
|
|
28
|
+
to = Array.from(targetPositions),
|
|
29
|
+
indices = Array.from(mesh.indices),
|
|
30
|
+
count = from.length / 3,
|
|
31
|
+
maxChecks = options.maxChecks ?? 1000000;
|
|
32
|
+
if (
|
|
33
|
+
!from.length ||
|
|
34
|
+
from.length % 3 ||
|
|
35
|
+
to.length !== from.length ||
|
|
36
|
+
!indices.length ||
|
|
37
|
+
indices.length % 3 ||
|
|
38
|
+
!from.every(Number.isFinite) ||
|
|
39
|
+
!to.every(Number.isFinite) ||
|
|
40
|
+
!indices.every(i => Number.isSafeInteger(i) && i >= 0 && i < count) ||
|
|
41
|
+
!Number.isSafeInteger(maxChecks) ||
|
|
42
|
+
maxChecks < 0
|
|
43
|
+
)
|
|
44
|
+
throw new RangeError('Invalid surface transition mesh or budget');
|
|
45
|
+
let scale = 0;
|
|
46
|
+
for (const positions of [from, to])
|
|
47
|
+
for (let i = 0; i < indices.length; i += 3)
|
|
48
|
+
for (let j = 0; j < 3; j++) {
|
|
49
|
+
const a = indices[i + j] * 3,
|
|
50
|
+
b = indices[i + ((j + 1) % 3)] * 3;
|
|
51
|
+
scale = Math.max(
|
|
52
|
+
scale,
|
|
53
|
+
Math.hypot(positions[b] - positions[a], positions[b + 1] - positions[a + 1])
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
const margin = options.minimumDoubleArea ?? scale * scale * 1e-12;
|
|
57
|
+
if (!Number.isFinite(margin) || margin <= 0)
|
|
58
|
+
throw new RangeError('Expected positive finite area margin');
|
|
59
|
+
let checks = 0;
|
|
60
|
+
const tick = () => {
|
|
61
|
+
if (++checks > maxChecks) throw new RangeError('Surface qualification exceeded maxChecks');
|
|
62
|
+
};
|
|
63
|
+
const edges = new Map<string, {a: number; b: number; faces: number}>(),
|
|
64
|
+
used = new Set(indices);
|
|
65
|
+
let minimum = Infinity;
|
|
66
|
+
const orientation = (a: number, b: number, c: number, t: number) => {
|
|
67
|
+
// Interpolate relative edges, avoiding cancellation from translating a patch
|
|
68
|
+
// across large world-coordinate origins before subtracting vertex positions.
|
|
69
|
+
const edge = (v: number, k: number) =>
|
|
70
|
+
(from[3 * v + k] - from[3 * a + k]) * (1 - t) + (to[3 * v + k] - to[3 * a + k]) * t;
|
|
71
|
+
return edge(b, 0) * edge(c, 1) - edge(b, 1) * edge(c, 0);
|
|
72
|
+
};
|
|
73
|
+
const minimumOrientation = (a: number, b: number, c: number) => {
|
|
74
|
+
tick();
|
|
75
|
+
return quadraticMinimum(t => orientation(a, b, c, t));
|
|
76
|
+
};
|
|
77
|
+
for (let i = 0; i < indices.length; i += 3) {
|
|
78
|
+
const tri = indices.slice(i, i + 3);
|
|
79
|
+
const area = minimumOrientation(tri[0], tri[1], tri[2]);
|
|
80
|
+
if (area < margin) throw new RangeError('Triangle collapses or reverses during transition');
|
|
81
|
+
minimum = Math.min(minimum, area);
|
|
82
|
+
for (let j = 0; j < 3; j++) {
|
|
83
|
+
const a = tri[j],
|
|
84
|
+
b = tri[(j + 1) % 3],
|
|
85
|
+
key = a < b ? `${a}:${b}` : `${b}:${a}`,
|
|
86
|
+
edge = edges.get(key);
|
|
87
|
+
if (edge) {
|
|
88
|
+
if (edge.faces !== 1 || edge.a !== b || edge.b !== a)
|
|
89
|
+
throw new RangeError('Expected oriented manifold edges');
|
|
90
|
+
edge.faces++;
|
|
91
|
+
} else edges.set(key, {a, b, faces: 1});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const next = new Map<number, number>();
|
|
95
|
+
for (const edge of edges.values())
|
|
96
|
+
if (edge.faces === 1) {
|
|
97
|
+
if (next.has(edge.a)) throw new RangeError('Nonmanifold boundary');
|
|
98
|
+
next.set(edge.a, edge.b);
|
|
99
|
+
}
|
|
100
|
+
if (!next.size || used.size !== count || used.size - edges.size + indices.length / 3 !== 1)
|
|
101
|
+
throw new RangeError('Expected a triangulated disk without unused vertices');
|
|
102
|
+
const start = next.keys().next().value!,
|
|
103
|
+
boundary: number[] = [];
|
|
104
|
+
const visitedBoundary = new Set<number>();
|
|
105
|
+
let v = start;
|
|
106
|
+
do {
|
|
107
|
+
if (visitedBoundary.has(v) || !next.has(v)) throw new RangeError('Expected one boundary cycle');
|
|
108
|
+
visitedBoundary.add(v);
|
|
109
|
+
boundary.push(v);
|
|
110
|
+
v = next.get(v)!;
|
|
111
|
+
} while (v !== start);
|
|
112
|
+
if (boundary.length !== next.size) throw new RangeError('Multiple boundary cycles');
|
|
113
|
+
// Every boundary vertex stays left of each directed support edge. This allows
|
|
114
|
+
// collinear refinement vertices while rejecting a nonconvex or crossing outline.
|
|
115
|
+
for (let i = 0; i < boundary.length; i++) {
|
|
116
|
+
const a = boundary[i],
|
|
117
|
+
b = boundary[(i + 1) % boundary.length];
|
|
118
|
+
// Distinct boundary vertices rule out multiple winding of a convex outline
|
|
119
|
+
// (a locally positive double-cover fan would otherwise pass support tests).
|
|
120
|
+
for (let j = i + 1; j < boundary.length; j++) {
|
|
121
|
+
tick();
|
|
122
|
+
const c = boundary[j];
|
|
123
|
+
const separation = quadraticMinimum(t => {
|
|
124
|
+
const dx = (from[3 * c] - from[3 * a]) * (1 - t) + (to[3 * c] - to[3 * a]) * t,
|
|
125
|
+
dy = (from[3 * c + 1] - from[3 * a + 1]) * (1 - t) + (to[3 * c + 1] - to[3 * a + 1]) * t;
|
|
126
|
+
return dx * dx + dy * dy;
|
|
127
|
+
});
|
|
128
|
+
if (separation < margin) throw new RangeError('Boundary vertices meet during transition');
|
|
129
|
+
}
|
|
130
|
+
for (const c of boundary)
|
|
131
|
+
if (c !== a && c !== b && minimumOrientation(a, b, c) < -margin * 1e-3)
|
|
132
|
+
throw new RangeError('Boundary is not convex throughout transition');
|
|
133
|
+
}
|
|
134
|
+
// Euler characteristic plus one boundary is insufficient if a closed component
|
|
135
|
+
// is present. Explicitly require connectivity of the vertex graph.
|
|
136
|
+
const neighbors = Array.from({length: count}, () => [] as number[]);
|
|
137
|
+
for (const {a, b} of edges.values()) {
|
|
138
|
+
neighbors[a].push(b);
|
|
139
|
+
neighbors[b].push(a);
|
|
140
|
+
}
|
|
141
|
+
const seen = new Set<number>([start]),
|
|
142
|
+
queue = [start];
|
|
143
|
+
for (let i = 0; i < queue.length; i++)
|
|
144
|
+
for (const n of neighbors[queue[i]])
|
|
145
|
+
if (!seen.has(n)) {
|
|
146
|
+
seen.add(n);
|
|
147
|
+
queue.push(n);
|
|
148
|
+
}
|
|
149
|
+
if (seen.size !== count) throw new RangeError('Disconnected surface mesh');
|
|
150
|
+
return {
|
|
151
|
+
scope: 'convex-xy-disk-linear',
|
|
152
|
+
minimumProjectedDoubleArea: minimum,
|
|
153
|
+
boundary: new Uint32Array(boundary)
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function quadraticMinimum(value: (t: number) => number): number {
|
|
158
|
+
const f = value(0),
|
|
159
|
+
g = value(1),
|
|
160
|
+
h = value(0.5),
|
|
161
|
+
a = 2 * (f + g - 2 * h),
|
|
162
|
+
b = g - f - a;
|
|
163
|
+
if (![f, g, h, a, b].every(Number.isFinite))
|
|
164
|
+
throw new RangeError('Nonfinite surface orientation polynomial');
|
|
165
|
+
const t = a > 0 ? -b / (2 * a) : -1;
|
|
166
|
+
const result = Math.min(f, g, t > 0 && t < 1 ? value(t) : Infinity);
|
|
167
|
+
if (!Number.isFinite(result)) throw new RangeError('Nonfinite surface orientation');
|
|
168
|
+
return result;
|
|
169
|
+
}
|