@pmndrs/vdb 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +137 -0
- package/LICENSE +21 -0
- package/README.md +448 -2
- package/THIRD_PARTY_NOTICES.md +147 -0
- package/dist/core/compute/atlas.d.ts +94 -0
- package/dist/core/compute/atlas.js +98 -0
- package/dist/core/compute/atlas.js.map +1 -0
- package/dist/core/compute/index.d.ts +13 -0
- package/dist/core/compute/index.js +9 -0
- package/dist/core/compute/index.js.map +1 -0
- package/dist/core/compute/pyramid.d.ts +105 -0
- package/dist/core/compute/pyramid.js +455 -0
- package/dist/core/compute/pyramid.js.map +1 -0
- package/dist/core/compute/shared.d.ts +115 -0
- package/dist/core/compute/shared.js +211 -0
- package/dist/core/compute/shared.js.map +1 -0
- package/dist/core/compute/stats.d.ts +71 -0
- package/dist/core/compute/stats.js +188 -0
- package/dist/core/compute/stats.js.map +1 -0
- package/dist/core/compute/value-transform.d.ts +23 -0
- package/dist/core/compute/value-transform.js +58 -0
- package/dist/core/compute/value-transform.js.map +1 -0
- package/dist/core/errors.d.ts +11 -0
- package/dist/core/errors.js +9 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/index.d.ts +30 -0
- package/dist/core/index.js +27 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/internal.d.ts +15 -0
- package/dist/core/internal.js +15 -0
- package/dist/core/internal.js.map +1 -0
- package/dist/core/pool/affine.d.ts +34 -0
- package/dist/core/pool/affine.js +100 -0
- package/dist/core/pool/affine.js.map +1 -0
- package/dist/core/pool/allocator.d.ts +32 -0
- package/dist/core/pool/allocator.js +81 -0
- package/dist/core/pool/allocator.js.map +1 -0
- package/dist/core/pool/image-header.d.ts +82 -0
- package/dist/core/pool/image-header.js +175 -0
- package/dist/core/pool/image-header.js.map +1 -0
- package/dist/core/pool/layout.d.ts +164 -0
- package/dist/core/pool/layout.js +267 -0
- package/dist/core/pool/layout.js.map +1 -0
- package/dist/core/pool/limits.d.ts +40 -0
- package/dist/core/pool/limits.js +79 -0
- package/dist/core/pool/limits.js.map +1 -0
- package/dist/core/pool/pool.d.ts +279 -0
- package/dist/core/pool/pool.js +953 -0
- package/dist/core/pool/pool.js.map +1 -0
- package/dist/core/source/dense.d.ts +19 -0
- package/dist/core/source/dense.js +287 -0
- package/dist/core/source/dense.js.map +1 -0
- package/dist/core/source/types.d.ts +63 -0
- package/dist/core/source/types.js +9 -0
- package/dist/core/source/types.js.map +1 -0
- package/dist/core/types.d.ts +28 -0
- package/dist/core/types.js +28 -0
- package/dist/core/types.js.map +1 -0
- package/dist/core/wgsl/api.d.ts +41 -0
- package/dist/core/wgsl/api.js +463 -0
- package/dist/core/wgsl/api.js.map +1 -0
- package/dist/core/wgsl/generate.d.ts +82 -0
- package/dist/core/wgsl/generate.js +505 -0
- package/dist/core/wgsl/generate.js.map +1 -0
- package/dist/core/wgsl/level-set.d.ts +54 -0
- package/dist/core/wgsl/level-set.js +194 -0
- package/dist/core/wgsl/level-set.js.map +1 -0
- package/dist/core/wgsl/pnanovdb.chunks.generated.d.ts +14 -0
- package/dist/core/wgsl/pnanovdb.chunks.generated.js +330 -0
- package/dist/core/wgsl/pnanovdb.chunks.generated.js.map +1 -0
- package/dist/core/wgsl/reserved.d.ts +9 -0
- package/dist/core/wgsl/reserved.js +26 -0
- package/dist/core/wgsl/reserved.js.map +1 -0
- package/dist/core/wgsl/types.d.ts +104 -0
- package/dist/core/wgsl/types.js +2 -0
- package/dist/core/wgsl/types.js.map +1 -0
- package/dist/three/coarse-first.d.ts +162 -0
- package/dist/three/coarse-first.js +187 -0
- package/dist/three/coarse-first.js.map +1 -0
- package/dist/three/compute.d.ts +63 -0
- package/dist/three/compute.js +56 -0
- package/dist/three/compute.js.map +1 -0
- package/dist/three/device.d.ts +59 -0
- package/dist/three/device.js +59 -0
- package/dist/three/device.js.map +1 -0
- package/dist/three/frame/blackbody.d.ts +42 -0
- package/dist/three/frame/blackbody.js +74 -0
- package/dist/three/frame/blackbody.js.map +1 -0
- package/dist/three/frame/chunks.d.ts +157 -0
- package/dist/three/frame/chunks.js +559 -0
- package/dist/three/frame/chunks.js.map +1 -0
- package/dist/three/frame/density-material.d.ts +53 -0
- package/dist/three/frame/density-material.js +353 -0
- package/dist/three/frame/density-material.js.map +1 -0
- package/dist/three/frame/emission-light.d.ts +167 -0
- package/dist/three/frame/emission-light.js +655 -0
- package/dist/three/frame/emission-light.js.map +1 -0
- package/dist/three/frame/environment-grid-material.d.ts +21 -0
- package/dist/three/frame/environment-grid-material.js +184 -0
- package/dist/three/frame/environment-grid-material.js.map +1 -0
- package/dist/three/frame/environment-material.d.ts +22 -0
- package/dist/three/frame/environment-material.js +87 -0
- package/dist/three/frame/environment-material.js.map +1 -0
- package/dist/three/frame/grid-mip-material.d.ts +12 -0
- package/dist/three/frame/grid-mip-material.js +61 -0
- package/dist/three/frame/grid-mip-material.js.map +1 -0
- package/dist/three/frame/index.d.ts +39 -0
- package/dist/three/frame/index.js +35 -0
- package/dist/three/frame/index.js.map +1 -0
- package/dist/three/frame/inject-material.d.ts +56 -0
- package/dist/three/frame/inject-material.js +424 -0
- package/dist/three/frame/inject-material.js.map +1 -0
- package/dist/three/frame/integrate-material.d.ts +26 -0
- package/dist/three/frame/integrate-material.js +198 -0
- package/dist/three/frame/integrate-material.js.map +1 -0
- package/dist/three/frame/layout.d.ts +34 -0
- package/dist/three/frame/layout.js +48 -0
- package/dist/three/frame/layout.js.map +1 -0
- package/dist/three/frame/lighting-material.d.ts +41 -0
- package/dist/three/frame/lighting-material.js +257 -0
- package/dist/three/frame/lighting-material.js.map +1 -0
- package/dist/three/frame/lights.d.ts +11 -0
- package/dist/three/frame/lights.js +16 -0
- package/dist/three/frame/lights.js.map +1 -0
- package/dist/three/frame/medium.d.ts +21 -0
- package/dist/three/frame/medium.js +29 -0
- package/dist/three/frame/medium.js.map +1 -0
- package/dist/three/frame/neural.d.ts +14 -0
- package/dist/three/frame/neural.js +105 -0
- package/dist/three/frame/neural.js.map +1 -0
- package/dist/three/frame/phase.d.ts +44 -0
- package/dist/three/frame/phase.js +80 -0
- package/dist/three/frame/phase.js.map +1 -0
- package/dist/three/frame/renderer.d.ts +493 -0
- package/dist/three/frame/renderer.js +1498 -0
- package/dist/three/frame/renderer.js.map +1 -0
- package/dist/three/frame/shadow-density-material.d.ts +30 -0
- package/dist/three/frame/shadow-density-material.js +123 -0
- package/dist/three/frame/shadow-density-material.js.map +1 -0
- package/dist/three/frame/spectrum.d.ts +18 -0
- package/dist/three/frame/spectrum.js +36 -0
- package/dist/three/frame/spectrum.js.map +1 -0
- package/dist/three/frame/temporal-material.d.ts +29 -0
- package/dist/three/frame/temporal-material.js +152 -0
- package/dist/three/frame/temporal-material.js.map +1 -0
- package/dist/three/frame/three-internals.d.ts +65 -0
- package/dist/three/frame/three-internals.js +150 -0
- package/dist/three/frame/three-internals.js.map +1 -0
- package/dist/three/frame/transmittance-material.d.ts +31 -0
- package/dist/three/frame/transmittance-material.js +315 -0
- package/dist/three/frame/transmittance-material.js.map +1 -0
- package/dist/three/frame/view.d.ts +103 -0
- package/dist/three/frame/view.js +296 -0
- package/dist/three/frame/view.js.map +1 -0
- package/dist/three/frame/volume-shadow-material.d.ts +36 -0
- package/dist/three/frame/volume-shadow-material.js +128 -0
- package/dist/three/frame/volume-shadow-material.js.map +1 -0
- package/dist/three/frame/volume-shadows.d.ts +153 -0
- package/dist/three/frame/volume-shadows.js +756 -0
- package/dist/three/frame/volume-shadows.js.map +1 -0
- package/dist/three/frame/volume.d.ts +172 -0
- package/dist/three/frame/volume.js +256 -0
- package/dist/three/frame/volume.js.map +1 -0
- package/dist/three/grid-node.d.ts +196 -0
- package/dist/three/grid-node.js +510 -0
- package/dist/three/grid-node.js.map +1 -0
- package/dist/three/index.d.ts +43 -0
- package/dist/three/index.js +49 -0
- package/dist/three/index.js.map +1 -0
- package/dist/three/internal.d.ts +14 -0
- package/dist/three/internal.js +14 -0
- package/dist/three/internal.js.map +1 -0
- package/dist/three/level-set-material.d.ts +112 -0
- package/dist/three/level-set-material.js +204 -0
- package/dist/three/level-set-material.js.map +1 -0
- package/dist/three/material.d.ts +194 -0
- package/dist/three/material.js +359 -0
- package/dist/three/material.js.map +1 -0
- package/dist/three/points.d.ts +84 -0
- package/dist/three/points.js +130 -0
- package/dist/three/points.js.map +1 -0
- package/dist/three/sequence.d.ts +317 -0
- package/dist/three/sequence.js +526 -0
- package/dist/three/sequence.js.map +1 -0
- package/dist/three/sparse-source.d.ts +233 -0
- package/dist/three/sparse-source.js +876 -0
- package/dist/three/sparse-source.js.map +1 -0
- package/dist/three/volume-source.d.ts +167 -0
- package/dist/three/volume-source.js +353 -0
- package/dist/three/volume-source.js.map +1 -0
- package/dist/tools/cpu/compute.d.ts +97 -0
- package/dist/tools/cpu/compute.js +218 -0
- package/dist/tools/cpu/compute.js.map +1 -0
- package/dist/tools/cpu/level-set.d.ts +72 -0
- package/dist/tools/cpu/level-set.js +261 -0
- package/dist/tools/cpu/level-set.js.map +1 -0
- package/dist/tools/cpu/march.d.ts +206 -0
- package/dist/tools/cpu/march.js +575 -0
- package/dist/tools/cpu/march.js.map +1 -0
- package/dist/tools/cpu/probe-coords.d.ts +70 -0
- package/dist/tools/cpu/probe-coords.js +116 -0
- package/dist/tools/cpu/probe-coords.js.map +1 -0
- package/dist/tools/cpu/read-value.d.ts +31 -0
- package/dist/tools/cpu/read-value.js +336 -0
- package/dist/tools/cpu/read-value.js.map +1 -0
- package/dist/tools/cpu/sample-trilinear.d.ts +30 -0
- package/dist/tools/cpu/sample-trilinear.js +156 -0
- package/dist/tools/cpu/sample-trilinear.js.map +1 -0
- package/dist/tools/cpu/stride-tables.d.ts +56 -0
- package/dist/tools/cpu/stride-tables.generated.d.ts +1058 -0
- package/dist/tools/cpu/stride-tables.generated.js +1266 -0
- package/dist/tools/cpu/stride-tables.generated.js.map +1 -0
- package/dist/tools/cpu/stride-tables.js +78 -0
- package/dist/tools/cpu/stride-tables.js.map +1 -0
- package/dist/tools/errors.d.ts +57 -0
- package/dist/tools/errors.js +58 -0
- package/dist/tools/errors.js.map +1 -0
- package/dist/tools/frame/blackbody.d.ts +42 -0
- package/dist/tools/frame/blackbody.js +116 -0
- package/dist/tools/frame/blackbody.js.map +1 -0
- package/dist/tools/frame/emission.d.ts +128 -0
- package/dist/tools/frame/emission.js +282 -0
- package/dist/tools/frame/emission.js.map +1 -0
- package/dist/tools/frame/environment.d.ts +105 -0
- package/dist/tools/frame/environment.js +268 -0
- package/dist/tools/frame/environment.js.map +1 -0
- package/dist/tools/frame/grid.d.ts +302 -0
- package/dist/tools/frame/grid.js +703 -0
- package/dist/tools/frame/grid.js.map +1 -0
- package/dist/tools/frame/index.d.ts +136 -0
- package/dist/tools/frame/index.js +348 -0
- package/dist/tools/frame/index.js.map +1 -0
- package/dist/tools/frame/medium.d.ts +61 -0
- package/dist/tools/frame/medium.js +99 -0
- package/dist/tools/frame/medium.js.map +1 -0
- package/dist/tools/frame/mie-table.d.ts +6 -0
- package/dist/tools/frame/mie-table.js +16 -0
- package/dist/tools/frame/mie-table.js.map +1 -0
- package/dist/tools/frame/mie.d.ts +54 -0
- package/dist/tools/frame/mie.js +176 -0
- package/dist/tools/frame/mie.js.map +1 -0
- package/dist/tools/frame/neural-weights.d.ts +9 -0
- package/dist/tools/frame/neural-weights.js +245 -0
- package/dist/tools/frame/neural-weights.js.map +1 -0
- package/dist/tools/frame/neural.d.ts +80 -0
- package/dist/tools/frame/neural.js +150 -0
- package/dist/tools/frame/neural.js.map +1 -0
- package/dist/tools/frame/pyramid-layout.d.ts +38 -0
- package/dist/tools/frame/pyramid-layout.js +53 -0
- package/dist/tools/frame/pyramid-layout.js.map +1 -0
- package/dist/tools/frame/shadows.d.ts +80 -0
- package/dist/tools/frame/shadows.js +182 -0
- package/dist/tools/frame/shadows.js.map +1 -0
- package/dist/tools/frame/sparse.d.ts +65 -0
- package/dist/tools/frame/sparse.js +191 -0
- package/dist/tools/frame/sparse.js.map +1 -0
- package/dist/tools/frame/spectral.d.ts +203 -0
- package/dist/tools/frame/spectral.js +550 -0
- package/dist/tools/frame/spectral.js.map +1 -0
- package/dist/tools/index.d.ts +106 -0
- package/dist/tools/index.js +88 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/internal.d.ts +46 -0
- package/dist/tools/internal.js +31 -0
- package/dist/tools/internal.js.map +1 -0
- package/dist/tools/nanovdb/build-from-vdb.d.ts +50 -0
- package/dist/tools/nanovdb/build-from-vdb.js +92 -0
- package/dist/tools/nanovdb/build-from-vdb.js.map +1 -0
- package/dist/tools/nanovdb/bytes.d.ts +220 -0
- package/dist/tools/nanovdb/bytes.js +345 -0
- package/dist/tools/nanovdb/bytes.js.map +1 -0
- package/dist/tools/nanovdb/coarse-first.d.ts +62 -0
- package/dist/tools/nanovdb/coarse-first.js +86 -0
- package/dist/tools/nanovdb/coarse-first.js.map +1 -0
- package/dist/tools/nanovdb/index.d.ts +32 -0
- package/dist/tools/nanovdb/index.js +24 -0
- package/dist/tools/nanovdb/index.js.map +1 -0
- package/dist/tools/nanovdb/inspect.d.ts +24 -0
- package/dist/tools/nanovdb/inspect.js +63 -0
- package/dist/tools/nanovdb/inspect.js.map +1 -0
- package/dist/tools/nanovdb/leaf-codec.d.ts +85 -0
- package/dist/tools/nanovdb/leaf-codec.js +320 -0
- package/dist/tools/nanovdb/leaf-codec.js.map +1 -0
- package/dist/tools/nanovdb/quantize.d.ts +43 -0
- package/dist/tools/nanovdb/quantize.js +277 -0
- package/dist/tools/nanovdb/quantize.js.map +1 -0
- package/dist/tools/nanovdb/resample.d.ts +160 -0
- package/dist/tools/nanovdb/resample.js +679 -0
- package/dist/tools/nanovdb/resample.js.map +1 -0
- package/dist/tools/nanovdb/serialize.d.ts +145 -0
- package/dist/tools/nanovdb/serialize.js +395 -0
- package/dist/tools/nanovdb/serialize.js.map +1 -0
- package/dist/tools/nanovdb/stats.d.ts +58 -0
- package/dist/tools/nanovdb/stats.js +149 -0
- package/dist/tools/nanovdb/stats.js.map +1 -0
- package/dist/tools/nanovdb/transform.d.ts +26 -0
- package/dist/tools/nanovdb/transform.js +91 -0
- package/dist/tools/nanovdb/transform.js.map +1 -0
- package/dist/tools/nanovdb/tree.d.ts +175 -0
- package/dist/tools/nanovdb/tree.js +518 -0
- package/dist/tools/nanovdb/tree.js.map +1 -0
- package/dist/tools/nanovdb/write-nvdb.d.ts +39 -0
- package/dist/tools/nanovdb/write-nvdb.js +210 -0
- package/dist/tools/nanovdb/write-nvdb.js.map +1 -0
- package/dist/tools/nvdb-file.d.ts +195 -0
- package/dist/tools/nvdb-file.js +606 -0
- package/dist/tools/nvdb-file.js.map +1 -0
- package/dist/tools/nvdb-stream.d.ts +138 -0
- package/dist/tools/nvdb-stream.js +365 -0
- package/dist/tools/nvdb-stream.js.map +1 -0
- package/dist/tools/vdb/bit-utils.d.ts +20 -0
- package/dist/tools/vdb/bit-utils.js +42 -0
- package/dist/tools/vdb/bit-utils.js.map +1 -0
- package/dist/tools/vdb/blosc.d.ts +56 -0
- package/dist/tools/vdb/blosc.js +231 -0
- package/dist/tools/vdb/blosc.js.map +1 -0
- package/dist/tools/vdb/byte-reader.d.ts +40 -0
- package/dist/tools/vdb/byte-reader.js +139 -0
- package/dist/tools/vdb/byte-reader.js.map +1 -0
- package/dist/tools/vdb/compression.d.ts +73 -0
- package/dist/tools/vdb/compression.js +249 -0
- package/dist/tools/vdb/compression.js.map +1 -0
- package/dist/tools/vdb/errors.d.ts +10 -0
- package/dist/tools/vdb/errors.js +11 -0
- package/dist/tools/vdb/errors.js.map +1 -0
- package/dist/tools/vdb/grid.d.ts +37 -0
- package/dist/tools/vdb/grid.js +147 -0
- package/dist/tools/vdb/grid.js.map +1 -0
- package/dist/tools/vdb/half-float.d.ts +9 -0
- package/dist/tools/vdb/half-float.js +24 -0
- package/dist/tools/vdb/half-float.js.map +1 -0
- package/dist/tools/vdb/index.d.ts +3 -0
- package/dist/tools/vdb/index.js +3 -0
- package/dist/tools/vdb/index.js.map +1 -0
- package/dist/tools/vdb/inflate.d.ts +24 -0
- package/dist/tools/vdb/inflate.js +334 -0
- package/dist/tools/vdb/inflate.js.map +1 -0
- package/dist/tools/vdb/metadata.d.ts +17 -0
- package/dist/tools/vdb/metadata.js +72 -0
- package/dist/tools/vdb/metadata.js.map +1 -0
- package/dist/tools/vdb/parse.d.ts +25 -0
- package/dist/tools/vdb/parse.js +171 -0
- package/dist/tools/vdb/parse.js.map +1 -0
- package/dist/tools/vdb/point-render-data.d.ts +31 -0
- package/dist/tools/vdb/point-render-data.js +57 -0
- package/dist/tools/vdb/point-render-data.js.map +1 -0
- package/dist/tools/vdb/points.d.ts +160 -0
- package/dist/tools/vdb/points.js +794 -0
- package/dist/tools/vdb/points.js.map +1 -0
- package/dist/tools/vdb/transform.d.ts +23 -0
- package/dist/tools/vdb/transform.js +54 -0
- package/dist/tools/vdb/transform.js.map +1 -0
- package/dist/tools/vdb/tree.d.ts +111 -0
- package/dist/tools/vdb/tree.js +228 -0
- package/dist/tools/vdb/tree.js.map +1 -0
- package/dist/tools/vdb/types.d.ts +138 -0
- package/dist/tools/vdb/types.js +3 -0
- package/dist/tools/vdb/types.js.map +1 -0
- package/dist/tools/wvdb/bake.d.ts +116 -0
- package/dist/tools/wvdb/bake.js +311 -0
- package/dist/tools/wvdb/bake.js.map +1 -0
- package/dist/tools/wvdb/dense.d.ts +112 -0
- package/dist/tools/wvdb/dense.js +309 -0
- package/dist/tools/wvdb/dense.js.map +1 -0
- package/dist/tools/wvdb/format.d.ts +161 -0
- package/dist/tools/wvdb/format.js +303 -0
- package/dist/tools/wvdb/format.js.map +1 -0
- package/dist/tools/wvdb/index.d.ts +13 -0
- package/dist/tools/wvdb/index.js +14 -0
- package/dist/tools/wvdb/index.js.map +1 -0
- package/dist/tools/wvdb/level-set.d.ts +48 -0
- package/dist/tools/wvdb/level-set.js +153 -0
- package/dist/tools/wvdb/level-set.js.map +1 -0
- package/dist/tools/wvdb/nanovdb.d.ts +54 -0
- package/dist/tools/wvdb/nanovdb.js +60 -0
- package/dist/tools/wvdb/nanovdb.js.map +1 -0
- package/dist/tools/wvdb/read.d.ts +260 -0
- package/dist/tools/wvdb/read.js +743 -0
- package/dist/tools/wvdb/read.js.map +1 -0
- package/dist/tools/wvdb/sha256.d.ts +7 -0
- package/dist/tools/wvdb/sha256.js +76 -0
- package/dist/tools/wvdb/sha256.js.map +1 -0
- package/dist/tools/wvdb/write.d.ts +127 -0
- package/dist/tools/wvdb/write.js +396 -0
- package/dist/tools/wvdb/write.js.map +1 -0
- package/package.json +82 -4
- package/vendor/LICENSE +201 -0
- package/vendor/NOTICE +19 -0
- package/vendor/VENDOR.md +85 -0
- package/vendor/pnanovdb.wgsl +1539 -0
|
@@ -0,0 +1,518 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tree.ts — dense array -> sparse NanoVDB node hierarchy (topology, masks,
|
|
3
|
+
* per-node stats and bbox). Pure data; no bytes are written here. `serialize.ts`
|
|
4
|
+
* turns the {@link BuiltTree} into a laid-out grid image.
|
|
5
|
+
*
|
|
6
|
+
* ## Topology policy (T3 decision, v1)
|
|
7
|
+
*
|
|
8
|
+
* A leaf (8^3 block) is materialised iff the block contains at least one active
|
|
9
|
+
* voxel. Every materialised leaf stores the *source value* for all 512 of its
|
|
10
|
+
* slots (active and inactive), so any voxel inside a materialised leaf round-
|
|
11
|
+
* trips exactly. A block with no active voxel collapses to an inactive
|
|
12
|
+
* background tile in its parent lower node — so empty space stays sparse.
|
|
13
|
+
*
|
|
14
|
+
* ## Collapsing uniform regions (K4.6)
|
|
15
|
+
*
|
|
16
|
+
* With `collapse` on, uniform nodes become tiles bottom-up, as OpenVDB's
|
|
17
|
+
* `tools::prune(tree, tolerance = 0)` (`TolerancePruneOp`) would do it:
|
|
18
|
+
* - a leaf whose 512 voxels all have the same active state and exactly equal
|
|
19
|
+
* values (`===`, so `-0` equals `0`, `NaN` never collapses) becomes a lower
|
|
20
|
+
* tile with that state and value (the leaf's first value);
|
|
21
|
+
* - a lower node with no child leaf whose 4096 slots all have one state and
|
|
22
|
+
* one value becomes an upper tile; an upper node likewise a root tile;
|
|
23
|
+
* - a tile that ends up inactive with the background value is the default
|
|
24
|
+
* slot, so it is not stored (an upper node that collapses to one is erased
|
|
25
|
+
* from the root, as `RootNode::eraseBackgroundTiles` does after a prune).
|
|
26
|
+
* Values read back identically. Stats follow GridStats' tile weighting (see
|
|
27
|
+
* below), so an upper or root average that mixes a collapsed region with other
|
|
28
|
+
* data weights it differently from the leaves it replaced, exactly as a native
|
|
29
|
+
* image of the pruned tree would. min/max, bbox and the active-voxel count do
|
|
30
|
+
* not change. Off by default here; the public builders choose (see
|
|
31
|
+
* `serialize.ts`).
|
|
32
|
+
*
|
|
33
|
+
* ## Active tiles passed in (K4.1)
|
|
34
|
+
*
|
|
35
|
+
* The leaf-iterator path ({@link buildTreeFromLeaves}) also accepts active
|
|
36
|
+
* tiles that already exist in the source (a parsed `.vdb`, or a NanoVDB image
|
|
37
|
+
* being re-encoded): lower-node tiles (8^3), upper-node tiles (128^3) and root
|
|
38
|
+
* tiles (4096^3). They are kept as tiles, never expanded into leaves. Stats
|
|
39
|
+
* follow NanoVDB `tools/GridStats.h`: a tile adds its value to its parent's
|
|
40
|
+
* min/max/ave/stddev with weight `ChildT::NUM_VALUES` (512, 4096 or 32768 — the
|
|
41
|
+
* child node's *slot* count, not its voxel count) and grows the parent's bbox
|
|
42
|
+
* by the tile's full extent. The active-voxel count, in contrast, counts each
|
|
43
|
+
* tile as `dim^3` voxels (CreateNanoGrid's `mVoxelCount`).
|
|
44
|
+
*
|
|
45
|
+
* ## Active predicate
|
|
46
|
+
*
|
|
47
|
+
* Default (no `activeThreshold`): a voxel is active iff `value !== background`
|
|
48
|
+
* (exact). With `activeThreshold` given: active iff
|
|
49
|
+
* `|value - background| > activeThreshold`. Note threshold 0 is equivalent to
|
|
50
|
+
* the exact rule. Under a non-zero threshold, a block whose every voxel is
|
|
51
|
+
* inactive collapses to background even if some of those voxels differ from the
|
|
52
|
+
* background value — those sub-threshold values are not preserved. This is
|
|
53
|
+
* documented and intended (topology follows activity).
|
|
54
|
+
*/
|
|
55
|
+
import { VDBToolsError } from "../errors.js";
|
|
56
|
+
import { leafCoordToOffset, lowerCoordToOffset, upperCoordToOffset, LEAF_TABLE_COUNT, } from "./bytes.js";
|
|
57
|
+
import { StatsAccumulator, Vec3StatsAccumulator } from "./stats.js";
|
|
58
|
+
function floorDiv(a, b) {
|
|
59
|
+
return Math.floor(a / b);
|
|
60
|
+
}
|
|
61
|
+
/** Deterministic `"x,y,z"` origin key used to dedupe/lookup nodes by coord. */
|
|
62
|
+
function leafKey(lx, ly, lz) {
|
|
63
|
+
return `${lx},${ly},${lz}`;
|
|
64
|
+
}
|
|
65
|
+
/** Aligns coord down to a power-of-two block size (handles negatives). */
|
|
66
|
+
function alignDown(v, block) {
|
|
67
|
+
return floorDiv(v, block) * block;
|
|
68
|
+
}
|
|
69
|
+
function makeIsActive(background, threshold) {
|
|
70
|
+
if (threshold === undefined)
|
|
71
|
+
return (v) => v !== background;
|
|
72
|
+
return (v) => Math.abs(v - background) > threshold;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Builds the sparse hierarchy for a dense `values` array of shape
|
|
76
|
+
* `[nx,ny,nz]` (x-major: idx = (x*ny + y)*nz + z), placed with its min corner at
|
|
77
|
+
* index-space `origin`.
|
|
78
|
+
*/
|
|
79
|
+
export function buildTree(values, dims, opts) {
|
|
80
|
+
const [nx, ny, nz] = dims;
|
|
81
|
+
const [ox, oy, oz] = opts.origin;
|
|
82
|
+
const isActive = makeIsActive(opts.background, opts.activeThreshold);
|
|
83
|
+
const denseIndex = (x, y, z) => (x * ny + y) * nz + z;
|
|
84
|
+
// -- Pass 1: discover leaves (blocks with >=1 active voxel) ---------------
|
|
85
|
+
const leafMap = new Map();
|
|
86
|
+
for (let x = 0; x < nx; x++) {
|
|
87
|
+
for (let y = 0; y < ny; y++) {
|
|
88
|
+
const base = (x * ny + y) * nz;
|
|
89
|
+
for (let z = 0; z < nz; z++) {
|
|
90
|
+
if (!isActive(values[base + z]))
|
|
91
|
+
continue;
|
|
92
|
+
const ix = ox + x;
|
|
93
|
+
const iy = oy + y;
|
|
94
|
+
const iz = oz + z;
|
|
95
|
+
const lx = alignDown(ix, 8);
|
|
96
|
+
const ly = alignDown(iy, 8);
|
|
97
|
+
const lz = alignDown(iz, 8);
|
|
98
|
+
const k = leafKey(lx, ly, lz);
|
|
99
|
+
if (!leafMap.has(k)) {
|
|
100
|
+
leafMap.set(k, {
|
|
101
|
+
origin: [lx, ly, lz],
|
|
102
|
+
values: new Float32Array(LEAF_TABLE_COUNT),
|
|
103
|
+
valueMask: new Uint32Array(16),
|
|
104
|
+
stats: new StatsAccumulator(),
|
|
105
|
+
memIndex: -1,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
// -- Pass 2: fill each leaf's 512 values + mask + stats -------------------
|
|
112
|
+
for (const leaf of leafMap.values()) {
|
|
113
|
+
const [lx, ly, lz] = leaf.origin;
|
|
114
|
+
for (let dx = 0; dx < 8; dx++) {
|
|
115
|
+
const ix = lx + dx;
|
|
116
|
+
const sx = ix - ox; // dense x
|
|
117
|
+
for (let dy = 0; dy < 8; dy++) {
|
|
118
|
+
const iy = ly + dy;
|
|
119
|
+
const sy = iy - oy;
|
|
120
|
+
for (let dz = 0; dz < 8; dz++) {
|
|
121
|
+
const iz = lz + dz;
|
|
122
|
+
const sz = iz - oz;
|
|
123
|
+
const n = leafCoordToOffset(ix, iy, iz);
|
|
124
|
+
const inRange = sx >= 0 && sx < nx && sy >= 0 && sy < ny && sz >= 0 && sz < nz;
|
|
125
|
+
const v = inRange ? values[denseIndex(sx, sy, sz)] : opts.background;
|
|
126
|
+
leaf.values[n] = v;
|
|
127
|
+
if (inRange && isActive(v)) {
|
|
128
|
+
leaf.valueMask[n >>> 5] |= 1 << (n & 31);
|
|
129
|
+
leaf.stats.addVoxel(v, ix, iy, iz);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return assembleTree([...leafMap.values()], [], 1, { collapse: opts.collapse === true, background: opts.background });
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Builds the sparse hierarchy directly from a set of already-materialised leaves
|
|
139
|
+
* (each carrying its 8-aligned origin, all 512 values, and a 512-bit value mask).
|
|
140
|
+
* This is the memory-frugal path used by `buildFromVdb` and `quantize`: it never
|
|
141
|
+
* allocates a dense array, so a 7M-voxel grid streams leaf-by-leaf. Per-leaf
|
|
142
|
+
* stats/bbox are (re)computed from the mask + values here.
|
|
143
|
+
*/
|
|
144
|
+
export function buildTreeFromLeaves(source, tiles = [], components = 1, options = {}) {
|
|
145
|
+
const leaves = [];
|
|
146
|
+
for (const src of source) {
|
|
147
|
+
const [lx, ly, lz] = src.origin;
|
|
148
|
+
if (src.values.length !== LEAF_TABLE_COUNT * components) {
|
|
149
|
+
throw new VDBToolsError("VDB_BAD_OPTION", `buildFromLeaves: leaf at [${lx}, ${ly}, ${lz}] has ${src.values.length} values, ` +
|
|
150
|
+
`expected ${LEAF_TABLE_COUNT * components}`);
|
|
151
|
+
}
|
|
152
|
+
const stats = components === 3 ? new Vec3StatsAccumulator() : new StatsAccumulator();
|
|
153
|
+
for (let n = 0; n < LEAF_TABLE_COUNT; n++) {
|
|
154
|
+
const active = (src.valueMask[n >>> 5] >>> (n & 31)) & 1;
|
|
155
|
+
if (active) {
|
|
156
|
+
const x = lx + ((n >> 6) & 7);
|
|
157
|
+
const y = ly + ((n >> 3) & 7);
|
|
158
|
+
const z = lz + (n & 7);
|
|
159
|
+
if (stats instanceof Vec3StatsAccumulator) {
|
|
160
|
+
const v = src.values;
|
|
161
|
+
stats.addVoxelVec(v[n * 3], v[n * 3 + 1], v[n * 3 + 2], x, y, z);
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
stats.addVoxel(src.values[n], x, y, z);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
leaves.push({
|
|
169
|
+
origin: [lx, ly, lz],
|
|
170
|
+
values: src.values,
|
|
171
|
+
valueMask: src.valueMask,
|
|
172
|
+
stats,
|
|
173
|
+
memIndex: -1,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
const background = options.background ?? (components === 3 ? [0, 0, 0] : 0);
|
|
177
|
+
return assembleTree(leaves, tiles, components, { collapse: options.collapse === true, background });
|
|
178
|
+
}
|
|
179
|
+
/** NanoVDB GridStats weight for a tile of edge `dim`: the child node type's slot count. */
|
|
180
|
+
const TILE_STATS_WEIGHT = { 8: 512, 128: 4096, 4096: 32768 };
|
|
181
|
+
/**
|
|
182
|
+
* Assembles lower/upper/root nodes (topology, ordering, stats aggregation) from
|
|
183
|
+
* a flat list of leaves. Shared by the dense (`buildTree`) and leaf-iterator
|
|
184
|
+
* (`buildTreeFromLeaves`) build paths.
|
|
185
|
+
*/
|
|
186
|
+
function assembleTree(leafList, tileList, components, collapseOpts) {
|
|
187
|
+
const newStats = () => (components === 3 ? new Vec3StatsAccumulator() : new StatsAccumulator());
|
|
188
|
+
// -- Assemble lower/upper nodes from leaves ------------------------------
|
|
189
|
+
const upperMap = new Map();
|
|
190
|
+
const lowerMap = new Map();
|
|
191
|
+
const nodeKey = leafKey;
|
|
192
|
+
const upperAt = (x, y, z) => {
|
|
193
|
+
const ux = alignDown(x, 4096);
|
|
194
|
+
const uy = alignDown(y, 4096);
|
|
195
|
+
const uz = alignDown(z, 4096);
|
|
196
|
+
const uk = nodeKey(ux, uy, uz);
|
|
197
|
+
let upper = upperMap.get(uk);
|
|
198
|
+
if (!upper) {
|
|
199
|
+
upper = {
|
|
200
|
+
origin: [ux, uy, uz],
|
|
201
|
+
children: new Map(),
|
|
202
|
+
tiles: new Map(),
|
|
203
|
+
inactiveTiles: new Map(),
|
|
204
|
+
stats: newStats(),
|
|
205
|
+
memIndex: -1,
|
|
206
|
+
};
|
|
207
|
+
upperMap.set(uk, upper);
|
|
208
|
+
}
|
|
209
|
+
return upper;
|
|
210
|
+
};
|
|
211
|
+
const lowerAt = (x, y, z) => {
|
|
212
|
+
const wx = alignDown(x, 128);
|
|
213
|
+
const wy = alignDown(y, 128);
|
|
214
|
+
const wz = alignDown(z, 128);
|
|
215
|
+
const lk = nodeKey(wx, wy, wz);
|
|
216
|
+
let lower = lowerMap.get(lk);
|
|
217
|
+
if (!lower) {
|
|
218
|
+
lower = {
|
|
219
|
+
origin: [wx, wy, wz],
|
|
220
|
+
children: new Map(),
|
|
221
|
+
tiles: new Map(),
|
|
222
|
+
inactiveTiles: new Map(),
|
|
223
|
+
stats: newStats(),
|
|
224
|
+
memIndex: -1,
|
|
225
|
+
};
|
|
226
|
+
lowerMap.set(lk, lower);
|
|
227
|
+
upperAt(x, y, z).children.set(upperCoordToOffset(wx, wy, wz), lower);
|
|
228
|
+
}
|
|
229
|
+
return lower;
|
|
230
|
+
};
|
|
231
|
+
for (const leaf of leafList) {
|
|
232
|
+
const [lx, ly, lz] = leaf.origin;
|
|
233
|
+
lowerAt(lx, ly, lz).children.set(lowerCoordToOffset(lx, ly, lz), leaf);
|
|
234
|
+
}
|
|
235
|
+
// -- Place active tiles --------------------------------------------------
|
|
236
|
+
const rootTileMap = new Map();
|
|
237
|
+
const tileCounts = { lower: 0, upper: 0, root: 0 };
|
|
238
|
+
let tileVoxels = 0;
|
|
239
|
+
for (const tile of tileList) {
|
|
240
|
+
const [tx, ty, tz] = tile.origin;
|
|
241
|
+
const { dim, value } = tile;
|
|
242
|
+
const active = tile.active !== false;
|
|
243
|
+
if ((typeof value === "number") !== (components === 1)) {
|
|
244
|
+
throw new VDBToolsError("VDB_BAD_OPTION", `buildFromLeaves: tile value ${JSON.stringify(value)} does not have ${components} component(s)`);
|
|
245
|
+
}
|
|
246
|
+
if (TILE_STATS_WEIGHT[dim] === undefined) {
|
|
247
|
+
throw new VDBToolsError("VDB_BAD_OPTION", `buildFromLeaves: tile dim ${dim} must be 8, 128 or 4096`);
|
|
248
|
+
}
|
|
249
|
+
if (alignDown(tx, dim) !== tx || alignDown(ty, dim) !== ty || alignDown(tz, dim) !== tz) {
|
|
250
|
+
throw new VDBToolsError("VDB_BAD_OPTION", `buildFromLeaves: tile origin [${tx}, ${ty}, ${tz}] is not ${dim}-aligned`);
|
|
251
|
+
}
|
|
252
|
+
const duplicate = () => {
|
|
253
|
+
throw new VDBToolsError("VDB_BAD_OPTION", `buildFromLeaves: duplicate ${dim}^3 tile at [${tx}, ${ty}, ${tz}]`);
|
|
254
|
+
};
|
|
255
|
+
if (dim === 8) {
|
|
256
|
+
const lower = lowerAt(tx, ty, tz);
|
|
257
|
+
const slot = lowerCoordToOffset(tx, ty, tz);
|
|
258
|
+
if (lower.tiles.has(slot) || lower.inactiveTiles.has(slot))
|
|
259
|
+
duplicate();
|
|
260
|
+
if (active) {
|
|
261
|
+
lower.tiles.set(slot, value);
|
|
262
|
+
tileCounts.lower++;
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
lower.inactiveTiles.set(slot, value);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
else if (dim === 128) {
|
|
269
|
+
const upper = upperAt(tx, ty, tz);
|
|
270
|
+
const slot = upperCoordToOffset(tx, ty, tz);
|
|
271
|
+
if (upper.tiles.has(slot) || upper.inactiveTiles.has(slot))
|
|
272
|
+
duplicate();
|
|
273
|
+
if (active) {
|
|
274
|
+
upper.tiles.set(slot, value);
|
|
275
|
+
tileCounts.upper++;
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
upper.inactiveTiles.set(slot, value);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
const k = nodeKey(tx, ty, tz);
|
|
283
|
+
if (rootTileMap.has(k))
|
|
284
|
+
duplicate();
|
|
285
|
+
rootTileMap.set(k, { origin: [tx, ty, tz], value, active });
|
|
286
|
+
if (active)
|
|
287
|
+
tileCounts.root++;
|
|
288
|
+
}
|
|
289
|
+
if (active)
|
|
290
|
+
tileVoxels += dim * dim * dim;
|
|
291
|
+
}
|
|
292
|
+
// A slot holds a child or a tile, never both.
|
|
293
|
+
const overlap = (where, origin) => {
|
|
294
|
+
throw new VDBToolsError("VDB_BAD_OPTION", `buildFromLeaves: active tile overlaps child data in ${where} at [${origin.join(", ")}]`);
|
|
295
|
+
};
|
|
296
|
+
for (const lower of lowerMap.values()) {
|
|
297
|
+
for (const slot of [...lower.tiles.keys(), ...lower.inactiveTiles.keys()]) {
|
|
298
|
+
if (lower.children.has(slot))
|
|
299
|
+
overlap("a lower node", lower.origin);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
for (const upper of upperMap.values()) {
|
|
303
|
+
for (const slot of [...upper.tiles.keys(), ...upper.inactiveTiles.keys()]) {
|
|
304
|
+
if (upper.children.has(slot))
|
|
305
|
+
overlap("an upper node", upper.origin);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
for (const [key, tile] of rootTileMap)
|
|
309
|
+
if (upperMap.has(key))
|
|
310
|
+
overlap("the root", tile.origin);
|
|
311
|
+
// -- Collapse uniform nodes into tiles, bottom-up (K4.6) ------------------
|
|
312
|
+
if (collapseOpts.collapse) {
|
|
313
|
+
const background = collapseOpts.background;
|
|
314
|
+
const placeTile = (tiles, inactive, slot, c) => {
|
|
315
|
+
if (c.active)
|
|
316
|
+
tiles.set(slot, c.value);
|
|
317
|
+
else if (!tileValuesEqual(c.value, background))
|
|
318
|
+
inactive.set(slot, c.value);
|
|
319
|
+
};
|
|
320
|
+
// Leaves -> lower-node tiles.
|
|
321
|
+
for (const lower of lowerMap.values()) {
|
|
322
|
+
for (const [slot, leaf] of lower.children) {
|
|
323
|
+
const c = constantLeaf(leaf, components);
|
|
324
|
+
if (!c)
|
|
325
|
+
continue;
|
|
326
|
+
lower.children.delete(slot);
|
|
327
|
+
placeTile(lower.tiles, lower.inactiveTiles, slot, c);
|
|
328
|
+
if (c.active) {
|
|
329
|
+
tileCounts.lower++;
|
|
330
|
+
tileVoxels += 512;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
// Lower nodes -> upper-node tiles.
|
|
335
|
+
for (const upper of upperMap.values()) {
|
|
336
|
+
for (const [slot, lower] of upper.children) {
|
|
337
|
+
const c = constantInternal(lower, LOWER_SLOTS, background);
|
|
338
|
+
if (!c)
|
|
339
|
+
continue;
|
|
340
|
+
upper.children.delete(slot);
|
|
341
|
+
lowerMap.delete(nodeKey(...lower.origin));
|
|
342
|
+
placeTile(upper.tiles, upper.inactiveTiles, slot, c);
|
|
343
|
+
if (c.active) {
|
|
344
|
+
tileCounts.lower -= LOWER_SLOTS;
|
|
345
|
+
tileCounts.upper++;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
// Upper nodes -> root tiles (an inactive background tile is erased).
|
|
350
|
+
for (const [key, upper] of upperMap) {
|
|
351
|
+
const c = constantInternal(upper, UPPER_SLOTS, background);
|
|
352
|
+
if (!c)
|
|
353
|
+
continue;
|
|
354
|
+
upperMap.delete(key);
|
|
355
|
+
if (c.active) {
|
|
356
|
+
rootTileMap.set(key, { origin: upper.origin, value: c.value, active: true });
|
|
357
|
+
tileCounts.upper -= UPPER_SLOTS;
|
|
358
|
+
tileCounts.root++;
|
|
359
|
+
}
|
|
360
|
+
else if (!tileValuesEqual(c.value, background)) {
|
|
361
|
+
rootTileMap.set(key, { origin: upper.origin, value: c.value, active: false });
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
// -- Deterministic breadth-first ordering + memIndex assignment ----------
|
|
366
|
+
const byOrigin = (a, b) => a.origin[0] - b.origin[0] || a.origin[1] - b.origin[1] || a.origin[2] - b.origin[2];
|
|
367
|
+
const uppers = [...upperMap.values()].sort(byOrigin);
|
|
368
|
+
uppers.forEach((u, i) => (u.memIndex = i));
|
|
369
|
+
// Lowers grouped by parent upper (in upper order), then by slot.
|
|
370
|
+
const lowers = [];
|
|
371
|
+
for (const upper of uppers) {
|
|
372
|
+
const slots = [...upper.children.keys()].sort((a, b) => a - b);
|
|
373
|
+
for (const s of slots)
|
|
374
|
+
lowers.push(upper.children.get(s));
|
|
375
|
+
}
|
|
376
|
+
lowers.forEach((l, i) => (l.memIndex = i));
|
|
377
|
+
// Leaves grouped by parent lower (in lower order), then by slot.
|
|
378
|
+
const leaves = [];
|
|
379
|
+
for (const lower of lowers) {
|
|
380
|
+
const slots = [...lower.children.keys()].sort((a, b) => a - b);
|
|
381
|
+
for (const s of slots)
|
|
382
|
+
leaves.push(lower.children.get(s));
|
|
383
|
+
}
|
|
384
|
+
leaves.forEach((l, i) => (l.memIndex = i));
|
|
385
|
+
// -- Aggregate stats bottom-up -------------------------------------------
|
|
386
|
+
// Tiles first, then children: the order NanoVDB GridStats accumulates in.
|
|
387
|
+
for (const lower of lowers) {
|
|
388
|
+
addTileStats(lower.stats, lower.tiles, lower.origin, 4, 8);
|
|
389
|
+
for (const leaf of lower.children.values())
|
|
390
|
+
lower.stats.merge(leaf.stats);
|
|
391
|
+
}
|
|
392
|
+
for (const upper of uppers) {
|
|
393
|
+
addTileStats(upper.stats, upper.tiles, upper.origin, 5, 128);
|
|
394
|
+
for (const lower of upper.children.values())
|
|
395
|
+
upper.stats.merge(lower.stats);
|
|
396
|
+
}
|
|
397
|
+
// -- Root table: children and tiles, sorted by NanoVDB key ----------------
|
|
398
|
+
// Unsigned 64-bit key order (K4.12): negative coordinates sort after the
|
|
399
|
+
// non-negative ones, not before them as a signed origin sort would put them.
|
|
400
|
+
const rootTiles = [
|
|
401
|
+
...uppers.map((u) => ({ key: keyForOrigin(u.origin), origin: u.origin, child: u })),
|
|
402
|
+
...[...rootTileMap.values()].map((t) => ({ key: keyForOrigin(t.origin), origin: t.origin, child: null, value: t.value, active: t.active })),
|
|
403
|
+
].sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0));
|
|
404
|
+
const root = newStats();
|
|
405
|
+
for (const entry of rootTiles) {
|
|
406
|
+
if (entry.child) {
|
|
407
|
+
root.merge(entry.child.stats);
|
|
408
|
+
}
|
|
409
|
+
else if (entry.active) {
|
|
410
|
+
const [x, y, z] = entry.origin;
|
|
411
|
+
addTileValue(root, entry.value, TILE_STATS_WEIGHT[4096], entry.origin, [x + 4095, y + 4095, z + 4095]);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
let leafVoxels = 0;
|
|
415
|
+
for (const leaf of leaves)
|
|
416
|
+
leafVoxels += leaf.stats.count;
|
|
417
|
+
return {
|
|
418
|
+
uppers,
|
|
419
|
+
lowers,
|
|
420
|
+
leaves,
|
|
421
|
+
rootTiles,
|
|
422
|
+
tileCounts,
|
|
423
|
+
root,
|
|
424
|
+
activeVoxelCount: leafVoxels + tileVoxels,
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
const LOWER_SLOTS = 4096;
|
|
428
|
+
const UPPER_SLOTS = 32768;
|
|
429
|
+
/** Exact tile value equality (`===` per component: `-0` equals `0`, `NaN` equals nothing). */
|
|
430
|
+
function tileValuesEqual(a, b) {
|
|
431
|
+
if (typeof a === "number" || typeof b === "number")
|
|
432
|
+
return a === b;
|
|
433
|
+
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2];
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* OpenVDB `LeafNode::isConstant` at tolerance 0: every voxel has the same
|
|
437
|
+
* active state and every value equals the first. Returns the tile, or null.
|
|
438
|
+
*/
|
|
439
|
+
function constantLeaf(leaf, components) {
|
|
440
|
+
const mask = leaf.valueMask;
|
|
441
|
+
const w0 = mask[0];
|
|
442
|
+
if (w0 !== 0 && w0 !== 0xffffffff)
|
|
443
|
+
return null;
|
|
444
|
+
for (let i = 1; i < 16; i++)
|
|
445
|
+
if (mask[i] !== w0)
|
|
446
|
+
return null;
|
|
447
|
+
const v = leaf.values;
|
|
448
|
+
if (components === 1) {
|
|
449
|
+
const first = v[0];
|
|
450
|
+
for (let n = 1; n < LEAF_TABLE_COUNT; n++)
|
|
451
|
+
if (v[n] !== first)
|
|
452
|
+
return null;
|
|
453
|
+
return { value: first, active: w0 !== 0 };
|
|
454
|
+
}
|
|
455
|
+
const x = v[0];
|
|
456
|
+
const y = v[1];
|
|
457
|
+
const z = v[2];
|
|
458
|
+
for (let n = 1; n < LEAF_TABLE_COUNT; n++) {
|
|
459
|
+
if (v[n * 3] !== x || v[n * 3 + 1] !== y || v[n * 3 + 2] !== z)
|
|
460
|
+
return null;
|
|
461
|
+
}
|
|
462
|
+
return { value: [x, y, z], active: w0 !== 0 };
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* OpenVDB `InternalNode::isConstant` at tolerance 0: no child, every slot the
|
|
466
|
+
* same state, every slot value equal (slots not stored are inactive background).
|
|
467
|
+
*/
|
|
468
|
+
function constantInternal(node, slots, background) {
|
|
469
|
+
if (node.children.size > 0)
|
|
470
|
+
return null;
|
|
471
|
+
if (node.tiles.size === slots) {
|
|
472
|
+
const first = node.tiles.get(0);
|
|
473
|
+
for (const v of node.tiles.values())
|
|
474
|
+
if (!tileValuesEqual(v, first))
|
|
475
|
+
return null;
|
|
476
|
+
return { value: first, active: true };
|
|
477
|
+
}
|
|
478
|
+
if (node.tiles.size > 0)
|
|
479
|
+
return null;
|
|
480
|
+
if (node.inactiveTiles.size === slots) {
|
|
481
|
+
const first = node.inactiveTiles.get(0);
|
|
482
|
+
for (const v of node.inactiveTiles.values())
|
|
483
|
+
if (!tileValuesEqual(v, first))
|
|
484
|
+
return null;
|
|
485
|
+
return { value: first, active: false };
|
|
486
|
+
}
|
|
487
|
+
// Some slots hold the background: the rest must too.
|
|
488
|
+
for (const v of node.inactiveTiles.values())
|
|
489
|
+
if (!tileValuesEqual(v, background))
|
|
490
|
+
return null;
|
|
491
|
+
return { value: background, active: false };
|
|
492
|
+
}
|
|
493
|
+
/** Adds an internal node's active tiles to its stats, in slot order (GridStats weighting). */
|
|
494
|
+
function addTileStats(stats, tiles, origin, log2dim, childDim) {
|
|
495
|
+
if (tiles.size === 0)
|
|
496
|
+
return;
|
|
497
|
+
const weight = TILE_STATS_WEIGHT[childDim];
|
|
498
|
+
const mask = (1 << log2dim) - 1;
|
|
499
|
+
const slots = [...tiles.keys()].sort((a, b) => a - b);
|
|
500
|
+
for (const n of slots) {
|
|
501
|
+
const x = origin[0] + ((n >> (2 * log2dim)) & mask) * childDim;
|
|
502
|
+
const y = origin[1] + ((n >> log2dim) & mask) * childDim;
|
|
503
|
+
const z = origin[2] + (n & mask) * childDim;
|
|
504
|
+
addTileValue(stats, tiles.get(n), weight, [x, y, z], [x + childDim - 1, y + childDim - 1, z + childDim - 1]);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
/** Adds a scalar or vector tile to its accumulator. */
|
|
508
|
+
function addTileValue(stats, value, weight, bboxMin, bboxMax) {
|
|
509
|
+
if (typeof value === "number")
|
|
510
|
+
stats.addTile(value, weight, bboxMin, bboxMax);
|
|
511
|
+
else
|
|
512
|
+
stats.addTileVec(value, weight, bboxMin, bboxMax);
|
|
513
|
+
}
|
|
514
|
+
import { coordToKey } from "./bytes.js";
|
|
515
|
+
function keyForOrigin(origin) {
|
|
516
|
+
return coordToKey(origin[0], origin[1], origin[2]);
|
|
517
|
+
}
|
|
518
|
+
//# sourceMappingURL=tree.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tree.js","sourceRoot":"","sources":["../../../src/tools/nanovdb/tree.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,kBAAkB,EAClB,gBAAgB,GACjB,MAAM,YAAY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AAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;AACrD,CAAC","sourcesContent":["/**\n * tree.ts — dense array -> sparse NanoVDB node hierarchy (topology, masks,\n * per-node stats and bbox). Pure data; no bytes are written here. `serialize.ts`\n * turns the {@link BuiltTree} into a laid-out grid image.\n *\n * ## Topology policy (T3 decision, v1)\n *\n * A leaf (8^3 block) is materialised iff the block contains at least one active\n * voxel. Every materialised leaf stores the *source value* for all 512 of its\n * slots (active and inactive), so any voxel inside a materialised leaf round-\n * trips exactly. A block with no active voxel collapses to an inactive\n * background tile in its parent lower node — so empty space stays sparse.\n *\n * ## Collapsing uniform regions (K4.6)\n *\n * With `collapse` on, uniform nodes become tiles bottom-up, as OpenVDB's\n * `tools::prune(tree, tolerance = 0)` (`TolerancePruneOp`) would do it:\n * - a leaf whose 512 voxels all have the same active state and exactly equal\n * values (`===`, so `-0` equals `0`, `NaN` never collapses) becomes a lower\n * tile with that state and value (the leaf's first value);\n * - a lower node with no child leaf whose 4096 slots all have one state and\n * one value becomes an upper tile; an upper node likewise a root tile;\n * - a tile that ends up inactive with the background value is the default\n * slot, so it is not stored (an upper node that collapses to one is erased\n * from the root, as `RootNode::eraseBackgroundTiles` does after a prune).\n * Values read back identically. Stats follow GridStats' tile weighting (see\n * below), so an upper or root average that mixes a collapsed region with other\n * data weights it differently from the leaves it replaced, exactly as a native\n * image of the pruned tree would. min/max, bbox and the active-voxel count do\n * not change. Off by default here; the public builders choose (see\n * `serialize.ts`).\n *\n * ## Active tiles passed in (K4.1)\n *\n * The leaf-iterator path ({@link buildTreeFromLeaves}) also accepts active\n * tiles that already exist in the source (a parsed `.vdb`, or a NanoVDB image\n * being re-encoded): lower-node tiles (8^3), upper-node tiles (128^3) and root\n * tiles (4096^3). They are kept as tiles, never expanded into leaves. Stats\n * follow NanoVDB `tools/GridStats.h`: a tile adds its value to its parent's\n * min/max/ave/stddev with weight `ChildT::NUM_VALUES` (512, 4096 or 32768 — the\n * child node's *slot* count, not its voxel count) and grows the parent's bbox\n * by the tile's full extent. The active-voxel count, in contrast, counts each\n * tile as `dim^3` voxels (CreateNanoGrid's `mVoxelCount`).\n *\n * ## Active predicate\n *\n * Default (no `activeThreshold`): a voxel is active iff `value !== background`\n * (exact). With `activeThreshold` given: active iff\n * `|value - background| > activeThreshold`. Note threshold 0 is equivalent to\n * the exact rule. Under a non-zero threshold, a block whose every voxel is\n * inactive collapses to background even if some of those voxels differ from the\n * background value — those sub-threshold values are not preserved. This is\n * documented and intended (topology follows activity).\n */\n\nimport { VDBToolsError } from \"../errors.js\";\nimport {\n leafCoordToOffset,\n lowerCoordToOffset,\n upperCoordToOffset,\n LEAF_TABLE_COUNT,\n} from \"./bytes.js\";\nimport { StatsAccumulator, Vec3StatsAccumulator } from \"./stats.js\";\n\n/** A tile (or background) value: a scalar, or `[x, y, z]` for Vec3f grids. */\nexport type TileValue = number | readonly [number, number, number];\n\n/** Values per voxel: 1 (scalar grids) or 3 (Vec3f, xyz interleaved). */\nexport type Components = 1 | 3;\n\nexport interface Leaf {\n /** 8-aligned index-space min corner. */\n origin: [number, number, number];\n /** All 512 voxel values in leaf-offset order (`components` scalars each). */\n values: Float32Array;\n /** 512-bit active mask as 16 u32 words. */\n valueMask: Uint32Array;\n stats: StatsAccumulator;\n memIndex: number;\n}\n\nexport interface LowerNode {\n /** 128-aligned index-space min corner. */\n origin: [number, number, number];\n /** slot (0..4095) -> child leaf. */\n children: Map<number, Leaf>;\n /** slot (0..4095) -> value of an active 8^3 tile. Other slots are background tiles. */\n tiles: Map<number, TileValue>;\n /** slot (0..4095) -> value of an inactive 8^3 tile with a non-background value (K4.9). */\n inactiveTiles: Map<number, TileValue>;\n stats: StatsAccumulator;\n memIndex: number;\n}\n\nexport interface UpperNode {\n /** 4096-aligned index-space min corner. */\n origin: [number, number, number];\n /** slot (0..32767) -> child lower node. */\n children: Map<number, LowerNode>;\n /** slot (0..32767) -> value of an active 128^3 tile. Other slots are background tiles. */\n tiles: Map<number, TileValue>;\n /** slot (0..32767) -> value of an inactive 128^3 tile with a non-background value (K4.9). */\n inactiveTiles: Map<number, TileValue>;\n stats: StatsAccumulator;\n memIndex: number;\n}\n\n/** One root-table entry: a child upper node, or a 4096^3 tile (active or inactive). */\nexport type RootTile =\n | { key: bigint; origin: [number, number, number]; child: UpperNode }\n | { key: bigint; origin: [number, number, number]; child: null; value: TileValue; active: boolean };\n\nexport interface BuiltTree {\n uppers: UpperNode[];\n lowers: LowerNode[];\n leaves: Leaf[];\n /** Root table, sorted by NanoVDB key (unsigned 64-bit), children and tiles interleaved. */\n rootTiles: RootTile[];\n /** Active tile counts per level (TreeData `mTileCount`). */\n tileCounts: { lower: number; upper: number; root: number };\n /** Global active-value stats + tight index bbox (== root stats/bbox). */\n root: StatsAccumulator;\n /** Active voxels in leaves plus `dim^3` per active tile. */\n activeVoxelCount: number;\n}\n\nexport interface BuildTreeOptions {\n origin: [number, number, number];\n background: number;\n activeThreshold: number | undefined;\n /** Collapse uniform nodes into tiles (K4.6). Default false. */\n collapse?: boolean;\n}\n\n/** Options of {@link buildTreeFromLeaves} beyond the leaves and tiles. */\nexport interface CollapseOptions {\n /** Collapse uniform nodes into tiles (K4.6). Default false. */\n collapse?: boolean;\n /** The grid background: an inactive tile with this value is not stored. Default 0 / [0, 0, 0]. */\n background?: TileValue;\n}\n\nfunction floorDiv(a: number, b: number): number {\n return Math.floor(a / b);\n}\n\n/** Deterministic `\"x,y,z\"` origin key used to dedupe/lookup nodes by coord. */\nfunction leafKey(lx: number, ly: number, lz: number): string {\n return `${lx},${ly},${lz}`;\n}\n\n/** Aligns coord down to a power-of-two block size (handles negatives). */\nfunction alignDown(v: number, block: number): number {\n return floorDiv(v, block) * block;\n}\n\nfunction makeIsActive(background: number, threshold: number | undefined): (v: number) => boolean {\n if (threshold === undefined) return (v) => v !== background;\n return (v) => Math.abs(v - background) > threshold;\n}\n\n/**\n * Builds the sparse hierarchy for a dense `values` array of shape\n * `[nx,ny,nz]` (x-major: idx = (x*ny + y)*nz + z), placed with its min corner at\n * index-space `origin`.\n */\nexport function buildTree(\n values: Float32Array,\n dims: [number, number, number],\n opts: BuildTreeOptions,\n): BuiltTree {\n const [nx, ny, nz] = dims;\n const [ox, oy, oz] = opts.origin;\n const isActive = makeIsActive(opts.background, opts.activeThreshold);\n\n const denseIndex = (x: number, y: number, z: number): number => (x * ny + y) * nz + z;\n\n // -- Pass 1: discover leaves (blocks with >=1 active voxel) ---------------\n const leafMap = new Map<string, Leaf>();\n\n for (let x = 0; x < nx; x++) {\n for (let y = 0; y < ny; y++) {\n const base = (x * ny + y) * nz;\n for (let z = 0; z < nz; z++) {\n if (!isActive(values[base + z]!)) continue;\n const ix = ox + x;\n const iy = oy + y;\n const iz = oz + z;\n const lx = alignDown(ix, 8);\n const ly = alignDown(iy, 8);\n const lz = alignDown(iz, 8);\n const k = leafKey(lx, ly, lz);\n if (!leafMap.has(k)) {\n leafMap.set(k, {\n origin: [lx, ly, lz],\n values: new Float32Array(LEAF_TABLE_COUNT),\n valueMask: new Uint32Array(16),\n stats: new StatsAccumulator(),\n memIndex: -1,\n });\n }\n }\n }\n }\n\n // -- Pass 2: fill each leaf's 512 values + mask + stats -------------------\n for (const leaf of leafMap.values()) {\n const [lx, ly, lz] = leaf.origin;\n for (let dx = 0; dx < 8; dx++) {\n const ix = lx + dx;\n const sx = ix - ox; // dense x\n for (let dy = 0; dy < 8; dy++) {\n const iy = ly + dy;\n const sy = iy - oy;\n for (let dz = 0; dz < 8; dz++) {\n const iz = lz + dz;\n const sz = iz - oz;\n const n = leafCoordToOffset(ix, iy, iz);\n const inRange = sx >= 0 && sx < nx && sy >= 0 && sy < ny && sz >= 0 && sz < nz;\n const v = inRange ? values[denseIndex(sx, sy, sz)]! : opts.background;\n leaf.values[n] = v;\n if (inRange && isActive(v)) {\n leaf.valueMask[n >>> 5]! |= 1 << (n & 31);\n leaf.stats.addVoxel(v, ix, iy, iz);\n }\n }\n }\n }\n }\n\n return assembleTree([...leafMap.values()], [], 1, { collapse: opts.collapse === true, background: opts.background });\n}\n\n/**\n * Builds the sparse hierarchy directly from a set of already-materialised leaves\n * (each carrying its 8-aligned origin, all 512 values, and a 512-bit value mask).\n * This is the memory-frugal path used by `buildFromVdb` and `quantize`: it never\n * allocates a dense array, so a 7M-voxel grid streams leaf-by-leaf. Per-leaf\n * stats/bbox are (re)computed from the mask + values here.\n */\nexport function buildTreeFromLeaves(\n source: Iterable<LeafInput>,\n tiles: Iterable<TileInput> = [],\n components: Components = 1,\n options: CollapseOptions = {},\n): BuiltTree {\n const leaves: Leaf[] = [];\n for (const src of source) {\n const [lx, ly, lz] = src.origin;\n if (src.values.length !== LEAF_TABLE_COUNT * components) {\n throw new VDBToolsError(\"VDB_BAD_OPTION\", \n `buildFromLeaves: leaf at [${lx}, ${ly}, ${lz}] has ${src.values.length} values, ` +\n `expected ${LEAF_TABLE_COUNT * components}`,\n );\n }\n const stats = components === 3 ? new Vec3StatsAccumulator() : new StatsAccumulator();\n for (let n = 0; n < LEAF_TABLE_COUNT; n++) {\n const active = (src.valueMask[n >>> 5]! >>> (n & 31)) & 1;\n if (active) {\n const x = lx + ((n >> 6) & 7);\n const y = ly + ((n >> 3) & 7);\n const z = lz + (n & 7);\n if (stats instanceof Vec3StatsAccumulator) {\n const v = src.values;\n stats.addVoxelVec(v[n * 3]!, v[n * 3 + 1]!, v[n * 3 + 2]!, x, y, z);\n } else {\n stats.addVoxel(src.values[n]!, x, y, z);\n }\n }\n }\n leaves.push({\n origin: [lx, ly, lz],\n values: src.values,\n valueMask: src.valueMask,\n stats,\n memIndex: -1,\n });\n }\n const background = options.background ?? (components === 3 ? ([0, 0, 0] as const) : 0);\n return assembleTree(leaves, tiles, components, { collapse: options.collapse === true, background });\n}\n\n/** Minimal shape a leaf source must provide to `buildTreeFromLeaves`. */\nexport interface LeafInput {\n origin: [number, number, number];\n /** All 512 voxel values in leaf-offset order (active and inactive); xyz interleaved for Vec3f. */\n values: Float32Array;\n /** 512-bit active mask as 16 u32 words. */\n valueMask: Uint32Array;\n}\n\n/**\n * An active tile to place above leaf level: `dim^3` voxels, all active, all\n * `value`. `dim` 8 is a lower-node tile, 128 an upper-node tile, 4096 a root\n * tile; `origin` must be a multiple of `dim`.\n */\nexport interface TileInput {\n origin: [number, number, number];\n dim: number;\n value: TileValue;\n /**\n * False for an inactive tile that keeps a stored value (e.g. a level set's\n * interior `-background`, K4.9): written with its value but no value-mask bit\n * (root: state 0), excluded from stats, tile counts and the active-voxel count.\n * Default true.\n */\n active?: boolean;\n}\n\n/** NanoVDB GridStats weight for a tile of edge `dim`: the child node type's slot count. */\nconst TILE_STATS_WEIGHT: Record<number, number> = { 8: 512, 128: 4096, 4096: 32768 };\n\n/**\n * Assembles lower/upper/root nodes (topology, ordering, stats aggregation) from\n * a flat list of leaves. Shared by the dense (`buildTree`) and leaf-iterator\n * (`buildTreeFromLeaves`) build paths.\n */\nfunction assembleTree(\n leafList: Leaf[],\n tileList: Iterable<TileInput>,\n components: Components,\n collapseOpts: { collapse: boolean; background: TileValue },\n): BuiltTree {\n const newStats = (): StatsAccumulator => (components === 3 ? new Vec3StatsAccumulator() : new StatsAccumulator());\n // -- Assemble lower/upper nodes from leaves ------------------------------\n const upperMap = new Map<string, UpperNode>();\n const lowerMap = new Map<string, LowerNode>();\n const nodeKey = leafKey;\n\n const upperAt = (x: number, y: number, z: number): UpperNode => {\n const ux = alignDown(x, 4096);\n const uy = alignDown(y, 4096);\n const uz = alignDown(z, 4096);\n const uk = nodeKey(ux, uy, uz);\n let upper = upperMap.get(uk);\n if (!upper) {\n upper = {\n origin: [ux, uy, uz],\n children: new Map(),\n tiles: new Map(),\n inactiveTiles: new Map(),\n stats: newStats(),\n memIndex: -1,\n };\n upperMap.set(uk, upper);\n }\n return upper;\n };\n\n const lowerAt = (x: number, y: number, z: number): LowerNode => {\n const wx = alignDown(x, 128);\n const wy = alignDown(y, 128);\n const wz = alignDown(z, 128);\n const lk = nodeKey(wx, wy, wz);\n let lower = lowerMap.get(lk);\n if (!lower) {\n lower = {\n origin: [wx, wy, wz],\n children: new Map(),\n tiles: new Map(),\n inactiveTiles: new Map(),\n stats: newStats(),\n memIndex: -1,\n };\n lowerMap.set(lk, lower);\n upperAt(x, y, z).children.set(upperCoordToOffset(wx, wy, wz), lower);\n }\n return lower;\n };\n\n for (const leaf of leafList) {\n const [lx, ly, lz] = leaf.origin;\n lowerAt(lx, ly, lz).children.set(lowerCoordToOffset(lx, ly, lz), leaf);\n }\n\n // -- Place active tiles --------------------------------------------------\n const rootTileMap = new Map<string, { origin: [number, number, number]; value: TileValue; active: boolean }>();\n const tileCounts = { lower: 0, upper: 0, root: 0 };\n let tileVoxels = 0;\n for (const tile of tileList) {\n const [tx, ty, tz] = tile.origin;\n const { dim, value } = tile;\n const active = tile.active !== false;\n if ((typeof value === \"number\") !== (components === 1)) {\n throw new VDBToolsError(\"VDB_BAD_OPTION\", `buildFromLeaves: tile value ${JSON.stringify(value)} does not have ${components} component(s)`);\n }\n if (TILE_STATS_WEIGHT[dim] === undefined) {\n throw new VDBToolsError(\"VDB_BAD_OPTION\", `buildFromLeaves: tile dim ${dim} must be 8, 128 or 4096`);\n }\n if (alignDown(tx, dim) !== tx || alignDown(ty, dim) !== ty || alignDown(tz, dim) !== tz) {\n throw new VDBToolsError(\"VDB_BAD_OPTION\", `buildFromLeaves: tile origin [${tx}, ${ty}, ${tz}] is not ${dim}-aligned`);\n }\n const duplicate = (): never => {\n throw new VDBToolsError(\"VDB_BAD_OPTION\", `buildFromLeaves: duplicate ${dim}^3 tile at [${tx}, ${ty}, ${tz}]`);\n };\n if (dim === 8) {\n const lower = lowerAt(tx, ty, tz);\n const slot = lowerCoordToOffset(tx, ty, tz);\n if (lower.tiles.has(slot) || lower.inactiveTiles.has(slot)) duplicate();\n if (active) {\n lower.tiles.set(slot, value);\n tileCounts.lower++;\n } else {\n lower.inactiveTiles.set(slot, value);\n }\n } else if (dim === 128) {\n const upper = upperAt(tx, ty, tz);\n const slot = upperCoordToOffset(tx, ty, tz);\n if (upper.tiles.has(slot) || upper.inactiveTiles.has(slot)) duplicate();\n if (active) {\n upper.tiles.set(slot, value);\n tileCounts.upper++;\n } else {\n upper.inactiveTiles.set(slot, value);\n }\n } else {\n const k = nodeKey(tx, ty, tz);\n if (rootTileMap.has(k)) duplicate();\n rootTileMap.set(k, { origin: [tx, ty, tz], value, active });\n if (active) tileCounts.root++;\n }\n if (active) tileVoxels += dim * dim * dim;\n }\n\n // A slot holds a child or a tile, never both.\n const overlap = (where: string, origin: readonly number[]): never => {\n throw new VDBToolsError(\"VDB_BAD_OPTION\", `buildFromLeaves: active tile overlaps child data in ${where} at [${origin.join(\", \")}]`);\n };\n for (const lower of lowerMap.values()) {\n for (const slot of [...lower.tiles.keys(), ...lower.inactiveTiles.keys()]) {\n if (lower.children.has(slot)) overlap(\"a lower node\", lower.origin);\n }\n }\n for (const upper of upperMap.values()) {\n for (const slot of [...upper.tiles.keys(), ...upper.inactiveTiles.keys()]) {\n if (upper.children.has(slot)) overlap(\"an upper node\", upper.origin);\n }\n }\n for (const [key, tile] of rootTileMap) if (upperMap.has(key)) overlap(\"the root\", tile.origin);\n\n // -- Collapse uniform nodes into tiles, bottom-up (K4.6) ------------------\n if (collapseOpts.collapse) {\n const background = collapseOpts.background;\n const placeTile = (\n tiles: Map<number, TileValue>,\n inactive: Map<number, TileValue>,\n slot: number,\n c: { value: TileValue; active: boolean },\n ): void => {\n if (c.active) tiles.set(slot, c.value);\n else if (!tileValuesEqual(c.value, background)) inactive.set(slot, c.value);\n };\n // Leaves -> lower-node tiles.\n for (const lower of lowerMap.values()) {\n for (const [slot, leaf] of lower.children) {\n const c = constantLeaf(leaf, components);\n if (!c) continue;\n lower.children.delete(slot);\n placeTile(lower.tiles, lower.inactiveTiles, slot, c);\n if (c.active) {\n tileCounts.lower++;\n tileVoxels += 512;\n }\n }\n }\n // Lower nodes -> upper-node tiles.\n for (const upper of upperMap.values()) {\n for (const [slot, lower] of upper.children) {\n const c = constantInternal(lower, LOWER_SLOTS, background);\n if (!c) continue;\n upper.children.delete(slot);\n lowerMap.delete(nodeKey(...lower.origin));\n placeTile(upper.tiles, upper.inactiveTiles, slot, c);\n if (c.active) {\n tileCounts.lower -= LOWER_SLOTS;\n tileCounts.upper++;\n }\n }\n }\n // Upper nodes -> root tiles (an inactive background tile is erased).\n for (const [key, upper] of upperMap) {\n const c = constantInternal(upper, UPPER_SLOTS, background);\n if (!c) continue;\n upperMap.delete(key);\n if (c.active) {\n rootTileMap.set(key, { origin: upper.origin, value: c.value, active: true });\n tileCounts.upper -= UPPER_SLOTS;\n tileCounts.root++;\n } else if (!tileValuesEqual(c.value, background)) {\n rootTileMap.set(key, { origin: upper.origin, value: c.value, active: false });\n }\n }\n }\n\n // -- Deterministic breadth-first ordering + memIndex assignment ----------\n const byOrigin = (a: { origin: [number, number, number] }, b: { origin: [number, number, number] }) =>\n a.origin[0] - b.origin[0] || a.origin[1] - b.origin[1] || a.origin[2] - b.origin[2];\n\n const uppers = [...upperMap.values()].sort(byOrigin);\n uppers.forEach((u, i) => (u.memIndex = i));\n\n // Lowers grouped by parent upper (in upper order), then by slot.\n const lowers: LowerNode[] = [];\n for (const upper of uppers) {\n const slots = [...upper.children.keys()].sort((a, b) => a - b);\n for (const s of slots) lowers.push(upper.children.get(s)!);\n }\n lowers.forEach((l, i) => (l.memIndex = i));\n\n // Leaves grouped by parent lower (in lower order), then by slot.\n const leaves: Leaf[] = [];\n for (const lower of lowers) {\n const slots = [...lower.children.keys()].sort((a, b) => a - b);\n for (const s of slots) leaves.push(lower.children.get(s)!);\n }\n leaves.forEach((l, i) => (l.memIndex = i));\n\n // -- Aggregate stats bottom-up -------------------------------------------\n // Tiles first, then children: the order NanoVDB GridStats accumulates in.\n for (const lower of lowers) {\n addTileStats(lower.stats, lower.tiles, lower.origin, 4, 8);\n for (const leaf of lower.children.values()) lower.stats.merge(leaf.stats);\n }\n for (const upper of uppers) {\n addTileStats(upper.stats, upper.tiles, upper.origin, 5, 128);\n for (const lower of upper.children.values()) upper.stats.merge(lower.stats);\n }\n\n // -- Root table: children and tiles, sorted by NanoVDB key ----------------\n // Unsigned 64-bit key order (K4.12): negative coordinates sort after the\n // non-negative ones, not before them as a signed origin sort would put them.\n const rootTiles: RootTile[] = [\n ...uppers.map((u): RootTile => ({ key: keyForOrigin(u.origin), origin: u.origin, child: u })),\n ...[...rootTileMap.values()].map(\n (t): RootTile => ({ key: keyForOrigin(t.origin), origin: t.origin, child: null, value: t.value, active: t.active }),\n ),\n ].sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0));\n\n const root = newStats();\n for (const entry of rootTiles) {\n if (entry.child) {\n root.merge(entry.child.stats);\n } else if (entry.active) {\n const [x, y, z] = entry.origin;\n addTileValue(root, entry.value, TILE_STATS_WEIGHT[4096]!, entry.origin, [x + 4095, y + 4095, z + 4095]);\n }\n }\n\n let leafVoxels = 0;\n for (const leaf of leaves) leafVoxels += leaf.stats.count;\n\n return {\n uppers,\n lowers,\n leaves,\n rootTiles,\n tileCounts,\n root,\n activeVoxelCount: leafVoxels + tileVoxels,\n };\n}\n\nconst LOWER_SLOTS = 4096;\nconst UPPER_SLOTS = 32768;\n\n/** Exact tile value equality (`===` per component: `-0` equals `0`, `NaN` equals nothing). */\nfunction tileValuesEqual(a: TileValue, b: TileValue): boolean {\n if (typeof a === \"number\" || typeof b === \"number\") return a === b;\n return a[0] === b[0] && a[1] === b[1] && a[2] === b[2];\n}\n\n/**\n * OpenVDB `LeafNode::isConstant` at tolerance 0: every voxel has the same\n * active state and every value equals the first. Returns the tile, or null.\n */\nfunction constantLeaf(leaf: Leaf, components: Components): { value: TileValue; active: boolean } | null {\n const mask = leaf.valueMask;\n const w0 = mask[0]!;\n if (w0 !== 0 && w0 !== 0xffffffff) return null;\n for (let i = 1; i < 16; i++) if (mask[i] !== w0) return null;\n const v = leaf.values;\n if (components === 1) {\n const first = v[0]!;\n for (let n = 1; n < LEAF_TABLE_COUNT; n++) if (v[n] !== first) return null;\n return { value: first, active: w0 !== 0 };\n }\n const x = v[0]!;\n const y = v[1]!;\n const z = v[2]!;\n for (let n = 1; n < LEAF_TABLE_COUNT; n++) {\n if (v[n * 3] !== x || v[n * 3 + 1] !== y || v[n * 3 + 2] !== z) return null;\n }\n return { value: [x, y, z], active: w0 !== 0 };\n}\n\n/**\n * OpenVDB `InternalNode::isConstant` at tolerance 0: no child, every slot the\n * same state, every slot value equal (slots not stored are inactive background).\n */\nfunction constantInternal(\n node: LowerNode | UpperNode,\n slots: number,\n background: TileValue,\n): { value: TileValue; active: boolean } | null {\n if (node.children.size > 0) return null;\n if (node.tiles.size === slots) {\n const first = node.tiles.get(0)!;\n for (const v of node.tiles.values()) if (!tileValuesEqual(v, first)) return null;\n return { value: first, active: true };\n }\n if (node.tiles.size > 0) return null;\n if (node.inactiveTiles.size === slots) {\n const first = node.inactiveTiles.get(0)!;\n for (const v of node.inactiveTiles.values()) if (!tileValuesEqual(v, first)) return null;\n return { value: first, active: false };\n }\n // Some slots hold the background: the rest must too.\n for (const v of node.inactiveTiles.values()) if (!tileValuesEqual(v, background)) return null;\n return { value: background, active: false };\n}\n\n/** Adds an internal node's active tiles to its stats, in slot order (GridStats weighting). */\nfunction addTileStats(\n stats: StatsAccumulator,\n tiles: Map<number, TileValue>,\n origin: readonly [number, number, number],\n log2dim: number,\n childDim: number,\n): void {\n if (tiles.size === 0) return;\n const weight = TILE_STATS_WEIGHT[childDim]!;\n const mask = (1 << log2dim) - 1;\n const slots = [...tiles.keys()].sort((a, b) => a - b);\n for (const n of slots) {\n const x = origin[0] + ((n >> (2 * log2dim)) & mask) * childDim;\n const y = origin[1] + ((n >> log2dim) & mask) * childDim;\n const z = origin[2] + (n & mask) * childDim;\n addTileValue(stats, tiles.get(n)!, weight, [x, y, z], [x + childDim - 1, y + childDim - 1, z + childDim - 1]);\n }\n}\n\n/** Adds a scalar or vector tile to its accumulator. */\nfunction addTileValue(\n stats: StatsAccumulator,\n value: TileValue,\n weight: number,\n bboxMin: readonly [number, number, number],\n bboxMax: readonly [number, number, number],\n): void {\n if (typeof value === \"number\") stats.addTile(value, weight, bboxMin, bboxMax);\n else (stats as Vec3StatsAccumulator).addTileVec(value, weight, bboxMin, bboxMax);\n}\n\nimport { coordToKey } from \"./bytes.js\";\nfunction keyForOrigin(origin: [number, number, number]): bigint {\n return coordToKey(origin[0], origin[1], origin[2]);\n}\n"]}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* write-nvdb.ts — `writeNvdb`: pack one or more finished grid images into one
|
|
3
|
+
* segment of a `.nvdb` file, in NanoVDB's own `io::Segment` layout (IO.h
|
|
4
|
+
* `writeGrid`: the header and every grid's metadata first, then the grids):
|
|
5
|
+
*
|
|
6
|
+
* FileHeader(16) + [FileMetaData(176) + gridName(nameSize)] x N + [grid data] x N
|
|
7
|
+
*
|
|
8
|
+
* With one grid this is FileHeader, FileMetaData, name, image. Grid data is
|
|
9
|
+
* the image (codec NONE) or, for codec ZIP (K4.18), `io::Internal::write`'s
|
|
10
|
+
* framing: a u64 LE compressed byte count, then the zlib (RFC 1950) stream
|
|
11
|
+
* of the image, as zlib's `compress()` writes it. `FileMetaData.fileSize` is
|
|
12
|
+
* the on-disk size of that grid's data (8 + compressed bytes for ZIP).
|
|
13
|
+
*
|
|
14
|
+
* FileHeader magic is `NanoVDB2` (current file format). Each grid's
|
|
15
|
+
* FileMetaData is derived by reading the grid image itself (GridData / TreeData
|
|
16
|
+
* / RootData), so `writeNvdb` accepts any valid image — not just ones this
|
|
17
|
+
* package built — and the metadata it writes always agrees with the image
|
|
18
|
+
* (which is exactly what the loader's `mGridSize` cross-check verifies).
|
|
19
|
+
*
|
|
20
|
+
* The 8-byte name key (`io::stringHash`) is written as 0: NanoVDB's
|
|
21
|
+
* name-based `readGrid` accepts 0 as "no key" and compares the names.
|
|
22
|
+
*/
|
|
23
|
+
export interface WriteNvdbOptions {
|
|
24
|
+
/**
|
|
25
|
+
* Per-grid codec. `'none'` (default) stores the images; `'zip'` stores each
|
|
26
|
+
* as NanoVDB's `Codec::ZIP` (zlib), which native tools read when built with
|
|
27
|
+
* zlib and `NvdbFile` / `NvdbStreamReader` decode.
|
|
28
|
+
*/
|
|
29
|
+
codec?: "none" | "zip";
|
|
30
|
+
/** zlib level for `codec: 'zip'`, 0–9. Default 9 (zlib's own default, used by `compress()`, is 6). */
|
|
31
|
+
zipLevel?: number;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Serializes `images` (each a complete NanoVDB grid image) into a `.nvdb`
|
|
35
|
+
* file (`NanoVDB2` header, one segment, codec NONE or ZIP) as an
|
|
36
|
+
* `ArrayBuffer`. Grids keep their order, so a coarse preview written first
|
|
37
|
+
* arrives first in a stream.
|
|
38
|
+
*/
|
|
39
|
+
export declare function writeNvdb(images: Uint32Array[], opts?: WriteNvdbOptions): ArrayBuffer;
|