@loaders.gl/tiles 5.0.0-alpha.2 → 5.0.0-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/dist/dist.dev.js +17325 -5650
- package/dist/dist.min.js +4 -1
- package/dist/index.cjs +1924 -178
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +9 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/dist/point-cloud/point-cloud-tileset.d.ts +4 -0
- package/dist/point-cloud/point-cloud-tileset.d.ts.map +1 -1
- package/dist/point-cloud/point-cloud-tileset.js +51 -12
- package/dist/point-cloud/point-cloud-tileset.js.map +1 -1
- package/dist/point-cloud/types.d.ts +17 -0
- package/dist/point-cloud/types.d.ts.map +1 -1
- package/dist/raster-set.d.ts +10 -8
- package/dist/raster-set.d.ts.map +1 -1
- package/dist/raster-set.js +18 -1
- package/dist/raster-set.js.map +1 -1
- package/dist/spatial/format-spatial-reference.d.ts +67 -0
- package/dist/spatial/format-spatial-reference.d.ts.map +1 -0
- package/dist/spatial/format-spatial-reference.js +233 -0
- package/dist/spatial/format-spatial-reference.js.map +1 -0
- package/dist/spatial/get-spatial-coordinate-frame.d.ts +9 -0
- package/dist/spatial/get-spatial-coordinate-frame.d.ts.map +1 -0
- package/dist/spatial/get-spatial-coordinate-frame.js +58 -0
- package/dist/spatial/get-spatial-coordinate-frame.js.map +1 -0
- package/dist/spatial/i3s-elevation.d.ts +20 -0
- package/dist/spatial/i3s-elevation.d.ts.map +1 -0
- package/dist/spatial/i3s-elevation.js +90 -0
- package/dist/spatial/i3s-elevation.js.map +1 -0
- package/dist/spatial/i3s-spatial-transformer.d.ts +189 -0
- package/dist/spatial/i3s-spatial-transformer.d.ts.map +1 -0
- package/dist/spatial/i3s-spatial-transformer.js +686 -0
- package/dist/spatial/i3s-spatial-transformer.js.map +1 -0
- package/dist/spatial/spatial-coordinate-transformer.d.ts +48 -0
- package/dist/spatial/spatial-coordinate-transformer.d.ts.map +1 -0
- package/dist/spatial/spatial-coordinate-transformer.js +211 -0
- package/dist/spatial/spatial-coordinate-transformer.js.map +1 -0
- package/dist/spatial/spatial-resource-registry.d.ts +44 -0
- package/dist/spatial/spatial-resource-registry.d.ts.map +1 -0
- package/dist/spatial/spatial-resource-registry.js +67 -0
- package/dist/spatial/spatial-resource-registry.js.map +1 -0
- package/dist/spatial/spatial-types.d.ts +190 -0
- package/dist/spatial/spatial-types.d.ts.map +1 -0
- package/dist/spatial/spatial-types.js +192 -0
- package/dist/spatial/spatial-types.js.map +1 -0
- package/dist/tileset-3d/common/tile-3d.d.ts +49 -1
- package/dist/tileset-3d/common/tile-3d.d.ts.map +1 -1
- package/dist/tileset-3d/common/tile-3d.js +132 -64
- package/dist/tileset-3d/common/tile-3d.js.map +1 -1
- package/dist/tileset-3d/common/tileset-3d.d.ts +25 -0
- package/dist/tileset-3d/common/tileset-3d.d.ts.map +1 -1
- package/dist/tileset-3d/common/tileset-3d.js +49 -7
- package/dist/tileset-3d/common/tileset-3d.js.map +1 -1
- package/dist/tileset-3d/common/tileset-source.d.ts +14 -0
- package/dist/tileset-3d/common/tileset-source.d.ts.map +1 -1
- package/dist/tileset-3d/common/tileset-source.js.map +1 -1
- package/dist/tileset-3d/common/tileset-traverser.d.ts +6 -1
- package/dist/tileset-3d/common/tileset-traverser.d.ts.map +1 -1
- package/dist/tileset-3d/common/tileset-traverser.js +11 -6
- package/dist/tileset-3d/common/tileset-traverser.js.map +1 -1
- package/dist/tileset-3d/format-3d-tiles/implicit-tiling.d.ts +26 -1
- package/dist/tileset-3d/format-3d-tiles/implicit-tiling.d.ts.map +1 -1
- package/dist/tileset-3d/format-3d-tiles/implicit-tiling.js +62 -18
- package/dist/tileset-3d/format-3d-tiles/implicit-tiling.js.map +1 -1
- package/dist/tileset-3d/format-3d-tiles/tiles-3d-source.d.ts +1 -7
- package/dist/tileset-3d/format-3d-tiles/tiles-3d-source.d.ts.map +1 -1
- package/dist/tileset-3d/format-3d-tiles/tiles-3d-source.js +32 -55
- package/dist/tileset-3d/format-3d-tiles/tiles-3d-source.js.map +1 -1
- package/dist/tileset-3d/format-i3s/i3s-source.d.ts +14 -1
- package/dist/tileset-3d/format-i3s/i3s-source.d.ts.map +1 -1
- package/dist/tileset-3d/format-i3s/i3s-source.js +107 -17
- package/dist/tileset-3d/format-i3s/i3s-source.js.map +1 -1
- package/dist/tileset-3d/helpers/bounding-volume.d.ts.map +1 -1
- package/dist/tileset-3d/helpers/bounding-volume.js +3 -33
- package/dist/tileset-3d/helpers/bounding-volume.js.map +1 -1
- package/dist/tileset-3d/helpers/frame-state.d.ts +3 -1
- package/dist/tileset-3d/helpers/frame-state.d.ts.map +1 -1
- package/dist/tileset-3d/helpers/frame-state.js +1 -0
- package/dist/tileset-3d/helpers/frame-state.js.map +1 -1
- package/dist/tileset-3d/helpers/i3s-lod.d.ts.map +1 -1
- package/dist/tileset-3d/helpers/i3s-lod.js +20 -8
- package/dist/tileset-3d/helpers/i3s-lod.js.map +1 -1
- package/dist/types.d.ts +3 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +12 -9
- package/src/index.ts +42 -1
- package/src/point-cloud/point-cloud-tileset.ts +66 -12
- package/src/point-cloud/types.ts +17 -0
- package/src/raster-set.ts +46 -11
- package/src/spatial/format-spatial-reference.ts +311 -0
- package/src/spatial/get-spatial-coordinate-frame.ts +74 -0
- package/src/spatial/i3s-elevation.ts +96 -0
- package/src/spatial/i3s-spatial-transformer.ts +957 -0
- package/src/spatial/spatial-coordinate-transformer.ts +276 -0
- package/src/spatial/spatial-resource-registry.ts +79 -0
- package/src/spatial/spatial-types.ts +422 -0
- package/src/tileset-3d/common/tile-3d.ts +156 -69
- package/src/tileset-3d/common/tileset-3d.ts +77 -6
- package/src/tileset-3d/common/tileset-source.ts +15 -0
- package/src/tileset-3d/common/tileset-traverser.ts +11 -6
- package/src/tileset-3d/format-3d-tiles/implicit-tiling.ts +100 -22
- package/src/tileset-3d/format-3d-tiles/tiles-3d-source.ts +36 -56
- package/src/tileset-3d/format-i3s/i3s-source.ts +119 -18
- package/src/tileset-3d/helpers/bounding-volume.ts +3 -55
- package/src/tileset-3d/helpers/frame-state.ts +3 -0
- package/src/tileset-3d/helpers/i3s-lod.ts +22 -8
- package/src/types.ts +3 -0
|
@@ -0,0 +1,957 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {Matrix3, Matrix4, Quaternion, Vector3} from '@math.gl/core';
|
|
6
|
+
import {Ellipsoid} from '@math.gl/geospatial';
|
|
7
|
+
import {
|
|
8
|
+
getSpatialCoordinateFrame,
|
|
9
|
+
SpatialCoordinateTransformer
|
|
10
|
+
} from './spatial-coordinate-transformer';
|
|
11
|
+
import {
|
|
12
|
+
createTilesetSpatialReference,
|
|
13
|
+
markTilesetSpatialReferenceTransformed
|
|
14
|
+
} from './spatial-types';
|
|
15
|
+
import {getI3SLinearUnitScale} from './i3s-elevation';
|
|
16
|
+
import type {
|
|
17
|
+
TilesetCoordinateFrame,
|
|
18
|
+
TilesetElevationBounds,
|
|
19
|
+
TilesetElevationMode,
|
|
20
|
+
TilesetElevationProvider,
|
|
21
|
+
TilesetSpatialOptions,
|
|
22
|
+
TilesetSpatialReference
|
|
23
|
+
} from './spatial-types';
|
|
24
|
+
|
|
25
|
+
/** Minimal I3S oriented bounding box required by the spatial adapter. */
|
|
26
|
+
export type I3SSpatialObb = {
|
|
27
|
+
/** Center in source CRS coordinates. */
|
|
28
|
+
center: ArrayLike<number>;
|
|
29
|
+
/** Half lengths along the oriented axes. */
|
|
30
|
+
halfSize: ArrayLike<number>;
|
|
31
|
+
/** Orientation quaternion. */
|
|
32
|
+
quaternion: ArrayLike<number>;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** Input used to rebuild an I3S node bound in the selected output frame. */
|
|
36
|
+
export type I3SSpatialBounds = {
|
|
37
|
+
/** Minimum bounding sphere encoded as source center plus radius. */
|
|
38
|
+
mbs?: ArrayLike<number>;
|
|
39
|
+
/** Oriented source bound, preferred when available. */
|
|
40
|
+
obb?: I3SSpatialObb;
|
|
41
|
+
/** Frame in which I3S vectors and quaternions are expressed. */
|
|
42
|
+
normalReferenceFrame?: string;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** Renderer-ready positions and metadata produced for an I3S geometry resource. */
|
|
46
|
+
export type I3STransformedPositions = {
|
|
47
|
+
/** Float32 offsets from the stable target origin. */
|
|
48
|
+
positions: Float32Array;
|
|
49
|
+
/** Reconstructed absolute source positions retained for normal transformation. */
|
|
50
|
+
sourcePositions: Float64Array;
|
|
51
|
+
/** Stable origin in target CRS coordinates. */
|
|
52
|
+
origin: [number, number, number];
|
|
53
|
+
/** Origin used by geographic deck.gl coordinate systems. */
|
|
54
|
+
cartographicOrigin: [number, number, number];
|
|
55
|
+
/** Renderer coordinate-system discriminator. */
|
|
56
|
+
coordinateSystem: 'cartesian' | 'lnglat-offsets';
|
|
57
|
+
/** Translation matrix used for Cartesian target offsets. */
|
|
58
|
+
modelMatrix: Matrix4;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Applies one normalized I3S horizontal CRS operation consistently to geometry, normals, origins,
|
|
63
|
+
* and node bounds.
|
|
64
|
+
*/
|
|
65
|
+
export class I3SSpatialTransformer {
|
|
66
|
+
/** Descriptor attached to output after all adapter-owned spatial values have been transformed. */
|
|
67
|
+
readonly spatialReference: TilesetSpatialReference;
|
|
68
|
+
|
|
69
|
+
/** Broad coordinate frame of the selected output CRS. */
|
|
70
|
+
readonly targetCoordinateFrame: TilesetCoordinateFrame;
|
|
71
|
+
|
|
72
|
+
/** Source-to-target position operation. */
|
|
73
|
+
private readonly positionTransformer: SpatialCoordinateTransformer;
|
|
74
|
+
|
|
75
|
+
/** Source-to-WGS84 geographic operation used for vector-frame conversion. */
|
|
76
|
+
private readonly sourceToGeographicTransformer: SpatialCoordinateTransformer;
|
|
77
|
+
|
|
78
|
+
/** WGS84-geographic-to-target operation used for normal finite differences. */
|
|
79
|
+
private readonly geographicToTargetTransformer: SpatialCoordinateTransformer;
|
|
80
|
+
|
|
81
|
+
/** Source frame recorded by I3S metadata. */
|
|
82
|
+
private readonly sourceCoordinateFrame: TilesetCoordinateFrame;
|
|
83
|
+
|
|
84
|
+
/** Source Z multiplier that normalizes stored elevation values to meters. */
|
|
85
|
+
private readonly sourceVerticalUnitScale: number;
|
|
86
|
+
|
|
87
|
+
/** I3S elevation placement mode. */
|
|
88
|
+
private readonly elevationMode?: TilesetElevationMode;
|
|
89
|
+
|
|
90
|
+
/** I3S elevation offset normalized to meters. */
|
|
91
|
+
private readonly elevationOffset: number;
|
|
92
|
+
|
|
93
|
+
/** Terrain or scene provider selected for the declared placement mode. */
|
|
94
|
+
private readonly elevationProvider?: TilesetElevationProvider;
|
|
95
|
+
|
|
96
|
+
/** Provider height multiplier that normalizes returned values to meters. */
|
|
97
|
+
private readonly providerUnitScale: number;
|
|
98
|
+
|
|
99
|
+
/** Optional provider-height to layer-height datum operation. */
|
|
100
|
+
private readonly providerHeightTransformer?: SpatialCoordinateTransformer;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Creates an I3S spatial adapter.
|
|
104
|
+
*
|
|
105
|
+
* @param spatialReference - Transformable I3S spatial descriptor.
|
|
106
|
+
* @param options - Registered geoid and custom resource options used by the shared transformer.
|
|
107
|
+
*/
|
|
108
|
+
constructor(spatialReference: TilesetSpatialReference, options: TilesetSpatialOptions = {}) {
|
|
109
|
+
if (spatialReference.status !== 'transformable' && spatialReference.status !== 'transformed') {
|
|
110
|
+
throw new Error('I3SSpatialTransformer requires a requested, transformable spatial output');
|
|
111
|
+
}
|
|
112
|
+
const targetCrs = spatialReference.targetCrs || spatialReference.sourceCrs;
|
|
113
|
+
if (!targetCrs) {
|
|
114
|
+
throw new Error('I3S spatial transformation requires a target CRS');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
this.positionTransformer = new SpatialCoordinateTransformer(spatialReference, options);
|
|
118
|
+
this.sourceCoordinateFrame = spatialReference.coordinateFrame;
|
|
119
|
+
this.sourceVerticalUnitScale = spatialReference.verticalUnitScale;
|
|
120
|
+
this.elevationMode = spatialReference.elevationMode;
|
|
121
|
+
this.elevationOffset =
|
|
122
|
+
(spatialReference.elevationOffset || 0) * spatialReference.elevationUnitScale;
|
|
123
|
+
this.elevationProvider = getElevationProvider(this.elevationMode, options);
|
|
124
|
+
if (
|
|
125
|
+
this.elevationProvider &&
|
|
126
|
+
(typeof this.elevationProvider.sampleElevations !== 'function' ||
|
|
127
|
+
typeof this.elevationProvider.getElevationRange !== 'function')
|
|
128
|
+
) {
|
|
129
|
+
throw new Error(
|
|
130
|
+
'An I3S elevation provider must implement sampleElevations() and getElevationRange()'
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
const providerUnitScale = getI3SLinearUnitScale(this.elevationProvider?.unit || 'meter');
|
|
134
|
+
if (this.elevationProvider && providerUnitScale === undefined) {
|
|
135
|
+
throw new Error(`Unsupported elevation-provider unit ${this.elevationProvider.unit}`);
|
|
136
|
+
}
|
|
137
|
+
this.providerUnitScale = providerUnitScale || 1;
|
|
138
|
+
this.targetCoordinateFrame = getSpatialCoordinateFrame(targetCrs);
|
|
139
|
+
this.spatialReference = markTilesetSpatialReferenceTransformed(spatialReference);
|
|
140
|
+
|
|
141
|
+
const sourceToGeographicReference = createTilesetSpatialReference(
|
|
142
|
+
{
|
|
143
|
+
sourceCrs: spatialReference.sourceCrs,
|
|
144
|
+
heightReference: spatialReference.heightReference,
|
|
145
|
+
coordinateFrame: spatialReference.coordinateFrame,
|
|
146
|
+
axisOrder: spatialReference.axisOrder,
|
|
147
|
+
provenance: spatialReference.provenance
|
|
148
|
+
},
|
|
149
|
+
{targetCrs: 'EPSG:4326'}
|
|
150
|
+
);
|
|
151
|
+
this.sourceToGeographicTransformer = new SpatialCoordinateTransformer(
|
|
152
|
+
sourceToGeographicReference
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
if (requiresElevationSurface(this.elevationMode)) {
|
|
156
|
+
if (!this.elevationProvider) {
|
|
157
|
+
const providerName = this.elevationMode === 'relativeToScene' ? 'scene' : 'terrain';
|
|
158
|
+
throw new Error(
|
|
159
|
+
`I3S elevation mode ${this.elevationMode} requires a ${providerName} elevation provider`
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
if (spatialReference.heightReference === 'unknown') {
|
|
163
|
+
throw new Error(
|
|
164
|
+
`I3S elevation mode ${this.elevationMode} requires a known source height reference`
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
const providerHeightReference =
|
|
168
|
+
this.elevationProvider.heightReference || spatialReference.heightReference;
|
|
169
|
+
if (providerHeightReference !== spatialReference.heightReference) {
|
|
170
|
+
if (
|
|
171
|
+
spatialReference.heightReference !== 'ellipsoidal' &&
|
|
172
|
+
spatialReference.heightReference !== 'orthometric'
|
|
173
|
+
) {
|
|
174
|
+
throw new Error(
|
|
175
|
+
'Converting elevation-provider heights requires an ellipsoidal or orthometric source height reference'
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
const providerHeightSpatialReference = createTilesetSpatialReference(
|
|
179
|
+
{
|
|
180
|
+
sourceCrs: 'EPSG:4326',
|
|
181
|
+
heightReference: providerHeightReference,
|
|
182
|
+
coordinateFrame: 'geographic',
|
|
183
|
+
axisOrder: 'xyz',
|
|
184
|
+
provenance: 'caller-override'
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
targetCrs: 'EPSG:4326',
|
|
188
|
+
targetHeightReference: spatialReference.heightReference,
|
|
189
|
+
geoidModel: options.geoidModel
|
|
190
|
+
}
|
|
191
|
+
);
|
|
192
|
+
this.providerHeightTransformer = new SpatialCoordinateTransformer(
|
|
193
|
+
providerHeightSpatialReference,
|
|
194
|
+
options
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const geographicToTargetReference = createTilesetSpatialReference(
|
|
200
|
+
{
|
|
201
|
+
sourceCrs: 'EPSG:4326',
|
|
202
|
+
heightReference: spatialReference.heightReference,
|
|
203
|
+
coordinateFrame: 'geographic',
|
|
204
|
+
axisOrder: 'xyz',
|
|
205
|
+
provenance: 'format-default'
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
targetCrs,
|
|
209
|
+
targetHeightReference: spatialReference.targetHeightReference,
|
|
210
|
+
geoidModel: options.geoidModel
|
|
211
|
+
}
|
|
212
|
+
);
|
|
213
|
+
this.geographicToTargetTransformer = new SpatialCoordinateTransformer(
|
|
214
|
+
geographicToTargetReference,
|
|
215
|
+
options
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** Transform one absolute I3S source position. */
|
|
220
|
+
transformPosition(position: ArrayLike<number>): [number, number, number] {
|
|
221
|
+
const placedPosition = this.placeSourcePositionsSync(position);
|
|
222
|
+
const transformed = this.positionTransformer.transformPosition([
|
|
223
|
+
placedPosition[0],
|
|
224
|
+
placedPosition[1],
|
|
225
|
+
placedPosition[2]
|
|
226
|
+
]);
|
|
227
|
+
return [transformed[0], transformed[1], transformed[2]];
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** Transform one absolute I3S source position after asynchronous surface placement. */
|
|
231
|
+
async transformPositionAsync(position: ArrayLike<number>): Promise<[number, number, number]> {
|
|
232
|
+
const placedPosition = await this.placeSourcePositions(position);
|
|
233
|
+
const transformed = this.positionTransformer.transformPosition([
|
|
234
|
+
placedPosition[0],
|
|
235
|
+
placedPosition[1],
|
|
236
|
+
placedPosition[2]
|
|
237
|
+
]);
|
|
238
|
+
return [transformed[0], transformed[1], transformed[2]];
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Transform an already placed, meter-normalized position to the requested target. */
|
|
242
|
+
private transformPlacedPosition(position: ArrayLike<number>): [number, number, number] {
|
|
243
|
+
const transformed = this.positionTransformer.transformPosition([
|
|
244
|
+
position[0],
|
|
245
|
+
position[1],
|
|
246
|
+
position[2]
|
|
247
|
+
]);
|
|
248
|
+
return [transformed[0], transformed[1], transformed[2]];
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** Transform one absolute I3S source position to WGS84 longitude, latitude, and height. */
|
|
252
|
+
transformSourcePositionToGeographic(position: ArrayLike<number>): [number, number, number] {
|
|
253
|
+
const placedPosition = this.placeSourcePositionsSync(position);
|
|
254
|
+
const transformed = this.sourceToGeographicTransformer.transformPosition([
|
|
255
|
+
placedPosition[0],
|
|
256
|
+
placedPosition[1],
|
|
257
|
+
placedPosition[2]
|
|
258
|
+
]);
|
|
259
|
+
return [transformed[0], transformed[1], transformed[2]];
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** Transform one source position to WGS84 after asynchronous elevation placement. */
|
|
263
|
+
async transformSourcePositionToGeographicAsync(
|
|
264
|
+
position: ArrayLike<number>
|
|
265
|
+
): Promise<[number, number, number]> {
|
|
266
|
+
const placedPosition = await this.placeSourcePositions(position);
|
|
267
|
+
const transformed = this.sourceToGeographicTransformer.transformPosition([
|
|
268
|
+
placedPosition[0],
|
|
269
|
+
placedPosition[1],
|
|
270
|
+
placedPosition[2]
|
|
271
|
+
]);
|
|
272
|
+
return [transformed[0], transformed[1], transformed[2]];
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Transform absolute Float64 positions and return stable Float32 offsets around a target origin.
|
|
277
|
+
*
|
|
278
|
+
* @param sourcePositions - Absolute positions in source CRS order.
|
|
279
|
+
* @param sourceOrigin - Stable source origin, normally the node MBS or OBB center.
|
|
280
|
+
*/
|
|
281
|
+
transformPositions(
|
|
282
|
+
sourcePositions: ArrayLike<number>,
|
|
283
|
+
sourceOrigin: ArrayLike<number>
|
|
284
|
+
): I3STransformedPositions {
|
|
285
|
+
const placedSourcePositions = this.placeSourcePositionsSync(sourcePositions);
|
|
286
|
+
const placedSourceOrigin = this.placeSourcePositionsSync(sourceOrigin);
|
|
287
|
+
return this.transformPlacedPositions(placedSourcePositions, placedSourceOrigin);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Transform positions after asynchronously sampling any requested terrain or scene surface.
|
|
292
|
+
*
|
|
293
|
+
* @param sourcePositions - Absolute positions in source CRS order.
|
|
294
|
+
* @param sourceOrigin - Stable source origin, normally the node MBS or OBB center.
|
|
295
|
+
* @returns Renderer-ready target-frame offsets and stable origin metadata.
|
|
296
|
+
*/
|
|
297
|
+
async transformPositionsAsync(
|
|
298
|
+
sourcePositions: ArrayLike<number>,
|
|
299
|
+
sourceOrigin: ArrayLike<number>
|
|
300
|
+
): Promise<I3STransformedPositions> {
|
|
301
|
+
const combinedPositions = new Float64Array(sourcePositions.length + 3);
|
|
302
|
+
combinedPositions.set(Array.from(sourcePositions), 0);
|
|
303
|
+
combinedPositions.set(Array.from(sourceOrigin).slice(0, 3), sourcePositions.length);
|
|
304
|
+
const placedPositions = await this.placeSourcePositions(combinedPositions);
|
|
305
|
+
return this.transformPlacedPositions(
|
|
306
|
+
placedPositions.subarray(0, sourcePositions.length),
|
|
307
|
+
placedPositions.subarray(sourcePositions.length)
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** Transform already placed positions into renderer-relative target coordinates. */
|
|
312
|
+
private transformPlacedPositions(
|
|
313
|
+
sourcePositions: ArrayLike<number>,
|
|
314
|
+
sourceOrigin: ArrayLike<number>
|
|
315
|
+
): I3STransformedPositions {
|
|
316
|
+
const origin = this.transformPlacedPosition(sourceOrigin);
|
|
317
|
+
const positions = new Float32Array(sourcePositions.length);
|
|
318
|
+
const absoluteSourcePositions = Float64Array.from(sourcePositions);
|
|
319
|
+
|
|
320
|
+
for (let index = 0; index < sourcePositions.length; index += 3) {
|
|
321
|
+
const transformed = this.transformPlacedPosition([
|
|
322
|
+
sourcePositions[index],
|
|
323
|
+
sourcePositions[index + 1],
|
|
324
|
+
sourcePositions[index + 2]
|
|
325
|
+
]);
|
|
326
|
+
positions[index] = transformed[0] - origin[0];
|
|
327
|
+
positions[index + 1] = transformed[1] - origin[1];
|
|
328
|
+
positions[index + 2] = transformed[2] - origin[2];
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const isGeographic = this.targetCoordinateFrame === 'geographic';
|
|
332
|
+
const modelMatrix = isGeographic ? new Matrix4() : new Matrix4().translate(origin);
|
|
333
|
+
return {
|
|
334
|
+
positions,
|
|
335
|
+
sourcePositions: absoluteSourcePositions,
|
|
336
|
+
origin,
|
|
337
|
+
cartographicOrigin: isGeographic ? origin : [0, 0, 0],
|
|
338
|
+
coordinateSystem: isGeographic ? 'lnglat-offsets' : 'cartesian',
|
|
339
|
+
modelMatrix
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Transform I3S normals into the selected target basis.
|
|
345
|
+
*
|
|
346
|
+
* Earth-centered normals are rotated directly. Vertex-reference-frame normals are first mapped
|
|
347
|
+
* through the local east/north/up basis at each source position.
|
|
348
|
+
*
|
|
349
|
+
* @param normals - Three-component normal vectors.
|
|
350
|
+
* @param sourcePositions - Absolute source position paired with each normal.
|
|
351
|
+
* @param normalReferenceFrame - I3S normal frame declaration.
|
|
352
|
+
*/
|
|
353
|
+
transformNormals(
|
|
354
|
+
normals: ArrayLike<number>,
|
|
355
|
+
sourcePositions: ArrayLike<number>,
|
|
356
|
+
normalReferenceFrame = 'earth-centered'
|
|
357
|
+
): Float32Array {
|
|
358
|
+
const transformedNormals = new Float32Array(normals.length);
|
|
359
|
+
for (let index = 0; index < normals.length; index += 3) {
|
|
360
|
+
const geographic = this.sourceToGeographicTransformer.transformPosition([
|
|
361
|
+
sourcePositions[index],
|
|
362
|
+
sourcePositions[index + 1],
|
|
363
|
+
sourcePositions[index + 2]
|
|
364
|
+
]);
|
|
365
|
+
const inputNormal = new Vector3([
|
|
366
|
+
normals[index],
|
|
367
|
+
normals[index + 1],
|
|
368
|
+
normals[index + 2]
|
|
369
|
+
]).normalize();
|
|
370
|
+
const ecefNormal =
|
|
371
|
+
normalReferenceFrame === 'earth-centered'
|
|
372
|
+
? inputNormal
|
|
373
|
+
: localEnuVectorToEcef(inputNormal, geographic);
|
|
374
|
+
const outputNormal = this.transformEcefNormal(ecefNormal, geographic);
|
|
375
|
+
transformedNormals.set(outputNormal, index);
|
|
376
|
+
}
|
|
377
|
+
return transformedNormals;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Rebuild an I3S node bound from transformed corner or sphere-axis samples.
|
|
382
|
+
*
|
|
383
|
+
* @param bounds - Source MBS/OBB fields.
|
|
384
|
+
* @returns A generic tile `region` for geographic output or axis-aligned `box` otherwise.
|
|
385
|
+
*/
|
|
386
|
+
transformBoundingVolume(bounds: I3SSpatialBounds): {box?: number[]; region?: number[]} {
|
|
387
|
+
const geographicSamples = this.getGeographicBoundSamples(bounds);
|
|
388
|
+
const targetSamples = geographicSamples.map(position =>
|
|
389
|
+
this.geographicToTargetTransformer.transformPosition(position)
|
|
390
|
+
);
|
|
391
|
+
return this.targetCoordinateFrame === 'geographic'
|
|
392
|
+
? {region: getGeographicRegion(targetSamples)}
|
|
393
|
+
: {box: getAxisAlignedBox(targetSamples)};
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/** Rebuild an I3S bound in the target frame after asynchronous elevation placement. */
|
|
397
|
+
async transformBoundingVolumeAsync(
|
|
398
|
+
bounds: I3SSpatialBounds
|
|
399
|
+
): Promise<{box?: number[]; region?: number[]}> {
|
|
400
|
+
const geographicSamples = await this.getGeographicBoundSamplesAsync(bounds);
|
|
401
|
+
const targetSamples = geographicSamples.map(position =>
|
|
402
|
+
this.geographicToTargetTransformer.transformPosition(position)
|
|
403
|
+
);
|
|
404
|
+
return this.targetCoordinateFrame === 'geographic'
|
|
405
|
+
? {region: getGeographicRegion(targetSamples)}
|
|
406
|
+
: {box: getAxisAlignedBox(targetSamples)};
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/** Rebuild an I3S bound as a WGS84 geographic region for geographic traversal algorithms. */
|
|
410
|
+
transformBoundingVolumeToGeographic(bounds: I3SSpatialBounds): {region: number[]} {
|
|
411
|
+
return {region: getGeographicRegion(this.getGeographicBoundSamples(bounds))};
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/** Rebuild an I3S bound as WGS84 geographic after asynchronous elevation placement. */
|
|
415
|
+
async transformBoundingVolumeToGeographicAsync(
|
|
416
|
+
bounds: I3SSpatialBounds
|
|
417
|
+
): Promise<{region: number[]}> {
|
|
418
|
+
return {region: getGeographicRegion(await this.getGeographicBoundSamplesAsync(bounds))};
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/** Rebuild an I3S bound as a WGS84 ECEF box for generic `Tileset3D` traversal. */
|
|
422
|
+
transformBoundingVolumeToGeocentric(bounds: I3SSpatialBounds): {box: number[]} {
|
|
423
|
+
const cartesianSamples = this.getGeographicBoundSamples(bounds).map(position =>
|
|
424
|
+
Array.from(Ellipsoid.WGS84.cartographicToCartesian(new Vector3(position)))
|
|
425
|
+
);
|
|
426
|
+
return {box: getAxisAlignedBox(cartesianSamples)};
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** Rebuild an I3S bound as WGS84 ECEF after asynchronous elevation placement. */
|
|
430
|
+
async transformBoundingVolumeToGeocentricAsync(
|
|
431
|
+
bounds: I3SSpatialBounds
|
|
432
|
+
): Promise<{box: number[]}> {
|
|
433
|
+
const cartesianSamples = (await this.getGeographicBoundSamplesAsync(bounds)).map(position =>
|
|
434
|
+
Array.from(Ellipsoid.WGS84.cartographicToCartesian(new Vector3(position)))
|
|
435
|
+
);
|
|
436
|
+
return {box: getAxisAlignedBox(cartesianSamples)};
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/** Return a WGS84 geographic MBS used by the I3S screen-threshold calculation. */
|
|
440
|
+
transformBoundingSphereToGeographic(bounds: I3SSpatialBounds): [number, number, number, number] {
|
|
441
|
+
const sourceCenter = bounds.mbs?.length
|
|
442
|
+
? Array.from(bounds.mbs).slice(0, 3)
|
|
443
|
+
: bounds.obb
|
|
444
|
+
? Array.from(bounds.obb.center).slice(0, 3)
|
|
445
|
+
: null;
|
|
446
|
+
if (!sourceCenter) {
|
|
447
|
+
throw new Error('I3S spatial transformation requires an MBS or OBB bound');
|
|
448
|
+
}
|
|
449
|
+
const placedCenter = this.placeSourcePositionsSync(sourceCenter);
|
|
450
|
+
const geographicCenter = this.sourceToGeographicTransformer.transformPosition(
|
|
451
|
+
Array.from(placedCenter)
|
|
452
|
+
);
|
|
453
|
+
const radius = bounds.mbs?.length
|
|
454
|
+
? bounds.mbs[3]
|
|
455
|
+
: Math.hypot(...Array.from(bounds.obb!.halfSize).slice(0, 3));
|
|
456
|
+
return [geographicCenter[0], geographicCenter[1], geographicCenter[2], radius];
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/** Return a placed WGS84 MBS used by the I3S screen-threshold calculation. */
|
|
460
|
+
async transformBoundingSphereToGeographicAsync(
|
|
461
|
+
bounds: I3SSpatialBounds
|
|
462
|
+
): Promise<[number, number, number, number]> {
|
|
463
|
+
return getGeographicBoundingSphere(await this.getGeographicBoundSamplesAsync(bounds));
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Rebuild traversal, target-output, and I3S LOD bounds from one elevation-sampling batch.
|
|
468
|
+
*
|
|
469
|
+
* @param bounds - Source MBS/OBB fields.
|
|
470
|
+
* @returns Parallel bounds in WGS84 ECEF, the selected output frame, and WGS84 geographic.
|
|
471
|
+
*/
|
|
472
|
+
async transformBoundsAsync(bounds: I3SSpatialBounds): Promise<{
|
|
473
|
+
boundingVolume: {box: number[]};
|
|
474
|
+
spatialBoundingVolume: {box?: number[]; region?: number[]};
|
|
475
|
+
i3sLodMbs: [number, number, number, number];
|
|
476
|
+
}> {
|
|
477
|
+
const geographicSamples = await this.getGeographicBoundSamplesAsync(bounds);
|
|
478
|
+
const cartesianSamples = geographicSamples.map(position =>
|
|
479
|
+
Array.from(Ellipsoid.WGS84.cartographicToCartesian(new Vector3(position)))
|
|
480
|
+
);
|
|
481
|
+
const targetSamples = geographicSamples.map(position =>
|
|
482
|
+
this.geographicToTargetTransformer.transformPosition(position)
|
|
483
|
+
);
|
|
484
|
+
return {
|
|
485
|
+
boundingVolume: {box: getAxisAlignedBox(cartesianSamples)},
|
|
486
|
+
spatialBoundingVolume:
|
|
487
|
+
this.targetCoordinateFrame === 'geographic'
|
|
488
|
+
? {region: getGeographicRegion(targetSamples)}
|
|
489
|
+
: {box: getAxisAlignedBox(targetSamples)},
|
|
490
|
+
i3sLodMbs: requiresElevationSurface(this.elevationMode)
|
|
491
|
+
? getGeographicBoundingSphere(geographicSamples)
|
|
492
|
+
: this.transformBoundingSphereToGeographic(bounds)
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Rebuild Point Cloud traversal and target-output bounds from one elevation-sampling batch.
|
|
498
|
+
*
|
|
499
|
+
* @param bounds - Source OBB fields.
|
|
500
|
+
* @returns WGS84 geographic traversal bound plus the selected output-frame bound.
|
|
501
|
+
*/
|
|
502
|
+
async transformPointCloudBoundsAsync(bounds: I3SSpatialBounds): Promise<{
|
|
503
|
+
boundingVolume: {region: number[]};
|
|
504
|
+
spatialBoundingVolume: {box?: number[]; region?: number[]};
|
|
505
|
+
}> {
|
|
506
|
+
const geographicSamples = await this.getGeographicBoundSamplesAsync(bounds);
|
|
507
|
+
const targetSamples = geographicSamples.map(position =>
|
|
508
|
+
this.geographicToTargetTransformer.transformPosition(position)
|
|
509
|
+
);
|
|
510
|
+
return {
|
|
511
|
+
boundingVolume: {region: getGeographicRegion(geographicSamples)},
|
|
512
|
+
spatialBoundingVolume:
|
|
513
|
+
this.targetCoordinateFrame === 'geographic'
|
|
514
|
+
? {region: getGeographicRegion(targetSamples)}
|
|
515
|
+
: {box: getAxisAlignedBox(targetSamples)}
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/** Sample a source I3S bound and normalize every sample to WGS84 geographic coordinates. */
|
|
520
|
+
private getGeographicBoundSamples(bounds: I3SSpatialBounds): number[][] {
|
|
521
|
+
const sourceSamples = getSourceBoundSamples(
|
|
522
|
+
bounds,
|
|
523
|
+
this.sourceCoordinateFrame,
|
|
524
|
+
this.sourceToGeographicTransformer,
|
|
525
|
+
this.sourceVerticalUnitScale
|
|
526
|
+
);
|
|
527
|
+
if (!sourceSamples.length) {
|
|
528
|
+
throw new Error('I3S spatial transformation requires an MBS or OBB bound');
|
|
529
|
+
}
|
|
530
|
+
const placedSourceSamples = this.placeSourcePositionsSync(sourceSamples.flat(), true);
|
|
531
|
+
const placedSamples = toPositionArrays(placedSourceSamples);
|
|
532
|
+
return this.sourceCoordinateFrame === 'geographic'
|
|
533
|
+
? placedSamples
|
|
534
|
+
: placedSamples.map(position =>
|
|
535
|
+
this.sourceToGeographicTransformer.transformPosition(position)
|
|
536
|
+
);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/** Sample, place, and normalize one source bound to WGS84 geographic coordinates. */
|
|
540
|
+
private async getGeographicBoundSamplesAsync(bounds: I3SSpatialBounds): Promise<number[][]> {
|
|
541
|
+
const sourceSamples = getSourceBoundSamples(
|
|
542
|
+
bounds,
|
|
543
|
+
this.sourceCoordinateFrame,
|
|
544
|
+
this.sourceToGeographicTransformer,
|
|
545
|
+
this.sourceVerticalUnitScale
|
|
546
|
+
);
|
|
547
|
+
if (!sourceSamples.length) {
|
|
548
|
+
throw new Error('I3S spatial transformation requires an MBS or OBB bound');
|
|
549
|
+
}
|
|
550
|
+
if (requiresElevationSurface(this.elevationMode)) {
|
|
551
|
+
const geographicSamples =
|
|
552
|
+
this.sourceCoordinateFrame === 'geographic'
|
|
553
|
+
? sourceSamples
|
|
554
|
+
: sourceSamples.map(position =>
|
|
555
|
+
this.sourceToGeographicTransformer.transformPosition(position)
|
|
556
|
+
);
|
|
557
|
+
return await this.placeGeographicBoundSamples(geographicSamples);
|
|
558
|
+
}
|
|
559
|
+
const placedSamples = toPositionArrays(
|
|
560
|
+
await this.placeSourcePositions(sourceSamples.flat(), true)
|
|
561
|
+
);
|
|
562
|
+
return this.sourceCoordinateFrame === 'geographic'
|
|
563
|
+
? placedSamples
|
|
564
|
+
: placedSamples.map(position =>
|
|
565
|
+
this.sourceToGeographicTransformer.transformPosition(position)
|
|
566
|
+
);
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/** Apply a provider's conservative surface range to an entire geographic bound footprint. */
|
|
570
|
+
private async placeGeographicBoundSamples(geographicSamples: number[][]): Promise<number[][]> {
|
|
571
|
+
const elevationProvider = this.elevationProvider!;
|
|
572
|
+
const elevationRange = await elevationProvider.getElevationRange(
|
|
573
|
+
getElevationBounds(geographicSamples)
|
|
574
|
+
);
|
|
575
|
+
const minimumProviderHeight = elevationRange.minimum * this.providerUnitScale;
|
|
576
|
+
const maximumProviderHeight = elevationRange.maximum * this.providerUnitScale;
|
|
577
|
+
if (
|
|
578
|
+
!Number.isFinite(minimumProviderHeight) ||
|
|
579
|
+
!Number.isFinite(maximumProviderHeight) ||
|
|
580
|
+
minimumProviderHeight > maximumProviderHeight
|
|
581
|
+
) {
|
|
582
|
+
throw new Error(
|
|
583
|
+
'Elevation provider returned an invalid range; minimum and maximum must be finite and ordered'
|
|
584
|
+
);
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
const sourceHeights = geographicSamples.map(position => position[2]);
|
|
588
|
+
const minimumSourceHeight = Math.min(...sourceHeights);
|
|
589
|
+
const maximumSourceHeight = Math.max(...sourceHeights);
|
|
590
|
+
const placedSamples: number[][] = [];
|
|
591
|
+
for (const [longitude, latitude] of geographicSamples) {
|
|
592
|
+
const convertedMinimum = this.providerHeightTransformer
|
|
593
|
+
? this.providerHeightTransformer.transformPosition([
|
|
594
|
+
longitude,
|
|
595
|
+
latitude,
|
|
596
|
+
minimumProviderHeight
|
|
597
|
+
])[2]
|
|
598
|
+
: minimumProviderHeight;
|
|
599
|
+
const convertedMaximum = this.providerHeightTransformer
|
|
600
|
+
? this.providerHeightTransformer.transformPosition([
|
|
601
|
+
longitude,
|
|
602
|
+
latitude,
|
|
603
|
+
maximumProviderHeight
|
|
604
|
+
])[2]
|
|
605
|
+
: maximumProviderHeight;
|
|
606
|
+
const minimumSurfaceHeight = Math.min(convertedMinimum, convertedMaximum);
|
|
607
|
+
const maximumSurfaceHeight = Math.max(convertedMinimum, convertedMaximum);
|
|
608
|
+
const minimumHeight =
|
|
609
|
+
this.elevationMode === 'onTheGround'
|
|
610
|
+
? minimumSurfaceHeight
|
|
611
|
+
: minimumSurfaceHeight + minimumSourceHeight + this.elevationOffset;
|
|
612
|
+
const maximumHeight =
|
|
613
|
+
this.elevationMode === 'onTheGround'
|
|
614
|
+
? maximumSurfaceHeight
|
|
615
|
+
: maximumSurfaceHeight + maximumSourceHeight + this.elevationOffset;
|
|
616
|
+
placedSamples.push(
|
|
617
|
+
[longitude, latitude, minimumHeight],
|
|
618
|
+
[longitude, latitude, maximumHeight]
|
|
619
|
+
);
|
|
620
|
+
}
|
|
621
|
+
return placedSamples;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
/** Apply source units and non-surface placement without awaiting external resources. */
|
|
625
|
+
private placeSourcePositionsSync(
|
|
626
|
+
sourcePositions: ArrayLike<number>,
|
|
627
|
+
sourceHeightsAreMeters = false
|
|
628
|
+
): Float64Array {
|
|
629
|
+
if (requiresElevationSurface(this.elevationMode)) {
|
|
630
|
+
throw new Error(
|
|
631
|
+
`I3S elevation mode ${this.elevationMode} requires asynchronous surface placement`
|
|
632
|
+
);
|
|
633
|
+
}
|
|
634
|
+
const positions = Float64Array.from(sourcePositions);
|
|
635
|
+
const offset = this.elevationMode === 'absoluteHeight' ? this.elevationOffset : 0;
|
|
636
|
+
for (let index = 2; index < positions.length; index += 3) {
|
|
637
|
+
positions[index] =
|
|
638
|
+
positions[index] * (sourceHeightsAreMeters ? 1 : this.sourceVerticalUnitScale) + offset;
|
|
639
|
+
}
|
|
640
|
+
return positions;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
/** Apply source units and all I3S elevation-placement modes. */
|
|
644
|
+
private async placeSourcePositions(
|
|
645
|
+
sourcePositions: ArrayLike<number>,
|
|
646
|
+
sourceHeightsAreMeters = false
|
|
647
|
+
): Promise<Float64Array> {
|
|
648
|
+
if (!requiresElevationSurface(this.elevationMode)) {
|
|
649
|
+
return this.placeSourcePositionsSync(sourcePositions, sourceHeightsAreMeters);
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
const positions = Float64Array.from(sourcePositions);
|
|
653
|
+
const geographicLocations = toPositionArrays(positions).map(position => {
|
|
654
|
+
const geographic = this.sourceToGeographicTransformer.transformPosition(position);
|
|
655
|
+
return [geographic[0], geographic[1]] as const;
|
|
656
|
+
});
|
|
657
|
+
const sampledHeights = await this.elevationProvider!.sampleElevations(geographicLocations);
|
|
658
|
+
if (sampledHeights.length !== geographicLocations.length) {
|
|
659
|
+
throw new Error(
|
|
660
|
+
`Elevation provider returned ${sampledHeights.length} heights for ${geographicLocations.length} positions`
|
|
661
|
+
);
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
for (let positionIndex = 0; positionIndex < geographicLocations.length; positionIndex++) {
|
|
665
|
+
const sampledHeight = sampledHeights[positionIndex] * this.providerUnitScale;
|
|
666
|
+
if (!Number.isFinite(sampledHeight)) {
|
|
667
|
+
throw new Error(
|
|
668
|
+
`Elevation provider returned a non-finite height at index ${positionIndex}`
|
|
669
|
+
);
|
|
670
|
+
}
|
|
671
|
+
const [longitude, latitude] = geographicLocations[positionIndex];
|
|
672
|
+
const surfaceHeight = this.providerHeightTransformer
|
|
673
|
+
? this.providerHeightTransformer.transformPosition([longitude, latitude, sampledHeight])[2]
|
|
674
|
+
: sampledHeight;
|
|
675
|
+
const sourceHeight =
|
|
676
|
+
positions[positionIndex * 3 + 2] *
|
|
677
|
+
(sourceHeightsAreMeters ? 1 : this.sourceVerticalUnitScale);
|
|
678
|
+
positions[positionIndex * 3 + 2] =
|
|
679
|
+
this.elevationMode === 'onTheGround'
|
|
680
|
+
? surfaceHeight
|
|
681
|
+
: surfaceHeight + sourceHeight + this.elevationOffset;
|
|
682
|
+
}
|
|
683
|
+
return positions;
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
/** Transform one earth-centered normal to the selected target basis. */
|
|
687
|
+
private transformEcefNormal(ecefNormal: Vector3, geographic: number[]): Vector3 {
|
|
688
|
+
if (this.targetCoordinateFrame === 'geocentric') {
|
|
689
|
+
return ecefNormal.normalize();
|
|
690
|
+
}
|
|
691
|
+
if (this.targetCoordinateFrame === 'geographic') {
|
|
692
|
+
return ecefVectorToLocalEnu(ecefNormal, geographic).normalize();
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
const cartesian = Ellipsoid.WGS84.cartographicToCartesian(new Vector3(geographic));
|
|
696
|
+
const jacobianColumns: number[] = [];
|
|
697
|
+
for (const direction of getPositiveAxisDirections()) {
|
|
698
|
+
const positiveGeographic = Ellipsoid.WGS84.cartesianToCartographic(
|
|
699
|
+
new Vector3(cartesian).add(direction)
|
|
700
|
+
);
|
|
701
|
+
const negativeGeographic = Ellipsoid.WGS84.cartesianToCartographic(
|
|
702
|
+
new Vector3(cartesian).subtract(direction)
|
|
703
|
+
);
|
|
704
|
+
const positiveTarget =
|
|
705
|
+
this.geographicToTargetTransformer.transformPosition(positiveGeographic);
|
|
706
|
+
const negativeTarget =
|
|
707
|
+
this.geographicToTargetTransformer.transformPosition(negativeGeographic);
|
|
708
|
+
jacobianColumns.push(
|
|
709
|
+
(positiveTarget[0] - negativeTarget[0]) / 2,
|
|
710
|
+
(positiveTarget[1] - negativeTarget[1]) / 2,
|
|
711
|
+
(positiveTarget[2] - negativeTarget[2]) / 2
|
|
712
|
+
);
|
|
713
|
+
}
|
|
714
|
+
const normalMatrix = new Matrix3(jacobianColumns);
|
|
715
|
+
if (Math.abs(normalMatrix.determinant()) < 1e-12) {
|
|
716
|
+
throw new Error('Cannot transform an I3S normal through a singular local projection');
|
|
717
|
+
}
|
|
718
|
+
normalMatrix.invert().transpose();
|
|
719
|
+
return new Vector3(ecefNormal).transformByMatrix3(normalMatrix).normalize();
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
/** Select the application-owned surface required by one I3S elevation mode. */
|
|
724
|
+
function getElevationProvider(
|
|
725
|
+
mode: TilesetElevationMode | undefined,
|
|
726
|
+
options: TilesetSpatialOptions
|
|
727
|
+
): TilesetElevationProvider | undefined {
|
|
728
|
+
return mode === 'relativeToScene'
|
|
729
|
+
? options.sceneElevationProvider
|
|
730
|
+
: requiresElevationSurface(mode)
|
|
731
|
+
? options.terrainElevationProvider
|
|
732
|
+
: undefined;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
/** Return whether one placement mode requires external surface samples. */
|
|
736
|
+
function requiresElevationSurface(mode: TilesetElevationMode | undefined): boolean {
|
|
737
|
+
return mode === 'onTheGround' || mode === 'relativeToGround' || mode === 'relativeToScene';
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
/** Split one packed XYZ sequence into position arrays without losing Float64 precision. */
|
|
741
|
+
function toPositionArrays(positions: ArrayLike<number>): number[][] {
|
|
742
|
+
if (positions.length % 3 !== 0) {
|
|
743
|
+
throw new Error('I3S position arrays must contain complete XYZ tuples');
|
|
744
|
+
}
|
|
745
|
+
const result: number[][] = [];
|
|
746
|
+
for (let index = 0; index < positions.length; index += 3) {
|
|
747
|
+
result.push([positions[index], positions[index + 1], positions[index + 2]]);
|
|
748
|
+
}
|
|
749
|
+
return result;
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
/** Return a dateline-aware WGS84 footprint in degrees for an elevation range request. */
|
|
753
|
+
function getElevationBounds(geographicSamples: number[][]): TilesetElevationBounds {
|
|
754
|
+
const region = getGeographicRegion(geographicSamples);
|
|
755
|
+
return {
|
|
756
|
+
west: (region[0] * 180) / Math.PI,
|
|
757
|
+
south: (region[1] * 180) / Math.PI,
|
|
758
|
+
east: (region[2] * 180) / Math.PI,
|
|
759
|
+
north: (region[3] * 180) / Math.PI
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
/** Build a conservative WGS84 geographic sphere from placed geographic samples. */
|
|
764
|
+
function getGeographicBoundingSphere(
|
|
765
|
+
geographicSamples: number[][]
|
|
766
|
+
): [number, number, number, number] {
|
|
767
|
+
if (!geographicSamples.length) {
|
|
768
|
+
throw new Error('I3S spatial transformation requires at least one bound sample');
|
|
769
|
+
}
|
|
770
|
+
const cartesianSamples = geographicSamples.map(position =>
|
|
771
|
+
Ellipsoid.WGS84.cartographicToCartesian(new Vector3(position))
|
|
772
|
+
);
|
|
773
|
+
const cartesianCenter = new Vector3();
|
|
774
|
+
for (const sample of cartesianSamples) {
|
|
775
|
+
cartesianCenter.add(sample);
|
|
776
|
+
}
|
|
777
|
+
cartesianCenter.scale(1 / cartesianSamples.length);
|
|
778
|
+
let radius = 0;
|
|
779
|
+
for (const sample of cartesianSamples) {
|
|
780
|
+
radius = Math.max(radius, new Vector3(sample).subtract(cartesianCenter).magnitude());
|
|
781
|
+
}
|
|
782
|
+
const geographicCenter = Ellipsoid.WGS84.cartesianToCartographic(cartesianCenter);
|
|
783
|
+
return [geographicCenter[0], geographicCenter[1], geographicCenter[2], radius];
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/** Generate source-frame samples that conservatively cover an I3S MBS or OBB. */
|
|
787
|
+
function getSourceBoundSamples(
|
|
788
|
+
bounds: I3SSpatialBounds,
|
|
789
|
+
sourceFrame: TilesetCoordinateFrame,
|
|
790
|
+
sourceToGeographicTransformer: SpatialCoordinateTransformer,
|
|
791
|
+
sourceVerticalUnitScale: number
|
|
792
|
+
): number[][] {
|
|
793
|
+
const obb = bounds.obb;
|
|
794
|
+
if (obb) {
|
|
795
|
+
const center = Array.from(obb.center).slice(0, 3);
|
|
796
|
+
center[2] *= sourceVerticalUnitScale;
|
|
797
|
+
const halfSize = Array.from(obb.halfSize).slice(0, 3);
|
|
798
|
+
const quaternion = new Quaternion().fromArray(Array.from(obb.quaternion));
|
|
799
|
+
const axes = [
|
|
800
|
+
new Vector3([halfSize[0], 0, 0]).transformByQuaternion(quaternion),
|
|
801
|
+
new Vector3([0, halfSize[1], 0]).transformByQuaternion(quaternion),
|
|
802
|
+
new Vector3([0, 0, halfSize[2]]).transformByQuaternion(quaternion)
|
|
803
|
+
];
|
|
804
|
+
if (sourceFrame === 'geographic') {
|
|
805
|
+
const geographicCenter = sourceToGeographicTransformer.transformPosition(center);
|
|
806
|
+
const cartesianCenter = Ellipsoid.WGS84.cartographicToCartesian(
|
|
807
|
+
new Vector3(geographicCenter)
|
|
808
|
+
);
|
|
809
|
+
const cartesianAxes =
|
|
810
|
+
bounds.normalReferenceFrame === 'vertex-reference-frame'
|
|
811
|
+
? axes.map(axis => localEnuVectorToEcef(axis, geographicCenter))
|
|
812
|
+
: axes;
|
|
813
|
+
return getBoxSigns().map(signs => {
|
|
814
|
+
const corner = new Vector3(cartesianCenter);
|
|
815
|
+
for (let axisIndex = 0; axisIndex < 3; axisIndex++) {
|
|
816
|
+
corner.add(new Vector3(cartesianAxes[axisIndex]).scale(signs[axisIndex]));
|
|
817
|
+
}
|
|
818
|
+
return Array.from(Ellipsoid.WGS84.cartesianToCartographic(corner));
|
|
819
|
+
});
|
|
820
|
+
}
|
|
821
|
+
return getBoxSigns().map(signs => {
|
|
822
|
+
const corner = new Vector3(center);
|
|
823
|
+
for (let axisIndex = 0; axisIndex < 3; axisIndex++) {
|
|
824
|
+
corner.add(new Vector3(axes[axisIndex]).scale(signs[axisIndex]));
|
|
825
|
+
}
|
|
826
|
+
return Array.from(corner);
|
|
827
|
+
});
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
const mbs = bounds.mbs ? Array.from(bounds.mbs) : [];
|
|
831
|
+
if (mbs.length < 4) {
|
|
832
|
+
return [];
|
|
833
|
+
}
|
|
834
|
+
const center = mbs.slice(0, 3);
|
|
835
|
+
center[2] *= sourceVerticalUnitScale;
|
|
836
|
+
const radius = mbs[3];
|
|
837
|
+
if (sourceFrame === 'geographic') {
|
|
838
|
+
const geographicCenter = sourceToGeographicTransformer.transformPosition(center);
|
|
839
|
+
const cartesianCenter = Ellipsoid.WGS84.cartographicToCartesian(new Vector3(geographicCenter));
|
|
840
|
+
return getAxisDirections().map(direction =>
|
|
841
|
+
Array.from(
|
|
842
|
+
Ellipsoid.WGS84.cartesianToCartographic(
|
|
843
|
+
new Vector3(cartesianCenter).add(new Vector3(direction).scale(radius))
|
|
844
|
+
)
|
|
845
|
+
)
|
|
846
|
+
);
|
|
847
|
+
}
|
|
848
|
+
return getAxisDirections().map(direction =>
|
|
849
|
+
Array.from(new Vector3(center).add(new Vector3(direction).scale(radius)))
|
|
850
|
+
);
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
/** Return the eight sign combinations of a box. */
|
|
854
|
+
function getBoxSigns(): number[][] {
|
|
855
|
+
return [-1, 1].flatMap(x => [-1, 1].flatMap(y => [-1, 1].map(z => [x, y, z])));
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
/** Return positive and negative unit directions for three Cartesian axes. */
|
|
859
|
+
function getAxisDirections(): number[][] {
|
|
860
|
+
return [
|
|
861
|
+
[1, 0, 0],
|
|
862
|
+
[-1, 0, 0],
|
|
863
|
+
[0, 1, 0],
|
|
864
|
+
[0, -1, 0],
|
|
865
|
+
[0, 0, 1],
|
|
866
|
+
[0, 0, -1]
|
|
867
|
+
];
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
/** Return the positive ECEF basis used to calculate a local projection Jacobian. */
|
|
871
|
+
function getPositiveAxisDirections(): Vector3[] {
|
|
872
|
+
return [new Vector3([1, 0, 0]), new Vector3([0, 1, 0]), new Vector3([0, 0, 1])];
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
/** Convert a local east/north/up vector into an earth-centered vector. */
|
|
876
|
+
function localEnuVectorToEcef(vector: ArrayLike<number>, geographic: ArrayLike<number>): Vector3 {
|
|
877
|
+
const longitude = (geographic[0] * Math.PI) / 180;
|
|
878
|
+
const latitude = (geographic[1] * Math.PI) / 180;
|
|
879
|
+
const east = new Vector3([-Math.sin(longitude), Math.cos(longitude), 0]);
|
|
880
|
+
const north = new Vector3([
|
|
881
|
+
-Math.sin(latitude) * Math.cos(longitude),
|
|
882
|
+
-Math.sin(latitude) * Math.sin(longitude),
|
|
883
|
+
Math.cos(latitude)
|
|
884
|
+
]);
|
|
885
|
+
const up = new Vector3([
|
|
886
|
+
Math.cos(latitude) * Math.cos(longitude),
|
|
887
|
+
Math.cos(latitude) * Math.sin(longitude),
|
|
888
|
+
Math.sin(latitude)
|
|
889
|
+
]);
|
|
890
|
+
return east.scale(vector[0]).add(north.scale(vector[1])).add(up.scale(vector[2]));
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
/** Convert an earth-centered vector into local east/north/up components. */
|
|
894
|
+
function ecefVectorToLocalEnu(vector: Vector3, geographic: ArrayLike<number>): Vector3 {
|
|
895
|
+
const longitude = (geographic[0] * Math.PI) / 180;
|
|
896
|
+
const latitude = (geographic[1] * Math.PI) / 180;
|
|
897
|
+
const east = new Vector3([-Math.sin(longitude), Math.cos(longitude), 0]);
|
|
898
|
+
const north = new Vector3([
|
|
899
|
+
-Math.sin(latitude) * Math.cos(longitude),
|
|
900
|
+
-Math.sin(latitude) * Math.sin(longitude),
|
|
901
|
+
Math.cos(latitude)
|
|
902
|
+
]);
|
|
903
|
+
const up = new Vector3([
|
|
904
|
+
Math.cos(latitude) * Math.cos(longitude),
|
|
905
|
+
Math.cos(latitude) * Math.sin(longitude),
|
|
906
|
+
Math.sin(latitude)
|
|
907
|
+
]);
|
|
908
|
+
return new Vector3([east.dot(vector), north.dot(vector), up.dot(vector)]);
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
/** Build a target-frame axis-aligned generic tile box. */
|
|
912
|
+
function getAxisAlignedBox(positions: number[][]): number[] {
|
|
913
|
+
const minimum = [Infinity, Infinity, Infinity];
|
|
914
|
+
const maximum = [-Infinity, -Infinity, -Infinity];
|
|
915
|
+
for (const position of positions) {
|
|
916
|
+
for (let component = 0; component < 3; component++) {
|
|
917
|
+
minimum[component] = Math.min(minimum[component], position[component]);
|
|
918
|
+
maximum[component] = Math.max(maximum[component], position[component]);
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
const center = minimum.map((value, index) => (value + maximum[index]) / 2);
|
|
922
|
+
const halfSize = minimum.map((value, index) => (maximum[index] - value) / 2);
|
|
923
|
+
return [center[0], center[1], center[2], halfSize[0], 0, 0, 0, halfSize[1], 0, 0, 0, halfSize[2]];
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
/** Build a dateline-aware geographic tile region in radians. */
|
|
927
|
+
function getGeographicRegion(positions: number[][]): number[] {
|
|
928
|
+
const referenceLongitude = positions[0][0];
|
|
929
|
+
let minimumLongitude = Infinity;
|
|
930
|
+
let maximumLongitude = -Infinity;
|
|
931
|
+
let minimumLatitude = Infinity;
|
|
932
|
+
let maximumLatitude = -Infinity;
|
|
933
|
+
let minimumHeight = Infinity;
|
|
934
|
+
let maximumHeight = -Infinity;
|
|
935
|
+
for (const position of positions) {
|
|
936
|
+
const longitude = referenceLongitude + normalizeLongitude(position[0] - referenceLongitude);
|
|
937
|
+
minimumLongitude = Math.min(minimumLongitude, longitude);
|
|
938
|
+
maximumLongitude = Math.max(maximumLongitude, longitude);
|
|
939
|
+
minimumLatitude = Math.min(minimumLatitude, position[1]);
|
|
940
|
+
maximumLatitude = Math.max(maximumLatitude, position[1]);
|
|
941
|
+
minimumHeight = Math.min(minimumHeight, position[2]);
|
|
942
|
+
maximumHeight = Math.max(maximumHeight, position[2]);
|
|
943
|
+
}
|
|
944
|
+
return [
|
|
945
|
+
(normalizeLongitude(minimumLongitude) * Math.PI) / 180,
|
|
946
|
+
(minimumLatitude * Math.PI) / 180,
|
|
947
|
+
(normalizeLongitude(maximumLongitude) * Math.PI) / 180,
|
|
948
|
+
(maximumLatitude * Math.PI) / 180,
|
|
949
|
+
minimumHeight,
|
|
950
|
+
maximumHeight
|
|
951
|
+
];
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
/** Normalize a longitude delta to the interval [-180, 180). */
|
|
955
|
+
function normalizeLongitude(longitude: number): number {
|
|
956
|
+
return ((((longitude + 180) % 360) + 360) % 360) - 180;
|
|
957
|
+
}
|