@loaders.gl/tiles 5.0.0-alpha.2 → 5.0.0-alpha.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.
Files changed (109) hide show
  1. package/LICENSE +1 -1
  2. package/dist/dist.dev.js +17325 -5650
  3. package/dist/dist.min.js +4 -1
  4. package/dist/index.cjs +1924 -178
  5. package/dist/index.cjs.map +3 -3
  6. package/dist/index.d.ts +9 -1
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +6 -0
  9. package/dist/index.js.map +1 -1
  10. package/dist/point-cloud/point-cloud-tileset.d.ts +4 -0
  11. package/dist/point-cloud/point-cloud-tileset.d.ts.map +1 -1
  12. package/dist/point-cloud/point-cloud-tileset.js +51 -12
  13. package/dist/point-cloud/point-cloud-tileset.js.map +1 -1
  14. package/dist/point-cloud/types.d.ts +17 -0
  15. package/dist/point-cloud/types.d.ts.map +1 -1
  16. package/dist/raster-set.d.ts +10 -8
  17. package/dist/raster-set.d.ts.map +1 -1
  18. package/dist/raster-set.js +18 -1
  19. package/dist/raster-set.js.map +1 -1
  20. package/dist/spatial/format-spatial-reference.d.ts +67 -0
  21. package/dist/spatial/format-spatial-reference.d.ts.map +1 -0
  22. package/dist/spatial/format-spatial-reference.js +233 -0
  23. package/dist/spatial/format-spatial-reference.js.map +1 -0
  24. package/dist/spatial/get-spatial-coordinate-frame.d.ts +9 -0
  25. package/dist/spatial/get-spatial-coordinate-frame.d.ts.map +1 -0
  26. package/dist/spatial/get-spatial-coordinate-frame.js +58 -0
  27. package/dist/spatial/get-spatial-coordinate-frame.js.map +1 -0
  28. package/dist/spatial/i3s-elevation.d.ts +20 -0
  29. package/dist/spatial/i3s-elevation.d.ts.map +1 -0
  30. package/dist/spatial/i3s-elevation.js +90 -0
  31. package/dist/spatial/i3s-elevation.js.map +1 -0
  32. package/dist/spatial/i3s-spatial-transformer.d.ts +189 -0
  33. package/dist/spatial/i3s-spatial-transformer.d.ts.map +1 -0
  34. package/dist/spatial/i3s-spatial-transformer.js +686 -0
  35. package/dist/spatial/i3s-spatial-transformer.js.map +1 -0
  36. package/dist/spatial/spatial-coordinate-transformer.d.ts +48 -0
  37. package/dist/spatial/spatial-coordinate-transformer.d.ts.map +1 -0
  38. package/dist/spatial/spatial-coordinate-transformer.js +211 -0
  39. package/dist/spatial/spatial-coordinate-transformer.js.map +1 -0
  40. package/dist/spatial/spatial-resource-registry.d.ts +44 -0
  41. package/dist/spatial/spatial-resource-registry.d.ts.map +1 -0
  42. package/dist/spatial/spatial-resource-registry.js +67 -0
  43. package/dist/spatial/spatial-resource-registry.js.map +1 -0
  44. package/dist/spatial/spatial-types.d.ts +190 -0
  45. package/dist/spatial/spatial-types.d.ts.map +1 -0
  46. package/dist/spatial/spatial-types.js +192 -0
  47. package/dist/spatial/spatial-types.js.map +1 -0
  48. package/dist/tileset-3d/common/tile-3d.d.ts +49 -1
  49. package/dist/tileset-3d/common/tile-3d.d.ts.map +1 -1
  50. package/dist/tileset-3d/common/tile-3d.js +132 -64
  51. package/dist/tileset-3d/common/tile-3d.js.map +1 -1
  52. package/dist/tileset-3d/common/tileset-3d.d.ts +25 -0
  53. package/dist/tileset-3d/common/tileset-3d.d.ts.map +1 -1
  54. package/dist/tileset-3d/common/tileset-3d.js +49 -7
  55. package/dist/tileset-3d/common/tileset-3d.js.map +1 -1
  56. package/dist/tileset-3d/common/tileset-source.d.ts +14 -0
  57. package/dist/tileset-3d/common/tileset-source.d.ts.map +1 -1
  58. package/dist/tileset-3d/common/tileset-source.js.map +1 -1
  59. package/dist/tileset-3d/common/tileset-traverser.d.ts +6 -1
  60. package/dist/tileset-3d/common/tileset-traverser.d.ts.map +1 -1
  61. package/dist/tileset-3d/common/tileset-traverser.js +11 -6
  62. package/dist/tileset-3d/common/tileset-traverser.js.map +1 -1
  63. package/dist/tileset-3d/format-3d-tiles/implicit-tiling.d.ts +26 -1
  64. package/dist/tileset-3d/format-3d-tiles/implicit-tiling.d.ts.map +1 -1
  65. package/dist/tileset-3d/format-3d-tiles/implicit-tiling.js +62 -18
  66. package/dist/tileset-3d/format-3d-tiles/implicit-tiling.js.map +1 -1
  67. package/dist/tileset-3d/format-3d-tiles/tiles-3d-source.d.ts +1 -7
  68. package/dist/tileset-3d/format-3d-tiles/tiles-3d-source.d.ts.map +1 -1
  69. package/dist/tileset-3d/format-3d-tiles/tiles-3d-source.js +32 -55
  70. package/dist/tileset-3d/format-3d-tiles/tiles-3d-source.js.map +1 -1
  71. package/dist/tileset-3d/format-i3s/i3s-source.d.ts +14 -1
  72. package/dist/tileset-3d/format-i3s/i3s-source.d.ts.map +1 -1
  73. package/dist/tileset-3d/format-i3s/i3s-source.js +107 -17
  74. package/dist/tileset-3d/format-i3s/i3s-source.js.map +1 -1
  75. package/dist/tileset-3d/helpers/bounding-volume.d.ts.map +1 -1
  76. package/dist/tileset-3d/helpers/bounding-volume.js +3 -33
  77. package/dist/tileset-3d/helpers/bounding-volume.js.map +1 -1
  78. package/dist/tileset-3d/helpers/frame-state.d.ts +3 -1
  79. package/dist/tileset-3d/helpers/frame-state.d.ts.map +1 -1
  80. package/dist/tileset-3d/helpers/frame-state.js +1 -0
  81. package/dist/tileset-3d/helpers/frame-state.js.map +1 -1
  82. package/dist/tileset-3d/helpers/i3s-lod.d.ts.map +1 -1
  83. package/dist/tileset-3d/helpers/i3s-lod.js +20 -8
  84. package/dist/tileset-3d/helpers/i3s-lod.js.map +1 -1
  85. package/dist/types.d.ts +3 -0
  86. package/dist/types.d.ts.map +1 -1
  87. package/package.json +12 -9
  88. package/src/index.ts +42 -1
  89. package/src/point-cloud/point-cloud-tileset.ts +66 -12
  90. package/src/point-cloud/types.ts +17 -0
  91. package/src/raster-set.ts +46 -11
  92. package/src/spatial/format-spatial-reference.ts +311 -0
  93. package/src/spatial/get-spatial-coordinate-frame.ts +74 -0
  94. package/src/spatial/i3s-elevation.ts +96 -0
  95. package/src/spatial/i3s-spatial-transformer.ts +957 -0
  96. package/src/spatial/spatial-coordinate-transformer.ts +276 -0
  97. package/src/spatial/spatial-resource-registry.ts +79 -0
  98. package/src/spatial/spatial-types.ts +422 -0
  99. package/src/tileset-3d/common/tile-3d.ts +156 -69
  100. package/src/tileset-3d/common/tileset-3d.ts +77 -6
  101. package/src/tileset-3d/common/tileset-source.ts +15 -0
  102. package/src/tileset-3d/common/tileset-traverser.ts +11 -6
  103. package/src/tileset-3d/format-3d-tiles/implicit-tiling.ts +100 -22
  104. package/src/tileset-3d/format-3d-tiles/tiles-3d-source.ts +36 -56
  105. package/src/tileset-3d/format-i3s/i3s-source.ts +119 -18
  106. package/src/tileset-3d/helpers/bounding-volume.ts +3 -55
  107. package/src/tileset-3d/helpers/frame-state.ts +3 -0
  108. package/src/tileset-3d/helpers/i3s-lod.ts +22 -8
  109. package/src/types.ts +3 -0
@@ -0,0 +1,686 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ import { Matrix3, Matrix4, Quaternion, Vector3 } from '@math.gl/core';
5
+ import { Ellipsoid } from '@math.gl/geospatial';
6
+ import { getSpatialCoordinateFrame, SpatialCoordinateTransformer } from "./spatial-coordinate-transformer.js";
7
+ import { createTilesetSpatialReference, markTilesetSpatialReferenceTransformed } from "./spatial-types.js";
8
+ import { getI3SLinearUnitScale } from "./i3s-elevation.js";
9
+ /**
10
+ * Applies one normalized I3S horizontal CRS operation consistently to geometry, normals, origins,
11
+ * and node bounds.
12
+ */
13
+ export class I3SSpatialTransformer {
14
+ /** Descriptor attached to output after all adapter-owned spatial values have been transformed. */
15
+ spatialReference;
16
+ /** Broad coordinate frame of the selected output CRS. */
17
+ targetCoordinateFrame;
18
+ /** Source-to-target position operation. */
19
+ positionTransformer;
20
+ /** Source-to-WGS84 geographic operation used for vector-frame conversion. */
21
+ sourceToGeographicTransformer;
22
+ /** WGS84-geographic-to-target operation used for normal finite differences. */
23
+ geographicToTargetTransformer;
24
+ /** Source frame recorded by I3S metadata. */
25
+ sourceCoordinateFrame;
26
+ /** Source Z multiplier that normalizes stored elevation values to meters. */
27
+ sourceVerticalUnitScale;
28
+ /** I3S elevation placement mode. */
29
+ elevationMode;
30
+ /** I3S elevation offset normalized to meters. */
31
+ elevationOffset;
32
+ /** Terrain or scene provider selected for the declared placement mode. */
33
+ elevationProvider;
34
+ /** Provider height multiplier that normalizes returned values to meters. */
35
+ providerUnitScale;
36
+ /** Optional provider-height to layer-height datum operation. */
37
+ providerHeightTransformer;
38
+ /**
39
+ * Creates an I3S spatial adapter.
40
+ *
41
+ * @param spatialReference - Transformable I3S spatial descriptor.
42
+ * @param options - Registered geoid and custom resource options used by the shared transformer.
43
+ */
44
+ constructor(spatialReference, options = {}) {
45
+ if (spatialReference.status !== 'transformable' && spatialReference.status !== 'transformed') {
46
+ throw new Error('I3SSpatialTransformer requires a requested, transformable spatial output');
47
+ }
48
+ const targetCrs = spatialReference.targetCrs || spatialReference.sourceCrs;
49
+ if (!targetCrs) {
50
+ throw new Error('I3S spatial transformation requires a target CRS');
51
+ }
52
+ this.positionTransformer = new SpatialCoordinateTransformer(spatialReference, options);
53
+ this.sourceCoordinateFrame = spatialReference.coordinateFrame;
54
+ this.sourceVerticalUnitScale = spatialReference.verticalUnitScale;
55
+ this.elevationMode = spatialReference.elevationMode;
56
+ this.elevationOffset =
57
+ (spatialReference.elevationOffset || 0) * spatialReference.elevationUnitScale;
58
+ this.elevationProvider = getElevationProvider(this.elevationMode, options);
59
+ if (this.elevationProvider &&
60
+ (typeof this.elevationProvider.sampleElevations !== 'function' ||
61
+ typeof this.elevationProvider.getElevationRange !== 'function')) {
62
+ throw new Error('An I3S elevation provider must implement sampleElevations() and getElevationRange()');
63
+ }
64
+ const providerUnitScale = getI3SLinearUnitScale(this.elevationProvider?.unit || 'meter');
65
+ if (this.elevationProvider && providerUnitScale === undefined) {
66
+ throw new Error(`Unsupported elevation-provider unit ${this.elevationProvider.unit}`);
67
+ }
68
+ this.providerUnitScale = providerUnitScale || 1;
69
+ this.targetCoordinateFrame = getSpatialCoordinateFrame(targetCrs);
70
+ this.spatialReference = markTilesetSpatialReferenceTransformed(spatialReference);
71
+ const sourceToGeographicReference = createTilesetSpatialReference({
72
+ sourceCrs: spatialReference.sourceCrs,
73
+ heightReference: spatialReference.heightReference,
74
+ coordinateFrame: spatialReference.coordinateFrame,
75
+ axisOrder: spatialReference.axisOrder,
76
+ provenance: spatialReference.provenance
77
+ }, { targetCrs: 'EPSG:4326' });
78
+ this.sourceToGeographicTransformer = new SpatialCoordinateTransformer(sourceToGeographicReference);
79
+ if (requiresElevationSurface(this.elevationMode)) {
80
+ if (!this.elevationProvider) {
81
+ const providerName = this.elevationMode === 'relativeToScene' ? 'scene' : 'terrain';
82
+ throw new Error(`I3S elevation mode ${this.elevationMode} requires a ${providerName} elevation provider`);
83
+ }
84
+ if (spatialReference.heightReference === 'unknown') {
85
+ throw new Error(`I3S elevation mode ${this.elevationMode} requires a known source height reference`);
86
+ }
87
+ const providerHeightReference = this.elevationProvider.heightReference || spatialReference.heightReference;
88
+ if (providerHeightReference !== spatialReference.heightReference) {
89
+ if (spatialReference.heightReference !== 'ellipsoidal' &&
90
+ spatialReference.heightReference !== 'orthometric') {
91
+ throw new Error('Converting elevation-provider heights requires an ellipsoidal or orthometric source height reference');
92
+ }
93
+ const providerHeightSpatialReference = createTilesetSpatialReference({
94
+ sourceCrs: 'EPSG:4326',
95
+ heightReference: providerHeightReference,
96
+ coordinateFrame: 'geographic',
97
+ axisOrder: 'xyz',
98
+ provenance: 'caller-override'
99
+ }, {
100
+ targetCrs: 'EPSG:4326',
101
+ targetHeightReference: spatialReference.heightReference,
102
+ geoidModel: options.geoidModel
103
+ });
104
+ this.providerHeightTransformer = new SpatialCoordinateTransformer(providerHeightSpatialReference, options);
105
+ }
106
+ }
107
+ const geographicToTargetReference = createTilesetSpatialReference({
108
+ sourceCrs: 'EPSG:4326',
109
+ heightReference: spatialReference.heightReference,
110
+ coordinateFrame: 'geographic',
111
+ axisOrder: 'xyz',
112
+ provenance: 'format-default'
113
+ }, {
114
+ targetCrs,
115
+ targetHeightReference: spatialReference.targetHeightReference,
116
+ geoidModel: options.geoidModel
117
+ });
118
+ this.geographicToTargetTransformer = new SpatialCoordinateTransformer(geographicToTargetReference, options);
119
+ }
120
+ /** Transform one absolute I3S source position. */
121
+ transformPosition(position) {
122
+ const placedPosition = this.placeSourcePositionsSync(position);
123
+ const transformed = this.positionTransformer.transformPosition([
124
+ placedPosition[0],
125
+ placedPosition[1],
126
+ placedPosition[2]
127
+ ]);
128
+ return [transformed[0], transformed[1], transformed[2]];
129
+ }
130
+ /** Transform one absolute I3S source position after asynchronous surface placement. */
131
+ async transformPositionAsync(position) {
132
+ const placedPosition = await this.placeSourcePositions(position);
133
+ const transformed = this.positionTransformer.transformPosition([
134
+ placedPosition[0],
135
+ placedPosition[1],
136
+ placedPosition[2]
137
+ ]);
138
+ return [transformed[0], transformed[1], transformed[2]];
139
+ }
140
+ /** Transform an already placed, meter-normalized position to the requested target. */
141
+ transformPlacedPosition(position) {
142
+ const transformed = this.positionTransformer.transformPosition([
143
+ position[0],
144
+ position[1],
145
+ position[2]
146
+ ]);
147
+ return [transformed[0], transformed[1], transformed[2]];
148
+ }
149
+ /** Transform one absolute I3S source position to WGS84 longitude, latitude, and height. */
150
+ transformSourcePositionToGeographic(position) {
151
+ const placedPosition = this.placeSourcePositionsSync(position);
152
+ const transformed = this.sourceToGeographicTransformer.transformPosition([
153
+ placedPosition[0],
154
+ placedPosition[1],
155
+ placedPosition[2]
156
+ ]);
157
+ return [transformed[0], transformed[1], transformed[2]];
158
+ }
159
+ /** Transform one source position to WGS84 after asynchronous elevation placement. */
160
+ async transformSourcePositionToGeographicAsync(position) {
161
+ const placedPosition = await this.placeSourcePositions(position);
162
+ const transformed = this.sourceToGeographicTransformer.transformPosition([
163
+ placedPosition[0],
164
+ placedPosition[1],
165
+ placedPosition[2]
166
+ ]);
167
+ return [transformed[0], transformed[1], transformed[2]];
168
+ }
169
+ /**
170
+ * Transform absolute Float64 positions and return stable Float32 offsets around a target origin.
171
+ *
172
+ * @param sourcePositions - Absolute positions in source CRS order.
173
+ * @param sourceOrigin - Stable source origin, normally the node MBS or OBB center.
174
+ */
175
+ transformPositions(sourcePositions, sourceOrigin) {
176
+ const placedSourcePositions = this.placeSourcePositionsSync(sourcePositions);
177
+ const placedSourceOrigin = this.placeSourcePositionsSync(sourceOrigin);
178
+ return this.transformPlacedPositions(placedSourcePositions, placedSourceOrigin);
179
+ }
180
+ /**
181
+ * Transform positions after asynchronously sampling any requested terrain or scene surface.
182
+ *
183
+ * @param sourcePositions - Absolute positions in source CRS order.
184
+ * @param sourceOrigin - Stable source origin, normally the node MBS or OBB center.
185
+ * @returns Renderer-ready target-frame offsets and stable origin metadata.
186
+ */
187
+ async transformPositionsAsync(sourcePositions, sourceOrigin) {
188
+ const combinedPositions = new Float64Array(sourcePositions.length + 3);
189
+ combinedPositions.set(Array.from(sourcePositions), 0);
190
+ combinedPositions.set(Array.from(sourceOrigin).slice(0, 3), sourcePositions.length);
191
+ const placedPositions = await this.placeSourcePositions(combinedPositions);
192
+ return this.transformPlacedPositions(placedPositions.subarray(0, sourcePositions.length), placedPositions.subarray(sourcePositions.length));
193
+ }
194
+ /** Transform already placed positions into renderer-relative target coordinates. */
195
+ transformPlacedPositions(sourcePositions, sourceOrigin) {
196
+ const origin = this.transformPlacedPosition(sourceOrigin);
197
+ const positions = new Float32Array(sourcePositions.length);
198
+ const absoluteSourcePositions = Float64Array.from(sourcePositions);
199
+ for (let index = 0; index < sourcePositions.length; index += 3) {
200
+ const transformed = this.transformPlacedPosition([
201
+ sourcePositions[index],
202
+ sourcePositions[index + 1],
203
+ sourcePositions[index + 2]
204
+ ]);
205
+ positions[index] = transformed[0] - origin[0];
206
+ positions[index + 1] = transformed[1] - origin[1];
207
+ positions[index + 2] = transformed[2] - origin[2];
208
+ }
209
+ const isGeographic = this.targetCoordinateFrame === 'geographic';
210
+ const modelMatrix = isGeographic ? new Matrix4() : new Matrix4().translate(origin);
211
+ return {
212
+ positions,
213
+ sourcePositions: absoluteSourcePositions,
214
+ origin,
215
+ cartographicOrigin: isGeographic ? origin : [0, 0, 0],
216
+ coordinateSystem: isGeographic ? 'lnglat-offsets' : 'cartesian',
217
+ modelMatrix
218
+ };
219
+ }
220
+ /**
221
+ * Transform I3S normals into the selected target basis.
222
+ *
223
+ * Earth-centered normals are rotated directly. Vertex-reference-frame normals are first mapped
224
+ * through the local east/north/up basis at each source position.
225
+ *
226
+ * @param normals - Three-component normal vectors.
227
+ * @param sourcePositions - Absolute source position paired with each normal.
228
+ * @param normalReferenceFrame - I3S normal frame declaration.
229
+ */
230
+ transformNormals(normals, sourcePositions, normalReferenceFrame = 'earth-centered') {
231
+ const transformedNormals = new Float32Array(normals.length);
232
+ for (let index = 0; index < normals.length; index += 3) {
233
+ const geographic = this.sourceToGeographicTransformer.transformPosition([
234
+ sourcePositions[index],
235
+ sourcePositions[index + 1],
236
+ sourcePositions[index + 2]
237
+ ]);
238
+ const inputNormal = new Vector3([
239
+ normals[index],
240
+ normals[index + 1],
241
+ normals[index + 2]
242
+ ]).normalize();
243
+ const ecefNormal = normalReferenceFrame === 'earth-centered'
244
+ ? inputNormal
245
+ : localEnuVectorToEcef(inputNormal, geographic);
246
+ const outputNormal = this.transformEcefNormal(ecefNormal, geographic);
247
+ transformedNormals.set(outputNormal, index);
248
+ }
249
+ return transformedNormals;
250
+ }
251
+ /**
252
+ * Rebuild an I3S node bound from transformed corner or sphere-axis samples.
253
+ *
254
+ * @param bounds - Source MBS/OBB fields.
255
+ * @returns A generic tile `region` for geographic output or axis-aligned `box` otherwise.
256
+ */
257
+ transformBoundingVolume(bounds) {
258
+ const geographicSamples = this.getGeographicBoundSamples(bounds);
259
+ const targetSamples = geographicSamples.map(position => this.geographicToTargetTransformer.transformPosition(position));
260
+ return this.targetCoordinateFrame === 'geographic'
261
+ ? { region: getGeographicRegion(targetSamples) }
262
+ : { box: getAxisAlignedBox(targetSamples) };
263
+ }
264
+ /** Rebuild an I3S bound in the target frame after asynchronous elevation placement. */
265
+ async transformBoundingVolumeAsync(bounds) {
266
+ const geographicSamples = await this.getGeographicBoundSamplesAsync(bounds);
267
+ const targetSamples = geographicSamples.map(position => this.geographicToTargetTransformer.transformPosition(position));
268
+ return this.targetCoordinateFrame === 'geographic'
269
+ ? { region: getGeographicRegion(targetSamples) }
270
+ : { box: getAxisAlignedBox(targetSamples) };
271
+ }
272
+ /** Rebuild an I3S bound as a WGS84 geographic region for geographic traversal algorithms. */
273
+ transformBoundingVolumeToGeographic(bounds) {
274
+ return { region: getGeographicRegion(this.getGeographicBoundSamples(bounds)) };
275
+ }
276
+ /** Rebuild an I3S bound as WGS84 geographic after asynchronous elevation placement. */
277
+ async transformBoundingVolumeToGeographicAsync(bounds) {
278
+ return { region: getGeographicRegion(await this.getGeographicBoundSamplesAsync(bounds)) };
279
+ }
280
+ /** Rebuild an I3S bound as a WGS84 ECEF box for generic `Tileset3D` traversal. */
281
+ transformBoundingVolumeToGeocentric(bounds) {
282
+ const cartesianSamples = this.getGeographicBoundSamples(bounds).map(position => Array.from(Ellipsoid.WGS84.cartographicToCartesian(new Vector3(position))));
283
+ return { box: getAxisAlignedBox(cartesianSamples) };
284
+ }
285
+ /** Rebuild an I3S bound as WGS84 ECEF after asynchronous elevation placement. */
286
+ async transformBoundingVolumeToGeocentricAsync(bounds) {
287
+ const cartesianSamples = (await this.getGeographicBoundSamplesAsync(bounds)).map(position => Array.from(Ellipsoid.WGS84.cartographicToCartesian(new Vector3(position))));
288
+ return { box: getAxisAlignedBox(cartesianSamples) };
289
+ }
290
+ /** Return a WGS84 geographic MBS used by the I3S screen-threshold calculation. */
291
+ transformBoundingSphereToGeographic(bounds) {
292
+ const sourceCenter = bounds.mbs?.length
293
+ ? Array.from(bounds.mbs).slice(0, 3)
294
+ : bounds.obb
295
+ ? Array.from(bounds.obb.center).slice(0, 3)
296
+ : null;
297
+ if (!sourceCenter) {
298
+ throw new Error('I3S spatial transformation requires an MBS or OBB bound');
299
+ }
300
+ const placedCenter = this.placeSourcePositionsSync(sourceCenter);
301
+ const geographicCenter = this.sourceToGeographicTransformer.transformPosition(Array.from(placedCenter));
302
+ const radius = bounds.mbs?.length
303
+ ? bounds.mbs[3]
304
+ : Math.hypot(...Array.from(bounds.obb.halfSize).slice(0, 3));
305
+ return [geographicCenter[0], geographicCenter[1], geographicCenter[2], radius];
306
+ }
307
+ /** Return a placed WGS84 MBS used by the I3S screen-threshold calculation. */
308
+ async transformBoundingSphereToGeographicAsync(bounds) {
309
+ return getGeographicBoundingSphere(await this.getGeographicBoundSamplesAsync(bounds));
310
+ }
311
+ /**
312
+ * Rebuild traversal, target-output, and I3S LOD bounds from one elevation-sampling batch.
313
+ *
314
+ * @param bounds - Source MBS/OBB fields.
315
+ * @returns Parallel bounds in WGS84 ECEF, the selected output frame, and WGS84 geographic.
316
+ */
317
+ async transformBoundsAsync(bounds) {
318
+ const geographicSamples = await this.getGeographicBoundSamplesAsync(bounds);
319
+ const cartesianSamples = geographicSamples.map(position => Array.from(Ellipsoid.WGS84.cartographicToCartesian(new Vector3(position))));
320
+ const targetSamples = geographicSamples.map(position => this.geographicToTargetTransformer.transformPosition(position));
321
+ return {
322
+ boundingVolume: { box: getAxisAlignedBox(cartesianSamples) },
323
+ spatialBoundingVolume: this.targetCoordinateFrame === 'geographic'
324
+ ? { region: getGeographicRegion(targetSamples) }
325
+ : { box: getAxisAlignedBox(targetSamples) },
326
+ i3sLodMbs: requiresElevationSurface(this.elevationMode)
327
+ ? getGeographicBoundingSphere(geographicSamples)
328
+ : this.transformBoundingSphereToGeographic(bounds)
329
+ };
330
+ }
331
+ /**
332
+ * Rebuild Point Cloud traversal and target-output bounds from one elevation-sampling batch.
333
+ *
334
+ * @param bounds - Source OBB fields.
335
+ * @returns WGS84 geographic traversal bound plus the selected output-frame bound.
336
+ */
337
+ async transformPointCloudBoundsAsync(bounds) {
338
+ const geographicSamples = await this.getGeographicBoundSamplesAsync(bounds);
339
+ const targetSamples = geographicSamples.map(position => this.geographicToTargetTransformer.transformPosition(position));
340
+ return {
341
+ boundingVolume: { region: getGeographicRegion(geographicSamples) },
342
+ spatialBoundingVolume: this.targetCoordinateFrame === 'geographic'
343
+ ? { region: getGeographicRegion(targetSamples) }
344
+ : { box: getAxisAlignedBox(targetSamples) }
345
+ };
346
+ }
347
+ /** Sample a source I3S bound and normalize every sample to WGS84 geographic coordinates. */
348
+ getGeographicBoundSamples(bounds) {
349
+ const sourceSamples = getSourceBoundSamples(bounds, this.sourceCoordinateFrame, this.sourceToGeographicTransformer, this.sourceVerticalUnitScale);
350
+ if (!sourceSamples.length) {
351
+ throw new Error('I3S spatial transformation requires an MBS or OBB bound');
352
+ }
353
+ const placedSourceSamples = this.placeSourcePositionsSync(sourceSamples.flat(), true);
354
+ const placedSamples = toPositionArrays(placedSourceSamples);
355
+ return this.sourceCoordinateFrame === 'geographic'
356
+ ? placedSamples
357
+ : placedSamples.map(position => this.sourceToGeographicTransformer.transformPosition(position));
358
+ }
359
+ /** Sample, place, and normalize one source bound to WGS84 geographic coordinates. */
360
+ async getGeographicBoundSamplesAsync(bounds) {
361
+ const sourceSamples = getSourceBoundSamples(bounds, this.sourceCoordinateFrame, this.sourceToGeographicTransformer, this.sourceVerticalUnitScale);
362
+ if (!sourceSamples.length) {
363
+ throw new Error('I3S spatial transformation requires an MBS or OBB bound');
364
+ }
365
+ if (requiresElevationSurface(this.elevationMode)) {
366
+ const geographicSamples = this.sourceCoordinateFrame === 'geographic'
367
+ ? sourceSamples
368
+ : sourceSamples.map(position => this.sourceToGeographicTransformer.transformPosition(position));
369
+ return await this.placeGeographicBoundSamples(geographicSamples);
370
+ }
371
+ const placedSamples = toPositionArrays(await this.placeSourcePositions(sourceSamples.flat(), true));
372
+ return this.sourceCoordinateFrame === 'geographic'
373
+ ? placedSamples
374
+ : placedSamples.map(position => this.sourceToGeographicTransformer.transformPosition(position));
375
+ }
376
+ /** Apply a provider's conservative surface range to an entire geographic bound footprint. */
377
+ async placeGeographicBoundSamples(geographicSamples) {
378
+ const elevationProvider = this.elevationProvider;
379
+ const elevationRange = await elevationProvider.getElevationRange(getElevationBounds(geographicSamples));
380
+ const minimumProviderHeight = elevationRange.minimum * this.providerUnitScale;
381
+ const maximumProviderHeight = elevationRange.maximum * this.providerUnitScale;
382
+ if (!Number.isFinite(minimumProviderHeight) ||
383
+ !Number.isFinite(maximumProviderHeight) ||
384
+ minimumProviderHeight > maximumProviderHeight) {
385
+ throw new Error('Elevation provider returned an invalid range; minimum and maximum must be finite and ordered');
386
+ }
387
+ const sourceHeights = geographicSamples.map(position => position[2]);
388
+ const minimumSourceHeight = Math.min(...sourceHeights);
389
+ const maximumSourceHeight = Math.max(...sourceHeights);
390
+ const placedSamples = [];
391
+ for (const [longitude, latitude] of geographicSamples) {
392
+ const convertedMinimum = this.providerHeightTransformer
393
+ ? this.providerHeightTransformer.transformPosition([
394
+ longitude,
395
+ latitude,
396
+ minimumProviderHeight
397
+ ])[2]
398
+ : minimumProviderHeight;
399
+ const convertedMaximum = this.providerHeightTransformer
400
+ ? this.providerHeightTransformer.transformPosition([
401
+ longitude,
402
+ latitude,
403
+ maximumProviderHeight
404
+ ])[2]
405
+ : maximumProviderHeight;
406
+ const minimumSurfaceHeight = Math.min(convertedMinimum, convertedMaximum);
407
+ const maximumSurfaceHeight = Math.max(convertedMinimum, convertedMaximum);
408
+ const minimumHeight = this.elevationMode === 'onTheGround'
409
+ ? minimumSurfaceHeight
410
+ : minimumSurfaceHeight + minimumSourceHeight + this.elevationOffset;
411
+ const maximumHeight = this.elevationMode === 'onTheGround'
412
+ ? maximumSurfaceHeight
413
+ : maximumSurfaceHeight + maximumSourceHeight + this.elevationOffset;
414
+ placedSamples.push([longitude, latitude, minimumHeight], [longitude, latitude, maximumHeight]);
415
+ }
416
+ return placedSamples;
417
+ }
418
+ /** Apply source units and non-surface placement without awaiting external resources. */
419
+ placeSourcePositionsSync(sourcePositions, sourceHeightsAreMeters = false) {
420
+ if (requiresElevationSurface(this.elevationMode)) {
421
+ throw new Error(`I3S elevation mode ${this.elevationMode} requires asynchronous surface placement`);
422
+ }
423
+ const positions = Float64Array.from(sourcePositions);
424
+ const offset = this.elevationMode === 'absoluteHeight' ? this.elevationOffset : 0;
425
+ for (let index = 2; index < positions.length; index += 3) {
426
+ positions[index] =
427
+ positions[index] * (sourceHeightsAreMeters ? 1 : this.sourceVerticalUnitScale) + offset;
428
+ }
429
+ return positions;
430
+ }
431
+ /** Apply source units and all I3S elevation-placement modes. */
432
+ async placeSourcePositions(sourcePositions, sourceHeightsAreMeters = false) {
433
+ if (!requiresElevationSurface(this.elevationMode)) {
434
+ return this.placeSourcePositionsSync(sourcePositions, sourceHeightsAreMeters);
435
+ }
436
+ const positions = Float64Array.from(sourcePositions);
437
+ const geographicLocations = toPositionArrays(positions).map(position => {
438
+ const geographic = this.sourceToGeographicTransformer.transformPosition(position);
439
+ return [geographic[0], geographic[1]];
440
+ });
441
+ const sampledHeights = await this.elevationProvider.sampleElevations(geographicLocations);
442
+ if (sampledHeights.length !== geographicLocations.length) {
443
+ throw new Error(`Elevation provider returned ${sampledHeights.length} heights for ${geographicLocations.length} positions`);
444
+ }
445
+ for (let positionIndex = 0; positionIndex < geographicLocations.length; positionIndex++) {
446
+ const sampledHeight = sampledHeights[positionIndex] * this.providerUnitScale;
447
+ if (!Number.isFinite(sampledHeight)) {
448
+ throw new Error(`Elevation provider returned a non-finite height at index ${positionIndex}`);
449
+ }
450
+ const [longitude, latitude] = geographicLocations[positionIndex];
451
+ const surfaceHeight = this.providerHeightTransformer
452
+ ? this.providerHeightTransformer.transformPosition([longitude, latitude, sampledHeight])[2]
453
+ : sampledHeight;
454
+ const sourceHeight = positions[positionIndex * 3 + 2] *
455
+ (sourceHeightsAreMeters ? 1 : this.sourceVerticalUnitScale);
456
+ positions[positionIndex * 3 + 2] =
457
+ this.elevationMode === 'onTheGround'
458
+ ? surfaceHeight
459
+ : surfaceHeight + sourceHeight + this.elevationOffset;
460
+ }
461
+ return positions;
462
+ }
463
+ /** Transform one earth-centered normal to the selected target basis. */
464
+ transformEcefNormal(ecefNormal, geographic) {
465
+ if (this.targetCoordinateFrame === 'geocentric') {
466
+ return ecefNormal.normalize();
467
+ }
468
+ if (this.targetCoordinateFrame === 'geographic') {
469
+ return ecefVectorToLocalEnu(ecefNormal, geographic).normalize();
470
+ }
471
+ const cartesian = Ellipsoid.WGS84.cartographicToCartesian(new Vector3(geographic));
472
+ const jacobianColumns = [];
473
+ for (const direction of getPositiveAxisDirections()) {
474
+ const positiveGeographic = Ellipsoid.WGS84.cartesianToCartographic(new Vector3(cartesian).add(direction));
475
+ const negativeGeographic = Ellipsoid.WGS84.cartesianToCartographic(new Vector3(cartesian).subtract(direction));
476
+ const positiveTarget = this.geographicToTargetTransformer.transformPosition(positiveGeographic);
477
+ const negativeTarget = this.geographicToTargetTransformer.transformPosition(negativeGeographic);
478
+ jacobianColumns.push((positiveTarget[0] - negativeTarget[0]) / 2, (positiveTarget[1] - negativeTarget[1]) / 2, (positiveTarget[2] - negativeTarget[2]) / 2);
479
+ }
480
+ const normalMatrix = new Matrix3(jacobianColumns);
481
+ if (Math.abs(normalMatrix.determinant()) < 1e-12) {
482
+ throw new Error('Cannot transform an I3S normal through a singular local projection');
483
+ }
484
+ normalMatrix.invert().transpose();
485
+ return new Vector3(ecefNormal).transformByMatrix3(normalMatrix).normalize();
486
+ }
487
+ }
488
+ /** Select the application-owned surface required by one I3S elevation mode. */
489
+ function getElevationProvider(mode, options) {
490
+ return mode === 'relativeToScene'
491
+ ? options.sceneElevationProvider
492
+ : requiresElevationSurface(mode)
493
+ ? options.terrainElevationProvider
494
+ : undefined;
495
+ }
496
+ /** Return whether one placement mode requires external surface samples. */
497
+ function requiresElevationSurface(mode) {
498
+ return mode === 'onTheGround' || mode === 'relativeToGround' || mode === 'relativeToScene';
499
+ }
500
+ /** Split one packed XYZ sequence into position arrays without losing Float64 precision. */
501
+ function toPositionArrays(positions) {
502
+ if (positions.length % 3 !== 0) {
503
+ throw new Error('I3S position arrays must contain complete XYZ tuples');
504
+ }
505
+ const result = [];
506
+ for (let index = 0; index < positions.length; index += 3) {
507
+ result.push([positions[index], positions[index + 1], positions[index + 2]]);
508
+ }
509
+ return result;
510
+ }
511
+ /** Return a dateline-aware WGS84 footprint in degrees for an elevation range request. */
512
+ function getElevationBounds(geographicSamples) {
513
+ const region = getGeographicRegion(geographicSamples);
514
+ return {
515
+ west: (region[0] * 180) / Math.PI,
516
+ south: (region[1] * 180) / Math.PI,
517
+ east: (region[2] * 180) / Math.PI,
518
+ north: (region[3] * 180) / Math.PI
519
+ };
520
+ }
521
+ /** Build a conservative WGS84 geographic sphere from placed geographic samples. */
522
+ function getGeographicBoundingSphere(geographicSamples) {
523
+ if (!geographicSamples.length) {
524
+ throw new Error('I3S spatial transformation requires at least one bound sample');
525
+ }
526
+ const cartesianSamples = geographicSamples.map(position => Ellipsoid.WGS84.cartographicToCartesian(new Vector3(position)));
527
+ const cartesianCenter = new Vector3();
528
+ for (const sample of cartesianSamples) {
529
+ cartesianCenter.add(sample);
530
+ }
531
+ cartesianCenter.scale(1 / cartesianSamples.length);
532
+ let radius = 0;
533
+ for (const sample of cartesianSamples) {
534
+ radius = Math.max(radius, new Vector3(sample).subtract(cartesianCenter).magnitude());
535
+ }
536
+ const geographicCenter = Ellipsoid.WGS84.cartesianToCartographic(cartesianCenter);
537
+ return [geographicCenter[0], geographicCenter[1], geographicCenter[2], radius];
538
+ }
539
+ /** Generate source-frame samples that conservatively cover an I3S MBS or OBB. */
540
+ function getSourceBoundSamples(bounds, sourceFrame, sourceToGeographicTransformer, sourceVerticalUnitScale) {
541
+ const obb = bounds.obb;
542
+ if (obb) {
543
+ const center = Array.from(obb.center).slice(0, 3);
544
+ center[2] *= sourceVerticalUnitScale;
545
+ const halfSize = Array.from(obb.halfSize).slice(0, 3);
546
+ const quaternion = new Quaternion().fromArray(Array.from(obb.quaternion));
547
+ const axes = [
548
+ new Vector3([halfSize[0], 0, 0]).transformByQuaternion(quaternion),
549
+ new Vector3([0, halfSize[1], 0]).transformByQuaternion(quaternion),
550
+ new Vector3([0, 0, halfSize[2]]).transformByQuaternion(quaternion)
551
+ ];
552
+ if (sourceFrame === 'geographic') {
553
+ const geographicCenter = sourceToGeographicTransformer.transformPosition(center);
554
+ const cartesianCenter = Ellipsoid.WGS84.cartographicToCartesian(new Vector3(geographicCenter));
555
+ const cartesianAxes = bounds.normalReferenceFrame === 'vertex-reference-frame'
556
+ ? axes.map(axis => localEnuVectorToEcef(axis, geographicCenter))
557
+ : axes;
558
+ return getBoxSigns().map(signs => {
559
+ const corner = new Vector3(cartesianCenter);
560
+ for (let axisIndex = 0; axisIndex < 3; axisIndex++) {
561
+ corner.add(new Vector3(cartesianAxes[axisIndex]).scale(signs[axisIndex]));
562
+ }
563
+ return Array.from(Ellipsoid.WGS84.cartesianToCartographic(corner));
564
+ });
565
+ }
566
+ return getBoxSigns().map(signs => {
567
+ const corner = new Vector3(center);
568
+ for (let axisIndex = 0; axisIndex < 3; axisIndex++) {
569
+ corner.add(new Vector3(axes[axisIndex]).scale(signs[axisIndex]));
570
+ }
571
+ return Array.from(corner);
572
+ });
573
+ }
574
+ const mbs = bounds.mbs ? Array.from(bounds.mbs) : [];
575
+ if (mbs.length < 4) {
576
+ return [];
577
+ }
578
+ const center = mbs.slice(0, 3);
579
+ center[2] *= sourceVerticalUnitScale;
580
+ const radius = mbs[3];
581
+ if (sourceFrame === 'geographic') {
582
+ const geographicCenter = sourceToGeographicTransformer.transformPosition(center);
583
+ const cartesianCenter = Ellipsoid.WGS84.cartographicToCartesian(new Vector3(geographicCenter));
584
+ return getAxisDirections().map(direction => Array.from(Ellipsoid.WGS84.cartesianToCartographic(new Vector3(cartesianCenter).add(new Vector3(direction).scale(radius)))));
585
+ }
586
+ return getAxisDirections().map(direction => Array.from(new Vector3(center).add(new Vector3(direction).scale(radius))));
587
+ }
588
+ /** Return the eight sign combinations of a box. */
589
+ function getBoxSigns() {
590
+ return [-1, 1].flatMap(x => [-1, 1].flatMap(y => [-1, 1].map(z => [x, y, z])));
591
+ }
592
+ /** Return positive and negative unit directions for three Cartesian axes. */
593
+ function getAxisDirections() {
594
+ return [
595
+ [1, 0, 0],
596
+ [-1, 0, 0],
597
+ [0, 1, 0],
598
+ [0, -1, 0],
599
+ [0, 0, 1],
600
+ [0, 0, -1]
601
+ ];
602
+ }
603
+ /** Return the positive ECEF basis used to calculate a local projection Jacobian. */
604
+ function getPositiveAxisDirections() {
605
+ return [new Vector3([1, 0, 0]), new Vector3([0, 1, 0]), new Vector3([0, 0, 1])];
606
+ }
607
+ /** Convert a local east/north/up vector into an earth-centered vector. */
608
+ function localEnuVectorToEcef(vector, geographic) {
609
+ const longitude = (geographic[0] * Math.PI) / 180;
610
+ const latitude = (geographic[1] * Math.PI) / 180;
611
+ const east = new Vector3([-Math.sin(longitude), Math.cos(longitude), 0]);
612
+ const north = new Vector3([
613
+ -Math.sin(latitude) * Math.cos(longitude),
614
+ -Math.sin(latitude) * Math.sin(longitude),
615
+ Math.cos(latitude)
616
+ ]);
617
+ const up = new Vector3([
618
+ Math.cos(latitude) * Math.cos(longitude),
619
+ Math.cos(latitude) * Math.sin(longitude),
620
+ Math.sin(latitude)
621
+ ]);
622
+ return east.scale(vector[0]).add(north.scale(vector[1])).add(up.scale(vector[2]));
623
+ }
624
+ /** Convert an earth-centered vector into local east/north/up components. */
625
+ function ecefVectorToLocalEnu(vector, geographic) {
626
+ const longitude = (geographic[0] * Math.PI) / 180;
627
+ const latitude = (geographic[1] * Math.PI) / 180;
628
+ const east = new Vector3([-Math.sin(longitude), Math.cos(longitude), 0]);
629
+ const north = new Vector3([
630
+ -Math.sin(latitude) * Math.cos(longitude),
631
+ -Math.sin(latitude) * Math.sin(longitude),
632
+ Math.cos(latitude)
633
+ ]);
634
+ const up = new Vector3([
635
+ Math.cos(latitude) * Math.cos(longitude),
636
+ Math.cos(latitude) * Math.sin(longitude),
637
+ Math.sin(latitude)
638
+ ]);
639
+ return new Vector3([east.dot(vector), north.dot(vector), up.dot(vector)]);
640
+ }
641
+ /** Build a target-frame axis-aligned generic tile box. */
642
+ function getAxisAlignedBox(positions) {
643
+ const minimum = [Infinity, Infinity, Infinity];
644
+ const maximum = [-Infinity, -Infinity, -Infinity];
645
+ for (const position of positions) {
646
+ for (let component = 0; component < 3; component++) {
647
+ minimum[component] = Math.min(minimum[component], position[component]);
648
+ maximum[component] = Math.max(maximum[component], position[component]);
649
+ }
650
+ }
651
+ const center = minimum.map((value, index) => (value + maximum[index]) / 2);
652
+ const halfSize = minimum.map((value, index) => (maximum[index] - value) / 2);
653
+ return [center[0], center[1], center[2], halfSize[0], 0, 0, 0, halfSize[1], 0, 0, 0, halfSize[2]];
654
+ }
655
+ /** Build a dateline-aware geographic tile region in radians. */
656
+ function getGeographicRegion(positions) {
657
+ const referenceLongitude = positions[0][0];
658
+ let minimumLongitude = Infinity;
659
+ let maximumLongitude = -Infinity;
660
+ let minimumLatitude = Infinity;
661
+ let maximumLatitude = -Infinity;
662
+ let minimumHeight = Infinity;
663
+ let maximumHeight = -Infinity;
664
+ for (const position of positions) {
665
+ const longitude = referenceLongitude + normalizeLongitude(position[0] - referenceLongitude);
666
+ minimumLongitude = Math.min(minimumLongitude, longitude);
667
+ maximumLongitude = Math.max(maximumLongitude, longitude);
668
+ minimumLatitude = Math.min(minimumLatitude, position[1]);
669
+ maximumLatitude = Math.max(maximumLatitude, position[1]);
670
+ minimumHeight = Math.min(minimumHeight, position[2]);
671
+ maximumHeight = Math.max(maximumHeight, position[2]);
672
+ }
673
+ return [
674
+ (normalizeLongitude(minimumLongitude) * Math.PI) / 180,
675
+ (minimumLatitude * Math.PI) / 180,
676
+ (normalizeLongitude(maximumLongitude) * Math.PI) / 180,
677
+ (maximumLatitude * Math.PI) / 180,
678
+ minimumHeight,
679
+ maximumHeight
680
+ ];
681
+ }
682
+ /** Normalize a longitude delta to the interval [-180, 180). */
683
+ function normalizeLongitude(longitude) {
684
+ return ((((longitude + 180) % 360) + 360) % 360) - 180;
685
+ }
686
+ //# sourceMappingURL=i3s-spatial-transformer.js.map