@pmndrs/vdb 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (576) hide show
  1. package/CHANGELOG.md +592 -2
  2. package/README.md +85 -37
  3. package/THIRD_PARTY_NOTICES.md +197 -29
  4. package/dist/core/compute/atlas.d.ts +10 -2
  5. package/dist/core/compute/atlas.js +6 -1
  6. package/dist/core/compute/atlas.js.map +1 -1
  7. package/dist/core/compute/index.d.ts +3 -3
  8. package/dist/core/compute/index.js +2 -2
  9. package/dist/core/compute/index.js.map +1 -1
  10. package/dist/core/compute/majorant-build.d.ts +45 -0
  11. package/dist/core/compute/majorant-build.js +193 -0
  12. package/dist/core/compute/majorant-build.js.map +1 -0
  13. package/dist/core/compute/multi-scale.d.ts +155 -0
  14. package/dist/core/compute/multi-scale.js +684 -0
  15. package/dist/core/compute/multi-scale.js.map +1 -0
  16. package/dist/core/compute/pyramid.d.ts +22 -7
  17. package/dist/core/compute/pyramid.js +22 -9
  18. package/dist/core/compute/pyramid.js.map +1 -1
  19. package/dist/core/compute/shared.d.ts +1 -0
  20. package/dist/core/compute/shared.js.map +1 -1
  21. package/dist/core/compute/stats.d.ts +10 -3
  22. package/dist/core/compute/stats.js +6 -1
  23. package/dist/core/compute/stats.js.map +1 -1
  24. package/dist/core/compute/value-transform.d.ts +8 -1
  25. package/dist/core/compute/value-transform.js +9 -3
  26. package/dist/core/compute/value-transform.js.map +1 -1
  27. package/dist/core/errors.d.ts +10 -0
  28. package/dist/core/errors.js +8 -0
  29. package/dist/core/errors.js.map +1 -1
  30. package/dist/core/frame/emission.d.ts +85 -0
  31. package/dist/core/frame/emission.js +422 -0
  32. package/dist/core/frame/emission.js.map +1 -0
  33. package/dist/core/frame/environment.d.ts +47 -0
  34. package/dist/core/frame/environment.js +178 -0
  35. package/dist/core/frame/environment.js.map +1 -0
  36. package/dist/core/frame/index.d.ts +39 -0
  37. package/dist/core/frame/index.js +27 -0
  38. package/dist/core/frame/index.js.map +1 -0
  39. package/dist/core/frame/kernels.d.ts +115 -0
  40. package/dist/core/frame/kernels.js +496 -0
  41. package/dist/core/frame/kernels.js.map +1 -0
  42. package/dist/core/frame/limits.d.ts +67 -0
  43. package/dist/core/frame/limits.js +109 -0
  44. package/dist/core/frame/limits.js.map +1 -0
  45. package/dist/core/frame/phase.d.ts +17 -0
  46. package/dist/core/frame/phase.js +53 -0
  47. package/dist/core/frame/phase.js.map +1 -0
  48. package/dist/core/frame/pipelines.d.ts +49 -0
  49. package/dist/core/frame/pipelines.js +147 -0
  50. package/dist/core/frame/pipelines.js.map +1 -0
  51. package/dist/core/frame/renderer.d.ts +122 -0
  52. package/dist/core/frame/renderer.js +1712 -0
  53. package/dist/core/frame/renderer.js.map +1 -0
  54. package/dist/core/frame/resources.d.ts +71 -0
  55. package/dist/core/frame/resources.js +100 -0
  56. package/dist/core/frame/resources.js.map +1 -0
  57. package/dist/core/frame/scattering.d.ts +97 -0
  58. package/dist/core/frame/scattering.js +332 -0
  59. package/dist/core/frame/scattering.js.map +1 -0
  60. package/dist/core/frame/settings.d.ts +243 -0
  61. package/dist/core/frame/settings.js +67 -0
  62. package/dist/core/frame/settings.js.map +1 -0
  63. package/dist/core/frame/shadows.d.ts +55 -0
  64. package/dist/core/frame/shadows.js +460 -0
  65. package/dist/core/frame/shadows.js.map +1 -0
  66. package/dist/core/frame/types.d.ts +415 -0
  67. package/dist/core/frame/types.js +49 -0
  68. package/dist/core/frame/types.js.map +1 -0
  69. package/dist/core/frame/uniforms.d.ts +105 -0
  70. package/dist/core/frame/uniforms.js +276 -0
  71. package/dist/core/frame/uniforms.js.map +1 -0
  72. package/dist/core/frame/wgsl/chunks.d.ts +4 -0
  73. package/dist/core/frame/wgsl/chunks.js +728 -0
  74. package/dist/core/frame/wgsl/chunks.js.map +1 -0
  75. package/dist/core/frame/wgsl/emission.d.ts +82 -0
  76. package/dist/core/frame/wgsl/emission.js +353 -0
  77. package/dist/core/frame/wgsl/emission.js.map +1 -0
  78. package/dist/core/frame/wgsl/environment.d.ts +42 -0
  79. package/dist/core/frame/wgsl/environment.js +711 -0
  80. package/dist/core/frame/wgsl/environment.js.map +1 -0
  81. package/dist/core/frame/wgsl/gather.d.ts +55 -0
  82. package/dist/core/frame/wgsl/gather.js +850 -0
  83. package/dist/core/frame/wgsl/gather.js.map +1 -0
  84. package/dist/core/frame/wgsl/integrate.d.ts +78 -0
  85. package/dist/core/frame/wgsl/integrate.js +213 -0
  86. package/dist/core/frame/wgsl/integrate.js.map +1 -0
  87. package/dist/core/frame/wgsl/lighting.d.ts +103 -0
  88. package/dist/core/frame/wgsl/lighting.js +1636 -0
  89. package/dist/core/frame/wgsl/lighting.js.map +1 -0
  90. package/dist/core/frame/wgsl/occluders.d.ts +20 -0
  91. package/dist/core/frame/wgsl/occluders.js +95 -0
  92. package/dist/core/frame/wgsl/occluders.js.map +1 -0
  93. package/dist/core/frame/wgsl/resolve.d.ts +19 -0
  94. package/dist/core/frame/wgsl/resolve.js +205 -0
  95. package/dist/core/frame/wgsl/resolve.js.map +1 -0
  96. package/dist/core/frame/wgsl/shadows.d.ts +35 -0
  97. package/dist/core/frame/wgsl/shadows.js +328 -0
  98. package/dist/core/frame/wgsl/shadows.js.map +1 -0
  99. package/dist/core/half.d.ts +11 -0
  100. package/dist/core/half.js +22 -0
  101. package/dist/core/half.js.map +1 -0
  102. package/dist/core/index.d.ts +25 -3
  103. package/dist/core/index.js +23 -4
  104. package/dist/core/index.js.map +1 -1
  105. package/dist/core/internal.d.ts +17 -1
  106. package/dist/core/internal.js +18 -1
  107. package/dist/core/internal.js.map +1 -1
  108. package/dist/core/pool/image-header.d.ts +13 -2
  109. package/dist/core/pool/image-header.js +24 -5
  110. package/dist/core/pool/image-header.js.map +1 -1
  111. package/dist/core/pool/layout.d.ts +35 -1
  112. package/dist/core/pool/layout.js +51 -2
  113. package/dist/core/pool/layout.js.map +1 -1
  114. package/dist/core/pool/limits.d.ts +1 -0
  115. package/dist/core/pool/limits.js +1 -0
  116. package/dist/core/pool/limits.js.map +1 -1
  117. package/dist/core/pool/pool.d.ts +120 -15
  118. package/dist/core/pool/pool.js +255 -24
  119. package/dist/core/pool/pool.js.map +1 -1
  120. package/dist/core/reference/index.d.ts +6 -0
  121. package/dist/core/reference/index.js +7 -0
  122. package/dist/core/reference/index.js.map +1 -0
  123. package/dist/core/reference/renderer.d.ts +105 -0
  124. package/dist/core/reference/renderer.js +474 -0
  125. package/dist/core/reference/renderer.js.map +1 -0
  126. package/dist/core/reference/wgsl.d.ts +65 -0
  127. package/dist/core/reference/wgsl.js +803 -0
  128. package/dist/core/reference/wgsl.js.map +1 -0
  129. package/dist/{three → core}/sequence.d.ts +32 -2
  130. package/dist/{three → core}/sequence.js +24 -2
  131. package/dist/core/sequence.js.map +1 -0
  132. package/dist/core/source/brick-cache.d.ts +165 -0
  133. package/dist/core/source/brick-cache.js +627 -0
  134. package/dist/core/source/brick-cache.js.map +1 -0
  135. package/dist/core/source/dense-fill.d.ts +53 -0
  136. package/dist/core/source/dense-fill.js +377 -0
  137. package/dist/core/source/dense-fill.js.map +1 -0
  138. package/dist/core/source/dense.d.ts +33 -2
  139. package/dist/core/source/dense.js +121 -24
  140. package/dist/core/source/dense.js.map +1 -1
  141. package/dist/core/source/sparse-wgsl.d.ts +92 -0
  142. package/dist/core/source/sparse-wgsl.js +481 -0
  143. package/dist/core/source/sparse-wgsl.js.map +1 -0
  144. package/dist/core/source/sparse.d.ts +192 -0
  145. package/dist/core/source/sparse.js +762 -0
  146. package/dist/core/source/sparse.js.map +1 -0
  147. package/dist/core/source/types.d.ts +59 -2
  148. package/dist/core/source/types.js.map +1 -1
  149. package/dist/core/surface/index.d.ts +10 -0
  150. package/dist/core/surface/index.js +9 -0
  151. package/dist/core/surface/index.js.map +1 -0
  152. package/dist/core/surface/level-set-pass.d.ts +140 -0
  153. package/dist/core/surface/level-set-pass.js +291 -0
  154. package/dist/core/surface/level-set-pass.js.map +1 -0
  155. package/dist/core/surface/level-set-wgsl.d.ts +46 -0
  156. package/dist/core/surface/level-set-wgsl.js +99 -0
  157. package/dist/core/surface/level-set-wgsl.js.map +1 -0
  158. package/dist/core/surface/points.d.ts +109 -0
  159. package/dist/core/surface/points.js +226 -0
  160. package/dist/core/surface/points.js.map +1 -0
  161. package/dist/core/types.d.ts +12 -3
  162. package/dist/core/types.js +6 -2
  163. package/dist/core/types.js.map +1 -1
  164. package/dist/core/wgsl/api.d.ts +29 -5
  165. package/dist/core/wgsl/api.js +110 -27
  166. package/dist/core/wgsl/api.js.map +1 -1
  167. package/dist/core/wgsl/generate.d.ts +8 -0
  168. package/dist/core/wgsl/generate.js +195 -22
  169. package/dist/core/wgsl/generate.js.map +1 -1
  170. package/dist/core/wgsl/lean.d.ts +50 -0
  171. package/dist/core/wgsl/lean.js +327 -0
  172. package/dist/core/wgsl/lean.js.map +1 -0
  173. package/dist/core/wgsl/level-set.d.ts +5 -1
  174. package/dist/core/wgsl/level-set.js +11 -5
  175. package/dist/core/wgsl/level-set.js.map +1 -1
  176. package/dist/core/wgsl/majorant.d.ts +62 -0
  177. package/dist/core/wgsl/majorant.js +381 -0
  178. package/dist/core/wgsl/majorant.js.map +1 -0
  179. package/dist/core/wgsl/pnanovdb.chunks.generated.d.ts +2 -2
  180. package/dist/core/wgsl/pnanovdb.chunks.generated.js +15 -12
  181. package/dist/core/wgsl/pnanovdb.chunks.generated.js.map +1 -1
  182. package/dist/core/wgsl/types.d.ts +97 -3
  183. package/dist/core/wgsl/types.js.map +1 -1
  184. package/dist/three/coarse-first.d.ts +53 -8
  185. package/dist/three/coarse-first.js +24 -6
  186. package/dist/three/coarse-first.js.map +1 -1
  187. package/dist/three/compute.d.ts +12 -2
  188. package/dist/three/compute.js +16 -6
  189. package/dist/three/compute.js.map +1 -1
  190. package/dist/three/device.d.ts +7 -0
  191. package/dist/three/device.js +3 -0
  192. package/dist/three/device.js.map +1 -1
  193. package/dist/three/frame/core-environment.d.ts +38 -0
  194. package/dist/three/frame/core-environment.js +154 -0
  195. package/dist/three/frame/core-environment.js.map +1 -0
  196. package/dist/three/frame/core-integrate-material.d.ts +34 -0
  197. package/dist/three/frame/core-integrate-material.js +162 -0
  198. package/dist/three/frame/core-integrate-material.js.map +1 -0
  199. package/dist/three/frame/core-occluders.d.ts +53 -0
  200. package/dist/three/frame/core-occluders.js +193 -0
  201. package/dist/three/frame/core-occluders.js.map +1 -0
  202. package/dist/three/frame/core-structs.d.ts +34 -0
  203. package/dist/three/frame/core-structs.js +61 -0
  204. package/dist/three/frame/core-structs.js.map +1 -0
  205. package/dist/three/frame/core-textures.d.ts +41 -0
  206. package/dist/three/frame/core-textures.js +114 -0
  207. package/dist/three/frame/core-textures.js.map +1 -0
  208. package/dist/three/frame/emission-light.d.ts +64 -114
  209. package/dist/three/frame/emission-light.js +188 -567
  210. package/dist/three/frame/emission-light.js.map +1 -1
  211. package/dist/three/frame/index.d.ts +11 -31
  212. package/dist/three/frame/index.js +9 -29
  213. package/dist/three/frame/index.js.map +1 -1
  214. package/dist/three/frame/medium.d.ts +12 -0
  215. package/dist/three/frame/medium.js.map +1 -1
  216. package/dist/three/frame/renderer.d.ts +58 -454
  217. package/dist/three/frame/renderer.js +409 -1427
  218. package/dist/three/frame/renderer.js.map +1 -1
  219. package/dist/three/frame/spectrum.d.ts +0 -14
  220. package/dist/three/frame/spectrum.js +2 -23
  221. package/dist/three/frame/spectrum.js.map +1 -1
  222. package/dist/three/frame/three-internals.d.ts +3 -8
  223. package/dist/three/frame/three-internals.js +13 -11
  224. package/dist/three/frame/three-internals.js.map +1 -1
  225. package/dist/three/frame/volume-shadows.d.ts +93 -107
  226. package/dist/three/frame/volume-shadows.js +278 -631
  227. package/dist/three/frame/volume-shadows.js.map +1 -1
  228. package/dist/three/frame/volume.d.ts +91 -33
  229. package/dist/three/frame/volume.js +63 -51
  230. package/dist/three/frame/volume.js.map +1 -1
  231. package/dist/three/grid-node.d.ts +34 -1
  232. package/dist/three/grid-node.js +36 -1
  233. package/dist/three/grid-node.js.map +1 -1
  234. package/dist/three/index.d.ts +20 -11
  235. package/dist/three/index.js +21 -12
  236. package/dist/three/index.js.map +1 -1
  237. package/dist/three/internal.d.ts +14 -8
  238. package/dist/three/internal.js +11 -7
  239. package/dist/three/internal.js.map +1 -1
  240. package/dist/three/level-set-material.d.ts +81 -8
  241. package/dist/three/level-set-material.js +162 -125
  242. package/dist/three/level-set-material.js.map +1 -1
  243. package/dist/three/level-set-physical-material.d.ts +154 -0
  244. package/dist/three/level-set-physical-material.js +253 -0
  245. package/dist/three/level-set-physical-material.js.map +1 -0
  246. package/dist/three/level-set-velocity.d.ts +94 -0
  247. package/dist/three/level-set-velocity.js +208 -0
  248. package/dist/three/level-set-velocity.js.map +1 -0
  249. package/dist/three/material.d.ts +141 -11
  250. package/dist/three/material.js +161 -18
  251. package/dist/three/material.js.map +1 -1
  252. package/dist/three/points.d.ts +60 -4
  253. package/dist/three/points.js +140 -9
  254. package/dist/three/points.js.map +1 -1
  255. package/dist/three/sparse-source.d.ts +49 -117
  256. package/dist/three/sparse-source.js +109 -580
  257. package/dist/three/sparse-source.js.map +1 -1
  258. package/dist/three/volume-source.d.ts +11 -156
  259. package/dist/three/volume-source.js +29 -272
  260. package/dist/three/volume-source.js.map +1 -1
  261. package/dist/tools/cpu/brick-atlas.d.ts +36 -0
  262. package/dist/tools/cpu/brick-atlas.js +129 -0
  263. package/dist/tools/cpu/brick-atlas.js.map +1 -0
  264. package/dist/tools/cpu/compute.d.ts +23 -4
  265. package/dist/tools/cpu/compute.js +35 -14
  266. package/dist/tools/cpu/compute.js.map +1 -1
  267. package/dist/tools/cpu/lean-reads.d.ts +81 -0
  268. package/dist/tools/cpu/lean-reads.js +262 -0
  269. package/dist/tools/cpu/lean-reads.js.map +1 -0
  270. package/dist/tools/cpu/level-set.d.ts +42 -3
  271. package/dist/tools/cpu/level-set.js +59 -7
  272. package/dist/tools/cpu/level-set.js.map +1 -1
  273. package/dist/tools/cpu/majorant.d.ts +265 -0
  274. package/dist/tools/cpu/majorant.js +541 -0
  275. package/dist/tools/cpu/majorant.js.map +1 -0
  276. package/dist/tools/cpu/march.d.ts +93 -2
  277. package/dist/tools/cpu/march.js +165 -14
  278. package/dist/tools/cpu/march.js.map +1 -1
  279. package/dist/tools/cpu/mesh-decimate.d.ts +124 -0
  280. package/dist/tools/cpu/mesh-decimate.js +1225 -0
  281. package/dist/tools/cpu/mesh-decimate.js.map +1 -0
  282. package/dist/tools/cpu/points.d.ts +29 -0
  283. package/dist/tools/cpu/points.js +38 -0
  284. package/dist/tools/cpu/points.js.map +1 -0
  285. package/dist/tools/cpu/read-value.d.ts +19 -6
  286. package/dist/tools/cpu/read-value.js +32 -17
  287. package/dist/tools/cpu/read-value.js.map +1 -1
  288. package/dist/tools/cpu/sample-trilinear.d.ts +17 -4
  289. package/dist/tools/cpu/sample-trilinear.js +19 -7
  290. package/dist/tools/cpu/sample-trilinear.js.map +1 -1
  291. package/dist/tools/cpu/stride-tables.d.ts +2 -2
  292. package/dist/tools/cpu/stride-tables.generated.d.ts +6 -5
  293. package/dist/tools/cpu/stride-tables.generated.js +7 -6
  294. package/dist/tools/cpu/stride-tables.generated.js.map +1 -1
  295. package/dist/tools/cpu/stride-tables.js +1 -1
  296. package/dist/tools/cpu/stride-tables.js.map +1 -1
  297. package/dist/tools/cpu/surface-nets.d.ts +90 -0
  298. package/dist/tools/cpu/surface-nets.js +597 -0
  299. package/dist/tools/cpu/surface-nets.js.map +1 -0
  300. package/dist/tools/errors.d.ts +9 -0
  301. package/dist/tools/errors.js +9 -0
  302. package/dist/tools/errors.js.map +1 -1
  303. package/dist/tools/frame/ambient.d.ts +98 -0
  304. package/dist/tools/frame/ambient.js +274 -0
  305. package/dist/tools/frame/ambient.js.map +1 -0
  306. package/dist/tools/frame/blackbody.d.ts +15 -0
  307. package/dist/tools/frame/blackbody.js +35 -0
  308. package/dist/tools/frame/blackbody.js.map +1 -1
  309. package/dist/tools/frame/brick-cache.d.ts +165 -0
  310. package/dist/tools/frame/brick-cache.js +326 -0
  311. package/dist/tools/frame/brick-cache.js.map +1 -0
  312. package/dist/tools/frame/emission.d.ts +9 -0
  313. package/dist/tools/frame/emission.js +9 -1
  314. package/dist/tools/frame/emission.js.map +1 -1
  315. package/dist/tools/frame/environment-gpu.d.ts +111 -0
  316. package/dist/tools/frame/environment-gpu.js +280 -0
  317. package/dist/tools/frame/environment-gpu.js.map +1 -0
  318. package/dist/tools/frame/environment.d.ts +3 -2
  319. package/dist/tools/frame/environment.js +8 -4
  320. package/dist/tools/frame/environment.js.map +1 -1
  321. package/dist/tools/frame/froxel.d.ts +58 -0
  322. package/dist/tools/frame/froxel.js +115 -0
  323. package/dist/tools/frame/froxel.js.map +1 -0
  324. package/dist/tools/frame/grid.d.ts +90 -0
  325. package/dist/tools/frame/grid.js +112 -13
  326. package/dist/tools/frame/grid.js.map +1 -1
  327. package/dist/tools/frame/index.d.ts +14 -0
  328. package/dist/tools/frame/index.js +45 -4
  329. package/dist/tools/frame/index.js.map +1 -1
  330. package/dist/tools/frame/medium.d.ts +16 -0
  331. package/dist/tools/frame/medium.js +3 -1
  332. package/dist/tools/frame/medium.js.map +1 -1
  333. package/dist/tools/frame/mie.d.ts +9 -0
  334. package/dist/tools/frame/mie.js +18 -0
  335. package/dist/tools/frame/mie.js.map +1 -1
  336. package/dist/tools/frame/mlp-nn.d.ts +36 -0
  337. package/dist/tools/frame/mlp-nn.js +159 -0
  338. package/dist/tools/frame/mlp-nn.js.map +1 -0
  339. package/dist/tools/frame/mlp-tza.d.ts +29 -0
  340. package/dist/tools/frame/mlp-tza.js +186 -0
  341. package/dist/tools/frame/mlp-tza.js.map +1 -0
  342. package/dist/tools/frame/mlp.d.ts +183 -0
  343. package/dist/tools/frame/mlp.js +434 -0
  344. package/dist/tools/frame/mlp.js.map +1 -0
  345. package/dist/tools/frame/ms-exit.d.ts +42 -0
  346. package/dist/tools/frame/ms-exit.js +90 -0
  347. package/dist/tools/frame/ms-exit.js.map +1 -0
  348. package/dist/tools/frame/multi-scale.d.ts +152 -0
  349. package/dist/tools/frame/multi-scale.js +302 -0
  350. package/dist/tools/frame/multi-scale.js.map +1 -0
  351. package/dist/tools/frame/neural-weights.d.ts +6 -5
  352. package/dist/tools/frame/neural-weights.js +234 -235
  353. package/dist/tools/frame/neural-weights.js.map +1 -1
  354. package/dist/tools/frame/neural.js +1 -1
  355. package/dist/tools/frame/neural.js.map +1 -1
  356. package/dist/tools/frame/occluders.d.ts +51 -0
  357. package/dist/tools/frame/occluders.js +72 -0
  358. package/dist/tools/frame/occluders.js.map +1 -0
  359. package/dist/tools/frame/pyramid-layout.d.ts +17 -0
  360. package/dist/tools/frame/pyramid-layout.js +47 -0
  361. package/dist/tools/frame/pyramid-layout.js.map +1 -1
  362. package/dist/tools/frame/shadows.d.ts +110 -0
  363. package/dist/tools/frame/shadows.js +286 -0
  364. package/dist/tools/frame/shadows.js.map +1 -1
  365. package/dist/tools/frame/sparse.d.ts +82 -0
  366. package/dist/tools/frame/sparse.js +242 -1
  367. package/dist/tools/frame/sparse.js.map +1 -1
  368. package/dist/tools/frame/spectral-stack.d.ts +159 -0
  369. package/dist/tools/frame/spectral-stack.js +391 -0
  370. package/dist/tools/frame/spectral-stack.js.map +1 -0
  371. package/dist/tools/frame/spectral.d.ts +21 -2
  372. package/dist/tools/frame/spectral.js +30 -8
  373. package/dist/tools/frame/spectral.js.map +1 -1
  374. package/dist/tools/frame/stack.d.ts +115 -0
  375. package/dist/tools/frame/stack.js +290 -0
  376. package/dist/tools/frame/stack.js.map +1 -0
  377. package/dist/tools/frame/view.d.ts +131 -0
  378. package/dist/tools/frame/view.js +337 -0
  379. package/dist/tools/frame/view.js.map +1 -0
  380. package/dist/tools/index.d.ts +66 -15
  381. package/dist/tools/index.js +55 -11
  382. package/dist/tools/index.js.map +1 -1
  383. package/dist/tools/internal.d.ts +58 -17
  384. package/dist/tools/internal.js +65 -11
  385. package/dist/tools/internal.js.map +1 -1
  386. package/dist/tools/nanovdb/build-from-vdb.d.ts +11 -2
  387. package/dist/tools/nanovdb/build-from-vdb.js +10 -2
  388. package/dist/tools/nanovdb/build-from-vdb.js.map +1 -1
  389. package/dist/tools/nanovdb/bytes.d.ts +17 -4
  390. package/dist/tools/nanovdb/bytes.js +46 -3
  391. package/dist/tools/nanovdb/bytes.js.map +1 -1
  392. package/dist/tools/nanovdb/coarse-first.d.ts +19 -3
  393. package/dist/tools/nanovdb/coarse-first.js +7 -1
  394. package/dist/tools/nanovdb/coarse-first.js.map +1 -1
  395. package/dist/tools/nanovdb/half.d.ts +22 -0
  396. package/dist/tools/nanovdb/half.js +73 -0
  397. package/dist/tools/nanovdb/half.js.map +1 -0
  398. package/dist/tools/nanovdb/index.d.ts +3 -1
  399. package/dist/tools/nanovdb/index.js +2 -1
  400. package/dist/tools/nanovdb/index.js.map +1 -1
  401. package/dist/tools/nanovdb/inspect.d.ts +10 -1
  402. package/dist/tools/nanovdb/inspect.js +8 -1
  403. package/dist/tools/nanovdb/inspect.js.map +1 -1
  404. package/dist/tools/nanovdb/leaf-codec.d.ts +35 -1
  405. package/dist/tools/nanovdb/leaf-codec.js +40 -4
  406. package/dist/tools/nanovdb/leaf-codec.js.map +1 -1
  407. package/dist/tools/nanovdb/quantize.d.ts +39 -10
  408. package/dist/tools/nanovdb/quantize.js +96 -44
  409. package/dist/tools/nanovdb/quantize.js.map +1 -1
  410. package/dist/tools/nanovdb/resample.d.ts +20 -3
  411. package/dist/tools/nanovdb/resample.js +20 -7
  412. package/dist/tools/nanovdb/resample.js.map +1 -1
  413. package/dist/tools/nanovdb/serialize.d.ts +34 -3
  414. package/dist/tools/nanovdb/serialize.js +51 -7
  415. package/dist/tools/nanovdb/serialize.js.map +1 -1
  416. package/dist/tools/nanovdb/stats.d.ts +34 -5
  417. package/dist/tools/nanovdb/stats.js +87 -11
  418. package/dist/tools/nanovdb/stats.js.map +1 -1
  419. package/dist/tools/nanovdb/transform.d.ts +4 -0
  420. package/dist/tools/nanovdb/transform.js +3 -0
  421. package/dist/tools/nanovdb/transform.js.map +1 -1
  422. package/dist/tools/nanovdb/tree.d.ts +14 -3
  423. package/dist/tools/nanovdb/tree.js +25 -13
  424. package/dist/tools/nanovdb/tree.js.map +1 -1
  425. package/dist/tools/nanovdb/write-nvdb.d.ts +47 -12
  426. package/dist/tools/nanovdb/write-nvdb.js +108 -28
  427. package/dist/tools/nanovdb/write-nvdb.js.map +1 -1
  428. package/dist/tools/nvdb-file.d.ts +77 -7
  429. package/dist/tools/nvdb-file.js +177 -20
  430. package/dist/tools/nvdb-file.js.map +1 -1
  431. package/dist/tools/nvdb-stream.d.ts +23 -7
  432. package/dist/tools/nvdb-stream.js +78 -34
  433. package/dist/tools/nvdb-stream.js.map +1 -1
  434. package/dist/tools/reference/analytic.d.ts +41 -0
  435. package/dist/tools/reference/analytic.js +95 -0
  436. package/dist/tools/reference/analytic.js.map +1 -0
  437. package/dist/tools/reference/environment.d.ts +62 -0
  438. package/dist/tools/reference/environment.js +154 -0
  439. package/dist/tools/reference/environment.js.map +1 -0
  440. package/dist/tools/reference/index.d.ts +12 -0
  441. package/dist/tools/reference/index.js +13 -0
  442. package/dist/tools/reference/index.js.map +1 -0
  443. package/dist/tools/reference/media.d.ts +14 -0
  444. package/dist/tools/reference/media.js +30 -0
  445. package/dist/tools/reference/media.js.map +1 -0
  446. package/dist/tools/reference/rng.d.ts +20 -0
  447. package/dist/tools/reference/rng.js +30 -0
  448. package/dist/tools/reference/rng.js.map +1 -0
  449. package/dist/tools/reference/scene.d.ts +312 -0
  450. package/dist/tools/reference/scene.js +169 -0
  451. package/dist/tools/reference/scene.js.map +1 -0
  452. package/dist/tools/reference/tables.d.ts +98 -0
  453. package/dist/tools/reference/tables.js +264 -0
  454. package/dist/tools/reference/tables.js.map +1 -0
  455. package/dist/tools/reference/trace.d.ts +160 -0
  456. package/dist/tools/reference/trace.js +497 -0
  457. package/dist/tools/reference/trace.js.map +1 -0
  458. package/dist/tools/vdb/blosc-write.d.ts +43 -0
  459. package/dist/tools/vdb/blosc-write.js +197 -0
  460. package/dist/tools/vdb/blosc-write.js.map +1 -0
  461. package/dist/tools/vdb/blosc.d.ts +35 -5
  462. package/dist/tools/vdb/blosc.js +82 -26
  463. package/dist/tools/vdb/blosc.js.map +1 -1
  464. package/dist/tools/vdb/blosclz.d.ts +33 -0
  465. package/dist/tools/vdb/blosclz.js +112 -0
  466. package/dist/tools/vdb/blosclz.js.map +1 -0
  467. package/dist/tools/vdb/byte-reader.d.ts +13 -3
  468. package/dist/tools/vdb/byte-reader.js +26 -4
  469. package/dist/tools/vdb/byte-reader.js.map +1 -1
  470. package/dist/tools/vdb/compression.d.ts +13 -5
  471. package/dist/tools/vdb/compression.js +13 -2
  472. package/dist/tools/vdb/compression.js.map +1 -1
  473. package/dist/tools/vdb/grid.js +8 -2
  474. package/dist/tools/vdb/grid.js.map +1 -1
  475. package/dist/tools/vdb/index.d.ts +2 -1
  476. package/dist/tools/vdb/index.js.map +1 -1
  477. package/dist/tools/vdb/lz4.d.ts +30 -0
  478. package/dist/tools/vdb/lz4.js +228 -0
  479. package/dist/tools/vdb/lz4.js.map +1 -0
  480. package/dist/tools/vdb/metadata.js +1 -1
  481. package/dist/tools/vdb/metadata.js.map +1 -1
  482. package/dist/tools/vdb/parse.d.ts +24 -2
  483. package/dist/tools/vdb/parse.js +102 -32
  484. package/dist/tools/vdb/parse.js.map +1 -1
  485. package/dist/tools/vdb/point-render-data.d.ts +49 -1
  486. package/dist/tools/vdb/point-render-data.js +91 -11
  487. package/dist/tools/vdb/point-render-data.js.map +1 -1
  488. package/dist/tools/vdb/points.d.ts +26 -0
  489. package/dist/tools/vdb/points.js +46 -5
  490. package/dist/tools/vdb/points.js.map +1 -1
  491. package/dist/tools/vdb/types.d.ts +59 -2
  492. package/dist/tools/vdb/types.js.map +1 -1
  493. package/dist/tools/wvdb/bake.d.ts +38 -9
  494. package/dist/tools/wvdb/bake.js +63 -13
  495. package/dist/tools/wvdb/bake.js.map +1 -1
  496. package/dist/tools/wvdb/dense.d.ts +80 -1
  497. package/dist/tools/wvdb/dense.js +67 -8
  498. package/dist/tools/wvdb/dense.js.map +1 -1
  499. package/dist/tools/wvdb/format.d.ts +59 -0
  500. package/dist/tools/wvdb/format.js +3 -0
  501. package/dist/tools/wvdb/format.js.map +1 -1
  502. package/dist/tools/wvdb/index.d.ts +1 -0
  503. package/dist/tools/wvdb/index.js +1 -0
  504. package/dist/tools/wvdb/index.js.map +1 -1
  505. package/dist/tools/wvdb/level-set.d.ts +10 -2
  506. package/dist/tools/wvdb/level-set.js.map +1 -1
  507. package/dist/tools/wvdb/nanovdb.d.ts +6 -1
  508. package/dist/tools/wvdb/nanovdb.js +5 -1
  509. package/dist/tools/wvdb/nanovdb.js.map +1 -1
  510. package/dist/tools/wvdb/partial.d.ts +74 -0
  511. package/dist/tools/wvdb/partial.js +233 -0
  512. package/dist/tools/wvdb/partial.js.map +1 -0
  513. package/dist/tools/wvdb/read.d.ts +77 -1
  514. package/dist/tools/wvdb/read.js +30 -7
  515. package/dist/tools/wvdb/read.js.map +1 -1
  516. package/dist/tools/wvdb/write.d.ts +23 -1
  517. package/dist/tools/wvdb/write.js +8 -1
  518. package/dist/tools/wvdb/write.js.map +1 -1
  519. package/package.json +2 -1
  520. package/vendor/VENDOR.md +21 -6
  521. package/vendor/pnanovdb.wgsl +52 -14
  522. package/dist/three/frame/blackbody.d.ts +0 -42
  523. package/dist/three/frame/blackbody.js +0 -74
  524. package/dist/three/frame/blackbody.js.map +0 -1
  525. package/dist/three/frame/chunks.d.ts +0 -157
  526. package/dist/three/frame/chunks.js +0 -559
  527. package/dist/three/frame/chunks.js.map +0 -1
  528. package/dist/three/frame/density-material.d.ts +0 -53
  529. package/dist/three/frame/density-material.js +0 -353
  530. package/dist/three/frame/density-material.js.map +0 -1
  531. package/dist/three/frame/environment-grid-material.d.ts +0 -21
  532. package/dist/three/frame/environment-grid-material.js +0 -184
  533. package/dist/three/frame/environment-grid-material.js.map +0 -1
  534. package/dist/three/frame/environment-material.d.ts +0 -22
  535. package/dist/three/frame/environment-material.js +0 -87
  536. package/dist/three/frame/environment-material.js.map +0 -1
  537. package/dist/three/frame/grid-mip-material.d.ts +0 -12
  538. package/dist/three/frame/grid-mip-material.js +0 -61
  539. package/dist/three/frame/grid-mip-material.js.map +0 -1
  540. package/dist/three/frame/inject-material.d.ts +0 -56
  541. package/dist/three/frame/inject-material.js +0 -424
  542. package/dist/three/frame/inject-material.js.map +0 -1
  543. package/dist/three/frame/integrate-material.d.ts +0 -26
  544. package/dist/three/frame/integrate-material.js +0 -198
  545. package/dist/three/frame/integrate-material.js.map +0 -1
  546. package/dist/three/frame/layout.d.ts +0 -34
  547. package/dist/three/frame/layout.js +0 -48
  548. package/dist/three/frame/layout.js.map +0 -1
  549. package/dist/three/frame/lighting-material.d.ts +0 -41
  550. package/dist/three/frame/lighting-material.js +0 -257
  551. package/dist/three/frame/lighting-material.js.map +0 -1
  552. package/dist/three/frame/lights.d.ts +0 -11
  553. package/dist/three/frame/lights.js +0 -16
  554. package/dist/three/frame/lights.js.map +0 -1
  555. package/dist/three/frame/neural.d.ts +0 -14
  556. package/dist/three/frame/neural.js +0 -105
  557. package/dist/three/frame/neural.js.map +0 -1
  558. package/dist/three/frame/phase.d.ts +0 -44
  559. package/dist/three/frame/phase.js +0 -80
  560. package/dist/three/frame/phase.js.map +0 -1
  561. package/dist/three/frame/shadow-density-material.d.ts +0 -30
  562. package/dist/three/frame/shadow-density-material.js +0 -123
  563. package/dist/three/frame/shadow-density-material.js.map +0 -1
  564. package/dist/three/frame/temporal-material.d.ts +0 -29
  565. package/dist/three/frame/temporal-material.js +0 -152
  566. package/dist/three/frame/temporal-material.js.map +0 -1
  567. package/dist/three/frame/transmittance-material.d.ts +0 -31
  568. package/dist/three/frame/transmittance-material.js +0 -315
  569. package/dist/three/frame/transmittance-material.js.map +0 -1
  570. package/dist/three/frame/view.d.ts +0 -103
  571. package/dist/three/frame/view.js +0 -296
  572. package/dist/three/frame/view.js.map +0 -1
  573. package/dist/three/frame/volume-shadow-material.d.ts +0 -36
  574. package/dist/three/frame/volume-shadow-material.js +0 -128
  575. package/dist/three/frame/volume-shadow-material.js.map +0 -1
  576. package/dist/three/sequence.js.map +0 -1
@@ -10,14 +10,18 @@
10
10
  *
11
11
  * The framing is NanoVDB's `io::Segment` (IO.h; see `nvdb-file.ts`): a 16-byte
12
12
  * FileHeader, then a 176-byte FileMetaData + name per grid, then each grid's
13
- * data (the image for codec NONE; a u64 size + zlib stream for codec ZIP). A
14
- * file is one or more segments. Every size the reader allocates is declared
15
- * up front, so it checks each one before allocating:
13
+ * data (the image for codec NONE; a u64 size + zlib stream for codec ZIP; one
14
+ * or more u64 size + Blosc1 frame chunks for codec BLOSC, K7.C). A file is one
15
+ * or more segments. Every size the reader allocates is declared up front, so it
16
+ * checks each one before allocating:
16
17
  *
17
18
  * - `gridSize` must be a safe integer, at least a GridData header, and at most
18
19
  * `maxGridBytes` (default 1 GiB);
19
20
  * - a ZIP payload must be at most zlib's `compressBound(gridSize)` (+64), and
20
21
  * inflates into gridSize + 1 bytes at most (`inflateZipGrid`);
22
+ * - a BLOSC chunk must be at most the grid bytes still missing + its 16-byte
23
+ * header (`bloscChunkBound`), and decodes straight into the grid's buffer,
24
+ * never past it;
21
25
  * - a grid name is at most `maxNameBytes` (default 64 KiB).
22
26
  *
23
27
  * Memory: the reader holds the current segment's metadata and the grid it is
@@ -25,13 +29,15 @@
25
29
  * per grid, 4-byte aligned, so `image` is a zero-copy `Uint32Array`).
26
30
  *
27
31
  * Errors are `NvdbFormatError` (a `VDBToolsError`) for malformed or
28
- * truncated data and `VDBUnsupportedError` for valid data out of scope
29
- * (BLOSC, a raw `NanoVDB1` grid buffer, unsupported grid types). After an
30
- * error the reader is failed: every later `push` rethrows it.
32
+ * truncated data and `VDBUnsupportedError` for valid data out of scope (a raw
33
+ * `NanoVDB1` grid buffer, another NanoVDB major version, unsupported grid
34
+ * types or Blosc codecs). After an error the reader is failed: every later
35
+ * `push` rethrows it.
31
36
  */
32
37
  import { NvdbFormatError } from "./errors.js";
33
38
  import { type NvdbGridMetadata } from "./nvdb-file.js";
34
39
  export { NvdbFormatError };
40
+ /** Options of {@link NvdbStreamReader} and {@link readNvdbStream}. */
35
41
  export interface NvdbStreamReaderOptions {
36
42
  /** Largest `gridSize` (decoded image bytes) the reader allocates. Default 1 GiB. */
37
43
  maxGridBytes?: number;
@@ -75,6 +81,7 @@ export declare function nvdbResponseLength(response: {
75
81
  export interface NvdbStreamGrid {
76
82
  /** Grid number in the file (counted across segments). */
77
83
  index: number;
84
+ /** The grid's metadata from the file's FileMetaData. */
78
85
  metadata: NvdbGridMetadata;
79
86
  /** The decoded grid image (zero-copy view of a buffer of its own). */
80
87
  image: Uint32Array;
@@ -87,7 +94,12 @@ export interface NvdbStreamGrid {
87
94
  /** The name without its `.lod<N>` suffix (the full grid's name). */
88
95
  baseName: string;
89
96
  }
90
- /** The preview name for `name` at `resolution`: `<name>.lod<resolution>` (K4.5). */
97
+ /**
98
+ * The preview name for `name` at `resolution`: `<name>.lod<resolution>` (K4.5).
99
+ *
100
+ * @param name The full grid's name.
101
+ * @param resolution Preview resolution, a positive integer.
102
+ */
91
103
  export declare function nvdbLodName(name: string, resolution: number): string;
92
104
  /** Splits a `<name>.lod<N>` preview name; a full grid's name gives `lod: null`. */
93
105
  export declare function parseNvdbLodName(name: string): {
@@ -100,6 +112,7 @@ export declare function parseNvdbLodName(name: string): {
100
112
  */
101
113
  export declare class NvdbStreamReader {
102
114
  #private;
115
+ /** @param options Size limits, a known total size and a progress callback. */
103
116
  constructor(options?: NvdbStreamReaderOptions);
104
117
  /** The expected file size in bytes, or null while unknown (see {@link NvdbProgress.bytesTotal}). */
105
118
  get bytesTotal(): number | null;
@@ -134,5 +147,8 @@ export type NvdbStreamSource = {
134
147
  * Reads a byte stream (a `fetch` Response or body, or any async iterable of chunks) through
135
148
  * an {@link NvdbStreamReader}, yielding each grid as it completes. Stops
136
149
  * reading (and cancels the stream) when the consumer breaks out of the loop.
150
+ *
151
+ * @param source A `Response`, a response body, or an async iterable of byte chunks.
152
+ * @param options Size limits, a known total size and a progress callback.
137
153
  */
138
154
  export declare function readNvdbStream(source: NvdbStreamSource, options?: NvdbStreamReaderOptions): AsyncGenerator<NvdbStreamGrid, void, undefined>;
@@ -10,14 +10,18 @@
10
10
  *
11
11
  * The framing is NanoVDB's `io::Segment` (IO.h; see `nvdb-file.ts`): a 16-byte
12
12
  * FileHeader, then a 176-byte FileMetaData + name per grid, then each grid's
13
- * data (the image for codec NONE; a u64 size + zlib stream for codec ZIP). A
14
- * file is one or more segments. Every size the reader allocates is declared
15
- * up front, so it checks each one before allocating:
13
+ * data (the image for codec NONE; a u64 size + zlib stream for codec ZIP; one
14
+ * or more u64 size + Blosc1 frame chunks for codec BLOSC, K7.C). A file is one
15
+ * or more segments. Every size the reader allocates is declared up front, so it
16
+ * checks each one before allocating:
16
17
  *
17
18
  * - `gridSize` must be a safe integer, at least a GridData header, and at most
18
19
  * `maxGridBytes` (default 1 GiB);
19
20
  * - a ZIP payload must be at most zlib's `compressBound(gridSize)` (+64), and
20
21
  * inflates into gridSize + 1 bytes at most (`inflateZipGrid`);
22
+ * - a BLOSC chunk must be at most the grid bytes still missing + its 16-byte
23
+ * header (`bloscChunkBound`), and decodes straight into the grid's buffer,
24
+ * never past it;
21
25
  * - a grid name is at most `maxNameBytes` (default 64 KiB).
22
26
  *
23
27
  * Memory: the reader holds the current segment's metadata and the grid it is
@@ -25,12 +29,13 @@
25
29
  * per grid, 4-byte aligned, so `image` is a zero-copy `Uint32Array`).
26
30
  *
27
31
  * Errors are `NvdbFormatError` (a `VDBToolsError`) for malformed or
28
- * truncated data and `VDBUnsupportedError` for valid data out of scope
29
- * (BLOSC, a raw `NanoVDB1` grid buffer, unsupported grid types). After an
30
- * error the reader is failed: every later `push` rethrows it.
32
+ * truncated data and `VDBUnsupportedError` for valid data out of scope (a raw
33
+ * `NanoVDB1` grid buffer, another NanoVDB major version, unsupported grid
34
+ * types or Blosc codecs). After an error the reader is failed: every later
35
+ * `push` rethrows it.
31
36
  */
32
37
  import { NvdbFormatError, VDBToolsError, VDBUnsupportedError } from "./errors.js";
33
- import { NvdbCodec, FILE_HEADER_SIZE, FILE_METADATA_SIZE, GRID_DATA_SIZE, assertSafeGridSize, decodeCString, finishGridRecord, gridClassName, gridTypeName, inflateZipGrid, isFileHeaderMagic, readFileHeader, readFileMetaData, validateGridType, } from "./nvdb-file.js";
38
+ import { NvdbCodec, FILE_HEADER_SIZE, FILE_METADATA_SIZE, GRID_DATA_SIZE, assertSafeGridSize, checkBloscChunkSize, decodeBloscChunk, decodeCString, finishGridRecord, gridClassName, gridTypeName, inflateZipGrid, isFileHeaderMagic, readFileHeader, readFileMetaData, validateGridType, } from "./nvdb-file.js";
34
39
  export { NvdbFormatError };
35
40
  /**
36
41
  * The byte length of a `fetch` Response's body when the header says so: its
@@ -55,7 +60,12 @@ function zipBound(n) {
55
60
  return n + Math.floor(n / 4096) + Math.floor(n / 16384) + Math.floor(n / 33554432) + 13 + 64;
56
61
  }
57
62
  const LOD_SUFFIX = /^(.*)\.lod(\d+)$/;
58
- /** The preview name for `name` at `resolution`: `<name>.lod<resolution>` (K4.5). */
63
+ /**
64
+ * The preview name for `name` at `resolution`: `<name>.lod<resolution>` (K4.5).
65
+ *
66
+ * @param name The full grid's name.
67
+ * @param resolution Preview resolution, a positive integer.
68
+ */
59
69
  export function nvdbLodName(name, resolution) {
60
70
  if (!Number.isInteger(resolution) || resolution < 1) {
61
71
  throw new VDBToolsError("VDB_BAD_OPTION", `nvdbLodName: resolution must be a positive integer, got ${resolution}`);
@@ -90,10 +100,14 @@ export class NvdbStreamReader {
90
100
  #nextMeta = null;
91
101
  #gridInSegment = 0;
92
102
  #received = 0;
103
+ /** BLOSC: the grid image being filled chunk by chunk, and how much of it is decoded. */
104
+ #gridBuffer = null;
105
+ #gridFilled = 0;
93
106
  #segments = 0;
94
107
  #emitted = 0;
95
108
  #failed = null;
96
109
  #grids = [];
110
+ /** @param options Size limits, a known total size and a progress callback. */
97
111
  constructor(options = {}) {
98
112
  this.#maxGridBytes = options.maxGridBytes ?? DEFAULT_MAX_GRID_BYTES;
99
113
  this.#maxNameBytes = options.maxNameBytes ?? DEFAULT_MAX_NAME_BYTES;
@@ -187,6 +201,10 @@ export class NvdbStreamReader {
187
201
  return `name of grid #${this.#grids.length}`;
188
202
  case "zipPrefix":
189
203
  return `ZIP size prefix of grid #${grid}`;
204
+ case "bloscPrefix":
205
+ return `BLOSC chunk size of grid #${grid}, ${this.#gridFilled} of ${this.#gridBuffer?.byteLength ?? 0} bytes decoded`;
206
+ case "bloscChunk":
207
+ return `BLOSC chunk of grid #${grid}, ${this.#filled} of ${this.#target.byteLength} bytes`;
190
208
  case "grid":
191
209
  return `data of grid #${grid}, ${this.#filled} of ${this.#target.byteLength} bytes`;
192
210
  }
@@ -210,16 +228,7 @@ export class NvdbStreamReader {
210
228
  throw new NvdbFormatError(`expected a NanoVDB FileHeader (magic NanoVDB2 or NanoVDB0) at byte ${this.#received - FILE_HEADER_SIZE}, found "${magic.replace(/[^\x20-\x7e]/g, "?")}".`);
211
229
  }
212
230
  const view = new DataView(this.#scratch.buffer, 0, FILE_HEADER_SIZE);
213
- let header;
214
- try {
215
- header = readFileHeader(view, 0);
216
- }
217
- catch (err) {
218
- const msg = err.message;
219
- if (/BLOSC/.test(msg))
220
- throw new VDBUnsupportedError(msg);
221
- throw new NvdbFormatError(msg);
222
- }
231
+ const header = readFileHeader(view, 0);
223
232
  if (header.gridCount === 0)
224
233
  throw new NvdbFormatError(`segment #${this.#segments} declares 0 grids.`);
225
234
  this.#codec = header.codec;
@@ -292,31 +301,63 @@ export class NvdbStreamReader {
292
301
  this.#expect("grid", new Uint8Array(size));
293
302
  return;
294
303
  }
295
- case "grid": {
296
- const k = this.#gridInSegment;
297
- const index = this.#segmentFirst() + k;
298
- const { meta, name } = this.#pending[k];
299
- const bytes = this.#codec === NvdbCodec.ZIP ? inflateZipGrid(this.#target, meta.gridSize, index, name) : this.#target;
300
- const record = finishGridRecord(meta, name, index, bytes);
301
- this.#grids[index] = record.metadata;
302
- const { baseName, lod } = parseNvdbLodName(name);
303
- out.push({ index, metadata: record.metadata, image: record.image, bytesReceived: this.#received, bytesTotal: this.bytesTotal, lod, baseName });
304
- this.#emitted++;
305
- this.#gridInSegment++;
306
- if (this.#gridInSegment < this.#segmentGrids)
307
- this.#startGrid();
308
- else {
309
- this.#pending = [];
310
- this.#expect("header", this.#scratch.subarray(0, FILE_HEADER_SIZE));
304
+ case "bloscPrefix": {
305
+ const { name } = this.#pending[this.#gridInSegment];
306
+ const index = this.#segmentFirst() + this.#gridInSegment;
307
+ const size = Number(new DataView(this.#scratch.buffer, 0, ZIP_PREFIX).getBigUint64(0, true));
308
+ checkBloscChunkSize(size, this.#gridBuffer.byteLength - this.#gridFilled, index, name);
309
+ this.#expect("bloscChunk", new Uint8Array(size));
310
+ return;
311
+ }
312
+ case "bloscChunk": {
313
+ const { name } = this.#pending[this.#gridInSegment];
314
+ const index = this.#segmentFirst() + this.#gridInSegment;
315
+ const grid = this.#gridBuffer;
316
+ this.#gridFilled += decodeBloscChunk(this.#target, grid.subarray(this.#gridFilled), index, name);
317
+ if (this.#gridFilled < grid.byteLength) {
318
+ this.#expect("bloscPrefix", this.#scratch.subarray(0, ZIP_PREFIX));
319
+ return;
311
320
  }
321
+ this.#gridBuffer = null;
322
+ this.#finishGrid(out, grid);
323
+ return;
324
+ }
325
+ case "grid": {
326
+ const { meta, name } = this.#pending[this.#gridInSegment];
327
+ const index = this.#segmentFirst() + this.#gridInSegment;
328
+ this.#finishGrid(out, this.#codec === NvdbCodec.ZIP ? inflateZipGrid(this.#target, meta.gridSize, index, name) : this.#target);
312
329
  return;
313
330
  }
314
331
  }
315
332
  }
333
+ /** Emits the current grid from its decoded image and moves to the next grid or segment. */
334
+ #finishGrid(out, bytes) {
335
+ const k = this.#gridInSegment;
336
+ const index = this.#segmentFirst() + k;
337
+ const { meta, name } = this.#pending[k];
338
+ const record = finishGridRecord(meta, name, index, bytes);
339
+ this.#grids[index] = record.metadata;
340
+ const { baseName, lod } = parseNvdbLodName(name);
341
+ out.push({ index, metadata: record.metadata, image: record.image, bytesReceived: this.#received, bytesTotal: this.bytesTotal, lod, baseName });
342
+ this.#emitted++;
343
+ this.#gridInSegment++;
344
+ if (this.#gridInSegment < this.#segmentGrids)
345
+ this.#startGrid();
346
+ else {
347
+ this.#pending = [];
348
+ this.#expect("header", this.#scratch.subarray(0, FILE_HEADER_SIZE));
349
+ }
350
+ }
316
351
  #startGrid() {
317
352
  const { meta } = this.#pending[this.#gridInSegment];
318
353
  if (this.#codec === NvdbCodec.ZIP)
319
354
  this.#expect("zipPrefix", this.#scratch.subarray(0, ZIP_PREFIX));
355
+ else if (this.#codec === NvdbCodec.BLOSC) {
356
+ // gridSize was checked against maxGridBytes with its FileMetaData; chunks decode into this.
357
+ this.#gridBuffer = new Uint8Array(meta.gridSize);
358
+ this.#gridFilled = 0;
359
+ this.#expect("bloscPrefix", this.#scratch.subarray(0, ZIP_PREFIX));
360
+ }
320
361
  else
321
362
  this.#expect("grid", new Uint8Array(meta.gridSize));
322
363
  }
@@ -328,6 +369,9 @@ function isResponse(source) {
328
369
  * Reads a byte stream (a `fetch` Response or body, or any async iterable of chunks) through
329
370
  * an {@link NvdbStreamReader}, yielding each grid as it completes. Stops
330
371
  * reading (and cancels the stream) when the consumer breaks out of the loop.
372
+ *
373
+ * @param source A `Response`, a response body, or an async iterable of byte chunks.
374
+ * @param options Size limits, a known total size and a progress callback.
331
375
  */
332
376
  export async function* readNvdbStream(source, options = {}) {
333
377
  if (isResponse(source)) {
@@ -1 +1 @@
1
- {"version":3,"file":"nvdb-stream.js","sourceRoot":"","sources":["../../src/tools/nvdb-stream.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,OAAO,EAAE,eAAe,EAAE,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,IAAI,CAAC;YACH,SAAS,CAAC;gBACR,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,CAAC,CAAC,IAAI,EAAE,CAAC;gBACvC,IAAI,IAAI;oBAAE,MAAM;gBAChB,KAAK,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YAC5B,CAAC;YACD,QAAQ,GAAG,IAAI,CAAC;YAChB,MAAM,CAAC,GAAG,EAAE,CAAC;QACf,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,QAAQ;gBAAE,MAAM,CAAC,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;YACvD,CAAC,CAAC,WAAW,EAAE,CAAC;QAClB,CAAC;QACD,OAAO;IACT,CAAC;IACD,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,MAAmC;QAAE,KAAK,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACzF,MAAM,CAAC,GAAG,EAAE,CAAC;AACf,CAAC","sourcesContent":["/**\n * `NvdbStreamReader` (K4.16): read a `.nvdb` file from pushed bytes and emit\n * each grid as soon as its data is complete, so a coarse preview written first\n * (docs/dev/design/K4-5-coarse-first.md, option a) can be drawn while the full\n * grid is still downloading.\n *\n * const reader = new NvdbStreamReader();\n * for await (const chunk of response.body) for (const g of reader.push(chunk)) show(g);\n * reader.end(); // throws if the stream stopped mid-grid\n *\n * The framing is NanoVDB's `io::Segment` (IO.h; see `nvdb-file.ts`): a 16-byte\n * FileHeader, then a 176-byte FileMetaData + name per grid, then each grid's\n * data (the image for codec NONE; a u64 size + zlib stream for codec ZIP). A\n * file is one or more segments. Every size the reader allocates is declared\n * up front, so it checks each one before allocating:\n *\n * - `gridSize` must be a safe integer, at least a GridData header, and at most\n * `maxGridBytes` (default 1 GiB);\n * - a ZIP payload must be at most zlib's `compressBound(gridSize)` (+64), and\n * inflates into gridSize + 1 bytes at most (`inflateZipGrid`);\n * - a grid name is at most `maxNameBytes` (default 64 KiB).\n *\n * Memory: the reader holds the current segment's metadata and the grid it is\n * filling, never the whole file. Emitted images are handed off (a fresh buffer\n * per grid, 4-byte aligned, so `image` is a zero-copy `Uint32Array`).\n *\n * Errors are `NvdbFormatError` (a `VDBToolsError`) for malformed or\n * truncated data and `VDBUnsupportedError` for valid data out of scope\n * (BLOSC, a raw `NanoVDB1` grid buffer, unsupported grid types). After an\n * error the reader is failed: every later `push` rethrows it.\n */\n\nimport { NvdbFormatError, VDBToolsError, VDBUnsupportedError } from \"./errors.js\";\nimport {\n NvdbCodec,\n FILE_HEADER_SIZE,\n FILE_METADATA_SIZE,\n GRID_DATA_SIZE,\n assertSafeGridSize,\n decodeCString,\n finishGridRecord,\n gridClassName,\n gridTypeName,\n inflateZipGrid,\n isFileHeaderMagic,\n readFileHeader,\n readFileMetaData,\n validateGridType,\n type NvdbGridMetadata,\n type ParsedFileMetaData,\n} from \"./nvdb-file.js\";\n\nexport { NvdbFormatError };\n\nexport interface NvdbStreamReaderOptions {\n /** Largest `gridSize` (decoded image bytes) the reader allocates. Default 1 GiB. */\n maxGridBytes?: number;\n /** Largest grid name (FileMetaData.nameSize). Default 64 KiB. */\n maxNameBytes?: number;\n /**\n * The whole stream's size in bytes when the caller knows it (a Response's\n * Content-Length: see {@link nvdbResponseLength}). Takes precedence over the\n * size derived from the grids' FileMetaData.\n */\n bytesTotal?: number;\n /** Called after every `push` with the progress so far (not inside the push's error handling). */\n onProgress?: (progress: NvdbProgress) => void;\n}\n\n/** Download progress of an {@link NvdbStreamReader}. */\nexport interface NvdbProgress {\n /** File bytes pushed so far (what the reader consumed; decoded bytes if the transport decompresses). */\n bytesReceived: number;\n /**\n * The expected total, or null while unknown: the `bytesTotal` option when\n * given, else the end of the latest segment whose FileMetaData have all\n * arrived (header + every grid's metadata, name and `fileSize`). That is\n * exact for a one-segment file and a lower bound while a later segment is\n * still ahead. Null until the first segment's metadata is in.\n */\n bytesTotal: number | null;\n /** Grids emitted so far. */\n gridsEmitted: number;\n}\n\n/**\n * The byte length of a `fetch` Response's body when the header says so: its\n * Content-Length, unless a Content-Encoding makes the (decoded) bytes the\n * reader sees differ from it. Null when unknown.\n */\nexport function nvdbResponseLength(response: { headers: { get(name: string): string | null } }): number | null {\n const encoding = response.headers.get(\"content-encoding\");\n if (encoding !== null && encoding.trim() !== \"\" && encoding.trim().toLowerCase() !== \"identity\") return null;\n const raw = response.headers.get(\"content-length\");\n if (raw === null || !/^\\d+$/.test(raw.trim())) return null;\n const n = Number(raw);\n return Number.isSafeInteger(n) ? n : null;\n}\n\n/** One grid, emitted by {@link NvdbStreamReader.push} as soon as its data is complete. */\nexport interface NvdbStreamGrid {\n /** Grid number in the file (counted across segments). */\n index: number;\n metadata: NvdbGridMetadata;\n /** The decoded grid image (zero-copy view of a buffer of its own). */\n image: Uint32Array;\n /** File bytes pushed when this grid completed (the end of its data). */\n bytesReceived: number;\n /** The expected file size when this grid completed, or null if unknown (see {@link NvdbProgress.bytesTotal}). */\n bytesTotal: number | null;\n /** The preview level from a `<name>.lod<N>` name, or null for a full grid. */\n lod: number | null;\n /** The name without its `.lod<N>` suffix (the full grid's name). */\n baseName: string;\n}\n\nconst DEFAULT_MAX_GRID_BYTES = 1 << 30;\nconst DEFAULT_MAX_NAME_BYTES = 1 << 16;\nconst ZIP_PREFIX = 8;\n\n/** zlib's `compressBound` (the largest `compress()` output for `n` input bytes), plus slack. */\nfunction zipBound(n: number): number {\n return n + Math.floor(n / 4096) + Math.floor(n / 16384) + Math.floor(n / 33554432) + 13 + 64;\n}\n\nconst LOD_SUFFIX = /^(.*)\\.lod(\\d+)$/;\n\n/** The preview name for `name` at `resolution`: `<name>.lod<resolution>` (K4.5). */\nexport function nvdbLodName(name: string, resolution: number): string {\n if (!Number.isInteger(resolution) || resolution < 1) {\n throw new VDBToolsError(\"VDB_BAD_OPTION\", `nvdbLodName: resolution must be a positive integer, got ${resolution}`);\n }\n return `${name}.lod${resolution}`;\n}\n\n/** Splits a `<name>.lod<N>` preview name; a full grid's name gives `lod: null`. */\nexport function parseNvdbLodName(name: string): { baseName: string; lod: number | null } {\n const m = LOD_SUFFIX.exec(name);\n return m ? { baseName: m[1]!, lod: Number(m[2]) } : { baseName: name, lod: null };\n}\n\ntype Phase = \"header\" | \"meta\" | \"name\" | \"zipPrefix\" | \"grid\";\n\ninterface PendingGrid {\n meta: ParsedFileMetaData;\n name: string;\n}\n\n/**\n * Push-based `.nvdb` reader: `push(chunk)` returns the grids that chunk\n * completed. Call `end()` when the stream ends to detect truncation.\n */\nexport class NvdbStreamReader {\n readonly #maxGridBytes: number;\n readonly #maxNameBytes: number;\n readonly #onProgress: ((progress: NvdbProgress) => void) | undefined;\n readonly #declaredTotal: number | null;\n #segmentStart = 0;\n #knownEnd: number | null = null;\n\n /** Small fixed-size fields (FileHeader, FileMetaData, ZIP prefix) and names. */\n #scratch = new Uint8Array(FILE_METADATA_SIZE);\n /** The buffer being filled (scratch or a grid payload) and how far. */\n #target: Uint8Array;\n #filled = 0;\n\n #phase: Phase = \"header\";\n #codec: NvdbCodec = NvdbCodec.NONE;\n #segmentGrids = 0;\n #pending: PendingGrid[] = [];\n #nextMeta: ParsedFileMetaData | null = null;\n #gridInSegment = 0;\n #received = 0;\n #segments = 0;\n #emitted = 0;\n #failed: Error | null = null;\n readonly #grids: NvdbGridMetadata[] = [];\n\n constructor(options: NvdbStreamReaderOptions = {}) {\n this.#maxGridBytes = options.maxGridBytes ?? DEFAULT_MAX_GRID_BYTES;\n this.#maxNameBytes = options.maxNameBytes ?? DEFAULT_MAX_NAME_BYTES;\n if (!Number.isSafeInteger(this.#maxGridBytes) || this.#maxGridBytes < GRID_DATA_SIZE) {\n throw new VDBToolsError(\"VDB_BAD_OPTION\", `NvdbStreamReader: maxGridBytes must be an integer >= ${GRID_DATA_SIZE}`);\n }\n if (!Number.isSafeInteger(this.#maxNameBytes) || this.#maxNameBytes < 1) {\n throw new VDBToolsError(\"VDB_BAD_OPTION\", \"NvdbStreamReader: maxNameBytes must be a positive integer\");\n }\n this.#onProgress = options.onProgress;\n this.#declaredTotal = options.bytesTotal ?? null;\n if (this.#declaredTotal !== null && (!Number.isSafeInteger(this.#declaredTotal) || this.#declaredTotal < 0)) {\n throw new VDBToolsError(\"VDB_BAD_OPTION\", \"NvdbStreamReader: bytesTotal must be a non-negative integer\");\n }\n this.#target = this.#scratch.subarray(0, FILE_HEADER_SIZE);\n }\n\n /** The expected file size in bytes, or null while unknown (see {@link NvdbProgress.bytesTotal}). */\n get bytesTotal(): number | null {\n return this.#declaredTotal ?? this.#knownEnd;\n }\n\n /** Progress so far. */\n get progress(): NvdbProgress {\n return { bytesReceived: this.#received, bytesTotal: this.bytesTotal, gridsEmitted: this.#emitted };\n }\n\n /** File bytes pushed so far. */\n get bytesReceived(): number {\n return this.#received;\n }\n\n /** Grids emitted so far. */\n get gridsEmitted(): number {\n return this.#emitted;\n }\n\n /**\n * Metadata of every grid whose FileMetaData has arrived, in file order,\n * including grids whose data has not (a segment lists all its grids before\n * any data, so the full grid's `gridByteSize` is known before its preview\n * is emitted). Names are as stored; see {@link parseNvdbLodName}.\n */\n get grids(): readonly NvdbGridMetadata[] {\n return this.#grids;\n }\n\n /** True between segments: every grid announced so far has been emitted. */\n get atBoundary(): boolean {\n return this.#phase === \"header\" && this.#filled === 0 && this.#segments > 0;\n }\n\n /** Pushes the next bytes of the file; returns the grids they completed, in order. */\n push(chunk: Uint8Array): NvdbStreamGrid[] {\n if (this.#failed) throw this.#failed;\n const out: NvdbStreamGrid[] = [];\n try {\n let at = 0;\n while (at < chunk.byteLength) {\n const take = Math.min(chunk.byteLength - at, this.#target.byteLength - this.#filled);\n this.#target.set(chunk.subarray(at, at + take), this.#filled);\n this.#filled += take;\n at += take;\n this.#received += take;\n if (this.#filled === this.#target.byteLength) this.#advance(out);\n }\n // A zero-length target (an empty name) completes without bytes.\n while (this.#target.byteLength === 0 && this.#filled === 0 && this.#phase !== \"header\") this.#advance(out);\n } catch (err) {\n this.#failed = err instanceof VDBToolsError ? err : new NvdbFormatError((err as Error).message);\n throw this.#failed;\n }\n if (this.#onProgress && chunk.byteLength > 0) this.#onProgress(this.progress);\n return out;\n }\n\n /** Declares the end of the stream; throws `NvdbFormatError` if it stopped inside a segment. */\n end(): void {\n if (this.#failed) throw this.#failed;\n if (this.atBoundary) return;\n this.#failed = new NvdbFormatError(\n `the stream ended after ${this.#received} bytes, in the ${this.#describe()}; the file is truncated.`,\n );\n throw this.#failed;\n }\n\n #describe(): string {\n const grid = this.#segmentFirst() + this.#gridInSegment;\n switch (this.#phase) {\n case \"header\":\n return `FileHeader, ${this.#filled} of ${FILE_HEADER_SIZE} bytes`;\n case \"meta\":\n return `FileMetaData of grid #${this.#grids.length}, ${this.#filled} of ${FILE_METADATA_SIZE} bytes`;\n case \"name\":\n return `name of grid #${this.#grids.length}`;\n case \"zipPrefix\":\n return `ZIP size prefix of grid #${grid}`;\n case \"grid\":\n return `data of grid #${grid}, ${this.#filled} of ${this.#target.byteLength} bytes`;\n }\n }\n\n #segmentFirst(): number {\n return this.#grids.length - this.#segmentGrids;\n }\n\n #expect(phase: Phase, target: Uint8Array): void {\n this.#phase = phase;\n this.#target = target;\n this.#filled = 0;\n }\n\n #advance(out: NvdbStreamGrid[]): void {\n switch (this.#phase) {\n case \"header\": {\n const magic = String.fromCharCode(...this.#scratch.subarray(0, 8));\n if (!isFileHeaderMagic(magic)) {\n if (magic === \"NanoVDB1\") {\n throw new VDBUnsupportedError(\n \".nvdb stream: this is a raw NanoVDB grid buffer (magic NanoVDB1), not a segment file; read it whole with NvdbFile.fromArrayBuffer.\",\n );\n }\n throw new NvdbFormatError(\n `expected a NanoVDB FileHeader (magic NanoVDB2 or NanoVDB0) at byte ${this.#received - FILE_HEADER_SIZE}, found \"${magic.replace(/[^\\x20-\\x7e]/g, \"?\")}\".`,\n );\n }\n const view = new DataView(this.#scratch.buffer, 0, FILE_HEADER_SIZE);\n let header: { gridCount: number; codec: NvdbCodec };\n try {\n header = readFileHeader(view, 0);\n } catch (err) {\n const msg = (err as Error).message;\n if (/BLOSC/.test(msg)) throw new VDBUnsupportedError(msg);\n throw new NvdbFormatError(msg);\n }\n if (header.gridCount === 0) throw new NvdbFormatError(`segment #${this.#segments} declares 0 grids.`);\n this.#codec = header.codec;\n this.#segmentGrids = header.gridCount;\n this.#segments++;\n this.#segmentStart = this.#received - FILE_HEADER_SIZE;\n this.#pending = [];\n this.#gridInSegment = 0;\n this.#expect(\"meta\", this.#scratch.subarray(0, FILE_METADATA_SIZE));\n return;\n }\n case \"meta\": {\n const i = this.#grids.length;\n const meta = readFileMetaData(new DataView(this.#scratch.buffer, 0, FILE_METADATA_SIZE), 0);\n assertSafeGridSize(meta.gridSize, `grid #${i}'s FileMetaData.gridSize`);\n if (meta.gridSize < GRID_DATA_SIZE) {\n throw new NvdbFormatError(`grid #${i} declares gridSize ${meta.gridSize}, smaller than a ${GRID_DATA_SIZE}-byte GridData header.`);\n }\n if (meta.gridSize > this.#maxGridBytes) {\n throw new NvdbFormatError(\n `grid #${i} declares gridSize ${meta.gridSize} bytes, over maxGridBytes (${this.#maxGridBytes}); raise the option if the file is trusted.`,\n );\n }\n if (meta.nameSize > this.#maxNameBytes) {\n throw new NvdbFormatError(`grid #${i} declares a ${meta.nameSize}-byte name, over maxNameBytes (${this.#maxNameBytes}).`);\n }\n this.#nextMeta = meta;\n this.#expect(\"name\", meta.nameSize <= this.#scratch.byteLength ? this.#scratch.subarray(0, meta.nameSize) : new Uint8Array(meta.nameSize));\n return;\n }\n case \"name\": {\n const meta = this.#nextMeta!;\n const name = decodeCString(this.#target);\n const gridType = gridTypeName(meta.gridTypeId);\n try {\n validateGridType(gridType, name);\n } catch (err) {\n throw new VDBUnsupportedError((err as Error).message);\n }\n this.#pending.push({ meta, name });\n this.#grids.push({\n name,\n gridType,\n gridClass: gridClassName(meta.gridClassId),\n worldBBox: meta.worldBBox,\n indexBBox: meta.indexBBox,\n voxelSize: meta.voxelSize,\n voxelCount: meta.voxelCount,\n gridByteSize: meta.gridSize,\n });\n this.#nextMeta = null;\n if (this.#pending.length < this.#segmentGrids) this.#expect(\"meta\", this.#scratch.subarray(0, FILE_METADATA_SIZE));\n else {\n let end = this.#segmentStart + FILE_HEADER_SIZE;\n for (const g of this.#pending) end += FILE_METADATA_SIZE + g.meta.nameSize + g.meta.fileSize;\n this.#knownEnd = Number.isSafeInteger(end) ? end : null;\n this.#startGrid();\n }\n return;\n }\n case \"zipPrefix\": {\n const { meta, name } = this.#pending[this.#gridInSegment]!;\n const size = Number(new DataView(this.#scratch.buffer, 0, ZIP_PREFIX).getBigUint64(0, true));\n const bound = zipBound(meta.gridSize);\n if (!Number.isSafeInteger(size) || size < 1 || size > bound) {\n throw new NvdbFormatError(\n `grid #${this.#segmentFirst() + this.#gridInSegment} (\"${name}\") declares a ${size}-byte ZIP payload for a ` +\n `${meta.gridSize}-byte grid (zlib's bound is ${bound}).`,\n );\n }\n this.#expect(\"grid\", new Uint8Array(size));\n return;\n }\n case \"grid\": {\n const k = this.#gridInSegment;\n const index = this.#segmentFirst() + k;\n const { meta, name } = this.#pending[k]!;\n const bytes = this.#codec === NvdbCodec.ZIP ? inflateZipGrid(this.#target, meta.gridSize, index, name) : this.#target;\n const record = finishGridRecord(meta, name, index, bytes);\n this.#grids[index] = record.metadata;\n const { baseName, lod } = parseNvdbLodName(name);\n out.push({ index, metadata: record.metadata, image: record.image, bytesReceived: this.#received, bytesTotal: this.bytesTotal, lod, baseName });\n this.#emitted++;\n this.#gridInSegment++;\n if (this.#gridInSegment < this.#segmentGrids) this.#startGrid();\n else {\n this.#pending = [];\n this.#expect(\"header\", this.#scratch.subarray(0, FILE_HEADER_SIZE));\n }\n return;\n }\n }\n }\n\n #startGrid(): void {\n const { meta } = this.#pending[this.#gridInSegment]!;\n if (this.#codec === NvdbCodec.ZIP) this.#expect(\"zipPrefix\", this.#scratch.subarray(0, ZIP_PREFIX));\n else this.#expect(\"grid\", new Uint8Array(meta.gridSize));\n }\n}\n\n/** What {@link readNvdbStream} reads: a `fetch` Response (its Content-Length feeds `bytesTotal`), its body, or chunks. */\nexport type NvdbStreamSource =\n | { body: ReadableStream<Uint8Array> | null; headers: { get(name: string): string | null } }\n | ReadableStream<Uint8Array>\n | AsyncIterable<Uint8Array>;\n\nfunction isResponse(source: NvdbStreamSource): source is Extract<NvdbStreamSource, { headers: unknown }> {\n return typeof (source as { headers?: unknown }).headers === \"object\" && \"body\" in source;\n}\n\n/**\n * Reads a byte stream (a `fetch` Response or body, or any async iterable of chunks) through\n * an {@link NvdbStreamReader}, yielding each grid as it completes. Stops\n * reading (and cancels the stream) when the consumer breaks out of the loop.\n */\nexport async function* readNvdbStream(\n source: NvdbStreamSource,\n options: NvdbStreamReaderOptions = {},\n): AsyncGenerator<NvdbStreamGrid, void, undefined> {\n if (isResponse(source)) {\n const len = nvdbResponseLength(source);\n if (source.body === null) throw new NvdbFormatError(\"readNvdbStream: the response has no body.\");\n options = len !== null && options.bytesTotal === undefined ? { ...options, bytesTotal: len } : options;\n source = source.body;\n }\n const reader = new NvdbStreamReader(options);\n if (typeof (source as ReadableStream<Uint8Array>).getReader === \"function\") {\n const r = (source as ReadableStream<Uint8Array>).getReader();\n let finished = false;\n try {\n for (;;) {\n const { done, value } = await r.read();\n if (done) break;\n yield* reader.push(value);\n }\n finished = true;\n reader.end();\n } finally {\n if (!finished) await r.cancel().catch(() => undefined);\n r.releaseLock();\n }\n return;\n }\n for await (const chunk of source as AsyncIterable<Uint8Array>) yield* reader.push(chunk);\n reader.end();\n}\n"]}
1
+ {"version":3,"file":"nvdb-stream.js","sourceRoot":"","sources":["../../src/tools/nvdb-stream.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAEH,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAClF,OAAO,EACL,SAAS,EACT,gBAAgB,EAChB,kBA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K,EAAE,MAAM,KAAK,IAAI,MAAmC;QAAE,KAAK,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACzF,MAAM,CAAC,GAAG,EAAE,CAAC;AACf,CAAC","sourcesContent":["/**\n * `NvdbStreamReader` (K4.16): read a `.nvdb` file from pushed bytes and emit\n * each grid as soon as its data is complete, so a coarse preview written first\n * (docs/dev/design/K4-5-coarse-first.md, option a) can be drawn while the full\n * grid is still downloading.\n *\n * const reader = new NvdbStreamReader();\n * for await (const chunk of response.body) for (const g of reader.push(chunk)) show(g);\n * reader.end(); // throws if the stream stopped mid-grid\n *\n * The framing is NanoVDB's `io::Segment` (IO.h; see `nvdb-file.ts`): a 16-byte\n * FileHeader, then a 176-byte FileMetaData + name per grid, then each grid's\n * data (the image for codec NONE; a u64 size + zlib stream for codec ZIP; one\n * or more u64 size + Blosc1 frame chunks for codec BLOSC, K7.C). A file is one\n * or more segments. Every size the reader allocates is declared up front, so it\n * checks each one before allocating:\n *\n * - `gridSize` must be a safe integer, at least a GridData header, and at most\n * `maxGridBytes` (default 1 GiB);\n * - a ZIP payload must be at most zlib's `compressBound(gridSize)` (+64), and\n * inflates into gridSize + 1 bytes at most (`inflateZipGrid`);\n * - a BLOSC chunk must be at most the grid bytes still missing + its 16-byte\n * header (`bloscChunkBound`), and decodes straight into the grid's buffer,\n * never past it;\n * - a grid name is at most `maxNameBytes` (default 64 KiB).\n *\n * Memory: the reader holds the current segment's metadata and the grid it is\n * filling, never the whole file. Emitted images are handed off (a fresh buffer\n * per grid, 4-byte aligned, so `image` is a zero-copy `Uint32Array`).\n *\n * Errors are `NvdbFormatError` (a `VDBToolsError`) for malformed or\n * truncated data and `VDBUnsupportedError` for valid data out of scope (a raw\n * `NanoVDB1` grid buffer, another NanoVDB major version, unsupported grid\n * types or Blosc codecs). After an error the reader is failed: every later\n * `push` rethrows it.\n */\n\nimport { NvdbFormatError, VDBToolsError, VDBUnsupportedError } from \"./errors.js\";\nimport {\n NvdbCodec,\n FILE_HEADER_SIZE,\n FILE_METADATA_SIZE,\n GRID_DATA_SIZE,\n assertSafeGridSize,\n checkBloscChunkSize,\n decodeBloscChunk,\n decodeCString,\n finishGridRecord,\n gridClassName,\n gridTypeName,\n inflateZipGrid,\n isFileHeaderMagic,\n readFileHeader,\n readFileMetaData,\n validateGridType,\n type NvdbGridMetadata,\n type ParsedFileMetaData,\n} from \"./nvdb-file.js\";\n\nexport { NvdbFormatError };\n\n/** Options of {@link NvdbStreamReader} and {@link readNvdbStream}. */\nexport interface NvdbStreamReaderOptions {\n /** Largest `gridSize` (decoded image bytes) the reader allocates. Default 1 GiB. */\n maxGridBytes?: number;\n /** Largest grid name (FileMetaData.nameSize). Default 64 KiB. */\n maxNameBytes?: number;\n /**\n * The whole stream's size in bytes when the caller knows it (a Response's\n * Content-Length: see {@link nvdbResponseLength}). Takes precedence over the\n * size derived from the grids' FileMetaData.\n */\n bytesTotal?: number;\n /** Called after every `push` with the progress so far (not inside the push's error handling). */\n onProgress?: (progress: NvdbProgress) => void;\n}\n\n/** Download progress of an {@link NvdbStreamReader}. */\nexport interface NvdbProgress {\n /** File bytes pushed so far (what the reader consumed; decoded bytes if the transport decompresses). */\n bytesReceived: number;\n /**\n * The expected total, or null while unknown: the `bytesTotal` option when\n * given, else the end of the latest segment whose FileMetaData have all\n * arrived (header + every grid's metadata, name and `fileSize`). That is\n * exact for a one-segment file and a lower bound while a later segment is\n * still ahead. Null until the first segment's metadata is in.\n */\n bytesTotal: number | null;\n /** Grids emitted so far. */\n gridsEmitted: number;\n}\n\n/**\n * The byte length of a `fetch` Response's body when the header says so: its\n * Content-Length, unless a Content-Encoding makes the (decoded) bytes the\n * reader sees differ from it. Null when unknown.\n */\nexport function nvdbResponseLength(response: { headers: { get(name: string): string | null } }): number | null {\n const encoding = response.headers.get(\"content-encoding\");\n if (encoding !== null && encoding.trim() !== \"\" && encoding.trim().toLowerCase() !== \"identity\") return null;\n const raw = response.headers.get(\"content-length\");\n if (raw === null || !/^\\d+$/.test(raw.trim())) return null;\n const n = Number(raw);\n return Number.isSafeInteger(n) ? n : null;\n}\n\n/** One grid, emitted by {@link NvdbStreamReader.push} as soon as its data is complete. */\nexport interface NvdbStreamGrid {\n /** Grid number in the file (counted across segments). */\n index: number;\n /** The grid's metadata from the file's FileMetaData. */\n metadata: NvdbGridMetadata;\n /** The decoded grid image (zero-copy view of a buffer of its own). */\n image: Uint32Array;\n /** File bytes pushed when this grid completed (the end of its data). */\n bytesReceived: number;\n /** The expected file size when this grid completed, or null if unknown (see {@link NvdbProgress.bytesTotal}). */\n bytesTotal: number | null;\n /** The preview level from a `<name>.lod<N>` name, or null for a full grid. */\n lod: number | null;\n /** The name without its `.lod<N>` suffix (the full grid's name). */\n baseName: string;\n}\n\nconst DEFAULT_MAX_GRID_BYTES = 1 << 30;\nconst DEFAULT_MAX_NAME_BYTES = 1 << 16;\nconst ZIP_PREFIX = 8;\n\n/** zlib's `compressBound` (the largest `compress()` output for `n` input bytes), plus slack. */\nfunction zipBound(n: number): number {\n return n + Math.floor(n / 4096) + Math.floor(n / 16384) + Math.floor(n / 33554432) + 13 + 64;\n}\n\nconst LOD_SUFFIX = /^(.*)\\.lod(\\d+)$/;\n\n/**\n * The preview name for `name` at `resolution`: `<name>.lod<resolution>` (K4.5).\n *\n * @param name The full grid's name.\n * @param resolution Preview resolution, a positive integer.\n */\nexport function nvdbLodName(name: string, resolution: number): string {\n if (!Number.isInteger(resolution) || resolution < 1) {\n throw new VDBToolsError(\"VDB_BAD_OPTION\", `nvdbLodName: resolution must be a positive integer, got ${resolution}`);\n }\n return `${name}.lod${resolution}`;\n}\n\n/** Splits a `<name>.lod<N>` preview name; a full grid's name gives `lod: null`. */\nexport function parseNvdbLodName(name: string): { baseName: string; lod: number | null } {\n const m = LOD_SUFFIX.exec(name);\n return m ? { baseName: m[1]!, lod: Number(m[2]) } : { baseName: name, lod: null };\n}\n\ntype Phase = \"header\" | \"meta\" | \"name\" | \"zipPrefix\" | \"grid\" | \"bloscPrefix\" | \"bloscChunk\";\n\ninterface PendingGrid {\n meta: ParsedFileMetaData;\n name: string;\n}\n\n/**\n * Push-based `.nvdb` reader: `push(chunk)` returns the grids that chunk\n * completed. Call `end()` when the stream ends to detect truncation.\n */\nexport class NvdbStreamReader {\n readonly #maxGridBytes: number;\n readonly #maxNameBytes: number;\n readonly #onProgress: ((progress: NvdbProgress) => void) | undefined;\n readonly #declaredTotal: number | null;\n #segmentStart = 0;\n #knownEnd: number | null = null;\n\n /** Small fixed-size fields (FileHeader, FileMetaData, ZIP prefix) and names. */\n #scratch = new Uint8Array(FILE_METADATA_SIZE);\n /** The buffer being filled (scratch or a grid payload) and how far. */\n #target: Uint8Array;\n #filled = 0;\n\n #phase: Phase = \"header\";\n #codec: NvdbCodec = NvdbCodec.NONE;\n #segmentGrids = 0;\n #pending: PendingGrid[] = [];\n #nextMeta: ParsedFileMetaData | null = null;\n #gridInSegment = 0;\n #received = 0;\n /** BLOSC: the grid image being filled chunk by chunk, and how much of it is decoded. */\n #gridBuffer: Uint8Array | null = null;\n #gridFilled = 0;\n #segments = 0;\n #emitted = 0;\n #failed: Error | null = null;\n readonly #grids: NvdbGridMetadata[] = [];\n\n /** @param options Size limits, a known total size and a progress callback. */\n constructor(options: NvdbStreamReaderOptions = {}) {\n this.#maxGridBytes = options.maxGridBytes ?? DEFAULT_MAX_GRID_BYTES;\n this.#maxNameBytes = options.maxNameBytes ?? DEFAULT_MAX_NAME_BYTES;\n if (!Number.isSafeInteger(this.#maxGridBytes) || this.#maxGridBytes < GRID_DATA_SIZE) {\n throw new VDBToolsError(\"VDB_BAD_OPTION\", `NvdbStreamReader: maxGridBytes must be an integer >= ${GRID_DATA_SIZE}`);\n }\n if (!Number.isSafeInteger(this.#maxNameBytes) || this.#maxNameBytes < 1) {\n throw new VDBToolsError(\"VDB_BAD_OPTION\", \"NvdbStreamReader: maxNameBytes must be a positive integer\");\n }\n this.#onProgress = options.onProgress;\n this.#declaredTotal = options.bytesTotal ?? null;\n if (this.#declaredTotal !== null && (!Number.isSafeInteger(this.#declaredTotal) || this.#declaredTotal < 0)) {\n throw new VDBToolsError(\"VDB_BAD_OPTION\", \"NvdbStreamReader: bytesTotal must be a non-negative integer\");\n }\n this.#target = this.#scratch.subarray(0, FILE_HEADER_SIZE);\n }\n\n /** The expected file size in bytes, or null while unknown (see {@link NvdbProgress.bytesTotal}). */\n get bytesTotal(): number | null {\n return this.#declaredTotal ?? this.#knownEnd;\n }\n\n /** Progress so far. */\n get progress(): NvdbProgress {\n return { bytesReceived: this.#received, bytesTotal: this.bytesTotal, gridsEmitted: this.#emitted };\n }\n\n /** File bytes pushed so far. */\n get bytesReceived(): number {\n return this.#received;\n }\n\n /** Grids emitted so far. */\n get gridsEmitted(): number {\n return this.#emitted;\n }\n\n /**\n * Metadata of every grid whose FileMetaData has arrived, in file order,\n * including grids whose data has not (a segment lists all its grids before\n * any data, so the full grid's `gridByteSize` is known before its preview\n * is emitted). Names are as stored; see {@link parseNvdbLodName}.\n */\n get grids(): readonly NvdbGridMetadata[] {\n return this.#grids;\n }\n\n /** True between segments: every grid announced so far has been emitted. */\n get atBoundary(): boolean {\n return this.#phase === \"header\" && this.#filled === 0 && this.#segments > 0;\n }\n\n /** Pushes the next bytes of the file; returns the grids they completed, in order. */\n push(chunk: Uint8Array): NvdbStreamGrid[] {\n if (this.#failed) throw this.#failed;\n const out: NvdbStreamGrid[] = [];\n try {\n let at = 0;\n while (at < chunk.byteLength) {\n const take = Math.min(chunk.byteLength - at, this.#target.byteLength - this.#filled);\n this.#target.set(chunk.subarray(at, at + take), this.#filled);\n this.#filled += take;\n at += take;\n this.#received += take;\n if (this.#filled === this.#target.byteLength) this.#advance(out);\n }\n // A zero-length target (an empty name) completes without bytes.\n while (this.#target.byteLength === 0 && this.#filled === 0 && this.#phase !== \"header\") this.#advance(out);\n } catch (err) {\n this.#failed = err instanceof VDBToolsError ? err : new NvdbFormatError((err as Error).message);\n throw this.#failed;\n }\n if (this.#onProgress && chunk.byteLength > 0) this.#onProgress(this.progress);\n return out;\n }\n\n /** Declares the end of the stream; throws `NvdbFormatError` if it stopped inside a segment. */\n end(): void {\n if (this.#failed) throw this.#failed;\n if (this.atBoundary) return;\n this.#failed = new NvdbFormatError(\n `the stream ended after ${this.#received} bytes, in the ${this.#describe()}; the file is truncated.`,\n );\n throw this.#failed;\n }\n\n #describe(): string {\n const grid = this.#segmentFirst() + this.#gridInSegment;\n switch (this.#phase) {\n case \"header\":\n return `FileHeader, ${this.#filled} of ${FILE_HEADER_SIZE} bytes`;\n case \"meta\":\n return `FileMetaData of grid #${this.#grids.length}, ${this.#filled} of ${FILE_METADATA_SIZE} bytes`;\n case \"name\":\n return `name of grid #${this.#grids.length}`;\n case \"zipPrefix\":\n return `ZIP size prefix of grid #${grid}`;\n case \"bloscPrefix\":\n return `BLOSC chunk size of grid #${grid}, ${this.#gridFilled} of ${this.#gridBuffer?.byteLength ?? 0} bytes decoded`;\n case \"bloscChunk\":\n return `BLOSC chunk of grid #${grid}, ${this.#filled} of ${this.#target.byteLength} bytes`;\n case \"grid\":\n return `data of grid #${grid}, ${this.#filled} of ${this.#target.byteLength} bytes`;\n }\n }\n\n #segmentFirst(): number {\n return this.#grids.length - this.#segmentGrids;\n }\n\n #expect(phase: Phase, target: Uint8Array): void {\n this.#phase = phase;\n this.#target = target;\n this.#filled = 0;\n }\n\n #advance(out: NvdbStreamGrid[]): void {\n switch (this.#phase) {\n case \"header\": {\n const magic = String.fromCharCode(...this.#scratch.subarray(0, 8));\n if (!isFileHeaderMagic(magic)) {\n if (magic === \"NanoVDB1\") {\n throw new VDBUnsupportedError(\n \".nvdb stream: this is a raw NanoVDB grid buffer (magic NanoVDB1), not a segment file; read it whole with NvdbFile.fromArrayBuffer.\",\n );\n }\n throw new NvdbFormatError(\n `expected a NanoVDB FileHeader (magic NanoVDB2 or NanoVDB0) at byte ${this.#received - FILE_HEADER_SIZE}, found \"${magic.replace(/[^\\x20-\\x7e]/g, \"?\")}\".`,\n );\n }\n const view = new DataView(this.#scratch.buffer, 0, FILE_HEADER_SIZE);\n const header = readFileHeader(view, 0);\n if (header.gridCount === 0) throw new NvdbFormatError(`segment #${this.#segments} declares 0 grids.`);\n this.#codec = header.codec;\n this.#segmentGrids = header.gridCount;\n this.#segments++;\n this.#segmentStart = this.#received - FILE_HEADER_SIZE;\n this.#pending = [];\n this.#gridInSegment = 0;\n this.#expect(\"meta\", this.#scratch.subarray(0, FILE_METADATA_SIZE));\n return;\n }\n case \"meta\": {\n const i = this.#grids.length;\n const meta = readFileMetaData(new DataView(this.#scratch.buffer, 0, FILE_METADATA_SIZE), 0);\n assertSafeGridSize(meta.gridSize, `grid #${i}'s FileMetaData.gridSize`);\n if (meta.gridSize < GRID_DATA_SIZE) {\n throw new NvdbFormatError(`grid #${i} declares gridSize ${meta.gridSize}, smaller than a ${GRID_DATA_SIZE}-byte GridData header.`);\n }\n if (meta.gridSize > this.#maxGridBytes) {\n throw new NvdbFormatError(\n `grid #${i} declares gridSize ${meta.gridSize} bytes, over maxGridBytes (${this.#maxGridBytes}); raise the option if the file is trusted.`,\n );\n }\n if (meta.nameSize > this.#maxNameBytes) {\n throw new NvdbFormatError(`grid #${i} declares a ${meta.nameSize}-byte name, over maxNameBytes (${this.#maxNameBytes}).`);\n }\n this.#nextMeta = meta;\n this.#expect(\"name\", meta.nameSize <= this.#scratch.byteLength ? this.#scratch.subarray(0, meta.nameSize) : new Uint8Array(meta.nameSize));\n return;\n }\n case \"name\": {\n const meta = this.#nextMeta!;\n const name = decodeCString(this.#target);\n const gridType = gridTypeName(meta.gridTypeId);\n try {\n validateGridType(gridType, name);\n } catch (err) {\n throw new VDBUnsupportedError((err as Error).message);\n }\n this.#pending.push({ meta, name });\n this.#grids.push({\n name,\n gridType,\n gridClass: gridClassName(meta.gridClassId),\n worldBBox: meta.worldBBox,\n indexBBox: meta.indexBBox,\n voxelSize: meta.voxelSize,\n voxelCount: meta.voxelCount,\n gridByteSize: meta.gridSize,\n });\n this.#nextMeta = null;\n if (this.#pending.length < this.#segmentGrids) this.#expect(\"meta\", this.#scratch.subarray(0, FILE_METADATA_SIZE));\n else {\n let end = this.#segmentStart + FILE_HEADER_SIZE;\n for (const g of this.#pending) end += FILE_METADATA_SIZE + g.meta.nameSize + g.meta.fileSize;\n this.#knownEnd = Number.isSafeInteger(end) ? end : null;\n this.#startGrid();\n }\n return;\n }\n case \"zipPrefix\": {\n const { meta, name } = this.#pending[this.#gridInSegment]!;\n const size = Number(new DataView(this.#scratch.buffer, 0, ZIP_PREFIX).getBigUint64(0, true));\n const bound = zipBound(meta.gridSize);\n if (!Number.isSafeInteger(size) || size < 1 || size > bound) {\n throw new NvdbFormatError(\n `grid #${this.#segmentFirst() + this.#gridInSegment} (\"${name}\") declares a ${size}-byte ZIP payload for a ` +\n `${meta.gridSize}-byte grid (zlib's bound is ${bound}).`,\n );\n }\n this.#expect(\"grid\", new Uint8Array(size));\n return;\n }\n case \"bloscPrefix\": {\n const { name } = this.#pending[this.#gridInSegment]!;\n const index = this.#segmentFirst() + this.#gridInSegment;\n const size = Number(new DataView(this.#scratch.buffer, 0, ZIP_PREFIX).getBigUint64(0, true));\n checkBloscChunkSize(size, this.#gridBuffer!.byteLength - this.#gridFilled, index, name);\n this.#expect(\"bloscChunk\", new Uint8Array(size));\n return;\n }\n case \"bloscChunk\": {\n const { name } = this.#pending[this.#gridInSegment]!;\n const index = this.#segmentFirst() + this.#gridInSegment;\n const grid = this.#gridBuffer!;\n this.#gridFilled += decodeBloscChunk(this.#target, grid.subarray(this.#gridFilled), index, name);\n if (this.#gridFilled < grid.byteLength) {\n this.#expect(\"bloscPrefix\", this.#scratch.subarray(0, ZIP_PREFIX));\n return;\n }\n this.#gridBuffer = null;\n this.#finishGrid(out, grid);\n return;\n }\n case \"grid\": {\n const { meta, name } = this.#pending[this.#gridInSegment]!;\n const index = this.#segmentFirst() + this.#gridInSegment;\n this.#finishGrid(out, this.#codec === NvdbCodec.ZIP ? inflateZipGrid(this.#target, meta.gridSize, index, name) : this.#target);\n return;\n }\n }\n }\n\n /** Emits the current grid from its decoded image and moves to the next grid or segment. */\n #finishGrid(out: NvdbStreamGrid[], bytes: Uint8Array): void {\n const k = this.#gridInSegment;\n const index = this.#segmentFirst() + k;\n const { meta, name } = this.#pending[k]!;\n const record = finishGridRecord(meta, name, index, bytes);\n this.#grids[index] = record.metadata;\n const { baseName, lod } = parseNvdbLodName(name);\n out.push({ index, metadata: record.metadata, image: record.image, bytesReceived: this.#received, bytesTotal: this.bytesTotal, lod, baseName });\n this.#emitted++;\n this.#gridInSegment++;\n if (this.#gridInSegment < this.#segmentGrids) this.#startGrid();\n else {\n this.#pending = [];\n this.#expect(\"header\", this.#scratch.subarray(0, FILE_HEADER_SIZE));\n }\n }\n\n #startGrid(): void {\n const { meta } = this.#pending[this.#gridInSegment]!;\n if (this.#codec === NvdbCodec.ZIP) this.#expect(\"zipPrefix\", this.#scratch.subarray(0, ZIP_PREFIX));\n else if (this.#codec === NvdbCodec.BLOSC) {\n // gridSize was checked against maxGridBytes with its FileMetaData; chunks decode into this.\n this.#gridBuffer = new Uint8Array(meta.gridSize);\n this.#gridFilled = 0;\n this.#expect(\"bloscPrefix\", this.#scratch.subarray(0, ZIP_PREFIX));\n } else this.#expect(\"grid\", new Uint8Array(meta.gridSize));\n }\n}\n\n/** What {@link readNvdbStream} reads: a `fetch` Response (its Content-Length feeds `bytesTotal`), its body, or chunks. */\nexport type NvdbStreamSource =\n | { body: ReadableStream<Uint8Array> | null; headers: { get(name: string): string | null } }\n | ReadableStream<Uint8Array>\n | AsyncIterable<Uint8Array>;\n\nfunction isResponse(source: NvdbStreamSource): source is Extract<NvdbStreamSource, { headers: unknown }> {\n return typeof (source as { headers?: unknown }).headers === \"object\" && \"body\" in source;\n}\n\n/**\n * Reads a byte stream (a `fetch` Response or body, or any async iterable of chunks) through\n * an {@link NvdbStreamReader}, yielding each grid as it completes. Stops\n * reading (and cancels the stream) when the consumer breaks out of the loop.\n *\n * @param source A `Response`, a response body, or an async iterable of byte chunks.\n * @param options Size limits, a known total size and a progress callback.\n */\nexport async function* readNvdbStream(\n source: NvdbStreamSource,\n options: NvdbStreamReaderOptions = {},\n): AsyncGenerator<NvdbStreamGrid, void, undefined> {\n if (isResponse(source)) {\n const len = nvdbResponseLength(source);\n if (source.body === null) throw new NvdbFormatError(\"readNvdbStream: the response has no body.\");\n options = len !== null && options.bytesTotal === undefined ? { ...options, bytesTotal: len } : options;\n source = source.body;\n }\n const reader = new NvdbStreamReader(options);\n if (typeof (source as ReadableStream<Uint8Array>).getReader === \"function\") {\n const r = (source as ReadableStream<Uint8Array>).getReader();\n let finished = false;\n try {\n for (;;) {\n const { done, value } = await r.read();\n if (done) break;\n yield* reader.push(value);\n }\n finished = true;\n reader.end();\n } finally {\n if (!finished) await r.cancel().catch(() => undefined);\n r.releaseLock();\n }\n return;\n }\n for await (const chunk of source as AsyncIterable<Uint8Array>) yield* reader.push(chunk);\n reader.end();\n}\n"]}
@@ -0,0 +1,41 @@
1
+ /**
2
+ * K6.R: closed forms and high-precision quadratures the reference renderer is checked against (homogeneous media in
3
+ * one box or ball).
4
+ *
5
+ * - Beer–Lambert: T = exp(−σ · chord).
6
+ * - Emission only (albedo 0): L = Le · (1 − T) (the renderer's source term σt · Le).
7
+ * - Single scattering of a directional light: L = ∫ σ · albedo · p(cos θ) · E · exp(−σ (t − t₀)) · exp(−σ ℓ(t)) dt
8
+ * over the camera ray's interval, ℓ(t) the distance from the point to the medium's exit toward the light
9
+ * (composite Simpson, n panels).
10
+ * - White furnace: albedo 1 under a uniform environment Lₑ (and no other light): L + T · Lₑ = Lₑ.
11
+ * - K6.R1: single scattering of a point light (three's falloff or none) and with a tabulated phase.
12
+ */
13
+ import { type VDBRefVec3, type VDBReferenceFalloff, type VDBReferencePhase } from "./scene.js";
14
+ /** A homogeneous medium in one box or ball. */
15
+ export interface VDBRefHomogeneous {
16
+ localToWorld: ArrayLike<number>;
17
+ sphere: boolean;
18
+ /** σt per world unit. */
19
+ sigma: number;
20
+ }
21
+ /** The chord [t₀, t₁] of the ray o + t·d (|d| = 1) in the medium, or null. */
22
+ export declare function vdbRefChord(m: VDBRefHomogeneous, o: readonly number[], d: readonly number[]): [number, number] | null;
23
+ /** Beer–Lambert transmittance of the ray through the medium. */
24
+ export declare function vdbRefBeerLambert(m: VDBRefHomogeneous, o: readonly number[], d: readonly number[]): number;
25
+ /** Radiance of a purely emitting medium (albedo 0) of emitted radiance Le: Le · (1 − T). */
26
+ export declare function vdbRefEmissionOnly(m: VDBRefHomogeneous, o: readonly number[], d: readonly number[], le: VDBRefVec3): VDBRefVec3;
27
+ /**
28
+ * Single-scattered radiance of a directional light (irradiance `e`, `toLight` unit) along o + t·d (|d| = 1) in a
29
+ * homogeneous medium of scattering albedo `albedo`, phase (g, backG, backWeight); composite Simpson, `n` panels.
30
+ */
31
+ export declare function vdbRefSingleScatter(m: VDBRefHomogeneous, o: readonly number[], d: readonly number[], toLight: readonly number[], e: VDBRefVec3, albedo: VDBRefVec3, phase: VDBReferencePhase, n?: number): VDBRefVec3;
32
+ /**
33
+ * K6.R1: single-scattered radiance of a point light (`intensity` at `position`, `falloff` three's attenuation or
34
+ * none) along o + t·d (|d| = 1) in a homogeneous medium; composite Simpson, `n` panels. The light's transmittance
35
+ * is exp(−σ · the part of the segment x → light inside the medium).
36
+ */
37
+ export declare function vdbRefSingleScatterPoint(m: VDBRefHomogeneous, o: readonly number[], d: readonly number[], light: {
38
+ position: VDBRefVec3;
39
+ intensity: VDBRefVec3;
40
+ falloff?: VDBReferenceFalloff;
41
+ }, albedo: VDBRefVec3, phase: VDBReferencePhase, n?: number): VDBRefVec3;
@@ -0,0 +1,95 @@
1
+ /**
2
+ * K6.R: closed forms and high-precision quadratures the reference renderer is checked against (homogeneous media in
3
+ * one box or ball).
4
+ *
5
+ * - Beer–Lambert: T = exp(−σ · chord).
6
+ * - Emission only (albedo 0): L = Le · (1 − T) (the renderer's source term σt · Le).
7
+ * - Single scattering of a directional light: L = ∫ σ · albedo · p(cos θ) · E · exp(−σ (t − t₀)) · exp(−σ ℓ(t)) dt
8
+ * over the camera ray's interval, ℓ(t) the distance from the point to the medium's exit toward the light
9
+ * (composite Simpson, n panels).
10
+ * - White furnace: albedo 1 under a uniform environment Lₑ (and no other light): L + T · Lₑ = Lₑ.
11
+ * - K6.R1: single scattering of a point light (three's falloff or none) and with a tabulated phase.
12
+ */
13
+ import { vdbRefDistanceAttenuation, vdbRefInvert4 } from "./scene.js";
14
+ import { vdbRefPhaseTableEval } from "./tables.js";
15
+ import { vdbRefPhase, vdbRefVolumeInterval } from "./trace.js";
16
+ const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
17
+ /** The chord [t₀, t₁] of the ray o + t·d (|d| = 1) in the medium, or null. */
18
+ export function vdbRefChord(m, o, d) {
19
+ const [t0, t1] = vdbRefVolumeInterval(vdbRefInvert4(m.localToWorld), m.sphere, o, d, 1e30);
20
+ return t0 < t1 ? [t0, t1] : null;
21
+ }
22
+ /** Beer–Lambert transmittance of the ray through the medium. */
23
+ export function vdbRefBeerLambert(m, o, d) {
24
+ const c = vdbRefChord(m, o, d);
25
+ return c ? Math.exp(-m.sigma * (c[1] - c[0])) : 1;
26
+ }
27
+ /** Radiance of a purely emitting medium (albedo 0) of emitted radiance Le: Le · (1 − T). */
28
+ export function vdbRefEmissionOnly(m, o, d, le) {
29
+ const a = 1 - vdbRefBeerLambert(m, o, d);
30
+ return [le[0] * a, le[1] * a, le[2] * a];
31
+ }
32
+ /**
33
+ * Single-scattered radiance of a directional light (irradiance `e`, `toLight` unit) along o + t·d (|d| = 1) in a
34
+ * homogeneous medium of scattering albedo `albedo`, phase (g, backG, backWeight); composite Simpson, `n` panels.
35
+ */
36
+ export function vdbRefSingleScatter(m, o, d, toLight, e, albedo, phase, n = 4096) {
37
+ const c = vdbRefChord(m, o, d);
38
+ if (!c)
39
+ return [0, 0, 0];
40
+ const w2l = vdbRefInvert4(m.localToWorld);
41
+ const pc = phaseOf(phase, dot(d, toLight));
42
+ const f = (t) => {
43
+ const x = [o[0] + t * d[0], o[1] + t * d[1], o[2] + t * d[2]];
44
+ const [, exit] = vdbRefVolumeInterval(w2l, m.sphere, x, toLight, 1e30);
45
+ const l = Math.max(exit, 0);
46
+ return Math.exp(-m.sigma * (t - c[0])) * Math.exp(-m.sigma * l);
47
+ };
48
+ const steps = n % 2 === 0 ? n : n + 1;
49
+ const h = (c[1] - c[0]) / steps;
50
+ let s = f(c[0]) + f(c[1]);
51
+ for (let i = 1; i < steps; i++)
52
+ s += (i % 2 ? 4 : 2) * f(c[0] + i * h);
53
+ const integral = (s * h) / 3;
54
+ return [0, 1, 2].map((k) => m.sigma * albedo[k] * pc[k] * e[k] * integral);
55
+ }
56
+ /** The phase per steradian per channel (HG / dual-lobe, or the table). */
57
+ function phaseOf(phase, cosTheta) {
58
+ if (phase.table)
59
+ return vdbRefPhaseTableEval(phase.table, cosTheta);
60
+ const p = vdbRefPhase(cosTheta, phase.g, phase.backG ?? 0, phase.backWeight ?? 0);
61
+ return [p, p, p];
62
+ }
63
+ /**
64
+ * K6.R1: single-scattered radiance of a point light (`intensity` at `position`, `falloff` three's attenuation or
65
+ * none) along o + t·d (|d| = 1) in a homogeneous medium; composite Simpson, `n` panels. The light's transmittance
66
+ * is exp(−σ · the part of the segment x → light inside the medium).
67
+ */
68
+ export function vdbRefSingleScatterPoint(m, o, d, light, albedo, phase, n = 4096) {
69
+ const c = vdbRefChord(m, o, d);
70
+ if (!c)
71
+ return [0, 0, 0];
72
+ const w2l = vdbRefInvert4(m.localToWorld);
73
+ const f = (t) => {
74
+ const x = [o[0] + t * d[0], o[1] + t * d[1], o[2] + t * d[2]];
75
+ const to = [light.position[0] - x[0], light.position[1] - x[1], light.position[2] - x[2]];
76
+ const dist = Math.hypot(to[0], to[1], to[2]);
77
+ const dir = [to[0] / dist, to[1] / dist, to[2] / dist];
78
+ const [t0, t1] = vdbRefVolumeInterval(w2l, m.sphere, x, dir, dist);
79
+ const inside = t0 < t1 ? t1 - t0 : 0;
80
+ const k = (light.falloff ? vdbRefDistanceAttenuation(dist, light.falloff) : 1) * Math.exp(-m.sigma * (t - c[0])) * Math.exp(-m.sigma * inside);
81
+ const p = phaseOf(phase, dot(d, dir));
82
+ return [p[0] * k, p[1] * k, p[2] * k];
83
+ };
84
+ const steps = n % 2 === 0 ? n : n + 1;
85
+ const h = (c[1] - c[0]) / steps;
86
+ const s = [0, 0, 0];
87
+ for (let i = 0; i <= steps; i++) {
88
+ const w = i === 0 || i === steps ? 1 : i % 2 ? 4 : 2;
89
+ const v = f(c[0] + i * h);
90
+ for (let k = 0; k < 3; k++)
91
+ s[k] = s[k] + w * v[k];
92
+ }
93
+ return [0, 1, 2].map((k) => (m.sigma * albedo[k] * light.intensity[k] * s[k] * h) / 3);
94
+ }
95
+ //# sourceMappingURL=analytic.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"analytic.js","sourceRoot":"","sources":["../../../src/tools/reference/analytic.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,yBAAyB,EAAE,aAAa,EAAqE,MAAM,YAAY,CAAC;AACzI,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAU/D,MAAM,GAAG,GAAG,CAAC,CAAoB,EAAE,CAAoB,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAE,GAAG,CAAC,CAAC,CAAC,CAAE,GAAG,CAAC,CAAC,CAAC,CAAE,GAAG,CAAC,CAAC,CAAC,CAAE,GAAG,CAAC,CAAC,CAAC,CAAE,GAAG,CAAC,CAAC,CAAC,CAAE,CAAC;AAE1G,8EAA8E;AAC9E,MAAM,UAAU,WAAW,CAAC,CAAoB,EAAE,CAAoB,EAAE,CAAoB;IAC1F,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG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GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;QAC1B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;YAAE,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAE,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAE,CAAC;IACvD,CAAC;IACD,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,MAAM,CAAC,CAAC,CAAE,GAAG,KAAK,CAAC,SAAS,CAAC,CAAC,CAAE,GAAG,CAAC,CAAC,CAAC,CAAE,GAAG,CAAC,CAAC,GAAG,CAAC,CAAe,CAAC;AAC1G,CAAC","sourcesContent":["/**\n * K6.R: closed forms and high-precision quadratures the reference renderer is checked against (homogeneous media in\n * one box or ball).\n *\n * - Beer–Lambert: T = exp(−σ · chord).\n * - Emission only (albedo 0): L = Le · (1 − T) (the renderer's source term σt · Le).\n * - Single scattering of a directional light: L = ∫ σ · albedo · p(cos θ) · E · exp(−σ (t − t₀)) · exp(−σ ℓ(t)) dt\n * over the camera ray's interval, ℓ(t) the distance from the point to the medium's exit toward the light\n * (composite Simpson, n panels).\n * - White furnace: albedo 1 under a uniform environment Lₑ (and no other light): L + T · Lₑ = Lₑ.\n * - K6.R1: single scattering of a point light (three's falloff or none) and with a tabulated phase.\n */\n\nimport { vdbRefDistanceAttenuation, vdbRefInvert4, type VDBRefVec3, type VDBReferenceFalloff, type VDBReferencePhase } from \"./scene.js\";\nimport { vdbRefPhaseTableEval } from \"./tables.js\";\nimport { vdbRefPhase, vdbRefVolumeInterval } from \"./trace.js\";\n\n/** A homogeneous medium in one box or ball. */\nexport interface VDBRefHomogeneous {\n localToWorld: ArrayLike<number>;\n sphere: boolean;\n /** σt per world unit. */\n sigma: number;\n}\n\nconst dot = (a: readonly number[], b: readonly number[]) => a[0]! * b[0]! + a[1]! * b[1]! + a[2]! * b[2]!;\n\n/** The chord [t₀, t₁] of the ray o + t·d (|d| = 1) in the medium, or null. */\nexport function vdbRefChord(m: VDBRefHomogeneous, o: readonly number[], d: readonly number[]): [number, number] | null {\n const [t0, t1] = vdbRefVolumeInterval(vdbRefInvert4(m.localToWorld), m.sphere, o, d, 1e30);\n return t0 < t1 ? [t0, t1] : null;\n}\n\n/** Beer–Lambert transmittance of the ray through the medium. */\nexport function vdbRefBeerLambert(m: VDBRefHomogeneous, o: readonly number[], d: readonly number[]): number {\n const c = vdbRefChord(m, o, d);\n return c ? Math.exp(-m.sigma * (c[1] - c[0])) : 1;\n}\n\n/** Radiance of a purely emitting medium (albedo 0) of emitted radiance Le: Le · (1 − T). */\nexport function vdbRefEmissionOnly(m: VDBRefHomogeneous, o: readonly number[], d: readonly number[], le: VDBRefVec3): VDBRefVec3 {\n const a = 1 - vdbRefBeerLambert(m, o, d);\n return [le[0] * a, le[1] * a, le[2] * a];\n}\n\n/**\n * Single-scattered radiance of a directional light (irradiance `e`, `toLight` unit) along o + t·d (|d| = 1) in a\n * homogeneous medium of scattering albedo `albedo`, phase (g, backG, backWeight); composite Simpson, `n` panels.\n */\nexport function vdbRefSingleScatter(\n m: VDBRefHomogeneous,\n o: readonly number[],\n d: readonly number[],\n toLight: readonly number[],\n e: VDBRefVec3,\n albedo: VDBRefVec3,\n phase: VDBReferencePhase,\n n = 4096,\n): VDBRefVec3 {\n const c = vdbRefChord(m, o, d);\n if (!c) return [0, 0, 0];\n const w2l = vdbRefInvert4(m.localToWorld);\n const pc = phaseOf(phase, dot(d, toLight));\n const f = (t: number) => {\n const x = [o[0]! + t * d[0]!, o[1]! + t * d[1]!, o[2]! + t * d[2]!];\n const [, exit] = vdbRefVolumeInterval(w2l, m.sphere, x, toLight, 1e30);\n const l = Math.max(exit, 0);\n return Math.exp(-m.sigma * (t - c[0])) * Math.exp(-m.sigma * l);\n };\n const steps = n % 2 === 0 ? n : n + 1;\n const h = (c[1] - c[0]) / steps;\n let s = f(c[0]) + f(c[1]);\n for (let i = 1; i < steps; i++) s += (i % 2 ? 4 : 2) * f(c[0] + i * h);\n const integral = (s * h) / 3;\n return [0, 1, 2].map((k) => m.sigma * albedo[k]! * pc[k]! * e[k]! * integral) as VDBRefVec3;\n}\n\n/** The phase per steradian per channel (HG / dual-lobe, or the table). */\nfunction phaseOf(phase: VDBReferencePhase, cosTheta: number): VDBRefVec3 {\n if (phase.table) return vdbRefPhaseTableEval(phase.table, cosTheta);\n const p = vdbRefPhase(cosTheta, phase.g, phase.backG ?? 0, phase.backWeight ?? 0);\n return [p, p, p];\n}\n\n/**\n * K6.R1: single-scattered radiance of a point light (`intensity` at `position`, `falloff` three's attenuation or\n * none) along o + t·d (|d| = 1) in a homogeneous medium; composite Simpson, `n` panels. The light's transmittance\n * is exp(−σ · the part of the segment x → light inside the medium).\n */\nexport function vdbRefSingleScatterPoint(\n m: VDBRefHomogeneous,\n o: readonly number[],\n d: readonly number[],\n light: { position: VDBRefVec3; intensity: VDBRefVec3; falloff?: VDBReferenceFalloff },\n albedo: VDBRefVec3,\n phase: VDBReferencePhase,\n n = 4096,\n): VDBRefVec3 {\n const c = vdbRefChord(m, o, d);\n if (!c) return [0, 0, 0];\n const w2l = vdbRefInvert4(m.localToWorld);\n const f = (t: number): VDBRefVec3 => {\n const x = [o[0]! + t * d[0]!, o[1]! + t * d[1]!, o[2]! + t * d[2]!];\n const to = [light.position[0] - x[0]!, light.position[1] - x[1]!, light.position[2] - x[2]!];\n const dist = Math.hypot(to[0]!, to[1]!, to[2]!);\n const dir = [to[0]! / dist, to[1]! / dist, to[2]! / dist];\n const [t0, t1] = vdbRefVolumeInterval(w2l, m.sphere, x, dir, dist);\n const inside = t0 < t1 ? t1 - t0 : 0;\n const k = (light.falloff ? vdbRefDistanceAttenuation(dist, light.falloff) : 1) * Math.exp(-m.sigma * (t - c[0])) * Math.exp(-m.sigma * inside);\n const p = phaseOf(phase, dot(d, dir));\n return [p[0] * k, p[1] * k, p[2] * k];\n };\n const steps = n % 2 === 0 ? n : n + 1;\n const h = (c[1] - c[0]) / steps;\n const s: VDBRefVec3 = [0, 0, 0];\n for (let i = 0; i <= steps; i++) {\n const w = i === 0 || i === steps ? 1 : i % 2 ? 4 : 2;\n const v = f(c[0] + i * h);\n for (let k = 0; k < 3; k++) s[k] = s[k]! + w * v[k]!;\n }\n return [0, 1, 2].map((k) => (m.sigma * albedo[k]! * light.intensity[k]! * s[k]! * h) / 3) as VDBRefVec3;\n}\n"]}