@math.gl/geospatial 5.0.0-beta.7 → 5.0.0-beta.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/dist/expressions.d.ts +1 -1
  2. package/dist/expressions.d.ts.map +1 -1
  3. package/dist/index.cjs +1354 -383
  4. package/dist/index.cjs.map +3 -3
  5. package/dist/index.d.ts +10 -0
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +9 -0
  8. package/dist/index.js.map +1 -1
  9. package/dist/tasks/index.cjs +1135 -142
  10. package/dist/tasks/index.cjs.map +3 -3
  11. package/dist/tasks/index.d.ts +12 -0
  12. package/dist/tasks/index.d.ts.map +1 -1
  13. package/dist/tasks/index.js +26 -0
  14. package/dist/tasks/index.js.map +1 -1
  15. package/dist/tasks/terrain-coverage-worker.d.ts +2 -0
  16. package/dist/tasks/terrain-coverage-worker.d.ts.map +1 -0
  17. package/dist/tasks/terrain-coverage-worker.js +7 -0
  18. package/dist/tasks/terrain-coverage-worker.js.map +1 -0
  19. package/dist/tasks/terrain-coverage.d.ts +25 -0
  20. package/dist/tasks/terrain-coverage.d.ts.map +1 -0
  21. package/dist/tasks/terrain-coverage.js +15 -0
  22. package/dist/tasks/terrain-coverage.js.map +1 -0
  23. package/dist/tasks/terrain-residency-worker.d.ts +2 -0
  24. package/dist/tasks/terrain-residency-worker.d.ts.map +1 -0
  25. package/dist/tasks/terrain-residency-worker.js +7 -0
  26. package/dist/tasks/terrain-residency-worker.js.map +1 -0
  27. package/dist/tasks/terrain-residency.d.ts +64 -0
  28. package/dist/tasks/terrain-residency.d.ts.map +1 -0
  29. package/dist/tasks/terrain-residency.js +124 -0
  30. package/dist/tasks/terrain-residency.js.map +1 -0
  31. package/dist/terrain/breakline-mesh.d.ts +18 -0
  32. package/dist/terrain/breakline-mesh.d.ts.map +1 -0
  33. package/dist/terrain/breakline-mesh.js +126 -0
  34. package/dist/terrain/breakline-mesh.js.map +1 -0
  35. package/dist/terrain/breakline-preprocess.d.ts +11 -0
  36. package/dist/terrain/breakline-preprocess.d.ts.map +1 -0
  37. package/dist/terrain/breakline-preprocess.js +94 -0
  38. package/dist/terrain/breakline-preprocess.js.map +1 -0
  39. package/dist/terrain/continuous-mesh-error.d.ts +34 -0
  40. package/dist/terrain/continuous-mesh-error.d.ts.map +1 -0
  41. package/dist/terrain/continuous-mesh-error.js +221 -0
  42. package/dist/terrain/continuous-mesh-error.js.map +1 -0
  43. package/dist/terrain/geographic-seam.d.ts +21 -0
  44. package/dist/terrain/geographic-seam.d.ts.map +1 -0
  45. package/dist/terrain/geographic-seam.js +52 -0
  46. package/dist/terrain/geographic-seam.js.map +1 -0
  47. package/dist/terrain/index.cjs +1104 -133
  48. package/dist/terrain/index.cjs.map +3 -3
  49. package/dist/terrain/index.d.ts +10 -0
  50. package/dist/terrain/index.d.ts.map +1 -1
  51. package/dist/terrain/index.js +9 -0
  52. package/dist/terrain/index.js.map +1 -1
  53. package/dist/terrain/lod-select.d.ts +32 -0
  54. package/dist/terrain/lod-select.d.ts.map +1 -0
  55. package/dist/terrain/lod-select.js +60 -0
  56. package/dist/terrain/lod-select.js.map +1 -0
  57. package/dist/terrain/lod-stitch.d.ts +13 -0
  58. package/dist/terrain/lod-stitch.d.ts.map +1 -0
  59. package/dist/terrain/lod-stitch.js +49 -0
  60. package/dist/terrain/lod-stitch.js.map +1 -0
  61. package/dist/terrain/mesh-coverage.d.ts +35 -0
  62. package/dist/terrain/mesh-coverage.d.ts.map +1 -0
  63. package/dist/terrain/mesh-coverage.js +186 -0
  64. package/dist/terrain/mesh-coverage.js.map +1 -0
  65. package/dist/terrain/mesh-regions-data.d.ts +2 -2
  66. package/dist/terrain/mesh-regions-data.d.ts.map +1 -1
  67. package/dist/terrain/mesh-regions-data.js +20 -10
  68. package/dist/terrain/mesh-regions-data.js.map +1 -1
  69. package/dist/terrain/mixed-halo-update.d.ts.map +1 -1
  70. package/dist/terrain/mixed-halo-update.js +93 -16
  71. package/dist/terrain/mixed-halo-update.js.map +1 -1
  72. package/dist/terrain/mixed-vertex-halos.d.ts +11 -1
  73. package/dist/terrain/mixed-vertex-halos.d.ts.map +1 -1
  74. package/dist/terrain/mixed-vertex-halos.js +43 -5
  75. package/dist/terrain/mixed-vertex-halos.js.map +1 -1
  76. package/dist/terrain/valid-domain-mesh.d.ts +29 -0
  77. package/dist/terrain/valid-domain-mesh.d.ts.map +1 -0
  78. package/dist/terrain/valid-domain-mesh.js +183 -0
  79. package/dist/terrain/valid-domain-mesh.js.map +1 -0
  80. package/dist/terrain/valid-domain-regions.d.ts +16 -0
  81. package/dist/terrain/valid-domain-regions.d.ts.map +1 -0
  82. package/dist/terrain/valid-domain-regions.js +19 -0
  83. package/dist/terrain/valid-domain-regions.js.map +1 -0
  84. package/dist/turf/expressions.d.ts +1 -1
  85. package/package.json +9 -7
  86. package/src/expressions.ts +1 -1
  87. package/src/index.ts +43 -0
  88. package/src/tasks/index.ts +34 -0
  89. package/src/tasks/terrain-coverage-worker.ts +6 -0
  90. package/src/tasks/terrain-coverage.ts +25 -0
  91. package/src/tasks/terrain-residency-worker.ts +6 -0
  92. package/src/tasks/terrain-residency.ts +154 -0
  93. package/src/terrain/breakline-mesh.ts +164 -0
  94. package/src/terrain/breakline-preprocess.ts +106 -0
  95. package/src/terrain/cdt2d.d.ts +10 -0
  96. package/src/terrain/continuous-mesh-error.ts +321 -0
  97. package/src/terrain/geographic-seam.ts +73 -0
  98. package/src/terrain/index.ts +39 -0
  99. package/src/terrain/lod-select.ts +95 -0
  100. package/src/terrain/lod-stitch.ts +70 -0
  101. package/src/terrain/mesh-coverage.ts +215 -0
  102. package/src/terrain/mesh-regions-data.ts +21 -13
  103. package/src/terrain/mixed-halo-update.ts +105 -17
  104. package/src/terrain/mixed-vertex-halos.ts +58 -4
  105. package/src/terrain/valid-domain-mesh.ts +221 -0
  106. package/src/terrain/valid-domain-regions.ts +41 -0
  107. package/src/turf/expressions.ts +1 -1
@@ -0,0 +1,321 @@
1
+ // math.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ import type {HeightField} from './height-field';
6
+
7
+ type Interval = readonly [number, number];
8
+ type XY = readonly [number, number];
9
+ type Plane = readonly [Interval, Interval, Interval];
10
+ type Triangle = readonly [XY, XY, XY];
11
+
12
+ export type TerrainContinuousMesh = {
13
+ /** Packed XY in fractional sample indices. This is the authoritative horizontal geometry. */
14
+ gridCoordinates: ArrayLike<number>;
15
+ /** Packed XYZ. Only Z is read. */
16
+ positions: ArrayLike<number>;
17
+ indices: ArrayLike<number>;
18
+ origin: readonly [number, number, number];
19
+ heightReference: string;
20
+ };
21
+
22
+ export type TerrainContinuousError = {
23
+ /** Upper bound on absolute vertical error over the common domain, or null if no box survived. */
24
+ upperBound: number | null;
25
+ lowerBound: number;
26
+ witness: XY | null;
27
+ boxes: number;
28
+ /** True when the upper and lower bounds differ by at most the requested gap. */
29
+ converged: boolean;
30
+ };
31
+
32
+ const exact = (x: number): Interval => [x, x];
33
+ function outward(lo: number, hi: number): Interval {
34
+ if (!Number.isFinite(lo) || !Number.isFinite(hi))
35
+ throw new Error('Continuous error arithmetic overflow');
36
+ // One full relative epsilon bounds rounding of a binary64 operation, including at binade
37
+ // boundaries. The smallest subnormal covers underflow and zero.
38
+ const result: Interval = [
39
+ lo - Math.max(Number.MIN_VALUE, Math.abs(lo) * Number.EPSILON),
40
+ hi + Math.max(Number.MIN_VALUE, Math.abs(hi) * Number.EPSILON)
41
+ ];
42
+ if (!result.every(Number.isFinite)) throw new Error('Continuous error arithmetic overflow');
43
+ return result;
44
+ }
45
+ const add = (a: Interval, b: Interval): Interval => outward(a[0] + b[0], a[1] + b[1]);
46
+ const sub = (a: Interval, b: Interval): Interval => outward(a[0] - b[1], a[1] - b[0]);
47
+ function mul(a: Interval, b: Interval): Interval {
48
+ const values = [a[0] * b[0], a[0] * b[1], a[1] * b[0], a[1] * b[1]];
49
+ return outward(Math.min(...values), Math.max(...values));
50
+ }
51
+ function div(a: Interval, b: Interval): Interval {
52
+ if (b[0] <= 0 && b[1] >= 0) throw new Error('Degenerate or numerically unresolved mesh triangle');
53
+ return mul(a, outward(1 / b[1], 1 / b[0]));
54
+ }
55
+ const magnitude = (a: Interval) => Math.max(-a[0], a[1]);
56
+ const minimumMagnitude = (a: Interval) => Math.max(0, a[0], -a[1]);
57
+ const evaluate = (p: Plane, x: Interval, y: Interval) => add(p[0], add(mul(p[1], x), mul(p[2], y)));
58
+
59
+ function edge(a: XY, b: XY, column: number, row: number): Plane {
60
+ const dx = sub(exact(b[0]), exact(a[0]));
61
+ const dy = sub(exact(b[1]), exact(a[1]));
62
+ return [
63
+ sub(mul(dx, sub(exact(row), exact(a[1]))), mul(dy, sub(exact(column), exact(a[0])))),
64
+ [-dy[1], -dy[0]],
65
+ dx
66
+ ];
67
+ }
68
+
69
+ function polygon(triangle: Triangle, column: number, row: number): Plane[] {
70
+ const [a, b, c] = triangle;
71
+ const first = edge(a, b, a[0], a[1]);
72
+ const orientation = evaluate(first, sub(exact(c[0]), exact(a[0])), sub(exact(c[1]), exact(a[1])));
73
+ if (orientation[0] <= 0 && orientation[1] >= 0)
74
+ throw new Error('Degenerate or numerically unresolved mesh triangle');
75
+ const sides = [edge(a, b, column, row), edge(b, c, column, row), edge(c, a, column, row)];
76
+ return orientation[0] > 0
77
+ ? sides
78
+ : sides.map(p => p.map(([lo, hi]) => [-hi, -lo]) as unknown as Plane);
79
+ }
80
+
81
+ /**
82
+ * Bound vertical error everywhere the supplied mesh and raster reference overlap.
83
+ * Overlapping mesh faces are checked independently. Uncovered reference area is outside
84
+ * this measurement. Bilinear cells require four valid heights; triangle mode follows
85
+ * the field diagonal and admits each valid half cell independently.
86
+ */
87
+ export function boundTerrainMeshError(
88
+ field: HeightField,
89
+ mesh: TerrainContinuousMesh,
90
+ options: {
91
+ reference?: 'triangle' | 'bilinear';
92
+ gap?: number;
93
+ maxBoxes?: number;
94
+ maxDepth?: number;
95
+ } = {}
96
+ ): TerrainContinuousError {
97
+ const {reference = 'triangle', gap = 0.05, maxBoxes = 2_000_000, maxDepth = 12} = options;
98
+ const {gridCoordinates: xy, positions, indices, origin} = mesh;
99
+ if (
100
+ !['triangle', 'bilinear'].includes(reference) ||
101
+ !Number.isFinite(gap) ||
102
+ gap < 0 ||
103
+ !Number.isSafeInteger(maxBoxes) ||
104
+ maxBoxes < 1 ||
105
+ !Number.isSafeInteger(maxDepth) ||
106
+ maxDepth < 0 ||
107
+ maxDepth > 24
108
+ )
109
+ throw new Error('Invalid continuous error options');
110
+ if (
111
+ mesh.heightReference !== field.heightReference ||
112
+ !Number.isFinite(origin[2]) ||
113
+ positions.length % 3 ||
114
+ xy.length !== (positions.length / 3) * 2 ||
115
+ indices.length % 3
116
+ )
117
+ throw new Error('Invalid continuous error mesh buffers or height reference');
118
+
119
+ let upperBound: number | null = null;
120
+ let lowerBound = 0;
121
+ let witness: XY | null = null;
122
+ let boxes = 0;
123
+
124
+ for (let i = 0; i < indices.length; i += 3) {
125
+ const ids = [indices[i], indices[i + 1], indices[i + 2]];
126
+ if (ids.some(id => !Number.isSafeInteger(id) || id < 0 || id >= xy.length / 2)) {
127
+ throw new Error('Invalid continuous error index');
128
+ }
129
+ const triangle: Triangle = [
130
+ [xy[2 * ids[0]], xy[2 * ids[0] + 1]],
131
+ [xy[2 * ids[1]], xy[2 * ids[1] + 1]],
132
+ [xy[2 * ids[2]], xy[2 * ids[2] + 1]]
133
+ ];
134
+ const [a, b, c] = triangle;
135
+ const [za, zb, zc] = ids.map(id => exact(positions[3 * id + 2]));
136
+ if (
137
+ triangle.some(([x, y]) => !Number.isFinite(x) || !Number.isFinite(y)) ||
138
+ [za, zb, zc].some(z => !Number.isFinite(z[0]))
139
+ )
140
+ throw new Error('Nonfinite continuous error vertex');
141
+ // A face identical to a reference half cell has zero difference on its
142
+ // entire footprint, including shared boundaries with neighboring cells.
143
+ if (
144
+ reference === 'triangle' &&
145
+ origin[2] === 0 &&
146
+ matchesReferenceHalf(field, triangle, [za[0], zb[0], zc[0]])
147
+ ) {
148
+ if (++boxes > maxBoxes) throw new Error('Continuous error box limit exceeded');
149
+ upperBound ??= 0;
150
+ witness ??= a;
151
+ continue;
152
+ }
153
+ const dx = sub(exact(b[0]), exact(a[0])),
154
+ dy = sub(exact(b[1]), exact(a[1]));
155
+ const ex = sub(exact(c[0]), exact(a[0])),
156
+ ey = sub(exact(c[1]), exact(a[1]));
157
+ const det = sub(mul(dx, ey), mul(dy, ex));
158
+ const dz = sub(zb, za),
159
+ ez = sub(zc, za);
160
+ const sx = div(sub(mul(dz, ey), mul(ez, dy)), det);
161
+ const sy = div(sub(mul(dx, ez), mul(ex, dz)), det);
162
+
163
+ for (
164
+ let row = Math.max(0, Math.ceil(Math.min(a[1], b[1], c[1])) - 1);
165
+ row <= Math.min(field.height - 2, Math.floor(Math.max(a[1], b[1], c[1])));
166
+ row++
167
+ ) {
168
+ for (
169
+ let column = Math.max(0, Math.ceil(Math.min(a[0], b[0], c[0])) - 1);
170
+ column <= Math.min(field.width - 2, Math.floor(Math.max(a[0], b[0], c[0])));
171
+ column++
172
+ ) {
173
+ if (++boxes > maxBoxes) throw new Error('Continuous error box limit exceeded');
174
+ const vertices: XY[] = [
175
+ [0, 0],
176
+ [1, 0],
177
+ [0, 1],
178
+ [1, 1]
179
+ ];
180
+ const h = vertices.map(([x, y]) => field.getHeight(column + x, row + y));
181
+ const sides = polygon(triangle, column, row);
182
+ const base = add(
183
+ za,
184
+ add(mul(sx, sub(exact(column), exact(a[0]))), mul(sy, sub(exact(row), exact(a[1]))))
185
+ );
186
+
187
+ function visit(
188
+ constraints: Plane[],
189
+ p: Plane,
190
+ mixed: Interval,
191
+ x: Interval,
192
+ y: Interval,
193
+ depth: number
194
+ ): void {
195
+ if (++boxes > maxBoxes) throw new Error('Continuous error box limit exceeded');
196
+ if (constraints.some(side => evaluate(side, x, y)[1] < 0)) return;
197
+ const error = (u: Interval, v: Interval) => add(evaluate(p, u, v), mul(mixed, mul(u, v)));
198
+ const centerX = (x[0] + x[1]) / 2;
199
+ const centerY = (y[0] + y[1]) / 2;
200
+ const u = exact(centerX),
201
+ v = exact(centerY);
202
+ if (constraints.every(side => evaluate(side, u, v)[0] >= 0)) {
203
+ const lower = minimumMagnitude(error(u, v));
204
+ if (!witness || lower > lowerBound) {
205
+ lowerBound = lower;
206
+ witness = [column + centerX, row + centerY];
207
+ }
208
+ }
209
+ // A bilinear polynomial reaches its box extrema at corners.
210
+ const upper = Math.max(
211
+ ...x.flatMap(u => y.map(v => magnitude(error(exact(u), exact(v)))))
212
+ );
213
+ if (upper <= lowerBound + gap || depth === maxDepth) {
214
+ upperBound = Math.max(upperBound ?? 0, upper);
215
+ return;
216
+ }
217
+ if (x[1] - x[0] >= y[1] - y[0]) {
218
+ visit(constraints, p, mixed, [x[0], centerX], y, depth + 1);
219
+ visit(constraints, p, mixed, [centerX, x[1]], y, depth + 1);
220
+ } else {
221
+ visit(constraints, p, mixed, x, [y[0], centerY], depth + 1);
222
+ visit(constraints, p, mixed, x, [centerY, y[1]], depth + 1);
223
+ }
224
+ }
225
+
226
+ const compare = (
227
+ constant: Interval,
228
+ x: Interval,
229
+ y: Interval,
230
+ mixed: Interval,
231
+ constraints: Plane[]
232
+ ) =>
233
+ visit(
234
+ constraints,
235
+ [sub(base, sub(constant, exact(origin[2]))), sub(sx, x), sub(sy, y)],
236
+ [-mixed[1], -mixed[0]],
237
+ [0, 1],
238
+ [0, 1],
239
+ 0
240
+ );
241
+
242
+ if (reference === 'bilinear') {
243
+ const [h00, h10, h01, h11] = h;
244
+ if (h00 === undefined || h10 === undefined || h01 === undefined || h11 === undefined)
245
+ continue;
246
+ const x = sub(exact(h10), exact(h00)),
247
+ y = sub(exact(h01), exact(h00));
248
+ compare(exact(h00), x, y, sub(sub(exact(h11), exact(h01)), x), sides);
249
+ } else {
250
+ const halves =
251
+ field.diagonal === '00-11'
252
+ ? [
253
+ [0, 1, 3],
254
+ [0, 3, 2]
255
+ ]
256
+ : [
257
+ [0, 1, 2],
258
+ [1, 3, 2]
259
+ ];
260
+ for (const [j, k, l] of halves) {
261
+ const z0 = h[j],
262
+ z1 = h[k],
263
+ z2 = h[l];
264
+ if (z0 === undefined || z1 === undefined || z2 === undefined) continue;
265
+ const p = vertices[j],
266
+ q = vertices[k],
267
+ r = vertices[l];
268
+ const x = sub(
269
+ mul(sub(exact(z1), exact(z0)), exact(r[1] - p[1])),
270
+ mul(sub(exact(z2), exact(z0)), exact(q[1] - p[1]))
271
+ );
272
+ const y = sub(
273
+ mul(exact(q[0] - p[0]), sub(exact(z2), exact(z0))),
274
+ mul(exact(r[0] - p[0]), sub(exact(z1), exact(z0)))
275
+ );
276
+ compare(sub(exact(z0), add(mul(x, exact(p[0])), mul(y, exact(p[1])))), x, y, exact(0), [
277
+ ...sides,
278
+ ...polygon([p, q, r], 0, 0)
279
+ ]);
280
+ }
281
+ }
282
+ }
283
+ }
284
+ }
285
+ return {
286
+ upperBound,
287
+ lowerBound,
288
+ witness,
289
+ boxes,
290
+ converged: upperBound !== null && upperBound - lowerBound <= gap
291
+ };
292
+ }
293
+
294
+ function matchesReferenceHalf(field: HeightField, triangle: Triangle, heights: number[]): boolean {
295
+ const column = Math.min(...triangle.map(p => p[0]));
296
+ const row = Math.min(...triangle.map(p => p[1]));
297
+ if (
298
+ !Number.isInteger(column) ||
299
+ !Number.isInteger(row) ||
300
+ column < 0 ||
301
+ row < 0 ||
302
+ column >= field.width - 1 ||
303
+ row >= field.height - 1
304
+ )
305
+ return false;
306
+ const corners = new Set<number>();
307
+ for (let i = 0; i < 3; i++) {
308
+ const [x, y] = triangle[i];
309
+ const u = x - column,
310
+ v = y - row;
311
+ if ((u !== 0 && u !== 1) || (v !== 0 && v !== 1) || field.getHeight(x, y) !== heights[i])
312
+ return false;
313
+ corners.add(u + 2 * v);
314
+ }
315
+ return (
316
+ corners.size === 3 &&
317
+ (field.diagonal === '00-11'
318
+ ? corners.has(0) && corners.has(3)
319
+ : corners.has(1) && corners.has(2))
320
+ );
321
+ }
@@ -0,0 +1,73 @@
1
+ // math.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ import type {GeographicHeightField} from './geographic';
6
+ import {TerrainSampleStatus, type TerrainSampleOptions} from './types';
7
+
8
+ export type TerrainGeographicProbe = readonly [longitude: number, latitude: number];
9
+
10
+ /** Map longitude to the first equivalent value in the unwrapped interval, if one exists.
11
+ * The western endpoint owns the alias when the interval covers a full turn.
12
+ */
13
+ export function wrapTerrainLongitude(
14
+ longitude: number,
15
+ west: number,
16
+ east: number
17
+ ): number | undefined {
18
+ if (![longitude, west, east].every(Number.isFinite) || east <= west || east - west > 360)
19
+ throw new Error('Invalid terrain longitude interval or query');
20
+ if (longitude >= west && longitude < east) return longitude;
21
+ // Reduce separately before subtracting to avoid overflow and preserve modulo information.
22
+ const delta = ((longitude % 360) - (west % 360)) % 360;
23
+ const offset = delta < 0 ? delta + 360 : delta;
24
+ const mapped = west + offset;
25
+ return mapped <= east ? mapped : undefined;
26
+ }
27
+
28
+ /** Compare independent one-sided stencils at the same geographic position.
29
+ * Exact poles remain outside the terrain derivative domain. No reflection or extrapolation.
30
+ */
31
+ export function measureGeographicTerrainSeam(
32
+ a: GeographicHeightField | null,
33
+ b: GeographicHeightField | null,
34
+ probes: readonly TerrainGeographicProbe[],
35
+ options: TerrainSampleOptions = {}
36
+ ) {
37
+ if (a && b && a.heightReference !== b.heightReference)
38
+ throw new Error('Terrain seam height references differ');
39
+ const sample = (field: GeographicHeightField | null, longitude: number, latitude: number) => {
40
+ if (!field || !Number.isFinite(longitude)) return field?.sample(longitude, latitude, options);
41
+ const mapped = wrapTerrainLongitude(longitude, field.bounds[0], field.bounds[2]);
42
+ return field.sample(mapped ?? longitude, latitude, options);
43
+ };
44
+ return probes.map(([longitude, latitude]) => {
45
+ const left = sample(a, longitude, latitude);
46
+ const right = sample(b, longitude, latitude);
47
+ const valid =
48
+ left?.status === TerrainSampleStatus.VALID && right?.status === TerrainSampleStatus.VALID;
49
+ const derivatives =
50
+ left?.gradientStatus === TerrainSampleStatus.VALID &&
51
+ right?.gradientStatus === TerrainSampleStatus.VALID;
52
+ const normalAngle = derivatives
53
+ ? Math.atan2(
54
+ Math.hypot(
55
+ left.normalY * right.normalZ - left.normalZ * right.normalY,
56
+ left.normalZ * right.normalX - left.normalX * right.normalZ,
57
+ left.normalX * right.normalY - left.normalY * right.normalX
58
+ ),
59
+ left.normalX * right.normalX + left.normalY * right.normalY + left.normalZ * right.normalZ
60
+ )
61
+ : null;
62
+ return {
63
+ longitude,
64
+ latitude,
65
+ statusA: left?.status ?? TerrainSampleStatus.NOT_RESIDENT,
66
+ statusB: right?.status ?? TerrainSampleStatus.NOT_RESIDENT,
67
+ gradientStatusA: left?.gradientStatus ?? TerrainSampleStatus.NOT_RESIDENT,
68
+ gradientStatusB: right?.gradientStatus ?? TerrainSampleStatus.NOT_RESIDENT,
69
+ heightJump: valid ? Math.abs(left.height - right.height) : null,
70
+ normalAngle
71
+ };
72
+ });
73
+ }
@@ -166,3 +166,42 @@ export {coordinateTerrainLodMorphs} from './lod-coordinate';
166
166
  export {createTerrainMixedIsobands} from './isobands-mixed';
167
167
  export type {TerrainLodTile} from './lod-coordinate';
168
168
  export type {TerrainMixedSurface, TerrainMixedSurfaceOptions} from './mixed-surface';
169
+
170
+ export {
171
+ createValidDomainTerrainMesh,
172
+ type ValidDomainTerrainMesh,
173
+ type ValidDomainMeshOptions
174
+ } from './valid-domain-mesh';
175
+
176
+ export {
177
+ createBreaklineTerrainMesh,
178
+ type TerrainBreaklinePoint,
179
+ type TerrainBreakline,
180
+ type BreaklineTerrainMesh,
181
+ type BreaklineMeshOptions
182
+ } from './breakline-mesh';
183
+
184
+ export {
185
+ boundTerrainMeshError,
186
+ type TerrainContinuousMesh,
187
+ type TerrainContinuousError
188
+ } from './continuous-mesh-error';
189
+
190
+ export {
191
+ wrapTerrainLongitude,
192
+ measureGeographicTerrainSeam,
193
+ type TerrainGeographicProbe
194
+ } from './geographic-seam';
195
+ export {selectTerrainLod, type TerrainLodCandidate, type TerrainLodView} from './lod-select';
196
+ export {stitchTerrainLodCut, type TerrainLodSeam} from './lod-stitch';
197
+
198
+ export {splitTerrainBreaklines} from './breakline-preprocess';
199
+
200
+ export {
201
+ updateTerrainValidDomainMeshRegions,
202
+ type TerrainValidDomainMeshRegions,
203
+ type TerrainValidDomainRegionOptions
204
+ } from './valid-domain-regions';
205
+
206
+ export {certifyTerrainMeshCoverage} from './mesh-coverage';
207
+ export type {TerrainCoverageMesh, TerrainMeshCoverage} from './mesh-coverage';
@@ -0,0 +1,95 @@
1
+ // math.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ type XYZ = readonly [number, number, number];
6
+
7
+ export type TerrainLodCandidate = {
8
+ id: string;
9
+ min: XYZ;
10
+ max: XYZ;
11
+ /** World-space geometric error for this tile. */
12
+ geometricError: number;
13
+ resident: boolean;
14
+ /** Children must completely partition the parent terrain footprint. */
15
+ children?: readonly TerrainLodCandidate[];
16
+ };
17
+
18
+ export type TerrainLodView = {
19
+ position: XYZ;
20
+ verticalFov: number;
21
+ viewportHeight: number;
22
+ targetPixels: number;
23
+ /** Fractional threshold reduction for previously refined tiles. */
24
+ hysteresis?: number;
25
+ previouslyRefined?: ReadonlySet<string>;
26
+ maxVisited?: number;
27
+ };
28
+
29
+ /** Select a resident cut using perspective screen-space error and complete child fallback. */
30
+ export function selectTerrainLod(roots: readonly TerrainLodCandidate[], view: TerrainLodView) {
31
+ const {
32
+ position,
33
+ verticalFov,
34
+ viewportHeight,
35
+ targetPixels,
36
+ hysteresis = 0.2,
37
+ previouslyRefined,
38
+ maxVisited = 100_000
39
+ } = view;
40
+ if (
41
+ !position.every(Number.isFinite) ||
42
+ !Number.isFinite(verticalFov) ||
43
+ verticalFov <= 0 ||
44
+ verticalFov >= Math.PI ||
45
+ !Number.isFinite(viewportHeight) ||
46
+ viewportHeight <= 0 ||
47
+ !Number.isFinite(targetPixels) ||
48
+ targetPixels <= 0 ||
49
+ !(hysteresis >= 0 && hysteresis < 1) ||
50
+ !Number.isSafeInteger(maxVisited) ||
51
+ maxVisited < 1
52
+ )
53
+ throw new Error('Invalid terrain LOD view');
54
+ const scale = viewportHeight / (2 * Math.tan(verticalFov / 2));
55
+ if (!Number.isFinite(scale)) throw new Error('Terrain LOD projection scale overflow');
56
+ const selected: {tile: TerrainLodCandidate; pixels: number}[] = [];
57
+ const refined = new Set<string>();
58
+ const unavailable: string[] = [];
59
+ const seen = new Set<string>();
60
+ const pending = [...roots].reverse();
61
+ while (pending.length) {
62
+ const tile = pending.pop()!;
63
+ if (seen.size >= maxVisited) throw new Error('Terrain LOD visit limit exceeded');
64
+ if (seen.has(tile.id)) throw new Error('Duplicate terrain LOD tile or cycle');
65
+ seen.add(tile.id);
66
+ if (
67
+ ![...tile.min, ...tile.max, tile.geometricError].every(Number.isFinite) ||
68
+ tile.geometricError < 0 ||
69
+ tile.min.some((n, i) => n > tile.max[i])
70
+ )
71
+ throw new Error('Invalid terrain LOD tile bounds or error');
72
+ if (!tile.resident) {
73
+ unavailable.push(tile.id);
74
+ continue;
75
+ }
76
+ const distance = Math.hypot(
77
+ ...position.map((n, i) => Math.max(tile.min[i] - n, n - tile.max[i], 0))
78
+ );
79
+ const pixels = tile.geometricError === 0 ? 0 : (tile.geometricError / distance) * scale;
80
+ const threshold = targetPixels * (previouslyRefined?.has(tile.id) ? 1 - hysteresis : 1);
81
+ if (
82
+ pixels > threshold &&
83
+ tile.children?.length &&
84
+ tile.children.every(child => child.resident)
85
+ ) {
86
+ if (pending.length + seen.size + tile.children.length > maxVisited)
87
+ throw new Error('Terrain LOD visit limit exceeded');
88
+ refined.add(tile.id);
89
+ for (let i = tile.children.length - 1; i >= 0; i--) pending.push(tile.children[i]);
90
+ } else {
91
+ selected.push({tile, pixels});
92
+ }
93
+ }
94
+ return {selected, refined, unavailable};
95
+ }
@@ -0,0 +1,70 @@
1
+ // math.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ import type {TerrainMeshEdge} from './mesh-data';
6
+ import {
7
+ stitchTerrainMeshEdges,
8
+ type TerrainSeamMesh,
9
+ type TerrainMeshStitchOptions
10
+ } from './mesh-stitch';
11
+
12
+ export type TerrainLodSeam = {
13
+ /** The first tile owns the seam heights. Process coarse owners first. */
14
+ a: number;
15
+ edgeA: TerrainMeshEdge;
16
+ b: number;
17
+ edgeB: TerrainMeshEdge;
18
+ options?: TerrainMeshStitchOptions;
19
+ };
20
+
21
+ /** Stitch a selected cut by splitting only incident boundary faces. */
22
+ export function stitchTerrainLodCut(
23
+ input: readonly TerrainSeamMesh[],
24
+ seams: readonly TerrainLodSeam[]
25
+ ): TerrainSeamMesh[] {
26
+ const meshes = input.slice();
27
+ const paired: {a: number; b: number; idsA: Uint32Array; idsB: Uint32Array}[] = [];
28
+ for (const {a, edgeA, b, edgeB, options} of seams) {
29
+ if (a === b || !Number.isInteger(a) || !Number.isInteger(b) || !meshes[a] || !meshes[b])
30
+ throw new Error('Invalid terrain LOD seam tile index');
31
+ const result = stitchTerrainMeshEdges(meshes[a], edgeA, meshes[b], edgeB, options);
32
+ meshes[a] = result.a;
33
+ meshes[b] = result.b;
34
+ paired.push({a, b, idsA: result.seamA, idsB: result.seamB});
35
+ }
36
+ // Later operations may move shared corners or subdivide an earlier seam.
37
+ // Recheck complete segment chains against the final meshes.
38
+ for (const {a, b, idsA, idsB} of paired) {
39
+ const ma = meshes[a],
40
+ mb = meshes[b];
41
+ const path = (mesh: TerrainSeamMesh, ids: Uint32Array) => {
42
+ const links = new Set<string>();
43
+ for (let i = 1; i < ids.length; i++) {
44
+ links.add(`${Math.min(ids[i - 1], ids[i])},${Math.max(ids[i - 1], ids[i])}`);
45
+ }
46
+ for (let i = 0; i < mesh.indices.length; i += 3) {
47
+ for (let j = 0; j < 3; j++) {
48
+ const p = mesh.indices[i + j],
49
+ q = mesh.indices[i + ((j + 1) % 3)];
50
+ links.delete(`${Math.min(p, q)},${Math.max(p, q)}`);
51
+ }
52
+ }
53
+ return links.size === 0;
54
+ };
55
+ if (!path(ma, idsA) || !path(mb, idsB))
56
+ throw new Error('Overlapping terrain LOD seam ranges require a common subdivision');
57
+ for (let i = 0; i < idsA.length; i++) {
58
+ for (let axis = 0; axis < 3; axis++) {
59
+ const offset = mb.origin[axis] - ma.origin[axis];
60
+ const av = ma.positions[3 * idsA[i] + axis],
61
+ bv = mb.positions[3 * idsB[i] + axis];
62
+ const residual = av - bv - offset;
63
+ const roundoff = 8 * Number.EPSILON * (Math.abs(av) + Math.abs(bv) + Math.abs(offset));
64
+ if (!Number.isFinite(residual) || Math.abs(residual) > roundoff)
65
+ throw new Error('Incompatible terrain LOD seam heights or shared corners');
66
+ }
67
+ }
68
+ }
69
+ return meshes;
70
+ }