@loaders.gl/tiles 4.5.3 → 5.0.0-alpha.10

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 (295) hide show
  1. package/LICENSE +14 -3
  2. package/README.md +136 -1
  3. package/dist/dist.dev.js +22570 -4947
  4. package/dist/dist.min.js +3 -1
  5. package/dist/image-set.d.ts +94 -0
  6. package/dist/image-set.d.ts.map +1 -0
  7. package/dist/image-set.js +243 -0
  8. package/dist/image-set.js.map +1 -0
  9. package/dist/index.cjs +10171 -2117
  10. package/dist/index.cjs.map +3 -4
  11. package/dist/index.d.ts +50 -9
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +28 -8
  14. package/dist/index.js.map +1 -1
  15. package/dist/point-cloud/point-cloud-tile.d.ts +47 -0
  16. package/dist/point-cloud/point-cloud-tile.d.ts.map +1 -0
  17. package/dist/point-cloud/point-cloud-tile.js +70 -0
  18. package/dist/point-cloud/point-cloud-tile.js.map +1 -0
  19. package/dist/point-cloud/point-cloud-tileset.d.ts +135 -0
  20. package/dist/point-cloud/point-cloud-tileset.d.ts.map +1 -0
  21. package/dist/point-cloud/point-cloud-tileset.js +549 -0
  22. package/dist/point-cloud/point-cloud-tileset.js.map +1 -0
  23. package/dist/point-cloud/types.d.ts +81 -0
  24. package/dist/point-cloud/types.d.ts.map +1 -0
  25. package/dist/point-cloud/types.js +5 -0
  26. package/dist/point-cloud/types.js.map +1 -0
  27. package/dist/raster-set.d.ts +124 -0
  28. package/dist/raster-set.d.ts.map +1 -0
  29. package/dist/raster-set.js +348 -0
  30. package/dist/raster-set.js.map +1 -0
  31. package/dist/spatial/format-spatial-reference.d.ts +68 -0
  32. package/dist/spatial/format-spatial-reference.d.ts.map +1 -0
  33. package/dist/spatial/format-spatial-reference.js +291 -0
  34. package/dist/spatial/format-spatial-reference.js.map +1 -0
  35. package/dist/spatial/get-spatial-coordinate-frame.d.ts +20 -0
  36. package/dist/spatial/get-spatial-coordinate-frame.d.ts.map +1 -0
  37. package/dist/spatial/get-spatial-coordinate-frame.js +139 -0
  38. package/dist/spatial/get-spatial-coordinate-frame.js.map +1 -0
  39. package/dist/spatial/i3s-elevation.d.ts +20 -0
  40. package/dist/spatial/i3s-elevation.d.ts.map +1 -0
  41. package/dist/spatial/i3s-elevation.js +90 -0
  42. package/dist/spatial/i3s-elevation.js.map +1 -0
  43. package/dist/spatial/i3s-spatial-transformer.d.ts +189 -0
  44. package/dist/spatial/i3s-spatial-transformer.d.ts.map +1 -0
  45. package/dist/spatial/i3s-spatial-transformer.js +686 -0
  46. package/dist/spatial/i3s-spatial-transformer.js.map +1 -0
  47. package/dist/spatial/normalize-crs-identifier.d.ts +15 -0
  48. package/dist/spatial/normalize-crs-identifier.d.ts.map +1 -0
  49. package/dist/spatial/normalize-crs-identifier.js +22 -0
  50. package/dist/spatial/normalize-crs-identifier.js.map +1 -0
  51. package/dist/spatial/spatial-coordinate-transformer.d.ts +52 -0
  52. package/dist/spatial/spatial-coordinate-transformer.d.ts.map +1 -0
  53. package/dist/spatial/spatial-coordinate-transformer.js +295 -0
  54. package/dist/spatial/spatial-coordinate-transformer.js.map +1 -0
  55. package/dist/spatial/spatial-resource-registry.d.ts +44 -0
  56. package/dist/spatial/spatial-resource-registry.d.ts.map +1 -0
  57. package/dist/spatial/spatial-resource-registry.js +67 -0
  58. package/dist/spatial/spatial-resource-registry.js.map +1 -0
  59. package/dist/spatial/spatial-types.d.ts +190 -0
  60. package/dist/spatial/spatial-types.d.ts.map +1 -0
  61. package/dist/spatial/spatial-types.js +192 -0
  62. package/dist/spatial/spatial-types.js.map +1 -0
  63. package/dist/spatial/tiles-3d-spatial-transformer.d.ts +32 -0
  64. package/dist/spatial/tiles-3d-spatial-transformer.d.ts.map +1 -0
  65. package/dist/spatial/tiles-3d-spatial-transformer.js +175 -0
  66. package/dist/spatial/tiles-3d-spatial-transformer.js.map +1 -0
  67. package/dist/tileset-2d/adapter.d.ts +37 -0
  68. package/dist/tileset-2d/adapter.d.ts.map +1 -0
  69. package/dist/tileset-2d/adapter.js +5 -0
  70. package/dist/tileset-2d/adapter.js.map +1 -0
  71. package/dist/tileset-2d/index.d.ts +7 -0
  72. package/dist/tileset-2d/index.d.ts.map +1 -0
  73. package/dist/tileset-2d/index.js +6 -0
  74. package/dist/tileset-2d/index.js.map +1 -0
  75. package/dist/tileset-2d/tile-2d-header.d.ts +68 -0
  76. package/dist/tileset-2d/tile-2d-header.d.ts.map +1 -0
  77. package/dist/tileset-2d/tile-2d-header.js +181 -0
  78. package/dist/tileset-2d/tile-2d-header.js.map +1 -0
  79. package/dist/tileset-2d/tileset-2d.d.ts +200 -0
  80. package/dist/tileset-2d/tileset-2d.d.ts.map +1 -0
  81. package/dist/tileset-2d/tileset-2d.js +532 -0
  82. package/dist/tileset-2d/tileset-2d.js.map +1 -0
  83. package/dist/tileset-2d/types.d.ts +44 -0
  84. package/dist/tileset-2d/types.d.ts.map +1 -0
  85. package/dist/tileset-2d/types.js +5 -0
  86. package/dist/tileset-2d/types.js.map +1 -0
  87. package/dist/tileset-2d.d.ts +2 -0
  88. package/dist/tileset-2d.d.ts.map +1 -0
  89. package/dist/tileset-2d.js +5 -0
  90. package/dist/tileset-2d.js.map +1 -0
  91. package/dist/tileset-3d/common/indexed-archive-tileset-source.d.ts +60 -0
  92. package/dist/tileset-3d/common/indexed-archive-tileset-source.d.ts.map +1 -0
  93. package/dist/tileset-3d/common/indexed-archive-tileset-source.js +130 -0
  94. package/dist/tileset-3d/common/indexed-archive-tileset-source.js.map +1 -0
  95. package/dist/tileset-3d/common/tile-3d-contracts.d.ts +63 -0
  96. package/dist/tileset-3d/common/tile-3d-contracts.d.ts.map +1 -0
  97. package/dist/tileset-3d/common/tile-3d-contracts.js +51 -0
  98. package/dist/tileset-3d/common/tile-3d-contracts.js.map +1 -0
  99. package/dist/tileset-3d/common/tile-3d.d.ts +410 -0
  100. package/dist/tileset-3d/common/tile-3d.d.ts.map +1 -0
  101. package/dist/tileset-3d/common/tile-3d.js +1056 -0
  102. package/dist/tileset-3d/common/tile-3d.js.map +1 -0
  103. package/dist/tileset-3d/common/tileset-3d.d.ts +360 -0
  104. package/dist/tileset-3d/common/tileset-3d.d.ts.map +1 -0
  105. package/dist/tileset-3d/common/tileset-3d.js +846 -0
  106. package/dist/tileset-3d/common/tileset-3d.js.map +1 -0
  107. package/dist/tileset-3d/common/tileset-cache.d.ts +29 -0
  108. package/dist/tileset-3d/common/tileset-cache.d.ts.map +1 -0
  109. package/dist/{tileset → tileset-3d/common}/tileset-cache.js +12 -4
  110. package/dist/tileset-3d/common/tileset-cache.js.map +1 -0
  111. package/dist/tileset-3d/common/tileset-content-traversal.d.ts +37 -0
  112. package/dist/tileset-3d/common/tileset-content-traversal.d.ts.map +1 -0
  113. package/dist/tileset-3d/common/tileset-content-traversal.js +80 -0
  114. package/dist/tileset-3d/common/tileset-content-traversal.js.map +1 -0
  115. package/dist/tileset-3d/common/tileset-observability.d.ts +60 -0
  116. package/dist/tileset-3d/common/tileset-observability.d.ts.map +1 -0
  117. package/dist/tileset-3d/common/tileset-observability.js +39 -0
  118. package/dist/tileset-3d/common/tileset-observability.js.map +1 -0
  119. package/dist/tileset-3d/common/tileset-source.d.ts +228 -0
  120. package/dist/tileset-3d/common/tileset-source.d.ts.map +1 -0
  121. package/dist/tileset-3d/common/tileset-source.js +17 -0
  122. package/dist/tileset-3d/common/tileset-source.js.map +1 -0
  123. package/dist/{tileset → tileset-3d/common}/tileset-traverser.d.ts +35 -3
  124. package/dist/tileset-3d/common/tileset-traverser.d.ts.map +1 -0
  125. package/dist/{tileset → tileset-3d/common}/tileset-traverser.js +61 -13
  126. package/dist/tileset-3d/common/tileset-traverser.js.map +1 -0
  127. package/dist/tileset-3d/format-3d-tiles/implicit-tiling.d.ts +182 -0
  128. package/dist/tileset-3d/format-3d-tiles/implicit-tiling.d.ts.map +1 -0
  129. package/dist/tileset-3d/format-3d-tiles/implicit-tiling.js +681 -0
  130. package/dist/tileset-3d/format-3d-tiles/implicit-tiling.js.map +1 -0
  131. package/dist/tileset-3d/format-3d-tiles/tiles-3d-source.d.ts +221 -0
  132. package/dist/tileset-3d/format-3d-tiles/tiles-3d-source.d.ts.map +1 -0
  133. package/dist/tileset-3d/format-3d-tiles/tiles-3d-source.js +770 -0
  134. package/dist/tileset-3d/format-3d-tiles/tiles-3d-source.js.map +1 -0
  135. package/dist/tileset-3d/format-3d-tiles/tileset-3d-traverser.d.ts +110 -0
  136. package/dist/tileset-3d/format-3d-tiles/tileset-3d-traverser.d.ts.map +1 -0
  137. package/dist/tileset-3d/format-3d-tiles/tileset-3d-traverser.js +331 -0
  138. package/dist/tileset-3d/format-3d-tiles/tileset-3d-traverser.js.map +1 -0
  139. package/dist/tileset-3d/format-i3s/i3s-pending-tiles-register.d.ts.map +1 -0
  140. package/dist/{tileset → tileset-3d}/format-i3s/i3s-pending-tiles-register.js +3 -0
  141. package/dist/tileset-3d/format-i3s/i3s-pending-tiles-register.js.map +1 -0
  142. package/dist/tileset-3d/format-i3s/i3s-source.d.ts +116 -0
  143. package/dist/tileset-3d/format-i3s/i3s-source.d.ts.map +1 -0
  144. package/dist/tileset-3d/format-i3s/i3s-source.js +450 -0
  145. package/dist/tileset-3d/format-i3s/i3s-source.js.map +1 -0
  146. package/dist/tileset-3d/format-i3s/i3s-tile-manager.d.ts.map +1 -0
  147. package/dist/{tileset → tileset-3d}/format-i3s/i3s-tile-manager.js +5 -2
  148. package/dist/tileset-3d/format-i3s/i3s-tile-manager.js.map +1 -0
  149. package/dist/{tileset → tileset-3d}/format-i3s/i3s-tileset-traverser.d.ts +2 -2
  150. package/dist/tileset-3d/format-i3s/i3s-tileset-traverser.d.ts.map +1 -0
  151. package/dist/{tileset → tileset-3d}/format-i3s/i3s-tileset-traverser.js +17 -23
  152. package/dist/tileset-3d/format-i3s/i3s-tileset-traverser.js.map +1 -0
  153. package/dist/{tileset → tileset-3d}/helpers/3d-tiles-options.d.ts +1 -1
  154. package/dist/tileset-3d/helpers/3d-tiles-options.d.ts.map +1 -0
  155. package/dist/{tileset → tileset-3d}/helpers/3d-tiles-options.js +3 -0
  156. package/dist/tileset-3d/helpers/3d-tiles-options.js.map +1 -0
  157. package/dist/{tileset → tileset-3d}/helpers/bounding-volume.d.ts +7 -3
  158. package/dist/tileset-3d/helpers/bounding-volume.d.ts.map +1 -0
  159. package/dist/{tileset → tileset-3d}/helpers/bounding-volume.js +20 -91
  160. package/dist/tileset-3d/helpers/bounding-volume.js.map +1 -0
  161. package/dist/tileset-3d/helpers/frame-state.d.ts +93 -0
  162. package/dist/tileset-3d/helpers/frame-state.d.ts.map +1 -0
  163. package/dist/{tileset → tileset-3d}/helpers/frame-state.js +92 -10
  164. package/dist/tileset-3d/helpers/frame-state.js.map +1 -0
  165. package/dist/{tileset → tileset-3d}/helpers/i3s-lod.d.ts +1 -1
  166. package/dist/tileset-3d/helpers/i3s-lod.d.ts.map +1 -0
  167. package/dist/{tileset → tileset-3d}/helpers/i3s-lod.js +21 -9
  168. package/dist/tileset-3d/helpers/i3s-lod.js.map +1 -0
  169. package/dist/tileset-3d/helpers/s2-bounding-volume.d.ts +17 -0
  170. package/dist/tileset-3d/helpers/s2-bounding-volume.d.ts.map +1 -0
  171. package/dist/tileset-3d/helpers/s2-bounding-volume.js +20 -0
  172. package/dist/tileset-3d/helpers/s2-bounding-volume.js.map +1 -0
  173. package/dist/tileset-3d/helpers/tiles-3d-lod.d.ts +72 -0
  174. package/dist/tileset-3d/helpers/tiles-3d-lod.d.ts.map +1 -0
  175. package/dist/tileset-3d/helpers/tiles-3d-lod.js +257 -0
  176. package/dist/tileset-3d/helpers/tiles-3d-lod.js.map +1 -0
  177. package/dist/tileset-3d/helpers/tiles-3d-request-priority.d.ts +134 -0
  178. package/dist/tileset-3d/helpers/tiles-3d-request-priority.d.ts.map +1 -0
  179. package/dist/tileset-3d/helpers/tiles-3d-request-priority.js +179 -0
  180. package/dist/tileset-3d/helpers/tiles-3d-request-priority.js.map +1 -0
  181. package/dist/{tileset → tileset-3d}/helpers/transform-utils.d.ts +1 -1
  182. package/dist/tileset-3d/helpers/transform-utils.d.ts.map +1 -0
  183. package/dist/tileset-3d/helpers/transform-utils.js.map +1 -0
  184. package/dist/tileset-3d/helpers/zoom.d.ts.map +1 -0
  185. package/dist/tileset-3d/helpers/zoom.js.map +1 -0
  186. package/dist/types.d.ts +14 -0
  187. package/dist/types.d.ts.map +1 -1
  188. package/dist/types.js +3 -0
  189. package/dist/types.js.map +1 -1
  190. package/dist/utils/doubly-linked-list.d.ts.map +1 -1
  191. package/dist/utils/doubly-linked-list.js +1 -0
  192. package/dist/utils/doubly-linked-list.js.map +1 -1
  193. package/dist/utils/managed-array.d.ts.map +1 -1
  194. package/dist/utils/managed-array.js +1 -0
  195. package/dist/utils/managed-array.js.map +1 -1
  196. package/package.json +12 -9
  197. package/src/image-set.ts +328 -0
  198. package/src/index.ts +157 -9
  199. package/src/point-cloud/point-cloud-tile.ts +80 -0
  200. package/src/point-cloud/point-cloud-tileset.ts +767 -0
  201. package/src/point-cloud/types.ts +99 -0
  202. package/src/raster-set.ts +504 -0
  203. package/src/spatial/format-spatial-reference.ts +385 -0
  204. package/src/spatial/get-spatial-coordinate-frame.ts +169 -0
  205. package/src/spatial/i3s-elevation.ts +96 -0
  206. package/src/spatial/i3s-spatial-transformer.ts +957 -0
  207. package/src/spatial/normalize-crs-identifier.ts +26 -0
  208. package/src/spatial/spatial-coordinate-transformer.ts +376 -0
  209. package/src/spatial/spatial-resource-registry.ts +79 -0
  210. package/src/spatial/spatial-types.ts +422 -0
  211. package/src/spatial/tiles-3d-spatial-transformer.ts +206 -0
  212. package/src/tileset-2d/adapter.ts +46 -0
  213. package/src/tileset-2d/index.ts +33 -0
  214. package/src/tileset-2d/tile-2d-header.ts +224 -0
  215. package/src/tileset-2d/tileset-2d.ts +715 -0
  216. package/src/tileset-2d/types.ts +39 -0
  217. package/src/tileset-2d.ts +5 -0
  218. package/src/tileset-3d/common/indexed-archive-tileset-source.ts +205 -0
  219. package/src/tileset-3d/common/tile-3d-contracts.ts +115 -0
  220. package/src/tileset-3d/common/tile-3d.ts +1310 -0
  221. package/src/tileset-3d/common/tileset-3d.ts +1166 -0
  222. package/src/{tileset → tileset-3d/common}/tileset-cache.ts +16 -8
  223. package/src/tileset-3d/common/tileset-content-traversal.ts +114 -0
  224. package/src/tileset-3d/common/tileset-observability.ts +100 -0
  225. package/src/tileset-3d/common/tileset-source.ts +284 -0
  226. package/src/{tileset → tileset-3d/common}/tileset-traverser.ts +74 -14
  227. package/src/tileset-3d/format-3d-tiles/implicit-tiling.ts +1059 -0
  228. package/src/tileset-3d/format-3d-tiles/tiles-3d-source.ts +1025 -0
  229. package/src/tileset-3d/format-3d-tiles/tileset-3d-traverser.ts +379 -0
  230. package/src/{tileset → tileset-3d}/format-i3s/i3s-pending-tiles-register.ts +4 -0
  231. package/src/tileset-3d/format-i3s/i3s-source.ts +557 -0
  232. package/src/{tileset → tileset-3d}/format-i3s/i3s-tile-manager.ts +6 -2
  233. package/src/{tileset → tileset-3d}/format-i3s/i3s-tileset-traverser.ts +20 -24
  234. package/src/{tileset → tileset-3d}/helpers/3d-tiles-options.ts +5 -1
  235. package/src/{tileset → tileset-3d}/helpers/bounding-volume.ts +31 -114
  236. package/src/tileset-3d/helpers/frame-state.ts +322 -0
  237. package/src/{tileset → tileset-3d}/helpers/i3s-lod.ts +24 -10
  238. package/src/tileset-3d/helpers/s2-bounding-volume.ts +35 -0
  239. package/src/tileset-3d/helpers/tiles-3d-lod.ts +370 -0
  240. package/src/tileset-3d/helpers/tiles-3d-request-priority.ts +301 -0
  241. package/src/{tileset → tileset-3d}/helpers/transform-utils.ts +1 -1
  242. package/src/types.ts +18 -0
  243. package/src/utils/doubly-linked-list.ts +2 -0
  244. package/src/utils/managed-array.ts +2 -0
  245. package/dist/tileset/format-3d-tiles/tileset-3d-traverser.d.ts +0 -7
  246. package/dist/tileset/format-3d-tiles/tileset-3d-traverser.d.ts.map +0 -1
  247. package/dist/tileset/format-3d-tiles/tileset-3d-traverser.js +0 -53
  248. package/dist/tileset/format-3d-tiles/tileset-3d-traverser.js.map +0 -1
  249. package/dist/tileset/format-i3s/i3s-pending-tiles-register.d.ts.map +0 -1
  250. package/dist/tileset/format-i3s/i3s-pending-tiles-register.js.map +0 -1
  251. package/dist/tileset/format-i3s/i3s-tile-manager.d.ts.map +0 -1
  252. package/dist/tileset/format-i3s/i3s-tile-manager.js.map +0 -1
  253. package/dist/tileset/format-i3s/i3s-tileset-traverser.d.ts.map +0 -1
  254. package/dist/tileset/format-i3s/i3s-tileset-traverser.js.map +0 -1
  255. package/dist/tileset/helpers/3d-tiles-options.d.ts.map +0 -1
  256. package/dist/tileset/helpers/3d-tiles-options.js.map +0 -1
  257. package/dist/tileset/helpers/bounding-volume.d.ts.map +0 -1
  258. package/dist/tileset/helpers/bounding-volume.js.map +0 -1
  259. package/dist/tileset/helpers/frame-state.d.ts +0 -28
  260. package/dist/tileset/helpers/frame-state.d.ts.map +0 -1
  261. package/dist/tileset/helpers/frame-state.js.map +0 -1
  262. package/dist/tileset/helpers/i3s-lod.d.ts.map +0 -1
  263. package/dist/tileset/helpers/i3s-lod.js.map +0 -1
  264. package/dist/tileset/helpers/tiles-3d-lod.d.ts +0 -8
  265. package/dist/tileset/helpers/tiles-3d-lod.d.ts.map +0 -1
  266. package/dist/tileset/helpers/tiles-3d-lod.js +0 -113
  267. package/dist/tileset/helpers/tiles-3d-lod.js.map +0 -1
  268. package/dist/tileset/helpers/transform-utils.d.ts.map +0 -1
  269. package/dist/tileset/helpers/transform-utils.js.map +0 -1
  270. package/dist/tileset/helpers/zoom.d.ts.map +0 -1
  271. package/dist/tileset/helpers/zoom.js.map +0 -1
  272. package/dist/tileset/tile-3d.d.ts +0 -230
  273. package/dist/tileset/tile-3d.d.ts.map +0 -1
  274. package/dist/tileset/tile-3d.js +0 -631
  275. package/dist/tileset/tile-3d.js.map +0 -1
  276. package/dist/tileset/tileset-3d.d.ts +0 -284
  277. package/dist/tileset/tileset-3d.d.ts.map +0 -1
  278. package/dist/tileset/tileset-3d.js +0 -814
  279. package/dist/tileset/tileset-3d.js.map +0 -1
  280. package/dist/tileset/tileset-cache.d.ts +0 -19
  281. package/dist/tileset/tileset-cache.d.ts.map +0 -1
  282. package/dist/tileset/tileset-cache.js.map +0 -1
  283. package/dist/tileset/tileset-traverser.d.ts.map +0 -1
  284. package/dist/tileset/tileset-traverser.js.map +0 -1
  285. package/src/tileset/format-3d-tiles/tileset-3d-traverser.ts +0 -63
  286. package/src/tileset/helpers/frame-state.ts +0 -183
  287. package/src/tileset/helpers/tiles-3d-lod.ts +0 -149
  288. package/src/tileset/tile-3d.ts +0 -788
  289. package/src/tileset/tileset-3d.ts +0 -1037
  290. /package/dist/{tileset → tileset-3d}/format-i3s/i3s-pending-tiles-register.d.ts +0 -0
  291. /package/dist/{tileset → tileset-3d}/format-i3s/i3s-tile-manager.d.ts +0 -0
  292. /package/dist/{tileset → tileset-3d}/helpers/transform-utils.js +0 -0
  293. /package/dist/{tileset → tileset-3d}/helpers/zoom.d.ts +0 -0
  294. /package/dist/{tileset → tileset-3d}/helpers/zoom.js +0 -0
  295. /package/src/{tileset → tileset-3d}/helpers/zoom.ts +0 -0
@@ -0,0 +1,1059 @@
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
+ // TypeScript's legacy `node` resolution does not inspect package `exports`, but this public
7
+ // subpath is resolved by the package at runtime and avoids loading unrelated DGGS decoders.
8
+ // @ts-expect-error Conditional package subpath exports require a modern module resolver.
9
+ import {getS2DescendantIndex, getS2IndexFromToken, getS2TokenFromIndex} from '@math.gl/dggs/s2';
10
+ import {convertS2BoundingVolumeToOBB, type S2VolumeInfo} from '../helpers/s2-bounding-volume';
11
+
12
+ const QUADTREE_CHILD_COUNT = 4;
13
+ const OCTREE_CHILD_COUNT = 8;
14
+ const AVAILABILITY_RANK_CACHE = new WeakMap<ImplicitAvailability, Uint32Array>();
15
+
16
+ /** Global coordinates of a tile in an implicit quadtree or octree. */
17
+ export type ImplicitTileCoordinates = {
18
+ /** Zero-based level relative to the implicit-tile root. */
19
+ level: number;
20
+ /** Zero-based horizontal coordinate at {@link level}. */
21
+ x: number;
22
+ /** Zero-based vertical coordinate at {@link level}. */
23
+ y: number;
24
+ /** Zero-based height coordinate at {@link level}; always zero for quadtrees. */
25
+ z: number;
26
+ };
27
+
28
+ /** Serializable information shared by every subtree in one implicit-tile hierarchy. */
29
+ export type ImplicitTilingDescriptor = {
30
+ /** Absolute template URL for render content, or an empty string for a contentless hierarchy. */
31
+ contentUrlTemplate: string;
32
+ /** Absolute templates for every implicit content stream, in source order. */
33
+ contentUrlTemplates?: string[];
34
+ /** Non-URI content metadata inherited by each available implicit content resource. */
35
+ contentHeader?: Record<string, any>;
36
+ /** Metadata inherited by every implicit content stream, in source order. */
37
+ contentHeaders?: Array<Record<string, any>>;
38
+ /** Whether generated headers preserve the standard `contents` source representation. */
39
+ useCanonicalContents?: boolean;
40
+ /** Absolute template URL for subtree availability files. */
41
+ subtreesUrlTemplate: string;
42
+ /** Spatial subdivision used by the hierarchy. */
43
+ subdivisionScheme: 'QUADTREE' | 'OCTREE';
44
+ /** Number of tile levels represented inside each subtree file. */
45
+ subtreeLevels: number;
46
+ /** Last valid global tile level, where the implicit root is level zero. */
47
+ maximumLevel: number;
48
+ /** Refinement mode inherited by generated tiles. */
49
+ refine: TILE_REFINEMENT | string;
50
+ /** LOD metric used by generated tiles. */
51
+ lodMetricType: LOD_METRIC_TYPE.GEOMETRIC_ERROR;
52
+ /** Source geometric error of the implicit root, in local meters. */
53
+ rootLodMetricValue: number;
54
+ /** Bounding volume of the complete implicit hierarchy. */
55
+ rootBoundingVolume: Record<string, any>;
56
+ /** Whether runtime transforms scale geometric error; false for draft 2.0. */
57
+ scaleGeometricError?: boolean;
58
+ /** Package files that may satisfy concrete content and subtree template references. */
59
+ resourceFiles?: ImplicitPackageFile[];
60
+ };
61
+
62
+ /** Structured-cloneable file retained from a draft glTF package. */
63
+ export type ImplicitPackageFile = {
64
+ name?: string;
65
+ mimeType: string;
66
+ uri?: string;
67
+ originalUri?: string;
68
+ data?: ArrayBuffer;
69
+ byteOffset: number;
70
+ byteLength: number;
71
+ bufferUri?: string;
72
+ };
73
+
74
+ /** Package reference attached to a generated content or subtree URL. */
75
+ export type ImplicitPackageResource = {
76
+ fileIndex: number;
77
+ files: ImplicitPackageFile[];
78
+ };
79
+
80
+ /** Lazy pointer from a runtime tile to the subtree that defines it and its descendants. */
81
+ export type ImplicitSubtreeReference = {
82
+ /** Global coordinates of the referenced subtree root. */
83
+ coordinates: ImplicitTileCoordinates;
84
+ /** Absolute subtree URL before source-level query inheritance is applied. */
85
+ subtreeUrl: string;
86
+ /** Serializable hierarchy description used to materialize the subtree. */
87
+ descriptor: ImplicitTilingDescriptor;
88
+ /** Embedded package resource matching {@link subtreeUrl}, when available. */
89
+ resource?: ImplicitPackageResource;
90
+ };
91
+
92
+ /** Constant or bitstream-backed availability declaration from a parsed subtree. */
93
+ export type ImplicitAvailability = {
94
+ /** Constant availability value applied to every addressed element. */
95
+ constant?: number;
96
+ /** Expanded least-significant-bit-first availability buffer. */
97
+ explicitBitstream?: Uint8Array;
98
+ };
99
+
100
+ /** Parsed subset of the 3D Tiles subtree schema needed for hierarchy materialization. */
101
+ export type ParsedImplicitSubtree = {
102
+ /** Availability of tiles inside this subtree. */
103
+ tileAvailability: ImplicitAvailability;
104
+ /**
105
+ * Availability of tile content. Array entries correspond to content templates in source order.
106
+ * Omitted availability denotes a metadata-only subtree with no render content.
107
+ */
108
+ contentAvailability?: ImplicitAvailability | ImplicitAvailability[];
109
+ /** Availability of subtree roots immediately below this subtree. */
110
+ childSubtreeAvailability: ImplicitAvailability;
111
+ /** Property-table payloads referenced by subtree metadata declarations. */
112
+ propertyTables?: unknown;
113
+ /** Property-table reference for metadata attached to each available tile. */
114
+ tileMetadata?: unknown;
115
+ /** Property-table references for metadata attached to each content stream. */
116
+ contentMetadata?: unknown;
117
+ /** Metadata entity attached to the subtree itself. */
118
+ subtreeMetadata?: unknown;
119
+ /** Decoded standard attribute accessors keyed by tile semantic. */
120
+ tileAttributes?: Record<string, ArrayLike<number>>;
121
+ /** Decoded standard attribute accessors keyed by content semantic. */
122
+ contentAttributes?: Record<string, ArrayLike<number>>;
123
+ /** Index of the property table assigned to available tiles. */
124
+ tileProperties?: unknown;
125
+ /** Index of the property table assigned to available contents. */
126
+ contentProperties?: unknown;
127
+ /** Fully decoded property rows for available tiles, in availability order. */
128
+ tilePropertyRows?: Array<Record<string, unknown>>;
129
+ /** Fully decoded property rows for available contents, in availability order. */
130
+ contentPropertyRows?: Array<Record<string, unknown>>;
131
+ /** Tile property rows filtered to values permitted in URI templates. */
132
+ tileTemplatePropertyRows?: Array<Record<string, unknown>>;
133
+ /** Content property rows filtered to values permitted in URI templates. */
134
+ contentTemplatePropertyRows?: Array<Record<string, unknown>>;
135
+ /** Files retained by a glTF subtree package for later content/subtree resolution. */
136
+ resourceFiles?: ImplicitPackageFile[];
137
+ };
138
+
139
+ /** Runtime header produced for an available implicit tile. */
140
+ export type ImplicitTileHeader = Record<string, any> & {
141
+ /** Stable source identity, including global implicit coordinates. */
142
+ id: string;
143
+ /** Materialized children that are represented by the same subtree file. */
144
+ children: ImplicitTileHeader[];
145
+ /** Optional lazy reference when this tile begins a child subtree. */
146
+ implicitSubtree?: ImplicitSubtreeReference;
147
+ /** Raw subtree metadata references preserved for application-level interpretation. */
148
+ implicitMetadata?: ImplicitSubtreeMetadata;
149
+ };
150
+
151
+ /** Metadata references inherited by generated implicit tile headers. */
152
+ export type ImplicitSubtreeMetadata = {
153
+ /** Property tables declared by the subtree. */
154
+ propertyTables?: unknown;
155
+ /** Tile-level metadata property-table reference. */
156
+ tileMetadata?: unknown;
157
+ /** Content-level metadata property-table references in content order. */
158
+ contentMetadata?: unknown;
159
+ /** Subtree-level metadata entity. */
160
+ subtreeMetadata?: unknown;
161
+ /** Decoded property-table row for this tile. */
162
+ tileProperties?: Record<string, unknown>;
163
+ /** Decoded property-table row for this tile's available content. */
164
+ contentProperties?: Record<string, unknown>;
165
+ };
166
+
167
+ /** Result of materializing exactly one subtree resource. */
168
+ export type MaterializedImplicitSubtree = {
169
+ /** Header for the subtree root, including its in-subtree descendants. */
170
+ root: ImplicitTileHeader;
171
+ /** Number of available headers materialized from this subtree. */
172
+ tileCount: number;
173
+ /** Number of lazy child-subtree references created at the subtree boundary. */
174
+ childSubtreeCount: number;
175
+ };
176
+
177
+ /**
178
+ * Creates the lazy reference stored on an implicit subtree-root header.
179
+ *
180
+ * @param descriptor - Shared implicit hierarchy description.
181
+ * @param coordinates - Global coordinates of the subtree root.
182
+ * @returns Serializable reference with its concrete subtree URL.
183
+ */
184
+ export function createImplicitSubtreeReference(
185
+ descriptor: ImplicitTilingDescriptor,
186
+ coordinates: ImplicitTileCoordinates
187
+ ): ImplicitSubtreeReference {
188
+ return {
189
+ coordinates,
190
+ subtreeUrl: replaceImplicitUrlTemplate(descriptor.subtreesUrlTemplate, coordinates),
191
+ descriptor,
192
+ resource: findImplicitPackageResource(
193
+ descriptor.resourceFiles,
194
+ replaceImplicitUrlTemplate(descriptor.subtreesUrlTemplate, coordinates)
195
+ )
196
+ };
197
+ }
198
+
199
+ /**
200
+ * Materializes the available tiles described by one subtree file without performing I/O.
201
+ *
202
+ * The function deliberately stops at the subtree boundary. Available child subtrees become lazy
203
+ * references so traversal, SSE, request-volume checks, and the request scheduler decide when the
204
+ * next network request is useful.
205
+ *
206
+ * @param subtree - Parsed availability data for one subtree resource.
207
+ * @param reference - Coordinates and hierarchy description for the subtree root.
208
+ * @returns Materialized root and diagnostic counts.
209
+ * @throws If the subtree root is unavailable or the descriptor is invalid.
210
+ */
211
+ export function materializeImplicitSubtree(
212
+ subtree: ParsedImplicitSubtree,
213
+ reference: ImplicitSubtreeReference
214
+ ): MaterializedImplicitSubtree {
215
+ const {descriptor} = reference;
216
+ validateImplicitDescriptor(descriptor);
217
+ validateImplicitSubtree(subtree, descriptor);
218
+
219
+ const counters = {tileCount: 0, childSubtreeCount: 0};
220
+ const root = materializeAvailableTile(
221
+ subtree,
222
+ reference,
223
+ {localLevel: 0, mortonIndex: 0},
224
+ reference.coordinates,
225
+ counters
226
+ );
227
+
228
+ if (!root) {
229
+ throw new Error(
230
+ `Invalid 3D Tiles subtree: root tile is unavailable at ${reference.subtreeUrl}`
231
+ );
232
+ }
233
+
234
+ return {root, ...counters};
235
+ }
236
+
237
+ /** Validates availability lengths and tightly packed draft attribute/property row counts. */
238
+ function validateImplicitSubtree(
239
+ subtree: ParsedImplicitSubtree,
240
+ descriptor: ImplicitTilingDescriptor
241
+ ): void {
242
+ const childCount = getImplicitChildCount(descriptor.subdivisionScheme);
243
+ const tileElementCount = (childCount ** descriptor.subtreeLevels - 1) / (childCount - 1);
244
+ const childSubtreeElementCount = childCount ** descriptor.subtreeLevels;
245
+ validateAvailabilityLength(subtree.tileAvailability, tileElementCount, 'tile');
246
+ const contentAvailabilityStreams = Array.isArray(subtree.contentAvailability)
247
+ ? subtree.contentAvailability
248
+ : subtree.contentAvailability
249
+ ? [subtree.contentAvailability]
250
+ : [];
251
+ for (const contentAvailability of contentAvailabilityStreams) {
252
+ validateAvailabilityLength(contentAvailability, tileElementCount, 'content');
253
+ }
254
+ validateAvailabilityLength(
255
+ subtree.childSubtreeAvailability,
256
+ childSubtreeElementCount,
257
+ 'child subtree'
258
+ );
259
+ const availableTileCount = countAvailableElements(subtree.tileAvailability, tileElementCount);
260
+ const availableContentCount = contentAvailabilityStreams[0]
261
+ ? countAvailableElements(contentAvailabilityStreams[0], tileElementCount)
262
+ : 0;
263
+ validateAttributeLengths(subtree.tileAttributes, availableTileCount, 'tile');
264
+ validateAttributeLengths(subtree.contentAttributes, availableContentCount, 'content');
265
+ if (subtree.tilePropertyRows && subtree.tilePropertyRows.length !== availableTileCount) {
266
+ throw new Error('3DTILES_subtree: tile property-table count does not match availability');
267
+ }
268
+ if (subtree.contentPropertyRows && subtree.contentPropertyRows.length !== availableContentCount) {
269
+ throw new Error('3DTILES_subtree: content property-table count does not match availability');
270
+ }
271
+ }
272
+
273
+ /** Verifies that a bitstream contains every bit required by its implicit subtree. */
274
+ function validateAvailabilityLength(
275
+ availability: ImplicitAvailability,
276
+ elementCount: number,
277
+ label: string
278
+ ): void {
279
+ if (availability.constant === 0 || availability.constant === 1) {
280
+ return;
281
+ }
282
+ if (
283
+ !availability.explicitBitstream ||
284
+ availability.explicitBitstream.byteLength * 8 < elementCount
285
+ ) {
286
+ throw new Error(`3DTILES_subtree: ${label} availability bitstream is too short`);
287
+ }
288
+ }
289
+
290
+ /** Counts available elements within the meaningful, unpadded part of an availability stream. */
291
+ function countAvailableElements(availability: ImplicitAvailability, elementCount: number): number {
292
+ if (availability.constant === 1) {
293
+ return elementCount;
294
+ }
295
+ if (availability.constant === 0) {
296
+ return 0;
297
+ }
298
+ let availableCount = 0;
299
+ for (let index = 0; index < elementCount; index++) {
300
+ if (getAvailabilityValue(availability, index)) {
301
+ availableCount++;
302
+ }
303
+ }
304
+ return availableCount;
305
+ }
306
+
307
+ /** Validates standard subtree attribute accessor counts after availability expansion. */
308
+ function validateAttributeLengths(
309
+ attributes: Record<string, ArrayLike<number>> | undefined,
310
+ availableCount: number,
311
+ label: string
312
+ ): void {
313
+ const componentCounts: Record<string, number> = {
314
+ TILE_BOUNDING_BOX: 16,
315
+ TILE_BOUNDING_SPHERE: 4,
316
+ TILE_GEOMETRIC_ERROR: 1,
317
+ TILE_REFINE: 1,
318
+ TILE_TRANSFORM: 16,
319
+ CONTENT_BOUNDING_BOX: 16,
320
+ CONTENT_BOUNDING_SPHERE: 4
321
+ };
322
+ for (const [semantic, values] of Object.entries(attributes || {})) {
323
+ const componentCount = componentCounts[semantic];
324
+ if (componentCount && values.length !== availableCount * componentCount) {
325
+ throw new Error(
326
+ `3DTILES_subtree: ${label} attribute ${semantic} count does not match availability`
327
+ );
328
+ }
329
+ }
330
+ }
331
+
332
+ /**
333
+ * Replaces implicit coordinate placeholders in a content or subtree URL.
334
+ *
335
+ * @param templateUrl - URL containing level/x/y/z placeholders.
336
+ * @param coordinates - Global coordinates to insert.
337
+ * @returns URL with all coordinate placeholders replaced.
338
+ */
339
+ export function replaceImplicitUrlTemplate(
340
+ templateUrl: string,
341
+ coordinates: ImplicitTileCoordinates,
342
+ properties: Record<string, unknown> = {}
343
+ ): string {
344
+ const values: Record<string, unknown> = {
345
+ level: coordinates.level,
346
+ x: coordinates.x,
347
+ y: coordinates.y,
348
+ z: coordinates.z,
349
+ ...properties
350
+ };
351
+ return templateUrl.replace(/{([^{}]+)}/g, (match, propertyName: string) => {
352
+ const value = Object.hasOwn(values, propertyName)
353
+ ? values[propertyName]
354
+ : values[propertyName.toLowerCase()];
355
+ return value === undefined ? match : String(value);
356
+ });
357
+ }
358
+
359
+ /**
360
+ * Builds one available tile and recursively materializes descendants in the same subtree.
361
+ *
362
+ * @param subtree - Parsed availability declarations.
363
+ * @param reference - Current subtree reference.
364
+ * @param localCoordinates - Level and Morton index relative to the current subtree.
365
+ * @param globalCoordinates - Coordinates relative to the complete implicit hierarchy.
366
+ * @param counters - Mutable diagnostic counters owned by the public materializer call.
367
+ * @returns Header for an available tile, or `null` for a sparse unavailable position.
368
+ */
369
+ function materializeAvailableTile(
370
+ subtree: ParsedImplicitSubtree,
371
+ reference: ImplicitSubtreeReference,
372
+ localCoordinates: {localLevel: number; mortonIndex: number},
373
+ globalCoordinates: ImplicitTileCoordinates,
374
+ counters: {tileCount: number; childSubtreeCount: number}
375
+ ): ImplicitTileHeader | null {
376
+ const {descriptor} = reference;
377
+ const childCount = getImplicitChildCount(descriptor.subdivisionScheme);
378
+ const availabilityIndex =
379
+ getLevelOffset(childCount, localCoordinates.localLevel) + localCoordinates.mortonIndex;
380
+
381
+ if (!getAvailabilityValue(subtree.tileAvailability, availabilityIndex)) {
382
+ return null;
383
+ }
384
+
385
+ counters.tileCount++;
386
+ const contentAvailability = getContentAvailability(
387
+ subtree.contentAvailability,
388
+ availabilityIndex
389
+ );
390
+ const children: ImplicitTileHeader[] = [];
391
+
392
+ if (globalCoordinates.level < descriptor.maximumLevel) {
393
+ if (localCoordinates.localLevel + 1 < descriptor.subtreeLevels) {
394
+ for (let childIndex = 0; childIndex < childCount; childIndex++) {
395
+ const childCoordinates = getChildCoordinates(
396
+ globalCoordinates,
397
+ childIndex,
398
+ descriptor.subdivisionScheme
399
+ );
400
+ const childHeader = materializeAvailableTile(
401
+ subtree,
402
+ reference,
403
+ {
404
+ localLevel: localCoordinates.localLevel + 1,
405
+ mortonIndex: localCoordinates.mortonIndex * childCount + childIndex
406
+ },
407
+ childCoordinates,
408
+ counters
409
+ );
410
+ if (childHeader) {
411
+ children.push(childHeader);
412
+ }
413
+ }
414
+ } else {
415
+ for (let childIndex = 0; childIndex < childCount; childIndex++) {
416
+ const childSubtreeIndex = localCoordinates.mortonIndex * childCount + childIndex;
417
+ if (getAvailabilityValue(subtree.childSubtreeAvailability, childSubtreeIndex)) {
418
+ const childCoordinates = getChildCoordinates(
419
+ globalCoordinates,
420
+ childIndex,
421
+ descriptor.subdivisionScheme
422
+ );
423
+ children.push(createLazyImplicitTileHeader(subtree, descriptor, childCoordinates));
424
+ counters.childSubtreeCount++;
425
+ }
426
+ }
427
+ }
428
+ }
429
+
430
+ return formatImplicitTileHeader(
431
+ subtree,
432
+ descriptor,
433
+ reference,
434
+ globalCoordinates,
435
+ availabilityIndex,
436
+ contentAvailability,
437
+ children
438
+ );
439
+ }
440
+
441
+ /**
442
+ * Creates a contentless placeholder for an available child subtree.
443
+ *
444
+ * @param descriptor - Shared implicit hierarchy description.
445
+ * @param coordinates - Global coordinates of the child subtree root.
446
+ * @returns Header that participates in culling and SSE before its subtree is requested.
447
+ */
448
+ function createLazyImplicitTileHeader(
449
+ subtree: ParsedImplicitSubtree,
450
+ descriptor: ImplicitTilingDescriptor,
451
+ coordinates: ImplicitTileCoordinates
452
+ ): ImplicitTileHeader {
453
+ const resourceFiles = mergeImplicitPackageFiles(descriptor.resourceFiles, subtree.resourceFiles);
454
+ const childDescriptor = resourceFiles ? {...descriptor, resourceFiles} : descriptor;
455
+ const reference = createImplicitSubtreeReference(childDescriptor, coordinates);
456
+ return {
457
+ id: getImplicitTileId(reference, coordinates),
458
+ children: [],
459
+ implicitSubtree: reference,
460
+ boundingVolume: calculateImplicitBoundingVolume(
461
+ descriptor.rootBoundingVolume,
462
+ coordinates,
463
+ descriptor.subdivisionScheme
464
+ ),
465
+ geometricError: descriptor.rootLodMetricValue / 2 ** coordinates.level,
466
+ lodMetricType: descriptor.lodMetricType,
467
+ lodMetricValue: descriptor.rootLodMetricValue / 2 ** coordinates.level,
468
+ refine: descriptor.refine,
469
+ type: TILE_TYPE.EMPTY,
470
+ implicitMetadata: getImplicitSubtreeMetadata(subtree),
471
+ _scaleGeometricError: descriptor.scaleGeometricError !== false
472
+ };
473
+ }
474
+
475
+ type ImplicitAttributeOverrides = {
476
+ boundingVolume?: Record<string, any>;
477
+ geometricError?: number;
478
+ refine?: TILE_REFINEMENT;
479
+ transform?: number[];
480
+ };
481
+
482
+ /** Decodes supported standard subtree attributes for one tightly packed available row. */
483
+ function getSubtreeAttributeOverrides(
484
+ attributes: Record<string, ArrayLike<number>> | undefined,
485
+ rowIndex: number,
486
+ scope: 'tile' | 'content'
487
+ ): ImplicitAttributeOverrides {
488
+ if (!attributes || rowIndex < 0) {
489
+ return {};
490
+ }
491
+ const box = getAttributeComponents(
492
+ attributes[scope === 'tile' ? 'TILE_BOUNDING_BOX' : 'CONTENT_BOUNDING_BOX'],
493
+ rowIndex,
494
+ 16
495
+ );
496
+ const sphere = getAttributeComponents(
497
+ attributes[scope === 'tile' ? 'TILE_BOUNDING_SPHERE' : 'CONTENT_BOUNDING_SPHERE'],
498
+ rowIndex,
499
+ 4
500
+ );
501
+ const boundingVolume = box
502
+ ? {
503
+ box: [
504
+ box[12],
505
+ box[13],
506
+ box[14],
507
+ box[0] / 2,
508
+ box[1] / 2,
509
+ box[2] / 2,
510
+ box[4] / 2,
511
+ box[5] / 2,
512
+ box[6] / 2,
513
+ box[8] / 2,
514
+ box[9] / 2,
515
+ box[10] / 2
516
+ ]
517
+ }
518
+ : sphere
519
+ ? {sphere}
520
+ : undefined;
521
+ if (scope === 'content') {
522
+ return {boundingVolume};
523
+ }
524
+ const geometricError = getAttributeComponents(attributes.TILE_GEOMETRIC_ERROR, rowIndex, 1)?.[0];
525
+ if (geometricError !== undefined && (!Number.isFinite(geometricError) || geometricError < 0)) {
526
+ throw new Error('3DTILES_subtree: TILE_GEOMETRIC_ERROR must be nonnegative');
527
+ }
528
+ const refineValue = getAttributeComponents(attributes.TILE_REFINE, rowIndex, 1)?.[0];
529
+ if (refineValue !== undefined && refineValue !== 0 && refineValue !== 1) {
530
+ throw new Error('3DTILES_subtree: TILE_REFINE must be 0 or 1');
531
+ }
532
+ return {
533
+ boundingVolume,
534
+ geometricError,
535
+ refine:
536
+ refineValue === undefined
537
+ ? undefined
538
+ : refineValue === 0
539
+ ? TILE_REFINEMENT.ADD
540
+ : TILE_REFINEMENT.REPLACE,
541
+ transform: getAttributeComponents(attributes.TILE_TRANSFORM, rowIndex, 16)
542
+ };
543
+ }
544
+
545
+ /** Copies one logical accessor element and validates its available-row count. */
546
+ function getAttributeComponents(
547
+ values: ArrayLike<number> | undefined,
548
+ rowIndex: number,
549
+ componentCount: number
550
+ ): number[] | undefined {
551
+ if (!values) {
552
+ return undefined;
553
+ }
554
+ const offset = rowIndex * componentCount;
555
+ if (offset + componentCount > values.length) {
556
+ throw new Error('3DTILES_subtree: attribute accessor is shorter than availability');
557
+ }
558
+ return Array.from({length: componentCount}, (_unused, componentIndex) =>
559
+ Number(values[offset + componentIndex])
560
+ );
561
+ }
562
+
563
+ /** Returns the tightly packed property/attribute row index for an available element. */
564
+ function getAvailabilityRank(availability: ImplicitAvailability, index: number): number {
565
+ if (availability.constant === 1) {
566
+ return index;
567
+ }
568
+ if (availability.constant === 0) {
569
+ return 0;
570
+ }
571
+ let ranks = AVAILABILITY_RANK_CACHE.get(availability);
572
+ if (!ranks) {
573
+ const bitCount = (availability.explicitBitstream?.byteLength || 0) * 8;
574
+ ranks = new Uint32Array(bitCount + 1);
575
+ for (let candidateIndex = 0; candidateIndex < bitCount; candidateIndex++) {
576
+ ranks[candidateIndex + 1] =
577
+ ranks[candidateIndex] + Number(getAvailabilityValue(availability, candidateIndex));
578
+ }
579
+ AVAILABILITY_RANK_CACHE.set(availability, ranks);
580
+ }
581
+ return ranks[index];
582
+ }
583
+
584
+ /** Locates a concrete generated URI in a retained glTF package. */
585
+ function findImplicitPackageResource(
586
+ files: ImplicitPackageFile[] | undefined,
587
+ resourceUrl: string
588
+ ): ImplicitPackageResource | undefined {
589
+ if (!files?.length) {
590
+ return undefined;
591
+ }
592
+ const urlWithoutQuery = resourceUrl.split('?')[0];
593
+ const packageRelativePath = /^gltf-package:\/\/[^/]+\/(.*)$/.exec(urlWithoutQuery)?.[1];
594
+ const exactFileIndex = files.findIndex(file => {
595
+ const reference = file.originalUri || file.name;
596
+ return (
597
+ file.uri === urlWithoutQuery ||
598
+ Boolean(reference && (urlWithoutQuery === reference || packageRelativePath === reference))
599
+ );
600
+ });
601
+ if (exactFileIndex >= 0) {
602
+ return {fileIndex: exactFileIndex, files};
603
+ }
604
+ const fileIndex = files.findIndex(file => {
605
+ const reference = file.originalUri || file.name;
606
+ return Boolean(
607
+ reference &&
608
+ (urlWithoutQuery.endsWith(`/${reference}`) || urlWithoutQuery.endsWith(`:${reference}`))
609
+ );
610
+ });
611
+ return fileIndex < 0 ? undefined : {fileIndex, files};
612
+ }
613
+
614
+ /** Layers files declared by a child package over inherited parent-package files. */
615
+ function mergeImplicitPackageFiles(
616
+ parentFiles: ImplicitPackageFile[] | undefined,
617
+ childFiles: ImplicitPackageFile[] | undefined
618
+ ): ImplicitPackageFile[] | undefined {
619
+ if (!childFiles?.length) {
620
+ return parentFiles;
621
+ }
622
+ if (!parentFiles?.length) {
623
+ return childFiles;
624
+ }
625
+ const childReferences = new Set(
626
+ childFiles.flatMap(file => [file.uri, file.originalUri, file.name].filter(Boolean) as string[])
627
+ );
628
+ return [
629
+ ...childFiles,
630
+ ...parentFiles.filter(
631
+ file =>
632
+ ![file.uri, file.originalUri, file.name].some(
633
+ reference => reference && childReferences.has(reference)
634
+ )
635
+ )
636
+ ];
637
+ }
638
+
639
+ /**
640
+ * Formats an available implicit tile as a runtime header.
641
+ *
642
+ * @param descriptor - Shared implicit hierarchy description.
643
+ * @param reference - Current subtree reference used to build stable IDs.
644
+ * @param coordinates - Global tile coordinates.
645
+ * @param contentAvailability - Availability for each content stream in source order.
646
+ * @param children - Materialized or lazy child headers.
647
+ * @returns Runtime tile header.
648
+ */
649
+ function formatImplicitTileHeader(
650
+ subtree: ParsedImplicitSubtree,
651
+ descriptor: ImplicitTilingDescriptor,
652
+ reference: ImplicitSubtreeReference,
653
+ coordinates: ImplicitTileCoordinates,
654
+ availabilityIndex: number,
655
+ contentAvailability: boolean[],
656
+ children: ImplicitTileHeader[]
657
+ ): ImplicitTileHeader {
658
+ const tilePropertyIndex = getAvailabilityRank(subtree.tileAvailability, availabilityIndex);
659
+ const tileProperties = subtree.tilePropertyRows?.[tilePropertyIndex];
660
+ const primaryContentAvailability = Array.isArray(subtree.contentAvailability)
661
+ ? subtree.contentAvailability[0]
662
+ : subtree.contentAvailability;
663
+ const contentPropertyIndex =
664
+ primaryContentAvailability &&
665
+ getAvailabilityValue(primaryContentAvailability, availabilityIndex)
666
+ ? getAvailabilityRank(primaryContentAvailability, availabilityIndex)
667
+ : -1;
668
+ const contentProperties = subtree.contentPropertyRows?.[contentPropertyIndex];
669
+ const tileTemplateProperties =
670
+ subtree.tileTemplatePropertyRows?.[tilePropertyIndex] || tileProperties;
671
+ const contentTemplateProperties =
672
+ subtree.contentTemplatePropertyRows?.[contentPropertyIndex] || contentProperties;
673
+ const templateProperties = {...tileTemplateProperties, ...contentTemplateProperties};
674
+ const tileAttributes = getSubtreeAttributeOverrides(
675
+ subtree.tileAttributes,
676
+ tilePropertyIndex,
677
+ 'tile'
678
+ );
679
+ const contentAttributes = getSubtreeAttributeOverrides(
680
+ subtree.contentAttributes,
681
+ contentPropertyIndex,
682
+ 'content'
683
+ );
684
+ const contentUrlTemplates = descriptor.contentUrlTemplates?.length
685
+ ? descriptor.contentUrlTemplates
686
+ : [descriptor.contentUrlTemplate];
687
+ const contentEntries = contentUrlTemplates
688
+ .map((templateUrl, contentIndex) => {
689
+ if (!contentAvailability[contentIndex] || !templateUrl) {
690
+ return null;
691
+ }
692
+ const contentUrl = replaceImplicitUrlTemplate(templateUrl, coordinates, templateProperties);
693
+ if (/{[^{}]+}/.test(contentUrl)) {
694
+ throw new Error(`Implicit content URI has unresolved template values: ${contentUrl}`);
695
+ }
696
+ return {
697
+ contentIndex,
698
+ contentUrl
699
+ };
700
+ })
701
+ .filter((entry): entry is {contentIndex: number; contentUrl: string} => Boolean(entry));
702
+ const availableContentUrls = contentEntries.map(entry => entry.contentUrl);
703
+ const resourceFiles = mergeImplicitPackageFiles(descriptor.resourceFiles, subtree.resourceFiles);
704
+ const content = contentEntries.map(({contentIndex, contentUrl}) => {
705
+ const packageResource = findImplicitPackageResource(resourceFiles, contentUrl);
706
+ return {
707
+ ...(descriptor.contentHeaders?.[contentIndex] ||
708
+ (contentIndex === 0 ? descriptor.contentHeader : {}) ||
709
+ {}),
710
+ ...contentAttributes,
711
+ uri: contentUrl,
712
+ ...(contentProperties ? {metadata: {properties: contentProperties}} : {}),
713
+ ...(packageResource ? {_resource: packageResource} : {})
714
+ };
715
+ });
716
+ const contentUrl = availableContentUrls[0];
717
+ const computedLodMetricValue = descriptor.rootLodMetricValue / 2 ** coordinates.level;
718
+ const lodMetricValue = tileAttributes.geometricError ?? computedLodMetricValue;
719
+ const computedBoundingVolume = calculateImplicitBoundingVolume(
720
+ descriptor.rootBoundingVolume,
721
+ coordinates,
722
+ descriptor.subdivisionScheme
723
+ );
724
+
725
+ return {
726
+ id: getImplicitTileId(reference, coordinates),
727
+ children,
728
+ contentUrl,
729
+ content: content.length > 1 ? content : content[0],
730
+ contents: descriptor.useCanonicalContents && content.length ? content : undefined,
731
+ contentUrls: availableContentUrls,
732
+ refine: tileAttributes.refine || descriptor.refine,
733
+ type: getImplicitTileType(contentUrl),
734
+ lodMetricType: descriptor.lodMetricType,
735
+ lodMetricValue,
736
+ geometricError: lodMetricValue,
737
+ boundingVolume: tileAttributes.boundingVolume || computedBoundingVolume,
738
+ transform: tileAttributes.transform,
739
+ transformMatrix: tileAttributes.transform,
740
+ metadata: tileProperties ? {properties: tileProperties} : undefined,
741
+ implicitMetadata: getImplicitSubtreeMetadata(subtree, tileProperties, contentProperties),
742
+ _scaleGeometricError: descriptor.scaleGeometricError !== false
743
+ };
744
+ }
745
+
746
+ /**
747
+ * Copies subtree metadata references without interpreting property-table schemas.
748
+ *
749
+ * Keeping the references on generated headers makes metadata available to applications while
750
+ * avoiding a false promise that the runtime has already decoded classes, enums, or values.
751
+ *
752
+ * @param subtree - Parsed subtree containing optional metadata declarations.
753
+ * @returns Metadata references, or `undefined` when the subtree declares none.
754
+ */
755
+ function getImplicitSubtreeMetadata(
756
+ subtree: ParsedImplicitSubtree,
757
+ tileProperties?: Record<string, unknown>,
758
+ contentProperties?: Record<string, unknown>
759
+ ): ImplicitSubtreeMetadata | undefined {
760
+ if (
761
+ !subtree.propertyTables &&
762
+ subtree.tileMetadata === undefined &&
763
+ !subtree.contentMetadata &&
764
+ subtree.subtreeMetadata === undefined &&
765
+ !tileProperties &&
766
+ !contentProperties
767
+ ) {
768
+ return undefined;
769
+ }
770
+ return {
771
+ propertyTables: subtree.propertyTables,
772
+ tileMetadata: subtree.tileMetadata,
773
+ contentMetadata: subtree.contentMetadata,
774
+ subtreeMetadata: subtree.subtreeMetadata,
775
+ tileProperties,
776
+ contentProperties
777
+ };
778
+ }
779
+
780
+ /**
781
+ * Returns a stable identity for contentless and content-bearing implicit tiles.
782
+ *
783
+ * @param reference - Owning subtree reference.
784
+ * @param coordinates - Global tile coordinates.
785
+ * @returns Stable source identity independent of content availability.
786
+ */
787
+ function getImplicitTileId(
788
+ reference: ImplicitSubtreeReference,
789
+ coordinates: ImplicitTileCoordinates
790
+ ): string {
791
+ return `${reference.subtreeUrl}#implicit=${coordinates.level}/${coordinates.x}/${coordinates.y}/${coordinates.z}`;
792
+ }
793
+
794
+ /**
795
+ * Reads an availability bit without assuming 32-bit coordinate or Morton arithmetic.
796
+ *
797
+ * @param availability - Constant or expanded bitstream availability.
798
+ * @param index - Zero-based availability index.
799
+ * @returns Whether the addressed tile, content, or child subtree exists.
800
+ */
801
+ function getAvailabilityValue(availability: ImplicitAvailability, index: number): boolean {
802
+ if (typeof availability?.constant === 'number') {
803
+ return Boolean(availability.constant);
804
+ }
805
+ const bitstream = availability?.explicitBitstream;
806
+ if (!bitstream) {
807
+ return false;
808
+ }
809
+ const byteIndex = Math.floor(index / 8);
810
+ const bitIndex = index % 8;
811
+ return Boolean((bitstream[byteIndex] >> bitIndex) & 1);
812
+ }
813
+
814
+ /**
815
+ * Reads every content availability stream for one tile in descriptor/source order.
816
+ *
817
+ * @param availability - Single or multiple-content availability declaration.
818
+ * @returns Availability flags aligned with the content URL templates.
819
+ */
820
+ function getContentAvailability(
821
+ availability: ImplicitAvailability | ImplicitAvailability[] | undefined,
822
+ index: number
823
+ ): boolean[] {
824
+ const streams = Array.isArray(availability) ? availability : [availability];
825
+ return streams.map(stream => getAvailabilityValue(stream || {constant: 0}, index));
826
+ }
827
+
828
+ /**
829
+ * Calculates the breadth-first availability offset for a local subtree level.
830
+ *
831
+ * @param childCount - Branching factor of the subdivision scheme.
832
+ * @param localLevel - Level relative to the subtree root.
833
+ * @returns First availability index for the requested level.
834
+ */
835
+ function getLevelOffset(childCount: number, localLevel: number): number {
836
+ return (childCount ** localLevel - 1) / (childCount - 1);
837
+ }
838
+
839
+ /**
840
+ * Calculates one child's global implicit coordinates.
841
+ *
842
+ * @param parent - Global parent coordinates.
843
+ * @param childIndex - Morton-ordered child index.
844
+ * @param subdivisionScheme - Quadtree or octree subdivision.
845
+ * @returns Global child coordinates.
846
+ */
847
+ function getChildCoordinates(
848
+ parent: ImplicitTileCoordinates,
849
+ childIndex: number,
850
+ subdivisionScheme: 'QUADTREE' | 'OCTREE'
851
+ ): ImplicitTileCoordinates {
852
+ return {
853
+ level: parent.level + 1,
854
+ x: parent.x * 2 + (childIndex & 1),
855
+ y: parent.y * 2 + ((childIndex >> 1) & 1),
856
+ z: subdivisionScheme === 'OCTREE' ? parent.z * 2 + ((childIndex >> 2) & 1) : 0
857
+ };
858
+ }
859
+
860
+ /**
861
+ * Returns the branching factor for a supported implicit subdivision scheme.
862
+ *
863
+ * @param subdivisionScheme - Quadtree or octree subdivision.
864
+ * @returns Four for quadtrees or eight for octrees.
865
+ */
866
+ function getImplicitChildCount(subdivisionScheme: 'QUADTREE' | 'OCTREE'): number {
867
+ return subdivisionScheme === 'OCTREE' ? OCTREE_CHILD_COUNT : QUADTREE_CHILD_COUNT;
868
+ }
869
+
870
+ /**
871
+ * Calculates the bounding volume for global implicit coordinates.
872
+ *
873
+ * Region volumes are divided in longitude/latitude and, for octrees, height. Oriented boxes are
874
+ * divided along their half-axis vectors. S2 roots use math.gl's Hilbert-aware descendant indexing
875
+ * to produce the exact child token and a corresponding geospatial oriented box.
876
+ *
877
+ * @param rootBoundingVolume - Bounding volume of the complete implicit hierarchy.
878
+ * @param coordinates - Global tile coordinates.
879
+ * @param subdivisionScheme - Quadtree or octree subdivision.
880
+ * @returns Bounding volume for the addressed tile.
881
+ */
882
+ function calculateImplicitBoundingVolume(
883
+ rootBoundingVolume: Record<string, any>,
884
+ coordinates: ImplicitTileCoordinates,
885
+ subdivisionScheme: 'QUADTREE' | 'OCTREE'
886
+ ): Record<string, any> {
887
+ if (rootBoundingVolume.region) {
888
+ const [west, south, east, north, minimumHeight, maximumHeight] = rootBoundingVolume.region;
889
+ const divisionCount = 2 ** coordinates.level;
890
+ const longitudeSpan = east >= west ? east - west : east + 2 * Math.PI - west;
891
+ const longitudeSize = longitudeSpan / divisionCount;
892
+ const latitudeSize = (north - south) / divisionCount;
893
+ const childWest = normalizeRegionLongitude(west + longitudeSize * coordinates.x);
894
+ const childEast = normalizeRegionLongitude(west + longitudeSize * (coordinates.x + 1));
895
+ const childSouth = south + latitudeSize * coordinates.y;
896
+ let childMinimumHeight = minimumHeight;
897
+ let childMaximumHeight = maximumHeight;
898
+ if (subdivisionScheme === 'OCTREE') {
899
+ const heightSize = (maximumHeight - minimumHeight) / divisionCount;
900
+ childMinimumHeight = minimumHeight + heightSize * coordinates.z;
901
+ childMaximumHeight = childMinimumHeight + heightSize;
902
+ }
903
+ return {
904
+ region: [
905
+ childWest,
906
+ childSouth,
907
+ childEast,
908
+ childSouth + latitudeSize,
909
+ childMinimumHeight,
910
+ childMaximumHeight
911
+ ]
912
+ };
913
+ }
914
+
915
+ if (rootBoundingVolume.box) {
916
+ if (rootBoundingVolume.s2VolumeInfo) {
917
+ return calculateS2ImplicitBoundingVolume(rootBoundingVolume, coordinates, subdivisionScheme);
918
+ }
919
+ if (rootBoundingVolume.extensions?.['3DTILES_shape_cylinder_region']) {
920
+ return {
921
+ ...rootBoundingVolume,
922
+ box: [...rootBoundingVolume.box]
923
+ };
924
+ }
925
+ const box = rootBoundingVolume.box;
926
+ const divisionCount = 2 ** coordinates.level;
927
+ const xOffset = -1 + (2 * coordinates.x + 1) / divisionCount;
928
+ const yOffset = -1 + (2 * coordinates.y + 1) / divisionCount;
929
+ const zOffset = -1 + (2 * coordinates.z + 1) / divisionCount;
930
+ const divideHeight = subdivisionScheme === 'OCTREE';
931
+ const center = [
932
+ box[0] + box[3] * xOffset + box[6] * yOffset + (divideHeight ? box[9] * zOffset : 0),
933
+ box[1] + box[4] * xOffset + box[7] * yOffset + (divideHeight ? box[10] * zOffset : 0),
934
+ box[2] + box[5] * xOffset + box[8] * yOffset + (divideHeight ? box[11] * zOffset : 0)
935
+ ];
936
+ return {
937
+ box: [
938
+ ...center,
939
+ box[3] / divisionCount,
940
+ box[4] / divisionCount,
941
+ box[5] / divisionCount,
942
+ box[6] / divisionCount,
943
+ box[7] / divisionCount,
944
+ box[8] / divisionCount,
945
+ divideHeight ? box[9] / divisionCount : box[9],
946
+ divideHeight ? box[10] / divisionCount : box[10],
947
+ divideHeight ? box[11] / divisionCount : box[11]
948
+ ]
949
+ };
950
+ }
951
+
952
+ throw new Error(
953
+ `Unsupported implicit 3D Tiles bounding volume: ${JSON.stringify(rootBoundingVolume)}`
954
+ );
955
+ }
956
+
957
+ /**
958
+ * Calculates an implicit descendant of an S2 root using math.gl's canonical Hilbert hierarchy.
959
+ *
960
+ * @param rootBoundingVolume - Normalized S2 root volume.
961
+ * @param coordinates - Global implicit tile coordinates.
962
+ * @param subdivisionScheme - Quadtree or octree subdivision.
963
+ * @returns Child volume with updated S2 metadata and oriented box.
964
+ */
965
+ function calculateS2ImplicitBoundingVolume(
966
+ rootBoundingVolume: Record<string, any>,
967
+ coordinates: ImplicitTileCoordinates,
968
+ subdivisionScheme: 'QUADTREE' | 'OCTREE'
969
+ ): Record<string, any> {
970
+ const rootS2VolumeInfo = rootBoundingVolume.s2VolumeInfo as S2VolumeInfo;
971
+ const rootIndex = getS2IndexFromToken(rootS2VolumeInfo.token);
972
+ const descendantIndex = getS2DescendantIndex(
973
+ rootIndex,
974
+ coordinates.level,
975
+ coordinates.x,
976
+ coordinates.y
977
+ );
978
+ const divisionCount = 2 ** coordinates.level;
979
+ const heightSize =
980
+ (rootS2VolumeInfo.maximumHeight - rootS2VolumeInfo.minimumHeight) / divisionCount;
981
+ const childS2VolumeInfo: S2VolumeInfo = {
982
+ token: getS2TokenFromIndex(descendantIndex),
983
+ minimumHeight:
984
+ rootS2VolumeInfo.minimumHeight +
985
+ (subdivisionScheme === 'OCTREE' ? heightSize * coordinates.z : 0),
986
+ maximumHeight:
987
+ rootS2VolumeInfo.minimumHeight +
988
+ (subdivisionScheme === 'OCTREE'
989
+ ? heightSize * (coordinates.z + 1)
990
+ : heightSize * divisionCount)
991
+ };
992
+ const extensions = rootBoundingVolume.extensions ? {...rootBoundingVolume.extensions} : undefined;
993
+ if (extensions?.['3DTILES_bounding_volume_S2']) {
994
+ extensions['3DTILES_bounding_volume_S2'] = childS2VolumeInfo;
995
+ }
996
+ if (extensions?.['3DTILES_shape_s2']) {
997
+ extensions['3DTILES_shape_s2'] = childS2VolumeInfo;
998
+ }
999
+ return {
1000
+ ...rootBoundingVolume,
1001
+ ...(extensions ? {extensions} : {}),
1002
+ s2VolumeInfo: childS2VolumeInfo,
1003
+ box: convertS2BoundingVolumeToOBB(childS2VolumeInfo)
1004
+ };
1005
+ }
1006
+
1007
+ /** Normalize a longitude while preserving the positive pi boundary for non-wrapped regions. */
1008
+ function normalizeRegionLongitude(longitude: number): number {
1009
+ let normalizedLongitude = longitude;
1010
+ while (normalizedLongitude > Math.PI) {
1011
+ normalizedLongitude -= 2 * Math.PI;
1012
+ }
1013
+ while (normalizedLongitude < -Math.PI) {
1014
+ normalizedLongitude += 2 * Math.PI;
1015
+ }
1016
+ return normalizedLongitude;
1017
+ }
1018
+
1019
+ /**
1020
+ * Detects the runtime tile category from an available content URL.
1021
+ *
1022
+ * @param contentUrl - Concrete content URL, if content is available.
1023
+ * @returns Runtime tile category.
1024
+ */
1025
+ function getImplicitTileType(contentUrl?: string): TILE_TYPE | string {
1026
+ if (!contentUrl) {
1027
+ return TILE_TYPE.EMPTY;
1028
+ }
1029
+ const extension = contentUrl.split('?')[0].split('.').pop()?.toLowerCase();
1030
+ switch (extension) {
1031
+ case 'pnts':
1032
+ return TILE_TYPE.POINTCLOUD;
1033
+ case 'i3dm':
1034
+ case 'b3dm':
1035
+ case 'glb':
1036
+ case 'gltf':
1037
+ return TILE_TYPE.SCENEGRAPH;
1038
+ default:
1039
+ return extension || TILE_TYPE.EMPTY;
1040
+ }
1041
+ }
1042
+
1043
+ /**
1044
+ * Validates descriptor invariants before allocating a runtime subtree.
1045
+ *
1046
+ * @param descriptor - Serializable implicit hierarchy description.
1047
+ * @throws If levels, scheme, or maximum-level semantics are invalid.
1048
+ */
1049
+ function validateImplicitDescriptor(descriptor: ImplicitTilingDescriptor): void {
1050
+ if (descriptor.subdivisionScheme !== 'QUADTREE' && descriptor.subdivisionScheme !== 'OCTREE') {
1051
+ throw new Error(`Unsupported implicit subdivision scheme: ${descriptor.subdivisionScheme}`);
1052
+ }
1053
+ if (!Number.isInteger(descriptor.subtreeLevels) || descriptor.subtreeLevels < 1) {
1054
+ throw new Error('Implicit subtreeLevels must be a positive integer');
1055
+ }
1056
+ if (!Number.isInteger(descriptor.maximumLevel) || descriptor.maximumLevel < 0) {
1057
+ throw new Error('Implicit availableLevels must describe at least the root level');
1058
+ }
1059
+ }