@math.gl/geospatial 5.0.0-beta.5 → 5.0.0-beta.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1526 -192
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +38 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -1
- package/dist/terrain/bounds-regions.d.ts +15 -0
- package/dist/terrain/bounds-regions.d.ts.map +1 -0
- package/dist/terrain/bounds-regions.js +84 -0
- package/dist/terrain/bounds-regions.js.map +1 -0
- package/dist/terrain/bounds-update.d.ts +11 -0
- package/dist/terrain/bounds-update.d.ts.map +1 -0
- package/dist/terrain/bounds-update.js +10 -0
- package/dist/terrain/bounds-update.js.map +1 -0
- package/dist/terrain/box-index.d.ts +9 -0
- package/dist/terrain/box-index.d.ts.map +1 -0
- package/dist/terrain/box-index.js +43 -0
- package/dist/terrain/box-index.js.map +1 -0
- package/dist/terrain/contour-simplify.d.ts +1 -0
- package/dist/terrain/contour-simplify.d.ts.map +1 -1
- package/dist/terrain/contour-simplify.js +5 -1
- package/dist/terrain/contour-simplify.js.map +1 -1
- package/dist/terrain/contour-topology.d.ts +30 -0
- package/dist/terrain/contour-topology.d.ts.map +1 -0
- package/dist/terrain/contour-topology.js +236 -0
- package/dist/terrain/contour-topology.js.map +1 -0
- package/dist/terrain/delatin-regions.d.ts +14 -0
- package/dist/terrain/delatin-regions.d.ts.map +1 -0
- package/dist/terrain/delatin-regions.js +15 -0
- package/dist/terrain/delatin-regions.js.map +1 -0
- package/dist/terrain/dirty-regions.d.ts +11 -0
- package/dist/terrain/dirty-regions.d.ts.map +1 -0
- package/dist/terrain/dirty-regions.js +34 -0
- package/dist/terrain/dirty-regions.js.map +1 -0
- package/dist/terrain/gradient-pyramid-regions.d.ts +15 -0
- package/dist/terrain/gradient-pyramid-regions.d.ts.map +1 -0
- package/dist/terrain/gradient-pyramid-regions.js +112 -0
- package/dist/terrain/gradient-pyramid-regions.js.map +1 -0
- package/dist/terrain/gradient-pyramid-update.d.ts +11 -0
- package/dist/terrain/gradient-pyramid-update.d.ts.map +1 -0
- package/dist/terrain/gradient-pyramid-update.js +17 -0
- package/dist/terrain/gradient-pyramid-update.js.map +1 -0
- package/dist/terrain/index.cjs +1422 -88
- package/dist/terrain/index.cjs.map +3 -3
- package/dist/terrain/index.d.ts +38 -0
- package/dist/terrain/index.d.ts.map +1 -1
- package/dist/terrain/index.js +19 -0
- package/dist/terrain/index.js.map +1 -1
- package/dist/terrain/isobands.d.ts +24 -0
- package/dist/terrain/isobands.d.ts.map +1 -0
- package/dist/terrain/isobands.js +281 -0
- package/dist/terrain/isobands.js.map +1 -0
- package/dist/terrain/lod-morph-evaluate.d.ts +8 -0
- package/dist/terrain/lod-morph-evaluate.d.ts.map +1 -0
- package/dist/terrain/lod-morph-evaluate.js +30 -0
- package/dist/terrain/lod-morph-evaluate.js.map +1 -0
- package/dist/terrain/lod-morph.d.ts +35 -0
- package/dist/terrain/lod-morph.d.ts.map +1 -0
- package/dist/terrain/lod-morph.js +87 -0
- package/dist/terrain/lod-morph.js.map +1 -0
- package/dist/terrain/martini-regions.d.ts +14 -0
- package/dist/terrain/martini-regions.d.ts.map +1 -0
- package/dist/terrain/martini-regions.js +26 -0
- package/dist/terrain/martini-regions.js.map +1 -0
- package/dist/terrain/mesh-error.d.ts.map +1 -1
- package/dist/terrain/mesh-error.js +9 -1
- package/dist/terrain/mesh-error.js.map +1 -1
- package/dist/terrain/mesh-regions-data.d.ts +24 -0
- package/dist/terrain/mesh-regions-data.d.ts.map +1 -0
- package/dist/terrain/mesh-regions-data.js +80 -0
- package/dist/terrain/mesh-regions-data.js.map +1 -0
- package/dist/terrain/mesh-regions.d.ts +12 -0
- package/dist/terrain/mesh-regions.d.ts.map +1 -0
- package/dist/terrain/mesh-regions.js +10 -0
- package/dist/terrain/mesh-regions.js.map +1 -0
- package/dist/terrain/mesh-update.d.ts +11 -0
- package/dist/terrain/mesh-update.d.ts.map +1 -0
- package/dist/terrain/mesh-update.js +10 -0
- package/dist/terrain/mesh-update.js.map +1 -0
- package/dist/terrain/mixed-gradient-update.d.ts +15 -0
- package/dist/terrain/mixed-gradient-update.d.ts.map +1 -0
- package/dist/terrain/mixed-gradient-update.js +24 -0
- package/dist/terrain/mixed-gradient-update.js.map +1 -0
- package/dist/terrain/mixed-halo-update.d.ts +16 -0
- package/dist/terrain/mixed-halo-update.d.ts.map +1 -0
- package/dist/terrain/mixed-halo-update.js +46 -0
- package/dist/terrain/mixed-halo-update.js.map +1 -0
- package/dist/terrain/mixed-vertex-halos.d.ts +32 -0
- package/dist/terrain/mixed-vertex-halos.d.ts.map +1 -0
- package/dist/terrain/mixed-vertex-halos.js +125 -0
- package/dist/terrain/mixed-vertex-halos.js.map +1 -0
- package/dist/terrain/persistent-bounds.d.ts +16 -0
- package/dist/terrain/persistent-bounds.d.ts.map +1 -0
- package/dist/terrain/persistent-bounds.js +51 -0
- package/dist/terrain/persistent-bounds.js.map +1 -0
- package/dist/terrain/persistent-gradients.d.ts +16 -0
- package/dist/terrain/persistent-gradients.d.ts.map +1 -0
- package/dist/terrain/persistent-gradients.js +69 -0
- package/dist/terrain/persistent-gradients.js.map +1 -0
- package/dist/terrain/persistent-hierarchy-data.d.ts +41 -0
- package/dist/terrain/persistent-hierarchy-data.d.ts.map +1 -0
- package/dist/terrain/persistent-hierarchy-data.js +130 -0
- package/dist/terrain/persistent-hierarchy-data.js.map +1 -0
- package/dist/terrain/sample-triangle-height.d.ts +4 -0
- package/dist/terrain/sample-triangle-height.d.ts.map +1 -0
- package/dist/terrain/sample-triangle-height.js +46 -0
- package/dist/terrain/sample-triangle-height.js.map +1 -0
- package/dist/terrain/tile-update-data.d.ts +14 -0
- package/dist/terrain/tile-update-data.d.ts.map +1 -0
- package/dist/terrain/tile-update-data.js +23 -0
- package/dist/terrain/tile-update-data.js.map +1 -0
- package/package.json +7 -7
- package/src/index.ts +59 -0
- package/src/terrain/bounds-regions.ts +106 -0
- package/src/terrain/bounds-update.ts +19 -0
- package/src/terrain/box-index.ts +47 -0
- package/src/terrain/contour-simplify.ts +10 -2
- package/src/terrain/contour-topology.ts +263 -0
- package/src/terrain/delatin-regions.ts +25 -0
- package/src/terrain/dirty-regions.ts +61 -0
- package/src/terrain/gradient-pyramid-regions.ts +140 -0
- package/src/terrain/gradient-pyramid-update.ts +25 -0
- package/src/terrain/index.ts +56 -0
- package/src/terrain/isobands.ts +348 -0
- package/src/terrain/lod-morph-evaluate.ts +36 -0
- package/src/terrain/lod-morph.ts +138 -0
- package/src/terrain/martini-regions.ts +38 -0
- package/src/terrain/mesh-error.ts +14 -1
- package/src/terrain/mesh-regions-data.ts +113 -0
- package/src/terrain/mesh-regions.ts +18 -0
- package/src/terrain/mesh-update.ts +15 -0
- package/src/terrain/mixed-gradient-update.ts +40 -0
- package/src/terrain/mixed-halo-update.ts +56 -0
- package/src/terrain/mixed-vertex-halos.ts +170 -0
- package/src/terrain/persistent-bounds.ts +95 -0
- package/src/terrain/persistent-gradients.ts +117 -0
- package/src/terrain/persistent-hierarchy-data.ts +196 -0
- package/src/terrain/sample-triangle-height.ts +50 -0
- package/src/terrain/tile-update-data.ts +32 -0
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
import {HeightFieldMinMax, type TerrainBoundsLevel} from './bounds';
|
|
5
|
+
import {
|
|
6
|
+
sameTerrainFrame,
|
|
7
|
+
validateTerrainDirtyWindows,
|
|
8
|
+
terrainDirtyIndices,
|
|
9
|
+
type TerrainDirtyWindow
|
|
10
|
+
} from './dirty-regions';
|
|
11
|
+
import type {HeightField} from './height-field';
|
|
12
|
+
export type TerrainBoundsRegions = {
|
|
13
|
+
field: HeightField;
|
|
14
|
+
levels: TerrainBoundsLevel[];
|
|
15
|
+
updatedCounts: Uint32Array;
|
|
16
|
+
};
|
|
17
|
+
/** Copy-on-write hierarchy arrays; recompute only cells near dirty source samples
|
|
18
|
+
* and their ancestors. Array copies retain snapshot immutability. */
|
|
19
|
+
export function updateTerrainBoundsRegions(
|
|
20
|
+
previous: TerrainBoundsRegions | null,
|
|
21
|
+
field: HeightField,
|
|
22
|
+
dirty: readonly TerrainDirtyWindow[],
|
|
23
|
+
options: {maxSamples?: number; maxTests?: number} = {}
|
|
24
|
+
): TerrainBoundsRegions {
|
|
25
|
+
const limit = options.maxSamples ?? 4_000_000,
|
|
26
|
+
budget = options.maxTests ?? 10_000_000;
|
|
27
|
+
if (
|
|
28
|
+
![limit, budget].every(Number.isSafeInteger) ||
|
|
29
|
+
limit < 0 ||
|
|
30
|
+
budget < 0 ||
|
|
31
|
+
field.width * field.height > limit
|
|
32
|
+
)
|
|
33
|
+
throw new Error('Invalid bounds region capacity');
|
|
34
|
+
validateTerrainDirtyWindows(dirty, field.width, field.height);
|
|
35
|
+
if (!previous || !sameTerrainFrame(previous.field, field)) {
|
|
36
|
+
const bounds = new HeightFieldMinMax(field),
|
|
37
|
+
levels = Array.from({length: bounds.levelCount}, (_, i) => bounds.copyLevel(i));
|
|
38
|
+
return {field, levels, updatedCounts: Uint32Array.from(levels, l => l.width * l.height)};
|
|
39
|
+
}
|
|
40
|
+
let tests = 0;
|
|
41
|
+
const charge = () => {
|
|
42
|
+
if (++tests > budget) throw new Error('Bounds region work budget exceeded');
|
|
43
|
+
};
|
|
44
|
+
let ids = terrainDirtyIndices(dirty, field.width - 1, field.height - 1, 1, charge);
|
|
45
|
+
const counts: number[] = [],
|
|
46
|
+
levels = previous.levels.slice();
|
|
47
|
+
for (let l = 0; l < levels.length; l++) {
|
|
48
|
+
counts.push(ids.size);
|
|
49
|
+
if (!ids.size) continue;
|
|
50
|
+
const old = levels[l],
|
|
51
|
+
next = {
|
|
52
|
+
...old,
|
|
53
|
+
minimums: old.minimums.slice(),
|
|
54
|
+
maximums: old.maximums.slice(),
|
|
55
|
+
populatedCellCounts: old.populatedCellCounts.slice(),
|
|
56
|
+
completeCellCounts: old.completeCellCounts.slice()
|
|
57
|
+
};
|
|
58
|
+
const parents = new Set<number>();
|
|
59
|
+
for (const id of ids) {
|
|
60
|
+
charge();
|
|
61
|
+
const x = id % next.width,
|
|
62
|
+
y = Math.floor(id / next.width);
|
|
63
|
+
let min = Infinity,
|
|
64
|
+
max = -Infinity,
|
|
65
|
+
populated = 0,
|
|
66
|
+
complete = 0;
|
|
67
|
+
if (l === 0) {
|
|
68
|
+
let valid = 0;
|
|
69
|
+
for (let dy = 0; dy < 2; dy++)
|
|
70
|
+
for (let dx = 0; dx < 2; dx++) {
|
|
71
|
+
const h = field.getHeight(x + dx, y + dy);
|
|
72
|
+
if (h !== undefined) {
|
|
73
|
+
valid++;
|
|
74
|
+
min = Math.min(min, h);
|
|
75
|
+
max = Math.max(max, h);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
populated = valid ? 1 : 0;
|
|
79
|
+
complete = valid === 4 ? 1 : 0;
|
|
80
|
+
} else {
|
|
81
|
+
const child = levels[l - 1];
|
|
82
|
+
for (let dy = 0; dy < 2; dy++)
|
|
83
|
+
for (let dx = 0; dx < 2; dx++) {
|
|
84
|
+
const cx = x * 2 + dx,
|
|
85
|
+
cy = y * 2 + dy;
|
|
86
|
+
if (cx >= child.width || cy >= child.height) continue;
|
|
87
|
+
const i = cy * child.width + cx;
|
|
88
|
+
if (child.populatedCellCounts[i]) {
|
|
89
|
+
min = Math.min(min, child.minimums[i]);
|
|
90
|
+
max = Math.max(max, child.maximums[i]);
|
|
91
|
+
}
|
|
92
|
+
populated += child.populatedCellCounts[i];
|
|
93
|
+
complete += child.completeCellCounts[i];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
next.minimums[id] = populated ? min : 0;
|
|
97
|
+
next.maximums[id] = populated ? max : 0;
|
|
98
|
+
next.populatedCellCounts[id] = populated;
|
|
99
|
+
next.completeCellCounts[id] = complete;
|
|
100
|
+
parents.add(Math.floor(y / 2) * Math.ceil(next.width / 2) + Math.floor(x / 2));
|
|
101
|
+
}
|
|
102
|
+
levels[l] = next;
|
|
103
|
+
ids = parents;
|
|
104
|
+
}
|
|
105
|
+
return {field, levels, updatedCounts: Uint32Array.from(counts)};
|
|
106
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
import {HeightFieldMinMax} from './bounds';
|
|
5
|
+
import {updateTerrainTileData, type TerrainFieldTile} from './tile-update-data';
|
|
6
|
+
export type TerrainBoundsSnapshot = {source: TerrainFieldTile; data: HeightFieldMinMax}[];
|
|
7
|
+
/** Rebuild conservative corner bounds only for edited patches; extrema are never averaged. */
|
|
8
|
+
export function updateTerrainBounds(
|
|
9
|
+
previous: TerrainBoundsSnapshot | null,
|
|
10
|
+
resident: readonly TerrainFieldTile[],
|
|
11
|
+
options: {maxSamples?: number} = {}
|
|
12
|
+
): TerrainBoundsSnapshot {
|
|
13
|
+
return updateTerrainTileData(
|
|
14
|
+
previous,
|
|
15
|
+
resident,
|
|
16
|
+
f => new HeightFieldMinMax(f),
|
|
17
|
+
options.maxSamples
|
|
18
|
+
);
|
|
19
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
export type TerrainBox = {minX: number; minY: number; maxX: number; maxY: number};
|
|
5
|
+
const overlaps = (a: TerrainBox, b: TerrainBox) =>
|
|
6
|
+
a.minX <= b.maxX && b.minX <= a.maxX && a.minY <= b.maxY && b.minY <= a.maxY;
|
|
7
|
+
type Node = TerrainBox & {left?: Node; right?: Node; ids?: number[]};
|
|
8
|
+
/** Internal static broad phase. Exact predicates remain authoritative. */
|
|
9
|
+
export function createTerrainBoxIndex(boxes: readonly TerrainBox[]) {
|
|
10
|
+
const build = (ids: number[]): Node => {
|
|
11
|
+
const box = {minX: Infinity, minY: Infinity, maxX: -Infinity, maxY: -Infinity};
|
|
12
|
+
for (const id of ids) {
|
|
13
|
+
const b = boxes[id];
|
|
14
|
+
box.minX = Math.min(box.minX, b.minX);
|
|
15
|
+
box.minY = Math.min(box.minY, b.minY);
|
|
16
|
+
box.maxX = Math.max(box.maxX, b.maxX);
|
|
17
|
+
box.maxY = Math.max(box.maxY, b.maxY);
|
|
18
|
+
}
|
|
19
|
+
if (ids.length <= 8) return {...box, ids};
|
|
20
|
+
const axis = box.maxX - box.minX >= box.maxY - box.minY ? 'X' : 'Y';
|
|
21
|
+
ids.sort(
|
|
22
|
+
(a, b) =>
|
|
23
|
+
boxes[a][`min${axis}`] / 2 +
|
|
24
|
+
boxes[a][`max${axis}`] / 2 -
|
|
25
|
+
(boxes[b][`min${axis}`] / 2 + boxes[b][`max${axis}`] / 2) || a - b
|
|
26
|
+
);
|
|
27
|
+
const mid = ids.length >>> 1;
|
|
28
|
+
return {...box, left: build(ids.slice(0, mid)), right: build(ids.slice(mid))};
|
|
29
|
+
};
|
|
30
|
+
const root = build(boxes.map((_, i) => i));
|
|
31
|
+
return (query: TerrainBox, charge: () => void): number[] => {
|
|
32
|
+
const found: number[] = [],
|
|
33
|
+
stack = [root];
|
|
34
|
+
while (stack.length) {
|
|
35
|
+
charge();
|
|
36
|
+
const node = stack.pop()!;
|
|
37
|
+
if (!overlaps(node, query)) continue;
|
|
38
|
+
if (node.ids) {
|
|
39
|
+
for (const id of node.ids) {
|
|
40
|
+
charge();
|
|
41
|
+
if (overlaps(boxes[id], query)) found.push(id);
|
|
42
|
+
}
|
|
43
|
+
} else stack.push(node.right!, node.left!);
|
|
44
|
+
}
|
|
45
|
+
return found.sort((a, b) => a - b);
|
|
46
|
+
};
|
|
47
|
+
}
|
|
@@ -24,7 +24,11 @@ export type TerrainSimplifiedContourPaths = TerrainContourPaths & {
|
|
|
24
24
|
*/
|
|
25
25
|
export function simplifyTerrainContourPaths(
|
|
26
26
|
paths: TerrainContourPaths,
|
|
27
|
-
options: TerrainContourPathLimits & {
|
|
27
|
+
options: TerrainContourPathLimits & {
|
|
28
|
+
maxError: number;
|
|
29
|
+
maxDistanceTests?: number;
|
|
30
|
+
protectedVertexIndices?: Uint32Array;
|
|
31
|
+
}
|
|
28
32
|
): TerrainSimplifiedContourPaths {
|
|
29
33
|
const limits = contourPathLimits(options),
|
|
30
34
|
count = validateContourData(paths, limits);
|
|
@@ -87,6 +91,9 @@ export function simplifyTerrainContourPaths(
|
|
|
87
91
|
uses[vertex]++;
|
|
88
92
|
}
|
|
89
93
|
}
|
|
94
|
+
const protectedIds = new Set(options.protectedVertexIndices);
|
|
95
|
+
for (const id of protectedIds)
|
|
96
|
+
if (id >= count) throw new Error('Invalid protected contour vertex');
|
|
90
97
|
const keep = new Uint8Array(paths.indices.length);
|
|
91
98
|
let distanceTests = 0,
|
|
92
99
|
maxDeviation = 0;
|
|
@@ -98,7 +105,8 @@ export function simplifyTerrainContourPaths(
|
|
|
98
105
|
keep[start + Math.floor((end - start) / 3)] = 1;
|
|
99
106
|
keep[start + Math.floor((2 * (end - start)) / 3)] = 1;
|
|
100
107
|
}
|
|
101
|
-
for (let i = start; i <= end; i++)
|
|
108
|
+
for (let i = start; i <= end; i++)
|
|
109
|
+
if (uses[paths.indices[i]] > 1 || protectedIds.has(paths.indices[i])) keep[i] = 1;
|
|
102
110
|
const stack: number[] = [];
|
|
103
111
|
let previous = start;
|
|
104
112
|
for (let i = start + 1; i <= end; i++)
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
import {createTerrainBoxIndex} from './box-index';
|
|
5
|
+
import {simplifyTerrainContourPaths} from './contour-simplify';
|
|
6
|
+
import type {TerrainContourPaths, TerrainContourPathLimits} from './contour-path-data';
|
|
7
|
+
|
|
8
|
+
// Exact dyadic arithmetic makes orientation independent of coordinate scale.
|
|
9
|
+
const bits = new DataView(new ArrayBuffer(8));
|
|
10
|
+
function integer(x: number): bigint {
|
|
11
|
+
bits.setFloat64(0, x);
|
|
12
|
+
const high = bits.getUint32(0),
|
|
13
|
+
low = bits.getUint32(4);
|
|
14
|
+
const exponent = (high >>> 20) & 2047;
|
|
15
|
+
const mantissa = (BigInt(high & 0xfffff) << 32n) | BigInt(low);
|
|
16
|
+
const value = exponent ? ((1n << 52n) | mantissa) << BigInt(exponent - 1) : mantissa;
|
|
17
|
+
return high >>> 31 ? -value : value;
|
|
18
|
+
}
|
|
19
|
+
type Point = {x: bigint; y: bigint};
|
|
20
|
+
const orientation = (a: Point, b: Point, c: Point) =>
|
|
21
|
+
(b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
|
|
22
|
+
const sign = (v: bigint) => (v < 0n ? -1 : v > 0n ? 1 : 0);
|
|
23
|
+
const between = (a: bigint, b: bigint, p: bigint) => p >= (a < b ? a : b) && p <= (a > b ? a : b);
|
|
24
|
+
const on = (a: Point, b: Point, p: Point) =>
|
|
25
|
+
orientation(a, b, p) === 0n && between(a.x, b.x, p.x) && between(a.y, b.y, p.y);
|
|
26
|
+
function intersects(a: Point, b: Point, c: Point, d: Point): boolean {
|
|
27
|
+
return (
|
|
28
|
+
on(a, b, c) ||
|
|
29
|
+
on(a, b, d) ||
|
|
30
|
+
on(c, d, a) ||
|
|
31
|
+
on(c, d, b) ||
|
|
32
|
+
(sign(orientation(a, b, c)) * sign(orientation(a, b, d)) < 0 &&
|
|
33
|
+
sign(orientation(c, d, a)) * sign(orientation(c, d, b)) < 0)
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Conservative topology-safe RDP. Rejects invalid input embeddings and falls back
|
|
38
|
+
* to the original geometry if any proposed shortcut crosses/touches another edge
|
|
39
|
+
* or sweeps another vertex. Shared IDs and caller-protected IDs remain anchors.
|
|
40
|
+
* All paths and levels participate in XY topology; no polygon classification is inferred.
|
|
41
|
+
*/
|
|
42
|
+
export function simplifyTerrainContourPathsTopology(
|
|
43
|
+
paths: TerrainContourPaths,
|
|
44
|
+
options: TerrainContourPathLimits & {
|
|
45
|
+
maxError: number;
|
|
46
|
+
maxDistanceTests?: number;
|
|
47
|
+
maxTopologyTests?: number;
|
|
48
|
+
protectedVertexIndices?: Uint32Array;
|
|
49
|
+
rejection?: 'batch' | 'path';
|
|
50
|
+
}
|
|
51
|
+
) {
|
|
52
|
+
if (
|
|
53
|
+
options.rejection !== undefined &&
|
|
54
|
+
options.rejection !== 'batch' &&
|
|
55
|
+
options.rejection !== 'path'
|
|
56
|
+
)
|
|
57
|
+
throw new Error('Invalid contour rejection policy');
|
|
58
|
+
const candidate = simplifyTerrainContourPaths(paths, options);
|
|
59
|
+
const budget = options.maxTopologyTests ?? 1_000_000;
|
|
60
|
+
if (!Number.isSafeInteger(budget) || budget < 0)
|
|
61
|
+
throw new Error('Invalid contour topology budget');
|
|
62
|
+
let topologyTests = 0;
|
|
63
|
+
const charge = () => {
|
|
64
|
+
if (++topologyTests > budget) throw new Error('Contour topology work budget exceeded');
|
|
65
|
+
};
|
|
66
|
+
const points: Point[] = new Array(paths.positions.length / 3);
|
|
67
|
+
for (const i of new Set(paths.indices))
|
|
68
|
+
points[i] = {x: integer(paths.positions[3 * i]), y: integer(paths.positions[3 * i + 1])};
|
|
69
|
+
const edges: {a: number; b: number; p: number; i: number}[] = [];
|
|
70
|
+
for (let p = 0; p < paths.closed.length; p++)
|
|
71
|
+
for (let i = paths.offsets[p]; i + 1 < paths.offsets[p + 1]; i++)
|
|
72
|
+
edges.push({a: paths.indices[i], b: paths.indices[i + 1], p, i});
|
|
73
|
+
const box = (a: number, b: number) => ({
|
|
74
|
+
minX: Math.min(paths.positions[3 * a], paths.positions[3 * b]),
|
|
75
|
+
maxX: Math.max(paths.positions[3 * a], paths.positions[3 * b]),
|
|
76
|
+
minY: Math.min(paths.positions[3 * a + 1], paths.positions[3 * b + 1]),
|
|
77
|
+
maxY: Math.max(paths.positions[3 * a + 1], paths.positions[3 * b + 1])
|
|
78
|
+
});
|
|
79
|
+
const edgeBoxes = edges.map(e => box(e.a, e.b)),
|
|
80
|
+
edgeIndex = createTerrainBoxIndex(edgeBoxes);
|
|
81
|
+
const vertexIds = [...new Set(paths.indices)],
|
|
82
|
+
vertexIndex = createTerrainBoxIndex(vertexIds.map(v => box(v, v)));
|
|
83
|
+
for (let i = 0; i < edges.length; i++)
|
|
84
|
+
for (const j of edgeIndex(edgeBoxes[i], charge)) {
|
|
85
|
+
if (j <= i) continue;
|
|
86
|
+
charge();
|
|
87
|
+
const a = edges[i],
|
|
88
|
+
b = edges[j];
|
|
89
|
+
const common = [a.a, a.b].filter(v => v === b.a || v === b.b);
|
|
90
|
+
if (common.length === 1) {
|
|
91
|
+
const v = common[0],
|
|
92
|
+
x = a.a === v ? a.b : a.a,
|
|
93
|
+
y = b.a === v ? b.b : b.a;
|
|
94
|
+
if (on(points[v], points[x], points[y]) || on(points[v], points[y], points[x]))
|
|
95
|
+
throw new Error('Overlapping contour edges');
|
|
96
|
+
} else if (intersects(points[a.a], points[a.b], points[b.a], points[b.b]))
|
|
97
|
+
throw new Error('Contour input has crossings or unshared touches');
|
|
98
|
+
}
|
|
99
|
+
let rejected = false;
|
|
100
|
+
const rejectedPaths = new Set<number>();
|
|
101
|
+
for (let p = 0; p < paths.closed.length; p++) {
|
|
102
|
+
let start = paths.offsets[p];
|
|
103
|
+
for (let k = candidate.offsets[p] + 1; k < candidate.offsets[p + 1]; k++) {
|
|
104
|
+
const endId = candidate.sourceVertexIndices[candidate.indices[k]];
|
|
105
|
+
let end = start + 1;
|
|
106
|
+
while (paths.indices[end] !== endId) end++;
|
|
107
|
+
if (end > start + 1) {
|
|
108
|
+
const a = paths.indices[start],
|
|
109
|
+
b = paths.indices[end];
|
|
110
|
+
const pa = points[a],
|
|
111
|
+
pb = points[b];
|
|
112
|
+
const axis = pa.x !== pb.x ? 'x' : 'y';
|
|
113
|
+
const direction = sign(pb[axis] - pa[axis]);
|
|
114
|
+
let straight = true;
|
|
115
|
+
for (let i = start + 1; i <= end; i++) {
|
|
116
|
+
charge();
|
|
117
|
+
const q = points[paths.indices[i]],
|
|
118
|
+
prior = points[paths.indices[i - 1]];
|
|
119
|
+
if (!on(pa, pb, q) || sign(q[axis] - prior[axis]) !== direction) straight = false;
|
|
120
|
+
}
|
|
121
|
+
const interior = new Set(Array.from(paths.indices.subarray(start + 1, end)));
|
|
122
|
+
let unsafe = false;
|
|
123
|
+
for (const edgeId of edgeIndex(box(a, b), charge)) {
|
|
124
|
+
const edge = edges[edgeId];
|
|
125
|
+
if (edge.p === p && edge.i >= start && edge.i < end) {
|
|
126
|
+
if (straight) continue;
|
|
127
|
+
charge();
|
|
128
|
+
// The swept chain plus chord must be simple. Endpoint edges may meet
|
|
129
|
+
// the chord only at their endpoint; interior edges must not touch it.
|
|
130
|
+
const others = [edge.a, edge.b].filter(v => v !== a && v !== b);
|
|
131
|
+
if (
|
|
132
|
+
others.some(v => on(points[a], points[b], points[v])) ||
|
|
133
|
+
(others.length === 2 &&
|
|
134
|
+
intersects(points[a], points[b], points[edge.a], points[edge.b]))
|
|
135
|
+
)
|
|
136
|
+
unsafe = true;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
charge();
|
|
140
|
+
// Endpoint contacts at existing shared IDs are allowed; any extra contact is not.
|
|
141
|
+
const others = [edge.a, edge.b].filter(v => v !== a && v !== b);
|
|
142
|
+
if (
|
|
143
|
+
others.some(v => on(points[a], points[b], points[v])) ||
|
|
144
|
+
(others.length === 2 &&
|
|
145
|
+
intersects(points[a], points[b], points[edge.a], points[edge.b]))
|
|
146
|
+
)
|
|
147
|
+
unsafe = true;
|
|
148
|
+
}
|
|
149
|
+
const swept = {minX: Infinity, minY: Infinity, maxX: -Infinity, maxY: -Infinity};
|
|
150
|
+
for (let i = start; i <= end; i++) {
|
|
151
|
+
const v = paths.indices[i];
|
|
152
|
+
swept.minX = Math.min(swept.minX, paths.positions[3 * v]);
|
|
153
|
+
swept.maxX = Math.max(swept.maxX, paths.positions[3 * v]);
|
|
154
|
+
swept.minY = Math.min(swept.minY, paths.positions[3 * v + 1]);
|
|
155
|
+
swept.maxY = Math.max(swept.maxY, paths.positions[3 * v + 1]);
|
|
156
|
+
}
|
|
157
|
+
for (const vertexId of vertexIndex(swept, charge)) {
|
|
158
|
+
const vertex = vertexIds[vertexId];
|
|
159
|
+
if (vertex === a || vertex === b || interior.has(vertex)) continue;
|
|
160
|
+
let inside = false;
|
|
161
|
+
for (let i = start, j = end; i <= end; j = i++) {
|
|
162
|
+
charge();
|
|
163
|
+
const u = points[paths.indices[i]],
|
|
164
|
+
v = points[paths.indices[j]],
|
|
165
|
+
q = points[vertex];
|
|
166
|
+
if (on(u, v, q)) {
|
|
167
|
+
unsafe = true;
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
if (u.y > q.y !== v.y > q.y && orientation(u, v, q) > 0n === v.y > u.y)
|
|
171
|
+
inside = !inside;
|
|
172
|
+
}
|
|
173
|
+
if (inside) unsafe = true;
|
|
174
|
+
}
|
|
175
|
+
if (unsafe) {
|
|
176
|
+
rejected = true;
|
|
177
|
+
rejectedPaths.add(p);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
start = end;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
const outputEdges: {a: number; b: number; p: number}[] = [];
|
|
184
|
+
for (let p = 0; p < candidate.closed.length; p++)
|
|
185
|
+
for (let i = candidate.offsets[p]; i + 1 < candidate.offsets[p + 1]; i++)
|
|
186
|
+
outputEdges.push({
|
|
187
|
+
p,
|
|
188
|
+
a: candidate.sourceVertexIndices[candidate.indices[i]],
|
|
189
|
+
b: candidate.sourceVertexIndices[candidate.indices[i + 1]]
|
|
190
|
+
});
|
|
191
|
+
const outputBoxes = outputEdges.map(e => box(e.a, e.b)),
|
|
192
|
+
outputIndex = createTerrainBoxIndex(outputBoxes);
|
|
193
|
+
for (let i = 0; i < outputEdges.length; i++)
|
|
194
|
+
for (const j of outputIndex(outputBoxes[i], charge)) {
|
|
195
|
+
if (j <= i) continue;
|
|
196
|
+
charge();
|
|
197
|
+
const a = outputEdges[i],
|
|
198
|
+
b = outputEdges[j];
|
|
199
|
+
const common = [a.a, a.b].filter(v => v === b.a || v === b.b);
|
|
200
|
+
if (common.length === 1) {
|
|
201
|
+
const v = common[0],
|
|
202
|
+
x = a.a === v ? a.b : a.a,
|
|
203
|
+
y = b.a === v ? b.b : b.a;
|
|
204
|
+
if (on(points[v], points[x], points[y]) || on(points[v], points[y], points[x])) {
|
|
205
|
+
rejected = true;
|
|
206
|
+
rejectedPaths.add(a.p);
|
|
207
|
+
rejectedPaths.add(b.p);
|
|
208
|
+
}
|
|
209
|
+
} else if (intersects(points[a.a], points[a.b], points[b.a], points[b.b])) {
|
|
210
|
+
rejected = true;
|
|
211
|
+
rejectedPaths.add(a.p);
|
|
212
|
+
rejectedPaths.add(b.p);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (rejected && options.rejection === 'path') {
|
|
216
|
+
const indices: number[] = [],
|
|
217
|
+
offsets = [0];
|
|
218
|
+
for (let p = 0; p < paths.closed.length; p++) {
|
|
219
|
+
if (rejectedPaths.has(p))
|
|
220
|
+
for (let i = paths.offsets[p]; i < paths.offsets[p + 1]; i++)
|
|
221
|
+
indices.push(paths.indices[i]);
|
|
222
|
+
else
|
|
223
|
+
for (let i = candidate.offsets[p]; i < candidate.offsets[p + 1]; i++)
|
|
224
|
+
indices.push(candidate.sourceVertexIndices[candidate.indices[i]]);
|
|
225
|
+
offsets.push(indices.length);
|
|
226
|
+
}
|
|
227
|
+
return {
|
|
228
|
+
...paths,
|
|
229
|
+
positions: paths.positions.slice(),
|
|
230
|
+
gridCoordinates: paths.gridCoordinates.slice(),
|
|
231
|
+
indices: Uint32Array.from(indices),
|
|
232
|
+
offsets: Uint32Array.from(offsets),
|
|
233
|
+
closed: paths.closed.slice(),
|
|
234
|
+
levelIndices: paths.levelIndices.slice(),
|
|
235
|
+
levels: paths.levels.slice(),
|
|
236
|
+
origin: Object.freeze([...paths.origin] as [number, number, number]),
|
|
237
|
+
sourceVertexIndices: Uint32Array.from({length: points.length}, (_, i) => i),
|
|
238
|
+
maxDeviation: candidate.maxDeviation,
|
|
239
|
+
requestedMaxError: options.maxError,
|
|
240
|
+
distanceTests: candidate.distanceTests,
|
|
241
|
+
topologyTests,
|
|
242
|
+
rejected: true
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
if (!rejected) return {...candidate, topologyTests, rejected: false};
|
|
246
|
+
return {
|
|
247
|
+
...paths,
|
|
248
|
+
positions: paths.positions.slice(),
|
|
249
|
+
gridCoordinates: paths.gridCoordinates.slice(),
|
|
250
|
+
indices: paths.indices.slice(),
|
|
251
|
+
offsets: paths.offsets.slice(),
|
|
252
|
+
levelIndices: paths.levelIndices.slice(),
|
|
253
|
+
closed: paths.closed.slice(),
|
|
254
|
+
levels: paths.levels.slice(),
|
|
255
|
+
origin: Object.freeze([...paths.origin] as [number, number, number]),
|
|
256
|
+
sourceVertexIndices: Uint32Array.from({length: points.length}, (_, i) => i),
|
|
257
|
+
maxDeviation: 0,
|
|
258
|
+
requestedMaxError: options.maxError,
|
|
259
|
+
distanceTests: candidate.distanceTests,
|
|
260
|
+
topologyTests,
|
|
261
|
+
rejected: true
|
|
262
|
+
};
|
|
263
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
import {createDelatinTerrainMesh} from './delatin';
|
|
5
|
+
import {updateMeshRegions, type TerrainMeshRegions} from './mesh-regions-data';
|
|
6
|
+
import type {HeightField} from './height-field';
|
|
7
|
+
import type {AdaptiveTerrainMesh} from './adaptive-mesh-types';
|
|
8
|
+
import type {TerrainDirtyWindow} from './dirty-regions';
|
|
9
|
+
export type TerrainDelatinMeshRegions = TerrainMeshRegions<AdaptiveTerrainMesh | null>;
|
|
10
|
+
/** Missing regions return null. Independent adaptive boundaries require explicit
|
|
11
|
+
* stitchTerrainMeshEdges before treating the regions as a conforming surface. */
|
|
12
|
+
export function updateTerrainDelatinMeshRegions(
|
|
13
|
+
previous: TerrainDelatinMeshRegions | null,
|
|
14
|
+
field: HeightField,
|
|
15
|
+
dirty: readonly TerrainDirtyWindow[],
|
|
16
|
+
options: {maxError: number; cellsPerRegion?: number; maxSamples?: number; maxTests?: number}
|
|
17
|
+
): TerrainDelatinMeshRegions {
|
|
18
|
+
if (!Number.isFinite(options.maxError) || options.maxError < 0)
|
|
19
|
+
throw new Error('Invalid Delatin region error');
|
|
20
|
+
return updateMeshRegions(previous, field, dirty, options, `delatin:${options.maxError}`, f =>
|
|
21
|
+
f.validCount !== f.width * f.height
|
|
22
|
+
? null
|
|
23
|
+
: createDelatinTerrainMesh(f, {maxError: options.maxError})
|
|
24
|
+
);
|
|
25
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
import type {HeightField} from './height-field';
|
|
5
|
+
export type TerrainDirtyWindow = {
|
|
6
|
+
minColumn: number;
|
|
7
|
+
minRow: number;
|
|
8
|
+
maxColumn: number;
|
|
9
|
+
maxRow: number;
|
|
10
|
+
};
|
|
11
|
+
export function validateTerrainDirtyWindows(
|
|
12
|
+
windows: readonly TerrainDirtyWindow[],
|
|
13
|
+
width: number,
|
|
14
|
+
height: number,
|
|
15
|
+
maxWindows = 100_000
|
|
16
|
+
): void {
|
|
17
|
+
if (windows.length > maxWindows) throw new Error('Terrain dirty window capacity exceeded');
|
|
18
|
+
for (const w of windows)
|
|
19
|
+
if (
|
|
20
|
+
![w.minColumn, w.minRow, w.maxColumn, w.maxRow].every(Number.isSafeInteger) ||
|
|
21
|
+
w.minColumn < 0 ||
|
|
22
|
+
w.minRow < 0 ||
|
|
23
|
+
w.maxColumn >= width ||
|
|
24
|
+
w.maxRow >= height ||
|
|
25
|
+
w.minColumn > w.maxColumn ||
|
|
26
|
+
w.minRow > w.maxRow
|
|
27
|
+
)
|
|
28
|
+
throw new Error('Invalid terrain dirty sample window');
|
|
29
|
+
}
|
|
30
|
+
export function sameTerrainFrame(a: HeightField, b: HeightField): boolean {
|
|
31
|
+
return (
|
|
32
|
+
a.width === b.width &&
|
|
33
|
+
a.height === b.height &&
|
|
34
|
+
a.registration === b.registration &&
|
|
35
|
+
a.diagonal === b.diagonal &&
|
|
36
|
+
a.heightReference === b.heightReference &&
|
|
37
|
+
a.origin.every((v, i) => v === b.origin[i]) &&
|
|
38
|
+
a.columnStep.every((v, i) => v === b.columnStep[i]) &&
|
|
39
|
+
a.rowStep.every((v, i) => v === b.rowStep[i])
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
export function terrainDirtyIndices(
|
|
43
|
+
windows: readonly TerrainDirtyWindow[],
|
|
44
|
+
width: number,
|
|
45
|
+
height: number,
|
|
46
|
+
expand: number,
|
|
47
|
+
charge: () => void
|
|
48
|
+
): Set<number> {
|
|
49
|
+
const ids = new Set<number>();
|
|
50
|
+
for (const w of windows)
|
|
51
|
+
for (let y = Math.max(0, w.minRow - expand); y <= Math.min(height - 1, w.maxRow + expand); y++)
|
|
52
|
+
for (
|
|
53
|
+
let x = Math.max(0, w.minColumn - expand);
|
|
54
|
+
x <= Math.min(width - 1, w.maxColumn + expand);
|
|
55
|
+
x++
|
|
56
|
+
) {
|
|
57
|
+
charge();
|
|
58
|
+
ids.add(y * width + x);
|
|
59
|
+
}
|
|
60
|
+
return ids;
|
|
61
|
+
}
|