@math.gl/geospatial 5.0.0-beta.2 → 5.0.0-beta.4
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 +618 -0
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -1
- package/dist/tasks/definitions.d.ts +90 -0
- package/dist/tasks/definitions.d.ts.map +1 -0
- package/dist/tasks/definitions.js +54 -0
- package/dist/tasks/definitions.js.map +1 -0
- package/dist/{workers → tasks}/index.cjs +158 -122
- package/dist/tasks/index.cjs.map +6 -0
- package/dist/tasks/index.d.ts +12 -0
- package/dist/tasks/index.d.ts.map +1 -0
- package/dist/tasks/index.js +30 -0
- package/dist/tasks/index.js.map +1 -0
- package/dist/{workers → tasks}/map-worker.d.ts.map +1 -1
- package/dist/tasks/map-worker.js +7 -0
- package/dist/tasks/map-worker.js.map +1 -0
- package/dist/{workers → tasks}/operations.d.ts +6 -4
- package/dist/tasks/operations.d.ts.map +1 -0
- package/dist/{workers → tasks}/operations.js +3 -5
- package/dist/tasks/operations.js.map +1 -0
- package/dist/tasks/projection-worker.d.ts.map +1 -0
- package/dist/tasks/projection-worker.js +7 -0
- package/dist/tasks/projection-worker.js.map +1 -0
- package/dist/terrain/contour-boundary-diagnostics.d.ts +23 -0
- package/dist/terrain/contour-boundary-diagnostics.d.ts.map +1 -0
- package/dist/terrain/contour-boundary-diagnostics.js +62 -0
- package/dist/terrain/contour-boundary-diagnostics.js.map +1 -0
- package/dist/terrain/contour-tile-paths.d.ts +7 -0
- package/dist/terrain/contour-tile-paths.d.ts.map +1 -0
- package/dist/terrain/contour-tile-paths.js +12 -0
- package/dist/terrain/contour-tile-paths.js.map +1 -0
- package/dist/terrain/contour-tile-weld.d.ts +24 -0
- package/dist/terrain/contour-tile-weld.d.ts.map +1 -0
- package/dist/terrain/contour-tile-weld.js +147 -0
- package/dist/terrain/contour-tile-weld.js.map +1 -0
- package/dist/terrain/filtered-pyramid.d.ts +22 -0
- package/dist/terrain/filtered-pyramid.d.ts.map +1 -0
- package/dist/terrain/filtered-pyramid.js +71 -0
- package/dist/terrain/filtered-pyramid.js.map +1 -0
- package/dist/terrain/gradient-pyramid.d.ts +29 -0
- package/dist/terrain/gradient-pyramid.d.ts.map +1 -0
- package/dist/terrain/gradient-pyramid.js +94 -0
- package/dist/terrain/gradient-pyramid.js.map +1 -0
- package/dist/terrain/index.cjs +619 -1
- package/dist/terrain/index.cjs.map +2 -2
- package/dist/terrain/index.d.ts +19 -0
- package/dist/terrain/index.d.ts.map +1 -1
- package/dist/terrain/index.js +11 -0
- package/dist/terrain/index.js.map +1 -1
- package/dist/terrain/profile-horizon.d.ts +20 -0
- package/dist/terrain/profile-horizon.d.ts.map +1 -0
- package/dist/terrain/profile-horizon.js +69 -0
- package/dist/terrain/profile-horizon.js.map +1 -0
- package/dist/terrain/profile-slope.d.ts +3 -0
- package/dist/terrain/profile-slope.d.ts.map +1 -0
- package/dist/terrain/profile-slope.js +35 -0
- package/dist/terrain/profile-slope.js.map +1 -0
- package/dist/terrain/profile-viewshed.d.ts +17 -0
- package/dist/terrain/profile-viewshed.d.ts.map +1 -0
- package/dist/terrain/profile-viewshed.js +51 -0
- package/dist/terrain/profile-viewshed.js.map +1 -0
- package/dist/terrain/profile-visibility.d.ts +21 -0
- package/dist/terrain/profile-visibility.d.ts.map +1 -0
- package/dist/terrain/profile-visibility.js +61 -0
- package/dist/terrain/profile-visibility.js.map +1 -0
- package/dist/terrain/vertex-halo-invalidation.d.ts +16 -0
- package/dist/terrain/vertex-halo-invalidation.d.ts.map +1 -0
- package/dist/terrain/vertex-halo-invalidation.js +50 -0
- package/dist/terrain/vertex-halo-invalidation.js.map +1 -0
- package/dist/terrain/vertex-halo-update.d.ts +18 -0
- package/dist/terrain/vertex-halo-update.d.ts.map +1 -0
- package/dist/terrain/vertex-halo-update.js +76 -0
- package/dist/terrain/vertex-halo-update.js.map +1 -0
- package/dist/terrain/vertex-halos.d.ts +26 -0
- package/dist/terrain/vertex-halos.d.ts.map +1 -0
- package/dist/terrain/vertex-halos.js +80 -0
- package/dist/terrain/vertex-halos.js.map +1 -0
- package/package.json +10 -15
- package/src/index.ts +22 -0
- package/src/tasks/definitions.ts +89 -0
- package/src/tasks/index.ts +39 -0
- package/src/tasks/map-worker.ts +6 -0
- package/src/{workers → tasks}/operations.ts +11 -7
- package/src/tasks/projection-worker.ts +6 -0
- package/src/terrain/contour-boundary-diagnostics.ts +90 -0
- package/src/terrain/contour-tile-paths.ts +20 -0
- package/src/terrain/contour-tile-weld.ts +182 -0
- package/src/terrain/filtered-pyramid.ts +95 -0
- package/src/terrain/gradient-pyramid.ts +129 -0
- package/src/terrain/index.ts +22 -0
- package/src/terrain/profile-horizon.ts +86 -0
- package/src/terrain/profile-slope.ts +32 -0
- package/src/terrain/profile-viewshed.ts +57 -0
- package/src/terrain/profile-visibility.ts +81 -0
- package/src/terrain/vertex-halo-invalidation.ts +57 -0
- package/src/terrain/vertex-halo-update.ts +91 -0
- package/src/terrain/vertex-halos.ts +101 -0
- package/dist/workers/index.cjs.map +0 -6
- package/dist/workers/index.d.ts +0 -12
- package/dist/workers/index.d.ts.map +0 -1
- package/dist/workers/index.js +0 -41
- package/dist/workers/index.js.map +0 -1
- package/dist/workers/map-worker.js +0 -7
- package/dist/workers/map-worker.js.map +0 -1
- package/dist/workers/operations.cjs +0 -21008
- package/dist/workers/operations.cjs.map +0 -6
- package/dist/workers/operations.d.ts.map +0 -1
- package/dist/workers/operations.js.map +0 -1
- package/dist/workers/projection-worker.d.ts.map +0 -1
- package/dist/workers/projection-worker.js +0 -7
- package/dist/workers/projection-worker.js.map +0 -1
- package/src/workers/index.ts +0 -59
- package/src/workers/map-worker.ts +0 -7
- package/src/workers/projection-worker.ts +0 -7
- /package/dist/{workers → tasks}/map-worker.d.ts +0 -0
- /package/dist/{workers → tasks}/projection-worker.d.ts +0 -0
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {TerrainContours} from './contours';
|
|
6
|
+
import {
|
|
7
|
+
contourPathLimits,
|
|
8
|
+
validateContourData,
|
|
9
|
+
validateContourVertex,
|
|
10
|
+
type TerrainContourPathLimits
|
|
11
|
+
} from './contour-path-data';
|
|
12
|
+
|
|
13
|
+
export type TerrainContourTile = {column: number; row: number; contours: TerrainContours};
|
|
14
|
+
export type TerrainContourTileWeldOptions = TerrainContourPathLimits & {
|
|
15
|
+
/** Number of vertices in each vertex-registered tile. */
|
|
16
|
+
width: number;
|
|
17
|
+
height: number;
|
|
18
|
+
/** Maximum world-frame XY disagreement for a matching lattice point. Default zero. */
|
|
19
|
+
maxPositionDifference?: number;
|
|
20
|
+
maxTiles?: number;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** Weld coincident contour vertices on same-level vertex-tile boundaries.
|
|
24
|
+
* Tiles share an integer lattice and identical contour levels and height reference.
|
|
25
|
+
* Boundary identity uses exact global grid coordinates, not a spatial tolerance.
|
|
26
|
+
* Missing tiles or missing source triangles remain open; no segments are invented.
|
|
27
|
+
* The output grid coordinates are global lattice coordinates, and its origin is
|
|
28
|
+
* taken from the lexicographically first tile. Equivalent inputs are order independent.
|
|
29
|
+
*/
|
|
30
|
+
export function weldTerrainContourTiles(
|
|
31
|
+
tiles: readonly TerrainContourTile[],
|
|
32
|
+
options: TerrainContourTileWeldOptions
|
|
33
|
+
): TerrainContours {
|
|
34
|
+
const {width, height} = options;
|
|
35
|
+
const limits = contourPathLimits(options);
|
|
36
|
+
const tolerance = options.maxPositionDifference ?? 0;
|
|
37
|
+
const maxTiles = options.maxTiles ?? 4096;
|
|
38
|
+
if (
|
|
39
|
+
!Number.isSafeInteger(width) ||
|
|
40
|
+
width < 2 ||
|
|
41
|
+
!Number.isSafeInteger(height) ||
|
|
42
|
+
height < 2 ||
|
|
43
|
+
!Number.isFinite(tolerance) ||
|
|
44
|
+
tolerance < 0 ||
|
|
45
|
+
!Number.isSafeInteger(maxTiles) ||
|
|
46
|
+
maxTiles < 1 ||
|
|
47
|
+
tiles.length > maxTiles ||
|
|
48
|
+
!tiles.length
|
|
49
|
+
)
|
|
50
|
+
throw new Error('Invalid terrain contour tile dimensions or tolerance');
|
|
51
|
+
const sorted = [...tiles].sort((a, b) => a.column - b.column || a.row - b.row);
|
|
52
|
+
const first = sorted[0].contours;
|
|
53
|
+
const origin: [number, number, number] = [...first.origin];
|
|
54
|
+
const levels = first.levels.slice();
|
|
55
|
+
const positions: number[] = [],
|
|
56
|
+
grid: number[] = [],
|
|
57
|
+
segments: number[] = [],
|
|
58
|
+
levelIndices: number[] = [];
|
|
59
|
+
const vertices = new Map<string, {id: number; tile: number}>();
|
|
60
|
+
const edges = new Map<string, number>();
|
|
61
|
+
const addresses = new Set<string>();
|
|
62
|
+
let inputVertices = 0;
|
|
63
|
+
let inputSegments = 0;
|
|
64
|
+
for (let tileIndex = 0; tileIndex < sorted.length; tileIndex++) {
|
|
65
|
+
const tile = sorted[tileIndex],
|
|
66
|
+
data = tile.contours;
|
|
67
|
+
const id = `${tile.column},${tile.row}`;
|
|
68
|
+
if (!Number.isSafeInteger(tile.column) || !Number.isSafeInteger(tile.row) || addresses.has(id))
|
|
69
|
+
throw new Error('Invalid or duplicate terrain contour tile address');
|
|
70
|
+
addresses.add(id);
|
|
71
|
+
const x0 = tile.column * (width - 1),
|
|
72
|
+
y0 = tile.row * (height - 1);
|
|
73
|
+
if (![x0, y0, x0 + width - 1, y0 + height - 1].every(Number.isSafeInteger))
|
|
74
|
+
throw new Error('Terrain contour tile lattice overflow');
|
|
75
|
+
const count = validateContourData(data, limits);
|
|
76
|
+
inputVertices += count;
|
|
77
|
+
if (inputVertices > limits.maxVertices)
|
|
78
|
+
throw new Error('Terrain contour input vertex capacity exceeded');
|
|
79
|
+
if (
|
|
80
|
+
data.heightReference !== first.heightReference ||
|
|
81
|
+
data.levels.length !== levels.length ||
|
|
82
|
+
!data.levels.every((level, i) => level === levels[i])
|
|
83
|
+
)
|
|
84
|
+
throw new Error('Terrain contour tile levels or height reference differ');
|
|
85
|
+
if (
|
|
86
|
+
!(data.segments instanceof Uint32Array) ||
|
|
87
|
+
data.segments.length % 2 ||
|
|
88
|
+
!(data.levelIndices instanceof Uint32Array) ||
|
|
89
|
+
data.levelIndices.length !== data.segments.length / 2 ||
|
|
90
|
+
data.levelIndices.length > limits.maxSegments
|
|
91
|
+
)
|
|
92
|
+
throw new Error('Invalid terrain contour tile segments');
|
|
93
|
+
inputSegments += data.levelIndices.length;
|
|
94
|
+
if (inputSegments > limits.maxSegments)
|
|
95
|
+
throw new Error('Terrain contour input segment capacity exceeded');
|
|
96
|
+
const assigned = new Uint32Array(count).fill(0xffffffff);
|
|
97
|
+
const remap = new Uint32Array(count).fill(0xffffffff);
|
|
98
|
+
const localBoundary = new Set<string>();
|
|
99
|
+
const mapVertex = (source: number, level: number): number => {
|
|
100
|
+
validateContourVertex(data, source, level, assigned);
|
|
101
|
+
if (remap[source] !== 0xffffffff) return remap[source];
|
|
102
|
+
const x = data.gridCoordinates[2 * source],
|
|
103
|
+
y = data.gridCoordinates[2 * source + 1];
|
|
104
|
+
if (x < 0 || x > width - 1 || y < 0 || y > height - 1)
|
|
105
|
+
throw new Error('Terrain contour vertex outside tile lattice');
|
|
106
|
+
const gx = x0 + x,
|
|
107
|
+
gy = y0 + y;
|
|
108
|
+
if (
|
|
109
|
+
!Number.isFinite(gx) ||
|
|
110
|
+
!Number.isFinite(gy) ||
|
|
111
|
+
(x % 1 !== 0 && Number.isInteger(gx)) ||
|
|
112
|
+
(y % 1 !== 0 && Number.isInteger(gy))
|
|
113
|
+
)
|
|
114
|
+
throw new Error('Terrain contour tile lattice loses precision');
|
|
115
|
+
const px = data.positions[3 * source] + (data.origin[0] - origin[0]);
|
|
116
|
+
const py = data.positions[3 * source + 1] + (data.origin[1] - origin[1]);
|
|
117
|
+
if (!Number.isFinite(px) || !Number.isFinite(py))
|
|
118
|
+
throw new Error('Terrain contour frame overflow');
|
|
119
|
+
const boundary = x === 0 || y === 0 || x === width - 1 || y === height - 1;
|
|
120
|
+
// Keep the integer lattice coordinate and local fractional remainder separate.
|
|
121
|
+
// Adding a small fraction to a large tile offset can round distinct points
|
|
122
|
+
// to the same binary64 global coordinate and must not weld them together.
|
|
123
|
+
const xi = Math.floor(x),
|
|
124
|
+
yi = Math.floor(y);
|
|
125
|
+
const vertexKey = `${level}/${x0 + xi}/${x - xi}/${y0 + yi}/${y - yi}`;
|
|
126
|
+
if (boundary) {
|
|
127
|
+
if (localBoundary.has(vertexKey))
|
|
128
|
+
throw new Error('Duplicate terrain contour boundary vertex');
|
|
129
|
+
localBoundary.add(vertexKey);
|
|
130
|
+
}
|
|
131
|
+
const found = boundary ? vertices.get(vertexKey) : undefined;
|
|
132
|
+
if (found) {
|
|
133
|
+
if (found.tile === tileIndex) throw new Error('Duplicate terrain contour boundary vertex');
|
|
134
|
+
const delta = Math.hypot(px - positions[3 * found.id], py - positions[3 * found.id + 1]);
|
|
135
|
+
if (!Number.isFinite(delta) || delta > tolerance)
|
|
136
|
+
throw new Error('Terrain contour boundary positions disagree');
|
|
137
|
+
remap[source] = found.id;
|
|
138
|
+
return found.id;
|
|
139
|
+
}
|
|
140
|
+
if (positions.length / 3 >= limits.maxVertices)
|
|
141
|
+
throw new Error('Terrain contour vertex capacity exceeded');
|
|
142
|
+
const output = positions.length / 3;
|
|
143
|
+
const pz = levels[level] - origin[2];
|
|
144
|
+
if (!Number.isFinite(pz)) throw new Error('Terrain contour frame overflow');
|
|
145
|
+
positions.push(px, py, pz);
|
|
146
|
+
grid.push(gx, gy);
|
|
147
|
+
if (boundary) vertices.set(vertexKey, {id: output, tile: tileIndex});
|
|
148
|
+
remap[source] = output;
|
|
149
|
+
return output;
|
|
150
|
+
};
|
|
151
|
+
for (let i = 0; i < data.levelIndices.length; i++) {
|
|
152
|
+
const level = data.levelIndices[i],
|
|
153
|
+
a = mapVertex(data.segments[2 * i], level),
|
|
154
|
+
b = mapVertex(data.segments[2 * i + 1], level);
|
|
155
|
+
if (
|
|
156
|
+
a === b ||
|
|
157
|
+
(positions[3 * a] === positions[3 * b] && positions[3 * a + 1] === positions[3 * b + 1])
|
|
158
|
+
)
|
|
159
|
+
throw new Error('Degenerate terrain contour segment after rebasing');
|
|
160
|
+
const edge = a < b ? `${a}/${b}` : `${b}/${a}`;
|
|
161
|
+
const previous = edges.get(edge);
|
|
162
|
+
if (previous !== undefined) {
|
|
163
|
+
if (previous === tileIndex) throw new Error('Duplicate terrain contour segment');
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
if (levelIndices.length >= limits.maxSegments)
|
|
167
|
+
throw new Error('Terrain contour segment capacity exceeded');
|
|
168
|
+
edges.set(edge, tileIndex);
|
|
169
|
+
segments.push(a, b);
|
|
170
|
+
levelIndices.push(level);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
positions: Float64Array.from(positions),
|
|
175
|
+
gridCoordinates: Float64Array.from(grid),
|
|
176
|
+
segments: Uint32Array.from(segments),
|
|
177
|
+
levelIndices: Uint32Array.from(levelIndices),
|
|
178
|
+
levels,
|
|
179
|
+
origin,
|
|
180
|
+
heightReference: first.heightReference
|
|
181
|
+
};
|
|
182
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
export type TerrainFilteredLevel = {
|
|
6
|
+
width: number;
|
|
7
|
+
height: number;
|
|
8
|
+
/** Mean of valid original samples, or zero when validCounts is zero. */
|
|
9
|
+
heights: Float64Array;
|
|
10
|
+
validCounts: Uint32Array;
|
|
11
|
+
/** Original sample count represented, including missing samples. */
|
|
12
|
+
sampleCounts: Uint32Array;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/** Box-filter a cell raster into a weighted pyramid. Odd edges retain their true
|
|
16
|
+
* source counts. Missing data is excluded from means but remains visible in counts.
|
|
17
|
+
* These averages are not conservative collision bounds or vertex-registered meshes.
|
|
18
|
+
*/
|
|
19
|
+
export function createTerrainFilteredPyramid(
|
|
20
|
+
input: {
|
|
21
|
+
width: number;
|
|
22
|
+
height: number;
|
|
23
|
+
heights: Float32Array | Float64Array;
|
|
24
|
+
validity?: Uint8Array;
|
|
25
|
+
},
|
|
26
|
+
options: {maxSamples?: number} = {}
|
|
27
|
+
): TerrainFilteredLevel[] {
|
|
28
|
+
const {width, height, heights, validity} = input;
|
|
29
|
+
const count = width * height;
|
|
30
|
+
const limit = options.maxSamples ?? 4_000_000;
|
|
31
|
+
if (
|
|
32
|
+
!Number.isSafeInteger(width) ||
|
|
33
|
+
width < 1 ||
|
|
34
|
+
!Number.isSafeInteger(height) ||
|
|
35
|
+
height < 1 ||
|
|
36
|
+
!Number.isSafeInteger(count) ||
|
|
37
|
+
count > 0xffffffff ||
|
|
38
|
+
!Number.isSafeInteger(limit) ||
|
|
39
|
+
limit < count ||
|
|
40
|
+
!(heights instanceof Float32Array || heights instanceof Float64Array) ||
|
|
41
|
+
heights.length !== count ||
|
|
42
|
+
(validity !== undefined && (!(validity instanceof Uint8Array) || validity.length !== count))
|
|
43
|
+
)
|
|
44
|
+
throw new Error('Invalid terrain filtered pyramid input or capacity');
|
|
45
|
+
const base: TerrainFilteredLevel = {
|
|
46
|
+
width,
|
|
47
|
+
height,
|
|
48
|
+
heights: new Float64Array(count),
|
|
49
|
+
validCounts: new Uint32Array(count),
|
|
50
|
+
sampleCounts: new Uint32Array(count).fill(1)
|
|
51
|
+
};
|
|
52
|
+
for (let i = 0; i < count; i++)
|
|
53
|
+
if ((!validity || validity[i] !== 0) && Number.isFinite(heights[i])) {
|
|
54
|
+
base.heights[i] = heights[i];
|
|
55
|
+
base.validCounts[i] = 1;
|
|
56
|
+
}
|
|
57
|
+
const levels = [base];
|
|
58
|
+
let source = base;
|
|
59
|
+
while (source.width > 1 || source.height > 1) {
|
|
60
|
+
const w = Math.ceil(source.width / 2),
|
|
61
|
+
h = Math.ceil(source.height / 2);
|
|
62
|
+
const next: TerrainFilteredLevel = {
|
|
63
|
+
width: w,
|
|
64
|
+
height: h,
|
|
65
|
+
heights: new Float64Array(w * h),
|
|
66
|
+
validCounts: new Uint32Array(w * h),
|
|
67
|
+
sampleCounts: new Uint32Array(w * h)
|
|
68
|
+
};
|
|
69
|
+
for (let y = 0; y < h; y++)
|
|
70
|
+
for (let x = 0; x < w; x++) {
|
|
71
|
+
const out = y * w + x;
|
|
72
|
+
for (let dy = 0; dy < 2; dy++)
|
|
73
|
+
for (let dx = 0; dx < 2; dx++) {
|
|
74
|
+
const sx = x * 2 + dx,
|
|
75
|
+
sy = y * 2 + dy;
|
|
76
|
+
if (sx >= source.width || sy >= source.height) continue;
|
|
77
|
+
const index = sy * source.width + sx;
|
|
78
|
+
const n = source.validCounts[index],
|
|
79
|
+
old = next.validCounts[out];
|
|
80
|
+
next.sampleCounts[out] += source.sampleCounts[index];
|
|
81
|
+
if (n) {
|
|
82
|
+
const total = old + n,
|
|
83
|
+
a = next.heights[out],
|
|
84
|
+
b = source.heights[index];
|
|
85
|
+
// Convex combination avoids overflow when values have opposite signs.
|
|
86
|
+
next.heights[out] = old ? a * (old / total) + b * (n / total) : b;
|
|
87
|
+
next.validCounts[out] = total;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
levels.push(next);
|
|
92
|
+
source = next;
|
|
93
|
+
}
|
|
94
|
+
return levels;
|
|
95
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {TerrainGradientGrid, TerrainGradientMethod} from './derivatives';
|
|
6
|
+
import {TerrainSampleStatus} from './types';
|
|
7
|
+
|
|
8
|
+
export type TerrainGradientPyramidLevel = {
|
|
9
|
+
width: number;
|
|
10
|
+
height: number;
|
|
11
|
+
/** Maximum side length, in source samples, of each block. Outer blocks may be smaller. */
|
|
12
|
+
blockSize: number;
|
|
13
|
+
/** Packed mean world-frame dz/dx and dz/dy; zero where validCounts is zero. */
|
|
14
|
+
gradients: Float64Array;
|
|
15
|
+
validCounts: Uint32Array;
|
|
16
|
+
sampleCounts: Uint32Array;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type TerrainGradientPyramid = {
|
|
20
|
+
levels: TerrainGradientPyramidLevel[];
|
|
21
|
+
method: TerrainGradientMethod;
|
|
22
|
+
origin: readonly [number, number];
|
|
23
|
+
columnStep: readonly [number, number];
|
|
24
|
+
rowStep: readonly [number, number];
|
|
25
|
+
registration: TerrainGradientGrid['registration'];
|
|
26
|
+
heightReference: string;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** Average valid world-frame gradient vectors over aligned source-sample blocks.
|
|
30
|
+
* Counts include missing samples and preserve exact source weights at odd edges.
|
|
31
|
+
* Slope of a mean gradient is not mean slope, nor the gradient of filtered heights.
|
|
32
|
+
* A level's blocks refer to the source grid; partial edge blocks have smaller extents.
|
|
33
|
+
*/
|
|
34
|
+
export function createTerrainGradientPyramid(
|
|
35
|
+
grid: TerrainGradientGrid,
|
|
36
|
+
options: {maxSamples?: number} = {}
|
|
37
|
+
): TerrainGradientPyramid {
|
|
38
|
+
const {width, height, gradients, statuses} = grid;
|
|
39
|
+
const count = width * height,
|
|
40
|
+
limit = options.maxSamples ?? 4_000_000;
|
|
41
|
+
if (
|
|
42
|
+
!Number.isSafeInteger(width) ||
|
|
43
|
+
width < 1 ||
|
|
44
|
+
!Number.isSafeInteger(height) ||
|
|
45
|
+
height < 1 ||
|
|
46
|
+
!Number.isSafeInteger(count) ||
|
|
47
|
+
count > 0xffffffff ||
|
|
48
|
+
!Number.isSafeInteger(limit) ||
|
|
49
|
+
limit < count ||
|
|
50
|
+
!(gradients instanceof Float64Array) ||
|
|
51
|
+
gradients.length !== 2 * count ||
|
|
52
|
+
!(statuses instanceof Uint8Array) ||
|
|
53
|
+
statuses.length !== count ||
|
|
54
|
+
(grid.method !== 'horn' && grid.method !== 'zevenbergen-thorne') ||
|
|
55
|
+
(grid.registration !== 'cell' && grid.registration !== 'vertex') ||
|
|
56
|
+
typeof grid.heightReference !== 'string' ||
|
|
57
|
+
![grid.origin, grid.columnStep, grid.rowStep].every(
|
|
58
|
+
v => v.length === 2 && v.every(Number.isFinite)
|
|
59
|
+
)
|
|
60
|
+
)
|
|
61
|
+
throw new Error('Invalid terrain gradient pyramid input or capacity');
|
|
62
|
+
const base: TerrainGradientPyramidLevel = {
|
|
63
|
+
width,
|
|
64
|
+
height,
|
|
65
|
+
blockSize: 1,
|
|
66
|
+
gradients: new Float64Array(2 * count),
|
|
67
|
+
validCounts: new Uint32Array(count),
|
|
68
|
+
sampleCounts: new Uint32Array(count).fill(1)
|
|
69
|
+
};
|
|
70
|
+
for (let i = 0; i < count; i++) {
|
|
71
|
+
if (statuses[i] > TerrainSampleStatus.NOT_RESIDENT)
|
|
72
|
+
throw new Error('Invalid terrain gradient status');
|
|
73
|
+
if (statuses[i] === TerrainSampleStatus.VALID) {
|
|
74
|
+
if (!Number.isFinite(gradients[2 * i]) || !Number.isFinite(gradients[2 * i + 1]))
|
|
75
|
+
throw new Error('Invalid terrain gradient value');
|
|
76
|
+
base.gradients[2 * i] = gradients[2 * i];
|
|
77
|
+
base.gradients[2 * i + 1] = gradients[2 * i + 1];
|
|
78
|
+
base.validCounts[i] = 1;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const levels = [base];
|
|
82
|
+
let source = base;
|
|
83
|
+
while (source.width > 1 || source.height > 1) {
|
|
84
|
+
const w = Math.ceil(source.width / 2),
|
|
85
|
+
h = Math.ceil(source.height / 2);
|
|
86
|
+
const next: TerrainGradientPyramidLevel = {
|
|
87
|
+
width: w,
|
|
88
|
+
height: h,
|
|
89
|
+
blockSize: source.blockSize * 2,
|
|
90
|
+
gradients: new Float64Array(2 * w * h),
|
|
91
|
+
validCounts: new Uint32Array(w * h),
|
|
92
|
+
sampleCounts: new Uint32Array(w * h)
|
|
93
|
+
};
|
|
94
|
+
for (let y = 0; y < h; y++)
|
|
95
|
+
for (let x = 0; x < w; x++) {
|
|
96
|
+
const out = y * w + x;
|
|
97
|
+
for (let dy = 0; dy < 2; dy++)
|
|
98
|
+
for (let dx = 0; dx < 2; dx++) {
|
|
99
|
+
const sx = 2 * x + dx,
|
|
100
|
+
sy = 2 * y + dy;
|
|
101
|
+
if (sx >= source.width || sy >= source.height) continue;
|
|
102
|
+
const index = sy * source.width + sx,
|
|
103
|
+
n = source.validCounts[index],
|
|
104
|
+
old = next.validCounts[out];
|
|
105
|
+
next.sampleCounts[out] += source.sampleCounts[index];
|
|
106
|
+
if (n) {
|
|
107
|
+
const total = old + n;
|
|
108
|
+
for (let axis = 0; axis < 2; axis++) {
|
|
109
|
+
const a = next.gradients[2 * out + axis],
|
|
110
|
+
b = source.gradients[2 * index + axis];
|
|
111
|
+
next.gradients[2 * out + axis] = old ? a * (old / total) + b * (n / total) : b;
|
|
112
|
+
}
|
|
113
|
+
next.validCounts[out] = total;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
levels.push(next);
|
|
118
|
+
source = next;
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
levels,
|
|
122
|
+
method: grid.method,
|
|
123
|
+
origin: [...grid.origin],
|
|
124
|
+
columnStep: [...grid.columnStep],
|
|
125
|
+
rowStep: [...grid.rowStep],
|
|
126
|
+
registration: grid.registration,
|
|
127
|
+
heightReference: grid.heightReference
|
|
128
|
+
};
|
|
129
|
+
}
|
package/src/terrain/index.ts
CHANGED
|
@@ -67,12 +67,34 @@ export type {TerrainProfileSampler, TerrainProfile} from './profile';
|
|
|
67
67
|
export {createTerrainContours} from './contours';
|
|
68
68
|
export type {TerrainContours, TerrainContourOptions} from './contours';
|
|
69
69
|
export {stitchTerrainContours} from './contour-paths';
|
|
70
|
+
export {weldTerrainContourTiles} from './contour-tile-weld';
|
|
71
|
+
export {
|
|
72
|
+
diagnoseTerrainContourBoundaries,
|
|
73
|
+
TerrainContourBoundaryAxis
|
|
74
|
+
} from './contour-boundary-diagnostics';
|
|
75
|
+
export type {TerrainContourBoundaryDiagnostics} from './contour-boundary-diagnostics';
|
|
76
|
+
export {stitchTerrainContourTiles} from './contour-tile-paths';
|
|
77
|
+
export {createTerrainGradientPyramid} from './gradient-pyramid';
|
|
78
|
+
export type {TerrainGradientPyramid, TerrainGradientPyramidLevel} from './gradient-pyramid';
|
|
79
|
+
export type {TerrainContourTile, TerrainContourTileWeldOptions} from './contour-tile-weld';
|
|
70
80
|
export type {TerrainContourPaths, TerrainContourPathLimits} from './contour-paths';
|
|
71
81
|
export {simplifyTerrainContourPaths} from './contour-simplify';
|
|
72
82
|
export type {TerrainSimplifiedContourPaths} from './contour-simplify';
|
|
73
83
|
export {measureTerrainProfile} from './profile-measure';
|
|
84
|
+
export {getTerrainProfileVisibility} from './profile-visibility';
|
|
85
|
+
export type {TerrainProfileVisibility} from './profile-visibility';
|
|
86
|
+
export {getTerrainProfileHorizon} from './profile-horizon';
|
|
87
|
+
export type {TerrainProfileHorizon, TerrainProfileHorizonOptions} from './profile-horizon';
|
|
88
|
+
export {getTerrainProfileViewshed, TerrainProfileViewStatus} from './profile-viewshed';
|
|
74
89
|
export type {TerrainProfileMeasurements} from './profile-measure';
|
|
75
90
|
export {reconcileTerrainVertexTiles} from './vertex-tiles';
|
|
91
|
+
export {createTerrainVertexTileHalos, TerrainVertexHaloStatus} from './vertex-halos';
|
|
92
|
+
export type {TerrainVertexHaloTile} from './vertex-halos';
|
|
93
|
+
export {getTerrainVertexHaloAffectedTiles} from './vertex-halo-invalidation';
|
|
94
|
+
export type {TerrainVertexTileAddress} from './vertex-halo-invalidation';
|
|
95
|
+
export {updateTerrainVertexTileHalos} from './vertex-halo-update';
|
|
96
|
+
export {createTerrainFilteredPyramid} from './filtered-pyramid';
|
|
97
|
+
export type {TerrainFilteredLevel} from './filtered-pyramid';
|
|
76
98
|
export type {
|
|
77
99
|
TerrainVertexTile,
|
|
78
100
|
TerrainVertexTileOptions,
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {TerrainProfile} from './profile';
|
|
6
|
+
import {TerrainSampleStatus} from './types';
|
|
7
|
+
import {compareTerrainSlopes} from './profile-slope';
|
|
8
|
+
|
|
9
|
+
/** Horizon of known samples. Missing coverage means the true horizon may be higher. */
|
|
10
|
+
export type TerrainProfileHorizon = {
|
|
11
|
+
angle: number | undefined;
|
|
12
|
+
sampleIndex: number | undefined;
|
|
13
|
+
missingSampleCount: number;
|
|
14
|
+
complete: boolean;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type TerrainProfileHorizonOptions = {observerHeight: number; maxSamples?: number};
|
|
18
|
+
|
|
19
|
+
/** Calculate the maximum elevation angle in radians of a straight, sampled profile.
|
|
20
|
+
* The observer is at station zero, and its absolute height must be above known terrain
|
|
21
|
+
* there. Values describe a local Cartesian piecewise-linear profile, not unsampled
|
|
22
|
+
* terrain, curvature, refraction or a geographic path. Missing samples leave the
|
|
23
|
+
* maximum of known samples as a lower bound rather than a complete horizon.
|
|
24
|
+
*/
|
|
25
|
+
export function getTerrainProfileHorizon(
|
|
26
|
+
profile: TerrainProfile,
|
|
27
|
+
options: TerrainProfileHorizonOptions
|
|
28
|
+
): TerrainProfileHorizon {
|
|
29
|
+
const {distances, heights, statuses, length} = profile;
|
|
30
|
+
const count = distances.length;
|
|
31
|
+
const limit = options.maxSamples ?? 1_000_000;
|
|
32
|
+
if (
|
|
33
|
+
!(distances instanceof Float64Array) ||
|
|
34
|
+
!(heights instanceof Float64Array) ||
|
|
35
|
+
!(statuses instanceof Uint8Array) ||
|
|
36
|
+
count < 2 ||
|
|
37
|
+
heights.length !== count ||
|
|
38
|
+
statuses.length !== count ||
|
|
39
|
+
!Number.isSafeInteger(limit) ||
|
|
40
|
+
limit < count ||
|
|
41
|
+
!Number.isFinite(options.observerHeight) ||
|
|
42
|
+
!Number.isFinite(length) ||
|
|
43
|
+
length <= 0 ||
|
|
44
|
+
distances[0] !== 0 ||
|
|
45
|
+
distances[count - 1] !== length
|
|
46
|
+
)
|
|
47
|
+
throw new Error('Invalid terrain horizon profile or options');
|
|
48
|
+
let winningRise = 0;
|
|
49
|
+
let winningDistance = 1;
|
|
50
|
+
let sampleIndex: number | undefined;
|
|
51
|
+
let missingSampleCount = 0;
|
|
52
|
+
for (let i = 0; i < count; i++) {
|
|
53
|
+
const distance = distances[i];
|
|
54
|
+
const status = statuses[i];
|
|
55
|
+
if (!Number.isFinite(distance) || (i > 0 && distance <= distances[i - 1]))
|
|
56
|
+
throw new Error('Invalid terrain horizon stationing');
|
|
57
|
+
if (status > TerrainSampleStatus.NOT_RESIDENT)
|
|
58
|
+
throw new Error('Invalid terrain horizon status');
|
|
59
|
+
if (status !== TerrainSampleStatus.VALID) {
|
|
60
|
+
missingSampleCount++;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (!Number.isFinite(heights[i])) throw new Error('Invalid terrain horizon height');
|
|
64
|
+
if (i === 0) {
|
|
65
|
+
if (options.observerHeight <= heights[i])
|
|
66
|
+
throw new Error('Terrain horizon observer must be above ground');
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const delta = heights[i] - options.observerHeight;
|
|
70
|
+
if (!Number.isFinite(delta)) throw new Error('Terrain horizon arithmetic overflow');
|
|
71
|
+
if (
|
|
72
|
+
sampleIndex === undefined ||
|
|
73
|
+
compareTerrainSlopes(delta, distance, winningRise, winningDistance) > 0
|
|
74
|
+
) {
|
|
75
|
+
winningRise = delta;
|
|
76
|
+
winningDistance = distance;
|
|
77
|
+
sampleIndex = i;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
angle: sampleIndex === undefined ? undefined : Math.atan2(winningRise, winningDistance),
|
|
82
|
+
sampleIndex,
|
|
83
|
+
missingSampleCount,
|
|
84
|
+
complete: missingSampleCount === 0
|
|
85
|
+
};
|
|
86
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
/** Compare finite rises divided by positive distances, including overflow and rounded ties. */
|
|
6
|
+
export function compareTerrainSlopes(a: number, ad: number, b: number, bd: number): number {
|
|
7
|
+
const as = a / ad;
|
|
8
|
+
const bs = b / bd;
|
|
9
|
+
if (as < bs) return -1;
|
|
10
|
+
if (as > bs) return 1;
|
|
11
|
+
if (a === 0 || b === 0 || Math.sign(a) !== Math.sign(b)) return Math.sign(a) - Math.sign(b);
|
|
12
|
+
// Division can round distinct ratios to the same value, zero or infinity.
|
|
13
|
+
// Compare the exact products of their binary64 significands only on that path.
|
|
14
|
+
const bytes = new DataView(new ArrayBuffer(8));
|
|
15
|
+
const parts = (value: number): [bigint, number] => {
|
|
16
|
+
bytes.setFloat64(0, Math.abs(value));
|
|
17
|
+
const bits = bytes.getBigUint64(0);
|
|
18
|
+
const exponent = Number((bits >> 52n) & 0x7ffn);
|
|
19
|
+
const fraction = bits & 0xfffffffffffffn;
|
|
20
|
+
return exponent ? [fraction | 0x10000000000000n, exponent - 1075] : [fraction, -1074];
|
|
21
|
+
};
|
|
22
|
+
const [am, ae] = parts(a),
|
|
23
|
+
[bdm, bde] = parts(bd);
|
|
24
|
+
const [bm, be] = parts(b),
|
|
25
|
+
[adm, ade] = parts(ad);
|
|
26
|
+
let left = am * bdm;
|
|
27
|
+
let right = bm * adm;
|
|
28
|
+
const shift = ae + bde - be - ade;
|
|
29
|
+
if (shift > 0) left <<= BigInt(shift);
|
|
30
|
+
else if (shift < 0) right <<= BigInt(-shift);
|
|
31
|
+
return (left < right ? -1 : left > right ? 1 : 0) * Math.sign(a);
|
|
32
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {TerrainProfile} from './profile';
|
|
6
|
+
import {TerrainSampleStatus} from './types';
|
|
7
|
+
import {getTerrainProfileHorizon, type TerrainProfileHorizonOptions} from './profile-horizon';
|
|
8
|
+
import {compareTerrainSlopes} from './profile-slope';
|
|
9
|
+
|
|
10
|
+
/** Sample classification for a single radial profile; station zero is the observer. */
|
|
11
|
+
export const TerrainProfileViewStatus = {
|
|
12
|
+
OBSERVER: 0,
|
|
13
|
+
VISIBLE: 1,
|
|
14
|
+
BLOCKED: 2,
|
|
15
|
+
UNKNOWN: 3,
|
|
16
|
+
MISSING: 4
|
|
17
|
+
} as const;
|
|
18
|
+
|
|
19
|
+
/** Classify each terrain sample against earlier samples on a straight radial profile.
|
|
20
|
+
* A previous sample on the ray blocks it. Missing earlier samples make otherwise
|
|
21
|
+
* visible targets unknown, but cannot undo a known obstruction. This is a sampled
|
|
22
|
+
* local profile, not an area viewshed or a guarantee about unsampled terrain.
|
|
23
|
+
*/
|
|
24
|
+
export function getTerrainProfileViewshed(
|
|
25
|
+
profile: TerrainProfile,
|
|
26
|
+
options: TerrainProfileHorizonOptions
|
|
27
|
+
): Uint8Array {
|
|
28
|
+
// Validate the whole profile before writing any output.
|
|
29
|
+
getTerrainProfileHorizon(profile, options);
|
|
30
|
+
const result = new Uint8Array(profile.distances.length);
|
|
31
|
+
let maxRise = 0;
|
|
32
|
+
let maxDistance = 1;
|
|
33
|
+
let hasHorizon = false;
|
|
34
|
+
let missingBefore = profile.statuses[0] !== TerrainSampleStatus.VALID;
|
|
35
|
+
for (let i = 1; i < result.length; i++) {
|
|
36
|
+
if (profile.statuses[i] !== TerrainSampleStatus.VALID) {
|
|
37
|
+
result[i] = TerrainProfileViewStatus.MISSING;
|
|
38
|
+
missingBefore = true;
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
const rise = profile.heights[i] - options.observerHeight;
|
|
42
|
+
const distance = profile.distances[i];
|
|
43
|
+
const comparison = hasHorizon ? compareTerrainSlopes(rise, distance, maxRise, maxDistance) : 1;
|
|
44
|
+
result[i] =
|
|
45
|
+
comparison <= 0
|
|
46
|
+
? TerrainProfileViewStatus.BLOCKED
|
|
47
|
+
: missingBefore
|
|
48
|
+
? TerrainProfileViewStatus.UNKNOWN
|
|
49
|
+
: TerrainProfileViewStatus.VISIBLE;
|
|
50
|
+
if (comparison > 0) {
|
|
51
|
+
maxRise = rise;
|
|
52
|
+
maxDistance = distance;
|
|
53
|
+
hasHorizon = true;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return result;
|
|
57
|
+
}
|