@spacexr/3d-tiles-runtime 0.0.0-stage → 0.1.0

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 (78) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +93 -2
  3. package/dist/chunk-AI6NWGCA.js +316 -0
  4. package/dist/chunk-AI6NWGCA.js.map +1 -0
  5. package/dist/chunk-G7YVZYMR.js +24 -0
  6. package/dist/chunk-G7YVZYMR.js.map +1 -0
  7. package/dist/chunk-PJIVVIOF.js +205 -0
  8. package/dist/chunk-PJIVVIOF.js.map +1 -0
  9. package/dist/chunk-Q2AL6LWU.js +3 -0
  10. package/dist/chunk-Q2AL6LWU.js.map +1 -0
  11. package/dist/chunk-Q35HKP5V.js +249 -0
  12. package/dist/chunk-Q35HKP5V.js.map +1 -0
  13. package/dist/chunk-URZDM4BH.js +488 -0
  14. package/dist/chunk-URZDM4BH.js.map +1 -0
  15. package/dist/implicit/index.d.ts +64 -0
  16. package/dist/implicit/index.js +4 -0
  17. package/dist/implicit/index.js.map +1 -0
  18. package/dist/index-CNnPRQZc.d.ts +350 -0
  19. package/dist/index.d.ts +713 -0
  20. package/dist/index.js +3074 -0
  21. package/dist/index.js.map +1 -0
  22. package/dist/metadata/index.d.ts +12 -0
  23. package/dist/metadata/index.js +3 -0
  24. package/dist/metadata/index.js.map +1 -0
  25. package/dist/pipeline/index.d.ts +5 -0
  26. package/dist/pipeline/index.js +3 -0
  27. package/dist/pipeline/index.js.map +1 -0
  28. package/dist/ports/index.d.ts +62 -0
  29. package/dist/ports/index.js +4 -0
  30. package/dist/ports/index.js.map +1 -0
  31. package/dist/resource-DH72MRMN.d.ts +34 -0
  32. package/dist/spatial/index.d.ts +402 -0
  33. package/dist/spatial/index.js +3 -0
  34. package/dist/spatial/index.js.map +1 -0
  35. package/dist/spatial-3IDPbyv4.d.ts +28 -0
  36. package/dist/types-Dn5i8Uqg.d.ts +78 -0
  37. package/dist/types-ri64XfSE.d.ts +237 -0
  38. package/package.json +102 -5
  39. package/src/implicit/availability.ts +55 -0
  40. package/src/implicit/coordinates.ts +193 -0
  41. package/src/implicit/dense-subtree-loader.ts +23 -0
  42. package/src/implicit/index.ts +6 -0
  43. package/src/implicit/standard-subtree-loader.ts +71 -0
  44. package/src/implicit/tile-data-source.ts +120 -0
  45. package/src/implicit/types.ts +156 -0
  46. package/src/index.ts +6 -0
  47. package/src/metadata/index.ts +2 -0
  48. package/src/metadata/metadata-resolver.ts +308 -0
  49. package/src/metadata/types.ts +91 -0
  50. package/src/pipeline/index.ts +4 -0
  51. package/src/pipeline/metadata-store.ts +44 -0
  52. package/src/pipeline/ports.ts +62 -0
  53. package/src/pipeline/states.ts +56 -0
  54. package/src/pipeline/tile-3d.ts +165 -0
  55. package/src/ports/camera.ts +12 -0
  56. package/src/ports/content.ts +71 -0
  57. package/src/ports/glyph.ts +13 -0
  58. package/src/ports/index.ts +32 -0
  59. package/src/ports/resource.ts +54 -0
  60. package/src/ports/spatial.ts +28 -0
  61. package/src/ports/telemetry.ts +44 -0
  62. package/src/runtime/cache.ts +112 -0
  63. package/src/runtime/contracts.ts +77 -0
  64. package/src/runtime/events.ts +57 -0
  65. package/src/runtime/hooks.ts +99 -0
  66. package/src/runtime/index.ts +7 -0
  67. package/src/runtime/instrumentation.ts +163 -0
  68. package/src/runtime/priority-scheduler.ts +88 -0
  69. package/src/runtime/quadtree-balance.ts +215 -0
  70. package/src/runtime/tile-runtime.ts +3508 -0
  71. package/src/spatial/bounding-volumes.ts +159 -0
  72. package/src/spatial/ecef-types.ts +110 -0
  73. package/src/spatial/ecef.ts +196 -0
  74. package/src/spatial/index.ts +6 -0
  75. package/src/spatial/utm-bounds.ts +151 -0
  76. package/src/spatial/utm-types.ts +83 -0
  77. package/src/spatial/vertical-scale.ts +125 -0
  78. package/src/spatial/view-metrics.ts +132 -0
@@ -0,0 +1,83 @@
1
+ import type { IUtmProjection } from "@spacexr/3d-tiles-core";
2
+ import type { Ellipsoid, IGeodeticCoordinates } from "@spacexr/geodesy";
3
+
4
+ /**
5
+ * Horizontal datum transformation from the datum of a UTM extent to the geodetic system of the spatial metric.
6
+ */
7
+ export interface IHorizontalDatumTransformation {
8
+ /** Identification of the transformation, for example `EPSG:1188 NAD83 to WGS 84 (1)`. */
9
+ readonly name: string;
10
+ /**
11
+ * Horizontal accuracy of the transformation in metres.
12
+ * Derived bounds are widened by this distance so that the true position stays inside them.
13
+ */
14
+ readonly accuracy: number;
15
+ /** Source CRS ellipsoid; defaults to the supplied target system ellipsoid. */
16
+ readonly sourceEllipsoid?: Ellipsoid;
17
+ /**
18
+ * Transforms geodetic coordinates in radians, in place, from the source datum to the target system.
19
+ * Omitted for a null transformation, whose whole error is covered by `accuracy`.
20
+ */
21
+ transform?(geodetic: IGeodeticCoordinates): void;
22
+ }
23
+
24
+ /**
25
+ * Selects the transformation of a resolved UTM projection.
26
+ * Returns `undefined` when no transformation is known, in which case the extent is not placed.
27
+ */
28
+ export type HorizontalDatumResolver = (projection: IUtmProjection) => IHorizontalDatumTransformation | undefined;
29
+
30
+ /**
31
+ * Geoid model returning the geoid undulation `N` in metres at a geodetic position in radians,
32
+ * so that ellipsoidal height equals orthometric height plus `N`.
33
+ */
34
+ export type GeoidUndulationModel = (latitudeRadians: number, longitudeRadians: number) => number;
35
+
36
+ /** Options controlling how UTM extents are placed in ECEF. */
37
+ export interface IUtmPlacementOptions {
38
+ /** Datum transformation resolver. Without a resolver, the datum remains unresolved. */
39
+ datumResolver?: HorizontalDatumResolver;
40
+ /** Explicit vertical uncertainty in metres when no geoid model is supplied. */
41
+ geoidUndulationMargin?: number;
42
+ /** Explicit height envelope when the vertical origin is unknown. */
43
+ unknownGroundHeightRange?: readonly [number, number];
44
+ /** Geoid model for orthometric heights. Without it, an explicit geoidUndulationMargin is required. */
45
+ geoidUndulation?: GeoidUndulationModel;
46
+ }
47
+
48
+ /**
49
+ * Outcome of placing a UTM extent.
50
+ *
51
+ * - `placed`: bounds were derived.
52
+ * - `unresolved-projection`: the zone and hemisphere could not be determined; no bounds were derived.
53
+ * - `unresolved-datum`: no datum transformation is known for the source datum; no bounds were derived.
54
+ */
55
+ export type UtmPlacementStatus = "placed" | "unresolved-projection" | "unresolved-datum";
56
+
57
+ /**
58
+ * Origin of the ellipsoidal heights used for the bounds.
59
+ *
60
+ * - `ellipsoid`: values were already ellipsoidal heights.
61
+ * - `geoid`: values were orthometric heights.
62
+ * - `ground-elevation`: values were measured from `groundElevation`, treated as an orthometric height.
63
+ * - `assumed-terrain`: the vertical origin is unknown, so the caller supplied a height envelope.
64
+ */
65
+ export type UtmVerticalOrigin = "ellipsoid" | "geoid" | "ground-elevation" | "assumed-terrain";
66
+
67
+ /** Explicit report of the conversions and approximations applied to a UTM extent. */
68
+ export interface IUtmPlacement {
69
+ status: UtmPlacementStatus;
70
+ /** Resolved zone, hemisphere and datum, when available. */
71
+ projection?: IUtmProjection;
72
+ /** Applied datum transformation, when available. */
73
+ datumTransformation?: Pick<IHorizontalDatumTransformation, "name" | "accuracy">;
74
+ /** Horizontal widening of the bounds in metres. */
75
+ horizontalMargin: number;
76
+ verticalOrigin?: UtmVerticalOrigin;
77
+ /** How orthometric heights were converted, when the vertical origin requires it. */
78
+ geoid?: "model" | "margin";
79
+ /** Vertical widening in metres applied for the geoid margin. `assumed-terrain` extents use `heightRange` instead. */
80
+ verticalMargin: number;
81
+ /** Height interval in metres used for the bounds. With a geoid model, the undulation of each sample is added to it. */
82
+ heightRange?: readonly [number, number];
83
+ }
@@ -0,0 +1,125 @@
1
+ import type { GeodeticSystem } from "@spacexr/geodesy";
2
+ import { cartesian3, matrix4 } from "@spacexr/geometry";
3
+ import type { ReferenceSurface } from "@spacexr/tiles";
4
+
5
+ import type { IEcefSpatialState, IVector3 } from "./ecef-types";
6
+
7
+ /**
8
+ * The vertical exaggeration of a scene. Terrain, picking, tile bounds, the camera and objects read this one value, so
9
+ * that they cannot disagree: changing it changes all of them.
10
+ */
11
+ export class VerticalScale {
12
+ private current: number;
13
+ private readonly listeners = new Set<(value: number) => void>();
14
+
15
+ /** @throws RangeError When the value is negative or not finite. */
16
+ public constructor(value = 1) {
17
+ this.current = VerticalScale.checked(value);
18
+ }
19
+
20
+ public get value(): number {
21
+ return this.current;
22
+ }
23
+
24
+ /** Changes the scale and notifies the listeners when it differs. @throws RangeError When the value is negative or not finite. */
25
+ public set(value: number): void {
26
+ const next = VerticalScale.checked(value);
27
+ if (next === this.current) return;
28
+ this.current = next;
29
+ for (const listener of [...this.listeners]) listener(next);
30
+ }
31
+
32
+ /** @returns The function that stops the notifications. */
33
+ public subscribe(listener: (value: number) => void): () => void {
34
+ this.listeners.add(listener);
35
+ return () => this.listeners.delete(listener);
36
+ }
37
+
38
+ private static checked(value: number): number {
39
+ if (!Number.isFinite(value) || value < 0) throw new RangeError("The vertical scale must be non-negative and finite.");
40
+ return value;
41
+ }
42
+ }
43
+
44
+ /** What a height means on a body: the same as for the terrain under the objects. */
45
+ export interface IVerticalFrame {
46
+ /** Height of a point in metres, over the surface the heights of the terrain refer to. */
47
+ height(point: Readonly<IVector3>): number;
48
+ /** Unit direction along which that height is measured at a point. */
49
+ up(point: Readonly<IVector3>): IVector3;
50
+ }
51
+
52
+ /** Radial heights over a reference surface, as within the pyramid root: `P = (r_ref(d) + h) d`. */
53
+ export function radialVerticalFrame(reference: Pick<ReferenceSurface, "height">): IVerticalFrame {
54
+ return {
55
+ height: (point) => reference.height(point),
56
+ up: (point) => cartesian3.normalize(point),
57
+ };
58
+ }
59
+
60
+ /** Heights along the normal of an ellipsoid, as on the Web Mercator grid. */
61
+ export function geodeticVerticalFrame(system: GeodeticSystem): IVerticalFrame {
62
+ return {
63
+ height: (point) => system.ecefToGeodeticRadians(point).height,
64
+ up: (point) => {
65
+ const { latitude, longitude } = system.ecefToGeodeticRadians(point);
66
+ const cosine = Math.cos(latitude);
67
+ return { x: cosine * Math.cos(longitude), y: cosine * Math.sin(longitude), z: Math.sin(latitude) };
68
+ },
69
+ };
70
+ }
71
+
72
+ /** How objects follow the vertical scale of their scene. */
73
+ export interface IVerticalPlacement {
74
+ scale: VerticalScale;
75
+ frame: IVerticalFrame;
76
+ }
77
+
78
+ /**
79
+ * How far the vertical scale moves an anchor: `h (s - 1)` along its up direction. A point of the terrain at height `h` is
80
+ * moved exactly as far, so an object placed on the terrain stays on it. The object itself is moved, never stretched.
81
+ */
82
+ export function verticalDisplacement(anchor: Readonly<IVector3> | undefined, placement: IVerticalPlacement): IVector3 {
83
+ const factor = placement.scale.value - 1;
84
+ // The centre of the body has no height: a volume around it does not move, it only grows.
85
+ if (factor === 0 || !anchor || (anchor.x === 0 && anchor.y === 0 && anchor.z === 0)) return { x: 0, y: 0, z: 0 };
86
+ const distance = placement.frame.height(anchor) * factor;
87
+ return cartesian3.scale(placement.frame.up(anchor), distance);
88
+ }
89
+
90
+ /**
91
+ * The bounds of a tile under the vertical scale.
92
+ *
93
+ * The first tile with a position, from the root down, is an object: it moves by the height of its origin, and its
94
+ * descendants move with it whatever their transforms, so that an object never comes apart. A tile above any object moves
95
+ * with the centre of its volume. It may hold objects of other heights, whose origins differ from that height by at most its
96
+ * radius: it grows by `r |s - 1|` to keep them inside. The transform stays as it was: children compose from the original
97
+ * placement.
98
+ */
99
+ export function placeVertically<TState extends IEcefSpatialState>(
100
+ state: TState,
101
+ placement: IVerticalPlacement,
102
+ parent?: Readonly<IEcefSpatialState>,
103
+ ): TState {
104
+ const origin = matrix4.translation(state.worldTransform);
105
+ const object = parent?.verticalObject ?? (cartesian3.length(origin) > 0 ? origin : undefined);
106
+ const sphere = state.boundingSphere;
107
+ const anchor = object ?? (sphere && cartesian3.copy(sphere.center));
108
+ const placed: TState = { ...state, ...(object ? { verticalObject: object } : {}), ...(anchor ? { verticalAnchor: anchor } : {}) };
109
+ const factor = placement.scale.value - 1;
110
+ if (factor === 0 || !sphere) return placed;
111
+ const moved = verticalDisplacement(anchor, placement);
112
+ const margin = object ? 0 : sphere.radius * Math.abs(factor);
113
+ placed.boundingSphere = {
114
+ center: cartesian3.add(sphere.center, moved),
115
+ radius: sphere.radius + margin,
116
+ };
117
+ if (state.boundingBox) {
118
+ const { min, max } = state.boundingBox;
119
+ placed.boundingBox = {
120
+ min: { x: min.x + moved.x - margin, y: min.y + moved.y - margin, z: min.z + moved.z - margin },
121
+ max: { x: max.x + moved.x + margin, y: max.y + moved.y + margin, z: max.z + moved.z + margin },
122
+ };
123
+ }
124
+ return placed;
125
+ }
@@ -0,0 +1,132 @@
1
+ import { cartesian3 } from "@spacexr/geometry";
2
+
3
+ import type { IEcefCameraFrame, IVector3 } from "./ecef-types";
4
+ import type { IVerticalFrame } from "./vertical-scale";
5
+
6
+ /** Viewport height and field of view used when a camera frame does not give them. */
7
+ export interface IProjectionFallback {
8
+ /** Viewport height in physical pixels. */
9
+ viewportHeight: number;
10
+ /** Vertical field of view in radians. */
11
+ verticalFovRadians: number;
12
+ }
13
+
14
+ /**
15
+ * Length on the ground covered by one pixel of the viewport, at a distance from the camera, along a direction facing it.
16
+ * It is the factor of the screen-space error: a geometric error `e` at that distance spans `e / metresPerPixel` pixels.
17
+ *
18
+ * @param camera - Camera frame giving the viewport height and the vertical field of view.
19
+ * @param distance - Distance from the camera, in metres.
20
+ * @param fallback - Values used when the frame gives no viewport height or field of view.
21
+ * @returns Metres per pixel, or `NaN` when the field of view is not in (0, PI).
22
+ */
23
+ export function metresPerPixel(camera: Readonly<IEcefCameraFrame>, distance: number, fallback: Readonly<IProjectionFallback>): number {
24
+ const viewportHeight = camera.viewportHeight > 0 ? camera.viewportHeight : fallback.viewportHeight;
25
+ const tanHalfVerticalFov = camera.tanHalfVerticalFov ?? Math.tan((camera.verticalFovRadians ?? fallback.verticalFovRadians) / 2);
26
+ if (!Number.isFinite(tanHalfVerticalFov) || tanHalfVerticalFov <= 0) return Number.NaN;
27
+ return (2 * tanHalfVerticalFov * distance) / viewportHeight;
28
+ }
29
+
30
+ /** Inputs of {@link describeView} known to the renderer or the application rather than the camera frame. */
31
+ export interface IViewMetricsOptions {
32
+ /** What a height means on the body: the frame of the terrain heights. */
33
+ vertical: IVerticalFrame;
34
+ /** Values used when the frame gives no viewport height or field of view. */
35
+ fallback: Readonly<IProjectionFallback>;
36
+ /**
37
+ * Height of the drawn terrain below the camera, over the same surface, vertical scale included. Without it the altitude
38
+ * over the ground is unknown and the view centre is sought on the reference surface.
39
+ */
40
+ groundHeight?: number;
41
+ /**
42
+ * Distance from the camera to the point at the centre of the view, when the renderer knows it, such as the radius of an
43
+ * orbiting camera or a depth read. Otherwise the centre ray is intersected with the sphere through the ground below the
44
+ * camera.
45
+ */
46
+ targetDistance?: number;
47
+ }
48
+
49
+ /** What a camera sees, for a scale and altitude display. Angles in degrees, lengths in metres. */
50
+ export interface IViewMetrics {
51
+ /** Latitude of the camera, along the up direction of the vertical frame: geodetic or planetocentric. */
52
+ latitude: number;
53
+ /** East longitude of the camera, in (-180, 180]. */
54
+ longitude: number;
55
+ /** Height of the camera over the reference surface. */
56
+ height: number;
57
+ /** Height of the camera over the drawn terrain, when its height below the camera is known. */
58
+ altitude?: number;
59
+ /** Direction of the view from north, clockwise, in [0, 360). Undefined when the camera looks straight up or down. */
60
+ heading?: number;
61
+ /** Angle of the view over the horizon: 0 level, -90 straight down. Undefined without a forward direction. */
62
+ pitch?: number;
63
+ /** Distance from the camera to the point at the centre of the view. Undefined when the view misses the body. */
64
+ targetDistance?: number;
65
+ /** Ground length covered by one pixel at the centre of the view; the screen-space error factor there. */
66
+ metresPerPixel?: number;
67
+ }
68
+
69
+ const DEGREES = 180 / Math.PI;
70
+
71
+ const { cross, dot } = cartesian3;
72
+
73
+ /** Unit vector of the same direction, undefined for a vector without length. */
74
+ function normalized(vector: Readonly<IVector3>): IVector3 | undefined {
75
+ return cartesian3.length(vector) > 0 ? cartesian3.normalize(vector) : undefined;
76
+ }
77
+
78
+ /** Distance along a unit ray from `origin` to the near side of a sphere centred on the body, if the ray meets it. */
79
+ function raySphereDistance(origin: Readonly<IVector3>, direction: Readonly<IVector3>, radius: number): number | undefined {
80
+ const b = dot(origin, direction);
81
+ const c = dot(origin, origin) - radius * radius;
82
+ const discriminant = b * b - c;
83
+ if (discriminant < 0) return undefined;
84
+ const root = Math.sqrt(discriminant);
85
+ const near = -b - root;
86
+ if (near >= 0) return near;
87
+ const far = -b + root;
88
+ return far >= 0 ? far : undefined;
89
+ }
90
+
91
+ /**
92
+ * Position, orientation and scale of a camera over a body, from its renderer-neutral frame.
93
+ *
94
+ * North is the body axis, ECEF +Z, projected on the plane normal to the up direction of the vertical frame. The scale is
95
+ * {@link metresPerPixel} at the distance of the view centre: the same factor the screen-space error divides by.
96
+ */
97
+ export function describeView(camera: Readonly<IEcefCameraFrame>, options: Readonly<IViewMetricsOptions>): IViewMetrics {
98
+ const position = camera.position;
99
+ const up = options.vertical.up(position);
100
+ const height = options.vertical.height(position);
101
+ const metrics: IViewMetrics = {
102
+ latitude: Math.asin(Math.max(-1, Math.min(1, up.z))) * DEGREES,
103
+ longitude: Math.atan2(up.y, up.x) * DEGREES,
104
+ height,
105
+ };
106
+ const ground = options.groundHeight;
107
+ if (ground !== undefined && Number.isFinite(ground)) metrics.altitude = height - ground;
108
+
109
+ const forward = camera.forward && normalized(camera.forward);
110
+ if (!forward) return metrics;
111
+ const vertical = dot(forward, up);
112
+ metrics.pitch = Math.asin(Math.max(-1, Math.min(1, vertical))) * DEGREES;
113
+ // At a pole, north is undefined: the heading is measured from the meridian of longitude 0.
114
+ const north = normalized({ x: -up.z * up.x, y: -up.z * up.y, z: 1 - up.z * up.z }) ?? normalized({ x: -Math.sign(up.z), y: 0, z: 0 });
115
+ const level = normalized({ x: forward.x - vertical * up.x, y: forward.y - vertical * up.y, z: forward.z - vertical * up.z });
116
+ if (north && level) {
117
+ const east = cross(north, up);
118
+ const heading = Math.atan2(dot(level, east), dot(level, north)) * DEGREES;
119
+ metrics.heading = heading < 0 ? heading + 360 : heading;
120
+ }
121
+
122
+ // The point of the ground below the camera, or of the reference surface without the ground height.
123
+ const drop = metrics.altitude ?? height;
124
+ const below = { x: position.x - drop * up.x, y: position.y - drop * up.y, z: position.z - drop * up.z };
125
+ const distance = options.targetDistance ?? raySphereDistance(position, forward, cartesian3.length(below));
126
+ if (distance !== undefined && Number.isFinite(distance) && distance > 0) {
127
+ metrics.targetDistance = distance;
128
+ const perPixel = metresPerPixel(camera, distance, options.fallback);
129
+ if (perPixel > 0) metrics.metresPerPixel = perPixel;
130
+ }
131
+ return metrics;
132
+ }