@loaders.gl/tiles 5.0.0-alpha.1 → 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 (161) hide show
  1. package/LICENSE +14 -3
  2. package/dist/dist.dev.js +17482 -3685
  3. package/dist/dist.min.js +4 -1
  4. package/dist/index.cjs +4594 -422
  5. package/dist/index.cjs.map +3 -4
  6. package/dist/index.d.ts +16 -2
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +7 -0
  9. package/dist/index.js.map +1 -1
  10. package/dist/point-cloud/point-cloud-tile.d.ts.map +1 -1
  11. package/dist/point-cloud/point-cloud-tile.js +3 -0
  12. package/dist/point-cloud/point-cloud-tile.js.map +1 -1
  13. package/dist/point-cloud/point-cloud-tileset.d.ts +4 -0
  14. package/dist/point-cloud/point-cloud-tileset.d.ts.map +1 -1
  15. package/dist/point-cloud/point-cloud-tileset.js +54 -12
  16. package/dist/point-cloud/point-cloud-tileset.js.map +1 -1
  17. package/dist/point-cloud/types.d.ts +17 -0
  18. package/dist/point-cloud/types.d.ts.map +1 -1
  19. package/dist/point-cloud/types.js +3 -0
  20. package/dist/point-cloud/types.js.map +1 -1
  21. package/dist/raster-set.d.ts +37 -29
  22. package/dist/raster-set.d.ts.map +1 -1
  23. package/dist/raster-set.js +25 -2
  24. package/dist/raster-set.js.map +1 -1
  25. package/dist/spatial/format-spatial-reference.d.ts +67 -0
  26. package/dist/spatial/format-spatial-reference.d.ts.map +1 -0
  27. package/dist/spatial/format-spatial-reference.js +233 -0
  28. package/dist/spatial/format-spatial-reference.js.map +1 -0
  29. package/dist/spatial/get-spatial-coordinate-frame.d.ts +9 -0
  30. package/dist/spatial/get-spatial-coordinate-frame.d.ts.map +1 -0
  31. package/dist/spatial/get-spatial-coordinate-frame.js +58 -0
  32. package/dist/spatial/get-spatial-coordinate-frame.js.map +1 -0
  33. package/dist/spatial/i3s-elevation.d.ts +20 -0
  34. package/dist/spatial/i3s-elevation.d.ts.map +1 -0
  35. package/dist/spatial/i3s-elevation.js +90 -0
  36. package/dist/spatial/i3s-elevation.js.map +1 -0
  37. package/dist/spatial/i3s-spatial-transformer.d.ts +189 -0
  38. package/dist/spatial/i3s-spatial-transformer.d.ts.map +1 -0
  39. package/dist/spatial/i3s-spatial-transformer.js +686 -0
  40. package/dist/spatial/i3s-spatial-transformer.js.map +1 -0
  41. package/dist/spatial/spatial-coordinate-transformer.d.ts +48 -0
  42. package/dist/spatial/spatial-coordinate-transformer.d.ts.map +1 -0
  43. package/dist/spatial/spatial-coordinate-transformer.js +211 -0
  44. package/dist/spatial/spatial-coordinate-transformer.js.map +1 -0
  45. package/dist/spatial/spatial-resource-registry.d.ts +44 -0
  46. package/dist/spatial/spatial-resource-registry.d.ts.map +1 -0
  47. package/dist/spatial/spatial-resource-registry.js +67 -0
  48. package/dist/spatial/spatial-resource-registry.js.map +1 -0
  49. package/dist/spatial/spatial-types.d.ts +190 -0
  50. package/dist/spatial/spatial-types.d.ts.map +1 -0
  51. package/dist/spatial/spatial-types.js +192 -0
  52. package/dist/spatial/spatial-types.js.map +1 -0
  53. package/dist/tileset-3d/common/tile-3d.d.ts +147 -4
  54. package/dist/tileset-3d/common/tile-3d.d.ts.map +1 -1
  55. package/dist/tileset-3d/common/tile-3d.js +358 -76
  56. package/dist/tileset-3d/common/tile-3d.js.map +1 -1
  57. package/dist/tileset-3d/common/tileset-3d.d.ts +168 -3
  58. package/dist/tileset-3d/common/tileset-3d.d.ts.map +1 -1
  59. package/dist/tileset-3d/common/tileset-3d.js +306 -23
  60. package/dist/tileset-3d/common/tileset-3d.js.map +1 -1
  61. package/dist/tileset-3d/common/tileset-cache.d.ts +11 -1
  62. package/dist/tileset-3d/common/tileset-cache.d.ts.map +1 -1
  63. package/dist/tileset-3d/common/tileset-cache.js +11 -3
  64. package/dist/tileset-3d/common/tileset-cache.js.map +1 -1
  65. package/dist/tileset-3d/common/tileset-source.d.ts +32 -0
  66. package/dist/tileset-3d/common/tileset-source.d.ts.map +1 -1
  67. package/dist/tileset-3d/common/tileset-source.js.map +1 -1
  68. package/dist/tileset-3d/common/tileset-traverser.d.ts +19 -1
  69. package/dist/tileset-3d/common/tileset-traverser.d.ts.map +1 -1
  70. package/dist/tileset-3d/common/tileset-traverser.js +39 -7
  71. package/dist/tileset-3d/common/tileset-traverser.js.map +1 -1
  72. package/dist/tileset-3d/format-3d-tiles/implicit-tiling.d.ts +136 -0
  73. package/dist/tileset-3d/format-3d-tiles/implicit-tiling.d.ts.map +1 -0
  74. package/dist/tileset-3d/format-3d-tiles/implicit-tiling.js +384 -0
  75. package/dist/tileset-3d/format-3d-tiles/implicit-tiling.js.map +1 -0
  76. package/dist/tileset-3d/format-3d-tiles/tiles-3d-source.d.ts +86 -3
  77. package/dist/tileset-3d/format-3d-tiles/tiles-3d-source.d.ts.map +1 -1
  78. package/dist/tileset-3d/format-3d-tiles/tiles-3d-source.js +196 -21
  79. package/dist/tileset-3d/format-3d-tiles/tiles-3d-source.js.map +1 -1
  80. package/dist/tileset-3d/format-3d-tiles/tileset-3d-traverser.d.ts +36 -0
  81. package/dist/tileset-3d/format-3d-tiles/tileset-3d-traverser.d.ts.map +1 -1
  82. package/dist/tileset-3d/format-3d-tiles/tileset-3d-traverser.js +66 -0
  83. package/dist/tileset-3d/format-3d-tiles/tileset-3d-traverser.js.map +1 -1
  84. package/dist/tileset-3d/format-i3s/i3s-pending-tiles-register.d.ts.map +1 -1
  85. package/dist/tileset-3d/format-i3s/i3s-pending-tiles-register.js +3 -0
  86. package/dist/tileset-3d/format-i3s/i3s-pending-tiles-register.js.map +1 -1
  87. package/dist/tileset-3d/format-i3s/i3s-source.d.ts +14 -1
  88. package/dist/tileset-3d/format-i3s/i3s-source.d.ts.map +1 -1
  89. package/dist/tileset-3d/format-i3s/i3s-source.js +107 -17
  90. package/dist/tileset-3d/format-i3s/i3s-source.js.map +1 -1
  91. package/dist/tileset-3d/format-i3s/i3s-tile-manager.d.ts.map +1 -1
  92. package/dist/tileset-3d/format-i3s/i3s-tile-manager.js +3 -0
  93. package/dist/tileset-3d/format-i3s/i3s-tile-manager.js.map +1 -1
  94. package/dist/tileset-3d/format-i3s/i3s-tileset-traverser.d.ts.map +1 -1
  95. package/dist/tileset-3d/format-i3s/i3s-tileset-traverser.js +3 -0
  96. package/dist/tileset-3d/format-i3s/i3s-tileset-traverser.js.map +1 -1
  97. package/dist/tileset-3d/helpers/3d-tiles-options.d.ts.map +1 -1
  98. package/dist/tileset-3d/helpers/3d-tiles-options.js +3 -0
  99. package/dist/tileset-3d/helpers/3d-tiles-options.js.map +1 -1
  100. package/dist/tileset-3d/helpers/bounding-volume.d.ts.map +1 -1
  101. package/dist/tileset-3d/helpers/bounding-volume.js +4 -33
  102. package/dist/tileset-3d/helpers/bounding-volume.js.map +1 -1
  103. package/dist/tileset-3d/helpers/frame-state.d.ts +62 -2
  104. package/dist/tileset-3d/helpers/frame-state.d.ts.map +1 -1
  105. package/dist/tileset-3d/helpers/frame-state.js +85 -9
  106. package/dist/tileset-3d/helpers/frame-state.js.map +1 -1
  107. package/dist/tileset-3d/helpers/i3s-lod.d.ts.map +1 -1
  108. package/dist/tileset-3d/helpers/i3s-lod.js +20 -8
  109. package/dist/tileset-3d/helpers/i3s-lod.js.map +1 -1
  110. package/dist/tileset-3d/helpers/tiles-3d-lod.d.ts +61 -9
  111. package/dist/tileset-3d/helpers/tiles-3d-lod.d.ts.map +1 -1
  112. package/dist/tileset-3d/helpers/tiles-3d-lod.js +198 -93
  113. package/dist/tileset-3d/helpers/tiles-3d-lod.js.map +1 -1
  114. package/dist/tileset-3d/helpers/tiles-3d-request-priority.d.ts +134 -0
  115. package/dist/tileset-3d/helpers/tiles-3d-request-priority.d.ts.map +1 -0
  116. package/dist/tileset-3d/helpers/tiles-3d-request-priority.js +179 -0
  117. package/dist/tileset-3d/helpers/tiles-3d-request-priority.js.map +1 -0
  118. package/dist/types.d.ts +5 -0
  119. package/dist/types.d.ts.map +1 -1
  120. package/dist/types.js +3 -0
  121. package/dist/types.js.map +1 -1
  122. package/dist/utils/doubly-linked-list.d.ts.map +1 -1
  123. package/dist/utils/doubly-linked-list.js +1 -0
  124. package/dist/utils/doubly-linked-list.js.map +1 -1
  125. package/dist/utils/managed-array.d.ts.map +1 -1
  126. package/dist/utils/managed-array.js +1 -0
  127. package/dist/utils/managed-array.js.map +1 -1
  128. package/package.json +12 -9
  129. package/src/index.ts +61 -1
  130. package/src/point-cloud/point-cloud-tile.ts +4 -0
  131. package/src/point-cloud/point-cloud-tileset.ts +70 -12
  132. package/src/point-cloud/types.ts +21 -0
  133. package/src/raster-set.ts +118 -45
  134. package/src/spatial/format-spatial-reference.ts +311 -0
  135. package/src/spatial/get-spatial-coordinate-frame.ts +74 -0
  136. package/src/spatial/i3s-elevation.ts +96 -0
  137. package/src/spatial/i3s-spatial-transformer.ts +957 -0
  138. package/src/spatial/spatial-coordinate-transformer.ts +276 -0
  139. package/src/spatial/spatial-resource-registry.ts +79 -0
  140. package/src/spatial/spatial-types.ts +422 -0
  141. package/src/tileset-3d/common/tile-3d.ts +435 -84
  142. package/src/tileset-3d/common/tileset-3d.ts +445 -23
  143. package/src/tileset-3d/common/tileset-cache.ts +15 -7
  144. package/src/tileset-3d/common/tileset-source.ts +36 -0
  145. package/src/tileset-3d/common/tileset-traverser.ts +40 -7
  146. package/src/tileset-3d/format-3d-tiles/implicit-tiling.ts +612 -0
  147. package/src/tileset-3d/format-3d-tiles/tiles-3d-source.ts +254 -21
  148. package/src/tileset-3d/format-3d-tiles/tileset-3d-traverser.ts +77 -0
  149. package/src/tileset-3d/format-i3s/i3s-pending-tiles-register.ts +4 -0
  150. package/src/tileset-3d/format-i3s/i3s-source.ts +119 -18
  151. package/src/tileset-3d/format-i3s/i3s-tile-manager.ts +4 -0
  152. package/src/tileset-3d/format-i3s/i3s-tileset-traverser.ts +4 -0
  153. package/src/tileset-3d/helpers/3d-tiles-options.ts +4 -0
  154. package/src/tileset-3d/helpers/bounding-volume.ts +5 -55
  155. package/src/tileset-3d/helpers/frame-state.ts +142 -11
  156. package/src/tileset-3d/helpers/i3s-lod.ts +22 -8
  157. package/src/tileset-3d/helpers/tiles-3d-lod.ts +291 -112
  158. package/src/tileset-3d/helpers/tiles-3d-request-priority.ts +301 -0
  159. package/src/types.ts +9 -0
  160. package/src/utils/doubly-linked-list.ts +2 -0
  161. package/src/utils/managed-array.ts +2 -0
@@ -0,0 +1,612 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright vis.gl contributors
4
+
5
+ import {LOD_METRIC_TYPE, TILE_REFINEMENT, TILE_TYPE} from '../../constants';
6
+
7
+ const QUADTREE_CHILD_COUNT = 4;
8
+ const OCTREE_CHILD_COUNT = 8;
9
+
10
+ /** Global coordinates of a tile in an implicit quadtree or octree. */
11
+ export type ImplicitTileCoordinates = {
12
+ /** Zero-based level relative to the implicit-tile root. */
13
+ level: number;
14
+ /** Zero-based horizontal coordinate at {@link level}. */
15
+ x: number;
16
+ /** Zero-based vertical coordinate at {@link level}. */
17
+ y: number;
18
+ /** Zero-based height coordinate at {@link level}; always zero for quadtrees. */
19
+ z: number;
20
+ };
21
+
22
+ /** Serializable information shared by every subtree in one implicit-tile hierarchy. */
23
+ export type ImplicitTilingDescriptor = {
24
+ /** Absolute template URL for render content, or an empty string for a contentless hierarchy. */
25
+ contentUrlTemplate: string;
26
+ /** Absolute templates for every implicit content stream, in source order. */
27
+ contentUrlTemplates?: string[];
28
+ /** Non-URI content metadata inherited by each available implicit content resource. */
29
+ contentHeader?: Record<string, any>;
30
+ /** Metadata inherited by every implicit content stream, in source order. */
31
+ contentHeaders?: Array<Record<string, any>>;
32
+ /** Absolute template URL for subtree availability files. */
33
+ subtreesUrlTemplate: string;
34
+ /** Spatial subdivision used by the hierarchy. */
35
+ subdivisionScheme: 'QUADTREE' | 'OCTREE';
36
+ /** Number of tile levels represented inside each subtree file. */
37
+ subtreeLevels: number;
38
+ /** Last valid global tile level, where the implicit root is level zero. */
39
+ maximumLevel: number;
40
+ /** Refinement mode inherited by generated tiles. */
41
+ refine: TILE_REFINEMENT | string;
42
+ /** LOD metric used by generated tiles. */
43
+ lodMetricType: LOD_METRIC_TYPE.GEOMETRIC_ERROR;
44
+ /** Source geometric error of the implicit root, in local meters. */
45
+ rootLodMetricValue: number;
46
+ /** Bounding volume of the complete implicit hierarchy. */
47
+ rootBoundingVolume: Record<string, any>;
48
+ };
49
+
50
+ /** Lazy pointer from a runtime tile to the subtree that defines it and its descendants. */
51
+ export type ImplicitSubtreeReference = {
52
+ /** Global coordinates of the referenced subtree root. */
53
+ coordinates: ImplicitTileCoordinates;
54
+ /** Absolute subtree URL before source-level query inheritance is applied. */
55
+ subtreeUrl: string;
56
+ /** Serializable hierarchy description used to materialize the subtree. */
57
+ descriptor: ImplicitTilingDescriptor;
58
+ };
59
+
60
+ /** Constant or bitstream-backed availability declaration from a parsed subtree. */
61
+ export type ImplicitAvailability = {
62
+ /** Constant availability value applied to every addressed element. */
63
+ constant?: number;
64
+ /** Expanded least-significant-bit-first availability buffer. */
65
+ explicitBitstream?: Uint8Array;
66
+ };
67
+
68
+ /** Parsed subset of the 3D Tiles subtree schema needed for hierarchy materialization. */
69
+ export type ParsedImplicitSubtree = {
70
+ /** Availability of tiles inside this subtree. */
71
+ tileAvailability: ImplicitAvailability;
72
+ /**
73
+ * Availability of tile content. Array entries correspond to content templates in source order.
74
+ * Omitted availability denotes a metadata-only subtree with no render content.
75
+ */
76
+ contentAvailability?: ImplicitAvailability | ImplicitAvailability[];
77
+ /** Availability of subtree roots immediately below this subtree. */
78
+ childSubtreeAvailability: ImplicitAvailability;
79
+ /** Property-table payloads referenced by subtree metadata declarations. */
80
+ propertyTables?: unknown;
81
+ /** Property-table reference for metadata attached to each available tile. */
82
+ tileMetadata?: unknown;
83
+ /** Property-table references for metadata attached to each content stream. */
84
+ contentMetadata?: unknown;
85
+ /** Metadata entity attached to the subtree itself. */
86
+ subtreeMetadata?: unknown;
87
+ };
88
+
89
+ /** Runtime header produced for an available implicit tile. */
90
+ export type ImplicitTileHeader = Record<string, any> & {
91
+ /** Stable source identity, including global implicit coordinates. */
92
+ id: string;
93
+ /** Materialized children that are represented by the same subtree file. */
94
+ children: ImplicitTileHeader[];
95
+ /** Optional lazy reference when this tile begins a child subtree. */
96
+ implicitSubtree?: ImplicitSubtreeReference;
97
+ /** Raw subtree metadata references preserved for application-level interpretation. */
98
+ implicitMetadata?: ImplicitSubtreeMetadata;
99
+ };
100
+
101
+ /** Metadata references inherited by generated implicit tile headers. */
102
+ export type ImplicitSubtreeMetadata = {
103
+ /** Property tables declared by the subtree. */
104
+ propertyTables?: unknown;
105
+ /** Tile-level metadata property-table reference. */
106
+ tileMetadata?: unknown;
107
+ /** Content-level metadata property-table references in content order. */
108
+ contentMetadata?: unknown;
109
+ /** Subtree-level metadata entity. */
110
+ subtreeMetadata?: unknown;
111
+ };
112
+
113
+ /** Result of materializing exactly one subtree resource. */
114
+ export type MaterializedImplicitSubtree = {
115
+ /** Header for the subtree root, including its in-subtree descendants. */
116
+ root: ImplicitTileHeader;
117
+ /** Number of available headers materialized from this subtree. */
118
+ tileCount: number;
119
+ /** Number of lazy child-subtree references created at the subtree boundary. */
120
+ childSubtreeCount: number;
121
+ };
122
+
123
+ /**
124
+ * Creates the lazy reference stored on an implicit subtree-root header.
125
+ *
126
+ * @param descriptor - Shared implicit hierarchy description.
127
+ * @param coordinates - Global coordinates of the subtree root.
128
+ * @returns Serializable reference with its concrete subtree URL.
129
+ */
130
+ export function createImplicitSubtreeReference(
131
+ descriptor: ImplicitTilingDescriptor,
132
+ coordinates: ImplicitTileCoordinates
133
+ ): ImplicitSubtreeReference {
134
+ return {
135
+ coordinates,
136
+ subtreeUrl: replaceImplicitUrlTemplate(descriptor.subtreesUrlTemplate, coordinates),
137
+ descriptor
138
+ };
139
+ }
140
+
141
+ /**
142
+ * Materializes the available tiles described by one subtree file without performing I/O.
143
+ *
144
+ * The function deliberately stops at the subtree boundary. Available child subtrees become lazy
145
+ * references so traversal, SSE, request-volume checks, and the request scheduler decide when the
146
+ * next network request is useful.
147
+ *
148
+ * @param subtree - Parsed availability data for one subtree resource.
149
+ * @param reference - Coordinates and hierarchy description for the subtree root.
150
+ * @returns Materialized root and diagnostic counts.
151
+ * @throws If the subtree root is unavailable or the descriptor is invalid.
152
+ */
153
+ export function materializeImplicitSubtree(
154
+ subtree: ParsedImplicitSubtree,
155
+ reference: ImplicitSubtreeReference
156
+ ): MaterializedImplicitSubtree {
157
+ const {descriptor} = reference;
158
+ validateImplicitDescriptor(descriptor);
159
+
160
+ const counters = {tileCount: 0, childSubtreeCount: 0};
161
+ const root = materializeAvailableTile(
162
+ subtree,
163
+ reference,
164
+ {localLevel: 0, mortonIndex: 0},
165
+ reference.coordinates,
166
+ counters
167
+ );
168
+
169
+ if (!root) {
170
+ throw new Error(
171
+ `Invalid 3D Tiles subtree: root tile is unavailable at ${reference.subtreeUrl}`
172
+ );
173
+ }
174
+
175
+ return {root, ...counters};
176
+ }
177
+
178
+ /**
179
+ * Replaces implicit coordinate placeholders in a content or subtree URL.
180
+ *
181
+ * @param templateUrl - URL containing level/x/y/z placeholders.
182
+ * @param coordinates - Global coordinates to insert.
183
+ * @returns URL with all coordinate placeholders replaced.
184
+ */
185
+ export function replaceImplicitUrlTemplate(
186
+ templateUrl: string,
187
+ coordinates: ImplicitTileCoordinates
188
+ ): string {
189
+ const values: Record<string, number> = {
190
+ level: coordinates.level,
191
+ x: coordinates.x,
192
+ y: coordinates.y,
193
+ z: coordinates.z
194
+ };
195
+ return templateUrl.replace(/{(level|x|y|z)}/gi, (_match, coordinateName: string) =>
196
+ String(values[coordinateName.toLowerCase()])
197
+ );
198
+ }
199
+
200
+ /**
201
+ * Builds one available tile and recursively materializes descendants in the same subtree.
202
+ *
203
+ * @param subtree - Parsed availability declarations.
204
+ * @param reference - Current subtree reference.
205
+ * @param localCoordinates - Level and Morton index relative to the current subtree.
206
+ * @param globalCoordinates - Coordinates relative to the complete implicit hierarchy.
207
+ * @param counters - Mutable diagnostic counters owned by the public materializer call.
208
+ * @returns Header for an available tile, or `null` for a sparse unavailable position.
209
+ */
210
+ function materializeAvailableTile(
211
+ subtree: ParsedImplicitSubtree,
212
+ reference: ImplicitSubtreeReference,
213
+ localCoordinates: {localLevel: number; mortonIndex: number},
214
+ globalCoordinates: ImplicitTileCoordinates,
215
+ counters: {tileCount: number; childSubtreeCount: number}
216
+ ): ImplicitTileHeader | null {
217
+ const {descriptor} = reference;
218
+ const childCount = getImplicitChildCount(descriptor.subdivisionScheme);
219
+ const availabilityIndex =
220
+ getLevelOffset(childCount, localCoordinates.localLevel) + localCoordinates.mortonIndex;
221
+
222
+ if (!getAvailabilityValue(subtree.tileAvailability, availabilityIndex)) {
223
+ return null;
224
+ }
225
+
226
+ counters.tileCount++;
227
+ const contentAvailability = getContentAvailability(
228
+ subtree.contentAvailability,
229
+ availabilityIndex
230
+ );
231
+ const children: ImplicitTileHeader[] = [];
232
+
233
+ if (globalCoordinates.level < descriptor.maximumLevel) {
234
+ if (localCoordinates.localLevel + 1 < descriptor.subtreeLevels) {
235
+ for (let childIndex = 0; childIndex < childCount; childIndex++) {
236
+ const childCoordinates = getChildCoordinates(
237
+ globalCoordinates,
238
+ childIndex,
239
+ descriptor.subdivisionScheme
240
+ );
241
+ const childHeader = materializeAvailableTile(
242
+ subtree,
243
+ reference,
244
+ {
245
+ localLevel: localCoordinates.localLevel + 1,
246
+ mortonIndex: localCoordinates.mortonIndex * childCount + childIndex
247
+ },
248
+ childCoordinates,
249
+ counters
250
+ );
251
+ if (childHeader) {
252
+ children.push(childHeader);
253
+ }
254
+ }
255
+ } else {
256
+ for (let childIndex = 0; childIndex < childCount; childIndex++) {
257
+ const childSubtreeIndex = localCoordinates.mortonIndex * childCount + childIndex;
258
+ if (getAvailabilityValue(subtree.childSubtreeAvailability, childSubtreeIndex)) {
259
+ const childCoordinates = getChildCoordinates(
260
+ globalCoordinates,
261
+ childIndex,
262
+ descriptor.subdivisionScheme
263
+ );
264
+ children.push(createLazyImplicitTileHeader(subtree, descriptor, childCoordinates));
265
+ counters.childSubtreeCount++;
266
+ }
267
+ }
268
+ }
269
+ }
270
+
271
+ return formatImplicitTileHeader(
272
+ subtree,
273
+ descriptor,
274
+ reference,
275
+ globalCoordinates,
276
+ contentAvailability,
277
+ children
278
+ );
279
+ }
280
+
281
+ /**
282
+ * Creates a contentless placeholder for an available child subtree.
283
+ *
284
+ * @param descriptor - Shared implicit hierarchy description.
285
+ * @param coordinates - Global coordinates of the child subtree root.
286
+ * @returns Header that participates in culling and SSE before its subtree is requested.
287
+ */
288
+ function createLazyImplicitTileHeader(
289
+ subtree: ParsedImplicitSubtree,
290
+ descriptor: ImplicitTilingDescriptor,
291
+ coordinates: ImplicitTileCoordinates
292
+ ): ImplicitTileHeader {
293
+ const reference = createImplicitSubtreeReference(descriptor, coordinates);
294
+ return {
295
+ id: getImplicitTileId(reference, coordinates),
296
+ children: [],
297
+ implicitSubtree: reference,
298
+ boundingVolume: calculateImplicitBoundingVolume(
299
+ descriptor.rootBoundingVolume,
300
+ coordinates,
301
+ descriptor.subdivisionScheme
302
+ ),
303
+ geometricError: descriptor.rootLodMetricValue / 2 ** coordinates.level,
304
+ lodMetricType: descriptor.lodMetricType,
305
+ lodMetricValue: descriptor.rootLodMetricValue / 2 ** coordinates.level,
306
+ refine: descriptor.refine,
307
+ type: TILE_TYPE.EMPTY,
308
+ implicitMetadata: getImplicitSubtreeMetadata(subtree)
309
+ };
310
+ }
311
+
312
+ /**
313
+ * Formats an available implicit tile as a runtime header.
314
+ *
315
+ * @param descriptor - Shared implicit hierarchy description.
316
+ * @param reference - Current subtree reference used to build stable IDs.
317
+ * @param coordinates - Global tile coordinates.
318
+ * @param contentAvailability - Availability for each content stream in source order.
319
+ * @param children - Materialized or lazy child headers.
320
+ * @returns Runtime tile header.
321
+ */
322
+ function formatImplicitTileHeader(
323
+ subtree: ParsedImplicitSubtree,
324
+ descriptor: ImplicitTilingDescriptor,
325
+ reference: ImplicitSubtreeReference,
326
+ coordinates: ImplicitTileCoordinates,
327
+ contentAvailability: boolean[],
328
+ children: ImplicitTileHeader[]
329
+ ): ImplicitTileHeader {
330
+ const contentUrlTemplates = descriptor.contentUrlTemplates?.length
331
+ ? descriptor.contentUrlTemplates
332
+ : [descriptor.contentUrlTemplate];
333
+ const contentEntries = contentUrlTemplates
334
+ .map((templateUrl, contentIndex) => {
335
+ if (!contentAvailability[contentIndex] || !templateUrl) {
336
+ return null;
337
+ }
338
+ return {
339
+ contentIndex,
340
+ contentUrl: replaceImplicitUrlTemplate(templateUrl, coordinates)
341
+ };
342
+ })
343
+ .filter((entry): entry is {contentIndex: number; contentUrl: string} => Boolean(entry));
344
+ const availableContentUrls = contentEntries.map(entry => entry.contentUrl);
345
+ const content = contentEntries.map(({contentIndex, contentUrl}) => ({
346
+ ...(descriptor.contentHeaders?.[contentIndex] ||
347
+ (contentIndex === 0 ? descriptor.contentHeader : {}) ||
348
+ {}),
349
+ uri: contentUrl
350
+ }));
351
+ const contentUrl = availableContentUrls[0];
352
+ const lodMetricValue = descriptor.rootLodMetricValue / 2 ** coordinates.level;
353
+
354
+ return {
355
+ id: getImplicitTileId(reference, coordinates),
356
+ children,
357
+ contentUrl,
358
+ content: content.length > 1 ? content : content[0],
359
+ contentUrls: availableContentUrls,
360
+ refine: descriptor.refine,
361
+ type: getImplicitTileType(contentUrl),
362
+ lodMetricType: descriptor.lodMetricType,
363
+ lodMetricValue,
364
+ geometricError: lodMetricValue,
365
+ boundingVolume: calculateImplicitBoundingVolume(
366
+ descriptor.rootBoundingVolume,
367
+ coordinates,
368
+ descriptor.subdivisionScheme
369
+ ),
370
+ implicitMetadata: getImplicitSubtreeMetadata(subtree)
371
+ };
372
+ }
373
+
374
+ /**
375
+ * Copies subtree metadata references without interpreting property-table schemas.
376
+ *
377
+ * Keeping the references on generated headers makes metadata available to applications while
378
+ * avoiding a false promise that the runtime has already decoded classes, enums, or values.
379
+ *
380
+ * @param subtree - Parsed subtree containing optional metadata declarations.
381
+ * @returns Metadata references, or `undefined` when the subtree declares none.
382
+ */
383
+ function getImplicitSubtreeMetadata(
384
+ subtree: ParsedImplicitSubtree
385
+ ): ImplicitSubtreeMetadata | undefined {
386
+ if (
387
+ !subtree.propertyTables &&
388
+ subtree.tileMetadata === undefined &&
389
+ !subtree.contentMetadata &&
390
+ subtree.subtreeMetadata === undefined
391
+ ) {
392
+ return undefined;
393
+ }
394
+ return {
395
+ propertyTables: subtree.propertyTables,
396
+ tileMetadata: subtree.tileMetadata,
397
+ contentMetadata: subtree.contentMetadata,
398
+ subtreeMetadata: subtree.subtreeMetadata
399
+ };
400
+ }
401
+
402
+ /**
403
+ * Returns a stable identity for contentless and content-bearing implicit tiles.
404
+ *
405
+ * @param reference - Owning subtree reference.
406
+ * @param coordinates - Global tile coordinates.
407
+ * @returns Stable source identity independent of content availability.
408
+ */
409
+ function getImplicitTileId(
410
+ reference: ImplicitSubtreeReference,
411
+ coordinates: ImplicitTileCoordinates
412
+ ): string {
413
+ return `${reference.subtreeUrl}#implicit=${coordinates.level}/${coordinates.x}/${coordinates.y}/${coordinates.z}`;
414
+ }
415
+
416
+ /**
417
+ * Reads an availability bit without assuming 32-bit coordinate or Morton arithmetic.
418
+ *
419
+ * @param availability - Constant or expanded bitstream availability.
420
+ * @param index - Zero-based availability index.
421
+ * @returns Whether the addressed tile, content, or child subtree exists.
422
+ */
423
+ function getAvailabilityValue(availability: ImplicitAvailability, index: number): boolean {
424
+ if (typeof availability?.constant === 'number') {
425
+ return Boolean(availability.constant);
426
+ }
427
+ const bitstream = availability?.explicitBitstream;
428
+ if (!bitstream) {
429
+ return false;
430
+ }
431
+ const byteIndex = Math.floor(index / 8);
432
+ const bitIndex = index % 8;
433
+ return Boolean((bitstream[byteIndex] >> bitIndex) & 1);
434
+ }
435
+
436
+ /**
437
+ * Reads every content availability stream for one tile in descriptor/source order.
438
+ *
439
+ * @param availability - Single or multiple-content availability declaration.
440
+ * @returns Availability flags aligned with the content URL templates.
441
+ */
442
+ function getContentAvailability(
443
+ availability: ImplicitAvailability | ImplicitAvailability[] | undefined,
444
+ index: number
445
+ ): boolean[] {
446
+ const streams = Array.isArray(availability) ? availability : [availability];
447
+ return streams.map(stream => getAvailabilityValue(stream || {constant: 0}, index));
448
+ }
449
+
450
+ /**
451
+ * Calculates the breadth-first availability offset for a local subtree level.
452
+ *
453
+ * @param childCount - Branching factor of the subdivision scheme.
454
+ * @param localLevel - Level relative to the subtree root.
455
+ * @returns First availability index for the requested level.
456
+ */
457
+ function getLevelOffset(childCount: number, localLevel: number): number {
458
+ return (childCount ** localLevel - 1) / (childCount - 1);
459
+ }
460
+
461
+ /**
462
+ * Calculates one child's global implicit coordinates.
463
+ *
464
+ * @param parent - Global parent coordinates.
465
+ * @param childIndex - Morton-ordered child index.
466
+ * @param subdivisionScheme - Quadtree or octree subdivision.
467
+ * @returns Global child coordinates.
468
+ */
469
+ function getChildCoordinates(
470
+ parent: ImplicitTileCoordinates,
471
+ childIndex: number,
472
+ subdivisionScheme: 'QUADTREE' | 'OCTREE'
473
+ ): ImplicitTileCoordinates {
474
+ return {
475
+ level: parent.level + 1,
476
+ x: parent.x * 2 + (childIndex & 1),
477
+ y: parent.y * 2 + ((childIndex >> 1) & 1),
478
+ z: subdivisionScheme === 'OCTREE' ? parent.z * 2 + ((childIndex >> 2) & 1) : 0
479
+ };
480
+ }
481
+
482
+ /**
483
+ * Returns the branching factor for a supported implicit subdivision scheme.
484
+ *
485
+ * @param subdivisionScheme - Quadtree or octree subdivision.
486
+ * @returns Four for quadtrees or eight for octrees.
487
+ */
488
+ function getImplicitChildCount(subdivisionScheme: 'QUADTREE' | 'OCTREE'): number {
489
+ return subdivisionScheme === 'OCTREE' ? OCTREE_CHILD_COUNT : QUADTREE_CHILD_COUNT;
490
+ }
491
+
492
+ /**
493
+ * Calculates the bounding volume for global implicit coordinates.
494
+ *
495
+ * Region volumes are divided in longitude/latitude and, for octrees, height. Oriented boxes are
496
+ * divided along their half-axis vectors. S2-derived boxes retain the root box conservatively: the
497
+ * lower-level tiles module intentionally does not duplicate the S2 geometry implementation owned
498
+ * by `@loaders.gl/3d-tiles`, and a conservative volume avoids incorrect culling.
499
+ *
500
+ * @param rootBoundingVolume - Bounding volume of the complete implicit hierarchy.
501
+ * @param coordinates - Global tile coordinates.
502
+ * @param subdivisionScheme - Quadtree or octree subdivision.
503
+ * @returns Bounding volume for the addressed tile.
504
+ */
505
+ function calculateImplicitBoundingVolume(
506
+ rootBoundingVolume: Record<string, any>,
507
+ coordinates: ImplicitTileCoordinates,
508
+ subdivisionScheme: 'QUADTREE' | 'OCTREE'
509
+ ): Record<string, any> {
510
+ if (rootBoundingVolume.region) {
511
+ const [west, south, east, north, minimumHeight, maximumHeight] = rootBoundingVolume.region;
512
+ const divisionCount = 2 ** coordinates.level;
513
+ const longitudeSize = (east - west) / divisionCount;
514
+ const latitudeSize = (north - south) / divisionCount;
515
+ const childWest = west + longitudeSize * coordinates.x;
516
+ const childSouth = south + latitudeSize * coordinates.y;
517
+ let childMinimumHeight = minimumHeight;
518
+ let childMaximumHeight = maximumHeight;
519
+ if (subdivisionScheme === 'OCTREE') {
520
+ const heightSize = (maximumHeight - minimumHeight) / divisionCount;
521
+ childMinimumHeight = minimumHeight + heightSize * coordinates.z;
522
+ childMaximumHeight = childMinimumHeight + heightSize;
523
+ }
524
+ return {
525
+ region: [
526
+ childWest,
527
+ childSouth,
528
+ childWest + longitudeSize,
529
+ childSouth + latitudeSize,
530
+ childMinimumHeight,
531
+ childMaximumHeight
532
+ ]
533
+ };
534
+ }
535
+
536
+ if (rootBoundingVolume.box) {
537
+ if (rootBoundingVolume.s2VolumeInfo) {
538
+ return {box: [...rootBoundingVolume.box], s2VolumeInfo: rootBoundingVolume.s2VolumeInfo};
539
+ }
540
+ const box = rootBoundingVolume.box;
541
+ const divisionCount = 2 ** coordinates.level;
542
+ const xOffset = -1 + (2 * coordinates.x + 1) / divisionCount;
543
+ const yOffset = -1 + (2 * coordinates.y + 1) / divisionCount;
544
+ const zOffset = -1 + (2 * coordinates.z + 1) / divisionCount;
545
+ const divideHeight = subdivisionScheme === 'OCTREE';
546
+ const center = [
547
+ box[0] + box[3] * xOffset + box[6] * yOffset + (divideHeight ? box[9] * zOffset : 0),
548
+ box[1] + box[4] * xOffset + box[7] * yOffset + (divideHeight ? box[10] * zOffset : 0),
549
+ box[2] + box[5] * xOffset + box[8] * yOffset + (divideHeight ? box[11] * zOffset : 0)
550
+ ];
551
+ return {
552
+ box: [
553
+ ...center,
554
+ box[3] / divisionCount,
555
+ box[4] / divisionCount,
556
+ box[5] / divisionCount,
557
+ box[6] / divisionCount,
558
+ box[7] / divisionCount,
559
+ box[8] / divisionCount,
560
+ divideHeight ? box[9] / divisionCount : box[9],
561
+ divideHeight ? box[10] / divisionCount : box[10],
562
+ divideHeight ? box[11] / divisionCount : box[11]
563
+ ]
564
+ };
565
+ }
566
+
567
+ throw new Error(
568
+ `Unsupported implicit 3D Tiles bounding volume: ${JSON.stringify(rootBoundingVolume)}`
569
+ );
570
+ }
571
+
572
+ /**
573
+ * Detects the runtime tile category from an available content URL.
574
+ *
575
+ * @param contentUrl - Concrete content URL, if content is available.
576
+ * @returns Runtime tile category.
577
+ */
578
+ function getImplicitTileType(contentUrl?: string): TILE_TYPE | string {
579
+ if (!contentUrl) {
580
+ return TILE_TYPE.EMPTY;
581
+ }
582
+ const extension = contentUrl.split('?')[0].split('.').pop()?.toLowerCase();
583
+ switch (extension) {
584
+ case 'pnts':
585
+ return TILE_TYPE.POINTCLOUD;
586
+ case 'i3dm':
587
+ case 'b3dm':
588
+ case 'glb':
589
+ case 'gltf':
590
+ return TILE_TYPE.SCENEGRAPH;
591
+ default:
592
+ return extension || TILE_TYPE.EMPTY;
593
+ }
594
+ }
595
+
596
+ /**
597
+ * Validates descriptor invariants before allocating a runtime subtree.
598
+ *
599
+ * @param descriptor - Serializable implicit hierarchy description.
600
+ * @throws If levels, scheme, or maximum-level semantics are invalid.
601
+ */
602
+ function validateImplicitDescriptor(descriptor: ImplicitTilingDescriptor): void {
603
+ if (descriptor.subdivisionScheme !== 'QUADTREE' && descriptor.subdivisionScheme !== 'OCTREE') {
604
+ throw new Error(`Unsupported implicit subdivision scheme: ${descriptor.subdivisionScheme}`);
605
+ }
606
+ if (!Number.isInteger(descriptor.subtreeLevels) || descriptor.subtreeLevels < 1) {
607
+ throw new Error('Implicit subtreeLevels must be a positive integer');
608
+ }
609
+ if (!Number.isInteger(descriptor.maximumLevel) || descriptor.maximumLevel < 0) {
610
+ throw new Error('Implicit availableLevels must describe at least the root level');
611
+ }
612
+ }