@math.gl/geometry 5.0.0-beta.1 → 5.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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,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
+ }