isard 0.0.0-stage → 0.2.1

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 (84) hide show
  1. package/CHANGELOG.md +176 -0
  2. package/LICENSE +21 -0
  3. package/README.md +150 -2
  4. package/dist/basemap.worker.d.ts +76 -0
  5. package/dist/basemap.worker.js +4 -0
  6. package/dist/basemap.worker.js.map +13 -0
  7. package/dist/basemaps.d.ts +48 -0
  8. package/dist/basemaps.js +97 -0
  9. package/dist/basemaps.js.map +1 -0
  10. package/dist/color.d.ts +8 -0
  11. package/dist/color.js +98 -0
  12. package/dist/color.js.map +1 -0
  13. package/dist/controls.d.ts +43 -0
  14. package/dist/controls.js +115 -0
  15. package/dist/controls.js.map +1 -0
  16. package/dist/decoder.worker.js +6 -0
  17. package/dist/decoder.worker.js.map +12 -0
  18. package/dist/decoders.d.ts +27 -0
  19. package/dist/decoders.js +109 -0
  20. package/dist/decoders.js.map +1 -0
  21. package/dist/equirectangular.d.ts +35 -0
  22. package/dist/equirectangular.js +143 -0
  23. package/dist/equirectangular.js.map +1 -0
  24. package/dist/extrusions.d.ts +79 -0
  25. package/dist/extrusions.js +224 -0
  26. package/dist/extrusions.js.map +1 -0
  27. package/dist/features.d.ts +159 -0
  28. package/dist/features.js +387 -0
  29. package/dist/features.js.map +1 -0
  30. package/dist/geoplateforme.d.ts +35 -0
  31. package/dist/geoplateforme.js +56 -0
  32. package/dist/geoplateforme.js.map +1 -0
  33. package/dist/glyphs.d.ts +104 -0
  34. package/dist/glyphs.js +383 -0
  35. package/dist/glyphs.js.map +1 -0
  36. package/dist/index.d.ts +34 -0
  37. package/dist/index.js +25 -0
  38. package/dist/index.js.map +1 -0
  39. package/dist/interaction.d.ts +53 -0
  40. package/dist/interaction.js +259 -0
  41. package/dist/interaction.js.map +1 -0
  42. package/dist/isard_bg.wasm +0 -0
  43. package/dist/isard_core_bg.wasm +0 -0
  44. package/dist/layers.d.ts +103 -0
  45. package/dist/layers.js +290 -0
  46. package/dist/layers.js.map +1 -0
  47. package/dist/loader.d.ts +30 -0
  48. package/dist/loader.js +102 -0
  49. package/dist/loader.js.map +1 -0
  50. package/dist/map.d.ts +211 -0
  51. package/dist/map.js +1222 -0
  52. package/dist/map.js.map +1 -0
  53. package/dist/mvt.d.ts +19 -0
  54. package/dist/mvt.js +231 -0
  55. package/dist/mvt.js.map +1 -0
  56. package/dist/overlay.d.ts +118 -0
  57. package/dist/overlay.js +334 -0
  58. package/dist/overlay.js.map +1 -0
  59. package/dist/protocol.d.ts +30 -0
  60. package/dist/protocol.js +2 -0
  61. package/dist/protocol.js.map +1 -0
  62. package/dist/style.d.ts +7 -0
  63. package/dist/style.js +130 -0
  64. package/dist/style.js.map +1 -0
  65. package/dist/symbols.d.ts +61 -0
  66. package/dist/symbols.js +238 -0
  67. package/dist/symbols.js.map +1 -0
  68. package/dist/terrain.d.ts +14 -0
  69. package/dist/terrain.js +56 -0
  70. package/dist/terrain.js.map +1 -0
  71. package/dist/types.d.ts +312 -0
  72. package/dist/types.js +2 -0
  73. package/dist/types.js.map +1 -0
  74. package/dist/vectorstyle.d.ts +83 -0
  75. package/dist/vectorstyle.js +518 -0
  76. package/dist/vectorstyle.js.map +1 -0
  77. package/dist/vectortiles.d.ts +124 -0
  78. package/dist/vectortiles.js +523 -0
  79. package/dist/vectortiles.js.map +1 -0
  80. package/dist/wasm/isard.d.ts +612 -0
  81. package/dist/wasm/isard.js +2623 -0
  82. package/dist/wasm/isard_core.d.ts +469 -0
  83. package/dist/wasm/isard_core.js +2385 -0
  84. package/package.json +49 -4
package/dist/layers.js ADDED
@@ -0,0 +1,290 @@
1
+ /**
2
+ * Handles of the layers added to a map. Each one keeps what it needs to be created again: after a WebGL context loss,
3
+ * the map recreates its engine and every layer in order, with the same handle (its id is updated).
4
+ */
5
+ import { parseColor, rampTexels, stops } from "./features.js";
6
+ export class Layer {
7
+ #id = -1;
8
+ /** @internal */
9
+ map;
10
+ /** @internal */
11
+ constructor(map) {
12
+ this.map = map;
13
+ }
14
+ /** Id of the layer in the engine */
15
+ get id() {
16
+ return this.#id;
17
+ }
18
+ /** @internal Creates the layer in an engine (again after a context loss); its id is -1 if that fails */
19
+ attach(engine) {
20
+ this.#id = -1;
21
+ this.#id = this.create(engine);
22
+ }
23
+ /** @internal Forgets its id in an engine that is gone (before the layers are created in a new one) */
24
+ forgetEngine() {
25
+ this.#id = -1;
26
+ }
27
+ /** @internal Sends what refers to other layers, once every layer of a new engine has its id */
28
+ relink(_engine) { }
29
+ remove() {
30
+ // Not created yet: a 3D Tiles layer waiting for the engine with 3D Tiles
31
+ if (this.#id >= 0)
32
+ this.map.engine.removeLayer(this.#id);
33
+ this.#id = -1;
34
+ this.map.detach(this);
35
+ this.map.requestRender();
36
+ }
37
+ /** Moves the layer under `other`, or on top of every layer (above the labels of a vector basemap too) */
38
+ moveBefore(other) {
39
+ this.map.moveLayer(this, other);
40
+ }
41
+ }
42
+ /** Raster tiles (basemaps, overlays) */
43
+ export class RasterLayer extends Layer {
44
+ options;
45
+ /** @internal */
46
+ constructor(map, options) {
47
+ super(map);
48
+ this.options = options;
49
+ }
50
+ setStyle(style, saturation = 1) {
51
+ const changed = typeof style === 'number' ? { opacity: style, saturation } : style;
52
+ this.options.opacity = changed.opacity ?? this.options.opacity;
53
+ this.options.saturation = changed.saturation ?? this.options.saturation;
54
+ this.map.engine.setRasterStyle(this.id, this.options.opacity, this.options.saturation);
55
+ this.map.requestRender();
56
+ }
57
+ /** @internal */
58
+ create(engine) {
59
+ const { url, minZoom, maxZoom, opacity, saturation, size, loader } = this.options;
60
+ return engine.addRaster(url, minZoom, maxZoom, opacity, saturation, size, loader);
61
+ }
62
+ }
63
+ /** Line network colored by a value per node (travel time, water depth…) */
64
+ export class NetworkLayer extends Layer {
65
+ geometry;
66
+ costs;
67
+ style;
68
+ edgeWidths;
69
+ values;
70
+ /** Node and line positions in Mercator, kept for a context loss: f32 offsets from an origin (exact to the centimetre
71
+ * across a region, half the memory of f64) */
72
+ kept;
73
+ /** @internal */
74
+ constructor(map, geometry, costs, style, edgeWidths) {
75
+ super(map);
76
+ this.geometry = geometry;
77
+ this.costs = costs;
78
+ this.style = style;
79
+ this.edgeWidths = edgeWidths;
80
+ }
81
+ /** One value per node, in node order; negative (or 1e30 and above) = unreached. Costs one texture upload, drawn
82
+ * next frame. */
83
+ setValues(values) {
84
+ this.values = values;
85
+ this.map.engine.setNetworkValues(this.id, values);
86
+ this.map.requestRender();
87
+ }
88
+ /** Costs of the edges, A → B then B → A (see `NetworkOptions.costs`), when the way the network is travelled
89
+ * changes; undefined: values interpolated along the edges. The geometry stays on the graphics card. */
90
+ setCosts(costs) {
91
+ this.costs = costs;
92
+ this.map.engine.setNetworkCosts(this.id, costs);
93
+ this.map.requestRender();
94
+ }
95
+ setStyle(style) {
96
+ const ramp = this.style.ramp;
97
+ this.style = { ...this.style, ...style };
98
+ this.map.engine.setNetworkStyle(this.id, networkStyle(this.style));
99
+ if (this.style.ramp !== ramp)
100
+ this.map.engine.setNetworkRamp(this.id, this.style.ramp && rampTexels(this.style.ramp).rgba);
101
+ this.map.requestRender();
102
+ }
103
+ /** Width factor of each edge (undefined: 1 for all) */
104
+ setEdgeWidths(widths) {
105
+ this.edgeWidths = widths;
106
+ this.map.engine.setNetworkScales(this.id, widths);
107
+ this.map.requestRender();
108
+ }
109
+ /** Pieces in all and drawn in the last frame */
110
+ stats() {
111
+ return this.map.engine.networkStats(this.id);
112
+ }
113
+ /** @internal */
114
+ create(engine) {
115
+ const { nodes, edges, lines } = this.geometry;
116
+ let mercator;
117
+ if (this.kept) {
118
+ const { origin, nodes: n, lines: l } = this.kept;
119
+ mercator = { nodes: unpack(n, origin), lines: l && unpack(l, origin) };
120
+ }
121
+ else {
122
+ // Mercator here: Math.tan and Math.log run natively, several times faster than in WebAssembly
123
+ mercator = { nodes: toMercator(nodes), lines: lines && toMercator(lines.points) };
124
+ const origin = [mercator.nodes[0] ?? 0, mercator.nodes[1] ?? 0];
125
+ this.kept = {
126
+ origin,
127
+ nodes: pack(mercator.nodes, origin),
128
+ lines: mercator.lines && pack(mercator.lines, origin),
129
+ };
130
+ // The degrees are not needed again: millions of points for a region
131
+ this.geometry = { nodes: EMPTY, edges, lines: lines && { start: lines.start, points: EMPTY } };
132
+ }
133
+ const id = engine.addNetwork(mercator.nodes, edges, lines?.start, mercator.lines, this.costs, networkStyle(this.style));
134
+ if (this.style.ramp)
135
+ engine.setNetworkRamp(id, rampTexels(this.style.ramp).rgba);
136
+ if (this.edgeWidths)
137
+ engine.setNetworkScales(id, this.edgeWidths);
138
+ if (this.values)
139
+ engine.setNetworkValues(id, this.values);
140
+ return id;
141
+ }
142
+ }
143
+ /** The 25 numbers of a network style for the engine */
144
+ function networkStyle(style) {
145
+ const { width, halo, max, contour, opacity, haloOpacity, unreachedColor, fade } = style;
146
+ return Float32Array.from([
147
+ ...stops(width),
148
+ ...stops(halo),
149
+ max,
150
+ contour,
151
+ opacity,
152
+ haloOpacity,
153
+ ...parseColor(unreachedColor),
154
+ fade,
155
+ ]);
156
+ }
157
+ const EMPTY = new Float64Array(0);
158
+ /** Interleaved points as f32 offsets from an origin, and back */
159
+ function pack(points, [ox, oy]) {
160
+ const out = new Float32Array(points.length);
161
+ for (let i = 0; i < points.length; i += 2) {
162
+ out[i] = (points[i] ?? 0) - ox;
163
+ out[i + 1] = (points[i + 1] ?? 0) - oy;
164
+ }
165
+ return out;
166
+ }
167
+ function unpack(points, [ox, oy]) {
168
+ const out = new Float64Array(points.length);
169
+ for (let i = 0; i < points.length; i += 2) {
170
+ out[i] = (points[i] ?? 0) + ox;
171
+ out[i + 1] = (points[i + 1] ?? 0) + oy;
172
+ }
173
+ return out;
174
+ }
175
+ const MAX_LAT = 85.051128779806589;
176
+ /** Longitudes and latitudes (degrees, interleaved) to Mercator [0, 1], y towards the south */
177
+ function toMercator(lonLat) {
178
+ const out = new Float64Array(lonLat.length);
179
+ for (let i = 0; i < lonLat.length; i += 2) {
180
+ const lat = (Math.max(-MAX_LAT, Math.min(MAX_LAT, lonLat[i + 1] ?? 0)) * Math.PI) / 180;
181
+ out[i] = ((lonLat[i] ?? 0) + 180) / 360;
182
+ out[i + 1] = 0.5 - Math.log(Math.tan(Math.PI / 4 + lat / 2)) / (2 * Math.PI);
183
+ }
184
+ return out;
185
+ }
186
+ /** Textured meshes in 3D Tiles */
187
+ export class Tiles3dLayer extends Layer {
188
+ url;
189
+ style;
190
+ /** @internal */
191
+ constructor(map, url, style) {
192
+ super(map);
193
+ this.url = url;
194
+ this.style = style;
195
+ }
196
+ setStyle(style) {
197
+ this.style = { ...this.style, ...style };
198
+ // Kept for its creation while the engine with 3D Tiles loads
199
+ if (this.id < 0)
200
+ return;
201
+ this.map.engine.setTiles3dStyle(this.id, this.style.maxScreenSpaceError, this.style.opacity);
202
+ this.map.requestRender();
203
+ }
204
+ /** Loading counters; undefined once the layer is removed, and while the engine with 3D Tiles loads (see
205
+ * `GeoMapOptions.tiles3d`) */
206
+ stats() {
207
+ return this.id < 0 ? undefined : this.map.engine.tiles3dStats(this.id);
208
+ }
209
+ /** @internal */
210
+ create(engine) {
211
+ const id = engine.addTiles3d(this.url, this.style.maxScreenSpaceError);
212
+ engine.setTiles3dStyle(id, this.style.maxScreenSpaceError, this.style.opacity);
213
+ return id;
214
+ }
215
+ }
216
+ const bytesOf = (data) => new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
217
+ /** The application's fragment shader run over the ground, draped on the relief (water depth, heat maps…) */
218
+ export class GroundLayer extends Layer {
219
+ options;
220
+ textures = new Map();
221
+ uniforms = new Map();
222
+ visible = true;
223
+ /** @internal */
224
+ constructor(map, options) {
225
+ super(map);
226
+ this.options = options;
227
+ for (const [name, texture] of Object.entries(options.textures ?? {}))
228
+ this.textures.set(name, texture);
229
+ for (const [name, value] of Object.entries(options.uniforms ?? {}))
230
+ this.uniforms.set(name, [value].flat());
231
+ }
232
+ /** Creates or replaces a texture of the code's sampler `name` */
233
+ setTexture(name, texture) {
234
+ this.textures.set(name, texture);
235
+ this.sendTexture(this.map.engine, this.id, name, texture);
236
+ this.map.requestRender();
237
+ }
238
+ /**
239
+ * Replaces a region of a texture: `data` holds `width` × `height` texels for (`x`, `y`) of `layer` (2D arrays).
240
+ * Keep the texture's own `data` up to date too if it must survive a WebGL context loss.
241
+ */
242
+ updateTexture(name, data, region) {
243
+ const { x = 0, y = 0, layer = 0, width, height } = region;
244
+ this.map.engine.updateGroundTexture(this.id, name, x, y, layer, width, height, bytesOf(data));
245
+ this.map.requestRender();
246
+ }
247
+ /** A uniform of the code: a number, or numbers for vectors, arrays and matrices (column major) */
248
+ setUniform(name, value) {
249
+ const values = [value].flat();
250
+ this.uniforms.set(name, values);
251
+ this.map.engine.setGroundUniform(this.id, name, Float64Array.from(values));
252
+ this.map.requestRender();
253
+ }
254
+ setBounds(bounds) {
255
+ this.options.bounds = bounds;
256
+ this.map.engine.setGroundBounds(this.id, ...mercatorBox(bounds));
257
+ this.map.requestRender();
258
+ }
259
+ setVisible(visible) {
260
+ this.visible = visible;
261
+ this.map.engine.setGroundVisible(this.id, visible);
262
+ this.map.requestRender();
263
+ }
264
+ sendTexture(engine, id, name, t) {
265
+ const data = t.data && bytesOf(t.data);
266
+ engine.setGroundTexture(id, name, t.format, t.width, t.height, t.depth ?? 0, t.linear ?? false, data);
267
+ }
268
+ /** @internal */
269
+ create(engine) {
270
+ const [ax, ay] = mercatorPoint(this.options.anchor);
271
+ const id = engine.addGround(this.options.code, ax, ay, ...mercatorBox(this.options.bounds ?? [-180, -85.06, 180, 85.06]));
272
+ for (const [name, texture] of this.textures)
273
+ this.sendTexture(engine, id, name, texture);
274
+ for (const [name, values] of this.uniforms)
275
+ engine.setGroundUniform(id, name, Float64Array.from(values));
276
+ engine.setGroundVisible(id, this.visible);
277
+ return id;
278
+ }
279
+ }
280
+ function mercatorPoint([lon, lat]) {
281
+ const out = toMercator(Float64Array.of(lon, lat));
282
+ return [out[0] ?? 0, out[1] ?? 0];
283
+ }
284
+ /** A lon/lat box to Mercator min x, min y, max x, max y (y towards the south) */
285
+ function mercatorBox([west, south, east, north]) {
286
+ const [x0, y1] = mercatorPoint([west, south]);
287
+ const [x1, y0] = mercatorPoint([east, north]);
288
+ return [x0, y0, x1, y1];
289
+ }
290
+ //# sourceMappingURL=layers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"layers.js","sourceRoot":"","sources":["../src/layers.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAY9D,MAAM,OAAgB,KAAK;IACzB,GAAG,GAAG,CAAC,CAAC,CAAC;IAET,gBAAgB;IACG,GAAG,CAAY;IAElC,gBAAgB;IAChB,YAAY,GAAc;QACxB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,CAAC;IAED,oCAAoC;IACpC,IAAI,EAAE;QACJ,OAAO,IAAI,CAAC,GAAG,CAAC;IAClB,CAAC;IAED,wGAAwG;IACxG,MAAM,CAAC,MAAc;QACnB,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC;QACd,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACjC,CAAC;IAED,sGAAsG;IACtG,YAAY;QACV,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC;IAChB,CAAC;IAED,+FAA+F;IAC/F,MAAM,CAAC,OAAe,IAAS,CAAC;IAKhC,MAAM;QACJ,yEAAyE;QACzE,IAAI,IAAI,CAAC,GAAG,IAAI,CAAC;YAAE,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACzD,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC;QACd,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACtB,IAAI,CAAC,GAAG,CAAC,aAAa,EAAE,C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E,GAAG,CAAC,CAAC,CAAC;IAClD,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;AACpC,CAAC;AAED,iFAAiF;AACjF,SAAS,WAAW,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,CAAmC;IAC/E,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG,aAAa,CAAC,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;IAC9C,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG,aAAa,CAAC,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;IAC9C,OAAO,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;AAC1B,CAAC","sourcesContent":["/**\n * Handles of the layers added to a map. Each one keeps what it needs to be created again: after a WebGL context loss,\n * the map recreates its engine and every layer in order, with the same handle (its id is updated).\n */\nimport { parseColor, rampTexels, stops } from './features.ts';\nimport type {\n LayerHost,\n LonLat,\n NetworkOptions,\n NetworkStyle,\n RasterOptions,\n RasterStyle,\n Tiles3dStyle,\n} from './types.ts';\nimport type { Engine, NetworkStats, Tiles3dStats } from './wasm/isard.js';\n\nexport abstract class Layer {\n #id = -1;\n\n /** @internal */\n protected readonly map: LayerHost;\n\n /** @internal */\n constructor(map: LayerHost) {\n this.map = map;\n }\n\n /** Id of the layer in the engine */\n get id(): number {\n return this.#id;\n }\n\n /** @internal Creates the layer in an engine (again after a context loss); its id is -1 if that fails */\n attach(engine: Engine): void {\n this.#id = -1;\n this.#id = this.create(engine);\n }\n\n /** @internal Forgets its id in an engine that is gone (before the layers are created in a new one) */\n forgetEngine(): void {\n this.#id = -1;\n }\n\n /** @internal Sends what refers to other layers, once every layer of a new engine has its id */\n relink(_engine: Engine): void {}\n\n /** @internal */\n protected abstract create(engine: Engine): number;\n\n remove(): void {\n // Not created yet: a 3D Tiles layer waiting for the engine with 3D Tiles\n if (this.#id >= 0) this.map.engine.removeLayer(this.#id);\n this.#id = -1;\n this.map.detach(this);\n this.map.requestRender();\n }\n\n /** Moves the layer under `other`, or on top of every layer (above the labels of a vector basemap too) */\n moveBefore(other: Layer | undefined): void {\n this.map.moveLayer(this, other);\n }\n}\n\n/** Raster tiles (basemaps, overlays) */\nexport class RasterLayer extends Layer {\n /** @internal */\n constructor(\n map: LayerHost,\n private readonly options: Required<Omit<RasterOptions, 'size' | 'loader' | 'grid' | 'attribution'>> & {\n size: number | undefined;\n loader: RasterOptions['loader'] | undefined;\n },\n ) {\n super(map);\n }\n\n /** Changes the opacity or saturation, the other kept */\n setStyle(style: Partial<RasterStyle>): void;\n /** @deprecated Positional form (saturation back to 1 when not given): `setStyle({ opacity, saturation })` */\n setStyle(opacity: number, saturation?: number): void;\n setStyle(style: Partial<RasterStyle> | number, saturation = 1): void {\n const changed = typeof style === 'number' ? { opacity: style, saturation } : style;\n this.options.opacity = changed.opacity ?? this.options.opacity;\n this.options.saturation = changed.saturation ?? this.options.saturation;\n this.map.engine.setRasterStyle(this.id, this.options.opacity, this.options.saturation);\n this.map.requestRender();\n }\n\n /** @internal */\n protected create(engine: Engine): number {\n const { url, minZoom, maxZoom, opacity, saturation, size, loader } = this.options;\n return engine.addRaster(url, minZoom, maxZoom, opacity, saturation, size, loader);\n }\n}\n\n/** Line network colored by a value per node (travel time, water depth…) */\nexport class NetworkLayer extends Layer {\n private values: Float32Array | undefined;\n /** Node and line positions in Mercator, kept for a context loss: f32 offsets from an origin (exact to the centimetre\n * across a region, half the memory of f64) */\n private kept: { origin: [number, number]; nodes: Float32Array; lines: Float32Array | undefined } | undefined;\n\n /** @internal */\n constructor(\n map: LayerHost,\n private geometry: { nodes: Float64Array; edges: Uint32Array; lines: NetworkOptions['lines'] | undefined },\n private costs: Float32Array | undefined,\n private style: NetworkStyle,\n private edgeWidths: Float32Array | undefined,\n ) {\n super(map);\n }\n\n /** One value per node, in node order; negative (or 1e30 and above) = unreached. Costs one texture upload, drawn\n * next frame. */\n setValues(values: Float32Array): void {\n this.values = values;\n this.map.engine.setNetworkValues(this.id, values);\n this.map.requestRender();\n }\n\n /** Costs of the edges, A → B then B → A (see `NetworkOptions.costs`), when the way the network is travelled\n * changes; undefined: values interpolated along the edges. The geometry stays on the graphics card. */\n setCosts(costs: Float32Array | undefined): void {\n this.costs = costs;\n this.map.engine.setNetworkCosts(this.id, costs);\n this.map.requestRender();\n }\n\n setStyle(style: Partial<NetworkStyle>): void {\n const ramp = this.style.ramp;\n this.style = { ...this.style, ...style };\n this.map.engine.setNetworkStyle(this.id, networkStyle(this.style));\n if (this.style.ramp !== ramp)\n this.map.engine.setNetworkRamp(this.id, this.style.ramp && rampTexels(this.style.ramp).rgba);\n this.map.requestRender();\n }\n\n /** Width factor of each edge (undefined: 1 for all) */\n setEdgeWidths(widths: Float32Array | undefined): void {\n this.edgeWidths = widths;\n this.map.engine.setNetworkScales(this.id, widths);\n this.map.requestRender();\n }\n\n /** Pieces in all and drawn in the last frame */\n stats(): NetworkStats | undefined {\n return this.map.engine.networkStats(this.id);\n }\n\n /** @internal */\n protected create(engine: Engine): number {\n const { nodes, edges, lines } = this.geometry;\n let mercator: { nodes: Float64Array; lines: Float64Array | undefined };\n if (this.kept) {\n const { origin, nodes: n, lines: l } = this.kept;\n mercator = { nodes: unpack(n, origin), lines: l && unpack(l, origin) };\n } else {\n // Mercator here: Math.tan and Math.log run natively, several times faster than in WebAssembly\n mercator = { nodes: toMercator(nodes), lines: lines && toMercator(lines.points) };\n const origin: [number, number] = [mercator.nodes[0] ?? 0, mercator.nodes[1] ?? 0];\n this.kept = {\n origin,\n nodes: pack(mercator.nodes, origin),\n lines: mercator.lines && pack(mercator.lines, origin),\n };\n // The degrees are not needed again: millions of points for a region\n this.geometry = { nodes: EMPTY, edges, lines: lines && { start: lines.start, points: EMPTY } };\n }\n const id = engine.addNetwork(\n mercator.nodes,\n edges,\n lines?.start,\n mercator.lines,\n this.costs,\n networkStyle(this.style),\n );\n if (this.style.ramp) engine.setNetworkRamp(id, rampTexels(this.style.ramp).rgba);\n if (this.edgeWidths) engine.setNetworkScales(id, this.edgeWidths);\n if (this.values) engine.setNetworkValues(id, this.values);\n return id;\n }\n}\n\n/** The 25 numbers of a network style for the engine */\nfunction networkStyle(style: NetworkStyle): Float32Array {\n const { width, halo, max, contour, opacity, haloOpacity, unreachedColor, fade } = style;\n return Float32Array.from([\n ...stops(width),\n ...stops(halo),\n max,\n contour,\n opacity,\n haloOpacity,\n ...parseColor(unreachedColor),\n fade,\n ]);\n}\n\nconst EMPTY = new Float64Array(0);\n\n/** Interleaved points as f32 offsets from an origin, and back */\nfunction pack(points: Float64Array, [ox, oy]: [number, number]): Float32Array {\n const out = new Float32Array(points.length);\n for (let i = 0; i < points.length; i += 2) {\n out[i] = (points[i] ?? 0) - ox;\n out[i + 1] = (points[i + 1] ?? 0) - oy;\n }\n return out;\n}\n\nfunction unpack(points: Float32Array, [ox, oy]: [number, number]): Float64Array {\n const out = new Float64Array(points.length);\n for (let i = 0; i < points.length; i += 2) {\n out[i] = (points[i] ?? 0) + ox;\n out[i + 1] = (points[i + 1] ?? 0) + oy;\n }\n return out;\n}\n\nconst MAX_LAT = 85.051128779806589;\n\n/** Longitudes and latitudes (degrees, interleaved) to Mercator [0, 1], y towards the south */\nfunction toMercator(lonLat: Float64Array): Float64Array {\n const out = new Float64Array(lonLat.length);\n for (let i = 0; i < lonLat.length; i += 2) {\n const lat = (Math.max(-MAX_LAT, Math.min(MAX_LAT, lonLat[i + 1] ?? 0)) * Math.PI) / 180;\n out[i] = ((lonLat[i] ?? 0) + 180) / 360;\n out[i + 1] = 0.5 - Math.log(Math.tan(Math.PI / 4 + lat / 2)) / (2 * Math.PI);\n }\n return out;\n}\n\n/** Textured meshes in 3D Tiles */\nexport class Tiles3dLayer extends Layer {\n /** @internal */\n constructor(\n map: LayerHost,\n private readonly url: string,\n private style: Tiles3dStyle,\n ) {\n super(map);\n }\n\n setStyle(style: Partial<Tiles3dStyle>): void {\n this.style = { ...this.style, ...style };\n // Kept for its creation while the engine with 3D Tiles loads\n if (this.id < 0) return;\n this.map.engine.setTiles3dStyle(this.id, this.style.maxScreenSpaceError, this.style.opacity);\n this.map.requestRender();\n }\n\n /** Loading counters; undefined once the layer is removed, and while the engine with 3D Tiles loads (see\n * `GeoMapOptions.tiles3d`) */\n stats(): Tiles3dStats | undefined {\n return this.id < 0 ? undefined : this.map.engine.tiles3dStats(this.id);\n }\n\n /** @internal */\n protected create(engine: Engine): number {\n const id = engine.addTiles3d(this.url, this.style.maxScreenSpaceError);\n engine.setTiles3dStyle(id, this.style.maxScreenSpaceError, this.style.opacity);\n return id;\n }\n}\n\n/** A texture of a ground layer (see `GroundLayer`) */\nexport interface GroundTexture {\n /** `r8`, `r8ui`, `r16ui`, `r16i`, `r32ui`, `r32f`, `rg16ui`, `rg32f`, `rgba8`, `rgba8ui`, `rgba16ui`, `rgba32f`:\n * integer formats are read with `usampler2D` / `isampler2D` and `texelFetch` */\n format: string;\n width: number;\n height: number;\n /** Layers of a 2D array (`sampler2DArray`, `usampler2DArray`…); 0 or undefined: a 2D texture */\n depth?: number;\n /** Linear filtering (non-integer formats); default nearest */\n linear?: boolean;\n /** Texels row after row (layer after layer), any typed array; undefined: left empty */\n data?: ArrayBufferView;\n}\n\nexport interface GroundOptions {\n /**\n * GLSL ES 3.0 defining `vec4 ground(vec2 offset)`: the color of the ground (premultiplied alpha, 0 for nothing) at\n * `offset`, the Mercator offset of the pixel from `anchor` (times `ISARD_EARTH`: metres of EPSG:3857), exact to\n * the millimetre at any zoom. The code declares its own uniforms and samplers; also there: `u_zoom`,\n * `u_pixel_ratio`\n */\n code: string;\n /** Where the offsets are taken from */\n anchor: LonLat;\n /** Box it shows in, `[west, south, east, north]` (default the world): only the ground there is drawn again */\n bounds?: [west: number, south: number, east: number, north: number];\n textures?: Record<string, GroundTexture>;\n uniforms?: Record<string, number | readonly number[]>;\n}\n\nconst bytesOf = (data: ArrayBufferView) => new Uint8Array(data.buffer, data.byteOffset, data.byteLength);\n\n/** The application's fragment shader run over the ground, draped on the relief (water depth, heat maps…) */\nexport class GroundLayer extends Layer {\n private readonly textures = new Map<string, GroundTexture>();\n private readonly uniforms = new Map<string, number[]>();\n private visible = true;\n\n /** @internal */\n constructor(\n map: LayerHost,\n private readonly options: GroundOptions,\n ) {\n super(map);\n for (const [name, texture] of Object.entries(options.textures ?? {})) this.textures.set(name, texture);\n for (const [name, value] of Object.entries(options.uniforms ?? {})) this.uniforms.set(name, [value].flat());\n }\n\n /** Creates or replaces a texture of the code's sampler `name` */\n setTexture(name: string, texture: GroundTexture): void {\n this.textures.set(name, texture);\n this.sendTexture(this.map.engine, this.id, name, texture);\n this.map.requestRender();\n }\n\n /**\n * Replaces a region of a texture: `data` holds `width` × `height` texels for (`x`, `y`) of `layer` (2D arrays).\n * Keep the texture's own `data` up to date too if it must survive a WebGL context loss.\n */\n updateTexture(\n name: string,\n data: ArrayBufferView,\n region: { x?: number; y?: number; layer?: number; width: number; height: number },\n ): void {\n const { x = 0, y = 0, layer = 0, width, height } = region;\n this.map.engine.updateGroundTexture(this.id, name, x, y, layer, width, height, bytesOf(data));\n this.map.requestRender();\n }\n\n /** A uniform of the code: a number, or numbers for vectors, arrays and matrices (column major) */\n setUniform(name: string, value: number | readonly number[]): void {\n const values = [value].flat();\n this.uniforms.set(name, values);\n this.map.engine.setGroundUniform(this.id, name, Float64Array.from(values));\n this.map.requestRender();\n }\n\n setBounds(bounds: [west: number, south: number, east: number, north: number]): void {\n this.options.bounds = bounds;\n this.map.engine.setGroundBounds(this.id, ...mercatorBox(bounds));\n this.map.requestRender();\n }\n\n setVisible(visible: boolean): void {\n this.visible = visible;\n this.map.engine.setGroundVisible(this.id, visible);\n this.map.requestRender();\n }\n\n private sendTexture(engine: Engine, id: number, name: string, t: GroundTexture): void {\n const data = t.data && bytesOf(t.data);\n engine.setGroundTexture(id, name, t.format, t.width, t.height, t.depth ?? 0, t.linear ?? false, data);\n }\n\n /** @internal */\n protected create(engine: Engine): number {\n const [ax, ay] = mercatorPoint(this.options.anchor);\n const id = engine.addGround(\n this.options.code,\n ax,\n ay,\n ...mercatorBox(this.options.bounds ?? [-180, -85.06, 180, 85.06]),\n );\n for (const [name, texture] of this.textures) this.sendTexture(engine, id, name, texture);\n for (const [name, values] of this.uniforms) engine.setGroundUniform(id, name, Float64Array.from(values));\n engine.setGroundVisible(id, this.visible);\n return id;\n }\n}\n\nfunction mercatorPoint([lon, lat]: LonLat): [number, number] {\n const out = toMercator(Float64Array.of(lon, lat));\n return [out[0] ?? 0, out[1] ?? 0];\n}\n\n/** A lon/lat box to Mercator min x, min y, max x, max y (y towards the south) */\nfunction mercatorBox([west, south, east, north]: [number, number, number, number]): [number, number, number, number] {\n const [x0, y1] = mercatorPoint([west, south]);\n const [x1, y0] = mercatorPoint([east, north]);\n return [x0, y0, x1, y1];\n}\n"]}
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Pool of decoding workers for 3D Tiles content. The engine calls `load` and `abort` (`TileLoaderLike`); the main
3
+ * thread then only uploads the decoded arrays to the GPU.
4
+ */
5
+ import type { DecoderUrls } from './decoders.ts';
6
+ import type { DecodedTile, TextureFormats, TileLoaderLike } from './wasm/isard.js';
7
+ export declare class TileLoader implements TileLoaderLike {
8
+ private readonly module;
9
+ private readonly workerUrl;
10
+ private readonly size;
11
+ private readonly decoders;
12
+ private readonly formats;
13
+ private workers;
14
+ /** Requests in flight per worker */
15
+ private readonly busy;
16
+ private readonly pending;
17
+ /** Every worker failed: why */
18
+ private failed;
19
+ constructor(module: WebAssembly.Module, workerUrl: string | URL, size: number, decoders: DecoderUrls, formats: TextureFormats);
20
+ /** Called by the engine: fetch and decode off the main thread, on the least busy worker */
21
+ load(id: number, url: string, transform: Float64Array, yUp: boolean, reload: boolean, priority?: 'high' | 'low'): Promise<DecodedTile>;
22
+ abort(id: number): void;
23
+ destroy(): void;
24
+ /** Workers start with the first tile: a map without 3D Tiles costs nothing */
25
+ private leastBusy;
26
+ private send;
27
+ private receive;
28
+ /** Rejects the requests of a failed worker */
29
+ private fail;
30
+ }
package/dist/loader.js ADDED
@@ -0,0 +1,102 @@
1
+ export class TileLoader {
2
+ module;
3
+ workerUrl;
4
+ size;
5
+ decoders;
6
+ formats;
7
+ workers = [];
8
+ /** Requests in flight per worker */
9
+ busy = new Map();
10
+ pending = new Map();
11
+ /** Every worker failed: why */
12
+ failed;
13
+ constructor(module, workerUrl, size, decoders, formats) {
14
+ this.module = module;
15
+ this.workerUrl = workerUrl;
16
+ this.size = size;
17
+ this.decoders = decoders;
18
+ this.formats = formats;
19
+ }
20
+ /** Called by the engine: fetch and decode off the main thread, on the least busy worker */
21
+ load(id, url, transform, yUp, reload, priority) {
22
+ if (this.failed)
23
+ return Promise.reject(this.failed);
24
+ const worker = this.leastBusy();
25
+ this.busy.set(worker, (this.busy.get(worker) ?? 0) + 1);
26
+ return new Promise((resolve, reject) => {
27
+ this.pending.set(id, { resolve, reject, worker });
28
+ this.send(worker, { type: 'load', id, url, transform, yUp, reload, ...(priority ? { priority } : {}) });
29
+ });
30
+ }
31
+ abort(id) {
32
+ const pending = this.pending.get(id);
33
+ if (pending)
34
+ this.send(pending.worker, { type: 'abort', id });
35
+ }
36
+ destroy() {
37
+ for (const worker of this.workers)
38
+ worker.terminate();
39
+ this.workers = [];
40
+ this.busy.clear();
41
+ for (const { reject } of this.pending.values())
42
+ reject(new Error('loader destroyed'));
43
+ this.pending.clear();
44
+ }
45
+ /** Workers start with the first tile: a map without 3D Tiles costs nothing */
46
+ leastBusy() {
47
+ if (this.workers.length === 0) {
48
+ for (let i = 0; i < this.size; i++) {
49
+ const worker = new Worker(this.workerUrl, { type: 'module' });
50
+ worker.onmessage = (event) => this.receive(worker, event.data);
51
+ // A worker that fails to load (a bundler serving it from elsewhere) or to read a message fails its requests
52
+ worker.onerror = (event) => {
53
+ event.preventDefault();
54
+ this.fail(worker, `the decoding worker ${this.workerUrl} failed${event.message ? `: ${event.message}` : ''}`);
55
+ };
56
+ worker.onmessageerror = () => this.fail(worker, `the decoding worker ${this.workerUrl} sent an unreadable message`);
57
+ this.send(worker, { type: 'init', module: this.module, decoders: this.decoders, formats: this.formats });
58
+ this.workers.push(worker);
59
+ this.busy.set(worker, 0);
60
+ }
61
+ }
62
+ let best = this.workers[0];
63
+ for (const worker of this.workers) {
64
+ if ((this.busy.get(worker) ?? 0) < (this.busy.get(best) ?? 0))
65
+ best = worker;
66
+ }
67
+ return best;
68
+ }
69
+ send(worker, message) {
70
+ worker.postMessage(message);
71
+ }
72
+ receive(worker, reply) {
73
+ this.busy.set(worker, Math.max(0, (this.busy.get(worker) ?? 1) - 1));
74
+ const pending = this.pending.get(reply.id);
75
+ if (!pending)
76
+ return;
77
+ this.pending.delete(reply.id);
78
+ if (reply.type === 'tile')
79
+ pending.resolve(reply.tile);
80
+ else if (reply.aborted)
81
+ pending.reject(new DOMException(`tile ${reply.id} aborted`, 'AbortError'));
82
+ else
83
+ pending.reject(new Error(reply.error));
84
+ }
85
+ /** Rejects the requests of a failed worker */
86
+ fail(worker, message) {
87
+ const error = new Error(`isard: ${message}. If a bundler moves the worker, give its URL in the \`workerUrl\` option (see "With a bundler" in the guide)`);
88
+ for (const [id, pending] of this.pending) {
89
+ if (pending.worker !== worker)
90
+ continue;
91
+ this.pending.delete(id);
92
+ pending.reject(error);
93
+ }
94
+ // Out of the pool: when none is left, the next requests fail at once
95
+ worker.terminate();
96
+ this.workers = this.workers.filter((w) => w !== worker);
97
+ this.busy.delete(worker);
98
+ if (this.workers.length === 0)
99
+ this.failed = error;
100
+ }
101
+ }
102
+ //# sourceMappingURL=loader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loader.js","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAcA,MAAM,OAAO,UAAU;IASF;IACA;IACA;IACA;IACA;IAZX,OAAO,GAAa,EAAE,CAAC;IAC/B,oCAAoC;I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QACzB,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC;YAAE,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACrD,CAAC;CACF","sourcesContent":["/**\n * Pool of decoding workers for 3D Tiles content. The engine calls `load` and `abort` (`TileLoaderLike`); the main\n * thread then only uploads the decoded arrays to the GPU.\n */\nimport type { DecoderUrls } from './decoders.ts';\nimport type { WorkerReply, WorkerRequest } from './protocol.ts';\nimport type { DecodedTile, TextureFormats, TileLoaderLike } from './wasm/isard.js';\n\ninterface Pending {\n resolve: (tile: DecodedTile) => void;\n reject: (error: Error) => void;\n worker: Worker;\n}\n\nexport class TileLoader implements TileLoaderLike {\n private workers: Worker[] = [];\n /** Requests in flight per worker */\n private readonly busy = new Map<Worker, number>();\n private readonly pending = new Map<number, Pending>();\n /** Every worker failed: why */\n private failed: Error | undefined;\n\n constructor(\n private readonly module: WebAssembly.Module,\n private readonly workerUrl: string | URL,\n private readonly size: number,\n private readonly decoders: DecoderUrls,\n private readonly formats: TextureFormats,\n ) {}\n\n /** Called by the engine: fetch and decode off the main thread, on the least busy worker */\n load(\n id: number,\n url: string,\n transform: Float64Array,\n yUp: boolean,\n reload: boolean,\n priority?: 'high' | 'low',\n ): Promise<DecodedTile> {\n if (this.failed) return Promise.reject(this.failed);\n const worker = this.leastBusy();\n this.busy.set(worker, (this.busy.get(worker) ?? 0) + 1);\n return new Promise((resolve, reject) => {\n this.pending.set(id, { resolve, reject, worker });\n this.send(worker, { type: 'load', id, url, transform, yUp, reload, ...(priority ? { priority } : {}) });\n });\n }\n\n abort(id: number): void {\n const pending = this.pending.get(id);\n if (pending) this.send(pending.worker, { type: 'abort', id });\n }\n\n destroy(): void {\n for (const worker of this.workers) worker.terminate();\n this.workers = [];\n this.busy.clear();\n for (const { reject } of this.pending.values()) reject(new Error('loader destroyed'));\n this.pending.clear();\n }\n\n /** Workers start with the first tile: a map without 3D Tiles costs nothing */\n private leastBusy(): Worker {\n if (this.workers.length === 0) {\n for (let i = 0; i < this.size; i++) {\n const worker = new Worker(this.workerUrl, { type: 'module' });\n worker.onmessage = (event: MessageEvent<WorkerReply>) => this.receive(worker, event.data);\n // A worker that fails to load (a bundler serving it from elsewhere) or to read a message fails its requests\n worker.onerror = (event) => {\n event.preventDefault();\n this.fail(worker, `the decoding worker ${this.workerUrl} failed${event.message ? `: ${event.message}` : ''}`);\n };\n worker.onmessageerror = () =>\n this.fail(worker, `the decoding worker ${this.workerUrl} sent an unreadable message`);\n this.send(worker, { type: 'init', module: this.module, decoders: this.decoders, formats: this.formats });\n this.workers.push(worker);\n this.busy.set(worker, 0);\n }\n }\n let best = this.workers[0] as Worker;\n for (const worker of this.workers) {\n if ((this.busy.get(worker) ?? 0) < (this.busy.get(best) ?? 0)) best = worker;\n }\n return best;\n }\n\n private send(worker: Worker, message: WorkerRequest): void {\n worker.postMessage(message);\n }\n\n private receive(worker: Worker, reply: WorkerReply): void {\n this.busy.set(worker, Math.max(0, (this.busy.get(worker) ?? 1) - 1));\n const pending = this.pending.get(reply.id);\n if (!pending) return;\n this.pending.delete(reply.id);\n if (reply.type === 'tile') pending.resolve(reply.tile);\n else if (reply.aborted) pending.reject(new DOMException(`tile ${reply.id} aborted`, 'AbortError'));\n else pending.reject(new Error(reply.error));\n }\n\n /** Rejects the requests of a failed worker */\n private fail(worker: Worker, message: string): void {\n const error = new Error(\n `isard: ${message}. If a bundler moves the worker, give its URL in the \\`workerUrl\\` option (see \"With a bundler\" in the guide)`,\n );\n for (const [id, pending] of this.pending) {\n if (pending.worker !== worker) continue;\n this.pending.delete(id);\n pending.reject(error);\n }\n // Out of the pool: when none is left, the next requests fail at once\n worker.terminate();\n this.workers = this.workers.filter((w) => w !== worker);\n this.busy.delete(worker);\n if (this.workers.length === 0) this.failed = error;\n }\n}\n"]}