@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.
- package/CHANGELOG.md +592 -2
- package/README.md +85 -37
- package/THIRD_PARTY_NOTICES.md +197 -29
- package/dist/core/compute/atlas.d.ts +10 -2
- package/dist/core/compute/atlas.js +6 -1
- package/dist/core/compute/atlas.js.map +1 -1
- package/dist/core/compute/index.d.ts +3 -3
- package/dist/core/compute/index.js +2 -2
- package/dist/core/compute/index.js.map +1 -1
- package/dist/core/compute/majorant-build.d.ts +45 -0
- package/dist/core/compute/majorant-build.js +193 -0
- package/dist/core/compute/majorant-build.js.map +1 -0
- package/dist/core/compute/multi-scale.d.ts +155 -0
- package/dist/core/compute/multi-scale.js +684 -0
- package/dist/core/compute/multi-scale.js.map +1 -0
- package/dist/core/compute/pyramid.d.ts +22 -7
- package/dist/core/compute/pyramid.js +22 -9
- package/dist/core/compute/pyramid.js.map +1 -1
- package/dist/core/compute/shared.d.ts +1 -0
- package/dist/core/compute/shared.js.map +1 -1
- package/dist/core/compute/stats.d.ts +10 -3
- package/dist/core/compute/stats.js +6 -1
- package/dist/core/compute/stats.js.map +1 -1
- package/dist/core/compute/value-transform.d.ts +8 -1
- package/dist/core/compute/value-transform.js +9 -3
- package/dist/core/compute/value-transform.js.map +1 -1
- package/dist/core/errors.d.ts +10 -0
- package/dist/core/errors.js +8 -0
- package/dist/core/errors.js.map +1 -1
- package/dist/core/frame/emission.d.ts +85 -0
- package/dist/core/frame/emission.js +422 -0
- package/dist/core/frame/emission.js.map +1 -0
- package/dist/core/frame/environment.d.ts +47 -0
- package/dist/core/frame/environment.js +178 -0
- package/dist/core/frame/environment.js.map +1 -0
- package/dist/core/frame/index.d.ts +39 -0
- package/dist/core/frame/index.js +27 -0
- package/dist/core/frame/index.js.map +1 -0
- package/dist/core/frame/kernels.d.ts +115 -0
- package/dist/core/frame/kernels.js +496 -0
- package/dist/core/frame/kernels.js.map +1 -0
- package/dist/core/frame/limits.d.ts +67 -0
- package/dist/core/frame/limits.js +109 -0
- package/dist/core/frame/limits.js.map +1 -0
- package/dist/core/frame/phase.d.ts +17 -0
- package/dist/core/frame/phase.js +53 -0
- package/dist/core/frame/phase.js.map +1 -0
- package/dist/core/frame/pipelines.d.ts +49 -0
- package/dist/core/frame/pipelines.js +147 -0
- package/dist/core/frame/pipelines.js.map +1 -0
- package/dist/core/frame/renderer.d.ts +122 -0
- package/dist/core/frame/renderer.js +1712 -0
- package/dist/core/frame/renderer.js.map +1 -0
- package/dist/core/frame/resources.d.ts +71 -0
- package/dist/core/frame/resources.js +100 -0
- package/dist/core/frame/resources.js.map +1 -0
- package/dist/core/frame/scattering.d.ts +97 -0
- package/dist/core/frame/scattering.js +332 -0
- package/dist/core/frame/scattering.js.map +1 -0
- package/dist/core/frame/settings.d.ts +243 -0
- package/dist/core/frame/settings.js +67 -0
- package/dist/core/frame/settings.js.map +1 -0
- package/dist/core/frame/shadows.d.ts +55 -0
- package/dist/core/frame/shadows.js +460 -0
- package/dist/core/frame/shadows.js.map +1 -0
- package/dist/core/frame/types.d.ts +415 -0
- package/dist/core/frame/types.js +49 -0
- package/dist/core/frame/types.js.map +1 -0
- package/dist/core/frame/uniforms.d.ts +105 -0
- package/dist/core/frame/uniforms.js +276 -0
- package/dist/core/frame/uniforms.js.map +1 -0
- package/dist/core/frame/wgsl/chunks.d.ts +4 -0
- package/dist/core/frame/wgsl/chunks.js +728 -0
- package/dist/core/frame/wgsl/chunks.js.map +1 -0
- package/dist/core/frame/wgsl/emission.d.ts +82 -0
- package/dist/core/frame/wgsl/emission.js +353 -0
- package/dist/core/frame/wgsl/emission.js.map +1 -0
- package/dist/core/frame/wgsl/environment.d.ts +42 -0
- package/dist/core/frame/wgsl/environment.js +711 -0
- package/dist/core/frame/wgsl/environment.js.map +1 -0
- package/dist/core/frame/wgsl/gather.d.ts +55 -0
- package/dist/core/frame/wgsl/gather.js +850 -0
- package/dist/core/frame/wgsl/gather.js.map +1 -0
- package/dist/core/frame/wgsl/integrate.d.ts +78 -0
- package/dist/core/frame/wgsl/integrate.js +213 -0
- package/dist/core/frame/wgsl/integrate.js.map +1 -0
- package/dist/core/frame/wgsl/lighting.d.ts +103 -0
- package/dist/core/frame/wgsl/lighting.js +1636 -0
- package/dist/core/frame/wgsl/lighting.js.map +1 -0
- package/dist/core/frame/wgsl/occluders.d.ts +20 -0
- package/dist/core/frame/wgsl/occluders.js +95 -0
- package/dist/core/frame/wgsl/occluders.js.map +1 -0
- package/dist/core/frame/wgsl/resolve.d.ts +19 -0
- package/dist/core/frame/wgsl/resolve.js +205 -0
- package/dist/core/frame/wgsl/resolve.js.map +1 -0
- package/dist/core/frame/wgsl/shadows.d.ts +35 -0
- package/dist/core/frame/wgsl/shadows.js +328 -0
- package/dist/core/frame/wgsl/shadows.js.map +1 -0
- package/dist/core/half.d.ts +11 -0
- package/dist/core/half.js +22 -0
- package/dist/core/half.js.map +1 -0
- package/dist/core/index.d.ts +25 -3
- package/dist/core/index.js +23 -4
- package/dist/core/index.js.map +1 -1
- package/dist/core/internal.d.ts +17 -1
- package/dist/core/internal.js +18 -1
- package/dist/core/internal.js.map +1 -1
- package/dist/core/pool/image-header.d.ts +13 -2
- package/dist/core/pool/image-header.js +24 -5
- package/dist/core/pool/image-header.js.map +1 -1
- package/dist/core/pool/layout.d.ts +35 -1
- package/dist/core/pool/layout.js +51 -2
- package/dist/core/pool/layout.js.map +1 -1
- package/dist/core/pool/limits.d.ts +1 -0
- package/dist/core/pool/limits.js +1 -0
- package/dist/core/pool/limits.js.map +1 -1
- package/dist/core/pool/pool.d.ts +120 -15
- package/dist/core/pool/pool.js +255 -24
- package/dist/core/pool/pool.js.map +1 -1
- package/dist/core/reference/index.d.ts +6 -0
- package/dist/core/reference/index.js +7 -0
- package/dist/core/reference/index.js.map +1 -0
- package/dist/core/reference/renderer.d.ts +105 -0
- package/dist/core/reference/renderer.js +474 -0
- package/dist/core/reference/renderer.js.map +1 -0
- package/dist/core/reference/wgsl.d.ts +65 -0
- package/dist/core/reference/wgsl.js +803 -0
- package/dist/core/reference/wgsl.js.map +1 -0
- package/dist/{three → core}/sequence.d.ts +32 -2
- package/dist/{three → core}/sequence.js +24 -2
- package/dist/core/sequence.js.map +1 -0
- package/dist/core/source/brick-cache.d.ts +165 -0
- package/dist/core/source/brick-cache.js +627 -0
- package/dist/core/source/brick-cache.js.map +1 -0
- package/dist/core/source/dense-fill.d.ts +53 -0
- package/dist/core/source/dense-fill.js +377 -0
- package/dist/core/source/dense-fill.js.map +1 -0
- package/dist/core/source/dense.d.ts +33 -2
- package/dist/core/source/dense.js +121 -24
- package/dist/core/source/dense.js.map +1 -1
- package/dist/core/source/sparse-wgsl.d.ts +92 -0
- package/dist/core/source/sparse-wgsl.js +481 -0
- package/dist/core/source/sparse-wgsl.js.map +1 -0
- package/dist/core/source/sparse.d.ts +192 -0
- package/dist/core/source/sparse.js +762 -0
- package/dist/core/source/sparse.js.map +1 -0
- package/dist/core/source/types.d.ts +59 -2
- package/dist/core/source/types.js.map +1 -1
- package/dist/core/surface/index.d.ts +10 -0
- package/dist/core/surface/index.js +9 -0
- package/dist/core/surface/index.js.map +1 -0
- package/dist/core/surface/level-set-pass.d.ts +140 -0
- package/dist/core/surface/level-set-pass.js +291 -0
- package/dist/core/surface/level-set-pass.js.map +1 -0
- package/dist/core/surface/level-set-wgsl.d.ts +46 -0
- package/dist/core/surface/level-set-wgsl.js +99 -0
- package/dist/core/surface/level-set-wgsl.js.map +1 -0
- package/dist/core/surface/points.d.ts +109 -0
- package/dist/core/surface/points.js +226 -0
- package/dist/core/surface/points.js.map +1 -0
- package/dist/core/types.d.ts +12 -3
- package/dist/core/types.js +6 -2
- package/dist/core/types.js.map +1 -1
- package/dist/core/wgsl/api.d.ts +29 -5
- package/dist/core/wgsl/api.js +110 -27
- package/dist/core/wgsl/api.js.map +1 -1
- package/dist/core/wgsl/generate.d.ts +8 -0
- package/dist/core/wgsl/generate.js +195 -22
- package/dist/core/wgsl/generate.js.map +1 -1
- package/dist/core/wgsl/lean.d.ts +50 -0
- package/dist/core/wgsl/lean.js +327 -0
- package/dist/core/wgsl/lean.js.map +1 -0
- package/dist/core/wgsl/level-set.d.ts +5 -1
- package/dist/core/wgsl/level-set.js +11 -5
- package/dist/core/wgsl/level-set.js.map +1 -1
- package/dist/core/wgsl/majorant.d.ts +62 -0
- package/dist/core/wgsl/majorant.js +381 -0
- package/dist/core/wgsl/majorant.js.map +1 -0
- package/dist/core/wgsl/pnanovdb.chunks.generated.d.ts +2 -2
- package/dist/core/wgsl/pnanovdb.chunks.generated.js +15 -12
- package/dist/core/wgsl/pnanovdb.chunks.generated.js.map +1 -1
- package/dist/core/wgsl/types.d.ts +97 -3
- package/dist/core/wgsl/types.js.map +1 -1
- package/dist/three/coarse-first.d.ts +53 -8
- package/dist/three/coarse-first.js +24 -6
- package/dist/three/coarse-first.js.map +1 -1
- package/dist/three/compute.d.ts +12 -2
- package/dist/three/compute.js +16 -6
- package/dist/three/compute.js.map +1 -1
- package/dist/three/device.d.ts +7 -0
- package/dist/three/device.js +3 -0
- package/dist/three/device.js.map +1 -1
- package/dist/three/frame/core-environment.d.ts +38 -0
- package/dist/three/frame/core-environment.js +154 -0
- package/dist/three/frame/core-environment.js.map +1 -0
- package/dist/three/frame/core-integrate-material.d.ts +34 -0
- package/dist/three/frame/core-integrate-material.js +162 -0
- package/dist/three/frame/core-integrate-material.js.map +1 -0
- package/dist/three/frame/core-occluders.d.ts +53 -0
- package/dist/three/frame/core-occluders.js +193 -0
- package/dist/three/frame/core-occluders.js.map +1 -0
- package/dist/three/frame/core-structs.d.ts +34 -0
- package/dist/three/frame/core-structs.js +61 -0
- package/dist/three/frame/core-structs.js.map +1 -0
- package/dist/three/frame/core-textures.d.ts +41 -0
- package/dist/three/frame/core-textures.js +114 -0
- package/dist/three/frame/core-textures.js.map +1 -0
- package/dist/three/frame/emission-light.d.ts +64 -114
- package/dist/three/frame/emission-light.js +188 -567
- package/dist/three/frame/emission-light.js.map +1 -1
- package/dist/three/frame/index.d.ts +11 -31
- package/dist/three/frame/index.js +9 -29
- package/dist/three/frame/index.js.map +1 -1
- package/dist/three/frame/medium.d.ts +12 -0
- package/dist/three/frame/medium.js.map +1 -1
- package/dist/three/frame/renderer.d.ts +58 -454
- package/dist/three/frame/renderer.js +409 -1427
- package/dist/three/frame/renderer.js.map +1 -1
- package/dist/three/frame/spectrum.d.ts +0 -14
- package/dist/three/frame/spectrum.js +2 -23
- package/dist/three/frame/spectrum.js.map +1 -1
- package/dist/three/frame/three-internals.d.ts +3 -8
- package/dist/three/frame/three-internals.js +13 -11
- package/dist/three/frame/three-internals.js.map +1 -1
- package/dist/three/frame/volume-shadows.d.ts +93 -107
- package/dist/three/frame/volume-shadows.js +278 -631
- package/dist/three/frame/volume-shadows.js.map +1 -1
- package/dist/three/frame/volume.d.ts +91 -33
- package/dist/three/frame/volume.js +63 -51
- package/dist/three/frame/volume.js.map +1 -1
- package/dist/three/grid-node.d.ts +34 -1
- package/dist/three/grid-node.js +36 -1
- package/dist/three/grid-node.js.map +1 -1
- package/dist/three/index.d.ts +20 -11
- package/dist/three/index.js +21 -12
- package/dist/three/index.js.map +1 -1
- package/dist/three/internal.d.ts +14 -8
- package/dist/three/internal.js +11 -7
- package/dist/three/internal.js.map +1 -1
- package/dist/three/level-set-material.d.ts +81 -8
- package/dist/three/level-set-material.js +162 -125
- package/dist/three/level-set-material.js.map +1 -1
- package/dist/three/level-set-physical-material.d.ts +154 -0
- package/dist/three/level-set-physical-material.js +253 -0
- package/dist/three/level-set-physical-material.js.map +1 -0
- package/dist/three/level-set-velocity.d.ts +94 -0
- package/dist/three/level-set-velocity.js +208 -0
- package/dist/three/level-set-velocity.js.map +1 -0
- package/dist/three/material.d.ts +141 -11
- package/dist/three/material.js +161 -18
- package/dist/three/material.js.map +1 -1
- package/dist/three/points.d.ts +60 -4
- package/dist/three/points.js +140 -9
- package/dist/three/points.js.map +1 -1
- package/dist/three/sparse-source.d.ts +49 -117
- package/dist/three/sparse-source.js +109 -580
- package/dist/three/sparse-source.js.map +1 -1
- package/dist/three/volume-source.d.ts +11 -156
- package/dist/three/volume-source.js +29 -272
- package/dist/three/volume-source.js.map +1 -1
- package/dist/tools/cpu/brick-atlas.d.ts +36 -0
- package/dist/tools/cpu/brick-atlas.js +129 -0
- package/dist/tools/cpu/brick-atlas.js.map +1 -0
- package/dist/tools/cpu/compute.d.ts +23 -4
- package/dist/tools/cpu/compute.js +35 -14
- package/dist/tools/cpu/compute.js.map +1 -1
- package/dist/tools/cpu/lean-reads.d.ts +81 -0
- package/dist/tools/cpu/lean-reads.js +262 -0
- package/dist/tools/cpu/lean-reads.js.map +1 -0
- package/dist/tools/cpu/level-set.d.ts +42 -3
- package/dist/tools/cpu/level-set.js +59 -7
- package/dist/tools/cpu/level-set.js.map +1 -1
- package/dist/tools/cpu/majorant.d.ts +265 -0
- package/dist/tools/cpu/majorant.js +541 -0
- package/dist/tools/cpu/majorant.js.map +1 -0
- package/dist/tools/cpu/march.d.ts +93 -2
- package/dist/tools/cpu/march.js +165 -14
- package/dist/tools/cpu/march.js.map +1 -1
- package/dist/tools/cpu/mesh-decimate.d.ts +124 -0
- package/dist/tools/cpu/mesh-decimate.js +1225 -0
- package/dist/tools/cpu/mesh-decimate.js.map +1 -0
- package/dist/tools/cpu/points.d.ts +29 -0
- package/dist/tools/cpu/points.js +38 -0
- package/dist/tools/cpu/points.js.map +1 -0
- package/dist/tools/cpu/read-value.d.ts +19 -6
- package/dist/tools/cpu/read-value.js +32 -17
- package/dist/tools/cpu/read-value.js.map +1 -1
- package/dist/tools/cpu/sample-trilinear.d.ts +17 -4
- package/dist/tools/cpu/sample-trilinear.js +19 -7
- package/dist/tools/cpu/sample-trilinear.js.map +1 -1
- package/dist/tools/cpu/stride-tables.d.ts +2 -2
- package/dist/tools/cpu/stride-tables.generated.d.ts +6 -5
- package/dist/tools/cpu/stride-tables.generated.js +7 -6
- package/dist/tools/cpu/stride-tables.generated.js.map +1 -1
- package/dist/tools/cpu/stride-tables.js +1 -1
- package/dist/tools/cpu/stride-tables.js.map +1 -1
- package/dist/tools/cpu/surface-nets.d.ts +90 -0
- package/dist/tools/cpu/surface-nets.js +597 -0
- package/dist/tools/cpu/surface-nets.js.map +1 -0
- package/dist/tools/errors.d.ts +9 -0
- package/dist/tools/errors.js +9 -0
- package/dist/tools/errors.js.map +1 -1
- package/dist/tools/frame/ambient.d.ts +98 -0
- package/dist/tools/frame/ambient.js +274 -0
- package/dist/tools/frame/ambient.js.map +1 -0
- package/dist/tools/frame/blackbody.d.ts +15 -0
- package/dist/tools/frame/blackbody.js +35 -0
- package/dist/tools/frame/blackbody.js.map +1 -1
- package/dist/tools/frame/brick-cache.d.ts +165 -0
- package/dist/tools/frame/brick-cache.js +326 -0
- package/dist/tools/frame/brick-cache.js.map +1 -0
- package/dist/tools/frame/emission.d.ts +9 -0
- package/dist/tools/frame/emission.js +9 -1
- package/dist/tools/frame/emission.js.map +1 -1
- package/dist/tools/frame/environment-gpu.d.ts +111 -0
- package/dist/tools/frame/environment-gpu.js +280 -0
- package/dist/tools/frame/environment-gpu.js.map +1 -0
- package/dist/tools/frame/environment.d.ts +3 -2
- package/dist/tools/frame/environment.js +8 -4
- package/dist/tools/frame/environment.js.map +1 -1
- package/dist/tools/frame/froxel.d.ts +58 -0
- package/dist/tools/frame/froxel.js +115 -0
- package/dist/tools/frame/froxel.js.map +1 -0
- package/dist/tools/frame/grid.d.ts +90 -0
- package/dist/tools/frame/grid.js +112 -13
- package/dist/tools/frame/grid.js.map +1 -1
- package/dist/tools/frame/index.d.ts +14 -0
- package/dist/tools/frame/index.js +45 -4
- package/dist/tools/frame/index.js.map +1 -1
- package/dist/tools/frame/medium.d.ts +16 -0
- package/dist/tools/frame/medium.js +3 -1
- package/dist/tools/frame/medium.js.map +1 -1
- package/dist/tools/frame/mie.d.ts +9 -0
- package/dist/tools/frame/mie.js +18 -0
- package/dist/tools/frame/mie.js.map +1 -1
- package/dist/tools/frame/mlp-nn.d.ts +36 -0
- package/dist/tools/frame/mlp-nn.js +159 -0
- package/dist/tools/frame/mlp-nn.js.map +1 -0
- package/dist/tools/frame/mlp-tza.d.ts +29 -0
- package/dist/tools/frame/mlp-tza.js +186 -0
- package/dist/tools/frame/mlp-tza.js.map +1 -0
- package/dist/tools/frame/mlp.d.ts +183 -0
- package/dist/tools/frame/mlp.js +434 -0
- package/dist/tools/frame/mlp.js.map +1 -0
- package/dist/tools/frame/ms-exit.d.ts +42 -0
- package/dist/tools/frame/ms-exit.js +90 -0
- package/dist/tools/frame/ms-exit.js.map +1 -0
- package/dist/tools/frame/multi-scale.d.ts +152 -0
- package/dist/tools/frame/multi-scale.js +302 -0
- package/dist/tools/frame/multi-scale.js.map +1 -0
- package/dist/tools/frame/neural-weights.d.ts +6 -5
- package/dist/tools/frame/neural-weights.js +234 -235
- package/dist/tools/frame/neural-weights.js.map +1 -1
- package/dist/tools/frame/neural.js +1 -1
- package/dist/tools/frame/neural.js.map +1 -1
- package/dist/tools/frame/occluders.d.ts +51 -0
- package/dist/tools/frame/occluders.js +72 -0
- package/dist/tools/frame/occluders.js.map +1 -0
- package/dist/tools/frame/pyramid-layout.d.ts +17 -0
- package/dist/tools/frame/pyramid-layout.js +47 -0
- package/dist/tools/frame/pyramid-layout.js.map +1 -1
- package/dist/tools/frame/shadows.d.ts +110 -0
- package/dist/tools/frame/shadows.js +286 -0
- package/dist/tools/frame/shadows.js.map +1 -1
- package/dist/tools/frame/sparse.d.ts +82 -0
- package/dist/tools/frame/sparse.js +242 -1
- package/dist/tools/frame/sparse.js.map +1 -1
- package/dist/tools/frame/spectral-stack.d.ts +159 -0
- package/dist/tools/frame/spectral-stack.js +391 -0
- package/dist/tools/frame/spectral-stack.js.map +1 -0
- package/dist/tools/frame/spectral.d.ts +21 -2
- package/dist/tools/frame/spectral.js +30 -8
- package/dist/tools/frame/spectral.js.map +1 -1
- package/dist/tools/frame/stack.d.ts +115 -0
- package/dist/tools/frame/stack.js +290 -0
- package/dist/tools/frame/stack.js.map +1 -0
- package/dist/tools/frame/view.d.ts +131 -0
- package/dist/tools/frame/view.js +337 -0
- package/dist/tools/frame/view.js.map +1 -0
- package/dist/tools/index.d.ts +66 -15
- package/dist/tools/index.js +55 -11
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/internal.d.ts +58 -17
- package/dist/tools/internal.js +65 -11
- package/dist/tools/internal.js.map +1 -1
- package/dist/tools/nanovdb/build-from-vdb.d.ts +11 -2
- package/dist/tools/nanovdb/build-from-vdb.js +10 -2
- package/dist/tools/nanovdb/build-from-vdb.js.map +1 -1
- package/dist/tools/nanovdb/bytes.d.ts +17 -4
- package/dist/tools/nanovdb/bytes.js +46 -3
- package/dist/tools/nanovdb/bytes.js.map +1 -1
- package/dist/tools/nanovdb/coarse-first.d.ts +19 -3
- package/dist/tools/nanovdb/coarse-first.js +7 -1
- package/dist/tools/nanovdb/coarse-first.js.map +1 -1
- package/dist/tools/nanovdb/half.d.ts +22 -0
- package/dist/tools/nanovdb/half.js +73 -0
- package/dist/tools/nanovdb/half.js.map +1 -0
- package/dist/tools/nanovdb/index.d.ts +3 -1
- package/dist/tools/nanovdb/index.js +2 -1
- package/dist/tools/nanovdb/index.js.map +1 -1
- package/dist/tools/nanovdb/inspect.d.ts +10 -1
- package/dist/tools/nanovdb/inspect.js +8 -1
- package/dist/tools/nanovdb/inspect.js.map +1 -1
- package/dist/tools/nanovdb/leaf-codec.d.ts +35 -1
- package/dist/tools/nanovdb/leaf-codec.js +40 -4
- package/dist/tools/nanovdb/leaf-codec.js.map +1 -1
- package/dist/tools/nanovdb/quantize.d.ts +39 -10
- package/dist/tools/nanovdb/quantize.js +96 -44
- package/dist/tools/nanovdb/quantize.js.map +1 -1
- package/dist/tools/nanovdb/resample.d.ts +20 -3
- package/dist/tools/nanovdb/resample.js +20 -7
- package/dist/tools/nanovdb/resample.js.map +1 -1
- package/dist/tools/nanovdb/serialize.d.ts +34 -3
- package/dist/tools/nanovdb/serialize.js +51 -7
- package/dist/tools/nanovdb/serialize.js.map +1 -1
- package/dist/tools/nanovdb/stats.d.ts +34 -5
- package/dist/tools/nanovdb/stats.js +87 -11
- package/dist/tools/nanovdb/stats.js.map +1 -1
- package/dist/tools/nanovdb/transform.d.ts +4 -0
- package/dist/tools/nanovdb/transform.js +3 -0
- package/dist/tools/nanovdb/transform.js.map +1 -1
- package/dist/tools/nanovdb/tree.d.ts +14 -3
- package/dist/tools/nanovdb/tree.js +25 -13
- package/dist/tools/nanovdb/tree.js.map +1 -1
- package/dist/tools/nanovdb/write-nvdb.d.ts +47 -12
- package/dist/tools/nanovdb/write-nvdb.js +108 -28
- package/dist/tools/nanovdb/write-nvdb.js.map +1 -1
- package/dist/tools/nvdb-file.d.ts +77 -7
- package/dist/tools/nvdb-file.js +177 -20
- package/dist/tools/nvdb-file.js.map +1 -1
- package/dist/tools/nvdb-stream.d.ts +23 -7
- package/dist/tools/nvdb-stream.js +78 -34
- package/dist/tools/nvdb-stream.js.map +1 -1
- package/dist/tools/reference/analytic.d.ts +41 -0
- package/dist/tools/reference/analytic.js +95 -0
- package/dist/tools/reference/analytic.js.map +1 -0
- package/dist/tools/reference/environment.d.ts +62 -0
- package/dist/tools/reference/environment.js +154 -0
- package/dist/tools/reference/environment.js.map +1 -0
- package/dist/tools/reference/index.d.ts +12 -0
- package/dist/tools/reference/index.js +13 -0
- package/dist/tools/reference/index.js.map +1 -0
- package/dist/tools/reference/media.d.ts +14 -0
- package/dist/tools/reference/media.js +30 -0
- package/dist/tools/reference/media.js.map +1 -0
- package/dist/tools/reference/rng.d.ts +20 -0
- package/dist/tools/reference/rng.js +30 -0
- package/dist/tools/reference/rng.js.map +1 -0
- package/dist/tools/reference/scene.d.ts +312 -0
- package/dist/tools/reference/scene.js +169 -0
- package/dist/tools/reference/scene.js.map +1 -0
- package/dist/tools/reference/tables.d.ts +98 -0
- package/dist/tools/reference/tables.js +264 -0
- package/dist/tools/reference/tables.js.map +1 -0
- package/dist/tools/reference/trace.d.ts +160 -0
- package/dist/tools/reference/trace.js +497 -0
- package/dist/tools/reference/trace.js.map +1 -0
- package/dist/tools/vdb/blosc-write.d.ts +43 -0
- package/dist/tools/vdb/blosc-write.js +197 -0
- package/dist/tools/vdb/blosc-write.js.map +1 -0
- package/dist/tools/vdb/blosc.d.ts +35 -5
- package/dist/tools/vdb/blosc.js +82 -26
- package/dist/tools/vdb/blosc.js.map +1 -1
- package/dist/tools/vdb/blosclz.d.ts +33 -0
- package/dist/tools/vdb/blosclz.js +112 -0
- package/dist/tools/vdb/blosclz.js.map +1 -0
- package/dist/tools/vdb/byte-reader.d.ts +13 -3
- package/dist/tools/vdb/byte-reader.js +26 -4
- package/dist/tools/vdb/byte-reader.js.map +1 -1
- package/dist/tools/vdb/compression.d.ts +13 -5
- package/dist/tools/vdb/compression.js +13 -2
- package/dist/tools/vdb/compression.js.map +1 -1
- package/dist/tools/vdb/grid.js +8 -2
- package/dist/tools/vdb/grid.js.map +1 -1
- package/dist/tools/vdb/index.d.ts +2 -1
- package/dist/tools/vdb/index.js.map +1 -1
- package/dist/tools/vdb/lz4.d.ts +30 -0
- package/dist/tools/vdb/lz4.js +228 -0
- package/dist/tools/vdb/lz4.js.map +1 -0
- package/dist/tools/vdb/metadata.js +1 -1
- package/dist/tools/vdb/metadata.js.map +1 -1
- package/dist/tools/vdb/parse.d.ts +24 -2
- package/dist/tools/vdb/parse.js +102 -32
- package/dist/tools/vdb/parse.js.map +1 -1
- package/dist/tools/vdb/point-render-data.d.ts +49 -1
- package/dist/tools/vdb/point-render-data.js +91 -11
- package/dist/tools/vdb/point-render-data.js.map +1 -1
- package/dist/tools/vdb/points.d.ts +26 -0
- package/dist/tools/vdb/points.js +46 -5
- package/dist/tools/vdb/points.js.map +1 -1
- package/dist/tools/vdb/types.d.ts +59 -2
- package/dist/tools/vdb/types.js.map +1 -1
- package/dist/tools/wvdb/bake.d.ts +38 -9
- package/dist/tools/wvdb/bake.js +63 -13
- package/dist/tools/wvdb/bake.js.map +1 -1
- package/dist/tools/wvdb/dense.d.ts +80 -1
- package/dist/tools/wvdb/dense.js +67 -8
- package/dist/tools/wvdb/dense.js.map +1 -1
- package/dist/tools/wvdb/format.d.ts +59 -0
- package/dist/tools/wvdb/format.js +3 -0
- package/dist/tools/wvdb/format.js.map +1 -1
- package/dist/tools/wvdb/index.d.ts +1 -0
- package/dist/tools/wvdb/index.js +1 -0
- package/dist/tools/wvdb/index.js.map +1 -1
- package/dist/tools/wvdb/level-set.d.ts +10 -2
- package/dist/tools/wvdb/level-set.js.map +1 -1
- package/dist/tools/wvdb/nanovdb.d.ts +6 -1
- package/dist/tools/wvdb/nanovdb.js +5 -1
- package/dist/tools/wvdb/nanovdb.js.map +1 -1
- package/dist/tools/wvdb/partial.d.ts +74 -0
- package/dist/tools/wvdb/partial.js +233 -0
- package/dist/tools/wvdb/partial.js.map +1 -0
- package/dist/tools/wvdb/read.d.ts +77 -1
- package/dist/tools/wvdb/read.js +30 -7
- package/dist/tools/wvdb/read.js.map +1 -1
- package/dist/tools/wvdb/write.d.ts +23 -1
- package/dist/tools/wvdb/write.js +8 -1
- package/dist/tools/wvdb/write.js.map +1 -1
- package/package.json +2 -1
- package/vendor/VENDOR.md +21 -6
- package/vendor/pnanovdb.wgsl +52 -14
- package/dist/three/frame/blackbody.d.ts +0 -42
- package/dist/three/frame/blackbody.js +0 -74
- package/dist/three/frame/blackbody.js.map +0 -1
- package/dist/three/frame/chunks.d.ts +0 -157
- package/dist/three/frame/chunks.js +0 -559
- package/dist/three/frame/chunks.js.map +0 -1
- package/dist/three/frame/density-material.d.ts +0 -53
- package/dist/three/frame/density-material.js +0 -353
- package/dist/three/frame/density-material.js.map +0 -1
- package/dist/three/frame/environment-grid-material.d.ts +0 -21
- package/dist/three/frame/environment-grid-material.js +0 -184
- package/dist/three/frame/environment-grid-material.js.map +0 -1
- package/dist/three/frame/environment-material.d.ts +0 -22
- package/dist/three/frame/environment-material.js +0 -87
- package/dist/three/frame/environment-material.js.map +0 -1
- package/dist/three/frame/grid-mip-material.d.ts +0 -12
- package/dist/three/frame/grid-mip-material.js +0 -61
- package/dist/three/frame/grid-mip-material.js.map +0 -1
- package/dist/three/frame/inject-material.d.ts +0 -56
- package/dist/three/frame/inject-material.js +0 -424
- package/dist/three/frame/inject-material.js.map +0 -1
- package/dist/three/frame/integrate-material.d.ts +0 -26
- package/dist/three/frame/integrate-material.js +0 -198
- package/dist/three/frame/integrate-material.js.map +0 -1
- package/dist/three/frame/layout.d.ts +0 -34
- package/dist/three/frame/layout.js +0 -48
- package/dist/three/frame/layout.js.map +0 -1
- package/dist/three/frame/lighting-material.d.ts +0 -41
- package/dist/three/frame/lighting-material.js +0 -257
- package/dist/three/frame/lighting-material.js.map +0 -1
- package/dist/three/frame/lights.d.ts +0 -11
- package/dist/three/frame/lights.js +0 -16
- package/dist/three/frame/lights.js.map +0 -1
- package/dist/three/frame/neural.d.ts +0 -14
- package/dist/three/frame/neural.js +0 -105
- package/dist/three/frame/neural.js.map +0 -1
- package/dist/three/frame/phase.d.ts +0 -44
- package/dist/three/frame/phase.js +0 -80
- package/dist/three/frame/phase.js.map +0 -1
- package/dist/three/frame/shadow-density-material.d.ts +0 -30
- package/dist/three/frame/shadow-density-material.js +0 -123
- package/dist/three/frame/shadow-density-material.js.map +0 -1
- package/dist/three/frame/temporal-material.d.ts +0 -29
- package/dist/three/frame/temporal-material.js +0 -152
- package/dist/three/frame/temporal-material.js.map +0 -1
- package/dist/three/frame/transmittance-material.d.ts +0 -31
- package/dist/three/frame/transmittance-material.js +0 -315
- package/dist/three/frame/transmittance-material.js.map +0 -1
- package/dist/three/frame/view.d.ts +0 -103
- package/dist/three/frame/view.js +0 -296
- package/dist/three/frame/view.js.map +0 -1
- package/dist/three/frame/volume-shadow-material.d.ts +0 -36
- package/dist/three/frame/volume-shadow-material.js +0 -128
- package/dist/three/frame/volume-shadow-material.js.map +0 -1
- package/dist/three/sequence.js.map +0 -1
|
@@ -0,0 +1,1225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `vdbLevelSetMeshDecimate` (K3.7c-b): fewer triangles for a `vdbLevelSetMesh` mesh, for collision and export,
|
|
3
|
+
* with the error bounded by the level set itself.
|
|
4
|
+
*
|
|
5
|
+
* Method: edge collapses ordered by the quadric error metric of M. Garland and P. Heckbert, "Surface
|
|
6
|
+
* simplification using quadric error metrics" (SIGGRAPH 1997). The method is the paper's, the code is ours; no code
|
|
7
|
+
* was ported. Where we depart from the paper (DIVERGES V290):
|
|
8
|
+
* - a collapse moves a vertex onto its neighbour (the paper solves for a new position), so the output's vertices
|
|
9
|
+
* are a subset of the input's and keep their positions and normals bit for bit;
|
|
10
|
+
* - only edges are collapsed (no pair contraction of unconnected vertices), and only where the surface stays a
|
|
11
|
+
* 2-manifold (the link condition), so the topology of the input is kept;
|
|
12
|
+
* - the quadric only orders the collapses. What accepts or rejects one is the grid: every sample point of the
|
|
13
|
+
* triangles a collapse changes must lie within `error` voxels of the trilinear iso surface (first-order
|
|
14
|
+
* distance `|f - iso| / |grad f|` in index space, plus the rest after one Newton step near the bound), and each such triangle must keep its side (its normal agrees
|
|
15
|
+
* with the field gradient and with the triangle it replaces) and must not be a needle;
|
|
16
|
+
* - boundary vertices never move or go away, so a chunk of `vdbLevelSetMeshChunks` keeps its seams and the
|
|
17
|
+
* decimated chunks still join on equal positions.
|
|
18
|
+
*
|
|
19
|
+
* K3.7c-b2 added, each off by default (the default output is byte for byte what K3.7c-b made):
|
|
20
|
+
* - `creases`: a collapse that leaves the surface where it is (every changed triangle lies in the plane of the one
|
|
21
|
+
* it replaces: a merge inside a flat face or along a straight crease) passes without the field, so rows of
|
|
22
|
+
* vertices along a sharp edge merge although the mesher's triangles there are already past the bound;
|
|
23
|
+
* - `repair`: the mesher's saddle edges (four triangles on one edge) are split into two sheets first, and a
|
|
24
|
+
* triangle that faces against the field may be replaced by one that does not;
|
|
25
|
+
* - `bound: 'hard'`: the bound holds at every point of a changed triangle, not only at sample points (a Lipschitz
|
|
26
|
+
* bound from per-cell gradient intervals of the trilinear field; see `hardBound`).
|
|
27
|
+
* It also caches the field check of a triangle (the same triangle is proposed again whenever a neighbour changes)
|
|
28
|
+
* and runs the cheap shape checks of a collapse before any field sample: pure speed, same result.
|
|
29
|
+
*/
|
|
30
|
+
import { VDBToolsError } from "../errors.js";
|
|
31
|
+
import { GRID_OFF_MAP, MAP_OFF_INVMATD, MAP_OFF_MATD, MAP_OFF_VECD } from "../nanovdb/bytes.js";
|
|
32
|
+
import { forEachTreeRegion, indexBBox, leafVoxels, vdbReadValueF32Cpu } from "./march.js";
|
|
33
|
+
/** Sample spacing on a changed triangle, in voxels, and the most subdivisions of its longest edge. */
|
|
34
|
+
const SAMPLE_SPACING = 0.25;
|
|
35
|
+
const SAMPLE_MAX_DIVISIONS = 48;
|
|
36
|
+
/** The same for `bound: 'hard'`: a finer lattice leaves less to the in-plane gradient bound. */
|
|
37
|
+
const HARD_SPACING = 0.125;
|
|
38
|
+
const HARD_MAX_DIVISIONS = 96;
|
|
39
|
+
/** A changed triangle may turn at most this far from the one it replaces (cosine). */
|
|
40
|
+
const MIN_TURN_COSINE = 0.2;
|
|
41
|
+
/**
|
|
42
|
+
* A changed triangle thinner than this (twice its area over its longest edge squared: about the smallest angle in
|
|
43
|
+
* radians for a needle) is refused unless the one it replaces was thinner still.
|
|
44
|
+
*/
|
|
45
|
+
const MIN_SHAPE = 0.1;
|
|
46
|
+
/** Tie-break of the collapse order: this times the squared edge length (voxels²) is added to the quadric error. */
|
|
47
|
+
const LENGTH_WEIGHT = 1e-4;
|
|
48
|
+
/** Decoded leaf blocks kept by the field sampler (512 floats each). */
|
|
49
|
+
const BLOCK_CACHE = 32768;
|
|
50
|
+
/** `creases`: how far a surface-preserving collapse may move the surface off the input mesh, as a share of `error`. */
|
|
51
|
+
const CREASE_TOLERANCE = 0.01;
|
|
52
|
+
/** Cached triangle checks kept per vertex before that vertex's cache is dropped. */
|
|
53
|
+
const TRIANGLE_CACHE = 4096;
|
|
54
|
+
/** Per-cell gradient intervals kept (`bound: 'hard'`). */
|
|
55
|
+
const CELL_CACHE = 1 << 20;
|
|
56
|
+
/**
|
|
57
|
+
* The trilinear field in index space, from a leaf-block cache: `sample(x, y, z, out)` writes the value and the
|
|
58
|
+
* gradient (per voxel) into `out[0..3]`.
|
|
59
|
+
*/
|
|
60
|
+
function fieldSampler(image) {
|
|
61
|
+
const OFF = 65536;
|
|
62
|
+
const inRange = (b) => b >= -OFF && b < OFF;
|
|
63
|
+
const key = (bx, by, bz) => ((bx + OFF) * 131072 + (by + OFF)) * 131072 + (bz + OFF);
|
|
64
|
+
const leafAt = new Map();
|
|
65
|
+
forEachTreeRegion(image, (r) => {
|
|
66
|
+
if (r.level !== 0)
|
|
67
|
+
return;
|
|
68
|
+
const bx = r.origin[0] >> 3;
|
|
69
|
+
const by = r.origin[1] >> 3;
|
|
70
|
+
const bz = r.origin[2] >> 3;
|
|
71
|
+
if (inRange(bx) && inRange(by) && inRange(bz))
|
|
72
|
+
leafAt.set(key(bx, by, bz), r.leaf);
|
|
73
|
+
});
|
|
74
|
+
const cache = new Map();
|
|
75
|
+
let lx = NaN;
|
|
76
|
+
let ly = NaN;
|
|
77
|
+
let lz = NaN;
|
|
78
|
+
let last = 0;
|
|
79
|
+
const voxel = (x, y, z) => {
|
|
80
|
+
const bx = x >> 3;
|
|
81
|
+
const by = y >> 3;
|
|
82
|
+
const bz = z >> 3;
|
|
83
|
+
if (bx !== lx || by !== ly || bz !== lz) {
|
|
84
|
+
if (!inRange(bx) || !inRange(by) || !inRange(bz))
|
|
85
|
+
return vdbReadValueF32Cpu(image, [x, y, z]);
|
|
86
|
+
const k = key(bx, by, bz);
|
|
87
|
+
let b = cache.get(k);
|
|
88
|
+
if (b === undefined) {
|
|
89
|
+
const leaf = leafAt.get(k);
|
|
90
|
+
// A block with no leaf lies in one tile or the background: one value.
|
|
91
|
+
b = leaf !== undefined ? leafVoxels(image, leaf).values : vdbReadValueF32Cpu(image, [bx * 8, by * 8, bz * 8]);
|
|
92
|
+
if (cache.size >= BLOCK_CACHE)
|
|
93
|
+
cache.delete(cache.keys().next().value);
|
|
94
|
+
cache.set(k, b);
|
|
95
|
+
}
|
|
96
|
+
lx = bx;
|
|
97
|
+
ly = by;
|
|
98
|
+
lz = bz;
|
|
99
|
+
last = b;
|
|
100
|
+
}
|
|
101
|
+
return typeof last === "number" ? last : last[((x & 7) << 6) | ((y & 7) << 3) | (z & 7)];
|
|
102
|
+
};
|
|
103
|
+
let calls = 0;
|
|
104
|
+
const sample = (x, y, z, out) => {
|
|
105
|
+
calls++;
|
|
106
|
+
const i = Math.floor(x);
|
|
107
|
+
const j = Math.floor(y);
|
|
108
|
+
const k = Math.floor(z);
|
|
109
|
+
const u = x - i;
|
|
110
|
+
const w = y - j;
|
|
111
|
+
const s = z - k;
|
|
112
|
+
const v000 = voxel(i, j, k);
|
|
113
|
+
const v001 = voxel(i, j, k + 1);
|
|
114
|
+
const v010 = voxel(i, j + 1, k);
|
|
115
|
+
const v011 = voxel(i, j + 1, k + 1);
|
|
116
|
+
const v100 = voxel(i + 1, j, k);
|
|
117
|
+
const v101 = voxel(i + 1, j, k + 1);
|
|
118
|
+
const v110 = voxel(i + 1, j + 1, k);
|
|
119
|
+
const v111 = voxel(i + 1, j + 1, k + 1);
|
|
120
|
+
// Along z, then y, then x.
|
|
121
|
+
const a00 = v000 + (v001 - v000) * s;
|
|
122
|
+
const a01 = v010 + (v011 - v010) * s;
|
|
123
|
+
const a10 = v100 + (v101 - v100) * s;
|
|
124
|
+
const a11 = v110 + (v111 - v110) * s;
|
|
125
|
+
const b0 = a00 + (a01 - a00) * w;
|
|
126
|
+
const b1 = a10 + (a11 - a10) * w;
|
|
127
|
+
out[0] = b0 + (b1 - b0) * u;
|
|
128
|
+
out[1] = b1 - b0;
|
|
129
|
+
out[2] = a01 - a00 + (a11 - a10 - (a01 - a00)) * u;
|
|
130
|
+
const dz0 = v001 - v000 + (v011 - v010 - (v001 - v000)) * w;
|
|
131
|
+
const dz1 = v101 - v100 + (v111 - v110 - (v101 - v100)) * w;
|
|
132
|
+
out[3] = dz0 + (dz1 - dz0) * u;
|
|
133
|
+
};
|
|
134
|
+
return { sample, voxel, count: () => calls };
|
|
135
|
+
}
|
|
136
|
+
/** A binary min-heap of collapse candidates, ordered by (cost, from, to). */
|
|
137
|
+
class CollapseHeap {
|
|
138
|
+
cost = [];
|
|
139
|
+
from = [];
|
|
140
|
+
to = [];
|
|
141
|
+
stamp = [];
|
|
142
|
+
get size() {
|
|
143
|
+
return this.cost.length;
|
|
144
|
+
}
|
|
145
|
+
less(i, j) {
|
|
146
|
+
if (this.cost[i] !== this.cost[j])
|
|
147
|
+
return this.cost[i] < this.cost[j];
|
|
148
|
+
if (this.from[i] !== this.from[j])
|
|
149
|
+
return this.from[i] < this.from[j];
|
|
150
|
+
return this.to[i] < this.to[j];
|
|
151
|
+
}
|
|
152
|
+
swap(i, j) {
|
|
153
|
+
for (const a of [this.cost, this.from, this.to, this.stamp]) {
|
|
154
|
+
const t = a[i];
|
|
155
|
+
a[i] = a[j];
|
|
156
|
+
a[j] = t;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
push(cost, from, to, stamp) {
|
|
160
|
+
let i = this.cost.length;
|
|
161
|
+
this.cost.push(cost);
|
|
162
|
+
this.from.push(from);
|
|
163
|
+
this.to.push(to);
|
|
164
|
+
this.stamp.push(stamp);
|
|
165
|
+
while (i > 0) {
|
|
166
|
+
const p = (i - 1) >> 1;
|
|
167
|
+
if (!this.less(i, p))
|
|
168
|
+
break;
|
|
169
|
+
this.swap(i, p);
|
|
170
|
+
i = p;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/** Removes the smallest candidate into `out` (cost, from, to, stamp). */
|
|
174
|
+
pop(out) {
|
|
175
|
+
out[0] = this.cost[0];
|
|
176
|
+
out[1] = this.from[0];
|
|
177
|
+
out[2] = this.to[0];
|
|
178
|
+
out[3] = this.stamp[0];
|
|
179
|
+
const n = this.cost.length - 1;
|
|
180
|
+
this.swap(0, n);
|
|
181
|
+
this.cost.pop();
|
|
182
|
+
this.from.pop();
|
|
183
|
+
this.to.pop();
|
|
184
|
+
this.stamp.pop();
|
|
185
|
+
for (let i = 0;;) {
|
|
186
|
+
const l = 2 * i + 1;
|
|
187
|
+
const r = l + 1;
|
|
188
|
+
let m = i;
|
|
189
|
+
if (l < n && this.less(l, m))
|
|
190
|
+
m = l;
|
|
191
|
+
if (r < n && this.less(r, m))
|
|
192
|
+
m = r;
|
|
193
|
+
if (m === i)
|
|
194
|
+
break;
|
|
195
|
+
this.swap(i, m);
|
|
196
|
+
i = m;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Calls `each(a, b, forward, backward)` for every undirected edge `a < b` that is not used exactly once in each
|
|
202
|
+
* direction (a boundary edge is used once; a non-manifold or misoriented edge otherwise).
|
|
203
|
+
*/
|
|
204
|
+
function irregularEdges(tri, triAlive, nT, nV, each) {
|
|
205
|
+
// One key per directed edge: 2 * (min * nV + max) + direction; sorted, an undirected edge's uses are adjacent.
|
|
206
|
+
let alive = 0;
|
|
207
|
+
for (let t = 0; t < nT; t++)
|
|
208
|
+
if (triAlive[t])
|
|
209
|
+
alive++;
|
|
210
|
+
const keys = new Float64Array(3 * alive);
|
|
211
|
+
let n = 0;
|
|
212
|
+
for (let t = 0; t < nT; t++) {
|
|
213
|
+
if (!triAlive[t])
|
|
214
|
+
continue;
|
|
215
|
+
for (let e = 0; e < 3; e++) {
|
|
216
|
+
const a = tri[3 * t + e];
|
|
217
|
+
const b = tri[3 * t + ((e + 1) % 3)];
|
|
218
|
+
keys[n++] = 2 * (Math.min(a, b) * nV + Math.max(a, b)) + (a < b ? 0 : 1);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
keys.sort();
|
|
222
|
+
for (let k = 0; k < n;) {
|
|
223
|
+
const und = Math.floor(keys[k] / 2);
|
|
224
|
+
let fwd = 0;
|
|
225
|
+
let bwd = 0;
|
|
226
|
+
for (; k < n && Math.floor(keys[k] / 2) === und; k++) {
|
|
227
|
+
if (keys[k] % 2 === 0)
|
|
228
|
+
fwd++;
|
|
229
|
+
else
|
|
230
|
+
bwd++;
|
|
231
|
+
}
|
|
232
|
+
if (fwd === 1 && bwd === 1)
|
|
233
|
+
continue;
|
|
234
|
+
const a = Math.floor(und / nV);
|
|
235
|
+
each(a, und - a * nV, fwd, bwd);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* `repair`: splits the mesher's saddle edges. A saddle edge has four triangles (two in each direction): two sheets
|
|
240
|
+
* of the surface that the mesher joined because both pass through the same pair of cells. Around the edge the four
|
|
241
|
+
* triangles bound four wedges, alternately inside and outside; the field at the edge's midpoint says which pair of
|
|
242
|
+
* wedges is joined through the edge, and so which two triangles belong to one sheet (those bounding the same wedge
|
|
243
|
+
* of the other kind). Each end vertex whose triangles then fall into more than one fan is duplicated, one copy per
|
|
244
|
+
* fan. Rewrites `tri` in place and returns the source vertex of each added vertex (ids `nV`, `nV + 1`, ...).
|
|
245
|
+
*/
|
|
246
|
+
function splitSaddleEdges(tri, triAlive, nT, nV, P, side, above) {
|
|
247
|
+
const saddles = new Map();
|
|
248
|
+
irregularEdges(tri, triAlive, nT, nV, (a, b, fwd, bwd) => {
|
|
249
|
+
if (fwd === 2 && bwd === 2)
|
|
250
|
+
saddles.set(a * nV + b, { a, b, tris: [], pairs: null });
|
|
251
|
+
});
|
|
252
|
+
if (saddles.size === 0)
|
|
253
|
+
return { added: [], split: 0, left: 0 };
|
|
254
|
+
const ends = new Map();
|
|
255
|
+
for (const s of saddles.values()) {
|
|
256
|
+
ends.set(s.a, []);
|
|
257
|
+
ends.set(s.b, []);
|
|
258
|
+
}
|
|
259
|
+
for (let t = 0; t < nT; t++) {
|
|
260
|
+
if (!triAlive[t])
|
|
261
|
+
continue;
|
|
262
|
+
for (let e = 0; e < 3; e++) {
|
|
263
|
+
const a = tri[3 * t + e];
|
|
264
|
+
const b = tri[3 * t + ((e + 1) % 3)];
|
|
265
|
+
saddles.get(Math.min(a, b) * nV + Math.max(a, b))?.tris.push(t);
|
|
266
|
+
ends.get(a)?.push(t);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
// The two sheets of each saddle edge.
|
|
270
|
+
for (const s of saddles.values()) {
|
|
271
|
+
const ax = P[3 * s.a];
|
|
272
|
+
const ay = P[3 * s.a + 1];
|
|
273
|
+
const az = P[3 * s.a + 2];
|
|
274
|
+
let ux = P[3 * s.b] - ax;
|
|
275
|
+
let uy = P[3 * s.b + 1] - ay;
|
|
276
|
+
let uz = P[3 * s.b + 2] - az;
|
|
277
|
+
const ul = Math.hypot(ux, uy, uz);
|
|
278
|
+
if (!(ul > 0) || s.tris.length !== 4)
|
|
279
|
+
continue;
|
|
280
|
+
ux /= ul;
|
|
281
|
+
uy /= ul;
|
|
282
|
+
uz /= ul;
|
|
283
|
+
let e1x = 0;
|
|
284
|
+
let e1y = 0;
|
|
285
|
+
let e1z = 0;
|
|
286
|
+
const around = [];
|
|
287
|
+
let ok = true;
|
|
288
|
+
for (const t of s.tris) {
|
|
289
|
+
const k = tri[3 * t] === s.a ? 0 : tri[3 * t + 1] === s.a ? 1 : 2;
|
|
290
|
+
const forward = tri[3 * t + ((k + 1) % 3)] === s.b;
|
|
291
|
+
const c = forward ? tri[3 * t + ((k + 2) % 3)] : tri[3 * t + ((k + 1) % 3)];
|
|
292
|
+
// The third vertex, perpendicular to the edge.
|
|
293
|
+
let rx = P[3 * c] - ax;
|
|
294
|
+
let ry = P[3 * c + 1] - ay;
|
|
295
|
+
let rz = P[3 * c + 2] - az;
|
|
296
|
+
const along = rx * ux + ry * uy + rz * uz;
|
|
297
|
+
rx -= along * ux;
|
|
298
|
+
ry -= along * uy;
|
|
299
|
+
rz -= along * uz;
|
|
300
|
+
const rl = Math.hypot(rx, ry, rz);
|
|
301
|
+
if (!(rl > 0)) {
|
|
302
|
+
ok = false;
|
|
303
|
+
break;
|
|
304
|
+
}
|
|
305
|
+
if (around.length === 0) {
|
|
306
|
+
e1x = rx / rl;
|
|
307
|
+
e1y = ry / rl;
|
|
308
|
+
e1z = rz / rl;
|
|
309
|
+
}
|
|
310
|
+
const e2x = uy * e1z - uz * e1y;
|
|
311
|
+
const e2y = uz * e1x - ux * e1z;
|
|
312
|
+
const e2z = ux * e1y - uy * e1x;
|
|
313
|
+
around.push({ t, forward, angle: Math.atan2(rx * e2x + ry * e2y + rz * e2z, rx * e1x + ry * e1y + rz * e1z) });
|
|
314
|
+
}
|
|
315
|
+
if (!ok)
|
|
316
|
+
continue;
|
|
317
|
+
around.sort((x, y) => x.angle - y.angle || x.t - y.t);
|
|
318
|
+
// An oriented surface alternates directions around the edge.
|
|
319
|
+
if (around[0].forward === around[1].forward || around[1].forward === around[2].forward || around[2].forward === around[3].forward)
|
|
320
|
+
continue;
|
|
321
|
+
// A forward triangle's normal points toward increasing angle: the wedge after it is on its positive side
|
|
322
|
+
// (the outside; the inside for a mirrored map, whose index-space triangles face inward).
|
|
323
|
+
const outsideAfter0 = around[0].forward === (side === 1);
|
|
324
|
+
const midOutside = above((ax + P[3 * s.b]) / 2, (ay + P[3 * s.b + 1]) / 2, (az + P[3 * s.b + 2]) / 2);
|
|
325
|
+
// Outside at the edge: the outside wedges are joined through it, so each sheet wraps an inside wedge.
|
|
326
|
+
const pairAfter0 = midOutside ? !outsideAfter0 : outsideAfter0;
|
|
327
|
+
s.pairs = pairAfter0
|
|
328
|
+
? [
|
|
329
|
+
[around[0].t, around[1].t],
|
|
330
|
+
[around[2].t, around[3].t],
|
|
331
|
+
]
|
|
332
|
+
: [
|
|
333
|
+
[around[1].t, around[2].t],
|
|
334
|
+
[around[3].t, around[0].t],
|
|
335
|
+
];
|
|
336
|
+
}
|
|
337
|
+
// The fans of each end vertex: triangles joined across its edges (a saddle edge joins only within a sheet).
|
|
338
|
+
const rewrites = [];
|
|
339
|
+
const added = [];
|
|
340
|
+
const sortedEnds = [...ends.keys()].sort((x, y) => x - y);
|
|
341
|
+
for (const v of sortedEnds) {
|
|
342
|
+
const T = ends.get(v);
|
|
343
|
+
const parent = T.map((_, i) => i);
|
|
344
|
+
const find = (i) => {
|
|
345
|
+
while (parent[i] !== i)
|
|
346
|
+
i = parent[i] = parent[parent[i]];
|
|
347
|
+
return i;
|
|
348
|
+
};
|
|
349
|
+
const slot = new Map();
|
|
350
|
+
T.forEach((t, i) => slot.set(t, i));
|
|
351
|
+
const byNeighbour = new Map();
|
|
352
|
+
T.forEach((t, i) => {
|
|
353
|
+
for (let e = 0; e < 3; e++) {
|
|
354
|
+
const w = tri[3 * t + e];
|
|
355
|
+
if (w === v)
|
|
356
|
+
continue;
|
|
357
|
+
let list = byNeighbour.get(w);
|
|
358
|
+
if (!list)
|
|
359
|
+
byNeighbour.set(w, (list = []));
|
|
360
|
+
list.push(i);
|
|
361
|
+
}
|
|
362
|
+
});
|
|
363
|
+
for (const [w, list] of byNeighbour) {
|
|
364
|
+
const s = saddles.get(Math.min(v, w) * nV + Math.max(v, w));
|
|
365
|
+
if (s?.pairs) {
|
|
366
|
+
for (const [x, y] of s.pairs)
|
|
367
|
+
parent[find(slot.get(x))] = find(slot.get(y));
|
|
368
|
+
}
|
|
369
|
+
else {
|
|
370
|
+
for (let i = 1; i < list.length; i++)
|
|
371
|
+
parent[find(list[i])] = find(list[0]);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
// The fan with the lowest triangle keeps the vertex; the others get copies, in triangle order.
|
|
375
|
+
const copy = new Map();
|
|
376
|
+
copy.set(find(0), v);
|
|
377
|
+
T.forEach((t, i) => {
|
|
378
|
+
const root = find(i);
|
|
379
|
+
let id = copy.get(root);
|
|
380
|
+
if (id === undefined) {
|
|
381
|
+
id = nV + added.length;
|
|
382
|
+
added.push(v);
|
|
383
|
+
copy.set(root, id);
|
|
384
|
+
}
|
|
385
|
+
if (id !== v)
|
|
386
|
+
rewrites.push(t, v, id);
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
// Which saddle edges came apart (computed on the original ids, before rewriting).
|
|
390
|
+
const idAfter = new Map();
|
|
391
|
+
for (let i = 0; i < rewrites.length; i += 3)
|
|
392
|
+
idAfter.set(rewrites[i] * nV + rewrites[i + 1], rewrites[i + 2]);
|
|
393
|
+
let split = 0;
|
|
394
|
+
for (const s of saddles.values()) {
|
|
395
|
+
if (!s.pairs)
|
|
396
|
+
continue;
|
|
397
|
+
const id = (t, v) => idAfter.get(t * nV + v) ?? v;
|
|
398
|
+
const [x, y] = [s.pairs[0][0], s.pairs[1][0]];
|
|
399
|
+
if (id(x, s.a) !== id(y, s.a) || id(x, s.b) !== id(y, s.b))
|
|
400
|
+
split++;
|
|
401
|
+
}
|
|
402
|
+
for (let i = 0; i < rewrites.length; i += 3) {
|
|
403
|
+
const t = rewrites[i];
|
|
404
|
+
const k = tri[3 * t] === rewrites[i + 1] ? 0 : tri[3 * t + 1] === rewrites[i + 1] ? 1 : 2;
|
|
405
|
+
tri[3 * t + k] = rewrites[i + 2];
|
|
406
|
+
}
|
|
407
|
+
return { added, split, left: saddles.size - split };
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* `bound: 'hard'`: a bound on the distance to the iso surface that holds at every point of a triangle.
|
|
411
|
+
*
|
|
412
|
+
* At a lattice point x of the triangle, with `u` its unit normal toward rising values and `rho` the lattice's
|
|
413
|
+
* covering radius (no point of the triangle is farther than `rho` from a lattice point):
|
|
414
|
+
* - inside the triangle's plane the field changes by at most `rho * T` from its value at x, with `T` the largest
|
|
415
|
+
* in-plane gradient over the cells within `rho`. The field is trilinear in each cell, so its gradient there lies
|
|
416
|
+
* in the box of the cell's edge differences (the derivative along x is bilinear in (y, z): between the smallest
|
|
417
|
+
* and largest of the cell's four x edges), and the squared in-plane part is convex: its largest value over a box
|
|
418
|
+
* is at a corner;
|
|
419
|
+
* - along `u` the field rises at least `m` per voxel over the cells within `rho + eps` (the smallest `u . g` over
|
|
420
|
+
* the same boxes);
|
|
421
|
+
* - so from every point within `rho` of x the field reaches the iso value within (|f(x) - iso| + rho * T) / m
|
|
422
|
+
* voxels along the normal, as long as that is at most `eps` (the reach of `m`).
|
|
423
|
+
*
|
|
424
|
+
* `lo` / `hi` are the box (index space) the cell cache is laid over; cells outside it are computed each time.
|
|
425
|
+
*/
|
|
426
|
+
function hardBound(field, iso, eps, lo, hi, onCell) {
|
|
427
|
+
const cellCache = new Map();
|
|
428
|
+
const pad = Math.ceil(eps) + 3;
|
|
429
|
+
const cellLo = [0, 1, 2].map((k) => Math.floor(lo[k]) - pad);
|
|
430
|
+
const cellDims = [0, 1, 2].map((k) => Math.floor(hi[k]) + pad - cellLo[k] + 1);
|
|
431
|
+
const cellKeyed = cellDims[0] * cellDims[1] * cellDims[2] < 2 ** 52;
|
|
432
|
+
const cellGradient = (cx, cy, cz) => {
|
|
433
|
+
const kx = cx - cellLo[0];
|
|
434
|
+
const ky = cy - cellLo[1];
|
|
435
|
+
const kz = cz - cellLo[2];
|
|
436
|
+
const key = cellKeyed && kx >= 0 && ky >= 0 && kz >= 0 && kx < cellDims[0] && ky < cellDims[1] && kz < cellDims[2] ? (kx * cellDims[1] + ky) * cellDims[2] + kz : -1;
|
|
437
|
+
if (key >= 0) {
|
|
438
|
+
const hit = cellCache.get(key);
|
|
439
|
+
if (hit)
|
|
440
|
+
return hit;
|
|
441
|
+
}
|
|
442
|
+
onCell();
|
|
443
|
+
const v = [0, 0, 0, 0, 0, 0, 0, 0];
|
|
444
|
+
for (let c = 0; c < 8; c++)
|
|
445
|
+
v[c] = field.voxel(cx + (c >> 2), cy + ((c >> 1) & 1), cz + (c & 1));
|
|
446
|
+
const out = new Float64Array(6);
|
|
447
|
+
// Edges along x join corners c and c + 4, along y c and c + 2, along z c and c + 1.
|
|
448
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
449
|
+
const step = 4 >> axis;
|
|
450
|
+
let least = Infinity;
|
|
451
|
+
let most = -Infinity;
|
|
452
|
+
for (let c = 0; c < 8; c++) {
|
|
453
|
+
if (c & step)
|
|
454
|
+
continue;
|
|
455
|
+
const d = v[c + step] - v[c];
|
|
456
|
+
least = Math.min(least, d);
|
|
457
|
+
most = Math.max(most, d);
|
|
458
|
+
}
|
|
459
|
+
out[2 * axis] = least;
|
|
460
|
+
out[2 * axis + 1] = most;
|
|
461
|
+
}
|
|
462
|
+
if (key >= 0) {
|
|
463
|
+
if (cellCache.size >= CELL_CACHE)
|
|
464
|
+
cellCache.clear();
|
|
465
|
+
cellCache.set(key, out);
|
|
466
|
+
}
|
|
467
|
+
return out;
|
|
468
|
+
};
|
|
469
|
+
const f = new Float64Array(4);
|
|
470
|
+
const box = new Float64Array(12);
|
|
471
|
+
/** At the last point: `terms[0]` = |f - iso| + rho * T, `terms[1]` = m. */
|
|
472
|
+
const terms = [0, 0];
|
|
473
|
+
const point = (x, y, z, ux, uy, uz, rho) => {
|
|
474
|
+
field.sample(x, y, z, f);
|
|
475
|
+
const d = Math.abs(f[0] - iso);
|
|
476
|
+
const r = rho + eps;
|
|
477
|
+
for (let k = 0; k < 6; k++) {
|
|
478
|
+
box[2 * k] = Infinity;
|
|
479
|
+
box[2 * k + 1] = -Infinity;
|
|
480
|
+
}
|
|
481
|
+
const ix0 = Math.floor(x - rho);
|
|
482
|
+
const ix1 = Math.floor(x + rho);
|
|
483
|
+
const iy0 = Math.floor(y - rho);
|
|
484
|
+
const iy1 = Math.floor(y + rho);
|
|
485
|
+
const iz0 = Math.floor(z - rho);
|
|
486
|
+
const iz1 = Math.floor(z + rho);
|
|
487
|
+
for (let cx = Math.floor(x - r); cx <= Math.floor(x + r); cx++) {
|
|
488
|
+
for (let cy = Math.floor(y - r); cy <= Math.floor(y + r); cy++) {
|
|
489
|
+
for (let cz = Math.floor(z - r); cz <= Math.floor(z + r); cz++) {
|
|
490
|
+
const c = cellGradient(cx, cy, cz);
|
|
491
|
+
const near = cx >= ix0 && cx <= ix1 && cy >= iy0 && cy <= iy1 && cz >= iz0 && cz <= iz1;
|
|
492
|
+
for (let k = 0; k < 3; k++) {
|
|
493
|
+
if (c[2 * k] < box[2 * k])
|
|
494
|
+
box[2 * k] = c[2 * k];
|
|
495
|
+
if (c[2 * k + 1] > box[2 * k + 1])
|
|
496
|
+
box[2 * k + 1] = c[2 * k + 1];
|
|
497
|
+
if (near) {
|
|
498
|
+
if (c[2 * k] < box[6 + 2 * k])
|
|
499
|
+
box[6 + 2 * k] = c[2 * k];
|
|
500
|
+
if (c[2 * k + 1] > box[7 + 2 * k])
|
|
501
|
+
box[7 + 2 * k] = c[2 * k + 1];
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
terms[1] = Math.min(ux * box[0], ux * box[1]) + Math.min(uy * box[2], uy * box[3]) + Math.min(uz * box[4], uz * box[5]);
|
|
508
|
+
let t2 = 0;
|
|
509
|
+
for (let c = 0; c < 8; c++) {
|
|
510
|
+
const gx = box[6 + (c & 1)];
|
|
511
|
+
const gy = box[8 + ((c >> 1) & 1)];
|
|
512
|
+
const gz = box[10 + ((c >> 2) & 1)];
|
|
513
|
+
const along = gx * ux + gy * uy + gz * uz;
|
|
514
|
+
t2 = Math.max(t2, gx * gx + gy * gy + gz * gz - along * along);
|
|
515
|
+
}
|
|
516
|
+
terms[0] = d + rho * Math.sqrt(t2);
|
|
517
|
+
};
|
|
518
|
+
/** Runs `each` after `point` at every lattice point of the triangle, until it returns false. */
|
|
519
|
+
const lattice = (tx, ty, tz, px, py, pz, qx, qy, qz, side, each) => {
|
|
520
|
+
const ax = px - tx;
|
|
521
|
+
const ay = py - ty;
|
|
522
|
+
const az = pz - tz;
|
|
523
|
+
const bx = qx - tx;
|
|
524
|
+
const by = qy - ty;
|
|
525
|
+
const bz = qz - tz;
|
|
526
|
+
const nx = ay * bz - az * by;
|
|
527
|
+
const ny = az * bx - ax * bz;
|
|
528
|
+
const nz = ax * by - ay * bx;
|
|
529
|
+
const longest = Math.max(ax * ax + ay * ay + az * az, bx * bx + by * by + bz * bz, (px - qx) ** 2 + (py - qy) ** 2 + (pz - qz) ** 2);
|
|
530
|
+
// Every point of the triangle is within `rho` of a lattice point (corners included): the lattice cuts it into
|
|
531
|
+
// triangles of its own shape with longest edge `longest / hd`, and no point of a triangle is farther than
|
|
532
|
+
// its longest edge / sqrt(3) from the nearest corner.
|
|
533
|
+
const hd = Math.min(HARD_MAX_DIVISIONS, Math.max(1, Math.ceil(Math.sqrt(longest) / HARD_SPACING)));
|
|
534
|
+
const rho = Math.sqrt(longest) / hd / Math.sqrt(3);
|
|
535
|
+
const nl = side / Math.sqrt(nx * nx + ny * ny + nz * nz);
|
|
536
|
+
for (let i = 0; i <= hd; i++) {
|
|
537
|
+
for (let j = 0; j <= hd - i; j++) {
|
|
538
|
+
const wi = i / hd;
|
|
539
|
+
const wj = j / hd;
|
|
540
|
+
const wk = (hd - i - j) / hd;
|
|
541
|
+
point(wi * tx + wj * px + wk * qx, wi * ty + wj * py + wk * qy, wi * tz + wj * pz + wk * qz, nx * nl, ny * nl, nz * nl, rho);
|
|
542
|
+
if (!each())
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
};
|
|
547
|
+
return {
|
|
548
|
+
triangleOk: (tx, ty, tz, px, py, pz, qx, qy, qz, side) => {
|
|
549
|
+
let ok = true;
|
|
550
|
+
lattice(tx, ty, tz, px, py, pz, qx, qy, qz, side, () => (ok = terms[1] > 0 && terms[0] <= eps * terms[1]));
|
|
551
|
+
return ok;
|
|
552
|
+
},
|
|
553
|
+
triangleBound: (tx, ty, tz, px, py, pz, qx, qy, qz, side) => {
|
|
554
|
+
let worst = 0;
|
|
555
|
+
lattice(tx, ty, tz, px, py, pz, qx, qy, qz, side, () => {
|
|
556
|
+
worst = Math.max(worst, terms[1] > 0 ? terms[0] / terms[1] : Infinity);
|
|
557
|
+
return worst < Infinity;
|
|
558
|
+
});
|
|
559
|
+
return worst;
|
|
560
|
+
},
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* The bound `bound: 'hard'` proves for one triangle (tools internal, for tests): nine index-space coordinates, wound
|
|
565
|
+
* so that its normal faces rising values. The result bounds the distance of every point of the triangle to the iso
|
|
566
|
+
* surface when it is at most `error`; a larger value (or Infinity) means "not shown".
|
|
567
|
+
*/
|
|
568
|
+
export function vdbLevelSetMeshHardBound(image, triangle, options = {}) {
|
|
569
|
+
const lo = [0, 1, 2].map((k) => Math.min(triangle[k], triangle[3 + k], triangle[6 + k]));
|
|
570
|
+
const hi = [0, 1, 2].map((k) => Math.max(triangle[k], triangle[3 + k], triangle[6 + k]));
|
|
571
|
+
const check = hardBound(fieldSampler(image), options.isoValue ?? 0, options.error ?? 0.1, lo, hi, () => { });
|
|
572
|
+
return check.triangleBound(triangle[0], triangle[1], triangle[2], triangle[3], triangle[4], triangle[5], triangle[6], triangle[7], triangle[8], 1);
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* Decimates a mesh of a level set's iso surface (`vdbLevelSetMesh` or one chunk of `vdbLevelSetMeshChunks`) against
|
|
576
|
+
* the grid it came from: edge collapses in quadric-error order (Garland and Heckbert 1997), each accepted only while
|
|
577
|
+
* the triangles it changes stay within `error` voxels of the iso surface, keep their side and are not needles. See
|
|
578
|
+
* the file header for the method.
|
|
579
|
+
*
|
|
580
|
+
* The bound is checked at sample points on each changed triangle; between them up to 9 % more was measured
|
|
581
|
+
* (see {@link VDBLevelSetMeshDecimateOptions.error}). `bound: 'hard'` checks every point instead.
|
|
582
|
+
*
|
|
583
|
+
* The result has a subset of the input's vertices (positions and normals copied bit for bit, in the input's order)
|
|
584
|
+
* and the surviving triangles in the input's order. Vertices on an open boundary (a clip box or a chunk seam) and on
|
|
585
|
+
* non-manifold edges are kept, so decimated chunks still join on equal positions. The result is a pure function of
|
|
586
|
+
* the image, the mesh and the options. (`repair` adds copies of some vertices after the input's: see the option.)
|
|
587
|
+
*
|
|
588
|
+
* @param image The grid image the mesh was made from.
|
|
589
|
+
* @param mesh A mesh in `'world'` or `'index'` space (its `space` field says which). Extra fields (a chunk's `bbox`)
|
|
590
|
+
* are carried over.
|
|
591
|
+
* @throws {VDBToolsError} `VDB_BAD_IMAGE` / `VDB_UNSUPPORTED_GRID_TYPE` for an image the CPU readers can't decode,
|
|
592
|
+
* `VDB_BAD_OPTION` for a bad option or a mesh whose arrays disagree with its counts.
|
|
593
|
+
*/
|
|
594
|
+
export function vdbLevelSetMeshDecimate(image, mesh, options = {}) {
|
|
595
|
+
return vdbLevelSetMeshDecimateStats(image, mesh, options).mesh;
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* {@link vdbLevelSetMeshDecimate} with its operation counts (tools internal). `cache: false` turns the triangle
|
|
599
|
+
* cache off, for the before / after counts; the mesh is the same either way.
|
|
600
|
+
*/
|
|
601
|
+
export function vdbLevelSetMeshDecimateStats(image, mesh, options = {}) {
|
|
602
|
+
const bad = (message) => {
|
|
603
|
+
throw new VDBToolsError("VDB_BAD_OPTION", `vdbLevelSetMeshDecimate: ${message}`);
|
|
604
|
+
};
|
|
605
|
+
if (!(image instanceof Uint32Array))
|
|
606
|
+
throw new VDBToolsError("VDB_BAD_IMAGE", "vdbLevelSetMeshDecimate: image must be a Uint32Array grid image");
|
|
607
|
+
const eps = options.error ?? 0.1;
|
|
608
|
+
if (!(Number.isFinite(eps) && eps > 0))
|
|
609
|
+
bad(`error must be a positive number of voxels, got ${String(eps)}`);
|
|
610
|
+
const iso = options.isoValue ?? 0;
|
|
611
|
+
if (!Number.isFinite(iso))
|
|
612
|
+
bad(`isoValue ${String(iso)} is not finite`);
|
|
613
|
+
const target = options.targetTriangles ?? 0;
|
|
614
|
+
if (!Number.isInteger(target) || target < 0)
|
|
615
|
+
bad(`targetTriangles must be a non-negative integer, got ${String(target)}`);
|
|
616
|
+
const creases = options.creases ?? false;
|
|
617
|
+
if (typeof creases !== "boolean")
|
|
618
|
+
bad(`creases must be a boolean, got ${String(creases)}`);
|
|
619
|
+
const repair = options.repair ?? false;
|
|
620
|
+
if (typeof repair !== "boolean")
|
|
621
|
+
bad(`repair must be a boolean, got ${String(repair)}`);
|
|
622
|
+
const boundMode = options.bound ?? "sampled";
|
|
623
|
+
if (boundMode !== "sampled" && boundMode !== "hard")
|
|
624
|
+
bad(`bound must be 'sampled' or 'hard', got ${String(boundMode)}`);
|
|
625
|
+
const hard = boundMode === "hard";
|
|
626
|
+
const useCache = options.cache ?? true;
|
|
627
|
+
if (mesh === null || typeof mesh !== "object" || !(mesh.positions instanceof Float32Array) || !(mesh.indices instanceof Uint32Array)) {
|
|
628
|
+
bad("mesh must be a vdbLevelSetMesh result (positions: Float32Array, indices: Uint32Array)");
|
|
629
|
+
}
|
|
630
|
+
const nV0 = mesh.vertexCount;
|
|
631
|
+
const nT = mesh.triangleCount;
|
|
632
|
+
if (!Number.isInteger(nV0) || !Number.isInteger(nT) || mesh.positions.length !== nV0 * 3 || mesh.indices.length !== nT * 3) {
|
|
633
|
+
bad("mesh.positions / mesh.indices do not match vertexCount / triangleCount");
|
|
634
|
+
}
|
|
635
|
+
if (mesh.normals !== undefined && (!(mesh.normals instanceof Float32Array) || mesh.normals.length !== nV0 * 3))
|
|
636
|
+
bad("mesh.normals does not match vertexCount");
|
|
637
|
+
if (mesh.space !== "world" && mesh.space !== "index")
|
|
638
|
+
bad(`mesh.space must be 'world' or 'index', got ${String(mesh.space)}`);
|
|
639
|
+
for (let i = 0; i < mesh.indices.length; i++)
|
|
640
|
+
if (mesh.indices[i] >= nV0)
|
|
641
|
+
bad(`mesh.indices[${i}] = ${mesh.indices[i]} is not a vertex`);
|
|
642
|
+
indexBBox(image); // validates the image (VDB_BAD_IMAGE / VDB_UNSUPPORTED_GRID_TYPE)
|
|
643
|
+
const stats = {
|
|
644
|
+
tried: 0,
|
|
645
|
+
accepted: 0,
|
|
646
|
+
acceptedInPlane: 0,
|
|
647
|
+
rejectedTopology: 0,
|
|
648
|
+
rejectedShape: 0,
|
|
649
|
+
rejectedBound: 0,
|
|
650
|
+
fieldSamples: 0,
|
|
651
|
+
triangleChecks: 0,
|
|
652
|
+
triangleCacheHits: 0,
|
|
653
|
+
cellBounds: 0,
|
|
654
|
+
saddleEdgesSplit: 0,
|
|
655
|
+
verticesAdded: 0,
|
|
656
|
+
saddleEdgesLeft: 0,
|
|
657
|
+
foldedTriangles: 0,
|
|
658
|
+
};
|
|
659
|
+
const unchanged = () => ({
|
|
660
|
+
...mesh,
|
|
661
|
+
positions: mesh.positions.slice(),
|
|
662
|
+
indices: mesh.indices.slice(),
|
|
663
|
+
...(mesh.normals ? { normals: mesh.normals.slice() } : {}),
|
|
664
|
+
});
|
|
665
|
+
if (nT === 0 || (target >= nT && !repair))
|
|
666
|
+
return { mesh: unchanged(), stats };
|
|
667
|
+
// Index-space positions (f64), and the side of a triangle's index-space normal that faces positive values.
|
|
668
|
+
let P = new Float64Array(nV0 * 3);
|
|
669
|
+
let side = 1;
|
|
670
|
+
if (mesh.space === "world") {
|
|
671
|
+
const view = new DataView(image.buffer, image.byteOffset, image.byteLength);
|
|
672
|
+
const mat = Array.from({ length: 9 }, (_, i) => view.getFloat64(GRID_OFF_MAP + MAP_OFF_MATD + i * 8, true));
|
|
673
|
+
const inv = Array.from({ length: 9 }, (_, i) => view.getFloat64(GRID_OFF_MAP + MAP_OFF_INVMATD + i * 8, true));
|
|
674
|
+
const vec = Array.from({ length: 3 }, (_, i) => view.getFloat64(GRID_OFF_MAP + MAP_OFF_VECD + i * 8, true));
|
|
675
|
+
const det = mat[0] * (mat[4] * mat[8] - mat[5] * mat[7]) -
|
|
676
|
+
mat[1] * (mat[3] * mat[8] - mat[5] * mat[6]) +
|
|
677
|
+
mat[2] * (mat[3] * mat[7] - mat[4] * mat[6]);
|
|
678
|
+
// The mesher winds a mirrored grid's triangles outward in world space: in index space they face inward.
|
|
679
|
+
if (det < 0)
|
|
680
|
+
side = -1;
|
|
681
|
+
for (let i = 0; i < nV0; i++) {
|
|
682
|
+
const x = mesh.positions[3 * i] - vec[0];
|
|
683
|
+
const y = mesh.positions[3 * i + 1] - vec[1];
|
|
684
|
+
const z = mesh.positions[3 * i + 2] - vec[2];
|
|
685
|
+
for (let r = 0; r < 3; r++)
|
|
686
|
+
P[3 * i + r] = inv[r * 3] * x + inv[r * 3 + 1] * y + inv[r * 3 + 2] * z;
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
else {
|
|
690
|
+
for (let i = 0; i < P.length; i++)
|
|
691
|
+
P[i] = mesh.positions[i];
|
|
692
|
+
}
|
|
693
|
+
const tri = Int32Array.from(mesh.indices);
|
|
694
|
+
const triAlive = new Uint8Array(nT).fill(1);
|
|
695
|
+
// Degenerate input triangles (a repeated vertex) are dropped at once.
|
|
696
|
+
let aliveTriangles = nT;
|
|
697
|
+
for (let t = 0; t < nT; t++) {
|
|
698
|
+
if (tri[3 * t] === tri[3 * t + 1] || tri[3 * t + 1] === tri[3 * t + 2] || tri[3 * t] === tri[3 * t + 2]) {
|
|
699
|
+
triAlive[t] = 0;
|
|
700
|
+
aliveTriangles--;
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
const field = fieldSampler(image);
|
|
704
|
+
const sample = field.sample;
|
|
705
|
+
const f = new Float64Array(4);
|
|
706
|
+
// `repair`: split the saddle edges. `origin[v]` is the input vertex of `v` (added vertices are copies).
|
|
707
|
+
let nV = nV0;
|
|
708
|
+
let origin;
|
|
709
|
+
if (repair) {
|
|
710
|
+
const r = splitSaddleEdges(tri, triAlive, nT, nV0, P, side, (x, y, z) => {
|
|
711
|
+
sample(x, y, z, f);
|
|
712
|
+
return f[0] > iso;
|
|
713
|
+
});
|
|
714
|
+
stats.saddleEdgesSplit = r.split;
|
|
715
|
+
stats.saddleEdgesLeft = r.left;
|
|
716
|
+
stats.verticesAdded = r.added.length;
|
|
717
|
+
if (r.added.length > 0) {
|
|
718
|
+
nV = nV0 + r.added.length;
|
|
719
|
+
origin = new Int32Array(nV);
|
|
720
|
+
const grown = new Float64Array(nV * 3);
|
|
721
|
+
grown.set(P);
|
|
722
|
+
for (let v = 0; v < nV; v++) {
|
|
723
|
+
const o = v < nV0 ? v : r.added[v - nV0];
|
|
724
|
+
origin[v] = o;
|
|
725
|
+
if (v >= nV0)
|
|
726
|
+
for (let k = 0; k < 3; k++)
|
|
727
|
+
grown[3 * v + k] = P[3 * o + k];
|
|
728
|
+
}
|
|
729
|
+
P = grown;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
// Vertex kinds: 0 free, 1 on a boundary edge (kept; a free vertex may collapse onto it), 2 on a non-manifold or
|
|
733
|
+
// misoriented edge or with more than one triangle fan (kept, and nothing collapses onto it).
|
|
734
|
+
const kind = new Uint8Array(nV);
|
|
735
|
+
irregularEdges(tri, triAlive, nT, nV, (a, b, fwd, bwd) => {
|
|
736
|
+
const what = fwd + bwd === 1 ? 1 : 2;
|
|
737
|
+
kind[a] = Math.max(kind[a], what);
|
|
738
|
+
kind[b] = Math.max(kind[b], what);
|
|
739
|
+
});
|
|
740
|
+
// The triangles of each vertex (alive ones only, kept up to date).
|
|
741
|
+
const vt = Array.from({ length: nV }, () => []);
|
|
742
|
+
for (let t = 0; t < nT; t++) {
|
|
743
|
+
if (!triAlive[t])
|
|
744
|
+
continue;
|
|
745
|
+
vt[tri[3 * t]].push(t);
|
|
746
|
+
vt[tri[3 * t + 1]].push(t);
|
|
747
|
+
vt[tri[3 * t + 2]].push(t);
|
|
748
|
+
}
|
|
749
|
+
/** The corner of triangle `t` that holds vertex `v`. */
|
|
750
|
+
const cornerOf = (t, v) => (tri[3 * t] === v ? 0 : tri[3 * t + 1] === v ? 1 : 2);
|
|
751
|
+
// A free vertex must have one closed fan of triangles (two cones meeting at a point are not a manifold vertex).
|
|
752
|
+
for (let v = 0; v < nV; v++) {
|
|
753
|
+
const T = vt[v];
|
|
754
|
+
if (T.length === 0) {
|
|
755
|
+
kind[v] = 2; // unreferenced: nothing to do with it
|
|
756
|
+
continue;
|
|
757
|
+
}
|
|
758
|
+
if (kind[v] !== 0)
|
|
759
|
+
continue;
|
|
760
|
+
let t = T[0];
|
|
761
|
+
let steps = 0;
|
|
762
|
+
do {
|
|
763
|
+
const prev = tri[3 * t + ((cornerOf(t, v) + 2) % 3)];
|
|
764
|
+
let found = -1;
|
|
765
|
+
for (const o of T) {
|
|
766
|
+
if (tri[3 * o + ((cornerOf(o, v) + 1) % 3)] === prev) {
|
|
767
|
+
found = o;
|
|
768
|
+
break;
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
steps++;
|
|
772
|
+
if (found < 0) {
|
|
773
|
+
steps = -1;
|
|
774
|
+
break;
|
|
775
|
+
}
|
|
776
|
+
t = found;
|
|
777
|
+
} while (t !== T[0] && steps <= T.length);
|
|
778
|
+
if (steps !== T.length)
|
|
779
|
+
kind[v] = 2;
|
|
780
|
+
}
|
|
781
|
+
// Quadrics: the area-weighted plane quadrics of each vertex's triangles (Garland and Heckbert, section 5).
|
|
782
|
+
const Q = new Float64Array(nV * 10);
|
|
783
|
+
for (let t = 0; t < nT; t++) {
|
|
784
|
+
if (!triAlive[t])
|
|
785
|
+
continue;
|
|
786
|
+
const a = tri[3 * t];
|
|
787
|
+
const b = tri[3 * t + 1];
|
|
788
|
+
const c = tri[3 * t + 2];
|
|
789
|
+
const ux = P[3 * b] - P[3 * a];
|
|
790
|
+
const uy = P[3 * b + 1] - P[3 * a + 1];
|
|
791
|
+
const uz = P[3 * b + 2] - P[3 * a + 2];
|
|
792
|
+
const wx = P[3 * c] - P[3 * a];
|
|
793
|
+
const wy = P[3 * c + 1] - P[3 * a + 1];
|
|
794
|
+
const wz = P[3 * c + 2] - P[3 * a + 2];
|
|
795
|
+
let nx = uy * wz - uz * wy;
|
|
796
|
+
let ny = uz * wx - ux * wz;
|
|
797
|
+
let nz = ux * wy - uy * wx;
|
|
798
|
+
const l = Math.hypot(nx, ny, nz);
|
|
799
|
+
if (!(l > 0))
|
|
800
|
+
continue;
|
|
801
|
+
nx /= l;
|
|
802
|
+
ny /= l;
|
|
803
|
+
nz /= l;
|
|
804
|
+
const d = -(nx * P[3 * a] + ny * P[3 * a + 1] + nz * P[3 * a + 2]);
|
|
805
|
+
const area = 0.5 * l;
|
|
806
|
+
const k = [nx * nx, nx * ny, nx * nz, nx * d, ny * ny, ny * nz, ny * d, nz * nz, nz * d, d * d];
|
|
807
|
+
for (const v of [a, b, c])
|
|
808
|
+
for (let i = 0; i < 10; i++)
|
|
809
|
+
Q[10 * v + i] += area * k[i];
|
|
810
|
+
}
|
|
811
|
+
/** The collapse cost of `from` onto `to`: the summed quadric at `to`, plus the length tie-break. */
|
|
812
|
+
const costOf = (from, to) => {
|
|
813
|
+
const x = P[3 * to];
|
|
814
|
+
const y = P[3 * to + 1];
|
|
815
|
+
const z = P[3 * to + 2];
|
|
816
|
+
const q = (i) => Q[10 * from + i] + Q[10 * to + i];
|
|
817
|
+
const e = q(0) * x * x + 2 * q(1) * x * y + 2 * q(2) * x * z + 2 * q(3) * x + q(4) * y * y + 2 * q(5) * y * z + 2 * q(6) * y + q(7) * z * z + 2 * q(8) * z + q(9);
|
|
818
|
+
const len2 = (x - P[3 * from]) ** 2 + (y - P[3 * from + 1]) ** 2 + (z - P[3 * from + 2]) ** 2;
|
|
819
|
+
return Math.max(0, e) + LENGTH_WEIGHT * len2;
|
|
820
|
+
};
|
|
821
|
+
const scratch = [];
|
|
822
|
+
/** The neighbours of `v` (each once), into `scratch`. */
|
|
823
|
+
const neighbours = (v) => {
|
|
824
|
+
scratch.length = 0;
|
|
825
|
+
for (const t of vt[v]) {
|
|
826
|
+
for (let e = 0; e < 3; e++) {
|
|
827
|
+
const o = tri[3 * t + e];
|
|
828
|
+
if (o !== v && !scratch.includes(o))
|
|
829
|
+
scratch.push(o);
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
return scratch;
|
|
833
|
+
};
|
|
834
|
+
const heap = new CollapseHeap();
|
|
835
|
+
const stamp = new Int32Array(nV);
|
|
836
|
+
/** Queues the cheapest collapse of `from` that comes after (`afterCost`, `afterTo`) in (cost, to) order. */
|
|
837
|
+
const queue = (from, afterCost, afterTo) => {
|
|
838
|
+
let best = Infinity;
|
|
839
|
+
let bestTo = -1;
|
|
840
|
+
for (const to of neighbours(from)) {
|
|
841
|
+
const c = costOf(from, to);
|
|
842
|
+
if (c < afterCost || (c === afterCost && to <= afterTo))
|
|
843
|
+
continue;
|
|
844
|
+
if (c < best || (c === best && to < bestTo)) {
|
|
845
|
+
best = c;
|
|
846
|
+
bestTo = to;
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
if (bestTo >= 0)
|
|
850
|
+
heap.push(best, from, bestTo, stamp[from]);
|
|
851
|
+
};
|
|
852
|
+
const mark = new Int32Array(nV);
|
|
853
|
+
let epoch = 0;
|
|
854
|
+
/** Is the point within `eps` voxels of the iso surface, with the field rising on the `n` side? */
|
|
855
|
+
const pointOk = (x, y, z, nx, ny, nz) => {
|
|
856
|
+
sample(x, y, z, f);
|
|
857
|
+
const g2 = f[1] * f[1] + f[2] * f[2] + f[3] * f[3];
|
|
858
|
+
const d = f[0] - iso;
|
|
859
|
+
// First-order distance |f - iso| / |grad f|; a flat field (outside the band) has no surface nearby.
|
|
860
|
+
if (!(d * d <= eps * eps * g2))
|
|
861
|
+
return false;
|
|
862
|
+
if (!(side * (nx * f[1] + ny * f[2] + nz * f[3]) > 0))
|
|
863
|
+
return false;
|
|
864
|
+
if (d * d <= 0.25 * eps * eps * g2)
|
|
865
|
+
return true;
|
|
866
|
+
// Near the bound, take the Newton step and add what is left there: the path to the surface, not its first leg.
|
|
867
|
+
const first = Math.abs(d) / Math.sqrt(g2);
|
|
868
|
+
const k = d / g2;
|
|
869
|
+
sample(x - k * f[1], y - k * f[2], z - k * f[3], f);
|
|
870
|
+
const h2 = f[1] * f[1] + f[2] * f[2] + f[3] * f[3];
|
|
871
|
+
return first + Math.abs(f[0] - iso) / Math.sqrt(h2) <= eps;
|
|
872
|
+
};
|
|
873
|
+
// `bound: 'hard'`: the checker, with its cell cache laid over the mesh's box.
|
|
874
|
+
let hardCheck;
|
|
875
|
+
if (hard) {
|
|
876
|
+
const lo = [Infinity, Infinity, Infinity];
|
|
877
|
+
const hi = [-Infinity, -Infinity, -Infinity];
|
|
878
|
+
for (let v = 0; v < nV; v++) {
|
|
879
|
+
for (let k = 0; k < 3; k++) {
|
|
880
|
+
lo[k] = Math.min(lo[k], P[3 * v + k]);
|
|
881
|
+
hi[k] = Math.max(hi[k], P[3 * v + k]);
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
hardCheck = hardBound(field, iso, eps, lo, hi, () => {
|
|
885
|
+
stats.cellBounds++;
|
|
886
|
+
});
|
|
887
|
+
}
|
|
888
|
+
/**
|
|
889
|
+
* The field check of the triangle (to, p, q): the bound and the side at its sample points. A pure function of the
|
|
890
|
+
* three vertices (positions never change), so its result is cached under them.
|
|
891
|
+
*/
|
|
892
|
+
const triangleOk = (to, p, q) => {
|
|
893
|
+
const tx = P[3 * to];
|
|
894
|
+
const ty = P[3 * to + 1];
|
|
895
|
+
const tz = P[3 * to + 2];
|
|
896
|
+
const px = P[3 * p];
|
|
897
|
+
const py = P[3 * p + 1];
|
|
898
|
+
const pz = P[3 * p + 2];
|
|
899
|
+
const qx = P[3 * q];
|
|
900
|
+
const qy = P[3 * q + 1];
|
|
901
|
+
const qz = P[3 * q + 2];
|
|
902
|
+
const ax = px - tx;
|
|
903
|
+
const ay = py - ty;
|
|
904
|
+
const az = pz - tz;
|
|
905
|
+
const bx = qx - tx;
|
|
906
|
+
const by = qy - ty;
|
|
907
|
+
const bz = qz - tz;
|
|
908
|
+
const nx = ay * bz - az * by;
|
|
909
|
+
const ny = az * bx - ax * bz;
|
|
910
|
+
const nz = ax * by - ay * bx;
|
|
911
|
+
const longest = Math.max(ax * ax + ay * ay + az * az, bx * bx + by * by + bz * bz, (px - qx) ** 2 + (py - qy) ** 2 + (pz - qz) ** 2);
|
|
912
|
+
if (hardCheck)
|
|
913
|
+
return hardCheck.triangleOk(tx, ty, tz, px, py, pz, qx, qy, qz, side);
|
|
914
|
+
// Sampled: a barycentric lattice over the triangle; the corners are input vertices and stay as they are.
|
|
915
|
+
const div = Math.min(SAMPLE_MAX_DIVISIONS, Math.max(1, Math.ceil(Math.sqrt(longest) / SAMPLE_SPACING)));
|
|
916
|
+
if (div < 3 && !pointOk((tx + px + qx) / 3, (ty + py + qy) / 3, (tz + pz + qz) / 3, nx, ny, nz))
|
|
917
|
+
return false;
|
|
918
|
+
// From the middle outward (the points whose smallest barycentric index is `ring`): a triangle that fails
|
|
919
|
+
// usually fails far from its corners, and the order does not change the answer.
|
|
920
|
+
for (let ring = Math.floor(div / 3); ring >= 0; ring--) {
|
|
921
|
+
for (let i = ring; i <= div - 2 * ring; i++) {
|
|
922
|
+
for (let j = ring; j <= div - i - ring; j++) {
|
|
923
|
+
const k = div - i - j;
|
|
924
|
+
if ((i !== ring && j !== ring && k !== ring) || i === div || j === div || k === div)
|
|
925
|
+
continue;
|
|
926
|
+
const wi = i / div;
|
|
927
|
+
const wj = j / div;
|
|
928
|
+
const wk = k / div;
|
|
929
|
+
if (!pointOk(wi * tx + wj * px + wk * qx, wi * ty + wj * py + wk * qy, wi * tz + wj * pz + wk * qz, nx, ny, nz))
|
|
930
|
+
return false;
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
return true;
|
|
935
|
+
};
|
|
936
|
+
const triangleCache = [];
|
|
937
|
+
const triangleOkCached = (to, p, q) => {
|
|
938
|
+
if (!useCache) {
|
|
939
|
+
stats.triangleChecks++;
|
|
940
|
+
return triangleOk(to, p, q);
|
|
941
|
+
}
|
|
942
|
+
let map = triangleCache[to];
|
|
943
|
+
if (!map)
|
|
944
|
+
triangleCache[to] = map = new Map();
|
|
945
|
+
const key = p * nV + q;
|
|
946
|
+
const hit = map.get(key);
|
|
947
|
+
if (hit !== undefined) {
|
|
948
|
+
stats.triangleCacheHits++;
|
|
949
|
+
return hit;
|
|
950
|
+
}
|
|
951
|
+
stats.triangleChecks++;
|
|
952
|
+
const ok = triangleOk(to, p, q);
|
|
953
|
+
if (map.size >= TRIANGLE_CACHE)
|
|
954
|
+
map.clear();
|
|
955
|
+
map.set(key, ok);
|
|
956
|
+
return ok;
|
|
957
|
+
};
|
|
958
|
+
// `repair`: the triangles that face against the field's gradient at their centroid.
|
|
959
|
+
let folded;
|
|
960
|
+
/** Does the triangle (a, b, c) face against the field at its centroid? */
|
|
961
|
+
const facesAgainst = (a, b, c) => {
|
|
962
|
+
const ux = P[3 * b] - P[3 * a];
|
|
963
|
+
const uy = P[3 * b + 1] - P[3 * a + 1];
|
|
964
|
+
const uz = P[3 * b + 2] - P[3 * a + 2];
|
|
965
|
+
const wx = P[3 * c] - P[3 * a];
|
|
966
|
+
const wy = P[3 * c + 1] - P[3 * a + 1];
|
|
967
|
+
const wz = P[3 * c + 2] - P[3 * a + 2];
|
|
968
|
+
sample((P[3 * a] + P[3 * b] + P[3 * c]) / 3, (P[3 * a + 1] + P[3 * b + 1] + P[3 * c + 1]) / 3, (P[3 * a + 2] + P[3 * b + 2] + P[3 * c + 2]) / 3, f);
|
|
969
|
+
return !(side * ((uy * wz - uz * wy) * f[1] + (uz * wx - ux * wz) * f[2] + (ux * wy - uy * wx) * f[3]) > 0);
|
|
970
|
+
};
|
|
971
|
+
if (repair) {
|
|
972
|
+
folded = new Uint8Array(nT);
|
|
973
|
+
for (let t = 0; t < nT; t++) {
|
|
974
|
+
if (triAlive[t] && facesAgainst(tri[3 * t], tri[3 * t + 1], tri[3 * t + 2])) {
|
|
975
|
+
folded[t] = 1;
|
|
976
|
+
stats.foldedTriangles++;
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
// `creases`: how far each triangle may already be from the input surface it replaced (surface-preserving
|
|
981
|
+
// collapses add up; a triangle that passed the field check starts again at 0).
|
|
982
|
+
const drift = creases ? new Float64Array(nT) : undefined;
|
|
983
|
+
const driftLimit = CREASE_TOLERANCE * eps;
|
|
984
|
+
/** Set by `valid`: the drift of the triangles the accepted collapse changes, or -1 when it passed the field. */
|
|
985
|
+
let acceptedDrift = -1;
|
|
986
|
+
/** May `from` collapse onto `to`? */
|
|
987
|
+
const valid = (from, to) => {
|
|
988
|
+
if (kind[from] !== 0 || kind[to] === 2) {
|
|
989
|
+
stats.rejectedTopology++;
|
|
990
|
+
return false;
|
|
991
|
+
}
|
|
992
|
+
const T = vt[from];
|
|
993
|
+
// The link condition: the edge has two triangles, and the two vertices share no other neighbour.
|
|
994
|
+
let shared = 0;
|
|
995
|
+
let oppA = -1;
|
|
996
|
+
let oppB = -1;
|
|
997
|
+
for (const t of T) {
|
|
998
|
+
const c = cornerOf(t, from);
|
|
999
|
+
const p = tri[3 * t + ((c + 1) % 3)];
|
|
1000
|
+
const q = tri[3 * t + ((c + 2) % 3)];
|
|
1001
|
+
if (p !== to && q !== to)
|
|
1002
|
+
continue;
|
|
1003
|
+
shared++;
|
|
1004
|
+
const o = p === to ? q : p;
|
|
1005
|
+
if (shared === 1)
|
|
1006
|
+
oppA = o;
|
|
1007
|
+
else
|
|
1008
|
+
oppB = o;
|
|
1009
|
+
}
|
|
1010
|
+
if (shared !== 2 || oppA === oppB) {
|
|
1011
|
+
stats.rejectedTopology++;
|
|
1012
|
+
return false;
|
|
1013
|
+
}
|
|
1014
|
+
epoch++;
|
|
1015
|
+
for (const n of neighbours(to))
|
|
1016
|
+
mark[n] = epoch;
|
|
1017
|
+
let common = 0;
|
|
1018
|
+
for (const n of neighbours(from))
|
|
1019
|
+
if (mark[n] === epoch)
|
|
1020
|
+
common++;
|
|
1021
|
+
if (common !== 2) {
|
|
1022
|
+
stats.rejectedTopology++;
|
|
1023
|
+
return false;
|
|
1024
|
+
}
|
|
1025
|
+
// The changed triangles: first what needs no field sample.
|
|
1026
|
+
const tx = P[3 * to];
|
|
1027
|
+
const ty = P[3 * to + 1];
|
|
1028
|
+
const tz = P[3 * to + 2];
|
|
1029
|
+
const fx = P[3 * from];
|
|
1030
|
+
const fy = P[3 * from + 1];
|
|
1031
|
+
const fz = P[3 * from + 2];
|
|
1032
|
+
// `creases`: the largest distance of `to` from the plane of a triangle it would take over; Infinity when one
|
|
1033
|
+
// of them faces against the field (that one needs the field check).
|
|
1034
|
+
let offPlane = 0;
|
|
1035
|
+
let before = 0;
|
|
1036
|
+
for (const t of T) {
|
|
1037
|
+
if (drift && drift[t] > before)
|
|
1038
|
+
before = drift[t];
|
|
1039
|
+
const c = cornerOf(t, from);
|
|
1040
|
+
const p = tri[3 * t + ((c + 1) % 3)];
|
|
1041
|
+
const q = tri[3 * t + ((c + 2) % 3)];
|
|
1042
|
+
if (p === to || q === to)
|
|
1043
|
+
continue;
|
|
1044
|
+
// A triangle on both opposite vertices would land on the one `to` already has there (a closed pocket).
|
|
1045
|
+
if ((p === oppA && q === oppB) || (p === oppB && q === oppA)) {
|
|
1046
|
+
for (const o of vt[to]) {
|
|
1047
|
+
const k = cornerOf(o, to);
|
|
1048
|
+
const a = tri[3 * o + ((k + 1) % 3)];
|
|
1049
|
+
const b = tri[3 * o + ((k + 2) % 3)];
|
|
1050
|
+
if ((a === oppA && b === oppB) || (a === oppB && b === oppA)) {
|
|
1051
|
+
stats.rejectedTopology++;
|
|
1052
|
+
return false;
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
const px = P[3 * p];
|
|
1057
|
+
const py = P[3 * p + 1];
|
|
1058
|
+
const pz = P[3 * p + 2];
|
|
1059
|
+
const qx = P[3 * q];
|
|
1060
|
+
const qy = P[3 * q + 1];
|
|
1061
|
+
const qz = P[3 * q + 2];
|
|
1062
|
+
const ax = px - tx;
|
|
1063
|
+
const ay = py - ty;
|
|
1064
|
+
const az = pz - tz;
|
|
1065
|
+
const bx = qx - tx;
|
|
1066
|
+
const by = qy - ty;
|
|
1067
|
+
const bz = qz - tz;
|
|
1068
|
+
const nx = ay * bz - az * by;
|
|
1069
|
+
const ny = az * bx - ax * bz;
|
|
1070
|
+
const nz = ax * by - ay * bx;
|
|
1071
|
+
const n2 = nx * nx + ny * ny + nz * nz;
|
|
1072
|
+
const longest = Math.max(ax * ax + ay * ay + az * az, bx * bx + by * by + bz * bz, (px - qx) ** 2 + (py - qy) ** 2 + (pz - qz) ** 2);
|
|
1073
|
+
// No sliver of (near) zero area.
|
|
1074
|
+
if (!(n2 > 1e-12 * longest * longest)) {
|
|
1075
|
+
stats.rejectedShape++;
|
|
1076
|
+
return false;
|
|
1077
|
+
}
|
|
1078
|
+
const ox = px - fx;
|
|
1079
|
+
const oy = py - fy;
|
|
1080
|
+
const oz = pz - fz;
|
|
1081
|
+
const rx = qx - fx;
|
|
1082
|
+
const ry = qy - fy;
|
|
1083
|
+
const rz = qz - fz;
|
|
1084
|
+
const mx = oy * rz - oz * ry;
|
|
1085
|
+
const my = oz * rx - ox * rz;
|
|
1086
|
+
const mz = ox * ry - oy * rx;
|
|
1087
|
+
const m2 = mx * mx + my * my + mz * mz;
|
|
1088
|
+
if (folded && folded[t]) {
|
|
1089
|
+
// `repair`: nothing to keep of a triangle that faces the wrong way; the field decides.
|
|
1090
|
+
offPlane = Infinity;
|
|
1091
|
+
}
|
|
1092
|
+
else {
|
|
1093
|
+
// No fold: the triangle keeps facing the way the one it replaces did.
|
|
1094
|
+
const dot = nx * mx + ny * my + nz * mz;
|
|
1095
|
+
if (!(dot > 0 && dot * dot >= MIN_TURN_COSINE * MIN_TURN_COSINE * n2 * m2)) {
|
|
1096
|
+
stats.rejectedShape++;
|
|
1097
|
+
return false;
|
|
1098
|
+
}
|
|
1099
|
+
if (drift)
|
|
1100
|
+
offPlane = Math.max(offPlane, Math.abs(mx * (tx - fx) + my * (ty - fy) + mz * (tz - fz)) / Math.sqrt(m2));
|
|
1101
|
+
}
|
|
1102
|
+
// No new needle: collision and export want triangles with some area for their length.
|
|
1103
|
+
const shape = n2 / (longest * longest);
|
|
1104
|
+
if (shape < MIN_SHAPE * MIN_SHAPE) {
|
|
1105
|
+
const was = Math.max(ox * ox + oy * oy + oz * oz, rx * rx + ry * ry + rz * rz, (px - qx) ** 2 + (py - qy) ** 2 + (pz - qz) ** 2);
|
|
1106
|
+
if (shape < m2 / (was * was)) {
|
|
1107
|
+
stats.rejectedShape++;
|
|
1108
|
+
return false;
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
// `creases`: every changed triangle stays in the plane of the one it replaces, so the surface stays where it
|
|
1113
|
+
// is (the fan around `from` is one flat piece, or two that meet along the line through `from` and `to`).
|
|
1114
|
+
if (drift && before + offPlane <= driftLimit) {
|
|
1115
|
+
acceptedDrift = before + offPlane;
|
|
1116
|
+
return true;
|
|
1117
|
+
}
|
|
1118
|
+
// The bound, on each changed triangle.
|
|
1119
|
+
for (const t of T) {
|
|
1120
|
+
const c = cornerOf(t, from);
|
|
1121
|
+
const p = tri[3 * t + ((c + 1) % 3)];
|
|
1122
|
+
const q = tri[3 * t + ((c + 2) % 3)];
|
|
1123
|
+
if (p === to || q === to)
|
|
1124
|
+
continue;
|
|
1125
|
+
if (!triangleOkCached(to, p, q) || (folded && folded[t] && facesAgainst(to, p, q))) {
|
|
1126
|
+
stats.rejectedBound++;
|
|
1127
|
+
return false;
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
acceptedDrift = -1;
|
|
1131
|
+
return true;
|
|
1132
|
+
};
|
|
1133
|
+
const alive = new Uint8Array(nV).fill(1);
|
|
1134
|
+
for (let v = 0; v < nV; v++)
|
|
1135
|
+
if (kind[v] === 0)
|
|
1136
|
+
queue(v, -Infinity, -1);
|
|
1137
|
+
const top = [0, 0, 0, 0];
|
|
1138
|
+
const touched = [];
|
|
1139
|
+
while (heap.size > 0 && aliveTriangles > target) {
|
|
1140
|
+
heap.pop(top);
|
|
1141
|
+
const cost = top[0];
|
|
1142
|
+
const from = top[1];
|
|
1143
|
+
const to = top[2];
|
|
1144
|
+
if (!alive[from] || stamp[from] !== top[3])
|
|
1145
|
+
continue;
|
|
1146
|
+
stats.tried++;
|
|
1147
|
+
if (!valid(from, to)) {
|
|
1148
|
+
queue(from, cost, to);
|
|
1149
|
+
continue;
|
|
1150
|
+
}
|
|
1151
|
+
stats.accepted++;
|
|
1152
|
+
if (acceptedDrift >= 0)
|
|
1153
|
+
stats.acceptedInPlane++;
|
|
1154
|
+
// Collapse: the edge's two triangles go, the others move their corner to `to`.
|
|
1155
|
+
for (const t of vt[from]) {
|
|
1156
|
+
const c = cornerOf(t, from);
|
|
1157
|
+
const p = tri[3 * t + ((c + 1) % 3)];
|
|
1158
|
+
const q = tri[3 * t + ((c + 2) % 3)];
|
|
1159
|
+
if (p === to || q === to) {
|
|
1160
|
+
triAlive[t] = 0;
|
|
1161
|
+
aliveTriangles--;
|
|
1162
|
+
const other = p === to ? q : p;
|
|
1163
|
+
vt[other] = vt[other].filter((o) => o !== t);
|
|
1164
|
+
vt[to] = vt[to].filter((o) => o !== t);
|
|
1165
|
+
}
|
|
1166
|
+
else {
|
|
1167
|
+
tri[3 * t + c] = to;
|
|
1168
|
+
vt[to].push(t);
|
|
1169
|
+
if (drift)
|
|
1170
|
+
drift[t] = Math.max(0, acceptedDrift);
|
|
1171
|
+
if (folded)
|
|
1172
|
+
folded[t] = 0;
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
vt[from] = [];
|
|
1176
|
+
alive[from] = 0;
|
|
1177
|
+
triangleCache[from] = undefined;
|
|
1178
|
+
for (let i = 0; i < 10; i++)
|
|
1179
|
+
Q[10 * to + i] += Q[10 * from + i];
|
|
1180
|
+
// Costs and validity changed for `to` and everything next to it.
|
|
1181
|
+
touched.length = 0;
|
|
1182
|
+
touched.push(to, ...neighbours(to));
|
|
1183
|
+
for (const v of touched) {
|
|
1184
|
+
stamp[v]++;
|
|
1185
|
+
if (kind[v] === 0)
|
|
1186
|
+
queue(v, -Infinity, -1);
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
stats.fieldSamples = field.count();
|
|
1190
|
+
// Output: the vertices the surviving triangles use, in the input's order (`repair`'s copies after them).
|
|
1191
|
+
const remap = new Int32Array(nV).fill(-1);
|
|
1192
|
+
for (let t = 0; t < nT; t++) {
|
|
1193
|
+
if (!triAlive[t])
|
|
1194
|
+
continue;
|
|
1195
|
+
remap[tri[3 * t]] = 0;
|
|
1196
|
+
remap[tri[3 * t + 1]] = 0;
|
|
1197
|
+
remap[tri[3 * t + 2]] = 0;
|
|
1198
|
+
}
|
|
1199
|
+
let outV = 0;
|
|
1200
|
+
for (let v = 0; v < nV; v++)
|
|
1201
|
+
if (remap[v] === 0)
|
|
1202
|
+
remap[v] = outV++;
|
|
1203
|
+
const positions = new Float32Array(outV * 3);
|
|
1204
|
+
const normals = mesh.normals ? new Float32Array(outV * 3) : undefined;
|
|
1205
|
+
for (let v = 0; v < nV; v++) {
|
|
1206
|
+
const o = remap[v];
|
|
1207
|
+
if (o < 0)
|
|
1208
|
+
continue;
|
|
1209
|
+
const src = origin ? origin[v] : v;
|
|
1210
|
+
positions.set(mesh.positions.subarray(3 * src, 3 * src + 3), 3 * o);
|
|
1211
|
+
if (normals)
|
|
1212
|
+
normals.set(mesh.normals.subarray(3 * src, 3 * src + 3), 3 * o);
|
|
1213
|
+
}
|
|
1214
|
+
const indices = new Uint32Array(aliveTriangles * 3);
|
|
1215
|
+
let n = 0;
|
|
1216
|
+
for (let t = 0; t < nT; t++) {
|
|
1217
|
+
if (!triAlive[t])
|
|
1218
|
+
continue;
|
|
1219
|
+
indices[n++] = remap[tri[3 * t]];
|
|
1220
|
+
indices[n++] = remap[tri[3 * t + 1]];
|
|
1221
|
+
indices[n++] = remap[tri[3 * t + 2]];
|
|
1222
|
+
}
|
|
1223
|
+
return { mesh: { ...mesh, positions, indices, ...(normals ? { normals } : {}), vertexCount: outV, triangleCount: aliveTriangles }, stats };
|
|
1224
|
+
}
|
|
1225
|
+
//# sourceMappingURL=mesh-decimate.js.map
|