@math.gl/geospatial 5.0.0-beta.3 → 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.
Files changed (35) hide show
  1. package/dist/index.cjs +244 -0
  2. package/dist/index.cjs.map +3 -3
  3. package/dist/index.d.ts +7 -0
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +4 -0
  6. package/dist/index.js.map +1 -1
  7. package/dist/terrain/contour-boundary-diagnostics.d.ts +23 -0
  8. package/dist/terrain/contour-boundary-diagnostics.d.ts.map +1 -0
  9. package/dist/terrain/contour-boundary-diagnostics.js +62 -0
  10. package/dist/terrain/contour-boundary-diagnostics.js.map +1 -0
  11. package/dist/terrain/contour-tile-paths.d.ts +7 -0
  12. package/dist/terrain/contour-tile-paths.d.ts.map +1 -0
  13. package/dist/terrain/contour-tile-paths.js +12 -0
  14. package/dist/terrain/contour-tile-paths.js.map +1 -0
  15. package/dist/terrain/contour-tile-weld.d.ts +24 -0
  16. package/dist/terrain/contour-tile-weld.d.ts.map +1 -0
  17. package/dist/terrain/contour-tile-weld.js +147 -0
  18. package/dist/terrain/contour-tile-weld.js.map +1 -0
  19. package/dist/terrain/gradient-pyramid.d.ts +29 -0
  20. package/dist/terrain/gradient-pyramid.d.ts.map +1 -0
  21. package/dist/terrain/gradient-pyramid.js +94 -0
  22. package/dist/terrain/gradient-pyramid.js.map +1 -0
  23. package/dist/terrain/index.cjs +245 -1
  24. package/dist/terrain/index.cjs.map +2 -2
  25. package/dist/terrain/index.d.ts +7 -0
  26. package/dist/terrain/index.d.ts.map +1 -1
  27. package/dist/terrain/index.js +4 -0
  28. package/dist/terrain/index.js.map +1 -1
  29. package/package.json +5 -5
  30. package/src/index.ts +10 -0
  31. package/src/terrain/contour-boundary-diagnostics.ts +90 -0
  32. package/src/terrain/contour-tile-paths.ts +20 -0
  33. package/src/terrain/contour-tile-weld.ts +182 -0
  34. package/src/terrain/gradient-pyramid.ts +129 -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BAAwB;AACtD,OAAO,EAAC,uBAAuB,EAAC,+BAA4B;AAC5D,OAAO,EACL,gCAAgC,EAChC,0BAA0B,EAC3B,0CAAuC;AAExC,OAAO,EAAC,yBAAyB,EAAC,gCAA6B;AAC/D,OAAO,EAAC,4BAA4B,EAAC,8BAA2B;AAIhE,OAAO,EAAC,2BAA2B,EAAC,8BAA2B;AAE/D,OAAO,EAAC,qBAAqB,EAAC,6BAA0B;AACxD,OAAO,EAAC,2BAA2B,EAAC,gCAA6B;AAEjE,OAAO,EAAC,wBAAwB,EAAC,6BAA0B;AAE3D,OAAO,EAAC,yBAAyB,EAAE,wBAAwB,EAAC,8BAA2B;AAEvF,OAAO,EAAC,2BAA2B,EAAC,0BAAuB;AAC3D,OAAO,EAAC,4BAA4B,EAAE,uBAAuB,EAAC,0BAAuB;AAErF,OAAO,EAAC,iCAAiC,EAAC,sCAAmC;AAE7E,OAAO,EAAC,4BAA4B,EAAC,gCAA6B;AAClE,OAAO,EAAC,4BAA4B,EAAC,8BAA2B;AAOhE,OAAO,EAAC,sBAAsB,EAAC,yBAAsB"}
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "publishConfig": {
7
7
  "access": "public"
8
8
  },
9
- "version": "5.0.0-beta.3",
9
+ "version": "5.0.0-beta.4",
10
10
  "keywords": [
11
11
  "webgl",
12
12
  "javascript",
@@ -1041,9 +1041,9 @@
1041
1041
  ],
1042
1042
  "dependencies": {
1043
1043
  "@mapbox/martini": "0.2.0",
1044
- "@math.gl/core": "5.0.0-beta.3",
1045
- "@math.gl/geometry": "5.0.0-beta.3",
1046
- "@math.gl/web-mercator": "5.0.0-beta.3",
1044
+ "@math.gl/core": "5.0.0-beta.4",
1045
+ "@math.gl/geometry": "5.0.0-beta.4",
1046
+ "@math.gl/web-mercator": "5.0.0-beta.4",
1047
1047
  "polygon-clipping": "0.15.7"
1048
1048
  },
1049
1049
  "devDependencies": {
@@ -1055,5 +1055,5 @@
1055
1055
  "proj4": "2.22.0"
1056
1056
  },
1057
1057
  "sideEffects": false,
1058
- "gitHead": "5b20f706e26c9c5cfb252202e503fb98517a7350"
1058
+ "gitHead": "61847e2c9615be580088d54804fbb21c537b3850"
1059
1059
  }
package/src/index.ts CHANGED
@@ -373,6 +373,16 @@ export type {TerrainProfileSampler, TerrainProfile} from './terrain/profile';
373
373
  export {createTerrainContours} from './terrain/contours';
374
374
  export type {TerrainContours, TerrainContourOptions} from './terrain/contours';
375
375
  export {stitchTerrainContours} from './terrain/contour-paths';
376
+ export {weldTerrainContourTiles} from './terrain/contour-tile-weld';
377
+ export {
378
+ diagnoseTerrainContourBoundaries,
379
+ TerrainContourBoundaryAxis
380
+ } from './terrain/contour-boundary-diagnostics';
381
+ export type {TerrainContourBoundaryDiagnostics} from './terrain/contour-boundary-diagnostics';
382
+ export {stitchTerrainContourTiles} from './terrain/contour-tile-paths';
383
+ export {createTerrainGradientPyramid} from './terrain/gradient-pyramid';
384
+ export type {TerrainGradientPyramid, TerrainGradientPyramidLevel} from './terrain/gradient-pyramid';
385
+ export type {TerrainContourTile, TerrainContourTileWeldOptions} from './terrain/contour-tile-weld';
376
386
  export type {TerrainContourPaths, TerrainContourPathLimits} from './terrain/contour-paths';
377
387
  export {simplifyTerrainContourPaths} from './terrain/contour-simplify';
378
388
  export type {TerrainSimplifiedContourPaths} from './terrain/contour-simplify';
@@ -0,0 +1,90 @@
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 const TerrainContourBoundaryAxis = {COLUMN: 1, ROW: 2} as const;
14
+ export type TerrainContourBoundaryDiagnostics = {
15
+ /** Boundary vertices with degree one (open) or greater than two (junction). */
16
+ vertexIndices: Uint32Array;
17
+ degrees: Uint32Array;
18
+ levelIndices: Uint32Array;
19
+ /** Bitmask of TerrainContourBoundaryAxis values; corners have both bits. */
20
+ boundaryAxes: Uint8Array;
21
+ };
22
+
23
+ /** Inspect a welded contour graph at same-level tile lattice lines. An open
24
+ * endpoint can indicate a dataset edge, missing tile, nodata or another break;
25
+ * this function does not infer the cause or alter the graph.
26
+ */
27
+ export function diagnoseTerrainContourBoundaries(
28
+ contours: TerrainContours,
29
+ options: TerrainContourPathLimits & {width: number; height: number}
30
+ ): TerrainContourBoundaryDiagnostics {
31
+ const {width, height} = options;
32
+ if (!Number.isSafeInteger(width) || width < 2 || !Number.isSafeInteger(height) || height < 2)
33
+ throw new Error('Invalid terrain contour boundary tile dimensions');
34
+ const limits = contourPathLimits(options);
35
+ const count = validateContourData(contours, limits);
36
+ if (
37
+ !(contours.segments instanceof Uint32Array) ||
38
+ contours.segments.length % 2 ||
39
+ !(contours.levelIndices instanceof Uint32Array) ||
40
+ contours.levelIndices.length !== contours.segments.length / 2 ||
41
+ contours.levelIndices.length > limits.maxSegments
42
+ )
43
+ throw new Error('Invalid terrain contour boundary segments or capacity');
44
+ const degrees = new Uint32Array(count),
45
+ assignments = new Uint32Array(count).fill(0xffffffff);
46
+ const seen = new Set<string>();
47
+ for (let i = 0; i < contours.levelIndices.length; i++) {
48
+ const a = contours.segments[2 * i],
49
+ b = contours.segments[2 * i + 1],
50
+ level = contours.levelIndices[i];
51
+ validateContourVertex(contours, a, level, assignments);
52
+ validateContourVertex(contours, b, level, assignments);
53
+ if (
54
+ a === b ||
55
+ (contours.positions[3 * a] === contours.positions[3 * b] &&
56
+ contours.positions[3 * a + 1] === contours.positions[3 * b + 1])
57
+ )
58
+ throw new Error('Degenerate terrain contour segment');
59
+ const key = a < b ? `${a}/${b}` : `${b}/${a}`;
60
+ if (seen.has(key)) throw new Error('Duplicate terrain contour segment');
61
+ seen.add(key);
62
+ degrees[a]++;
63
+ degrees[b]++;
64
+ }
65
+ const vertices: number[] = [],
66
+ resultDegrees: number[] = [],
67
+ levels: number[] = [],
68
+ axes: number[] = [];
69
+ for (let i = 0; i < count; i++) {
70
+ if (degrees[i] !== 1 && degrees[i] <= 2) continue;
71
+ const x = contours.gridCoordinates[2 * i],
72
+ y = contours.gridCoordinates[2 * i + 1];
73
+ if (Math.abs(x) > Number.MAX_SAFE_INTEGER || Math.abs(y) > Number.MAX_SAFE_INTEGER)
74
+ throw new Error('Terrain contour boundary lattice exceeds safe integer range');
75
+ const axis =
76
+ (Number.isSafeInteger(x) && x % (width - 1) === 0 ? TerrainContourBoundaryAxis.COLUMN : 0) |
77
+ (Number.isSafeInteger(y) && y % (height - 1) === 0 ? TerrainContourBoundaryAxis.ROW : 0);
78
+ if (!axis) continue;
79
+ vertices.push(i);
80
+ resultDegrees.push(degrees[i]);
81
+ levels.push(assignments[i]);
82
+ axes.push(axis);
83
+ }
84
+ return {
85
+ vertexIndices: Uint32Array.from(vertices),
86
+ degrees: Uint32Array.from(resultDegrees),
87
+ levelIndices: Uint32Array.from(levels),
88
+ boundaryAxes: Uint8Array.from(axes)
89
+ };
90
+ }
@@ -0,0 +1,20 @@
1
+ // math.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ import {
6
+ weldTerrainContourTiles,
7
+ type TerrainContourTile,
8
+ type TerrainContourTileWeldOptions
9
+ } from './contour-tile-weld';
10
+ import {stitchTerrainContours, type TerrainContourPaths} from './contour-paths';
11
+
12
+ /** Weld a same-level tile snapshot and trace its maximal contour paths.
13
+ * Missing tiles and source gaps remain open. This does not simplify paths.
14
+ */
15
+ export function stitchTerrainContourTiles(
16
+ tiles: readonly TerrainContourTile[],
17
+ options: TerrainContourTileWeldOptions
18
+ ): TerrainContourPaths {
19
+ return stitchTerrainContours(weldTerrainContourTiles(tiles, options), options);
20
+ }
@@ -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,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
+ }
@@ -67,6 +67,16 @@ 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';