@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,215 @@
1
+ import {
2
+ TILE_CORNER_NEIGHBORS,
3
+ TILE_EDGE_NEIGHBORS,
4
+ ancestorTileAddress,
5
+ tileAddressKey,
6
+ tileNeighborAddress,
7
+ type ITileTopology,
8
+ type TileEdgeNeighbor,
9
+ } from "@spacexr/tiles";
10
+
11
+ import type { ITileAddress } from "../implicit/tile-data-source";
12
+
13
+ /** Address of a quadtree tile within a namespace: one implicit tileset, or a pyramid shared by several. */
14
+ export interface IQuadtreeTileLocation {
15
+ /** Prefix shared by the tiles that can be neighbours. */
16
+ namespace: string;
17
+ address: ITileAddress;
18
+ /** Wraps columns around the antimeridian, for pyramids covering every longitude. */
19
+ wrapX: boolean;
20
+ /** How the tiles connect, for example across configured roots, in place of `wrapX`. */
21
+ topology?: ITileTopology;
22
+ }
23
+
24
+ /** Neighbour of a located tile across an edge. */
25
+ function edgeNeighbor(location: IQuadtreeTileLocation, edge: TileEdgeNeighbor): ITileAddress | undefined {
26
+ const { address, topology, wrapX } = location;
27
+ return topology ? topology.neighbor(address, edge)?.address : tileNeighborAddress(address, edge, { wrapX });
28
+ }
29
+
30
+ export function quadtreeTileKey(namespace: string, address: ITileAddress): string {
31
+ return `${namespace}${tileAddressKey(address)}`;
32
+ }
33
+
34
+ /**
35
+ * Whether a tile is the neighbour of one of some tiles: it touches one of them, by an edge or a corner, that is at its
36
+ * level or coarser. A tile coarser than the one it touches is not its neighbour.
37
+ *
38
+ * @param location - The tile.
39
+ * @param tiles - Keys built with {@link quadtreeTileKey}.
40
+ */
41
+ export function quadtreeTouches(location: IQuadtreeTileLocation, tiles: ReadonlySet<string>): boolean {
42
+ if (tiles.size === 0) return false;
43
+ const { address, namespace, topology, wrapX } = location;
44
+ for (const position of [...TILE_EDGE_NEIGHBORS, ...TILE_CORNER_NEIGHBORS]) {
45
+ const neighbor = topology ? topology.neighbor(address, position)?.address : tileNeighborAddress(address, position, { wrapX });
46
+ if (!neighbor) continue;
47
+ if (tiles.has(quadtreeTileKey(namespace, neighbor))) return true;
48
+ for (let levels = 1; levels <= neighbor.lod; levels++) {
49
+ if (tiles.has(quadtreeTileKey(namespace, ancestorTileAddress(neighbor, levels)))) return true;
50
+ }
51
+ }
52
+ return false;
53
+ }
54
+
55
+ /** A displayed tile next to a displayed tile more levels coarser than allowed. */
56
+ export interface IQuadtreeImbalance<T> {
57
+ fine: T;
58
+ /** Level of the coarser neighbour. */
59
+ coarseLod: number;
60
+ }
61
+
62
+ /**
63
+ * Pairs of displayed tiles sharing an edge whose levels differ by more than `maxLevelDifference`, reported from the
64
+ * finer tile.
65
+ *
66
+ * @param displayed - Displayed tiles with their location and a value returned in the result.
67
+ * @param maxLevelDifference - Largest difference allowed between edge neighbours. Defaults to 1, the 2:1 balance.
68
+ */
69
+ export function quadtreeImbalances<T>(
70
+ displayed: Iterable<{ location: IQuadtreeTileLocation; value: T }>,
71
+ maxLevelDifference = 1,
72
+ ): IQuadtreeImbalance<T>[] {
73
+ const entries = [...displayed];
74
+ const keys = new Set(entries.map(({ location }) => quadtreeTileKey(location.namespace, location.address)));
75
+ const imbalances: IQuadtreeImbalance<T>[] = [];
76
+ for (const { location, value } of entries) {
77
+ const { namespace } = location;
78
+ let coarseLod: number | undefined;
79
+ for (const edge of TILE_EDGE_NEIGHBORS) {
80
+ const neighbor = edgeNeighbor(location, edge);
81
+ if (!neighbor) continue;
82
+ for (let levels = maxLevelDifference + 1; levels <= neighbor.lod; levels++) {
83
+ const ancestor = ancestorTileAddress(neighbor, levels);
84
+ if (!keys.has(quadtreeTileKey(namespace, ancestor))) continue;
85
+ coarseLod = Math.min(coarseLod ?? ancestor.lod, ancestor.lod);
86
+ break;
87
+ }
88
+ }
89
+ if (coarseLod !== undefined) imbalances.push({ fine: value, coarseLod });
90
+ }
91
+ return imbalances;
92
+ }
93
+
94
+ /**
95
+ * Tiles that must refine so that every displayed tile has its edge neighbours within `maxLevelDifference` levels of
96
+ * detail: one by default, the 2:1 balance. For each displayed tile of level `L` and each edge neighbour at level `L`,
97
+ * every ancestor of that neighbour at level `L - maxLevelDifference - 1` or coarser must refine: such an ancestor would
98
+ * otherwise be displayed next to the tile, too many levels apart. Diagonal neighbours do not matter, because tile
99
+ * corners exist at every level.
100
+ *
101
+ * @returns Keys built with {@link quadtreeTileKey}.
102
+ */
103
+ export function quadtreeBalanceRefinements(displayed: Iterable<IQuadtreeTileLocation>, maxLevelDifference = 1): Set<string> {
104
+ const refinements = new Set<string>();
105
+ for (const location of displayed) {
106
+ const { namespace, address } = location;
107
+ if (address.lod <= maxLevelDifference) continue;
108
+ for (const edge of TILE_EDGE_NEIGHBORS) {
109
+ const neighbor = edgeNeighbor(location, edge);
110
+ if (!neighbor) continue;
111
+ for (let levels = maxLevelDifference + 1; levels <= neighbor.lod; levels++) {
112
+ const key = quadtreeTileKey(namespace, ancestorTileAddress(neighbor, levels));
113
+ // Coarser ancestors were added together with this one by an earlier tile.
114
+ if (refinements.has(key)) break;
115
+ refinements.add(key);
116
+ }
117
+ }
118
+ }
119
+ return refinements;
120
+ }
121
+
122
+ /** A leaf of a ready quadtree cut. */
123
+ export interface IQuadtreeCutTile<T> {
124
+ location: IQuadtreeTileLocation;
125
+ value: T;
126
+ }
127
+
128
+ /**
129
+ * Admits a ready cut exclusively by refining incompatible coarse neighbours. Existing fine leaves never coarsen.
130
+ * Each successful split strictly increases depth; only its front and the fine leaf return to the local queue.
131
+ * A cold front is requested once, and makes the whole transaction inadmissible without publishing a partial cut.
132
+ *
133
+ * @param blocked - Receives, for each edge a cold front leaves more than `maxLevelDifference` levels apart, the tiles
134
+ * of `displayed` that led to it: the coarse tile, or the one it was split from, and the fine tile with every tile split
135
+ * for it.
136
+ * @param maxLevelDifference - Largest difference allowed between edge neighbours. Defaults to 1, the 2:1 balance.
137
+ */
138
+ export function refineQuadtreeCut<T>(
139
+ displayed: Iterable<IQuadtreeCutTile<T>>,
140
+ refine: (coarse: IQuadtreeCutTile<T>) => readonly IQuadtreeCutTile<T>[] | undefined,
141
+ blocked?: (involved: readonly IQuadtreeCutTile<T>[]) => void,
142
+ maxLevelDifference = 1,
143
+ ): IQuadtreeCutTile<T>[] | undefined {
144
+ const leaves = new Map<string, IQuadtreeCutTile<T>>();
145
+ const queue: IQuadtreeCutTile<T>[] = [];
146
+ const unavailable = new Set<string>();
147
+ // The tiles of the input each leaf comes from: itself, or the tiles whose splits produced it.
148
+ const origins = new Map<IQuadtreeCutTile<T>, readonly IQuadtreeCutTile<T>[]>();
149
+ const keyOf = ({ location }: IQuadtreeCutTile<T>): string => quadtreeTileKey(location.namespace, location.address);
150
+ for (const tile of displayed) {
151
+ leaves.set(keyOf(tile), tile);
152
+ queue.push(tile);
153
+ origins.set(tile, [tile]);
154
+ }
155
+ const involved = (fine: IQuadtreeCutTile<T>, coarse: IQuadtreeCutTile<T>): IQuadtreeCutTile<T>[] => [
156
+ ...new Set([...origins.get(fine)!, ...origins.get(coarse)!]),
157
+ ];
158
+ const covering = (namespace: string, address: ITileAddress): IQuadtreeCutTile<T> | undefined => {
159
+ for (let levels = 0; levels <= address.lod; levels++) {
160
+ const tile = leaves.get(quadtreeTileKey(namespace, levels === 0 ? address : ancestorTileAddress(address, levels)));
161
+ if (tile) return tile;
162
+ }
163
+ return undefined;
164
+ };
165
+ while (queue.length > 0) {
166
+ const fine = queue.pop()!;
167
+ if (leaves.get(keyOf(fine)) !== fine) continue;
168
+ let coarse: IQuadtreeCutTile<T> | undefined;
169
+ for (const edge of TILE_EDGE_NEIGHBORS) {
170
+ const neighbor = edgeNeighbor(fine.location, edge);
171
+ if (!neighbor) continue;
172
+ const other = covering(fine.location.namespace, neighbor);
173
+ if (other && other.location.address.lod < fine.location.address.lod - maxLevelDifference) {
174
+ coarse = other;
175
+ break;
176
+ }
177
+ }
178
+ if (!coarse) continue;
179
+ if (unavailable.has(keyOf(coarse))) {
180
+ blocked?.(involved(fine, coarse));
181
+ continue;
182
+ }
183
+ const front = refine(coarse);
184
+ if (!front) {
185
+ unavailable.add(keyOf(coarse));
186
+ blocked?.(involved(fine, coarse));
187
+ continue;
188
+ }
189
+ const keys = new Set(front.map(keyOf));
190
+ if (keys.size !== front.length) throw new Error("A quadtree refinement front cannot contain duplicate leaves.");
191
+ for (const child of front) {
192
+ const { namespace, address } = child.location;
193
+ const difference = address.lod - coarse.location.address.lod;
194
+ if (
195
+ difference <= 0 ||
196
+ namespace !== coarse.location.namespace ||
197
+ quadtreeTileKey(namespace, ancestorTileAddress(address, difference)) !== keyOf(coarse)
198
+ )
199
+ throw new Error("A quadtree refinement must return strict descendants of the coarse leaf in its namespace.");
200
+ for (let levels = 1; levels < difference; levels++) {
201
+ if (keys.has(quadtreeTileKey(namespace, ancestorTileAddress(address, levels))))
202
+ throw new Error("A quadtree refinement front cannot overlap itself.");
203
+ }
204
+ }
205
+ leaves.delete(keyOf(coarse));
206
+ const origin = involved(fine, coarse);
207
+ for (const child of front) {
208
+ leaves.set(keyOf(child), child);
209
+ queue.push(child);
210
+ origins.set(child, origin);
211
+ }
212
+ queue.push(fine);
213
+ }
214
+ return unavailable.size === 0 ? [...leaves.values()] : undefined;
215
+ }