@macrostrat/cesium-martini 1.5.1 → 1.5.3

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.
@@ -0,0 +1,267 @@
1
+ import type { NdArray } from "ndarray";
2
+
3
+ export interface TerrainWorkerInput extends QuantizedMeshOptions {
4
+ imageData: Uint8ClampedArray;
5
+ maxVertexDistance: number | null;
6
+ x: number;
7
+ y: number;
8
+ z: number;
9
+ }
10
+
11
+ export interface TerrainUpscaleInput
12
+ extends Omit<TerrainWorkerInput, "imageData"> {
13
+ overscaleFactor: number;
14
+ heightData: Float32Array;
15
+ }
16
+
17
+ export type DecodeRgbFunction = (
18
+ r: number,
19
+ g: number,
20
+ b: number,
21
+ a: number,
22
+ ) => number;
23
+
24
+ /** Mapbox Terrain-RGB default decode function
25
+ * (r * 256 * 256) / 10 + (g * 256) / 10 + b / 10 - 10000
26
+ */
27
+ const defaultMapboxDecodeRgb: DecodeRgbFunction = (r, g, b, a) =>
28
+ r * 6553.6 + g * 25.6 + b * 0.1 - 10000;
29
+
30
+ function rgbTerrainToGrid(png: NdArray, decodeRgb?: DecodeRgbFunction) {
31
+ // maybe we should do this on the GPU using REGL?
32
+ // but that would require GPU -> CPU -> GPU
33
+ const gridSize = png.shape[0] + 1;
34
+ const terrain = new Float32Array(gridSize * gridSize);
35
+ const tileSize = png.shape[0];
36
+
37
+ const decode = decodeRgb ?? defaultMapboxDecodeRgb;
38
+
39
+ // decode terrain values
40
+ for (let y = 0; y < tileSize; y++) {
41
+ for (let x = 0; x < tileSize; x++) {
42
+ const yc = y;
43
+ const r = png.get(x, yc, 0);
44
+ const g = png.get(x, yc, 1);
45
+ const b = png.get(x, yc, 2);
46
+ const a = png.get(x, yc, 3);
47
+ terrain[y * gridSize + x] = decode(r, g, b, a);
48
+ }
49
+ }
50
+ // backfill right and bottom borders
51
+ for (let x = 0; x < gridSize - 1; x++) {
52
+ terrain[gridSize * (gridSize - 1) + x] =
53
+ terrain[gridSize * (gridSize - 2) + x];
54
+ }
55
+ for (let y = 0; y < gridSize; y++) {
56
+ terrain[gridSize * y + gridSize - 1] = terrain[gridSize * y + gridSize - 2];
57
+ }
58
+ return terrain;
59
+ }
60
+
61
+ export type Window = { x0: number; x1: number; y0: number; y1: number };
62
+
63
+ export function subsetByWindow(
64
+ array: Float32Array,
65
+ window: Window,
66
+ augmented: boolean,
67
+ ) {
68
+ const sz = Math.sqrt(array.length);
69
+ const x0 = window.x0;
70
+ const x1 = window.x1;
71
+ const y0 = window.y0;
72
+ const y1 = window.y1;
73
+ const aug = augmented ? 1 : 0;
74
+ const n = Math.floor(x1 - x0) + aug;
75
+ const m = Math.floor(y1 - y0) + aug;
76
+ const result = new Float32Array(n * m);
77
+ for (let i = 0; i < m; i++) {
78
+ for (let j = 0; j < n; j++) {
79
+ result[i * n + j] = array[(i + y0) * sz + j + x0];
80
+ }
81
+ }
82
+ return result;
83
+ }
84
+
85
+ export function testMeshData(): QuantizedMeshResult {
86
+ return {
87
+ minimumHeight: -100,
88
+ maximumHeight: 2101,
89
+ quantizedVertices: new Uint16Array([
90
+ // order is SW NW SE NE
91
+ // longitude
92
+ 0, 0, 32767, 32767,
93
+ // latitude
94
+ 0, 32767, 0, 32767,
95
+ // heights
96
+ 16384, 0, 32767, 16384,
97
+ ]),
98
+ indices: new Uint16Array([0, 3, 1, 0, 2, 3]),
99
+ westIndices: [0, 1],
100
+ southIndices: [0, 1],
101
+ eastIndices: [2, 3],
102
+ northIndices: [1, 3],
103
+ };
104
+ }
105
+
106
+ function _emptyMesh(n: number): QuantizedMeshResult {
107
+ n = Math.max(n, 2);
108
+ const nTriangles = Math.pow(n - 1, 2) * 2;
109
+ const nVertices = Math.pow(n, 2);
110
+ const quantizedVertices = new Uint16Array(nVertices * 3);
111
+ const indices = new Uint16Array(nTriangles * 3);
112
+ const westIndices = [];
113
+ const southIndices = [];
114
+ const eastIndices = [];
115
+ const northIndices = [];
116
+
117
+ let tix = 0;
118
+
119
+ for (let i = 0; i < nVertices; i++) {
120
+ let rx = i % n; //* 32767) / (n - 1);
121
+ let ry = Math.floor(i / n); //* 32767) / (n - 1);
122
+ const ix = n * rx + ry;
123
+ quantizedVertices[ix] = (rx * 32768) / (n - 1);
124
+ quantizedVertices[nVertices + ix] = (ry * 32768) / (n - 1);
125
+ quantizedVertices[2 * nVertices + ix] = 0;
126
+ if (ry == 0) westIndices.push(ix);
127
+ if (rx == 0) southIndices.push(ix);
128
+ if (rx == n - 1) eastIndices.push(ix);
129
+ if (ry == n - 1) northIndices.push(ix);
130
+
131
+ // Add triangles
132
+ const rix = i - ry * n;
133
+ if (rix != n - 1) {
134
+ indices[tix * 3] = i;
135
+ indices[tix * 3 + 1] = i + n + 1;
136
+ indices[tix * 3 + 2] = i + 1;
137
+ tix++;
138
+ }
139
+ if (rix != 0) {
140
+ indices[tix * 3] = i - 1;
141
+ indices[tix * 3 + 1] = i + n - 1;
142
+ indices[tix * 3 + 2] = i + n;
143
+ tix++;
144
+ }
145
+ }
146
+
147
+ return {
148
+ minimumHeight: 0,
149
+ maximumHeight: 0,
150
+ quantizedVertices,
151
+ indices,
152
+ westIndices,
153
+ southIndices,
154
+ eastIndices,
155
+ northIndices,
156
+ };
157
+ }
158
+
159
+ let _meshCache = [];
160
+ export function emptyMesh(n: number) {
161
+ // A memoized function to return empty meshes
162
+ if (n in _meshCache) {
163
+ return _meshCache[n];
164
+ } else {
165
+ const result = _emptyMesh(n);
166
+ _meshCache[n] = result;
167
+ return result;
168
+ }
169
+ }
170
+
171
+ export interface QuantizedMeshOptions {
172
+ errorLevel: number;
173
+ tileSize: number;
174
+ ellipsoidRadius: number;
175
+ }
176
+
177
+ export interface QuantizedMeshResult {
178
+ minimumHeight: number;
179
+ maximumHeight: number;
180
+ quantizedVertices: Uint16Array;
181
+ indices: Uint16Array;
182
+ westIndices: number[];
183
+ southIndices: number[];
184
+ eastIndices: number[];
185
+ northIndices: number[];
186
+ quantizedHeights?: Float32Array;
187
+ }
188
+
189
+ /** Terrain workers should return a quantized mesh */
190
+ export type TerrainWorkerOutput = QuantizedMeshResult;
191
+
192
+ function createQuantizedMeshData(
193
+ tile: any,
194
+ mesh: any,
195
+ tileSize: number,
196
+ terrain: Float32Array | null,
197
+ ): QuantizedMeshResult {
198
+ /** Terrain is passed through so we can keep track of it
199
+ * for overscaled tiles
200
+ */
201
+
202
+ const xvals = [];
203
+ const yvals = [];
204
+ const heightMeters = [];
205
+ const northIndices = [];
206
+ const southIndices = [];
207
+ const eastIndices = [];
208
+ const westIndices = [];
209
+
210
+ let minimumHeight = Infinity;
211
+ let maximumHeight = -Infinity;
212
+ const scalar = 32768.0 / tileSize;
213
+
214
+ // There appears to be a problem with the x/y indexing when using 512x512 tiles
215
+ // This may be solved by increasing the minumumErrorLevel in the terrain provider
216
+ for (let ix = 0; ix < mesh.vertices.length / 2; ix++) {
217
+ const vertexIx = ix;
218
+ const px = mesh.vertices[ix * 2];
219
+ const py = mesh.vertices[ix * 2 + 1];
220
+ const height = tile.terrain[py * (tileSize + 1) + px];
221
+ if (height > maximumHeight) maximumHeight = height;
222
+ if (height < minimumHeight) minimumHeight = height;
223
+
224
+ heightMeters.push(height);
225
+
226
+ if (py == 0) northIndices.push(vertexIx);
227
+ if (py == tileSize) southIndices.push(vertexIx);
228
+ if (px == 0) westIndices.push(vertexIx);
229
+ if (px == tileSize) eastIndices.push(vertexIx);
230
+
231
+ let xv = px * scalar;
232
+ let yv = (tileSize - py) * scalar;
233
+
234
+ xvals.push(xv);
235
+ yvals.push(yv);
236
+ }
237
+
238
+ const heightRange = maximumHeight - minimumHeight;
239
+
240
+ const heights = heightMeters.map((d) => {
241
+ if (heightRange < 1) return 0;
242
+ return (d - minimumHeight) * (32768.0 / heightRange);
243
+ });
244
+
245
+ const triangles = new Uint16Array(mesh.triangles);
246
+ const quantizedVertices = new Uint16Array(
247
+ //verts
248
+ [...xvals, ...yvals, ...heights],
249
+ );
250
+
251
+ // SE NW NE
252
+ // NE NW SE
253
+
254
+ return {
255
+ minimumHeight,
256
+ maximumHeight,
257
+ quantizedVertices,
258
+ indices: triangles,
259
+ westIndices,
260
+ southIndices,
261
+ eastIndices,
262
+ northIndices,
263
+ quantizedHeights: terrain,
264
+ };
265
+ }
266
+
267
+ export { rgbTerrainToGrid, createQuantizedMeshData };
package/.env.example DELETED
@@ -1 +0,0 @@
1
- MAPBOX_API_TOKEN='<your_mapbox_api_token>'
package/.prettierrc DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "proseWrap": "always"
3
- }
package/babel.config.js DELETED
@@ -1,3 +0,0 @@
1
- module.exports = {
2
- presets: ["@babel/preset-env", "@babel/preset-typescript"],
3
- };
package/rollup.config.js DELETED
@@ -1,46 +0,0 @@
1
- import pkg from "./package.json";
2
- import babel from "@rollup/plugin-babel";
3
- import resolve from "@rollup/plugin-node-resolve";
4
- const deps = { ...pkg.dependencies, ...pkg.peerDependencies };
5
- // bundle web workers
6
- import webWorkerLoader from "rollup-plugin-web-worker-loader";
7
- import commonjs from "@rollup/plugin-commonjs";
8
-
9
- //https://2ality.com/2017/02/babel-preset-env.html
10
-
11
- const extensions = [".js", ".ts"];
12
-
13
- let external = Object.keys(deps);
14
- delete external["maplibre-gl"];
15
-
16
- export default {
17
- input: "src/index.ts", // our source file
18
- output: [
19
- {
20
- file: pkg.main,
21
- format: "cjs",
22
- sourcemap: true,
23
- exports: "named",
24
- },
25
- {
26
- file: pkg.module,
27
- format: "esm",
28
- sourcemap: true,
29
- },
30
- ],
31
- external: Object.keys(deps),
32
- plugins: [
33
- resolve({ extensions, module: true }),
34
- commonjs(),
35
- babel({
36
- extensions,
37
- include: ["src/**/*.ts", "node_modules/maplibre-gl/**/*.ts"],
38
- }),
39
- webWorkerLoader({
40
- inline: true,
41
- targetPlatform: "browser",
42
- extensions: ["ts", "js"],
43
- external: [],
44
- }),
45
- ],
46
- };
package/tsconfig.json DELETED
@@ -1,19 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "moduleResolution": "node",
4
- "target": "es2022",
5
- "module":"es2022",
6
- "lib": ["es2022", "dom"],
7
- "strict": false,
8
- "sourceMap": true,
9
- "declaration": true,
10
- "allowSyntheticDefaultImports": true,
11
- "experimentalDecorators": true,
12
- "emitDecoratorMetadata": true,
13
- "declarationDir": "dist/types",
14
- "outDir": "dist/lib",
15
- "typeRoots": ["node_modules/@types"]
16
- },
17
- "include": ["src", "examples"],
18
- "exclude": ["node_modules"]
19
- }