@loaders.gl/raster 5.0.0-beta.2 → 5.0.0-beta.5

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/README.md +1 -1
  2. package/THIRD-PARTY-NOTICES.md +1 -1
  3. package/dist/geotiff/advanced.cjs +4 -4
  4. package/dist/geotiff/geotiff-loader-types.js +1 -1
  5. package/dist/geotiff/geotiff-raster-loader-types.js +1 -1
  6. package/dist/geotiff/geotiff-source-loader.js +1 -1
  7. package/dist/geotiff/loaders.js +1 -1
  8. package/dist/geotiff/ometiff-source-loader.js +1 -1
  9. package/dist/index.cjs +4 -4
  10. package/dist/lerc/lerc-loader-types.js +1 -1
  11. package/dist/quantized-mesh-worker.js +985 -368
  12. package/dist/terrain/index.cjs +26 -525
  13. package/dist/terrain/index.cjs.map +4 -4
  14. package/dist/terrain/lib/parse-terrain.d.ts +2 -0
  15. package/dist/terrain/lib/parse-terrain.d.ts.map +1 -1
  16. package/dist/terrain/lib/parse-terrain.js +23 -38
  17. package/dist/terrain/lib/parse-terrain.js.map +1 -1
  18. package/dist/terrain/lib/utils/version.js +1 -1
  19. package/dist/terrain/quantized-mesh-loader.js +1 -1
  20. package/dist/terrain/quantized-mesh-loader.js.map +1 -1
  21. package/dist/terrain/terrain-loader.js +1 -1
  22. package/dist/terrain/terrain-loader.js.map +1 -1
  23. package/dist/terrain-worker.js +16699 -15463
  24. package/package.json +6 -7
  25. package/src/terrain/lib/parse-terrain.ts +36 -40
  26. package/src/terrain/quantized-mesh-loader.ts +1 -1
  27. package/src/terrain/terrain-loader.ts +1 -1
  28. package/dist/terrain/libs/README.md +0 -10
  29. package/dist/terrain/libs/martini-LICENSE +0 -15
  30. package/dist/terrain/libs/martini.d.mts +0 -19
  31. package/dist/terrain/libs/martini.mjs +0 -164
  32. package/src/terrain/libs/README.md +0 -10
  33. package/src/terrain/libs/martini-LICENSE +0 -15
  34. package/src/terrain/libs/martini.d.mts +0 -19
  35. package/src/terrain/libs/martini.mjs +0 -164
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loaders.gl/raster",
3
- "version": "5.0.0-beta.2",
3
+ "version": "5.0.0-beta.5",
4
4
  "description": "GeoTIFF, OME-TIFF, LERC, and terrain raster loaders",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -318,16 +318,16 @@
318
318
  "third-party-licenses"
319
319
  ],
320
320
  "scripts": {
321
- "copy-libs": "mkdir -p dist/terrain/libs dist/lerc/libs && cp -R src/terrain/libs/. dist/terrain/libs/ && cp -R src/lerc/libs/. dist/lerc/libs/",
321
+ "copy-libs": "mkdir -p dist/lerc/libs && cp -R src/lerc/libs/. dist/lerc/libs/",
322
322
  "build-cjs": "node ../../node_modules/@vis.gl/dev-tools/dist/build-cjs.js",
323
323
  "build-worker-terrain": "node ../../scripts/build-codec-worker.mjs src/terrain/workers/terrain-worker.ts --bundle --outfile=dist/terrain-worker.js --define:__VERSION__=\\\"$npm_package_version\\\"",
324
324
  "build-worker-quantized-mesh": "node ../../scripts/build-codec-worker.mjs src/terrain/workers/quantized-mesh-worker.ts --bundle --outfile=dist/quantized-mesh-worker.js --define:__VERSION__=\\\"$npm_package_version\\\"",
325
325
  "pre-build": "npm run copy-libs && node ./build-cjs.mjs && npm run build-worker-terrain && npm run build-worker-quantized-mesh"
326
326
  },
327
327
  "dependencies": {
328
- "@loaders.gl/documents": "5.0.0-beta.2",
329
- "@loaders.gl/images": "5.0.0-beta.2",
330
- "@math.gl/geospatial": "5.0.0-beta.2"
328
+ "@loaders.gl/documents": "5.0.0-beta.5",
329
+ "@loaders.gl/images": "5.0.0-beta.5",
330
+ "@math.gl/geospatial": "5.0.0-beta.5"
331
331
  },
332
332
  "peerDependencies": {
333
333
  "@loaders.gl/core": "~5.0.0-alpha.0",
@@ -343,7 +343,6 @@
343
343
  }
344
344
  },
345
345
  "devDependencies": {
346
- "@mapbox/martini": "0.2.0",
347
346
  "geotiff": "^2.1.0",
348
347
  "lerc": "^4.2.0"
349
348
  },
@@ -373,5 +372,5 @@
373
372
  "terrain",
374
373
  "tif"
375
374
  ],
376
- "gitHead": "d259e5bc74caf8d75e808314b7fef2fa1bd70ff7"
375
+ "gitHead": "673b72c560b284f158ff145ba74707f9a3994935"
377
376
  }
@@ -4,8 +4,11 @@
4
4
 
5
5
  import type {Mesh} from '@loaders.gl/core/schema';
6
6
  import {deduceMeshSchema, getMeshBoundingBox} from '@loaders.gl/core/schema-utils';
7
- import Martini from '../libs/martini.mjs';
8
- import Delatin from './delatin/index';
7
+ import {
8
+ HeightField,
9
+ createMartiniTerrainMesh,
10
+ createDelatinTerrainMesh
11
+ } from '@math.gl/geospatial/terrain';
9
12
  import {addSkirt} from './helpers/skirt';
10
13
  import {decodeTerrainHeightmapInto} from './decode-terrain-heightmap';
11
14
  import type {ElevationDecoder} from './terrain-heightmap-types';
@@ -13,6 +16,8 @@ import type {ElevationDecoder} from './terrain-heightmap-types';
13
16
  export type TerrainOptions = {
14
17
  /** Maximum terrain mesh error in meters. */
15
18
  meshMaxError: number;
19
+ /** Maximum source-node validation tests per refinement pass; defaults scale with the padded grid. */
20
+ meshMaxSampleTests?: number;
16
21
  /** Bounds used to map terrain image coordinates to x/y positions. */
17
22
  bounds: number[];
18
23
  /** Decoder used to convert terrain image channels to elevation values. */
@@ -43,7 +48,7 @@ export function makeTerrainMeshFromImage(
43
48
  terrainImage: TerrainImage,
44
49
  terrainOptions: TerrainOptions
45
50
  ): Mesh {
46
- const {meshMaxError, bounds, elevationDecoder} = terrainOptions;
51
+ const {bounds, elevationDecoder} = terrainOptions;
47
52
 
48
53
  const {data, width, height} = terrainImage;
49
54
 
@@ -52,20 +57,20 @@ export function makeTerrainMeshFromImage(
52
57
  switch (terrainOptions.tesselator) {
53
58
  case 'martini':
54
59
  terrain = getTerrain(data, width, height, elevationDecoder, terrainOptions.tesselator);
55
- mesh = getMartiniTileMesh(meshMaxError, width, terrain);
60
+ mesh = getTerrainTileMesh(terrainOptions, width, height, terrain, 'martini');
56
61
  break;
57
62
  case 'delatin':
58
63
  terrain = getTerrain(data, width, height, elevationDecoder, terrainOptions.tesselator);
59
- mesh = getDelatinTileMesh(meshMaxError, width, height, terrain);
64
+ mesh = getTerrainTileMesh(terrainOptions, width, height, terrain, 'delatin');
60
65
  break;
61
66
  // auto
62
67
  default:
63
68
  if (width === height && !(height & (width - 1))) {
64
69
  terrain = getTerrain(data, width, height, elevationDecoder, 'martini');
65
- mesh = getMartiniTileMesh(meshMaxError, width, terrain);
70
+ mesh = getTerrainTileMesh(terrainOptions, width, height, terrain, 'martini');
66
71
  } else {
67
72
  terrain = getTerrain(data, width, height, elevationDecoder, 'delatin');
68
- mesh = getDelatinTileMesh(meshMaxError, width, height, terrain);
73
+ mesh = getTerrainTileMesh(terrainOptions, width, height, terrain, 'delatin');
69
74
  }
70
75
  break;
71
76
  }
@@ -112,39 +117,30 @@ export function makeTerrainMeshFromImage(
112
117
  };
113
118
  }
114
119
 
115
- /**
116
- * Get Martini generated vertices and triangles
117
- *
118
- * @param {number} meshMaxError threshold for simplifying mesh
119
- * @param {number} width width of the input data
120
- * @param {number[] | Float32Array} terrain elevation data
121
- * @returns {{vertices: Uint16Array, triangles: Uint32Array}} vertices and triangles data
122
- */
123
- function getMartiniTileMesh(meshMaxError, width, terrain) {
124
- const gridSize = width + 1;
125
- const martini = new Martini(gridSize);
126
- const tile = martini.createTile(terrain);
127
- const {vertices, triangles} = tile.getMesh(meshMaxError);
128
-
129
- return {vertices, triangles};
130
- }
131
-
132
- /**
133
- * Get Delatin generated vertices and triangles
134
- *
135
- * @param {number} meshMaxError threshold for simplifying mesh
136
- * @param {number} width width of the input data array
137
- * @param {number} height height of the input data array
138
- * @param {number[] | Float32Array} terrain elevation data
139
- * @returns {{vertices: number[], triangles: number[]}} vertices and triangles data
140
- */
141
- function getDelatinTileMesh(meshMaxError, width, height, terrain) {
142
- const tin = new Delatin(terrain, width + 1, height + 1);
143
- tin.run(meshMaxError);
144
- // @ts-expect-error
145
- const {coords, triangles} = tin;
146
- const vertices = coords;
147
- return {vertices, triangles};
120
+ /** Adapts the padded, north-first image grid to upstream adaptive terrain meshing. */
121
+ function getTerrainTileMesh(
122
+ terrainOptions: TerrainOptions,
123
+ width: number,
124
+ height: number,
125
+ terrain: Float32Array,
126
+ tesselator: 'martini' | 'delatin'
127
+ ) {
128
+ const field = new HeightField({
129
+ width: width + 1,
130
+ height: height + 1,
131
+ heights: terrain,
132
+ rowStep: [0, -1]
133
+ });
134
+ // Fully refined RTIN triangles can inspect 32 neighboring grid nodes per source cell.
135
+ const options = {
136
+ maxError: terrainOptions.meshMaxError,
137
+ maxSampleTests: terrainOptions.meshMaxSampleTests ?? Math.max(10_000_000, 32 * terrain.length)
138
+ };
139
+ const mesh =
140
+ tesselator === 'martini'
141
+ ? createMartiniTerrainMesh(field, options)
142
+ : createDelatinTerrainMesh(field, options);
143
+ return {vertices: mesh.gridCoordinates, triangles: mesh.indices};
148
144
  }
149
145
 
150
146
  function getTerrain(
@@ -5,7 +5,7 @@
5
5
  import {
6
6
  serializeArrowWorkerResult,
7
7
  deserializeArrowWorkerResult
8
- } from '@loaders.gl/core/schema-utils/arrow-transport';
8
+ } from '@loaders.gl/core/arrow-worker-transport';
9
9
  import type {LoaderWithParser} from '@loaders.gl/core/loader-utils';
10
10
  import type {Mesh, MeshArrowTable} from '@loaders.gl/core/schema';
11
11
  import {convertMeshToTable, convertTableToMesh} from '@loaders.gl/core/schema-utils';
@@ -5,7 +5,7 @@
5
5
  import {
6
6
  serializeArrowWorkerResult,
7
7
  deserializeArrowWorkerResult
8
- } from '@loaders.gl/core/schema-utils/arrow-transport';
8
+ } from '@loaders.gl/core/arrow-worker-transport';
9
9
  import type {LoaderContext, LoaderWithParser} from '@loaders.gl/core/loader-utils';
10
10
  import {parseFromContext} from '@loaders.gl/core/loader-utils';
11
11
  import type {Mesh, MeshArrowTable} from '@loaders.gl/core/schema';
@@ -1,10 +0,0 @@
1
- # Martini
2
-
3
- martini.mjs is copied unchanged from the ISC-licensed @mapbox/martini 0.2.0
4
- package. See martini-LICENSE. Upstream: https://github.com/mapbox/martini/tree/v0.2.0
5
-
6
- SHA-256: 4df72465a6d168d419ad41593bbeea72025f0be083b01ce16372b071e316430c
7
-
8
- Retain the pinned development dependency for source verification when updating.
9
- The existing vendored-library coverage exclusion applies; terrain adapter tests
10
- exercise mesh generation through this implementation.
@@ -1,15 +0,0 @@
1
- ISC License
2
-
3
- Copyright (c) 2019, Mapbox
4
-
5
- Permission to use, copy, modify, and/or distribute this software for any purpose
6
- with or without fee is hereby granted, provided that the above copyright notice
7
- and this permission notice appear in all copies.
8
-
9
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
11
- FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
13
- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
14
- TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
15
- THIS SOFTWARE.
@@ -1,19 +0,0 @@
1
- /** Pinned Martini terrain tessellator. */
2
- export default class Martini {
3
- /** Creates a tessellator for a 2^n+1 grid. */
4
- constructor(gridSize?: number);
5
- /** Creates a tile from one padded elevation grid. */
6
- createTile(terrain: Float32Array): MartiniTile;
7
- }
8
- /** One heightmap tile's mesh decoder. */
9
- export interface MartiniTile {
10
- /** Builds mesh vertices and triangle indices at the requested maximum error. */
11
- getMesh(maxError?: number): MartiniMesh;
12
- }
13
- /** Generated integer-grid mesh buffers. */
14
- export interface MartiniMesh {
15
- /** Paired grid coordinates of each vertex. */
16
- readonly vertices: Uint16Array;
17
- /** Triangle vertex indices. */
18
- readonly triangles: Uint32Array;
19
- }
@@ -1,164 +0,0 @@
1
-
2
- export default class Martini {
3
- constructor(gridSize = 257) {
4
- this.gridSize = gridSize;
5
- const tileSize = gridSize - 1;
6
- if (tileSize & (tileSize - 1)) throw new Error(
7
- `Expected grid size to be 2^n+1, got ${gridSize}.`);
8
-
9
- this.numTriangles = tileSize * tileSize * 2 - 2;
10
- this.numParentTriangles = this.numTriangles - tileSize * tileSize;
11
-
12
- this.indices = new Uint32Array(this.gridSize * this.gridSize);
13
-
14
- // coordinates for all possible triangles in an RTIN tile
15
- this.coords = new Uint16Array(this.numTriangles * 4);
16
-
17
- // get triangle coordinates from its index in an implicit binary tree
18
- for (let i = 0; i < this.numTriangles; i++) {
19
- let id = i + 2;
20
- let ax = 0, ay = 0, bx = 0, by = 0, cx = 0, cy = 0;
21
- if (id & 1) {
22
- bx = by = cx = tileSize; // bottom-left triangle
23
- } else {
24
- ax = ay = cy = tileSize; // top-right triangle
25
- }
26
- while ((id >>= 1) > 1) {
27
- const mx = (ax + bx) >> 1;
28
- const my = (ay + by) >> 1;
29
-
30
- if (id & 1) { // left half
31
- bx = ax; by = ay;
32
- ax = cx; ay = cy;
33
- } else { // right half
34
- ax = bx; ay = by;
35
- bx = cx; by = cy;
36
- }
37
- cx = mx; cy = my;
38
- }
39
- const k = i * 4;
40
- this.coords[k + 0] = ax;
41
- this.coords[k + 1] = ay;
42
- this.coords[k + 2] = bx;
43
- this.coords[k + 3] = by;
44
- }
45
- }
46
-
47
- createTile(terrain) {
48
- return new Tile(terrain, this);
49
- }
50
- }
51
-
52
- class Tile {
53
- constructor(terrain, martini) {
54
- const size = martini.gridSize;
55
- if (terrain.length !== size * size) throw new Error(
56
- `Expected terrain data of length ${size * size} (${size} x ${size}), got ${terrain.length}.`);
57
-
58
- this.terrain = terrain;
59
- this.martini = martini;
60
- this.errors = new Float32Array(terrain.length);
61
- this.update();
62
- }
63
-
64
- update() {
65
- const {numTriangles, numParentTriangles, coords, gridSize: size} = this.martini;
66
- const {terrain, errors} = this;
67
-
68
- // iterate over all possible triangles, starting from the smallest level
69
- for (let i = numTriangles - 1; i >= 0; i--) {
70
- const k = i * 4;
71
- const ax = coords[k + 0];
72
- const ay = coords[k + 1];
73
- const bx = coords[k + 2];
74
- const by = coords[k + 3];
75
- const mx = (ax + bx) >> 1;
76
- const my = (ay + by) >> 1;
77
- const cx = mx + my - ay;
78
- const cy = my + ax - mx;
79
-
80
- // calculate error in the middle of the long edge of the triangle
81
- const interpolatedHeight = (terrain[ay * size + ax] + terrain[by * size + bx]) / 2;
82
- const middleIndex = my * size + mx;
83
- const middleError = Math.abs(interpolatedHeight - terrain[middleIndex]);
84
-
85
- errors[middleIndex] = Math.max(errors[middleIndex], middleError);
86
-
87
- if (i < numParentTriangles) { // bigger triangles; accumulate error with children
88
- const leftChildIndex = ((ay + cy) >> 1) * size + ((ax + cx) >> 1);
89
- const rightChildIndex = ((by + cy) >> 1) * size + ((bx + cx) >> 1);
90
- errors[middleIndex] = Math.max(errors[middleIndex], errors[leftChildIndex], errors[rightChildIndex]);
91
- }
92
- }
93
- }
94
-
95
- getMesh(maxError = 0) {
96
- const {gridSize: size, indices} = this.martini;
97
- const {errors} = this;
98
- let numVertices = 0;
99
- let numTriangles = 0;
100
- const max = size - 1;
101
-
102
- // use an index grid to keep track of vertices that were already used to avoid duplication
103
- indices.fill(0);
104
-
105
- // retrieve mesh in two stages that both traverse the error map:
106
- // - countElements: find used vertices (and assign each an index), and count triangles (for minimum allocation)
107
- // - processTriangle: fill the allocated vertices & triangles typed arrays
108
-
109
- function countElements(ax, ay, bx, by, cx, cy) {
110
- const mx = (ax + bx) >> 1;
111
- const my = (ay + by) >> 1;
112
-
113
- if (Math.abs(ax - cx) + Math.abs(ay - cy) > 1 && errors[my * size + mx] > maxError) {
114
- countElements(cx, cy, ax, ay, mx, my);
115
- countElements(bx, by, cx, cy, mx, my);
116
- } else {
117
- indices[ay * size + ax] = indices[ay * size + ax] || ++numVertices;
118
- indices[by * size + bx] = indices[by * size + bx] || ++numVertices;
119
- indices[cy * size + cx] = indices[cy * size + cx] || ++numVertices;
120
- numTriangles++;
121
- }
122
- }
123
- countElements(0, 0, max, max, max, 0);
124
- countElements(max, max, 0, 0, 0, max);
125
-
126
- const vertices = new Uint16Array(numVertices * 2);
127
- const triangles = new Uint32Array(numTriangles * 3);
128
- let triIndex = 0;
129
-
130
- function processTriangle(ax, ay, bx, by, cx, cy) {
131
- const mx = (ax + bx) >> 1;
132
- const my = (ay + by) >> 1;
133
-
134
- if (Math.abs(ax - cx) + Math.abs(ay - cy) > 1 && errors[my * size + mx] > maxError) {
135
- // triangle doesn't approximate the surface well enough; drill down further
136
- processTriangle(cx, cy, ax, ay, mx, my);
137
- processTriangle(bx, by, cx, cy, mx, my);
138
-
139
- } else {
140
- // add a triangle
141
- const a = indices[ay * size + ax] - 1;
142
- const b = indices[by * size + bx] - 1;
143
- const c = indices[cy * size + cx] - 1;
144
-
145
- vertices[2 * a] = ax;
146
- vertices[2 * a + 1] = ay;
147
-
148
- vertices[2 * b] = bx;
149
- vertices[2 * b + 1] = by;
150
-
151
- vertices[2 * c] = cx;
152
- vertices[2 * c + 1] = cy;
153
-
154
- triangles[triIndex++] = a;
155
- triangles[triIndex++] = b;
156
- triangles[triIndex++] = c;
157
- }
158
- }
159
- processTriangle(0, 0, max, max, max, 0);
160
- processTriangle(max, max, 0, 0, 0, max);
161
-
162
- return {vertices, triangles};
163
- }
164
- }
@@ -1,10 +0,0 @@
1
- # Martini
2
-
3
- martini.mjs is copied unchanged from the ISC-licensed @mapbox/martini 0.2.0
4
- package. See martini-LICENSE. Upstream: https://github.com/mapbox/martini/tree/v0.2.0
5
-
6
- SHA-256: 4df72465a6d168d419ad41593bbeea72025f0be083b01ce16372b071e316430c
7
-
8
- Retain the pinned development dependency for source verification when updating.
9
- The existing vendored-library coverage exclusion applies; terrain adapter tests
10
- exercise mesh generation through this implementation.
@@ -1,15 +0,0 @@
1
- ISC License
2
-
3
- Copyright (c) 2019, Mapbox
4
-
5
- Permission to use, copy, modify, and/or distribute this software for any purpose
6
- with or without fee is hereby granted, provided that the above copyright notice
7
- and this permission notice appear in all copies.
8
-
9
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
11
- FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
13
- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
14
- TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
15
- THIS SOFTWARE.
@@ -1,19 +0,0 @@
1
- /** Pinned Martini terrain tessellator. */
2
- export default class Martini {
3
- /** Creates a tessellator for a 2^n+1 grid. */
4
- constructor(gridSize?: number);
5
- /** Creates a tile from one padded elevation grid. */
6
- createTile(terrain: Float32Array): MartiniTile;
7
- }
8
- /** One heightmap tile's mesh decoder. */
9
- export interface MartiniTile {
10
- /** Builds mesh vertices and triangle indices at the requested maximum error. */
11
- getMesh(maxError?: number): MartiniMesh;
12
- }
13
- /** Generated integer-grid mesh buffers. */
14
- export interface MartiniMesh {
15
- /** Paired grid coordinates of each vertex. */
16
- readonly vertices: Uint16Array;
17
- /** Triangle vertex indices. */
18
- readonly triangles: Uint32Array;
19
- }
@@ -1,164 +0,0 @@
1
-
2
- export default class Martini {
3
- constructor(gridSize = 257) {
4
- this.gridSize = gridSize;
5
- const tileSize = gridSize - 1;
6
- if (tileSize & (tileSize - 1)) throw new Error(
7
- `Expected grid size to be 2^n+1, got ${gridSize}.`);
8
-
9
- this.numTriangles = tileSize * tileSize * 2 - 2;
10
- this.numParentTriangles = this.numTriangles - tileSize * tileSize;
11
-
12
- this.indices = new Uint32Array(this.gridSize * this.gridSize);
13
-
14
- // coordinates for all possible triangles in an RTIN tile
15
- this.coords = new Uint16Array(this.numTriangles * 4);
16
-
17
- // get triangle coordinates from its index in an implicit binary tree
18
- for (let i = 0; i < this.numTriangles; i++) {
19
- let id = i + 2;
20
- let ax = 0, ay = 0, bx = 0, by = 0, cx = 0, cy = 0;
21
- if (id & 1) {
22
- bx = by = cx = tileSize; // bottom-left triangle
23
- } else {
24
- ax = ay = cy = tileSize; // top-right triangle
25
- }
26
- while ((id >>= 1) > 1) {
27
- const mx = (ax + bx) >> 1;
28
- const my = (ay + by) >> 1;
29
-
30
- if (id & 1) { // left half
31
- bx = ax; by = ay;
32
- ax = cx; ay = cy;
33
- } else { // right half
34
- ax = bx; ay = by;
35
- bx = cx; by = cy;
36
- }
37
- cx = mx; cy = my;
38
- }
39
- const k = i * 4;
40
- this.coords[k + 0] = ax;
41
- this.coords[k + 1] = ay;
42
- this.coords[k + 2] = bx;
43
- this.coords[k + 3] = by;
44
- }
45
- }
46
-
47
- createTile(terrain) {
48
- return new Tile(terrain, this);
49
- }
50
- }
51
-
52
- class Tile {
53
- constructor(terrain, martini) {
54
- const size = martini.gridSize;
55
- if (terrain.length !== size * size) throw new Error(
56
- `Expected terrain data of length ${size * size} (${size} x ${size}), got ${terrain.length}.`);
57
-
58
- this.terrain = terrain;
59
- this.martini = martini;
60
- this.errors = new Float32Array(terrain.length);
61
- this.update();
62
- }
63
-
64
- update() {
65
- const {numTriangles, numParentTriangles, coords, gridSize: size} = this.martini;
66
- const {terrain, errors} = this;
67
-
68
- // iterate over all possible triangles, starting from the smallest level
69
- for (let i = numTriangles - 1; i >= 0; i--) {
70
- const k = i * 4;
71
- const ax = coords[k + 0];
72
- const ay = coords[k + 1];
73
- const bx = coords[k + 2];
74
- const by = coords[k + 3];
75
- const mx = (ax + bx) >> 1;
76
- const my = (ay + by) >> 1;
77
- const cx = mx + my - ay;
78
- const cy = my + ax - mx;
79
-
80
- // calculate error in the middle of the long edge of the triangle
81
- const interpolatedHeight = (terrain[ay * size + ax] + terrain[by * size + bx]) / 2;
82
- const middleIndex = my * size + mx;
83
- const middleError = Math.abs(interpolatedHeight - terrain[middleIndex]);
84
-
85
- errors[middleIndex] = Math.max(errors[middleIndex], middleError);
86
-
87
- if (i < numParentTriangles) { // bigger triangles; accumulate error with children
88
- const leftChildIndex = ((ay + cy) >> 1) * size + ((ax + cx) >> 1);
89
- const rightChildIndex = ((by + cy) >> 1) * size + ((bx + cx) >> 1);
90
- errors[middleIndex] = Math.max(errors[middleIndex], errors[leftChildIndex], errors[rightChildIndex]);
91
- }
92
- }
93
- }
94
-
95
- getMesh(maxError = 0) {
96
- const {gridSize: size, indices} = this.martini;
97
- const {errors} = this;
98
- let numVertices = 0;
99
- let numTriangles = 0;
100
- const max = size - 1;
101
-
102
- // use an index grid to keep track of vertices that were already used to avoid duplication
103
- indices.fill(0);
104
-
105
- // retrieve mesh in two stages that both traverse the error map:
106
- // - countElements: find used vertices (and assign each an index), and count triangles (for minimum allocation)
107
- // - processTriangle: fill the allocated vertices & triangles typed arrays
108
-
109
- function countElements(ax, ay, bx, by, cx, cy) {
110
- const mx = (ax + bx) >> 1;
111
- const my = (ay + by) >> 1;
112
-
113
- if (Math.abs(ax - cx) + Math.abs(ay - cy) > 1 && errors[my * size + mx] > maxError) {
114
- countElements(cx, cy, ax, ay, mx, my);
115
- countElements(bx, by, cx, cy, mx, my);
116
- } else {
117
- indices[ay * size + ax] = indices[ay * size + ax] || ++numVertices;
118
- indices[by * size + bx] = indices[by * size + bx] || ++numVertices;
119
- indices[cy * size + cx] = indices[cy * size + cx] || ++numVertices;
120
- numTriangles++;
121
- }
122
- }
123
- countElements(0, 0, max, max, max, 0);
124
- countElements(max, max, 0, 0, 0, max);
125
-
126
- const vertices = new Uint16Array(numVertices * 2);
127
- const triangles = new Uint32Array(numTriangles * 3);
128
- let triIndex = 0;
129
-
130
- function processTriangle(ax, ay, bx, by, cx, cy) {
131
- const mx = (ax + bx) >> 1;
132
- const my = (ay + by) >> 1;
133
-
134
- if (Math.abs(ax - cx) + Math.abs(ay - cy) > 1 && errors[my * size + mx] > maxError) {
135
- // triangle doesn't approximate the surface well enough; drill down further
136
- processTriangle(cx, cy, ax, ay, mx, my);
137
- processTriangle(bx, by, cx, cy, mx, my);
138
-
139
- } else {
140
- // add a triangle
141
- const a = indices[ay * size + ax] - 1;
142
- const b = indices[by * size + bx] - 1;
143
- const c = indices[cy * size + cx] - 1;
144
-
145
- vertices[2 * a] = ax;
146
- vertices[2 * a + 1] = ay;
147
-
148
- vertices[2 * b] = bx;
149
- vertices[2 * b + 1] = by;
150
-
151
- vertices[2 * c] = cx;
152
- vertices[2 * c + 1] = cy;
153
-
154
- triangles[triIndex++] = a;
155
- triangles[triIndex++] = b;
156
- triangles[triIndex++] = c;
157
- }
158
- }
159
- processTriangle(0, 0, max, max, max, 0);
160
- processTriangle(max, max, 0, 0, 0, max);
161
-
162
- return {vertices, triangles};
163
- }
164
- }