@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.
- package/dist/expressions.d.ts +1 -1
- package/dist/expressions.d.ts.map +1 -1
- package/dist/index.cjs +1354 -383
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -1
- package/dist/tasks/index.cjs +1135 -142
- package/dist/tasks/index.cjs.map +3 -3
- package/dist/tasks/index.d.ts +12 -0
- package/dist/tasks/index.d.ts.map +1 -1
- package/dist/tasks/index.js +26 -0
- package/dist/tasks/index.js.map +1 -1
- package/dist/tasks/terrain-coverage-worker.d.ts +2 -0
- package/dist/tasks/terrain-coverage-worker.d.ts.map +1 -0
- package/dist/tasks/terrain-coverage-worker.js +7 -0
- package/dist/tasks/terrain-coverage-worker.js.map +1 -0
- package/dist/tasks/terrain-coverage.d.ts +25 -0
- package/dist/tasks/terrain-coverage.d.ts.map +1 -0
- package/dist/tasks/terrain-coverage.js +15 -0
- package/dist/tasks/terrain-coverage.js.map +1 -0
- package/dist/tasks/terrain-residency-worker.d.ts +2 -0
- package/dist/tasks/terrain-residency-worker.d.ts.map +1 -0
- package/dist/tasks/terrain-residency-worker.js +7 -0
- package/dist/tasks/terrain-residency-worker.js.map +1 -0
- package/dist/tasks/terrain-residency.d.ts +64 -0
- package/dist/tasks/terrain-residency.d.ts.map +1 -0
- package/dist/tasks/terrain-residency.js +124 -0
- package/dist/tasks/terrain-residency.js.map +1 -0
- package/dist/terrain/breakline-mesh.d.ts +18 -0
- package/dist/terrain/breakline-mesh.d.ts.map +1 -0
- package/dist/terrain/breakline-mesh.js +126 -0
- package/dist/terrain/breakline-mesh.js.map +1 -0
- package/dist/terrain/breakline-preprocess.d.ts +11 -0
- package/dist/terrain/breakline-preprocess.d.ts.map +1 -0
- package/dist/terrain/breakline-preprocess.js +94 -0
- package/dist/terrain/breakline-preprocess.js.map +1 -0
- package/dist/terrain/continuous-mesh-error.d.ts +34 -0
- package/dist/terrain/continuous-mesh-error.d.ts.map +1 -0
- package/dist/terrain/continuous-mesh-error.js +221 -0
- package/dist/terrain/continuous-mesh-error.js.map +1 -0
- package/dist/terrain/geographic-seam.d.ts +21 -0
- package/dist/terrain/geographic-seam.d.ts.map +1 -0
- package/dist/terrain/geographic-seam.js +52 -0
- package/dist/terrain/geographic-seam.js.map +1 -0
- package/dist/terrain/index.cjs +1104 -133
- package/dist/terrain/index.cjs.map +3 -3
- package/dist/terrain/index.d.ts +10 -0
- package/dist/terrain/index.d.ts.map +1 -1
- package/dist/terrain/index.js +9 -0
- package/dist/terrain/index.js.map +1 -1
- package/dist/terrain/lod-select.d.ts +32 -0
- package/dist/terrain/lod-select.d.ts.map +1 -0
- package/dist/terrain/lod-select.js +60 -0
- package/dist/terrain/lod-select.js.map +1 -0
- package/dist/terrain/lod-stitch.d.ts +13 -0
- package/dist/terrain/lod-stitch.d.ts.map +1 -0
- package/dist/terrain/lod-stitch.js +49 -0
- package/dist/terrain/lod-stitch.js.map +1 -0
- package/dist/terrain/mesh-coverage.d.ts +35 -0
- package/dist/terrain/mesh-coverage.d.ts.map +1 -0
- package/dist/terrain/mesh-coverage.js +186 -0
- package/dist/terrain/mesh-coverage.js.map +1 -0
- package/dist/terrain/mesh-regions-data.d.ts +2 -2
- package/dist/terrain/mesh-regions-data.d.ts.map +1 -1
- package/dist/terrain/mesh-regions-data.js +20 -10
- package/dist/terrain/mesh-regions-data.js.map +1 -1
- package/dist/terrain/mixed-halo-update.d.ts.map +1 -1
- package/dist/terrain/mixed-halo-update.js +93 -16
- package/dist/terrain/mixed-halo-update.js.map +1 -1
- package/dist/terrain/mixed-vertex-halos.d.ts +11 -1
- package/dist/terrain/mixed-vertex-halos.d.ts.map +1 -1
- package/dist/terrain/mixed-vertex-halos.js +43 -5
- package/dist/terrain/mixed-vertex-halos.js.map +1 -1
- package/dist/terrain/valid-domain-mesh.d.ts +29 -0
- package/dist/terrain/valid-domain-mesh.d.ts.map +1 -0
- package/dist/terrain/valid-domain-mesh.js +183 -0
- package/dist/terrain/valid-domain-mesh.js.map +1 -0
- package/dist/terrain/valid-domain-regions.d.ts +16 -0
- package/dist/terrain/valid-domain-regions.d.ts.map +1 -0
- package/dist/terrain/valid-domain-regions.js +19 -0
- package/dist/terrain/valid-domain-regions.js.map +1 -0
- package/dist/turf/expressions.d.ts +1 -1
- package/package.json +9 -7
- package/src/expressions.ts +1 -1
- package/src/index.ts +43 -0
- package/src/tasks/index.ts +34 -0
- package/src/tasks/terrain-coverage-worker.ts +6 -0
- package/src/tasks/terrain-coverage.ts +25 -0
- package/src/tasks/terrain-residency-worker.ts +6 -0
- package/src/tasks/terrain-residency.ts +154 -0
- package/src/terrain/breakline-mesh.ts +164 -0
- package/src/terrain/breakline-preprocess.ts +106 -0
- package/src/terrain/cdt2d.d.ts +10 -0
- package/src/terrain/continuous-mesh-error.ts +321 -0
- package/src/terrain/geographic-seam.ts +73 -0
- package/src/terrain/index.ts +39 -0
- package/src/terrain/lod-select.ts +95 -0
- package/src/terrain/lod-stitch.ts +70 -0
- package/src/terrain/mesh-coverage.ts +215 -0
- package/src/terrain/mesh-regions-data.ts +21 -13
- package/src/terrain/mixed-halo-update.ts +105 -17
- package/src/terrain/mixed-vertex-halos.ts +58 -4
- package/src/terrain/valid-domain-mesh.ts +221 -0
- package/src/terrain/valid-domain-regions.ts +41 -0
- package/src/turf/expressions.ts +1 -1
|
@@ -0,0 +1,215 @@
|
|
|
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 Point = readonly [number, number];
|
|
8
|
+
type Edge = {a: number; b: number; weight: number};
|
|
9
|
+
|
|
10
|
+
/** Horizontal triangle geometry in the field's fractional sample coordinates. */
|
|
11
|
+
export type TerrainCoverageMesh = {
|
|
12
|
+
gridCoordinates: ArrayLike<number>;
|
|
13
|
+
indices: ArrayLike<number>;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type TerrainMeshCoverage = {
|
|
17
|
+
status: 'certified' | 'mismatch' | 'budget-exhausted';
|
|
18
|
+
/** Nonzero boundary intervals of mesh multiplicity minus reference multiplicity. */
|
|
19
|
+
unmatchedSegments: number;
|
|
20
|
+
witness: {start: Point; end: Point; multiplicityJump: number} | null;
|
|
21
|
+
/** Candidate triangles plus reference half cells examined. */
|
|
22
|
+
trianglesVisited: number;
|
|
23
|
+
/** Edges remaining after cancellation of equal endpoints. */
|
|
24
|
+
residualEdges: number;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
function gcd(a: bigint, b: bigint): bigint {
|
|
28
|
+
if (a < 0n) a = -a;
|
|
29
|
+
if (b < 0n) b = -b;
|
|
30
|
+
while (b !== 0n) [a, b] = [b, a % b];
|
|
31
|
+
return a;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Certify that mesh multiplicity is one on the valid raster domain and zero outside,
|
|
36
|
+
* almost everywhere in XY. Coincident and subdivided edges are allowed.
|
|
37
|
+
* The reference is either independently valid triangles or four-corner-valid cells.
|
|
38
|
+
* An explicit reference mesh must tile the intended domain once.
|
|
39
|
+
* The result says nothing about heights or zero-area boundary features.
|
|
40
|
+
* Exact arithmetic applies to the supplied binary64 coordinates, without snapping.
|
|
41
|
+
*/
|
|
42
|
+
export function certifyTerrainMeshCoverage(
|
|
43
|
+
field: HeightField,
|
|
44
|
+
mesh: TerrainCoverageMesh,
|
|
45
|
+
options: {reference?: 'triangle' | 'bilinear' | TerrainCoverageMesh; maxTriangles?: number} = {}
|
|
46
|
+
): TerrainMeshCoverage {
|
|
47
|
+
const {reference = 'triangle', maxTriangles = 2_000_000} = options;
|
|
48
|
+
const referenceMesh = typeof reference === 'object' ? reference : null;
|
|
49
|
+
const validShape = (input: TerrainCoverageMesh) =>
|
|
50
|
+
input.indices.length % 3 === 0 && input.gridCoordinates.length % 2 === 0;
|
|
51
|
+
if (
|
|
52
|
+
!Number.isSafeInteger(maxTriangles) ||
|
|
53
|
+
maxTriangles < 1 ||
|
|
54
|
+
(!referenceMesh && reference !== 'triangle' && reference !== 'bilinear') ||
|
|
55
|
+
!validShape(mesh) ||
|
|
56
|
+
(referenceMesh && !validShape(referenceMesh))
|
|
57
|
+
)
|
|
58
|
+
throw new Error('Invalid terrain coverage input');
|
|
59
|
+
|
|
60
|
+
const trianglesVisited =
|
|
61
|
+
mesh.indices.length / 3 +
|
|
62
|
+
(referenceMesh ? referenceMesh.indices.length / 3 : 2 * (field.width - 1) * (field.height - 1));
|
|
63
|
+
if (trianglesVisited > maxTriangles) {
|
|
64
|
+
return {
|
|
65
|
+
status: 'budget-exhausted',
|
|
66
|
+
unmatchedSegments: 0,
|
|
67
|
+
witness: null,
|
|
68
|
+
trianglesVisited: 0,
|
|
69
|
+
residualEdges: 0
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Every finite binary64 value is an integer multiple of 2^-1074.
|
|
74
|
+
const bits = new DataView(new ArrayBuffer(8));
|
|
75
|
+
function exact(value: number): bigint {
|
|
76
|
+
bits.setFloat64(0, value);
|
|
77
|
+
const raw = bits.getBigUint64(0);
|
|
78
|
+
const exponent = Number((raw >> 52n) & 2047n);
|
|
79
|
+
const fraction = raw & ((1n << 52n) - 1n);
|
|
80
|
+
const integer = exponent === 0 ? fraction : ((1n << 52n) | fraction) << BigInt(exponent - 1);
|
|
81
|
+
return value < 0 ? -integer : integer;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const vertices: Point[] = [];
|
|
85
|
+
const vertexIds = new Map<string, number>();
|
|
86
|
+
const edges = new Map<number | string, Edge>();
|
|
87
|
+
function vertex(x: number, y: number): number {
|
|
88
|
+
if (!Number.isFinite(x) || !Number.isFinite(y))
|
|
89
|
+
throw new Error('Nonfinite coverage coordinate');
|
|
90
|
+
const key = `${x},${y}`;
|
|
91
|
+
const previous = vertexIds.get(key);
|
|
92
|
+
if (previous !== undefined) return previous;
|
|
93
|
+
const id = vertices.length;
|
|
94
|
+
vertices.push([x, y]);
|
|
95
|
+
vertexIds.set(key, id);
|
|
96
|
+
return id;
|
|
97
|
+
}
|
|
98
|
+
function edge(a: number, b: number, weight: number): void {
|
|
99
|
+
if (a > b) {
|
|
100
|
+
[a, b] = [b, a];
|
|
101
|
+
weight = -weight;
|
|
102
|
+
}
|
|
103
|
+
// Pack two 26-bit vertex IDs into an exactly representable number on ordinary workloads.
|
|
104
|
+
const key = b < 2 ** 26 ? a * 2 ** 26 + b : `${a},${b}`;
|
|
105
|
+
const sum = (edges.get(key)?.weight ?? 0) + weight;
|
|
106
|
+
if (sum === 0) edges.delete(key);
|
|
107
|
+
else edges.set(key, {a, b, weight: sum});
|
|
108
|
+
}
|
|
109
|
+
function triangle(a: number, b: number, c: number, weight: number): void {
|
|
110
|
+
edge(a, b, weight);
|
|
111
|
+
edge(b, c, weight);
|
|
112
|
+
edge(c, a, weight);
|
|
113
|
+
}
|
|
114
|
+
function addMesh({indices, gridCoordinates}: TerrainCoverageMesh, weight: number): void {
|
|
115
|
+
for (let i = 0; i < indices.length; i += 3) {
|
|
116
|
+
const ids = [indices[i], indices[i + 1], indices[i + 2]].map(id => {
|
|
117
|
+
if (!Number.isSafeInteger(id) || id < 0 || id >= gridCoordinates.length / 2)
|
|
118
|
+
throw new Error('Invalid coverage vertex index');
|
|
119
|
+
return vertex(gridCoordinates[id * 2], gridCoordinates[id * 2 + 1]);
|
|
120
|
+
});
|
|
121
|
+
const [a, b, c] = ids.map(id => vertices[id]);
|
|
122
|
+
const left = (b[0] - a[0]) * (c[1] - a[1]);
|
|
123
|
+
const right = (b[1] - a[1]) * (c[0] - a[0]);
|
|
124
|
+
const det = left - right;
|
|
125
|
+
const limit = 8 * Number.EPSILON * (Math.abs(left) + Math.abs(right)) + 8 * Number.MIN_VALUE;
|
|
126
|
+
let sign = Math.sign(det);
|
|
127
|
+
if (!Number.isFinite(det) || Math.abs(det) <= limit) {
|
|
128
|
+
const [ax, ay] = a.map(exact),
|
|
129
|
+
[bx, by] = b.map(exact),
|
|
130
|
+
[cx, cy] = c.map(exact);
|
|
131
|
+
const determinant = (bx - ax) * (cy - ay) - (by - ay) * (cx - ax);
|
|
132
|
+
sign = determinant > 0n ? 1 : determinant < 0n ? -1 : 0;
|
|
133
|
+
}
|
|
134
|
+
if (!sign) throw new Error('Degenerate coverage triangle');
|
|
135
|
+
triangle(ids[0], ids[1], ids[2], weight * sign);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
addMesh(mesh, 1);
|
|
139
|
+
if (referenceMesh) addMesh(referenceMesh, -1);
|
|
140
|
+
else
|
|
141
|
+
for (let y = 0; y < field.height - 1; y++) {
|
|
142
|
+
for (let x = 0; x < field.width - 1; x++) {
|
|
143
|
+
const valid = [
|
|
144
|
+
field.getHeight(x, y),
|
|
145
|
+
field.getHeight(x + 1, y),
|
|
146
|
+
field.getHeight(x, y + 1),
|
|
147
|
+
field.getHeight(x + 1, y + 1)
|
|
148
|
+
].map(z => z !== undefined);
|
|
149
|
+
if (reference === 'bilinear' && !valid.every(Boolean)) continue;
|
|
150
|
+
const corners: Point[] = [
|
|
151
|
+
[x, y],
|
|
152
|
+
[x + 1, y],
|
|
153
|
+
[x, y + 1],
|
|
154
|
+
[x + 1, y + 1]
|
|
155
|
+
];
|
|
156
|
+
const faces =
|
|
157
|
+
field.diagonal === '00-11'
|
|
158
|
+
? [
|
|
159
|
+
[0, 1, 3],
|
|
160
|
+
[0, 3, 2]
|
|
161
|
+
]
|
|
162
|
+
: [
|
|
163
|
+
[0, 1, 2],
|
|
164
|
+
[1, 3, 2]
|
|
165
|
+
];
|
|
166
|
+
for (const [a, b, c] of faces) {
|
|
167
|
+
if (!valid[a] || !valid[b] || !valid[c]) continue;
|
|
168
|
+
triangle(vertex(...corners[a]), vertex(...corners[b]), vertex(...corners[c]), -1);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// A compactly supported triangle multiplicity is uniquely determined by its oriented
|
|
174
|
+
// boundary. Group collinear residual edges to cancel differently subdivided boundaries.
|
|
175
|
+
const lines = new Map<string, Map<number, {point: Point; delta: number}>>();
|
|
176
|
+
for (const {a, b, weight} of edges.values()) {
|
|
177
|
+
const p = vertices[a],
|
|
178
|
+
q = vertices[b];
|
|
179
|
+
const [x, y] = p.map(exact),
|
|
180
|
+
[u, v] = q.map(exact);
|
|
181
|
+
const divisor = gcd(u - x, v - y);
|
|
182
|
+
let dx = (u - x) / divisor,
|
|
183
|
+
dy = (v - y) / divisor;
|
|
184
|
+
if (dx < 0n || (dx === 0n && dy < 0n)) [dx, dy] = [-dx, -dy];
|
|
185
|
+
const key = `${dx},${dy},${dx * y - dy * x}`;
|
|
186
|
+
let events = lines.get(key);
|
|
187
|
+
if (!events) {
|
|
188
|
+
events = new Map();
|
|
189
|
+
lines.set(key, events);
|
|
190
|
+
}
|
|
191
|
+
const axis = dx === 0n ? 1 : 0;
|
|
192
|
+
const delta = weight;
|
|
193
|
+
events.set(p[axis], {point: p, delta: (events.get(p[axis])?.delta ?? 0) + delta});
|
|
194
|
+
events.set(q[axis], {point: q, delta: (events.get(q[axis])?.delta ?? 0) - delta});
|
|
195
|
+
}
|
|
196
|
+
let unmatchedSegments = 0;
|
|
197
|
+
let witness: TerrainMeshCoverage['witness'] = null;
|
|
198
|
+
for (const events of lines.values()) {
|
|
199
|
+
const sorted = [...events].sort(([a], [b]) => a - b);
|
|
200
|
+
let jump = 0;
|
|
201
|
+
for (let i = 0; i + 1 < sorted.length; i++) {
|
|
202
|
+
jump += sorted[i][1].delta;
|
|
203
|
+
if (!jump) continue;
|
|
204
|
+
unmatchedSegments++;
|
|
205
|
+
witness ??= {start: sorted[i][1].point, end: sorted[i + 1][1].point, multiplicityJump: jump};
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return {
|
|
209
|
+
status: unmatchedSegments ? 'mismatch' : 'certified',
|
|
210
|
+
unmatchedSegments,
|
|
211
|
+
witness,
|
|
212
|
+
trianglesVisited,
|
|
213
|
+
residualEdges: edges.size
|
|
214
|
+
};
|
|
215
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// math.gl
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
-
import type {
|
|
4
|
+
import type {TerrainSeamMesh} from './mesh-stitch';
|
|
5
5
|
import {HeightField} from './height-field';
|
|
6
6
|
import {
|
|
7
7
|
validateTerrainDirtyWindows,
|
|
@@ -18,7 +18,7 @@ export type TerrainMeshRegions<T> = {
|
|
|
18
18
|
rebuilt: Uint32Array;
|
|
19
19
|
};
|
|
20
20
|
/** Internal independent chunk updater. Buffers in untouched regions remain shared. */
|
|
21
|
-
export function updateMeshRegions<T extends
|
|
21
|
+
export function updateMeshRegions<T extends TerrainSeamMesh | null>(
|
|
22
22
|
previous: TerrainMeshRegions<T> | null,
|
|
23
23
|
field: HeightField,
|
|
24
24
|
dirty: readonly TerrainDirtyWindow[],
|
|
@@ -41,6 +41,24 @@ export function updateMeshRegions<T extends TerrainGridMesh | null>(
|
|
|
41
41
|
sameTerrainFrame(previous.field, field) &&
|
|
42
42
|
previous.cellsPerRegion === cells &&
|
|
43
43
|
previous.configuration === configuration;
|
|
44
|
+
const affectedRegions = new Set<number>();
|
|
45
|
+
const columns = Math.ceil((field.width - 1) / cells);
|
|
46
|
+
const rows = Math.ceil((field.height - 1) / cells);
|
|
47
|
+
if (reusable) {
|
|
48
|
+
for (const window of dirty) {
|
|
49
|
+
// Shared sample rows and columns belong to both incident regions.
|
|
50
|
+
const firstColumn = Math.max(0, Math.ceil(window.minColumn / cells) - 1);
|
|
51
|
+
const firstRow = Math.max(0, Math.ceil(window.minRow / cells) - 1);
|
|
52
|
+
const lastColumn = Math.min(columns - 1, Math.floor(window.maxColumn / cells));
|
|
53
|
+
const lastRow = Math.min(rows - 1, Math.floor(window.maxRow / cells));
|
|
54
|
+
for (let y = firstRow; y <= lastRow; y++) {
|
|
55
|
+
for (let x = firstColumn; x <= lastColumn; x++) {
|
|
56
|
+
if (++tests > budget) throw new Error('Terrain mesh region work budget exceeded');
|
|
57
|
+
affectedRegions.add(y * columns + x);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
44
62
|
const cache = new Map(previous?.regions.map(r => [`${r.column},${r.row}`, r]));
|
|
45
63
|
for (let row = 0; row < field.height - 1; row += cells)
|
|
46
64
|
for (let column = 0; column < field.width - 1; column += cells) {
|
|
@@ -49,17 +67,7 @@ export function updateMeshRegions<T extends TerrainGridMesh | null>(
|
|
|
49
67
|
count += width * height;
|
|
50
68
|
if (!Number.isSafeInteger(count) || count > limit)
|
|
51
69
|
throw new Error('Terrain mesh region sample capacity exceeded');
|
|
52
|
-
|
|
53
|
-
for (const w of reusable ? dirty : []) {
|
|
54
|
-
if (++tests > budget) throw new Error('Terrain mesh region work budget exceeded');
|
|
55
|
-
if (
|
|
56
|
-
w.minColumn <= column + width - 1 &&
|
|
57
|
-
w.maxColumn >= column &&
|
|
58
|
-
w.minRow <= row + height - 1 &&
|
|
59
|
-
w.maxRow >= row
|
|
60
|
-
)
|
|
61
|
-
affected = true;
|
|
62
|
-
}
|
|
70
|
+
const affected = !reusable || affectedRegions.has((row / cells) * columns + column / cells);
|
|
63
71
|
const old = cache.get(`${column},${row}`);
|
|
64
72
|
if (!affected && old) {
|
|
65
73
|
regions.push(old);
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
// math.gl
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
-
import {
|
|
4
|
+
import {createTerrainBoxIndex, type TerrainBox} from './box-index';
|
|
5
|
+
import {
|
|
6
|
+
buildMixedVertexTileHalos,
|
|
7
|
+
type TerrainMixedVertexTile,
|
|
8
|
+
type MixedHaloPreparation
|
|
9
|
+
} from './mixed-vertex-halos';
|
|
5
10
|
|
|
6
11
|
export type TerrainMixedHaloSnapshot = {
|
|
7
12
|
/** Immutable source descriptors and buffers; replace the descriptor after every edit. */
|
|
@@ -11,6 +16,62 @@ export type TerrainMixedHaloSnapshot = {
|
|
|
11
16
|
const key = (t: Pick<TerrainMixedVertexTile, 'spacing' | 'column' | 'row'>) =>
|
|
12
17
|
`${t.spacing}/${t.column}/${t.row}`;
|
|
13
18
|
|
|
19
|
+
const bounds = (tile: TerrainMixedVertexTile, padding = 0): TerrainBox => ({
|
|
20
|
+
minX: tile.column - padding,
|
|
21
|
+
minY: tile.row - padding,
|
|
22
|
+
maxX: tile.column + (tile.field.width - 1) * tile.spacing + padding,
|
|
23
|
+
maxY: tile.row + (tile.field.height - 1) * tile.spacing + padding
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const preparations = new WeakMap<TerrainMixedHaloSnapshot, MixedHaloPreparation>();
|
|
27
|
+
|
|
28
|
+
const targetIndexes = new WeakMap<
|
|
29
|
+
TerrainMixedHaloSnapshot,
|
|
30
|
+
ReturnType<typeof createTerrainBoxIndex>
|
|
31
|
+
>();
|
|
32
|
+
|
|
33
|
+
// Each changed vertex can influence only the cells incident to that vertex.
|
|
34
|
+
function editedBounds(
|
|
35
|
+
old: TerrainMixedVertexTile,
|
|
36
|
+
next: TerrainMixedVertexTile
|
|
37
|
+
): TerrainBox | null {
|
|
38
|
+
const a = old.field;
|
|
39
|
+
const b = next.field;
|
|
40
|
+
if (
|
|
41
|
+
a.width !== b.width ||
|
|
42
|
+
a.height !== b.height ||
|
|
43
|
+
(a.diagonal ?? '00-11') !== (b.diagonal ?? '00-11')
|
|
44
|
+
)
|
|
45
|
+
return bounds(next);
|
|
46
|
+
const sample = (field: TerrainMixedVertexTile['field'], x: number, y: number) => {
|
|
47
|
+
const i = (field.offset ?? 0) + y * (field.stride ?? field.width) + x;
|
|
48
|
+
const z = field.heights[i];
|
|
49
|
+
return Number.isFinite(z) && (field.validity === undefined || field.validity[i] !== 0)
|
|
50
|
+
? z
|
|
51
|
+
: undefined;
|
|
52
|
+
};
|
|
53
|
+
let left = b.width,
|
|
54
|
+
right = -1,
|
|
55
|
+
top = b.height,
|
|
56
|
+
bottom = -1;
|
|
57
|
+
for (let y = 0; y < b.height; y++) {
|
|
58
|
+
for (let x = 0; x < b.width; x++) {
|
|
59
|
+
if (Object.is(sample(a, x, y), sample(b, x, y))) continue;
|
|
60
|
+
left = Math.min(left, x);
|
|
61
|
+
right = Math.max(right, x);
|
|
62
|
+
top = Math.min(top, y);
|
|
63
|
+
bottom = Math.max(bottom, y);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (right < 0) return null;
|
|
67
|
+
return {
|
|
68
|
+
minX: next.column + Math.max(0, left - 1) * next.spacing,
|
|
69
|
+
maxX: next.column + Math.min(b.width - 1, right + 1) * next.spacing,
|
|
70
|
+
minY: next.row + Math.max(0, top - 1) * next.spacing,
|
|
71
|
+
maxY: next.row + Math.min(b.height - 1, bottom + 1) * next.spacing
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
14
75
|
/** Conservative dependencies include old and new coverage, corners and overlap ownership. */
|
|
15
76
|
export function updateTerrainMixedVertexTileHalos(
|
|
16
77
|
previous: TerrainMixedHaloSnapshot | null,
|
|
@@ -22,35 +83,62 @@ export function updateTerrainMixedVertexTileHalos(
|
|
|
22
83
|
throw new Error('Invalid mixed halo dependency budget');
|
|
23
84
|
const old = new Map(previous?.sources.map(t => [key(t), t]));
|
|
24
85
|
const current = new Map(resident.map(t => [key(t), t]));
|
|
25
|
-
const changed = [...old.values()]
|
|
26
|
-
.filter(t => current.get(key(t)) !== t)
|
|
27
|
-
.concat(resident.filter(t => old.get(key(t)) !== t));
|
|
28
86
|
const cached = new Map(previous?.tiles.map(t => [key(t), t]));
|
|
29
87
|
const targets = new Set<string>();
|
|
88
|
+
const unchangedLayout =
|
|
89
|
+
previous?.sources.length === resident.length &&
|
|
90
|
+
resident.every((tile, i) => {
|
|
91
|
+
const prior = previous.sources[i];
|
|
92
|
+
return (
|
|
93
|
+
key(prior) === key(tile) &&
|
|
94
|
+
prior.field.width === tile.field.width &&
|
|
95
|
+
prior.field.height === tile.field.height
|
|
96
|
+
);
|
|
97
|
+
});
|
|
98
|
+
let index = unchangedLayout && previous ? targetIndexes.get(previous) : undefined;
|
|
30
99
|
let tests = 0;
|
|
100
|
+
const charge = () => {
|
|
101
|
+
if (++tests > budget) throw new Error('Mixed halo dependency budget exceeded');
|
|
102
|
+
};
|
|
103
|
+
function invalidate(box: TerrainBox): void {
|
|
104
|
+
index ??= createTerrainBoxIndex(resident.map(tile => bounds(tile, tile.spacing)));
|
|
105
|
+
for (const id of index(box, charge)) targets.add(key(resident[id]));
|
|
106
|
+
}
|
|
31
107
|
for (const tile of resident) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
108
|
+
const address = key(tile);
|
|
109
|
+
const prior = old.get(address);
|
|
110
|
+
if (!cached.has(address) || prior !== tile) targets.add(address);
|
|
111
|
+
if (prior === tile || !previous) continue;
|
|
112
|
+
if (!prior) {
|
|
113
|
+
invalidate(bounds(tile));
|
|
114
|
+
} else if (prior.field.width !== tile.field.width || prior.field.height !== tile.field.height) {
|
|
115
|
+
invalidate(bounds(prior));
|
|
116
|
+
invalidate(bounds(tile));
|
|
117
|
+
} else {
|
|
118
|
+
const dirty = editedBounds(prior, tile);
|
|
119
|
+
if (dirty) invalidate(dirty);
|
|
43
120
|
}
|
|
44
121
|
}
|
|
45
|
-
const
|
|
122
|
+
for (const [address, tile] of old) {
|
|
123
|
+
if (!current.has(address) && resident.length) invalidate(bounds(tile));
|
|
124
|
+
}
|
|
125
|
+
const priorPreparation = previous && preparations.get(previous);
|
|
126
|
+
const preparation: MixedHaloPreparation = {
|
|
127
|
+
fields: priorPreparation?.fields ?? new WeakMap(),
|
|
128
|
+
layout: priorPreparation?.layout
|
|
129
|
+
};
|
|
130
|
+
const built = buildMixedVertexTileHalos(resident, options, targets, preparation).tiles;
|
|
46
131
|
for (const tile of built) cached.set(key(tile), tile);
|
|
47
132
|
const tiles = resident
|
|
48
133
|
.map(t => cached.get(key(t))!)
|
|
49
134
|
.sort((a, b) => a.spacing - b.spacing || a.column - b.column || a.row - b.row);
|
|
50
|
-
|
|
135
|
+
const snapshot = {
|
|
51
136
|
sources: resident.slice(),
|
|
52
137
|
tiles,
|
|
53
138
|
rebuilt: built.map(key),
|
|
54
139
|
removed: [...old.keys()].filter(k => !current.has(k)).sort()
|
|
55
140
|
};
|
|
141
|
+
preparations.set(snapshot, preparation);
|
|
142
|
+
if (index) targetIndexes.set(snapshot, index);
|
|
143
|
+
return snapshot;
|
|
56
144
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// math.gl
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
import {createTerrainBoxIndex} from './box-index';
|
|
4
5
|
import {HeightField} from './height-field';
|
|
5
6
|
import {sampleTerrainTriangleHeight} from './sample-triangle-height';
|
|
6
7
|
import type {HeightFieldProps} from './types';
|
|
@@ -34,11 +35,18 @@ export function createTerrainMixedVertexTileHalos(
|
|
|
34
35
|
return buildMixedVertexTileHalos(tiles, options);
|
|
35
36
|
}
|
|
36
37
|
|
|
38
|
+
/** Internal preparation shared only by immutable incremental snapshots. */
|
|
39
|
+
export type MixedHaloPreparation = {
|
|
40
|
+
fields: WeakMap<TerrainMixedVertexTile, HeightField>;
|
|
41
|
+
layout?: {keys: string[]; query: ReturnType<typeof createTerrainBoxIndex>};
|
|
42
|
+
};
|
|
43
|
+
|
|
37
44
|
/** Internal selective builder; all source metadata and total capacity are validated. */
|
|
38
45
|
export function buildMixedVertexTileHalos(
|
|
39
46
|
tiles: readonly TerrainMixedVertexTile[],
|
|
40
47
|
options: {maxSamples?: number; maxTileTests?: number} = {},
|
|
41
|
-
targets?: ReadonlySet<string
|
|
48
|
+
targets?: ReadonlySet<string>,
|
|
49
|
+
preparation?: MixedHaloPreparation
|
|
42
50
|
): ReturnType<typeof createTerrainMixedVertexTileHalos> {
|
|
43
51
|
const maxSamples = options.maxSamples ?? 4_000_000,
|
|
44
52
|
maxTests = options.maxTileTests ?? 10_000_000;
|
|
@@ -64,7 +72,11 @@ export function buildMixedVertexTileHalos(
|
|
|
64
72
|
count += (tile.field.width + 2) * (tile.field.height + 2);
|
|
65
73
|
if (!Number.isSafeInteger(count) || count > maxSamples)
|
|
66
74
|
throw new Error('Mixed halo sample capacity exceeded');
|
|
67
|
-
|
|
75
|
+
let field = preparation?.fields.get(tile);
|
|
76
|
+
if (!field) {
|
|
77
|
+
field = new HeightField(tile.field);
|
|
78
|
+
preparation?.fields.set(tile, field);
|
|
79
|
+
}
|
|
68
80
|
if (field.registration !== 'vertex')
|
|
69
81
|
throw new Error('Mixed halos require vertex registration');
|
|
70
82
|
const column = field.columnStep.map(v => v / tile.spacing);
|
|
@@ -109,7 +121,39 @@ export function buildMixedVertexTileHalos(
|
|
|
109
121
|
if (sources.length && !Number.isSafeInteger(max - min))
|
|
110
122
|
throw new Error('Mixed halo lattice span overflow');
|
|
111
123
|
}
|
|
124
|
+
if (targets && targets.size === 0) return {tiles: []};
|
|
125
|
+
let index: ReturnType<typeof createTerrainBoxIndex> | undefined;
|
|
126
|
+
if (sources.length > 8) {
|
|
127
|
+
const keys =
|
|
128
|
+
preparation &&
|
|
129
|
+
sources.map(
|
|
130
|
+
tile =>
|
|
131
|
+
`${tile.spacing}/${tile.column}/${tile.row}/${tile.source.width}/${tile.source.height}`
|
|
132
|
+
);
|
|
133
|
+
const cached = preparation?.layout;
|
|
134
|
+
if (
|
|
135
|
+
keys &&
|
|
136
|
+
cached &&
|
|
137
|
+
keys.length === cached.keys.length &&
|
|
138
|
+
keys.every((key, i) => key === cached.keys[i])
|
|
139
|
+
) {
|
|
140
|
+
index = cached.query;
|
|
141
|
+
} else {
|
|
142
|
+
index = createTerrainBoxIndex(
|
|
143
|
+
sources.map(tile => ({
|
|
144
|
+
minX: tile.column,
|
|
145
|
+
minY: tile.row,
|
|
146
|
+
maxX: tile.column + (tile.source.width - 1) * tile.spacing,
|
|
147
|
+
maxY: tile.row + (tile.source.height - 1) * tile.spacing
|
|
148
|
+
}))
|
|
149
|
+
);
|
|
150
|
+
if (preparation && keys) preparation.layout = {keys, query: index};
|
|
151
|
+
}
|
|
152
|
+
}
|
|
112
153
|
let tests = 0;
|
|
154
|
+
const charge = () => {
|
|
155
|
+
if (++tests > maxTests) throw new Error('Mixed halo tile-test budget exceeded');
|
|
156
|
+
};
|
|
113
157
|
return {
|
|
114
158
|
tiles: sources
|
|
115
159
|
.filter(tile => !targets || targets.has(`${tile.spacing}/${tile.column}/${tile.row}`))
|
|
@@ -120,14 +164,24 @@ export function buildMixedVertexTileHalos(
|
|
|
120
164
|
const heights = new Float64Array(width * height),
|
|
121
165
|
validity = new Uint8Array(heights.length),
|
|
122
166
|
statuses = new Uint8Array(heights.length);
|
|
167
|
+
const candidates =
|
|
168
|
+
index?.(
|
|
169
|
+
{
|
|
170
|
+
minX: tile.column - tile.spacing,
|
|
171
|
+
minY: tile.row - tile.spacing,
|
|
172
|
+
maxX: tile.column + source.width * tile.spacing,
|
|
173
|
+
maxY: tile.row + source.height * tile.spacing
|
|
174
|
+
},
|
|
175
|
+
charge
|
|
176
|
+
).map(id => sources[id]) ?? sources;
|
|
123
177
|
for (let y = 0; y < height; y++)
|
|
124
178
|
for (let x = 0; x < width; x++) {
|
|
125
179
|
const gx = tile.column + (x - 1) * tile.spacing,
|
|
126
180
|
gy = tile.row + (y - 1) * tile.spacing;
|
|
127
181
|
if (![gx, gy].every(Number.isSafeInteger))
|
|
128
182
|
throw new Error('Mixed halo lattice overflow');
|
|
129
|
-
for (const candidate of
|
|
130
|
-
|
|
183
|
+
for (const candidate of candidates) {
|
|
184
|
+
charge();
|
|
131
185
|
const cx = (gx - candidate.column) / candidate.spacing,
|
|
132
186
|
cy = (gy - candidate.row) / candidate.spacing;
|
|
133
187
|
const f = candidate.source;
|