opsiclear-admin commited on
Commit
648cdec
·
verified ·
1 Parent(s): ad1b20d

Upload folder using huggingface_hub

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .claude/settings.local.json +68 -0
  2. .gitattributes +23 -0
  3. .gitignore +207 -0
  4. .gitmodules +3 -0
  5. README.md +0 -2
  6. SECURITY.md +14 -0
  7. app.py +243 -179
  8. app_texturing.py +151 -0
  9. o-voxel/README.md +174 -0
  10. o-voxel/assets/overview.webp +3 -0
  11. o-voxel/build/lib.win-amd64-cpython-311/o_voxel/__init__.py +7 -0
  12. o-voxel/build/lib.win-amd64-cpython-311/o_voxel/convert/__init__.py +2 -0
  13. o-voxel/build/lib.win-amd64-cpython-311/o_voxel/convert/flexible_dual_grid.py +283 -0
  14. o-voxel/build/lib.win-amd64-cpython-311/o_voxel/convert/volumetic_attr.py +583 -0
  15. o-voxel/build/lib.win-amd64-cpython-311/o_voxel/io/__init__.py +45 -0
  16. o-voxel/build/lib.win-amd64-cpython-311/o_voxel/io/npz.py +43 -0
  17. o-voxel/build/lib.win-amd64-cpython-311/o_voxel/io/ply.py +72 -0
  18. o-voxel/build/lib.win-amd64-cpython-311/o_voxel/io/vxz.py +365 -0
  19. o-voxel/build/lib.win-amd64-cpython-311/o_voxel/postprocess.py +331 -0
  20. o-voxel/build/lib.win-amd64-cpython-311/o_voxel/rasterize.py +111 -0
  21. o-voxel/build/lib.win-amd64-cpython-311/o_voxel/serialize.py +68 -0
  22. o-voxel/build/temp.win-amd64-cpython-311/Release/.ninja_deps +3 -0
  23. o-voxel/build/temp.win-amd64-cpython-311/Release/.ninja_log +12 -0
  24. o-voxel/build/temp.win-amd64-cpython-311/Release/build.ninja +46 -0
  25. o-voxel/build/temp.win-amd64-cpython-311/Release/src/convert/_C.cp311-win_amd64.exp +0 -0
  26. o-voxel/build/temp.win-amd64-cpython-311/Release/src/convert/_C.cp311-win_amd64.lib +0 -0
  27. o-voxel/build/temp.win-amd64-cpython-311/Release/src/convert/flexible_dual_grid.obj +3 -0
  28. o-voxel/build/temp.win-amd64-cpython-311/Release/src/convert/volumetic_attr.obj +3 -0
  29. o-voxel/build/temp.win-amd64-cpython-311/Release/src/ext.obj +3 -0
  30. o-voxel/build/temp.win-amd64-cpython-311/Release/src/hash/hash.obj +3 -0
  31. o-voxel/build/temp.win-amd64-cpython-311/Release/src/io/filter_neighbor.obj +3 -0
  32. o-voxel/build/temp.win-amd64-cpython-311/Release/src/io/filter_parent.obj +3 -0
  33. o-voxel/build/temp.win-amd64-cpython-311/Release/src/io/svo.obj +3 -0
  34. o-voxel/build/temp.win-amd64-cpython-311/Release/src/rasterize/rasterize.obj +3 -0
  35. o-voxel/build/temp.win-amd64-cpython-311/Release/src/serialize/api.obj +3 -0
  36. o-voxel/build/temp.win-amd64-cpython-311/Release/src/serialize/hilbert.obj +0 -0
  37. o-voxel/build/temp.win-amd64-cpython-311/Release/src/serialize/z_order.obj +0 -0
  38. o-voxel/examples/mesh2ovox.py +57 -0
  39. o-voxel/examples/ovox2glb.py +52 -0
  40. o-voxel/examples/ovox2mesh.py +45 -0
  41. o-voxel/examples/render_ovox.py +39 -0
  42. o-voxel/examples/utils.py +27 -0
  43. o-voxel/o_voxel.egg-info/PKG-INFO +15 -0
  44. o-voxel/o_voxel.egg-info/SOURCES.txt +30 -0
  45. o-voxel/o_voxel.egg-info/dependency_links.txt +1 -0
  46. o-voxel/o_voxel.egg-info/requires.txt +9 -0
  47. o-voxel/o_voxel.egg-info/top_level.txt +1 -0
  48. o-voxel/o_voxel/__init__.py +7 -0
  49. o-voxel/o_voxel/convert/__init__.py +2 -0
  50. o-voxel/o_voxel/convert/flexible_dual_grid.py +283 -0
.claude/settings.local.json ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "permissions": {
3
+ "allow": [
4
+ "Bash(git checkout:*)",
5
+ "WebFetch(domain:viser.studio)",
6
+ "WebSearch",
7
+ "WebFetch(domain:github.com)",
8
+ "mcp__plugin_context7_context7__resolve-library-id",
9
+ "mcp__plugin_context7_context7__query-docs",
10
+ "Bash(python -c:*)",
11
+ "Bash(uv add:*)",
12
+ "Bash(uv:*)",
13
+ "Bash(grep:*)",
14
+ "Bash(nvidia-smi:*)",
15
+ "Bash(nvcc:*)",
16
+ "Bash(where:*)",
17
+ "Bash(gcc:*)",
18
+ "Bash(cl)",
19
+ "Bash(python:*)",
20
+ "Bash(DISTUTILS_USE_SDK=1 uv pip install:*)",
21
+ "Bash(curl:*)",
22
+ "Bash(export PATH=\"/c/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.44.35207/bin/Hostx64/x64:$PATH\")",
23
+ "Bash(git submodule:*)",
24
+ "Bash(set \"PATH=C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Tools\\\\MSVC\\\\14.42.34433\\\\bin\\\\Hostx64\\\\x64;%PATH%\")",
25
+ "Bash(set \"ATTN_BACKEND=xformers\")",
26
+ "Bash(cmd /c \"set PATH=C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Tools\\\\MSVC\\\\14.42.34433\\\\bin\\\\Hostx64\\\\x64;%PATH% && set ATTN_BACKEND=xformers && uv run python visualize_flow.py --image assets/example_image/T.png\")",
27
+ "Bash(powershell -Command \"$env:ATTN_BACKEND=''xformers''; $env:PATH=''C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Tools\\\\MSVC\\\\14.42.34433\\\\bin\\\\Hostx64\\\\x64;'' + $env:PATH; uv run python visualize_flow.py --image assets/example_image/T.png\")",
28
+ "Bash(timeout:*)",
29
+ "Bash(.venvScriptspython.exe -c \"from huggingface_hub import whoami; print\\(whoami\\(\\)\\)\")",
30
+ "Bash(.venv/Scripts/python.exe:*)",
31
+ "Bash(.venv/Scripts/pip.exe install:*)",
32
+ "Bash(cd:*)",
33
+ "Bash(ping:*)",
34
+ "Bash(conda activate:*)",
35
+ "Bash(pkill:*)",
36
+ "Bash(tasklist:*)",
37
+ "Bash(wmic OS get:*)",
38
+ "Bash(powershell:*)",
39
+ "Bash(dir /b /s \"C:\\\\Users\\\\opsiclear\\\\Desktop\\\\projects\\\\Trellis2_multi_image_conditioning\\\\trellis2\\\\pipelines\"\")",
40
+ "Bash(findstr:*)",
41
+ "Bash(netstat:*)",
42
+ "Bash(taskkill:*)",
43
+ "Bash(git add:*)",
44
+ "Bash(git commit:*)",
45
+ "Bash(git push:*)",
46
+ "Bash(gh auth:*)",
47
+ "Bash(git config:*)",
48
+ "Bash(git ls-tree:*)",
49
+ "Bash(ls:*)",
50
+ "Bash(wc:*)",
51
+ "Bash(git rm:*)",
52
+ "Bash(git clone:*)",
53
+ "Bash(huggingface-cli upload:*)",
54
+ "Bash(pip install:*)",
55
+ "Bash(\"C:/Users/opsiclear/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0/LocalCache/local-packages/Python312/Scripts/hf.exe\" upload OpsiClear/Trellis.2.multi-image \"C:/Users/opsiclear/Desktop/projects/Trellis.2.multi-image\" . --repo-type=space)",
56
+ "Bash(\"C:/Users/opsiclear/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0/LocalCache/local-packages/Python312/Scripts/hf.exe\" login)",
57
+ "Bash(\"C:/Users/opsiclear/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0/LocalCache/local-packages/Python312/Scripts/hf.exe\" --help)",
58
+ "Bash(\"C:/Users/opsiclear/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0/LocalCache/local-packages/Python312/Scripts/hf.exe\" auth --help)",
59
+ "Bash(\"C:/Users/opsiclear/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0/LocalCache/local-packages/Python312/Scripts/hf.exe\" auth whoami)",
60
+ "Bash(C:UsersopsiclearAppDataRoamingPythonPython310Scriptshuggingface-cli.exe repo info spaces/OpsiClear/Trellis.2.multi-image)",
61
+ "Bash(\"C:\\\\Users\\\\opsiclear\\\\AppData\\\\Roaming\\\\Python\\\\Python310\\\\Scripts\\\\hf.exe\" upload spaces/OpsiClear/Trellis.2.multi-image README.md --commit-message \"Add suggested_hardware: a100-large for GPU support\")",
62
+ "Bash(..venvScriptspython.exe app_local.py)",
63
+ "Bash(pip show:*)",
64
+ "Bash(huggingface-cli whoami:*)",
65
+ "Bash(git remote add:*)"
66
+ ]
67
+ }
68
+ }
.gitattributes CHANGED
@@ -132,3 +132,26 @@ assets/hdri/night.exr filter=lfs diff=lfs merge=lfs -text
132
  assets/hdri/sunrise.exr filter=lfs diff=lfs merge=lfs -text
133
  assets/hdri/sunset.exr filter=lfs diff=lfs merge=lfs -text
134
  assets/teaser.webp filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  assets/hdri/sunrise.exr filter=lfs diff=lfs merge=lfs -text
133
  assets/hdri/sunset.exr filter=lfs diff=lfs merge=lfs -text
134
  assets/teaser.webp filter=lfs diff=lfs merge=lfs -text
135
+ o-voxel/assets/overview.webp filter=lfs diff=lfs merge=lfs -text
136
+ o-voxel/build/temp.win-amd64-cpython-311/Release/.ninja_deps filter=lfs diff=lfs merge=lfs -text
137
+ o-voxel/build/temp.win-amd64-cpython-311/Release/src/convert/flexible_dual_grid.obj filter=lfs diff=lfs merge=lfs -text
138
+ o-voxel/build/temp.win-amd64-cpython-311/Release/src/convert/volumetic_attr.obj filter=lfs diff=lfs merge=lfs -text
139
+ o-voxel/build/temp.win-amd64-cpython-311/Release/src/ext.obj filter=lfs diff=lfs merge=lfs -text
140
+ o-voxel/build/temp.win-amd64-cpython-311/Release/src/hash/hash.obj filter=lfs diff=lfs merge=lfs -text
141
+ o-voxel/build/temp.win-amd64-cpython-311/Release/src/io/filter_neighbor.obj filter=lfs diff=lfs merge=lfs -text
142
+ o-voxel/build/temp.win-amd64-cpython-311/Release/src/io/filter_parent.obj filter=lfs diff=lfs merge=lfs -text
143
+ o-voxel/build/temp.win-amd64-cpython-311/Release/src/io/svo.obj filter=lfs diff=lfs merge=lfs -text
144
+ o-voxel/build/temp.win-amd64-cpython-311/Release/src/rasterize/rasterize.obj filter=lfs diff=lfs merge=lfs -text
145
+ o-voxel/build/temp.win-amd64-cpython-311/Release/src/serialize/api.obj filter=lfs diff=lfs merge=lfs -text
146
+ outputs/step_meshes/step_000.glb filter=lfs diff=lfs merge=lfs -text
147
+ outputs/step_meshes/step_001.glb filter=lfs diff=lfs merge=lfs -text
148
+ outputs/step_meshes/step_002.glb filter=lfs diff=lfs merge=lfs -text
149
+ outputs/step_meshes/step_003.glb filter=lfs diff=lfs merge=lfs -text
150
+ outputs/step_meshes/step_004.glb filter=lfs diff=lfs merge=lfs -text
151
+ outputs/step_meshes/step_005.glb filter=lfs diff=lfs merge=lfs -text
152
+ outputs/step_meshes/step_006.glb filter=lfs diff=lfs merge=lfs -text
153
+ outputs/step_meshes/step_007.glb filter=lfs diff=lfs merge=lfs -text
154
+ outputs/step_meshes/step_008.glb filter=lfs diff=lfs merge=lfs -text
155
+ outputs/step_meshes/step_009.glb filter=lfs diff=lfs merge=lfs -text
156
+ outputs/step_meshes/step_010.glb filter=lfs diff=lfs merge=lfs -text
157
+ outputs/step_meshes/step_011.glb filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,207 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ #uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ #poetry.lock
109
+ #poetry.toml
110
+
111
+ # pdm
112
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
113
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
114
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
115
+ #pdm.lock
116
+ #pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # pixi
121
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
122
+ #pixi.lock
123
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
124
+ # in the .venv directory. It is recommended not to include this directory in version control.
125
+ .pixi
126
+
127
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
128
+ __pypackages__/
129
+
130
+ # Celery stuff
131
+ celerybeat-schedule
132
+ celerybeat.pid
133
+
134
+ # SageMath parsed files
135
+ *.sage.py
136
+
137
+ # Environments
138
+ .env
139
+ .envrc
140
+ .venv
141
+ env/
142
+ venv/
143
+ ENV/
144
+ env.bak/
145
+ venv.bak/
146
+
147
+ # Spyder project settings
148
+ .spyderproject
149
+ .spyproject
150
+
151
+ # Rope project settings
152
+ .ropeproject
153
+
154
+ # mkdocs documentation
155
+ /site
156
+
157
+ # mypy
158
+ .mypy_cache/
159
+ .dmypy.json
160
+ dmypy.json
161
+
162
+ # Pyre type checker
163
+ .pyre/
164
+
165
+ # pytype static type analyzer
166
+ .pytype/
167
+
168
+ # Cython debug symbols
169
+ cython_debug/
170
+
171
+ # PyCharm
172
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
173
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
174
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
175
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
176
+ #.idea/
177
+
178
+ # Abstra
179
+ # Abstra is an AI-powered process automation framework.
180
+ # Ignore directories containing user credentials, local state, and settings.
181
+ # Learn more at https://abstra.io/docs
182
+ .abstra/
183
+
184
+ # Visual Studio Code
185
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
186
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
187
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
188
+ # you could uncomment the following to ignore the entire vscode folder
189
+ # .vscode/
190
+
191
+ # Ruff stuff:
192
+ .ruff_cache/
193
+
194
+ # PyPI configuration file
195
+ .pypirc
196
+
197
+ # Cursor
198
+ # Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
199
+ # exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
200
+ # refer to https://docs.cursor.com/context/ignore-files
201
+ .cursorignore
202
+ .cursorindexingignore
203
+
204
+ # Marimo
205
+ marimo/_static/
206
+ marimo/_lsp/
207
+ __marimo__/
.gitmodules ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ [submodule "o-voxel/third_party/eigen"]
2
+ path = o-voxel/third_party/eigen
3
+ url = https://gitlab.com/libeigen/eigen.git
README.md CHANGED
@@ -5,12 +5,10 @@ colorFrom: blue
5
  colorTo: purple
6
  sdk: gradio
7
  sdk_version: 6.1.0
8
- python_version: "3.10"
9
  app_file: app.py
10
  pinned: false
11
  license: mit
12
  short_description: Multi-view image to 3D generation
13
- suggested_hardware: a100-large
14
  ---
15
 
16
  # TRELLIS.2 Multi-Image Conditioning Fork
 
5
  colorTo: purple
6
  sdk: gradio
7
  sdk_version: 6.1.0
 
8
  app_file: app.py
9
  pinned: false
10
  license: mit
11
  short_description: Multi-view image to 3D generation
 
12
  ---
13
 
14
  # TRELLIS.2 Multi-Image Conditioning Fork
SECURITY.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!-- BEGIN MICROSOFT SECURITY.MD V1.0.0 BLOCK -->
2
+
3
+ ## Security
4
+
5
+ Microsoft takes the security of our software products and services seriously, which
6
+ includes all source code repositories in our GitHub organizations.
7
+
8
+ **Please do not report security vulnerabilities through public GitHub issues.**
9
+
10
+ For security reporting information, locations, contact information, and policies,
11
+ please review the latest guidance for Microsoft repositories at
12
+ [https://aka.ms/SECURITY.md](https://aka.ms/SECURITY.md).
13
+
14
+ <!-- END MICROSOFT SECURITY.MD BLOCK -->
app.py CHANGED
@@ -13,78 +13,17 @@ from datetime import datetime
13
  import shutil
14
  import cv2
15
  from typing import *
 
16
  import numpy as np
17
  from PIL import Image
18
  import base64
19
  import io
20
  import tempfile
21
-
22
- # Lazy imports - will be loaded when GPU is available
23
- torch = None
24
- SparseTensor = None
25
- Trellis2ImageTo3DPipeline = None
26
- EnvMap = None
27
- render_utils = None
28
- o_voxel = None
29
-
30
- # Global state - initialized on first GPU call
31
- pipeline = None
32
- envmap = None
33
- _initialized = False
34
-
35
-
36
- def _lazy_import():
37
- """Import GPU-dependent modules. Must be called from within a @spaces.GPU function."""
38
- global torch, SparseTensor, Trellis2ImageTo3DPipeline, EnvMap, render_utils, o_voxel
39
- if torch is None:
40
- import torch as _torch
41
- torch = _torch
42
- if SparseTensor is None:
43
- from trellis2.modules.sparse import SparseTensor as _SparseTensor
44
- SparseTensor = _SparseTensor
45
- if Trellis2ImageTo3DPipeline is None:
46
- from trellis2.pipelines import Trellis2ImageTo3DPipeline as _Trellis2ImageTo3DPipeline
47
- Trellis2ImageTo3DPipeline = _Trellis2ImageTo3DPipeline
48
- if EnvMap is None:
49
- from trellis2.renderers import EnvMap as _EnvMap
50
- EnvMap = _EnvMap
51
- if render_utils is None:
52
- from trellis2.utils import render_utils as _render_utils
53
- render_utils = _render_utils
54
- if o_voxel is None:
55
- import o_voxel as _o_voxel
56
- o_voxel = _o_voxel
57
-
58
-
59
- def _initialize_pipeline():
60
- """Initialize the pipeline and environment maps. Must be called from within a @spaces.GPU function."""
61
- global pipeline, envmap, _initialized
62
- if _initialized:
63
- return
64
-
65
- _lazy_import()
66
-
67
- pipeline = Trellis2ImageTo3DPipeline.from_pretrained('microsoft/TRELLIS.2-4B')
68
- pipeline.rembg_model = None
69
- pipeline.low_vram = False
70
- pipeline.cuda()
71
-
72
- envmap = {
73
- 'forest': EnvMap(torch.tensor(
74
- cv2.cvtColor(cv2.imread('assets/hdri/forest.exr', cv2.IMREAD_UNCHANGED), cv2.COLOR_BGR2RGB),
75
- dtype=torch.float32, device='cuda'
76
- )),
77
- 'sunset': EnvMap(torch.tensor(
78
- cv2.cvtColor(cv2.imread('assets/hdri/sunset.exr', cv2.IMREAD_UNCHANGED), cv2.COLOR_BGR2RGB),
79
- dtype=torch.float32, device='cuda'
80
- )),
81
- 'courtyard': EnvMap(torch.tensor(
82
- cv2.cvtColor(cv2.imread('assets/hdri/courtyard.exr', cv2.IMREAD_UNCHANGED), cv2.COLOR_BGR2RGB),
83
- dtype=torch.float32, device='cuda'
84
- )),
85
- }
86
-
87
- _initialized = True
88
 
89
 
90
  MAX_SEED = np.iinfo(np.int32).max
@@ -103,30 +42,54 @@ DEFAULT_STEP = 3
103
 
104
 
105
  css = """
106
- /* ColmapView Dark Theme */
107
- :root {
108
- --body-background-fill: #0a0a0a !important;
109
- --background-fill-primary: #0f0f0f !important;
110
- --background-fill-secondary: #161616 !important;
111
- --block-background-fill: #161616 !important;
112
- --input-background-fill: #1a1a1a !important;
113
- --body-text-color: #e8e8e8 !important;
114
- --block-label-text-color: #8a8a8a !important;
115
- --block-title-text-color: #e8e8e8 !important;
116
- --border-color-primary: #2a2a2a !important;
117
- --color-accent: #b8b8b8 !important;
118
- --color-accent-soft: rgba(184, 184, 184, 0.15) !important;
119
- --button-primary-background-fill: #b8b8b8 !important;
120
- --button-primary-text-color: #0a0a0a !important;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  }
122
 
123
- body { background: #0a0a0a !important; }
124
- .gradio-container { background: #0f0f0f !important; }
125
- .dark { background: #0f0f0f !important; }
 
 
 
 
 
 
 
 
 
126
 
127
- /* Previewer (required for custom HTML viewer) */
128
  .previewer-container {
129
- background: #0a0a0a;
130
  position: relative;
131
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
132
  width: 100%;
@@ -177,6 +140,7 @@ body { background: #0a0a0a !important; }
177
  opacity: 100%;
178
  }
179
 
 
180
  .previewer-container .mode-row {
181
  width: 100%;
182
  display: flex;
@@ -202,6 +166,7 @@ body { background: #0a0a0a !important; }
202
  transform: scale(1.1);
203
  }
204
 
 
205
  .previewer-container .display-row {
206
  margin-bottom: 20px;
207
  min-height: 400px;
@@ -222,6 +187,7 @@ body { background: #0a0a0a !important; }
222
  display: block;
223
  }
224
 
 
225
  .previewer-container .slider-row {
226
  width: 100%;
227
  display: flex;
@@ -259,6 +225,7 @@ body { background: #0a0a0a !important; }
259
  transform: scale(1.2);
260
  }
261
 
 
262
  .gradio-container .padded:has(.previewer-container) {
263
  padding: 0 !important;
264
  }
@@ -288,9 +255,11 @@ head = """
288
  }
289
 
290
  // 2. Hide ALL images
 
291
  allImgs.forEach(img => img.classList.remove('visible'));
292
 
293
  // 3. Construct the specific ID for the current state
 
294
  const targetId = 'view-m' + mode + '-s' + step;
295
  const targetImg = document.getElementById(targetId);
296
 
@@ -320,10 +289,10 @@ head = """
320
  """
321
 
322
 
323
- empty_html = """
324
  <div class="previewer-container">
325
- <svg style="opacity: .5; height: var(--size-5); color: var(--body-text-color);"
326
- xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect><circle cx="8.5" cy="8.5" r="1.5"></circle><polyline points="21 15 16 10 5 21"></polyline></svg>
327
  </div>
328
  """
329
 
@@ -343,8 +312,7 @@ def start_session(req: gr.Request):
343
 
344
  def end_session(req: gr.Request):
345
  user_dir = os.path.join(TMP_DIR, str(req.session_hash))
346
- if os.path.exists(user_dir):
347
- shutil.rmtree(user_dir)
348
 
349
 
350
  def remove_background(input: Image.Image) -> Image.Image:
@@ -357,7 +325,10 @@ def remove_background(input: Image.Image) -> Image.Image:
357
 
358
 
359
  def preprocess_image(input: Image.Image) -> Image.Image:
360
- """Preprocess a single input image."""
 
 
 
361
  has_alpha = False
362
  if input.mode == 'RGBA':
363
  alpha = np.array(input)[:, :, 3]
@@ -379,7 +350,7 @@ def preprocess_image(input: Image.Image) -> Image.Image:
379
  size = max(bbox[2] - bbox[0], bbox[3] - bbox[1])
380
  size = int(size * 1)
381
  bbox = center[0] - size // 2, center[1] - size // 2, center[0] + size // 2, center[1] + size // 2
382
- output = output.crop(bbox)
383
  output = np.array(output).astype(np.float32) / 255
384
  output = output[:, :, :3] * output[:, :, 3:4]
385
  output = Image.fromarray((output * 255).astype(np.uint8))
@@ -387,16 +358,17 @@ def preprocess_image(input: Image.Image) -> Image.Image:
387
 
388
 
389
  def preprocess_images(images: List[Tuple[Image.Image, str]]) -> List[Image.Image]:
390
- """Preprocess a list of input images. Uses parallel processing."""
391
- if not images:
392
- return []
393
- imgs = [img[0] if isinstance(img, tuple) else img for img in images]
394
- with ThreadPoolExecutor(max_workers=min(4, len(imgs))) as executor:
395
- processed_images = list(executor.map(preprocess_image, imgs))
 
396
  return processed_images
397
 
398
 
399
- def pack_state(latents):
400
  shape_slat, tex_slat, res = latents
401
  return {
402
  'shape_slat_feats': shape_slat.feats.cpu().numpy(),
@@ -406,8 +378,7 @@ def pack_state(latents):
406
  }
407
 
408
 
409
- def unpack_state(state: dict):
410
- _lazy_import()
411
  shape_slat = SparseTensor(
412
  feats=torch.from_numpy(state['shape_slat_feats']).cuda(),
413
  coords=torch.from_numpy(state['coords']).cuda(),
@@ -417,32 +388,33 @@ def unpack_state(state: dict):
417
 
418
 
419
  def get_seed(randomize_seed: bool, seed: int) -> int:
 
 
 
420
  return np.random.randint(0, MAX_SEED) if randomize_seed else seed
421
 
422
 
423
  def prepare_multi_example() -> List[Image.Image]:
424
- """Prepare multi-image examples as concatenated images for gr.Examples."""
425
- example_dir = "assets/example_multi_image"
426
- if not os.path.exists(example_dir):
427
- return []
428
- cases = list(set([f.split('_')[0] for f in os.listdir(example_dir) if '_' in f and f.endswith('.png')]))
429
  images = []
430
- for case in sorted(cases):
431
- case_images = []
432
- for i in range(1, 10):
433
- img_path = f'{example_dir}/{case}_{i}.png'
434
- if os.path.exists(img_path):
435
- img = Image.open(img_path)
436
- W, H = img.size
437
- img = img.resize((int(W / H * 512), 512))
438
- case_images.append(np.array(img))
439
- if case_images:
440
- images.append(Image.fromarray(np.concatenate(case_images, axis=1)))
441
  return images
442
 
443
 
444
  def split_image(image: Image.Image) -> List[Image.Image]:
445
- """Split a concatenated multi-view image into separate images based on alpha."""
 
 
446
  image = np.array(image)
447
  alpha = image[..., 3]
448
  alpha = np.any(alpha > 0, axis=0)
@@ -451,12 +423,12 @@ def split_image(image: Image.Image) -> List[Image.Image]:
451
  images = []
452
  for s, e in zip(start_pos, end_pos):
453
  images.append(Image.fromarray(image[:, s:e+1]))
454
- return [preprocess_image(img) for img in images]
455
 
456
 
457
  @spaces.GPU(duration=120)
458
  def image_to_3d(
459
- images: List[Tuple[Image.Image, str]],
460
  seed: int,
461
  resolution: str,
462
  ss_guidance_strength: float,
@@ -471,24 +443,16 @@ def image_to_3d(
471
  tex_slat_guidance_rescale: float,
472
  tex_slat_sampling_steps: int,
473
  tex_slat_rescale_t: float,
474
- multiimage_algo: Literal["multidiffusion", "stochastic"],
475
  req: gr.Request,
476
  progress=gr.Progress(track_tqdm=True),
 
 
 
477
  ) -> str:
478
- # Initialize pipeline on first call
479
- _initialize_pipeline()
480
-
481
- # Extract images from gallery format
482
- if not images:
483
- raise gr.Error("Please upload at least one image")
484
-
485
- imgs = [img[0] if isinstance(img, tuple) else img for img in images]
486
-
487
  # --- Sampling ---
488
- if len(imgs) == 1:
489
- # Single image mode
490
  outputs, latents = pipeline.run(
491
- imgs[0],
492
  seed=seed,
493
  preprocess_image=False,
494
  sparse_structure_sampler_params={
@@ -517,9 +481,8 @@ def image_to_3d(
517
  return_latent=True,
518
  )
519
  else:
520
- # Multi-image mode
521
  outputs, latents = pipeline.run_multi_image(
522
- imgs,
523
  seed=seed,
524
  preprocess_image=False,
525
  sparse_structure_sampler_params={
@@ -548,44 +511,85 @@ def image_to_3d(
548
  return_latent=True,
549
  mode=multiimage_algo,
550
  )
551
-
552
  mesh = outputs[0]
553
- mesh.simplify(16777216)
554
- render_images = render_utils.render_snapshot(mesh, resolution=1024, r=2, fov=36, nviews=STEPS, envmap=envmap)
555
  state = pack_state(latents)
556
  torch.cuda.empty_cache()
557
 
558
  # --- HTML Construction ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
559
  images_html = ""
560
  for m_idx, mode in enumerate(MODES):
561
  for s_idx in range(STEPS):
562
  unique_id = f"view-m{m_idx}-s{s_idx}"
563
  is_visible = (m_idx == DEFAULT_MODE and s_idx == DEFAULT_STEP)
564
  vis_class = "visible" if is_visible else ""
565
- img_base64 = image_to_base64(Image.fromarray(render_images[mode['render_key']][s_idx]))
566
- images_html += f'<img id="{unique_id}" class="previewer-main-image {vis_class}" src="{img_base64}" loading="eager">'
 
 
 
 
 
 
567
 
 
568
  btns_html = ""
569
  for idx, mode in enumerate(MODES):
570
  active_class = "active" if idx == DEFAULT_MODE else ""
571
- btns_html += f'<img src="{mode["icon_base64"]}" class="mode-btn {active_class}" onclick="selectMode({idx})" title="{mode["name"]}">'
572
-
 
 
 
 
 
 
 
573
  full_html = f"""
574
  <div class="previewer-container">
575
  <div class="tips-wrapper">
576
- <div class="tips-icon">Tips</div>
577
  <div class="tips-text">
578
- <p>Render Mode - Click buttons to switch render modes.</p>
579
- <p>View Angle - Drag slider to change view.</p>
580
  </div>
581
  </div>
582
- <div class="display-row">{images_html}</div>
583
- <div class="mode-row" id="btn-group">{btns_html}</div>
 
 
 
 
 
 
 
 
 
 
584
  <div class="slider-row">
585
  <input type="range" id="custom-slider" min="0" max="{STEPS - 1}" value="{DEFAULT_STEP}" step="1" oninput="onSliderChange(this.value)">
586
  </div>
587
  </div>
588
  """
 
589
  return state, full_html
590
 
591
 
@@ -597,12 +601,21 @@ def extract_glb(
597
  req: gr.Request,
598
  progress=gr.Progress(track_tqdm=True),
599
  ) -> Tuple[str, str]:
600
- _initialize_pipeline()
 
 
 
 
 
 
601
 
 
 
 
602
  user_dir = os.path.join(TMP_DIR, str(req.session_hash))
603
  shape_slat, tex_slat, res = unpack_state(state)
604
  mesh = pipeline.decode_latent(shape_slat, tex_slat, res)[0]
605
- mesh.simplify(16777216)
606
  glb = o_voxel.postprocess.to_glb(
607
  vertices=mesh.vertices,
608
  faces=mesh.faces,
@@ -629,22 +642,22 @@ def extract_glb(
629
 
630
  with gr.Blocks(delete_cache=(600, 600)) as demo:
631
  gr.Markdown("""
632
- ## Multi-View Image to 3D Asset with [TRELLIS.2](https://microsoft.github.io/TRELLIS.2)
633
- * Upload one or more images and click Generate to create a 3D asset.
634
- * Multiple views from different angles will produce better results.
635
- * Click Extract GLB to export and download the generated GLB file.
636
  """)
637
 
638
  with gr.Row():
639
  with gr.Column(scale=1, min_width=360):
640
- image_prompt = gr.Gallery(
641
- label="Input Images",
642
- format="png",
643
- type="pil",
644
- height=400,
645
- columns=3,
646
- object_fit="contain"
647
- )
 
648
 
649
  resolution = gr.Radio(["512", "1024", "1536"], label="Resolution", value="1024")
650
  seed = gr.Slider(0, MAX_SEED, label="Seed", value=0, step=1)
@@ -676,44 +689,73 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
676
  multiimage_algo = gr.Radio(["stochastic", "multidiffusion"], label="Multi-image Algorithm", value="stochastic")
677
 
678
  with gr.Column(scale=10):
679
- with gr.Walkthrough(selected=0) as walkthrough:
680
- with gr.Step("Preview", id=0):
681
  preview_output = gr.HTML(empty_html, label="3D Asset Preview", show_label=True, container=True)
682
  extract_btn = gr.Button("Extract GLB")
683
- with gr.Step("Extract", id=1):
684
  glb_output = gr.Model3D(label="Extracted GLB", height=724, show_label=True, display_mode="solid", clear_color=(0.25, 0.25, 0.25, 1.0))
685
  download_btn = gr.DownloadButton(label="Download GLB")
686
- gr.Markdown("*GLB extraction may take 30+ seconds.*")
687
 
688
- with gr.Column(scale=1, min_width=200):
689
- # Hidden image for examples input
690
- example_image = gr.Image(visible=False, type="pil", image_mode="RGBA")
691
- gr.Markdown("### Multi-View Examples")
692
  examples = gr.Examples(
 
 
 
 
 
 
 
 
 
 
 
 
 
693
  examples=prepare_multi_example(),
694
- inputs=[example_image],
 
695
  fn=split_image,
696
- outputs=[image_prompt],
697
  run_on_click=True,
698
- examples_per_page=12,
699
  )
700
 
 
701
  output_buf = gr.State()
702
 
 
703
  # Handlers
704
  demo.load(start_session)
705
  demo.unload(end_session)
706
 
 
 
 
 
 
 
 
 
 
707
  image_prompt.upload(
708
- preprocess_images,
709
  inputs=[image_prompt],
710
  outputs=[image_prompt],
711
  )
 
 
 
 
 
712
 
713
  generate_btn.click(
714
- get_seed, inputs=[randomize_seed, seed], outputs=[seed],
 
 
715
  ).then(
716
- lambda: gr.Walkthrough(selected=0), outputs=walkthrough
717
  ).then(
718
  image_to_3d,
719
  inputs=[
@@ -721,13 +763,13 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
721
  ss_guidance_strength, ss_guidance_rescale, ss_sampling_steps, ss_rescale_t,
722
  shape_slat_guidance_strength, shape_slat_guidance_rescale, shape_slat_sampling_steps, shape_slat_rescale_t,
723
  tex_slat_guidance_strength, tex_slat_guidance_rescale, tex_slat_sampling_steps, tex_slat_rescale_t,
724
- multiimage_algo
725
  ],
726
  outputs=[output_buf, preview_output],
727
  )
728
 
729
  extract_btn.click(
730
- lambda: gr.Walkthrough(selected=1), outputs=walkthrough
731
  ).then(
732
  extract_glb,
733
  inputs=[output_buf, decimation_target, texture_size],
@@ -735,13 +777,35 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
735
  )
736
 
737
 
 
738
  if __name__ == "__main__":
739
  os.makedirs(TMP_DIR, exist_ok=True)
740
 
 
 
741
  for i in range(len(MODES)):
742
  icon = Image.open(MODES[i]['icon'])
743
  MODES[i]['icon_base64'] = image_to_base64(icon)
744
 
745
  rmbg_client = Client("briaai/BRIA-RMBG-2.0")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
746
 
747
  demo.launch(css=css, head=head)
 
13
  import shutil
14
  import cv2
15
  from typing import *
16
+ import torch
17
  import numpy as np
18
  from PIL import Image
19
  import base64
20
  import io
21
  import tempfile
22
+ from trellis2.modules.sparse import SparseTensor
23
+ from trellis2.pipelines import Trellis2ImageTo3DPipeline
24
+ from trellis2.renderers import EnvMap
25
+ from trellis2.utils import render_utils
26
+ import o_voxel
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
 
29
  MAX_SEED = np.iinfo(np.int32).max
 
42
 
43
 
44
  css = """
45
+ /* Overwrite Gradio Default Style */
46
+ .stepper-wrapper {
47
+ padding: 0;
48
+ }
49
+
50
+ .stepper-container {
51
+ padding: 0;
52
+ align-items: center;
53
+ }
54
+
55
+ .step-button {
56
+ flex-direction: row;
57
+ }
58
+
59
+ .step-connector {
60
+ transform: none;
61
+ }
62
+
63
+ .step-number {
64
+ width: 16px;
65
+ height: 16px;
66
+ }
67
+
68
+ .step-label {
69
+ position: relative;
70
+ bottom: 0;
71
+ }
72
+
73
+ .wrap.center.full {
74
+ inset: 0;
75
+ height: 100%;
76
  }
77
 
78
+ .wrap.center.full.translucent {
79
+ background: var(--block-background-fill);
80
+ }
81
+
82
+ .meta-text-center {
83
+ display: block !important;
84
+ position: absolute !important;
85
+ top: unset !important;
86
+ bottom: 0 !important;
87
+ right: 0 !important;
88
+ transform: unset !important;
89
+ }
90
 
91
+ /* Previewer */
92
  .previewer-container {
 
93
  position: relative;
94
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
95
  width: 100%;
 
140
  opacity: 100%;
141
  }
142
 
143
+ /* Row 1: Display Modes */
144
  .previewer-container .mode-row {
145
  width: 100%;
146
  display: flex;
 
166
  transform: scale(1.1);
167
  }
168
 
169
+ /* Row 2: Display Image */
170
  .previewer-container .display-row {
171
  margin-bottom: 20px;
172
  min-height: 400px;
 
187
  display: block;
188
  }
189
 
190
+ /* Row 3: Custom HTML Slider */
191
  .previewer-container .slider-row {
192
  width: 100%;
193
  display: flex;
 
225
  transform: scale(1.2);
226
  }
227
 
228
+ /* Overwrite Previewer Block Style */
229
  .gradio-container .padded:has(.previewer-container) {
230
  padding: 0 !important;
231
  }
 
255
  }
256
 
257
  // 2. Hide ALL images
258
+ // We select all elements with class 'previewer-main-image'
259
  allImgs.forEach(img => img.classList.remove('visible'));
260
 
261
  // 3. Construct the specific ID for the current state
262
+ // Format: view-m{mode}-s{step}
263
  const targetId = 'view-m' + mode + '-s' + step;
264
  const targetImg = document.getElementById(targetId);
265
 
 
289
  """
290
 
291
 
292
+ empty_html = f"""
293
  <div class="previewer-container">
294
+ <svg style=" opacity: .5; height: var(--size-5); color: var(--body-text-color);"
295
+ xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="feather feather-image"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect><circle cx="8.5" cy="8.5" r="1.5"></circle><polyline points="21 15 16 10 5 21"></polyline></svg>
296
  </div>
297
  """
298
 
 
312
 
313
  def end_session(req: gr.Request):
314
  user_dir = os.path.join(TMP_DIR, str(req.session_hash))
315
+ shutil.rmtree(user_dir)
 
316
 
317
 
318
  def remove_background(input: Image.Image) -> Image.Image:
 
325
 
326
 
327
  def preprocess_image(input: Image.Image) -> Image.Image:
328
+ """
329
+ Preprocess the input image.
330
+ """
331
+ # if has alpha channel, use it directly; otherwise, remove background
332
  has_alpha = False
333
  if input.mode == 'RGBA':
334
  alpha = np.array(input)[:, :, 3]
 
350
  size = max(bbox[2] - bbox[0], bbox[3] - bbox[1])
351
  size = int(size * 1)
352
  bbox = center[0] - size // 2, center[1] - size // 2, center[0] + size // 2, center[1] + size // 2
353
+ output = output.crop(bbox) # type: ignore
354
  output = np.array(output).astype(np.float32) / 255
355
  output = output[:, :, :3] * output[:, :, 3:4]
356
  output = Image.fromarray((output * 255).astype(np.uint8))
 
358
 
359
 
360
  def preprocess_images(images: List[Tuple[Image.Image, str]]) -> List[Image.Image]:
361
+ """
362
+ Preprocess a list of input images for multi-image conditioning.
363
+ Uses parallel processing for faster background removal.
364
+ """
365
+ images = [image[0] for image in images]
366
+ with ThreadPoolExecutor(max_workers=min(4, len(images))) as executor:
367
+ processed_images = list(executor.map(preprocess_image, images))
368
  return processed_images
369
 
370
 
371
+ def pack_state(latents: Tuple[SparseTensor, SparseTensor, int]) -> dict:
372
  shape_slat, tex_slat, res = latents
373
  return {
374
  'shape_slat_feats': shape_slat.feats.cpu().numpy(),
 
378
  }
379
 
380
 
381
+ def unpack_state(state: dict) -> Tuple[SparseTensor, SparseTensor, int]:
 
382
  shape_slat = SparseTensor(
383
  feats=torch.from_numpy(state['shape_slat_feats']).cuda(),
384
  coords=torch.from_numpy(state['coords']).cuda(),
 
388
 
389
 
390
  def get_seed(randomize_seed: bool, seed: int) -> int:
391
+ """
392
+ Get the random seed.
393
+ """
394
  return np.random.randint(0, MAX_SEED) if randomize_seed else seed
395
 
396
 
397
  def prepare_multi_example() -> List[Image.Image]:
398
+ """
399
+ Prepare multi-image examples for the gallery.
400
+ """
401
+ multi_case = list(set([i.split('_')[0] for i in os.listdir("assets/example_multi_image")]))
 
402
  images = []
403
+ for case in multi_case:
404
+ _images = []
405
+ for i in range(1, 4):
406
+ img = Image.open(f'assets/example_multi_image/{case}_{i}.png')
407
+ W, H = img.size
408
+ img = img.resize((int(W / H * 512), 512))
409
+ _images.append(np.array(img))
410
+ images.append(Image.fromarray(np.concatenate(_images, axis=1)))
 
 
 
411
  return images
412
 
413
 
414
  def split_image(image: Image.Image) -> List[Image.Image]:
415
+ """
416
+ Split a concatenated image into multiple views.
417
+ """
418
  image = np.array(image)
419
  alpha = image[..., 3]
420
  alpha = np.any(alpha > 0, axis=0)
 
423
  images = []
424
  for s, e in zip(start_pos, end_pos):
425
  images.append(Image.fromarray(image[:, s:e+1]))
426
+ return [preprocess_image(image) for image in images]
427
 
428
 
429
  @spaces.GPU(duration=120)
430
  def image_to_3d(
431
+ image: Image.Image,
432
  seed: int,
433
  resolution: str,
434
  ss_guidance_strength: float,
 
443
  tex_slat_guidance_rescale: float,
444
  tex_slat_sampling_steps: int,
445
  tex_slat_rescale_t: float,
 
446
  req: gr.Request,
447
  progress=gr.Progress(track_tqdm=True),
448
+ multiimages: List[Tuple[Image.Image, str]] = None,
449
+ is_multiimage: bool = False,
450
+ multiimage_algo: Literal["multidiffusion", "stochastic"] = "stochastic",
451
  ) -> str:
 
 
 
 
 
 
 
 
 
452
  # --- Sampling ---
453
+ if not is_multiimage:
 
454
  outputs, latents = pipeline.run(
455
+ image,
456
  seed=seed,
457
  preprocess_image=False,
458
  sparse_structure_sampler_params={
 
481
  return_latent=True,
482
  )
483
  else:
 
484
  outputs, latents = pipeline.run_multi_image(
485
+ [image[0] for image in multiimages],
486
  seed=seed,
487
  preprocess_image=False,
488
  sparse_structure_sampler_params={
 
511
  return_latent=True,
512
  mode=multiimage_algo,
513
  )
 
514
  mesh = outputs[0]
515
+ mesh.simplify(16777216) # nvdiffrast limit
516
+ images = render_utils.render_snapshot(mesh, resolution=1024, r=2, fov=36, nviews=STEPS, envmap=envmap)
517
  state = pack_state(latents)
518
  torch.cuda.empty_cache()
519
 
520
  # --- HTML Construction ---
521
+ # The Stack of 48 Images - encode in parallel for speed
522
+ def encode_preview_image(args):
523
+ m_idx, s_idx, render_key = args
524
+ img_base64 = image_to_base64(Image.fromarray(images[render_key][s_idx]))
525
+ return (m_idx, s_idx, img_base64)
526
+
527
+ encode_tasks = [
528
+ (m_idx, s_idx, mode['render_key'])
529
+ for m_idx, mode in enumerate(MODES)
530
+ for s_idx in range(STEPS)
531
+ ]
532
+
533
+ with ThreadPoolExecutor(max_workers=8) as executor:
534
+ encoded_results = list(executor.map(encode_preview_image, encode_tasks))
535
+
536
+ # Build HTML from encoded results
537
+ encoded_map = {(m, s): b64 for m, s, b64 in encoded_results}
538
  images_html = ""
539
  for m_idx, mode in enumerate(MODES):
540
  for s_idx in range(STEPS):
541
  unique_id = f"view-m{m_idx}-s{s_idx}"
542
  is_visible = (m_idx == DEFAULT_MODE and s_idx == DEFAULT_STEP)
543
  vis_class = "visible" if is_visible else ""
544
+ img_base64 = encoded_map[(m_idx, s_idx)]
545
+
546
+ images_html += f"""
547
+ <img id="{unique_id}"
548
+ class="previewer-main-image {vis_class}"
549
+ src="{img_base64}"
550
+ loading="eager">
551
+ """
552
 
553
+ # Button Row HTML
554
  btns_html = ""
555
  for idx, mode in enumerate(MODES):
556
  active_class = "active" if idx == DEFAULT_MODE else ""
557
+ # Note: onclick calls the JS function defined in Head
558
+ btns_html += f"""
559
+ <img src="{mode['icon_base64']}"
560
+ class="mode-btn {active_class}"
561
+ onclick="selectMode({idx})"
562
+ title="{mode['name']}">
563
+ """
564
+
565
+ # Assemble the full component
566
  full_html = f"""
567
  <div class="previewer-container">
568
  <div class="tips-wrapper">
569
+ <div class="tips-icon">💡Tips</div>
570
  <div class="tips-text">
571
+ <p>● <b>Render Mode</b> - Click on the circular buttons to switch between different render modes.</p>
572
+ <p>● <b>View Angle</b> - Drag the slider to change the view angle.</p>
573
  </div>
574
  </div>
575
+
576
+ <!-- Row 1: Viewport containing 48 static <img> tags -->
577
+ <div class="display-row">
578
+ {images_html}
579
+ </div>
580
+
581
+ <!-- Row 2 -->
582
+ <div class="mode-row" id="btn-group">
583
+ {btns_html}
584
+ </div>
585
+
586
+ <!-- Row 3: Slider -->
587
  <div class="slider-row">
588
  <input type="range" id="custom-slider" min="0" max="{STEPS - 1}" value="{DEFAULT_STEP}" step="1" oninput="onSliderChange(this.value)">
589
  </div>
590
  </div>
591
  """
592
+
593
  return state, full_html
594
 
595
 
 
601
  req: gr.Request,
602
  progress=gr.Progress(track_tqdm=True),
603
  ) -> Tuple[str, str]:
604
+ """
605
+ Extract a GLB file from the 3D model.
606
+
607
+ Args:
608
+ state (dict): The state of the generated 3D model.
609
+ decimation_target (int): The target face count for decimation.
610
+ texture_size (int): The texture resolution.
611
 
612
+ Returns:
613
+ str: The path to the extracted GLB file.
614
+ """
615
  user_dir = os.path.join(TMP_DIR, str(req.session_hash))
616
  shape_slat, tex_slat, res = unpack_state(state)
617
  mesh = pipeline.decode_latent(shape_slat, tex_slat, res)[0]
618
+ mesh.simplify(16777216) # nvdiffrast limit
619
  glb = o_voxel.postprocess.to_glb(
620
  vertices=mesh.vertices,
621
  faces=mesh.faces,
 
642
 
643
  with gr.Blocks(delete_cache=(600, 600)) as demo:
644
  gr.Markdown("""
645
+ ## Image to 3D Asset with [TRELLIS.2](https://microsoft.github.io/TRELLIS.2)
646
+ * Upload an image (preferably with an alpha-masked foreground object) and click Generate to create a 3D asset.
647
+ * Click Extract GLB to export and download the generated GLB file if you're satisfied with the result. Otherwise, try another time.
 
648
  """)
649
 
650
  with gr.Row():
651
  with gr.Column(scale=1, min_width=360):
652
+ with gr.Tabs() as input_tabs:
653
+ with gr.Tab(label="Single Image", id=0) as single_image_input_tab:
654
+ image_prompt = gr.Image(label="Image Prompt", format="png", image_mode="RGBA", type="pil", height=400)
655
+ with gr.Tab(label="Multiple Images", id=1) as multiimage_input_tab:
656
+ multiimage_prompt = gr.Gallery(label="Image Prompt", format="png", type="pil", height=400, columns=3)
657
+ gr.Markdown("""
658
+ Input different views of the object in separate images.
659
+ *NOTE: this is an experimental algorithm without training a specialized model. It may not produce the best results for all images, especially those having different poses or inconsistent details.*
660
+ """)
661
 
662
  resolution = gr.Radio(["512", "1024", "1536"], label="Resolution", value="1024")
663
  seed = gr.Slider(0, MAX_SEED, label="Seed", value=0, step=1)
 
689
  multiimage_algo = gr.Radio(["stochastic", "multidiffusion"], label="Multi-image Algorithm", value="stochastic")
690
 
691
  with gr.Column(scale=10):
692
+ with gr.Tabs() as tabs:
693
+ with gr.Tab("Preview", id=0):
694
  preview_output = gr.HTML(empty_html, label="3D Asset Preview", show_label=True, container=True)
695
  extract_btn = gr.Button("Extract GLB")
696
+ with gr.Tab("Extract", id=1):
697
  glb_output = gr.Model3D(label="Extracted GLB", height=724, show_label=True, display_mode="solid", clear_color=(0.25, 0.25, 0.25, 1.0))
698
  download_btn = gr.DownloadButton(label="Download GLB")
699
+ gr.Markdown("*We are actively working on improving the speed of GLB extraction. Currently, it may take half a minute or more and face count is limited.*")
700
 
701
+ with gr.Column(scale=1, min_width=172) as single_image_example:
 
 
 
702
  examples = gr.Examples(
703
+ examples=[
704
+ f'assets/example_image/{image}'
705
+ for image in os.listdir("assets/example_image")
706
+ ],
707
+ inputs=[image_prompt],
708
+ fn=preprocess_image,
709
+ outputs=[image_prompt],
710
+ run_on_click=True,
711
+ examples_per_page=18,
712
+ )
713
+
714
+ with gr.Column(visible=True) as multiimage_example:
715
+ examples_multi = gr.Examples(
716
  examples=prepare_multi_example(),
717
+ label="Multi Image Examples",
718
+ inputs=[image_prompt],
719
  fn=split_image,
720
+ outputs=[multiimage_prompt],
721
  run_on_click=True,
722
+ examples_per_page=8,
723
  )
724
 
725
+ is_multiimage = gr.State(False)
726
  output_buf = gr.State()
727
 
728
+
729
  # Handlers
730
  demo.load(start_session)
731
  demo.unload(end_session)
732
 
733
+ single_image_input_tab.select(
734
+ lambda: (False, gr.update(visible=True), gr.update(visible=True)),
735
+ outputs=[is_multiimage, single_image_example, multiimage_example]
736
+ )
737
+ multiimage_input_tab.select(
738
+ lambda: (True, gr.update(visible=True), gr.update(visible=True)),
739
+ outputs=[is_multiimage, single_image_example, multiimage_example]
740
+ )
741
+
742
  image_prompt.upload(
743
+ preprocess_image,
744
  inputs=[image_prompt],
745
  outputs=[image_prompt],
746
  )
747
+ multiimage_prompt.upload(
748
+ preprocess_images,
749
+ inputs=[multiimage_prompt],
750
+ outputs=[multiimage_prompt],
751
+ )
752
 
753
  generate_btn.click(
754
+ get_seed,
755
+ inputs=[randomize_seed, seed],
756
+ outputs=[seed],
757
  ).then(
758
+ lambda: gr.Tabs(selected=0), outputs=tabs
759
  ).then(
760
  image_to_3d,
761
  inputs=[
 
763
  ss_guidance_strength, ss_guidance_rescale, ss_sampling_steps, ss_rescale_t,
764
  shape_slat_guidance_strength, shape_slat_guidance_rescale, shape_slat_sampling_steps, shape_slat_rescale_t,
765
  tex_slat_guidance_strength, tex_slat_guidance_rescale, tex_slat_sampling_steps, tex_slat_rescale_t,
766
+ multiimage_prompt, is_multiimage, multiimage_algo
767
  ],
768
  outputs=[output_buf, preview_output],
769
  )
770
 
771
  extract_btn.click(
772
+ lambda: gr.Tabs(selected=1), outputs=tabs
773
  ).then(
774
  extract_glb,
775
  inputs=[output_buf, decimation_target, texture_size],
 
777
  )
778
 
779
 
780
+ # Launch the Gradio app
781
  if __name__ == "__main__":
782
  os.makedirs(TMP_DIR, exist_ok=True)
783
 
784
+ # Construct ui components
785
+ btn_img_base64_strs = {}
786
  for i in range(len(MODES)):
787
  icon = Image.open(MODES[i]['icon'])
788
  MODES[i]['icon_base64'] = image_to_base64(icon)
789
 
790
  rmbg_client = Client("briaai/BRIA-RMBG-2.0")
791
+ pipeline = Trellis2ImageTo3DPipeline.from_pretrained('microsoft/TRELLIS.2-4B')
792
+ pipeline.rembg_model = None
793
+ pipeline.low_vram = False
794
+ pipeline.cuda()
795
+
796
+ envmap = {
797
+ 'forest': EnvMap(torch.tensor(
798
+ cv2.cvtColor(cv2.imread('assets/hdri/forest.exr', cv2.IMREAD_UNCHANGED), cv2.COLOR_BGR2RGB),
799
+ dtype=torch.float32, device='cuda'
800
+ )),
801
+ 'sunset': EnvMap(torch.tensor(
802
+ cv2.cvtColor(cv2.imread('assets/hdri/sunset.exr', cv2.IMREAD_UNCHANGED), cv2.COLOR_BGR2RGB),
803
+ dtype=torch.float32, device='cuda'
804
+ )),
805
+ 'courtyard': EnvMap(torch.tensor(
806
+ cv2.cvtColor(cv2.imread('assets/hdri/courtyard.exr', cv2.IMREAD_UNCHANGED), cv2.COLOR_BGR2RGB),
807
+ dtype=torch.float32, device='cuda'
808
+ )),
809
+ }
810
 
811
  demo.launch(css=css, head=head)
app_texturing.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ import os
4
+ os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
5
+ from datetime import datetime
6
+ import shutil
7
+ from typing import *
8
+ import torch
9
+ import numpy as np
10
+ import trimesh
11
+ from PIL import Image
12
+ from trellis2.pipelines import Trellis2TexturingPipeline
13
+
14
+
15
+ MAX_SEED = np.iinfo(np.int32).max
16
+ TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
17
+
18
+
19
+ def start_session(req: gr.Request):
20
+ user_dir = os.path.join(TMP_DIR, str(req.session_hash))
21
+ os.makedirs(user_dir, exist_ok=True)
22
+
23
+
24
+ def end_session(req: gr.Request):
25
+ user_dir = os.path.join(TMP_DIR, str(req.session_hash))
26
+ shutil.rmtree(user_dir)
27
+
28
+
29
+ def preprocess_image(image: Image.Image) -> Image.Image:
30
+ """
31
+ Preprocess the input image.
32
+
33
+ Args:
34
+ image (Image.Image): The input image.
35
+
36
+ Returns:
37
+ Image.Image: The preprocessed image.
38
+ """
39
+ processed_image = pipeline.preprocess_image(image)
40
+ return processed_image
41
+
42
+
43
+ def get_seed(randomize_seed: bool, seed: int) -> int:
44
+ """
45
+ Get the random seed.
46
+ """
47
+ return np.random.randint(0, MAX_SEED) if randomize_seed else seed
48
+
49
+
50
+ def shapeimage_to_tex(
51
+ mesh_file: str,
52
+ image: Image.Image,
53
+ seed: int,
54
+ resolution: str,
55
+ texture_size: int,
56
+ tex_slat_guidance_strength: float,
57
+ tex_slat_guidance_rescale: float,
58
+ tex_slat_sampling_steps: int,
59
+ tex_slat_rescale_t: float,
60
+ req: gr.Request,
61
+ progress=gr.Progress(track_tqdm=True),
62
+ ) -> str:
63
+ mesh = trimesh.load(mesh_file)
64
+ if isinstance(mesh, trimesh.Scene):
65
+ mesh = mesh.to_mesh()
66
+ output = pipeline.run(
67
+ mesh,
68
+ image,
69
+ seed=seed,
70
+ preprocess_image=False,
71
+ tex_slat_sampler_params={
72
+ "steps": tex_slat_sampling_steps,
73
+ "guidance_strength": tex_slat_guidance_strength,
74
+ "guidance_rescale": tex_slat_guidance_rescale,
75
+ "rescale_t": tex_slat_rescale_t,
76
+ },
77
+ resolution=int(resolution),
78
+ texture_size=texture_size,
79
+ )
80
+ now = datetime.now()
81
+ timestamp = now.strftime("%Y-%m-%dT%H%M%S") + f".{now.microsecond // 1000:03d}"
82
+ user_dir = os.path.join(TMP_DIR, str(req.session_hash))
83
+ os.makedirs(user_dir, exist_ok=True)
84
+ glb_path = os.path.join(user_dir, f'sample_{timestamp}.glb')
85
+ output.export(glb_path, extension_webp=True)
86
+ torch.cuda.empty_cache()
87
+ return glb_path, glb_path
88
+
89
+
90
+ with gr.Blocks(delete_cache=(600, 600)) as demo:
91
+ gr.Markdown("""
92
+ ## Texturing a mesh with [TRELLIS.2](https://microsoft.github.io/TRELLIS.2)
93
+ * Upload a mesh and corresponding reference image (preferably with an alpha-masked foreground object) and click Generate to create a textured 3D asset.
94
+ """)
95
+
96
+ with gr.Row():
97
+ with gr.Column(scale=1, min_width=360):
98
+ mesh_file = gr.File(label="Upload Mesh", file_types=[".ply", ".obj", ".glb", ".gltf"], file_count="single")
99
+ image_prompt = gr.Image(label="Image Prompt", format="png", image_mode="RGBA", type="pil", height=400)
100
+
101
+ resolution = gr.Radio(["512", "1024", "1536"], label="Resolution", value="1024")
102
+ seed = gr.Slider(0, MAX_SEED, label="Seed", value=0, step=1)
103
+ randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
104
+ texture_size = gr.Slider(1024, 4096, label="Texture Size", value=2048, step=1024)
105
+
106
+ generate_btn = gr.Button("Generate")
107
+
108
+ with gr.Accordion(label="Advanced Settings", open=False):
109
+ with gr.Row():
110
+ tex_slat_guidance_strength = gr.Slider(1.0, 10.0, label="Guidance Strength", value=1.0, step=0.1)
111
+ tex_slat_guidance_rescale = gr.Slider(0.0, 1.0, label="Guidance Rescale", value=0.0, step=0.01)
112
+ tex_slat_sampling_steps = gr.Slider(1, 50, label="Sampling Steps", value=12, step=1)
113
+ tex_slat_rescale_t = gr.Slider(1.0, 6.0, label="Rescale T", value=3.0, step=0.1)
114
+
115
+ with gr.Column(scale=10):
116
+ glb_output = gr.Model3D(label="Extracted GLB", height=724, show_label=True, display_mode="solid", clear_color=(0.25, 0.25, 0.25, 1.0))
117
+ download_btn = gr.DownloadButton(label="Download GLB")
118
+
119
+
120
+ # Handlers
121
+ demo.load(start_session)
122
+ demo.unload(end_session)
123
+
124
+ image_prompt.upload(
125
+ preprocess_image,
126
+ inputs=[image_prompt],
127
+ outputs=[image_prompt],
128
+ )
129
+
130
+ generate_btn.click(
131
+ get_seed,
132
+ inputs=[randomize_seed, seed],
133
+ outputs=[seed],
134
+ ).then(
135
+ shapeimage_to_tex,
136
+ inputs=[
137
+ mesh_file, image_prompt, seed, resolution, texture_size,
138
+ tex_slat_guidance_strength, tex_slat_guidance_rescale, tex_slat_sampling_steps, tex_slat_rescale_t,
139
+ ],
140
+ outputs=[glb_output, download_btn],
141
+ )
142
+
143
+
144
+ # Launch the Gradio app
145
+ if __name__ == "__main__":
146
+ os.makedirs(TMP_DIR, exist_ok=True)
147
+
148
+ pipeline = Trellis2TexturingPipeline.from_pretrained('microsoft/TRELLIS.2-4B', config_file="texturing_pipeline.json")
149
+ pipeline.cuda()
150
+
151
+ demo.launch()
o-voxel/README.md ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # O-Voxel: A Native 3D Representation
2
+
3
+ **O-Voxel** is a sparse, voxel-based native 3D representation designed for high-quality 3D generation and reconstruction. Unlike traditional methods that rely on fields (e.g., Occupancy fields, SDFs), O-Voxel utilizes a **Flexible Dual Grid** formulation to robustly represent surfaces with arbitrary topology (including non-manifold and open surfaces) and **volumetric surface properties** such as Physically-Based Rendering (PBR) material attributes.
4
+
5
+ This library provides an efficient implementation for the instant bidirectional conversion between Meshes and O-Voxels, along with tools for sparse voxel compression, serialization, and rendering.
6
+
7
+ ![Overview](assets/overview.webp)
8
+
9
+ ## Key Features
10
+
11
+ - **🧱 Flexible Dual Grid**: A geometry representation that solves a enhanced QEF (Quadratic Error Function) to accurately capture sharp features and open boundaries without requiring watertight meshes.
12
+ - **🎨 Volumetric PBR Attributes**: Native support for physically-based rendering properties (Base Color, Metallic, Roughness, Opacity) aligned with the sparse voxel grid.
13
+ - **⚡ Instant Bidirectional Conversion**: Rapid `Mesh <-> O-Voxel` conversion without expensive SDF evaluation, flood-filling, or iterative optimization.
14
+ - **💾 Efficient Compression**: Supports custom `.vxz` format for compact storage of sparse voxel structures using Z-order/Hilbert curve encoding.
15
+ - **🛠️ Production Ready**: Tools to export converted assets directly to `.glb` with UV unwrapping and texture baking.
16
+
17
+ ## Installation
18
+
19
+ ```bash
20
+ git clone -b main https://github.com/microsoft/TRELLIS.2.git --recursive
21
+ pip install TRELLIS.2/o_voxel --no-build-isolation
22
+ ```
23
+
24
+ ## Quick Start
25
+
26
+ > See also the [examples](examples) directory for more detailed usage.
27
+
28
+ ### 1. Convert Mesh to O-Voxel [[link]](examples/mesh2ovox.py)
29
+ Convert a standard 3D mesh (with textures) into the O-Voxel representation.
30
+
31
+ ```python
32
+ asset = trimesh.load("path/to/mesh.glb")
33
+
34
+ # 1. Geometry Voxelization (Flexible Dual Grid)
35
+ # Returns: occupied indices, dual vertices (QEF solution), and edge intersected
36
+ mesh = asset.to_mesh()
37
+ vertices = torch.from_numpy(mesh.vertices).float()
38
+ faces = torch.from_numpy(mesh.faces).long()
39
+ voxel_indices, dual_vertices, intersected = o_voxel.convert.mesh_to_flexible_dual_grid(
40
+ vertices, faces,
41
+ grid_size=RES, # Resolution
42
+ aabb=[[-0.5,-0.5,-0.5],[0.5,0.5,0.5]], # Axis-aligned bounding box
43
+ face_weight=1.0, # Face term weight in QEF
44
+ boundary_weight=0.2, # Boundary term weight in QEF
45
+ regularization_weight=1e-2, # Regularization term weight in QEF
46
+ timing=True
47
+ )
48
+ ## sort to ensure align between geometry and material voxelization
49
+ vid = o_voxel.serialize.encode_seq(voxel_indices)
50
+ mapping = torch.argsort(vid)
51
+ voxel_indices = voxel_indices[mapping]
52
+ dual_vertices = dual_vertices[mapping]
53
+ intersected = intersected[mapping]
54
+
55
+ # 2. Material Voxelization (Volumetric Attributes)
56
+ # Returns: dict containing 'base_color', 'metallic', 'roughness', etc.
57
+ voxel_indices_mat, attributes = o_voxel.convert.textured_mesh_to_volumetric_attr(
58
+ asset,
59
+ grid_size=RES,
60
+ aabb=[[-0.5,-0.5,-0.5],[0.5,0.5,0.5]],
61
+ timing=True
62
+ )
63
+ ## sort to ensure align between geometry and material voxelization
64
+ vid_mat = o_voxel.serialize.encode_seq(voxel_indices_mat)
65
+ mapping_mat = torch.argsort(vid_mat)
66
+ attributes = {k: v[mapping_mat] for k, v in attributes.items()}
67
+
68
+ # Save to compressed .vxz format
69
+ ## packing
70
+ dual_vertices = dual_vertices * RES - voxel_indices
71
+ dual_vertices = (torch.clamp(dual_vertices, 0, 1) * 255).type(torch.uint8)
72
+ intersected = (intersected[:, 0:1] + 2 * intersected[:, 1:2] + 4 * intersected[:, 2:3]).type(torch.uint8)
73
+ attributes['dual_vertices'] = dual_vertices
74
+ attributes['intersected'] = intersected
75
+ o_voxel.io.write("ovoxel_helmet.vxz", voxel_indices, attributes)
76
+ ```
77
+
78
+ ### 2. Recover Mesh from O-Voxel [[link]](examples/ovox2mesh.py)
79
+ Reconstruct the surface mesh from the sparse voxel data.
80
+
81
+ ```python
82
+ # Load data
83
+ coords, data = o_voxel.io.read("path/to/ovoxel.vxz")
84
+ dual_vertices = data['dual_vertices']
85
+ intersected = data['intersected']
86
+ base_color = data['base_color']
87
+ ## ... other attributes omitted for brevity
88
+
89
+ # Depack
90
+ dual_vertices = dual_vertices / 255
91
+ intersected = torch.cat([
92
+ intersected % 2,
93
+ intersected // 2 % 2,
94
+ intersected // 4 % 2,
95
+ ], dim=-1).bool()
96
+
97
+ # Extract Mesh
98
+ # O-Voxel connects dual vertices to form quads, optionally splitting them
99
+ # based on geometric features.
100
+ rec_verts, rec_faces = o_voxel.convert.flexible_dual_grid_to_mesh(
101
+ coords.cuda(),
102
+ dual_vertices.cuda(),
103
+ intersected.cuda(),
104
+ split_weight=None, # Auto-split based on min angle if None
105
+ grid_size=RES,
106
+ aabb=[[-0.5,-0.5,-0.5],[0.5,0.5,0.5]],
107
+ )
108
+ ```
109
+
110
+ ### 3. Export to GLB [[link]](examples/ovox2glb.py)
111
+ For visualization in standard 3D viewers, you can clean, UV-unwrap, and bake the volumetric attributes into textures.
112
+
113
+ ```python
114
+ # Assuming you have the reconstructed verts/faces and volume attributes
115
+ mesh = o_voxel.postprocess.to_glb(
116
+ vertices=rec_verts,
117
+ faces=rec_faces,
118
+ attr_volume=attr_tensor, # Concatenated attributes
119
+ coords=coords,
120
+ attr_layout={'base_color': slice(0,3), 'metallic': slice(3,4), ...},
121
+ grid_size=RES,
122
+ aabb=[[-0.5,-0.5,-0.5],[0.5,0.5,0.5]],
123
+ decimation_target=100000,
124
+ texture_size=2048,
125
+ verbose=True,
126
+ )
127
+ mesh.export("rec_helmet.glb")
128
+ ```
129
+
130
+ ### 4. Voxel Rendering [[link]](examples/render_ovox.py)
131
+ Render the voxel representation directly.
132
+
133
+ ```python
134
+ # Load data
135
+ coords, data = o_voxel.io.read("ovoxel_helmet.vxz")
136
+ position = (coords / RES - 0.5).cuda()
137
+ base_color = (data['base_color'] / 255).cuda()
138
+
139
+ # Render
140
+ renderer = o_voxel.rasterize.VoxelRenderer(
141
+ rendering_options={"resolution": 512, "ssaa": 2}
142
+ )
143
+ output = renderer.render(
144
+ position=position, # Voxel centers
145
+ attrs=base_color, # Color/Opacity etc.
146
+ voxel_size=1.0/RES,
147
+ extrinsics=extr,
148
+ intrinsics=intr
149
+ )
150
+ # output.attr contains the rendered image (C, H, W)
151
+ ```
152
+
153
+ ## API Overview
154
+
155
+ ### `o_voxel.convert`
156
+ Core algorithms for the conversion between meshes and O-Voxels.
157
+ * `mesh_to_flexible_dual_grid`: Determines the active sparse voxels and solves the QEF to determine dual vertex positions within voxels based on mesh-voxel grid intersections.
158
+ * `flexible_dual_grid_to_mesh`: Reconnects dual vertices to form a surface.
159
+ * `textured_mesh_to_volumetric_attr`: Samples texture maps into voxel space.
160
+
161
+ ### `o_voxel.io`
162
+ Handles sparse voxel file I/O operations.
163
+ * **Formats**: `.npz` (NumPy), `.ply` (Point Cloud), `.vxz` (Custom compressed, recommended).
164
+ * **Functions**: `read()`, `write()`.
165
+
166
+ ### `o_voxel.serialize`
167
+ Utilities for spatial hashing and ordering.
168
+ * `encode_seq` / `decode_seq`: Converts 3D coordinates to/from Morton codes (Z-order) or Hilbert curves for efficient storage and processing.
169
+
170
+ ### `o_voxel.rasterize`
171
+ * `VoxelRenderer`: A lightweight renderer for sparse voxel visualization during training.
172
+
173
+ ### `o_voxel.postprocess`
174
+ * `to_glb`: A comprehensive pipeline for mesh cleaning, remeshing, UV unwrapping, and texture baking.
o-voxel/assets/overview.webp ADDED

Git LFS Details

  • SHA256: ba51a74520803e36ec5be8e1aab1c71fea8a8df78a0502f0736e338ff4e97f93
  • Pointer size: 131 Bytes
  • Size of remote file: 392 kB
o-voxel/build/lib.win-amd64-cpython-311/o_voxel/__init__.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ from . import (
2
+ convert,
3
+ io,
4
+ postprocess,
5
+ rasterize,
6
+ serialize
7
+ )
o-voxel/build/lib.win-amd64-cpython-311/o_voxel/convert/__init__.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ from .flexible_dual_grid import *
2
+ from .volumetic_attr import *
o-voxel/build/lib.win-amd64-cpython-311/o_voxel/convert/flexible_dual_grid.py ADDED
@@ -0,0 +1,283 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import *
2
+ import numpy as np
3
+ import torch
4
+ from .. import _C
5
+
6
+ __all__ = [
7
+ "mesh_to_flexible_dual_grid",
8
+ "flexible_dual_grid_to_mesh",
9
+ ]
10
+
11
+
12
+ def _init_hashmap(grid_size, capacity, device):
13
+ VOL = (grid_size[0] * grid_size[1] * grid_size[2]).item()
14
+
15
+ # If the number of elements in the tensor is less than 2^32, use uint32 as the hashmap type, otherwise use uint64.
16
+ if VOL < 2**32:
17
+ hashmap_keys = torch.full((capacity,), torch.iinfo(torch.uint32).max, dtype=torch.uint32, device=device)
18
+ elif VOL < 2**64:
19
+ hashmap_keys = torch.full((capacity,), torch.iinfo(torch.uint64).max, dtype=torch.uint64, device=device)
20
+ else:
21
+ raise ValueError(f"The spatial size is too large to fit in a hashmap. Get volumn {VOL} > 2^64.")
22
+
23
+ hashmap_vals = torch.empty((capacity,), dtype=torch.uint32, device=device)
24
+
25
+ return hashmap_keys, hashmap_vals
26
+
27
+
28
+ @torch.no_grad()
29
+ def mesh_to_flexible_dual_grid(
30
+ vertices: torch.Tensor,
31
+ faces: torch.Tensor,
32
+ voxel_size: Union[float, list, tuple, np.ndarray, torch.Tensor] = None,
33
+ grid_size: Union[int, list, tuple, np.ndarray, torch.Tensor] = None,
34
+ aabb: Union[list, tuple, np.ndarray, torch.Tensor] = None,
35
+ face_weight: float = 1.0,
36
+ boundary_weight: float = 1.0,
37
+ regularization_weight: float = 0.1,
38
+ timing: bool = False,
39
+ ) -> Union[torch.Tensor, torch.Tensor, torch.Tensor]:
40
+ """
41
+ Voxelize a mesh into a sparse voxel grid.
42
+
43
+ Args:
44
+ vertices (torch.Tensor): The vertices of the mesh.
45
+ faces (torch.Tensor): The faces of the mesh.
46
+ voxel_size (float, list, tuple, np.ndarray, torch.Tensor): The size of each voxel.
47
+ grid_size (int, list, tuple, np.ndarray, torch.Tensor): The size of the grid.
48
+ NOTE: One of voxel_size and grid_size must be provided.
49
+ aabb (list, tuple, np.ndarray, torch.Tensor): The axis-aligned bounding box of the mesh.
50
+ If not provided, it will be computed automatically.
51
+ face_weight (float): The weight of the face term in the QEF when solving the dual vertices.
52
+ boundary_weight (float): The weight of the boundary term in the QEF when solving the dual vertices.
53
+ regularization_weight (float): The weight of the regularization term in the QEF when solving the dual vertices.
54
+ timing (bool): Whether to time the voxelization process.
55
+
56
+ Returns:
57
+ torch.Tensor: The indices of the voxels that are occupied by the mesh.
58
+ The shape of the tensor is (N, 3), where N is the number of occupied voxels.
59
+ torch.Tensor: The dual vertices of the mesh.
60
+ torch.Tensor: The intersected flag of each voxel.
61
+ """
62
+
63
+ # Load mesh
64
+ vertices = vertices.float()
65
+ faces = faces.int()
66
+
67
+ # Voxelize settings
68
+ assert voxel_size is not None or grid_size is not None, "Either voxel_size or grid_size must be provided"
69
+
70
+ if voxel_size is not None:
71
+ if isinstance(voxel_size, float):
72
+ voxel_size = [voxel_size, voxel_size, voxel_size]
73
+ if isinstance(voxel_size, (list, tuple)):
74
+ voxel_size = np.array(voxel_size)
75
+ if isinstance(voxel_size, np.ndarray):
76
+ voxel_size = torch.tensor(voxel_size, dtype=torch.float32)
77
+ assert isinstance(voxel_size, torch.Tensor), f"voxel_size must be a float, list, tuple, np.ndarray, or torch.Tensor, but got {type(voxel_size)}"
78
+ assert voxel_size.dim() == 1, f"voxel_size must be a 1D tensor, but got {voxel_size.shape}"
79
+ assert voxel_size.size(0) == 3, f"voxel_size must have 3 elements, but got {voxel_size.size(0)}"
80
+
81
+ if grid_size is not None:
82
+ if isinstance(grid_size, int):
83
+ grid_size = [grid_size, grid_size, grid_size]
84
+ if isinstance(grid_size, (list, tuple)):
85
+ grid_size = np.array(grid_size)
86
+ if isinstance(grid_size, np.ndarray):
87
+ grid_size = torch.tensor(grid_size, dtype=torch.int32)
88
+ assert isinstance(grid_size, torch.Tensor), f"grid_size must be an int, list, tuple, np.ndarray, or torch.Tensor, but got {type(grid_size)}"
89
+ assert grid_size.dim() == 1, f"grid_size must be a 1D tensor, but got {grid_size.shape}"
90
+ assert grid_size.size(0) == 3, f"grid_size must have 3 elements, but got {grid_size.size(0)}"
91
+
92
+ if aabb is not None:
93
+ if isinstance(aabb, (list, tuple)):
94
+ aabb = np.array(aabb)
95
+ if isinstance(aabb, np.ndarray):
96
+ aabb = torch.tensor(aabb, dtype=torch.float32)
97
+ assert isinstance(aabb, torch.Tensor), f"aabb must be a list, tuple, np.ndarray, or torch.Tensor, but got {type(aabb)}"
98
+ assert aabb.dim() == 2, f"aabb must be a 2D tensor, but got {aabb.shape}"
99
+ assert aabb.size(0) == 2, f"aabb must have 2 rows, but got {aabb.size(0)}"
100
+ assert aabb.size(1) == 3, f"aabb must have 3 columns, but got {aabb.size(1)}"
101
+
102
+ # Auto adjust aabb
103
+ if aabb is None:
104
+ min_xyz = vertices.min(dim=0).values
105
+ max_xyz = vertices.max(dim=0).values
106
+
107
+ if voxel_size is not None:
108
+ padding = torch.ceil((max_xyz - min_xyz) / voxel_size) * voxel_size - (max_xyz - min_xyz)
109
+ min_xyz -= padding * 0.5
110
+ max_xyz += padding * 0.5
111
+ if grid_size is not None:
112
+ padding = (max_xyz - min_xyz) / (grid_size - 1)
113
+ min_xyz -= padding * 0.5
114
+ max_xyz += padding * 0.5
115
+
116
+ aabb = torch.stack([min_xyz, max_xyz], dim=0).float().cuda()
117
+
118
+ # Fill voxel size or grid size
119
+ if voxel_size is None:
120
+ voxel_size = (aabb[1] - aabb[0]) / grid_size
121
+ if grid_size is None:
122
+ grid_size = ((aabb[1] - aabb[0]) / voxel_size).round().int()
123
+
124
+ # subdivide mesh
125
+ vertices = vertices - aabb[0].reshape(1, 3)
126
+ grid_range = torch.stack([torch.zeros_like(grid_size), grid_size], dim=0).int()
127
+
128
+ ret = _C.mesh_to_flexible_dual_grid_cpu(
129
+ vertices,
130
+ faces,
131
+ voxel_size,
132
+ grid_range,
133
+ face_weight,
134
+ boundary_weight,
135
+ regularization_weight,
136
+ timing,
137
+ )
138
+
139
+ return ret
140
+
141
+
142
+ def flexible_dual_grid_to_mesh(
143
+ coords: torch.Tensor,
144
+ dual_vertices: torch.Tensor,
145
+ intersected_flag: torch.Tensor,
146
+ split_weight: Union[torch.Tensor, None],
147
+ aabb: Union[list, tuple, np.ndarray, torch.Tensor],
148
+ voxel_size: Union[float, list, tuple, np.ndarray, torch.Tensor] = None,
149
+ grid_size: Union[int, list, tuple, np.ndarray, torch.Tensor] = None,
150
+ train: bool = False,
151
+ ):
152
+ """
153
+ Extract mesh from sparse voxel structures using flexible dual grid.
154
+
155
+ Args:
156
+ coords (torch.Tensor): The coordinates of the voxels.
157
+ dual_vertices (torch.Tensor): The dual vertices.
158
+ intersected_flag (torch.Tensor): The intersected flag.
159
+ split_weight (torch.Tensor): The split weight of each dual quad. If None, the algorithm
160
+ will split based on minimum angle.
161
+ aabb (list, tuple, np.ndarray, torch.Tensor): The axis-aligned bounding box of the mesh.
162
+ voxel_size (float, list, tuple, np.ndarray, torch.Tensor): The size of each voxel.
163
+ grid_size (int, list, tuple, np.ndarray, torch.Tensor): The size of the grid.
164
+ NOTE: One of voxel_size and grid_size must be provided.
165
+ train (bool): Whether to use training mode.
166
+
167
+ Returns:
168
+ vertices (torch.Tensor): The vertices of the mesh.
169
+ faces (torch.Tensor): The faces of the mesh.
170
+ """
171
+ # Static variables
172
+ if not hasattr(flexible_dual_grid_to_mesh, "edge_neighbor_voxel_offset"):
173
+ flexible_dual_grid_to_mesh.edge_neighbor_voxel_offset = torch.tensor([
174
+ [[0, 0, 0], [0, 0, 1], [0, 1, 1], [0, 1, 0]], # x-axis
175
+ [[0, 0, 0], [1, 0, 0], [1, 0, 1], [0, 0, 1]], # y-axis
176
+ [[0, 0, 0], [0, 1, 0], [1, 1, 0], [1, 0, 0]], # z-axis
177
+ ], dtype=torch.int, device=coords.device).unsqueeze(0)
178
+ if not hasattr(flexible_dual_grid_to_mesh, "quad_split_1"):
179
+ flexible_dual_grid_to_mesh.quad_split_1 = torch.tensor([0, 1, 2, 0, 2, 3], dtype=torch.long, device=coords.device, requires_grad=False)
180
+ if not hasattr(flexible_dual_grid_to_mesh, "quad_split_2"):
181
+ flexible_dual_grid_to_mesh.quad_split_2 = torch.tensor([0, 1, 3, 3, 1, 2], dtype=torch.long, device=coords.device, requires_grad=False)
182
+ if not hasattr(flexible_dual_grid_to_mesh, "quad_split_train"):
183
+ flexible_dual_grid_to_mesh.quad_split_train = torch.tensor([0, 1, 4, 1, 2, 4, 2, 3, 4, 3, 0, 4], dtype=torch.long, device=coords.device, requires_grad=False)
184
+
185
+ # AABB
186
+ if isinstance(aabb, (list, tuple)):
187
+ aabb = np.array(aabb)
188
+ if isinstance(aabb, np.ndarray):
189
+ aabb = torch.tensor(aabb, dtype=torch.float32, device=coords.device)
190
+ assert isinstance(aabb, torch.Tensor), f"aabb must be a list, tuple, np.ndarray, or torch.Tensor, but got {type(aabb)}"
191
+ assert aabb.dim() == 2, f"aabb must be a 2D tensor, but got {aabb.shape}"
192
+ assert aabb.size(0) == 2, f"aabb must have 2 rows, but got {aabb.size(0)}"
193
+ assert aabb.size(1) == 3, f"aabb must have 3 columns, but got {aabb.size(1)}"
194
+
195
+ # Voxel size
196
+ if voxel_size is not None:
197
+ if isinstance(voxel_size, float):
198
+ voxel_size = [voxel_size, voxel_size, voxel_size]
199
+ if isinstance(voxel_size, (list, tuple)):
200
+ voxel_size = np.array(voxel_size)
201
+ if isinstance(voxel_size, np.ndarray):
202
+ voxel_size = torch.tensor(voxel_size, dtype=torch.float32, device=coords.device)
203
+ grid_size = ((aabb[1] - aabb[0]) / voxel_size).round().int()
204
+ else:
205
+ assert grid_size is not None, "Either voxel_size or grid_size must be provided"
206
+ if isinstance(grid_size, int):
207
+ grid_size = [grid_size, grid_size, grid_size]
208
+ if isinstance(grid_size, (list, tuple)):
209
+ grid_size = np.array(grid_size)
210
+ if isinstance(grid_size, np.ndarray):
211
+ grid_size = torch.tensor(grid_size, dtype=torch.int32, device=coords.device)
212
+ voxel_size = (aabb[1] - aabb[0]) / grid_size
213
+ assert isinstance(voxel_size, torch.Tensor), f"voxel_size must be a float, list, tuple, np.ndarray, or torch.Tensor, but got {type(voxel_size)}"
214
+ assert voxel_size.dim() == 1, f"voxel_size must be a 1D tensor, but got {voxel_size.shape}"
215
+ assert voxel_size.size(0) == 3, f"voxel_size must have 3 elements, but got {voxel_size.size(0)}"
216
+ assert isinstance(grid_size, torch.Tensor), f"grid_size must be an int, list, tuple, np.ndarray, or torch.Tensor, but got {type(grid_size)}"
217
+ assert grid_size.dim() == 1, f"grid_size must be a 1D tensor, but got {grid_size.shape}"
218
+ assert grid_size.size(0) == 3, f"grid_size must have 3 elements, but got {grid_size.size(0)}"
219
+
220
+ # Extract mesh
221
+ N = dual_vertices.shape[0]
222
+ mesh_vertices = (coords.float() + dual_vertices) / (2 * N) - 0.5
223
+
224
+ # Store active voxels into hashmap
225
+ hashmap = _init_hashmap(grid_size, 2 * N, device=coords.device)
226
+ _C.hashmap_insert_3d_idx_as_val_cuda(*hashmap, torch.cat([torch.zeros_like(coords[:, :1]), coords], dim=-1), *grid_size.tolist())
227
+
228
+ # Find connected voxels
229
+ edge_neighbor_voxel = coords.reshape(N, 1, 1, 3) + flexible_dual_grid_to_mesh.edge_neighbor_voxel_offset # (N, 3, 4, 3)
230
+ connected_voxel = edge_neighbor_voxel[intersected_flag] # (M, 4, 3)
231
+ M = connected_voxel.shape[0]
232
+ connected_voxel_hash_key = torch.cat([
233
+ torch.zeros((M * 4, 1), dtype=torch.int, device=coords.device),
234
+ connected_voxel.reshape(-1, 3)
235
+ ], dim=1)
236
+ connected_voxel_indices = _C.hashmap_lookup_3d_cuda(*hashmap, connected_voxel_hash_key, *grid_size.tolist()).reshape(M, 4).int()
237
+ connected_voxel_valid = (connected_voxel_indices != 0xffffffff).all(dim=1)
238
+ quad_indices = connected_voxel_indices[connected_voxel_valid].int() # (L, 4)
239
+ L = quad_indices.shape[0]
240
+
241
+ # Construct triangles
242
+ if not train:
243
+ mesh_vertices = (coords.float() + dual_vertices) * voxel_size + aabb[0].reshape(1, 3)
244
+ if split_weight is None:
245
+ # if split 1
246
+ atempt_triangles_0 = quad_indices[:, flexible_dual_grid_to_mesh.quad_split_1]
247
+ normals0 = torch.cross(mesh_vertices[atempt_triangles_0[:, 1]] - mesh_vertices[atempt_triangles_0[:, 0]], mesh_vertices[atempt_triangles_0[:, 2]] - mesh_vertices[atempt_triangles_0[:, 0]])
248
+ normals1 = torch.cross(mesh_vertices[atempt_triangles_0[:, 2]] - mesh_vertices[atempt_triangles_0[:, 1]], mesh_vertices[atempt_triangles_0[:, 3]] - mesh_vertices[atempt_triangles_0[:, 1]])
249
+ align0 = (normals0 * normals1).sum(dim=1, keepdim=True).abs()
250
+ # if split 2
251
+ atempt_triangles_1 = quad_indices[:, flexible_dual_grid_to_mesh.quad_split_2]
252
+ normals0 = torch.cross(mesh_vertices[atempt_triangles_1[:, 1]] - mesh_vertices[atempt_triangles_1[:, 0]], mesh_vertices[atempt_triangles_1[:, 2]] - mesh_vertices[atempt_triangles_1[:, 0]])
253
+ normals1 = torch.cross(mesh_vertices[atempt_triangles_1[:, 2]] - mesh_vertices[atempt_triangles_1[:, 1]], mesh_vertices[atempt_triangles_1[:, 3]] - mesh_vertices[atempt_triangles_1[:, 1]])
254
+ align1 = (normals0 * normals1).sum(dim=1, keepdim=True).abs()
255
+ # select split
256
+ mesh_triangles = torch.where(align0 > align1, atempt_triangles_0, atempt_triangles_1).reshape(-1, 3)
257
+ else:
258
+ split_weight_ws = split_weight[quad_indices]
259
+ split_weight_ws_02 = split_weight_ws[:, 0] * split_weight_ws[:, 2]
260
+ split_weight_ws_13 = split_weight_ws[:, 1] * split_weight_ws[:, 3]
261
+ mesh_triangles = torch.where(
262
+ split_weight_ws_02 > split_weight_ws_13,
263
+ quad_indices[:, flexible_dual_grid_to_mesh.quad_split_1],
264
+ quad_indices[:, flexible_dual_grid_to_mesh.quad_split_2]
265
+ ).reshape(-1, 3)
266
+ else:
267
+ assert split_weight is not None, "split_weight must be provided in training mode"
268
+ mesh_vertices = (coords.float() + dual_vertices) * voxel_size + aabb[0].reshape(1, 3)
269
+ quad_vs = mesh_vertices[quad_indices]
270
+ mean_v02 = (quad_vs[:, 0] + quad_vs[:, 2]) / 2
271
+ mean_v13 = (quad_vs[:, 1] + quad_vs[:, 3]) / 2
272
+ split_weight_ws = split_weight[quad_indices]
273
+ split_weight_ws_02 = split_weight_ws[:, 0] * split_weight_ws[:, 2]
274
+ split_weight_ws_13 = split_weight_ws[:, 1] * split_weight_ws[:, 3]
275
+ mid_vertices = (
276
+ split_weight_ws_02 * mean_v02 +
277
+ split_weight_ws_13 * mean_v13
278
+ ) / (split_weight_ws_02 + split_weight_ws_13)
279
+ mesh_vertices = torch.cat([mesh_vertices, mid_vertices], dim=0)
280
+ quad_indices = torch.cat([quad_indices, torch.arange(N, N + L, device='cuda').unsqueeze(1)], dim=1)
281
+ mesh_triangles = quad_indices[:, flexible_dual_grid_to_mesh.quad_split_train].reshape(-1, 3)
282
+
283
+ return mesh_vertices, mesh_triangles
o-voxel/build/lib.win-amd64-cpython-311/o_voxel/convert/volumetic_attr.py ADDED
@@ -0,0 +1,583 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import *
2
+ import io
3
+ from PIL import Image
4
+ import torch
5
+ import numpy as np
6
+ from tqdm import tqdm
7
+ import trimesh
8
+ import trimesh.visual
9
+
10
+ from .. import _C
11
+
12
+ __all__ = [
13
+ "textured_mesh_to_volumetric_attr",
14
+ "blender_dump_to_volumetric_attr"
15
+ ]
16
+
17
+
18
+ ALPHA_MODE_ENUM = {
19
+ "OPAQUE": 0,
20
+ "MASK": 1,
21
+ "BLEND": 2,
22
+ }
23
+
24
+
25
+ def is_power_of_two(n: int) -> bool:
26
+ return n > 0 and (n & (n - 1)) == 0
27
+
28
+
29
+ def nearest_power_of_two(n: int) -> int:
30
+ if n < 1:
31
+ raise ValueError("n must be >= 1")
32
+ if is_power_of_two(n):
33
+ return n
34
+ lower = 2 ** (n.bit_length() - 1)
35
+ upper = 2 ** n.bit_length()
36
+ if n - lower < upper - n:
37
+ return lower
38
+ else:
39
+ return upper
40
+
41
+
42
+ def textured_mesh_to_volumetric_attr(
43
+ mesh: Union[trimesh.Scene, trimesh.Trimesh, str],
44
+ voxel_size: Union[float, list, tuple, np.ndarray, torch.Tensor] = None,
45
+ grid_size: Union[int, list, tuple, np.ndarray, torch.Tensor] = None,
46
+ aabb: Union[list, tuple, np.ndarray, torch.Tensor] = None,
47
+ mip_level_offset: float = 0.0,
48
+ verbose: bool = False,
49
+ timing: bool = False,
50
+ ) -> Union[torch.Tensor, Dict[str, torch.Tensor]]:
51
+ """
52
+ Voxelize a mesh into a sparse voxel grid with PBR properties.
53
+
54
+ Args:
55
+ mesh (trimesh.Scene, trimesh.Trimesh, str): The input mesh.
56
+ If a string is provided, it will be loaded as a mesh using trimesh.load().
57
+ voxel_size (float, list, tuple, np.ndarray, torch.Tensor): The size of each voxel.
58
+ grid_size (int, list, tuple, np.ndarray, torch.Tensor): The size of the grid.
59
+ NOTE: One of voxel_size and grid_size must be provided.
60
+ aabb (list, tuple, np.ndarray, torch.Tensor): The axis-aligned bounding box of the mesh.
61
+ If not provided, it will be computed automatically.
62
+ tile_size (int): The size of the tiles used for each individual voxelization.
63
+ mip_level_offset (float): The mip level offset for texture mip level selection.
64
+ verbose (bool): Whether to print the settings.
65
+ timing (bool): Whether to print the timing information.
66
+
67
+ Returns:
68
+ torch.Tensor: The indices of the voxels that are occupied by the mesh.
69
+ Dict[str, torch.Tensor]: A dictionary containing the following keys:
70
+ - "base_color": The base color of the occupied voxels.
71
+ - "metallic": The metallic value of the occupied voxels.
72
+ - "roughness": The roughness value of the occupied voxels.
73
+ - "emissive": The emissive value of the occupied voxels.
74
+ - "alpha": The alpha value of the occupied voxels.
75
+ - "normal": The normal of the occupied voxels.
76
+ """
77
+
78
+ # Load mesh
79
+ if isinstance(mesh, str):
80
+ mesh = trimesh.load(mesh)
81
+ if isinstance(mesh, trimesh.Scene):
82
+ groups = mesh.dump()
83
+ if isinstance(mesh, trimesh.Trimesh):
84
+ groups = [mesh]
85
+ scene = trimesh.Scene(groups)
86
+
87
+ # Voxelize settings
88
+ assert voxel_size is not None or grid_size is not None, "Either voxel_size or grid_size must be provided"
89
+
90
+ if voxel_size is not None:
91
+ if isinstance(voxel_size, float):
92
+ voxel_size = [voxel_size, voxel_size, voxel_size]
93
+ if isinstance(voxel_size, (list, tuple)):
94
+ voxel_size = np.array(voxel_size)
95
+ if isinstance(voxel_size, np.ndarray):
96
+ voxel_size = torch.tensor(voxel_size, dtype=torch.float32)
97
+ assert isinstance(voxel_size, torch.Tensor), f"voxel_size must be a float, list, tuple, np.ndarray, or torch.Tensor, but got {type(voxel_size)}"
98
+ assert voxel_size.dim() == 1, f"voxel_size must be a 1D tensor, but got {voxel_size.shape}"
99
+ assert voxel_size.size(0) == 3, f"voxel_size must have 3 elements, but got {voxel_size.size(0)}"
100
+
101
+ if grid_size is not None:
102
+ if isinstance(grid_size, int):
103
+ grid_size = [grid_size, grid_size, grid_size]
104
+ if isinstance(grid_size, (list, tuple)):
105
+ grid_size = np.array(grid_size)
106
+ if isinstance(grid_size, np.ndarray):
107
+ grid_size = torch.tensor(grid_size, dtype=torch.int32)
108
+ assert isinstance(grid_size, torch.Tensor), f"grid_size must be an int, list, tuple, np.ndarray, or torch.Tensor, but got {type(grid_size)}"
109
+ assert grid_size.dim() == 1, f"grid_size must be a 1D tensor, but got {grid_size.shape}"
110
+ assert grid_size.size(0) == 3, f"grid_size must have 3 elements, but got {grid_size.size(0)}"
111
+
112
+ if aabb is not None:
113
+ if isinstance(aabb, (list, tuple)):
114
+ aabb = np.array(aabb)
115
+ if isinstance(aabb, np.ndarray):
116
+ aabb = torch.tensor(aabb, dtype=torch.float32)
117
+ assert isinstance(aabb, torch.Tensor), f"aabb must be a list, tuple, np.ndarray, or torch.Tensor, but got {type(aabb)}"
118
+ assert aabb.dim() == 2, f"aabb must be a 2D tensor, but got {aabb.shape}"
119
+ assert aabb.size(0) == 2, f"aabb must have 2 rows, but got {aabb.size(0)}"
120
+ assert aabb.size(1) == 3, f"aabb must have 3 columns, but got {aabb.size(1)}"
121
+
122
+ # Auto adjust aabb
123
+ if aabb is None:
124
+ aabb = scene.bounds
125
+ min_xyz = aabb[0]
126
+ max_xyz = aabb[1]
127
+
128
+ if voxel_size is not None:
129
+ padding = torch.ceil((max_xyz - min_xyz) / voxel_size) * voxel_size - (max_xyz - min_xyz)
130
+ min_xyz -= padding * 0.5
131
+ max_xyz += padding * 0.5
132
+ if grid_size is not None:
133
+ padding = (max_xyz - min_xyz) / (grid_size - 1)
134
+ min_xyz -= padding * 0.5
135
+ max_xyz += padding * 0.5
136
+
137
+ aabb = torch.stack([min_xyz, max_xyz], dim=0).float()
138
+
139
+ # Fill voxel size or grid size
140
+ if voxel_size is None:
141
+ voxel_size = (aabb[1] - aabb[0]) / grid_size
142
+ if grid_size is None:
143
+ grid_size = ((aabb[1] - aabb[0]) / voxel_size).round().int()
144
+
145
+ grid_range = torch.stack([torch.zeros_like(grid_size), grid_size], dim=0).int()
146
+
147
+ # Print settings
148
+ if verbose:
149
+ print(f"Voxelize settings:")
150
+ print(f" Voxel size: {voxel_size}")
151
+ print(f" Grid size: {grid_size}")
152
+ print(f" AABB: {aabb}")
153
+
154
+ # Load Scene
155
+ scene_buffers = {
156
+ 'triangles': [],
157
+ 'normals': [],
158
+ 'uvs': [],
159
+ 'material_ids': [],
160
+ 'base_color_factor': [],
161
+ 'base_color_texture': [],
162
+ 'metallic_factor': [],
163
+ 'metallic_texture': [],
164
+ 'roughness_factor': [],
165
+ 'roughness_texture': [],
166
+ 'emissive_factor': [],
167
+ 'emissive_texture': [],
168
+ 'alpha_mode': [],
169
+ 'alpha_cutoff': [],
170
+ 'alpha_factor': [],
171
+ 'alpha_texture': [],
172
+ 'normal_texture': [],
173
+ }
174
+ for sid, (name, g) in tqdm(enumerate(scene.geometry.items()), total=len(scene.geometry), desc="Loading Scene", disable=not verbose):
175
+ if verbose:
176
+ print(f"Geometry: {name}")
177
+ print(f" Visual: {g.visual}")
178
+ print(f" Triangles: {g.triangles.shape[0]}")
179
+ print(f" Vertices: {g.vertices.shape[0]}")
180
+ print(f" Normals: {g.vertex_normals.shape[0]}")
181
+ if g.visual.material.baseColorFactor is not None:
182
+ print(f" Base color factor: {g.visual.material.baseColorFactor}")
183
+ if g.visual.material.baseColorTexture is not None:
184
+ print(f" Base color texture: {g.visual.material.baseColorTexture.size} {g.visual.material.baseColorTexture.mode}")
185
+ if g.visual.material.metallicFactor is not None:
186
+ print(f" Metallic factor: {g.visual.material.metallicFactor}")
187
+ if g.visual.material.roughnessFactor is not None:
188
+ print(f" Roughness factor: {g.visual.material.roughnessFactor}")
189
+ if g.visual.material.metallicRoughnessTexture is not None:
190
+ print(f" Metallic roughness texture: {g.visual.material.metallicRoughnessTexture.size} {g.visual.material.metallicRoughnessTexture.mode}")
191
+ if g.visual.material.emissiveFactor is not None:
192
+ print(f" Emissive factor: {g.visual.material.emissiveFactor}")
193
+ if g.visual.material.emissiveTexture is not None:
194
+ print(f" Emissive texture: {g.visual.material.emissiveTexture.size} {g.visual.material.emissiveTexture.mode}")
195
+ if g.visual.material.alphaMode is not None:
196
+ print(f" Alpha mode: {g.visual.material.alphaMode}")
197
+ if g.visual.material.alphaCutoff is not None:
198
+ print(f" Alpha cutoff: {g.visual.material.alphaCutoff}")
199
+ if g.visual.material.normalTexture is not None:
200
+ print(f" Normal texture: {g.visual.material.normalTexture.size} {g.visual.material.normalTexture.mode}")
201
+
202
+ assert isinstance(g, trimesh.Trimesh), f"Only trimesh.Trimesh is supported, but got {type(g)}"
203
+ assert isinstance(g.visual, trimesh.visual.TextureVisuals), f"Only trimesh.visual.TextureVisuals is supported, but got {type(g.visual)}"
204
+ assert isinstance(g.visual.material, trimesh.visual.material.PBRMaterial), f"Only trimesh.visual.material.PBRMaterial is supported, but got {type(g.visual.material)}"
205
+ triangles = torch.tensor(g.triangles, dtype=torch.float32) - aabb[0].reshape(1, 1, 3) # [N, 3, 3]
206
+ normals = torch.tensor(g.vertex_normals[g.faces], dtype=torch.float32) # [N, 3, 3]
207
+ uvs = torch.tensor(g.visual.uv[g.faces], dtype=torch.float32) if g.visual.uv is not None \
208
+ else torch.zeros(g.triangles.shape[0], 3, 2, dtype=torch.float32) # [N, 3, 2]
209
+ baseColorFactor = torch.tensor(g.visual.material.baseColorFactor / 255, dtype=torch.float32) if g.visual.material.baseColorFactor is not None \
210
+ else torch.ones(3, dtype=torch.float32) # [3]
211
+ baseColorTexture = torch.tensor(np.array(g.visual.material.baseColorTexture.convert('RGBA'))[..., :3], dtype=torch.uint8) if g.visual.material.baseColorTexture is not None \
212
+ else torch.tensor([]) # [H, W, 3]
213
+ metallicFactor = g.visual.material.metallicFactor if g.visual.material.metallicFactor is not None else 1.0
214
+ metallicTexture = torch.tensor(np.array(g.visual.material.metallicRoughnessTexture.convert('RGB'))[..., 2], dtype=torch.uint8) if g.visual.material.metallicRoughnessTexture is not None \
215
+ else torch.tensor([]) # [H, W]
216
+ roughnessFactor = g.visual.material.roughnessFactor if g.visual.material.roughnessFactor is not None else 1.0
217
+ roughnessTexture = torch.tensor(np.array(g.visual.material.metallicRoughnessTexture.convert('RGB'))[..., 1], dtype=torch.uint8) if g.visual.material.metallicRoughnessTexture is not None \
218
+ else torch.tensor([]) # [H, W]
219
+ emissiveFactor = torch.tensor(g.visual.material.emissiveFactor, dtype=torch.float32) if g.visual.material.emissiveFactor is not None \
220
+ else torch.zeros(3, dtype=torch.float32) # [3]
221
+ emissiveTexture = torch.tensor(np.array(g.visual.material.emissiveTexture.convert('RGB'))[..., :3], dtype=torch.uint8) if g.visual.material.emissiveTexture is not None \
222
+ else torch.tensor([]) # [H, W, 3]
223
+ alphaMode = ALPHA_MODE_ENUM[g.visual.material.alphaMode] if g.visual.material.alphaMode in ALPHA_MODE_ENUM else 0
224
+ alphaCutoff = g.visual.material.alphaCutoff if g.visual.material.alphaCutoff is not None else 0.5
225
+ alphaFactor = g.visual.material.baseColorFactor[3] / 255 if g.visual.material.baseColorFactor is not None else 1.0
226
+ alphaTexture = torch.tensor(np.array(g.visual.material.baseColorTexture.convert('RGBA'))[..., 3], dtype=torch.uint8) if g.visual.material.baseColorTexture is not None and alphaMode != 0 \
227
+ else torch.tensor([]) # [H, W]
228
+ normalTexture = torch.tensor(np.array(g.visual.material.normalTexture.convert('RGB'))[..., :3], dtype=torch.uint8) if g.visual.material.normalTexture is not None \
229
+ else torch.tensor([]) # [H, W, 3]
230
+
231
+ scene_buffers['triangles'].append(triangles)
232
+ scene_buffers['normals'].append(normals)
233
+ scene_buffers['uvs'].append(uvs)
234
+ scene_buffers['material_ids'].append(torch.full((triangles.shape[0],), sid, dtype=torch.int32))
235
+ scene_buffers['base_color_factor'].append(baseColorFactor)
236
+ scene_buffers['base_color_texture'].append(baseColorTexture)
237
+ scene_buffers['metallic_factor'].append(metallicFactor)
238
+ scene_buffers['metallic_texture'].append(metallicTexture)
239
+ scene_buffers['roughness_factor'].append(roughnessFactor)
240
+ scene_buffers['roughness_texture'].append(roughnessTexture)
241
+ scene_buffers['emissive_factor'].append(emissiveFactor)
242
+ scene_buffers['emissive_texture'].append(emissiveTexture)
243
+ scene_buffers['alpha_mode'].append(alphaMode)
244
+ scene_buffers['alpha_cutoff'].append(alphaCutoff)
245
+ scene_buffers['alpha_factor'].append(alphaFactor)
246
+ scene_buffers['alpha_texture'].append(alphaTexture)
247
+ scene_buffers['normal_texture'].append(normalTexture)
248
+
249
+ scene_buffers['triangles'] = torch.cat(scene_buffers['triangles'], dim=0) # [N, 3, 3]
250
+ scene_buffers['normals'] = torch.cat(scene_buffers['normals'], dim=0) # [N, 3, 3]
251
+ scene_buffers['uvs'] = torch.cat(scene_buffers['uvs'], dim=0) # [N, 3, 2]
252
+ scene_buffers['material_ids'] = torch.cat(scene_buffers['material_ids'], dim=0) # [N]
253
+
254
+ # Voxelize
255
+ out_tuple = _C.textured_mesh_to_volumetric_attr_cpu(
256
+ voxel_size,
257
+ grid_range,
258
+ scene_buffers["triangles"],
259
+ scene_buffers["normals"],
260
+ scene_buffers["uvs"],
261
+ scene_buffers["material_ids"],
262
+ scene_buffers["base_color_factor"],
263
+ scene_buffers["base_color_texture"],
264
+ [1] * len(scene_buffers["base_color_texture"]),
265
+ [0] * len(scene_buffers["base_color_texture"]),
266
+ scene_buffers["metallic_factor"],
267
+ scene_buffers["metallic_texture"],
268
+ [1] * len(scene_buffers["metallic_texture"]),
269
+ [0] * len(scene_buffers["metallic_texture"]),
270
+ scene_buffers["roughness_factor"],
271
+ scene_buffers["roughness_texture"],
272
+ [1] * len(scene_buffers["roughness_texture"]),
273
+ [0] * len(scene_buffers["roughness_texture"]),
274
+ scene_buffers["emissive_factor"],
275
+ scene_buffers["emissive_texture"],
276
+ [1] * len(scene_buffers["emissive_texture"]),
277
+ [0] * len(scene_buffers["emissive_texture"]),
278
+ scene_buffers["alpha_mode"],
279
+ scene_buffers["alpha_cutoff"],
280
+ scene_buffers["alpha_factor"],
281
+ scene_buffers["alpha_texture"],
282
+ [1] * len(scene_buffers["alpha_texture"]),
283
+ [0] * len(scene_buffers["alpha_texture"]),
284
+ scene_buffers["normal_texture"],
285
+ [1] * len(scene_buffers["normal_texture"]),
286
+ [0] * len(scene_buffers["normal_texture"]),
287
+ mip_level_offset,
288
+ timing,
289
+ )
290
+
291
+ # Post process
292
+ coord = out_tuple[0]
293
+ attr = {
294
+ "base_color": torch.clamp(out_tuple[1] * 255, 0, 255).byte().reshape(-1, 3),
295
+ "metallic": torch.clamp(out_tuple[2] * 255, 0, 255).byte().reshape(-1, 1),
296
+ "roughness": torch.clamp(out_tuple[3] * 255, 0, 255).byte().reshape(-1, 1),
297
+ "emissive": torch.clamp(out_tuple[4] * 255, 0, 255).byte().reshape(-1, 3),
298
+ "alpha": torch.clamp(out_tuple[5] * 255, 0, 255).byte().reshape(-1, 1),
299
+ "normal": torch.clamp((out_tuple[6] * 0.5 + 0.5) * 255, 0, 255).byte().reshape(-1, 3),
300
+ }
301
+
302
+ return coord, attr
303
+
304
+
305
+ def blender_dump_to_volumetric_attr(
306
+ dump: Dict[str, Any],
307
+ voxel_size: Union[float, list, tuple, np.ndarray, torch.Tensor] = None,
308
+ grid_size: Union[int, list, tuple, np.ndarray, torch.Tensor] = None,
309
+ aabb: Union[list, tuple, np.ndarray, torch.Tensor] = None,
310
+ mip_level_offset: float = 0.0,
311
+ verbose: bool = False,
312
+ timing: bool = False,
313
+ ) -> Union[torch.Tensor, Dict[str, torch.Tensor]]:
314
+ """
315
+ Voxelize a mesh into a sparse voxel grid with PBR properties.
316
+
317
+ Args:
318
+ dump (Dict[str, Any]): Dumped data from a blender scene.
319
+ voxel_size (float, list, tuple, np.ndarray, torch.Tensor): The size of each voxel.
320
+ grid_size (int, list, tuple, np.ndarray, torch.Tensor): The size of the grid.
321
+ NOTE: One of voxel_size and grid_size must be provided.
322
+ aabb (list, tuple, np.ndarray, torch.Tensor): The axis-aligned bounding box of the mesh.
323
+ If not provided, it will be computed automatically.
324
+ mip_level_offset (float): The mip level offset for texture mip level selection.
325
+ verbose (bool): Whether to print the settings.
326
+ timing (bool): Whether to print the timing information.
327
+
328
+ Returns:
329
+ torch.Tensor: The indices of the voxels that are occupied by the mesh.
330
+ Dict[str, torch.Tensor]: A dictionary containing the following keys:
331
+ - "base_color": The base color of the occupied voxels.
332
+ - "metallic": The metallic value of the occupied voxels.
333
+ - "roughness": The roughness value of the occupied voxels.
334
+ - "emissive": The emissive value of the occupied voxels.
335
+ - "alpha": The alpha value of the occupied voxels.
336
+ - "normal": The normal of the occupied voxels.
337
+ """
338
+ # Voxelize settings
339
+ assert voxel_size is not None or grid_size is not None, "Either voxel_size or grid_size must be provided"
340
+
341
+ if voxel_size is not None:
342
+ if isinstance(voxel_size, float):
343
+ voxel_size = [voxel_size, voxel_size, voxel_size]
344
+ if isinstance(voxel_size, (list, tuple)):
345
+ voxel_size = np.array(voxel_size)
346
+ if isinstance(voxel_size, np.ndarray):
347
+ voxel_size = torch.tensor(voxel_size, dtype=torch.float32)
348
+ assert isinstance(voxel_size, torch.Tensor), f"voxel_size must be a float, list, tuple, np.ndarray, or torch.Tensor, but got {type(voxel_size)}"
349
+ assert voxel_size.dim() == 1, f"voxel_size must be a 1D tensor, but got {voxel_size.shape}"
350
+ assert voxel_size.size(0) == 3, f"voxel_size must have 3 elements, but got {voxel_size.size(0)}"
351
+
352
+ if grid_size is not None:
353
+ if isinstance(grid_size, int):
354
+ grid_size = [grid_size, grid_size, grid_size]
355
+ if isinstance(grid_size, (list, tuple)):
356
+ grid_size = np.array(grid_size)
357
+ if isinstance(grid_size, np.ndarray):
358
+ grid_size = torch.tensor(grid_size, dtype=torch.int32)
359
+ assert isinstance(grid_size, torch.Tensor), f"grid_size must be an int, list, tuple, np.ndarray, or torch.Tensor, but got {type(grid_size)}"
360
+ assert grid_size.dim() == 1, f"grid_size must be a 1D tensor, but got {grid_size.shape}"
361
+ assert grid_size.size(0) == 3, f"grid_size must have 3 elements, but got {grid_size.size(0)}"
362
+
363
+ if aabb is not None:
364
+ if isinstance(aabb, (list, tuple)):
365
+ aabb = np.array(aabb)
366
+ if isinstance(aabb, np.ndarray):
367
+ aabb = torch.tensor(aabb, dtype=torch.float32)
368
+ assert isinstance(aabb, torch.Tensor), f"aabb must be a list, tuple, np.ndarray, or torch.Tensor, but got {type(aabb)}"
369
+ assert aabb.dim() == 2, f"aabb must be a 2D tensor, but got {aabb.shape}"
370
+ assert aabb.size(0) == 2, f"aabb must have 2 rows, but got {aabb.size(0)}"
371
+ assert aabb.size(1) == 3, f"aabb must have 3 columns, but got {aabb.size(1)}"
372
+
373
+ # Auto adjust aabb
374
+ if aabb is None:
375
+ min_xyz = np.min([
376
+ object['vertices'].min(axis=0)
377
+ for object in dump['objects']
378
+ ], axis=0)
379
+ max_xyz = np.max([
380
+ object['vertices'].max(axis=0)
381
+ for object in dump['objects']
382
+ ], axis=0)
383
+
384
+ if voxel_size is not None:
385
+ padding = torch.ceil((max_xyz - min_xyz) / voxel_size) * voxel_size - (max_xyz - min_xyz)
386
+ min_xyz -= padding * 0.5
387
+ max_xyz += padding * 0.5
388
+ if grid_size is not None:
389
+ padding = (max_xyz - min_xyz) / (grid_size - 1)
390
+ min_xyz -= padding * 0.5
391
+ max_xyz += padding * 0.5
392
+
393
+ aabb = torch.stack([min_xyz, max_xyz], dim=0).float()
394
+
395
+ # Fill voxel size or grid size
396
+ if voxel_size is None:
397
+ voxel_size = (aabb[1] - aabb[0]) / grid_size
398
+ if grid_size is None:
399
+ grid_size = ((aabb[1] - aabb[0]) / voxel_size).round().int()
400
+
401
+ grid_range = torch.stack([torch.zeros_like(grid_size), grid_size], dim=0).int()
402
+
403
+ # Print settings
404
+ if verbose:
405
+ print(f"Voxelize settings:")
406
+ print(f" Voxel size: {voxel_size}")
407
+ print(f" Grid size: {grid_size}")
408
+ print(f" AABB: {aabb}")
409
+
410
+ # Load Scene
411
+ scene_buffers = {
412
+ 'triangles': [],
413
+ 'normals': [],
414
+ 'uvs': [],
415
+ 'material_ids': [],
416
+ 'base_color_factor': [],
417
+ 'base_color_texture': [],
418
+ 'base_color_texture_filter': [],
419
+ 'base_color_texture_wrap': [],
420
+ 'metallic_factor': [],
421
+ 'metallic_texture': [],
422
+ 'metallic_texture_filter': [],
423
+ 'metallic_texture_wrap': [],
424
+ 'roughness_factor': [],
425
+ 'roughness_texture': [],
426
+ 'roughness_texture_filter': [],
427
+ 'roughness_texture_wrap': [],
428
+ 'alpha_mode': [],
429
+ 'alpha_cutoff': [],
430
+ 'alpha_factor': [],
431
+ 'alpha_texture': [],
432
+ 'alpha_texture_filter': [],
433
+ 'alpha_texture_wrap': [],
434
+ }
435
+
436
+ def load_texture(pack):
437
+ png_bytes = pack['image']
438
+ image = Image.open(io.BytesIO(png_bytes))
439
+ if image.width != image.height or not is_power_of_two(image.width):
440
+ size = nearest_power_of_two(max(image.width, image.height))
441
+ image = image.resize((size, size), Image.LANCZOS)
442
+ texture = torch.tensor(np.array(image), dtype=torch.uint8)
443
+ filter_mode = {
444
+ 'Linear': 1,
445
+ 'Closest': 0,
446
+ 'Cubic': 1,
447
+ 'Smart': 1,
448
+ }[pack['interpolation']]
449
+ wrap_mode = {
450
+ 'REPEAT': 0,
451
+ 'EXTEND': 1,
452
+ 'CLIP': 1,
453
+ 'MIRROR': 2,
454
+ }[pack['extension']]
455
+ return texture, filter_mode, wrap_mode
456
+
457
+ for material in dump['materials']:
458
+ baseColorFactor = torch.tensor(material['baseColorFactor'][:3], dtype=torch.float32)
459
+ if material['baseColorTexture'] is not None:
460
+ baseColorTexture, baseColorTextureFilter, baseColorTextureWrap = \
461
+ load_texture(material['baseColorTexture'])
462
+ assert baseColorTexture.shape[2] == 3, f"Base color texture must have 3 channels, but got {baseColorTexture.shape[2]}"
463
+ else:
464
+ baseColorTexture = torch.tensor([])
465
+ baseColorTextureFilter = 0
466
+ baseColorTextureWrap = 0
467
+ scene_buffers['base_color_factor'].append(baseColorFactor)
468
+ scene_buffers['base_color_texture'].append(baseColorTexture)
469
+ scene_buffers['base_color_texture_filter'].append(baseColorTextureFilter)
470
+ scene_buffers['base_color_texture_wrap'].append(baseColorTextureWrap)
471
+
472
+ metallicFactor = material['metallicFactor']
473
+ if material['metallicTexture'] is not None:
474
+ metallicTexture, metallicTextureFilter, metallicTextureWrap = \
475
+ load_texture(material['metallicTexture'])
476
+ assert metallicTexture.dim() == 2, f"Metallic roughness texture must have 2 dimensions, but got {metallicTexture.dim()}"
477
+ else:
478
+ metallicTexture = torch.tensor([])
479
+ metallicTextureFilter = 0
480
+ metallicTextureWrap = 0
481
+ scene_buffers['metallic_factor'].append(metallicFactor)
482
+ scene_buffers['metallic_texture'].append(metallicTexture)
483
+ scene_buffers['metallic_texture_filter'].append(metallicTextureFilter)
484
+ scene_buffers['metallic_texture_wrap'].append(metallicTextureWrap)
485
+
486
+ roughnessFactor = material['roughnessFactor']
487
+ if material['roughnessTexture'] is not None:
488
+ roughnessTexture, roughnessTextureFilter, roughnessTextureWrap = \
489
+ load_texture(material['roughnessTexture'])
490
+ assert roughnessTexture.dim() == 2, f"Metallic roughness texture must have 2 dimensions, but got {roughnessTexture.dim()}"
491
+ else:
492
+ roughnessTexture = torch.tensor([])
493
+ roughnessTextureFilter = 0
494
+ roughnessTextureWrap = 0
495
+ scene_buffers['roughness_factor'].append(roughnessFactor)
496
+ scene_buffers['roughness_texture'].append(roughnessTexture)
497
+ scene_buffers['roughness_texture_filter'].append(roughnessTextureFilter)
498
+ scene_buffers['roughness_texture_wrap'].append(roughnessTextureWrap)
499
+
500
+ alphaMode = ALPHA_MODE_ENUM[material['alphaMode']]
501
+ alphaCutoff = material['alphaCutoff']
502
+ alphaFactor = material['alphaFactor']
503
+ if material['alphaTexture'] is not None:
504
+ alphaTexture, alphaTextureFilter, alphaTextureWrap = \
505
+ load_texture(material['alphaTexture'])
506
+ assert alphaTexture.dim() == 2, f"Alpha texture must have 2 dimensions, but got {alphaTexture.dim()}"
507
+ else:
508
+ alphaTexture = torch.tensor([])
509
+ alphaTextureFilter = 0
510
+ alphaTextureWrap = 0
511
+ scene_buffers['alpha_mode'].append(alphaMode)
512
+ scene_buffers['alpha_cutoff'].append(alphaCutoff)
513
+ scene_buffers['alpha_factor'].append(alphaFactor)
514
+ scene_buffers['alpha_texture'].append(alphaTexture)
515
+ scene_buffers['alpha_texture_filter'].append(alphaTextureFilter)
516
+ scene_buffers['alpha_texture_wrap'].append(alphaTextureWrap)
517
+
518
+ for object in dump['objects']:
519
+ triangles = torch.tensor(object['vertices'][object['faces']], dtype=torch.float32).reshape(-1, 3, 3) - aabb[0].reshape(1, 1, 3)
520
+ normails = torch.tensor(object['normals'], dtype=torch.float32)
521
+ uvs = torch.tensor(object['uvs'], dtype=torch.float32) if object['uvs'] is not None else torch.zeros(triangles.shape[0], 3, 2, dtype=torch.float32)
522
+ material_id = torch.tensor(object['mat_ids'], dtype=torch.int32)
523
+ scene_buffers['triangles'].append(triangles)
524
+ scene_buffers['normals'].append(normails)
525
+ scene_buffers['uvs'].append(uvs)
526
+ scene_buffers['material_ids'].append(material_id)
527
+
528
+ scene_buffers['triangles'] = torch.cat(scene_buffers['triangles'], dim=0) # [N, 3, 3]
529
+ scene_buffers['normals'] = torch.cat(scene_buffers['normals'], dim=0) # [N, 3, 3]
530
+ scene_buffers['uvs'] = torch.cat(scene_buffers['uvs'], dim=0) # [N, 3, 2]
531
+ scene_buffers['material_ids'] = torch.cat(scene_buffers['material_ids'], dim=0) # [N]
532
+
533
+ scene_buffers['uvs'][:, :, 1] = 1 - scene_buffers['uvs'][:, :, 1] # Flip v coordinate
534
+
535
+ # Voxelize
536
+ out_tuple = _C.textured_mesh_to_volumetric_attr_cpu(
537
+ voxel_size,
538
+ grid_range,
539
+ scene_buffers["triangles"],
540
+ scene_buffers["normals"],
541
+ scene_buffers["uvs"],
542
+ scene_buffers["material_ids"],
543
+ scene_buffers["base_color_factor"],
544
+ scene_buffers["base_color_texture"],
545
+ scene_buffers["base_color_texture_filter"],
546
+ scene_buffers["base_color_texture_wrap"],
547
+ scene_buffers["metallic_factor"],
548
+ scene_buffers["metallic_texture"],
549
+ scene_buffers["metallic_texture_filter"],
550
+ scene_buffers["metallic_texture_wrap"],
551
+ scene_buffers["roughness_factor"],
552
+ scene_buffers["roughness_texture"],
553
+ scene_buffers["roughness_texture_filter"],
554
+ scene_buffers["roughness_texture_wrap"],
555
+ [torch.zeros(3, dtype=torch.float32) for _ in range(len(scene_buffers["base_color_texture"]))],
556
+ [torch.tensor([]) for _ in range(len(scene_buffers["base_color_texture"]))],
557
+ [0] * len(scene_buffers["base_color_texture"]),
558
+ [0] * len(scene_buffers["base_color_texture"]),
559
+ scene_buffers["alpha_mode"],
560
+ scene_buffers["alpha_cutoff"],
561
+ scene_buffers["alpha_factor"],
562
+ scene_buffers["alpha_texture"],
563
+ scene_buffers["alpha_texture_filter"],
564
+ scene_buffers["alpha_texture_wrap"],
565
+ [torch.tensor([]) for _ in range(len(scene_buffers["base_color_texture"]))],
566
+ [0] * len(scene_buffers["base_color_texture"]),
567
+ [0] * len(scene_buffers["base_color_texture"]),
568
+ mip_level_offset,
569
+ timing,
570
+ )
571
+
572
+ # Post process
573
+ coord = out_tuple[0]
574
+ attr = {
575
+ "base_color": torch.clamp(out_tuple[1] * 255, 0, 255).byte().reshape(-1, 3),
576
+ "metallic": torch.clamp(out_tuple[2] * 255, 0, 255).byte().reshape(-1, 1),
577
+ "roughness": torch.clamp(out_tuple[3] * 255, 0, 255).byte().reshape(-1, 1),
578
+ "emissive": torch.clamp(out_tuple[4] * 255, 0, 255).byte().reshape(-1, 3),
579
+ "alpha": torch.clamp(out_tuple[5] * 255, 0, 255).byte().reshape(-1, 1),
580
+ "normal": torch.clamp((out_tuple[6] * 0.5 + 0.5) * 255, 0, 255).byte().reshape(-1, 3),
581
+ }
582
+
583
+ return coord, attr
o-voxel/build/lib.win-amd64-cpython-311/o_voxel/io/__init__.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, Union
2
+ import torch
3
+ from .ply import *
4
+ from .npz import *
5
+ from .vxz import *
6
+
7
+
8
+ def read(file_path: str) -> Union[torch.Tensor, Dict[str, torch.Tensor]]:
9
+ """
10
+ Read a file containing voxels.
11
+
12
+ Args:
13
+ file_path: Path to the file.
14
+
15
+ Returns:
16
+ torch.Tensor: the coordinates of the voxels.
17
+ Dict[str, torch.Tensor]: the attributes of the voxels.
18
+ """
19
+ if file_path.endswith('.npz'):
20
+ return read_npz(file_path)
21
+ elif file_path.endswith('.ply'):
22
+ return read_ply(file_path)
23
+ elif file_path.endswith('.vxz'):
24
+ return read_vxz(file_path)
25
+ else:
26
+ raise ValueError(f"Unsupported file type {file_path}")
27
+
28
+
29
+ def write(file_path: str, coord: torch.Tensor, attr: Dict[str, torch.Tensor], **kwargs):
30
+ """
31
+ Write a file containing voxels.
32
+
33
+ Args:
34
+ file_path: Path to the file.
35
+ coord: the coordinates of the voxels.
36
+ attr: the attributes of the voxels.
37
+ """
38
+ if file_path.endswith('.npz'):
39
+ write_npz(file_path, coord, attr, **kwargs)
40
+ elif file_path.endswith('.ply'):
41
+ write_ply(file_path, coord, attr, **kwargs)
42
+ elif file_path.endswith('.vxz'):
43
+ write_vxz(file_path, coord, attr, **kwargs)
44
+ else:
45
+ raise ValueError(f"Unsupported file type {file_path}")
o-voxel/build/lib.win-amd64-cpython-311/o_voxel/io/npz.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import *
2
+ import torch
3
+ import numpy as np
4
+
5
+
6
+ __all__ = [
7
+ "read_npz",
8
+ "write_npz",
9
+ ]
10
+
11
+
12
+ def read_npz(file) -> Union[torch.Tensor, Dict[str, torch.Tensor]]:
13
+ """
14
+ Read a NPZ file containing voxels.
15
+
16
+ Args:
17
+ file_path: Path or file object from which to read the NPZ file.
18
+
19
+ Returns:
20
+ torch.Tensor: the coordinates of the voxels.
21
+ Dict[str, torch.Tensor]: the attributes of the voxels.
22
+ """
23
+ data = np.load(file)
24
+ coord = torch.from_numpy(data['coord']).int()
25
+ attr = {k: torch.from_numpy(v) for k, v in data.items() if k!= 'coord'}
26
+ return coord, attr
27
+
28
+
29
+ def write_npz(file, coord: torch.Tensor, attr: Dict[str, torch.Tensor], compress=True):
30
+ """
31
+ Write a NPZ file containing voxels.
32
+
33
+ Args:
34
+ file_path: Path or file object to which to write the NPZ file.
35
+ coord: the coordinates of the voxels.
36
+ attr: the attributes of the voxels.
37
+ """
38
+ data = {'coord': coord.cpu().numpy().astype(np.uint16)}
39
+ data.update({k: v.cpu().numpy() for k, v in attr.items()})
40
+ if compress:
41
+ np.savez_compressed(file, **data)
42
+ else:
43
+ np.savez(file, **data)
o-voxel/build/lib.win-amd64-cpython-311/o_voxel/io/ply.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import *
2
+ import io
3
+ import torch
4
+ import numpy as np
5
+ import plyfile
6
+
7
+
8
+ __all__ = [
9
+ "read_ply",
10
+ "write_ply",
11
+ ]
12
+
13
+
14
+ DTYPE_MAP = {
15
+ torch.uint8: 'u1',
16
+ torch.uint16: 'u2',
17
+ torch.uint32: 'u4',
18
+ torch.int8: 'i1',
19
+ torch.int16: 'i2',
20
+ torch.int32: 'i4',
21
+ torch.float32: 'f4',
22
+ torch.float64: 'f8'
23
+ }
24
+
25
+
26
+ def read_ply(file) -> Union[torch.Tensor, Dict[str, torch.Tensor]]:
27
+ """
28
+ Read a PLY file containing voxels.
29
+
30
+ Args:
31
+ file: Path or file-like object of the PLY file.
32
+
33
+ Returns:
34
+ torch.Tensor: the coordinates of the voxels.
35
+ Dict[str, torch.Tensor]: the attributes of the voxels.
36
+ """
37
+ plydata = plyfile.PlyData.read(file)
38
+ xyz = np.stack([plydata.elements[0][k] for k in ['x', 'y', 'z']], axis=1)
39
+ coord = np.round(xyz).astype(int)
40
+ coord = torch.from_numpy(coord)
41
+
42
+ attr_keys = [k for k in plydata.elements[0].data.dtype.names if k not in ['x', 'y', 'z']]
43
+ attr_names = ['_'.join(k.split('_')[:-1]) for k in attr_keys]
44
+ attr_chs = [sum([1 for k in attr_keys if k.startswith(f'{name}_')]) for name in attr_names]
45
+
46
+ attr = {}
47
+ for i, name in enumerate(attr_names):
48
+ attr[name] = np.stack([plydata.elements[0][f'{name}_{j}'] for j in range(attr_chs[i])], axis=1)
49
+ attr = {k: torch.from_numpy(v) for k, v in attr.items()}
50
+
51
+ return coord, attr
52
+
53
+
54
+ def write_ply(file, coord: torch.Tensor, attr: Dict[str, torch.Tensor]):
55
+ """
56
+ Write a PLY file containing voxels.
57
+
58
+ Args:
59
+ file: Path or file-like object of the PLY file.
60
+ coord: the coordinates of the voxels.
61
+ attr: the attributes of the voxels.
62
+ """
63
+ dtypes = [('x', 'f4'), ('y', 'f4'), ('z', 'f4')]
64
+ for k, v in attr.items():
65
+ for j in range(v.shape[-1]):
66
+ assert v.dtype in DTYPE_MAP, f"Unsupported data type {v.dtype} for attribute {k}"
67
+ dtypes.append((f'{k}_{j}', DTYPE_MAP[v.dtype]))
68
+ data = np.empty(len(coord), dtype=dtypes)
69
+ all_chs = np.concatenate([coord.cpu().numpy().astype(np.float32)] + [v.cpu().numpy() for v in attr.values()], axis=1)
70
+ data[:] = list(map(tuple, all_chs))
71
+ plyfile.PlyData([plyfile.PlyElement.describe(data, 'vertex')]).write(file)
72
+
o-voxel/build/lib.win-amd64-cpython-311/o_voxel/io/vxz.py ADDED
@@ -0,0 +1,365 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import *
2
+ import os
3
+ import json
4
+ import struct
5
+ import torch
6
+ import numpy as np
7
+ import zlib
8
+ import lzma
9
+ import zstandard
10
+ from concurrent.futures import ThreadPoolExecutor
11
+ from ..serialize import encode_seq, decode_seq
12
+ from .. import _C
13
+
14
+
15
+ __all__ = [
16
+ "read_vxz",
17
+ "read_vxz_info",
18
+ "write_vxz",
19
+ ]
20
+
21
+
22
+ """
23
+ VXZ format
24
+
25
+ Header:
26
+ - file type (3 bytes) - 'VXZ'
27
+ - version (1 byte) - 0
28
+ - binary start offset (4 bytes)
29
+ - structure (json) -
30
+ {
31
+ "num_voxel": int,
32
+ "chunk_size": int,
33
+ "filter": str,
34
+ "compression": str,
35
+ "compression_level": int,
36
+ "raw_size": int,
37
+ "compressed_size": int,
38
+ "compress_ratio": float,
39
+ "attr_interleave": str,
40
+ "attr": [
41
+ {"name": str, "chs": int},
42
+ ...
43
+ ]
44
+ "chunks": [
45
+ {
46
+ "ptr": [offset, length], # offset from global binary start
47
+ "svo": [offset, length], # offset from this chunk start
48
+ "attr": [offset, length], # offset from this chunk start
49
+ },
50
+ ...
51
+ ]
52
+ }
53
+ - binary data
54
+ """
55
+
56
+ DEFAULT_COMPRESION_LEVEL = {
57
+ 'none': 0,
58
+ 'deflate': 9,
59
+ 'lzma': 9,
60
+ 'zstd': 22,
61
+ }
62
+
63
+
64
+ def _compress(data: bytes, algo: Literal['none', 'deflate', 'lzma', 'zstd'], level: int) -> bytes:
65
+ if algo == 'none':
66
+ return data
67
+ if level is None:
68
+ level = DEFAULT_COMPRESION_LEVEL[algo]
69
+ if algo == 'deflate':
70
+ compresser = zlib.compressobj(level, wbits=-15)
71
+ return compresser.compress(data) + compresser.flush()
72
+ if algo == 'lzma':
73
+ compresser = lzma.LZMACompressor(format=lzma.FORMAT_RAW, filters=[{'id': lzma.FILTER_LZMA2, 'preset': level}])
74
+ return compresser.compress(data) + compresser.flush()
75
+ if algo == 'zstd':
76
+ compresser = zstandard.ZstdCompressor(level=level, write_checksum=False, write_content_size=True, threads=-1)
77
+ return compresser.compress(data)
78
+ raise ValueError(f"Invalid compression algorithm: {algo}")
79
+
80
+
81
+ def _decompress(data: bytes, algo: Literal['none', 'deflate', 'lzma', 'zstd'], level: int) -> bytes:
82
+ if algo == 'none':
83
+ return data
84
+ if level is None:
85
+ level = DEFAULT_COMPRESION_LEVEL[algo]
86
+ if algo == 'deflate':
87
+ decompresser = zlib.decompressobj(wbits=-15)
88
+ return decompresser.decompress(data) + decompresser.flush()
89
+ if algo == 'lzma':
90
+ decompresser = lzma.LZMADecompressor(format=lzma.FORMAT_RAW, filters=[{'id': lzma.FILTER_LZMA2, 'preset': level}])
91
+ return decompresser.decompress(data)
92
+ if algo == 'zstd':
93
+ decompresser = zstandard.ZstdDecompressor(format=zstandard.FORMAT_ZSTD1)
94
+ return decompresser.decompress(data)
95
+ raise ValueError(f"Invalid compression algorithm: {algo}")
96
+
97
+
98
+ def read_vxz_info(file) -> Dict:
99
+ """
100
+ Read the header of a VXZ file without decompressing the binary data.
101
+
102
+ Args:
103
+ file_path: Path or file-like object to the VXZ file.
104
+
105
+ Returns:
106
+ Dict: the header of the VXZ file.
107
+ """
108
+ if isinstance(file, str):
109
+ with open(file, 'rb') as f:
110
+ file_data = f.read()
111
+ else:
112
+ file_data = file.read()
113
+
114
+ assert file_data[:3] == b'VXZ', "Invalid file type"
115
+ version = file_data[3]
116
+ assert version == 0, "Invalid file version"
117
+
118
+ bin_start = struct.unpack('>I', file_data[4:8])[0]
119
+ structure_data = json.loads(file_data[8:bin_start].decode())
120
+ return structure_data
121
+
122
+
123
+ def read_vxz(file, num_threads: int = -1) -> Union[torch.Tensor, Dict[str, torch.Tensor]]:
124
+ """
125
+ Read a VXZ file containing voxels.
126
+
127
+ Args:
128
+ file_path: Path or file-like object to the VXZ file.
129
+ num_threads: the number of threads to use for reading the file.
130
+
131
+ Returns:
132
+ torch.Tensor: the coordinates of the voxels.
133
+ Dict[str, torch.Tensor]: the attributes of the voxels.
134
+ """
135
+ if isinstance(file, str):
136
+ with open(file, 'rb') as f:
137
+ file_data = f.read()
138
+ else:
139
+ file_data = file.read()
140
+
141
+ num_threads = num_threads if num_threads > 0 else os.cpu_count()
142
+
143
+ # Parse header
144
+ assert file_data[:3] == b'VXZ', "Invalid file type"
145
+ version = file_data[3]
146
+ assert version == 0, "Invalid file version"
147
+
148
+ bin_start = struct.unpack('>I', file_data[4:8])[0]
149
+ structure_data = json.loads(file_data[8:bin_start].decode())
150
+ bin_data = file_data[bin_start:]
151
+
152
+ # Decode chunks
153
+ chunk_size = structure_data['chunk_size']
154
+ chunk_depth = np.log2(chunk_size)
155
+ assert chunk_depth.is_integer(), f"Chunk size must be a power of 2, got {chunk_size}"
156
+ chunk_depth = int(chunk_depth)
157
+
158
+ def worker(chunk_info):
159
+ decompressed = {}
160
+ chunk_data = bin_data[chunk_info['ptr'][0]:chunk_info['ptr'][0]+chunk_info['ptr'][1]]
161
+ for k, v in chunk_info.items():
162
+ if k in ['ptr', 'idx']:
163
+ continue
164
+ decompressed[k] = np.frombuffer(_decompress(chunk_data[v[0]:v[0]+v[1]], structure_data['compression'], structure_data['compression_level']), dtype=np.uint8)
165
+ svo = torch.tensor(np.frombuffer(decompressed['svo'], dtype=np.uint8))
166
+ morton_code = _C.decode_sparse_voxel_octree_cpu(svo, chunk_depth)
167
+ coord = decode_seq(morton_code.int()).cpu()
168
+
169
+ # deinterleave attributes
170
+ if structure_data['attr_interleave'] == 'none':
171
+ all_attr = []
172
+ for k, chs in structure_data['attr']:
173
+ for i in range(chs):
174
+ all_attr.append(torch.tensor(decompressed[f'{k}_{i}']))
175
+ all_attr = torch.stack(all_attr, dim=1)
176
+ elif structure_data['attr_interleave'] == 'as_is':
177
+ all_attr = []
178
+ for k, chs in structure_data['attr']:
179
+ all_attr.append(torch.tensor(decompressed[k].reshape(-1, chs)))
180
+ all_attr = torch.cat(all_attr, dim=1)
181
+ elif structure_data['attr_interleave'] == 'all':
182
+ all_chs = sum(chs for k, chs in structure_data['attr'])
183
+ all_attr = decompressed['attr'].reshape(-1, all_chs)
184
+
185
+ # unfilter
186
+ if structure_data['filter'] == 'none':
187
+ pass
188
+ elif structure_data['filter'] == 'parent':
189
+ all_attr = _C.decode_sparse_voxel_octree_attr_parent_cpu(svo, chunk_depth, all_attr)
190
+ elif structure_data['filter'] == 'neighbor':
191
+ all_attr = _C.decode_sparse_voxel_octree_attr_neighbor_cpu(coord, chunk_size, all_attr)
192
+
193
+ # final
194
+ attr = {}
195
+ ch = 0
196
+ for k, chs in structure_data['attr']:
197
+ attr[k] = all_attr[:, ch:ch+chs]
198
+ ch += chs
199
+ return {
200
+ 'coord': coord,
201
+ 'attr': attr,
202
+ }
203
+
204
+ if num_threads == 1:
205
+ chunks = [worker(info) for info in structure_data['chunks']]
206
+ else:
207
+ with ThreadPoolExecutor(max_workers=num_threads) as executor:
208
+ chunks = list(executor.map(worker, structure_data['chunks']))
209
+
210
+ # Combine chunks
211
+ coord = []
212
+ attr = {k: [] for k, _ in structure_data['attr']}
213
+ for info, chunk in zip(structure_data['chunks'], chunks):
214
+ coord.append(chunk['coord'] + torch.tensor([[info['idx'][0] * chunk_size, info['idx'][1] * chunk_size, info['idx'][2] * chunk_size]]).int())
215
+ for k, v in chunk['attr'].items():
216
+ attr[k].append(v)
217
+ coord = torch.cat(coord, dim=0)
218
+ for k, v in attr.items():
219
+ attr[k] = torch.cat(v, dim=0)
220
+ return coord, attr
221
+
222
+
223
+ def write_vxz(
224
+ file,
225
+ coord: torch.Tensor,
226
+ attr: Dict[str, torch.Tensor],
227
+ chunk_size: int = 256,
228
+ filter: Literal['none', 'parent', 'neighbor'] = 'none',
229
+ compression: Literal['none', 'deflate', 'lzma', 'zstd'] = 'lzma',
230
+ compression_level: Optional[int] = None,
231
+ attr_interleave: Literal['none', 'as_is', 'all'] = 'as_is',
232
+ num_threads: int = -1,
233
+ ):
234
+ """
235
+ Write a VXZ file containing voxels.
236
+
237
+ Args:
238
+ file: Path or file-like object to the VXZ file.
239
+ coord: the coordinates of the voxels.
240
+ attr: the attributes of the voxels.
241
+ chunk_size: the size of each chunk.
242
+ filter: the filter to apply to the voxels.
243
+ compression: the compression algorithm to use.
244
+ compression_level: the level of compression.
245
+ attr_interleave: how to interleave the attributes.
246
+ num_threads: the number of threads to use for compression.
247
+ """
248
+ # Check
249
+ for k, v in attr.items():
250
+ assert coord.shape[0] == v.shape[0], f"Number of coordinates and attributes do not match for key {k}"
251
+ assert v.dtype == torch.uint8, f"Attributes must be uint8, got {v.dtype} for key {k}"
252
+ assert attr_interleave in ['none', 'as_is', 'all'], f"Invalid attr_interleave value: {attr_interleave}"
253
+
254
+ compression_level = compression_level or DEFAULT_COMPRESION_LEVEL[compression]
255
+ num_threads = num_threads if num_threads > 0 else os.cpu_count()
256
+
257
+ file_info = {
258
+ 'num_voxel': coord.shape[0],
259
+ 'chunk_size': chunk_size,
260
+ 'filter': filter,
261
+ 'compression': compression,
262
+ 'compression_level': compression_level,
263
+ 'raw_size': sum([coord.numel() * 4] + [v.numel() for v in attr.values()]),
264
+ 'compressed_size': 0,
265
+ 'compress_ratio': 0.0,
266
+ 'attr_interleave': attr_interleave,
267
+ 'attr': [[k, v.shape[1]] for k, v in attr.items()],
268
+ 'chunks': [],
269
+ }
270
+ bin_data = b''
271
+
272
+ # Split into chunks
273
+ chunk_depth = np.log2(chunk_size)
274
+ assert chunk_depth.is_integer(), f"Chunk size must be a power of 2, got {chunk_size}"
275
+ chunk_depth = int(chunk_depth)
276
+
277
+ chunk_coord = coord // chunk_size
278
+ coord = coord % chunk_size
279
+ unique_chunk_coord, inverse = torch.unique(chunk_coord, dim=0, return_inverse=True)
280
+
281
+ chunks = []
282
+ for idx, chunk_xyz in enumerate(unique_chunk_coord.tolist()):
283
+ chunk_mask = (inverse == idx)
284
+ chunks.append({
285
+ 'idx': chunk_xyz,
286
+ 'coord': coord[chunk_mask],
287
+ 'attr': {k: v[chunk_mask] for k, v in attr.items()},
288
+ })
289
+
290
+ # Compress each chunk
291
+ with ThreadPoolExecutor(max_workers=num_threads) as executor:
292
+ def worker(chunk):
293
+ ## compress to binary
294
+ coord = chunk['coord']
295
+ morton_code = encode_seq(coord)
296
+ sorted_idx = morton_code.argsort().cpu()
297
+ coord = coord.cpu()[sorted_idx]
298
+ morton_code = morton_code.cpu()[sorted_idx]
299
+ attr = torch.cat([v.cpu()[sorted_idx] for v in chunk['attr'].values()], dim=1)
300
+ svo = _C.encode_sparse_voxel_octree_cpu(morton_code, chunk_depth)
301
+ svo_bytes = _compress(svo.numpy().tobytes(), compression, compression_level)
302
+
303
+ # filter
304
+ if filter == 'none':
305
+ attr = attr.numpy()
306
+ elif filter == 'parent':
307
+ attr = _C.encode_sparse_voxel_octree_attr_parent_cpu(svo, chunk_depth, attr).numpy()
308
+ elif filter == 'neighbor':
309
+ attr = _C.encode_sparse_voxel_octree_attr_neighbor_cpu(coord, chunk_size, attr).numpy()
310
+
311
+ # interleave attributes
312
+ attr_bytes = {}
313
+ if attr_interleave == 'none':
314
+ ch = 0
315
+ for k, chs in file_info['attr']:
316
+ for i in range(chs):
317
+ attr_bytes[f'{k}_{i}'] = _compress(attr[:, ch].tobytes(), compression, compression_level)
318
+ ch += 1
319
+ elif attr_interleave == 'as_is':
320
+ ch = 0
321
+ for k, chs in file_info['attr']:
322
+ attr_bytes[k] = _compress(attr[:, ch:ch+chs].tobytes(), compression, compression_level)
323
+ ch += chs
324
+ elif attr_interleave == 'all':
325
+ attr_bytes['attr'] = _compress(attr.tobytes(), compression, compression_level)
326
+
327
+ ## buffer for each chunk
328
+ chunk_info = {'idx': chunk['idx']}
329
+ bin_data = b''
330
+
331
+ ### svo
332
+ chunk_info['svo'] = [len(bin_data), len(svo_bytes)]
333
+ bin_data += svo_bytes
334
+
335
+ ### attr
336
+ for k, v in attr_bytes.items():
337
+ chunk_info[k] = [len(bin_data), len(v)]
338
+ bin_data += v
339
+
340
+ return chunk_info, bin_data
341
+
342
+ chunks = list(executor.map(worker, chunks))
343
+
344
+ for chunk_info, chunk_data in chunks:
345
+ chunk_info['ptr'] = [len(bin_data), len(chunk_data)]
346
+ bin_data += chunk_data
347
+ file_info['chunks'].append(chunk_info)
348
+
349
+ file_info['compressed_size'] = len(bin_data)
350
+ file_info['compress_ratio'] = file_info['raw_size'] / file_info['compressed_size']
351
+
352
+ # File parts
353
+ structure_data = json.dumps(file_info).encode()
354
+ header = b'VXZ\x00' + struct.pack('>I', len(structure_data) + 8)
355
+
356
+ # Write to file
357
+ if isinstance(file, str):
358
+ with open(file, 'wb') as f:
359
+ f.write(header)
360
+ f.write(structure_data)
361
+ f.write(bin_data)
362
+ else:
363
+ file.write(header)
364
+ file.write(structure_data)
365
+ file.write(bin_data)
o-voxel/build/lib.win-amd64-cpython-311/o_voxel/postprocess.py ADDED
@@ -0,0 +1,331 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import *
2
+ from tqdm import tqdm
3
+ import numpy as np
4
+ import torch
5
+ import cv2
6
+ from PIL import Image
7
+ import trimesh
8
+ import trimesh.visual
9
+ from flex_gemm.ops.grid_sample import grid_sample_3d
10
+ import nvdiffrast.torch as dr
11
+ import cumesh
12
+
13
+
14
+ def to_glb(
15
+ vertices: torch.Tensor,
16
+ faces: torch.Tensor,
17
+ attr_volume: torch.Tensor,
18
+ coords: torch.Tensor,
19
+ attr_layout: Dict[str, slice],
20
+ aabb: Union[list, tuple, np.ndarray, torch.Tensor],
21
+ voxel_size: Union[float, list, tuple, np.ndarray, torch.Tensor] = None,
22
+ grid_size: Union[int, list, tuple, np.ndarray, torch.Tensor] = None,
23
+ decimation_target: int = 1000000,
24
+ texture_size: int = 2048,
25
+ remesh: bool = False,
26
+ remesh_band: float = 1,
27
+ remesh_project: float = 0.9,
28
+ mesh_cluster_threshold_cone_half_angle_rad=np.radians(90.0),
29
+ mesh_cluster_refine_iterations=0,
30
+ mesh_cluster_global_iterations=1,
31
+ mesh_cluster_smooth_strength=1,
32
+ verbose: bool = False,
33
+ use_tqdm: bool = False,
34
+ ):
35
+ """
36
+ Convert an extracted mesh to a GLB file.
37
+ Performs cleaning, optional remeshing, UV unwrapping, and texture baking from a volume.
38
+
39
+ Args:
40
+ vertices: (N, 3) tensor of vertex positions
41
+ faces: (M, 3) tensor of vertex indices
42
+ attr_volume: (L, C) features of a sprase tensor for attribute interpolation
43
+ coords: (L, 3) tensor of coordinates for each voxel
44
+ attr_layout: dictionary of slice objects for each attribute
45
+ aabb: (2, 3) tensor of minimum and maximum coordinates of the volume
46
+ voxel_size: (3,) tensor of size of each voxel
47
+ grid_size: (3,) tensor of number of voxels in each dimension
48
+ decimation_target: target number of vertices for mesh simplification
49
+ texture_size: size of the texture for baking
50
+ remesh: whether to perform remeshing
51
+ remesh_band: size of the remeshing band
52
+ remesh_project: projection factor for remeshing
53
+ mesh_cluster_threshold_cone_half_angle_rad: threshold for cone-based clustering in uv unwrapping
54
+ mesh_cluster_refine_iterations: number of iterations for refining clusters in uv unwrapping
55
+ mesh_cluster_global_iterations: number of global iterations for clustering in uv unwrapping
56
+ mesh_cluster_smooth_strength: strength of smoothing for clustering in uv unwrapping
57
+ verbose: whether to print verbose messages
58
+ use_tqdm: whether to use tqdm to display progress bar
59
+ """
60
+ # --- Input Normalization (AABB, Voxel Size, Grid Size) ---
61
+ if isinstance(aabb, (list, tuple)):
62
+ aabb = np.array(aabb)
63
+ if isinstance(aabb, np.ndarray):
64
+ aabb = torch.tensor(aabb, dtype=torch.float32, device=coords.device)
65
+ assert isinstance(aabb, torch.Tensor), f"aabb must be a list, tuple, np.ndarray, or torch.Tensor, but got {type(aabb)}"
66
+ assert aabb.dim() == 2, f"aabb must be a 2D tensor, but got {aabb.shape}"
67
+ assert aabb.size(0) == 2, f"aabb must have 2 rows, but got {aabb.size(0)}"
68
+ assert aabb.size(1) == 3, f"aabb must have 3 columns, but got {aabb.size(1)}"
69
+
70
+ # Calculate grid dimensions based on AABB and voxel size
71
+ if voxel_size is not None:
72
+ if isinstance(voxel_size, float):
73
+ voxel_size = [voxel_size, voxel_size, voxel_size]
74
+ if isinstance(voxel_size, (list, tuple)):
75
+ voxel_size = np.array(voxel_size)
76
+ if isinstance(voxel_size, np.ndarray):
77
+ voxel_size = torch.tensor(voxel_size, dtype=torch.float32, device=coords.device)
78
+ grid_size = ((aabb[1] - aabb[0]) / voxel_size).round().int()
79
+ else:
80
+ assert grid_size is not None, "Either voxel_size or grid_size must be provided"
81
+ if isinstance(grid_size, int):
82
+ grid_size = [grid_size, grid_size, grid_size]
83
+ if isinstance(grid_size, (list, tuple)):
84
+ grid_size = np.array(grid_size)
85
+ if isinstance(grid_size, np.ndarray):
86
+ grid_size = torch.tensor(grid_size, dtype=torch.int32, device=coords.device)
87
+ voxel_size = (aabb[1] - aabb[0]) / grid_size
88
+
89
+ # Assertions for dimensions
90
+ assert isinstance(voxel_size, torch.Tensor)
91
+ assert voxel_size.dim() == 1 and voxel_size.size(0) == 3
92
+ assert isinstance(grid_size, torch.Tensor)
93
+ assert grid_size.dim() == 1 and grid_size.size(0) == 3
94
+
95
+ if use_tqdm:
96
+ pbar = tqdm(total=6, desc="Extracting GLB")
97
+ if verbose:
98
+ print(f"Original mesh: {vertices.shape[0]} vertices, {faces.shape[0]} faces")
99
+
100
+ # Move data to GPU
101
+ vertices = vertices.cuda()
102
+ faces = faces.cuda()
103
+
104
+ # Initialize CUDA mesh handler
105
+ mesh = cumesh.CuMesh()
106
+ mesh.init(vertices, faces)
107
+
108
+ # --- Initial Mesh Cleaning ---
109
+ # Fills holes as much as we can before processing
110
+ mesh.fill_holes(max_hole_perimeter=3e-2)
111
+ if verbose:
112
+ print(f"After filling holes: {mesh.num_vertices} vertices, {mesh.num_faces} faces")
113
+ vertices, faces = mesh.read()
114
+ if use_tqdm:
115
+ pbar.update(1)
116
+
117
+ # Build BVH for the current mesh to guide remeshing
118
+ if use_tqdm:
119
+ pbar.set_description("Building BVH")
120
+ if verbose:
121
+ print(f"Building BVH for current mesh...", end='', flush=True)
122
+ bvh = cumesh.cuBVH(vertices, faces)
123
+ if use_tqdm:
124
+ pbar.update(1)
125
+ if verbose:
126
+ print("Done")
127
+
128
+ if use_tqdm:
129
+ pbar.set_description("Cleaning mesh")
130
+ if verbose:
131
+ print("Cleaning mesh...")
132
+
133
+ # --- Branch 1: Standard Pipeline (Simplification & Cleaning) ---
134
+ if not remesh:
135
+ # Step 1: Aggressive simplification (3x target)
136
+ mesh.simplify(decimation_target * 3, verbose=verbose)
137
+ if verbose:
138
+ print(f"After inital simplification: {mesh.num_vertices} vertices, {mesh.num_faces} faces")
139
+
140
+ # Step 2: Clean up topology (duplicates, non-manifolds, isolated parts)
141
+ mesh.remove_duplicate_faces()
142
+ mesh.repair_non_manifold_edges()
143
+ mesh.remove_small_connected_components(1e-5)
144
+ mesh.fill_holes(max_hole_perimeter=3e-2)
145
+ if verbose:
146
+ print(f"After initial cleanup: {mesh.num_vertices} vertices, {mesh.num_faces} faces")
147
+
148
+ # Step 3: Final simplification to target count
149
+ mesh.simplify(decimation_target, verbose=verbose)
150
+ if verbose:
151
+ print(f"After final simplification: {mesh.num_vertices} vertices, {mesh.num_faces} faces")
152
+
153
+ # Step 4: Final Cleanup loop
154
+ mesh.remove_duplicate_faces()
155
+ mesh.repair_non_manifold_edges()
156
+ mesh.remove_small_connected_components(1e-5)
157
+ mesh.fill_holes(max_hole_perimeter=3e-2)
158
+ if verbose:
159
+ print(f"After final cleanup: {mesh.num_vertices} vertices, {mesh.num_faces} faces")
160
+
161
+ # Step 5: Unify face orientations
162
+ mesh.unify_face_orientations()
163
+
164
+ # --- Branch 2: Remeshing Pipeline ---
165
+ else:
166
+ center = aabb.mean(dim=0)
167
+ scale = (aabb[1] - aabb[0]).max().item()
168
+ resolution = grid_size.max().item()
169
+
170
+ # Perform Dual Contouring remeshing (rebuilds topology)
171
+ mesh.init(*cumesh.remeshing.remesh_narrow_band_dc(
172
+ vertices, faces,
173
+ center = center,
174
+ scale = (resolution + 3 * remesh_band) / resolution * scale,
175
+ resolution = resolution,
176
+ band = remesh_band,
177
+ project_back = remesh_project, # Snaps vertices back to original surface
178
+ verbose = verbose,
179
+ bvh = bvh,
180
+ ))
181
+ if verbose:
182
+ print(f"After remeshing: {mesh.num_vertices} vertices, {mesh.num_faces} faces")
183
+
184
+ # Simplify and clean the remeshed result (similar logic to above)
185
+ mesh.simplify(decimation_target, verbose=verbose)
186
+ if verbose:
187
+ print(f"After simplifying: {mesh.num_vertices} vertices, {mesh.num_faces} faces")
188
+
189
+ if use_tqdm:
190
+ pbar.update(1)
191
+ if verbose:
192
+ print("Done")
193
+
194
+
195
+ # --- UV Parameterization ---
196
+ if use_tqdm:
197
+ pbar.set_description("Parameterizing new mesh")
198
+ if verbose:
199
+ print("Parameterizing new mesh...")
200
+
201
+ out_vertices, out_faces, out_uvs, out_vmaps = mesh.uv_unwrap(
202
+ compute_charts_kwargs={
203
+ "threshold_cone_half_angle_rad": mesh_cluster_threshold_cone_half_angle_rad,
204
+ "refine_iterations": mesh_cluster_refine_iterations,
205
+ "global_iterations": mesh_cluster_global_iterations,
206
+ "smooth_strength": mesh_cluster_smooth_strength,
207
+ },
208
+ return_vmaps=True,
209
+ verbose=verbose,
210
+ )
211
+ out_vertices = out_vertices.cuda()
212
+ out_faces = out_faces.cuda()
213
+ out_uvs = out_uvs.cuda()
214
+ out_vmaps = out_vmaps.cuda()
215
+ mesh.compute_vertex_normals()
216
+ out_normals = mesh.read_vertex_normals()[out_vmaps]
217
+
218
+ if use_tqdm:
219
+ pbar.update(1)
220
+ if verbose:
221
+ print("Done")
222
+
223
+ # --- Texture Baking (Attribute Sampling) ---
224
+ if use_tqdm:
225
+ pbar.set_description("Sampling attributes")
226
+ if verbose:
227
+ print("Sampling attributes...", end='', flush=True)
228
+
229
+ # Setup differentiable rasterizer context
230
+ ctx = dr.RasterizeCudaContext()
231
+ # Prepare UV coordinates for rasterization (rendering in UV space)
232
+ uvs_rast = torch.cat([out_uvs * 2 - 1, torch.zeros_like(out_uvs[:, :1]), torch.ones_like(out_uvs[:, :1])], dim=-1).unsqueeze(0)
233
+ rast = torch.zeros((1, texture_size, texture_size, 4), device='cuda', dtype=torch.float32)
234
+
235
+ # Rasterize in chunks to save memory
236
+ for i in range(0, out_faces.shape[0], 100000):
237
+ rast_chunk, _ = dr.rasterize(
238
+ ctx, uvs_rast, out_faces[i:i+100000],
239
+ resolution=[texture_size, texture_size],
240
+ )
241
+ mask_chunk = rast_chunk[..., 3:4] > 0
242
+ rast_chunk[..., 3:4] += i # Store face ID in alpha channel
243
+ rast = torch.where(mask_chunk, rast_chunk, rast)
244
+
245
+ # Mask of valid pixels in texture
246
+ mask = rast[0, ..., 3] > 0
247
+
248
+ # Interpolate 3D positions in UV space (finding 3D coord for every texel)
249
+ pos = dr.interpolate(out_vertices.unsqueeze(0), rast, out_faces)[0][0]
250
+ valid_pos = pos[mask]
251
+
252
+ # Map these positions back to the *original* high-res mesh to get accurate attributes
253
+ # This corrects geometric errors introduced by simplification/remeshing
254
+ _, face_id, uvw = bvh.unsigned_distance(valid_pos, return_uvw=True)
255
+ orig_tri_verts = vertices[faces[face_id.long()]] # (N_new, 3, 3)
256
+ valid_pos = (orig_tri_verts * uvw.unsqueeze(-1)).sum(dim=1)
257
+
258
+ # Trilinear sampling from the attribute volume (Color, Material props)
259
+ attrs = torch.zeros(texture_size, texture_size, attr_volume.shape[1], device='cuda')
260
+ attrs[mask] = grid_sample_3d(
261
+ attr_volume,
262
+ torch.cat([torch.zeros_like(coords[:, :1]), coords], dim=-1),
263
+ shape=torch.Size([1, attr_volume.shape[1], *grid_size.tolist()]),
264
+ grid=((valid_pos - aabb[0]) / voxel_size).reshape(1, -1, 3),
265
+ mode='trilinear',
266
+ )
267
+ if use_tqdm:
268
+ pbar.update(1)
269
+ if verbose:
270
+ print("Done")
271
+
272
+ # --- Texture Post-Processing & Material Construction ---
273
+ if use_tqdm:
274
+ pbar.set_description("Finalizing mesh")
275
+ if verbose:
276
+ print("Finalizing mesh...", end='', flush=True)
277
+
278
+ mask = mask.cpu().numpy()
279
+
280
+ # Extract channels based on layout (BaseColor, Metallic, Roughness, Alpha)
281
+ base_color = np.clip(attrs[..., attr_layout['base_color']].cpu().numpy() * 255, 0, 255).astype(np.uint8)
282
+ metallic = np.clip(attrs[..., attr_layout['metallic']].cpu().numpy() * 255, 0, 255).astype(np.uint8)
283
+ roughness = np.clip(attrs[..., attr_layout['roughness']].cpu().numpy() * 255, 0, 255).astype(np.uint8)
284
+ alpha = np.clip(attrs[..., attr_layout['alpha']].cpu().numpy() * 255, 0, 255).astype(np.uint8)
285
+ alpha_mode = 'OPAQUE'
286
+
287
+ # Inpainting: fill gaps (dilation) to prevent black seams at UV boundaries
288
+ mask_inv = (~mask).astype(np.uint8)
289
+ base_color = cv2.inpaint(base_color, mask_inv, 3, cv2.INPAINT_TELEA)
290
+ metallic = cv2.inpaint(metallic, mask_inv, 1, cv2.INPAINT_TELEA)[..., None]
291
+ roughness = cv2.inpaint(roughness, mask_inv, 1, cv2.INPAINT_TELEA)[..., None]
292
+ alpha = cv2.inpaint(alpha, mask_inv, 1, cv2.INPAINT_TELEA)[..., None]
293
+
294
+ # Create PBR material
295
+ # Standard PBR packs Metallic and Roughness into Blue and Green channels
296
+ material = trimesh.visual.material.PBRMaterial(
297
+ baseColorTexture=Image.fromarray(np.concatenate([base_color, alpha], axis=-1)),
298
+ baseColorFactor=np.array([255, 255, 255, 255], dtype=np.uint8),
299
+ metallicRoughnessTexture=Image.fromarray(np.concatenate([np.zeros_like(metallic), roughness, metallic], axis=-1)),
300
+ metallicFactor=1.0,
301
+ roughnessFactor=1.0,
302
+ alphaMode=alpha_mode,
303
+ doubleSided=True if not remesh else False,
304
+ )
305
+
306
+ # --- Coordinate System Conversion & Final Object ---
307
+ vertices_np = out_vertices.cpu().numpy()
308
+ faces_np = out_faces.cpu().numpy()
309
+ uvs_np = out_uvs.cpu().numpy()
310
+ normals_np = out_normals.cpu().numpy()
311
+
312
+ # Swap Y and Z axes, invert Y (common conversion for GLB compatibility)
313
+ vertices_np[:, 1], vertices_np[:, 2] = vertices_np[:, 2], -vertices_np[:, 1]
314
+ normals_np[:, 1], normals_np[:, 2] = normals_np[:, 2], -normals_np[:, 1]
315
+ uvs_np[:, 1] = 1 - uvs_np[:, 1] # Flip UV V-coordinate
316
+
317
+ textured_mesh = trimesh.Trimesh(
318
+ vertices=vertices_np,
319
+ faces=faces_np,
320
+ vertex_normals=normals_np,
321
+ process=False,
322
+ visual=trimesh.visual.TextureVisuals(uv=uvs_np, material=material)
323
+ )
324
+
325
+ if use_tqdm:
326
+ pbar.update(1)
327
+ pbar.close()
328
+ if verbose:
329
+ print("Done")
330
+
331
+ return textured_mesh
o-voxel/build/lib.win-amd64-cpython-311/o_voxel/rasterize.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn.functional as F
3
+ from easydict import EasyDict as edict
4
+ from . import _C
5
+
6
+
7
+ def intrinsics_to_projection(
8
+ intrinsics: torch.Tensor,
9
+ near: float,
10
+ far: float,
11
+ ) -> torch.Tensor:
12
+ """
13
+ OpenCV intrinsics to OpenGL perspective matrix
14
+
15
+ Args:
16
+ intrinsics (torch.Tensor): [3, 3] OpenCV intrinsics matrix
17
+ near (float): near plane to clip
18
+ far (float): far plane to clip
19
+ Returns:
20
+ (torch.Tensor): [4, 4] OpenGL perspective matrix
21
+ """
22
+ fx, fy = intrinsics[0, 0], intrinsics[1, 1]
23
+ cx, cy = intrinsics[0, 2], intrinsics[1, 2]
24
+ ret = torch.zeros((4, 4), dtype=intrinsics.dtype, device=intrinsics.device)
25
+ ret[0, 0] = 2 * fx
26
+ ret[1, 1] = 2 * fy
27
+ ret[0, 2] = 2 * cx - 1
28
+ ret[1, 2] = - 2 * cy + 1
29
+ ret[2, 2] = far / (far - near)
30
+ ret[2, 3] = near * far / (near - far)
31
+ ret[3, 2] = 1.
32
+ return ret
33
+
34
+
35
+ class VoxelRenderer:
36
+ """
37
+ Renderer for the Voxel representation.
38
+
39
+ Args:
40
+ rendering_options (dict): Rendering options.
41
+ """
42
+
43
+ def __init__(self, rendering_options={}) -> None:
44
+ self.rendering_options = edict({
45
+ "resolution": None,
46
+ "near": 0.1,
47
+ "far": 10.0,
48
+ "ssaa": 1,
49
+ })
50
+ self.rendering_options.update(rendering_options)
51
+
52
+ def render(
53
+ self,
54
+ position: torch.Tensor,
55
+ attrs: torch.Tensor,
56
+ voxel_size: float,
57
+ extrinsics: torch.Tensor,
58
+ intrinsics: torch.Tensor,
59
+ ) -> edict:
60
+ """
61
+ Render the octree.
62
+
63
+ Args:
64
+ position (torch.Tensor): (N, 3) xyz positions
65
+ attrs (torch.Tensor): (N, C) attributes
66
+ voxel_size (float): voxel size
67
+ extrinsics (torch.Tensor): (4, 4) camera extrinsics
68
+ intrinsics (torch.Tensor): (3, 3) camera intrinsics
69
+
70
+ Returns:
71
+ edict containing:
72
+ attr (torch.Tensor): (C, H, W) rendered color
73
+ depth (torch.Tensor): (H, W) rendered depth
74
+ alpha (torch.Tensor): (H, W) rendered alpha
75
+ """
76
+ resolution = self.rendering_options["resolution"]
77
+ near = self.rendering_options["near"]
78
+ far = self.rendering_options["far"]
79
+ ssaa = self.rendering_options["ssaa"]
80
+
81
+ view = extrinsics
82
+ perspective = intrinsics_to_projection(intrinsics, near, far)
83
+ camera = torch.inverse(view)[:3, 3]
84
+ focalx = intrinsics[0, 0]
85
+ focaly = intrinsics[1, 1]
86
+ args = (
87
+ position,
88
+ attrs,
89
+ voxel_size,
90
+ view.T.contiguous(),
91
+ (perspective @ view).T.contiguous(),
92
+ camera,
93
+ 0.5 / focalx,
94
+ 0.5 / focaly,
95
+ resolution * ssaa,
96
+ resolution * ssaa,
97
+ )
98
+ color, depth, alpha = _C.rasterize_voxels_cuda(*args)
99
+
100
+ if ssaa > 1:
101
+ color = F.interpolate(color[None], size=(resolution, resolution), mode='bilinear', align_corners=False, antialias=True).squeeze()
102
+ depth = F.interpolate(depth[None, None], size=(resolution, resolution), mode='bilinear', align_corners=False, antialias=True).squeeze()
103
+ alpha = F.interpolate(alpha[None, None], size=(resolution, resolution), mode='bilinear', align_corners=False, antialias=True).squeeze()
104
+
105
+ ret = edict({
106
+ 'attr': color,
107
+ 'depth': depth,
108
+ 'alpha': alpha,
109
+ })
110
+ return ret
111
+
o-voxel/build/lib.win-amd64-cpython-311/o_voxel/serialize.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import *
2
+ import torch
3
+ from . import _C
4
+
5
+
6
+ @torch.no_grad()
7
+ def encode_seq(coords: torch.Tensor, permute: List[int] = [0, 1, 2], mode: Literal['z_order', 'hilbert'] = 'z_order') -> torch.Tensor:
8
+ """
9
+ Encodes 3D coordinates into a 30-bit code.
10
+
11
+ Args:
12
+ coords: a tensor of shape [N, 3] containing the 3D coordinates.
13
+ permute: the permutation of the coordinates.
14
+ mode: the encoding mode to use.
15
+ """
16
+ assert coords.shape[-1] == 3 and coords.ndim == 2, "Input coordinates must be of shape [N, 3]"
17
+ x = coords[:, permute[0]].int()
18
+ y = coords[:, permute[1]].int()
19
+ z = coords[:, permute[2]].int()
20
+ if mode == 'z_order':
21
+ if coords.device.type == 'cpu':
22
+ return _C.z_order_encode_cpu(x, y, z)
23
+ elif coords.device.type == 'cuda':
24
+ return _C.z_order_encode_cuda(x, y, z)
25
+ else:
26
+ raise ValueError(f"Unsupported device type: {coords.device.type}")
27
+ elif mode == 'hilbert':
28
+ if coords.device.type == 'cpu':
29
+ return _C.hilbert_encode_cpu(x, y, z)
30
+ elif coords.device.type == 'cuda':
31
+ return _C.hilbert_encode_cuda(x, y, z)
32
+ else:
33
+ raise ValueError(f"Unsupported device type: {coords.device.type}")
34
+ else:
35
+ raise ValueError(f"Unknown encoding mode: {mode}")
36
+
37
+
38
+ @torch.no_grad()
39
+ def decode_seq(code: torch.Tensor, permute: List[int] = [0, 1, 2], mode: Literal['z_order', 'hilbert'] = 'z_order') -> torch.Tensor:
40
+ """
41
+ Decodes a 30-bit code into 3D coordinates.
42
+
43
+ Args:
44
+ code: a tensor of shape [N] containing the 30-bit code.
45
+ permute: the permutation of the coordinates.
46
+ mode: the decoding mode to use.
47
+ """
48
+ assert code.ndim == 1, "Input code must be of shape [N]"
49
+ if mode == 'z_order':
50
+ if code.device.type == 'cpu':
51
+ coords = _C.z_order_decode_cpu(code)
52
+ elif code.device.type == 'cuda':
53
+ coords = _C.z_order_decode_cuda(code)
54
+ else:
55
+ raise ValueError(f"Unsupported device type: {code.device.type}")
56
+ elif mode == 'hilbert':
57
+ if code.device.type == 'cpu':
58
+ coords = _C.hilbert_decode_cpu(code)
59
+ elif code.device.type == 'cuda':
60
+ coords = _C.hilbert_decode_cuda(code)
61
+ else:
62
+ raise ValueError(f"Unsupported device type: {code.device.type}")
63
+ else:
64
+ raise ValueError(f"Unknown decoding mode: {mode}")
65
+ x = coords[permute.index(0)]
66
+ y = coords[permute.index(1)]
67
+ z = coords[permute.index(2)]
68
+ return torch.stack([x, y, z], dim=-1)
o-voxel/build/temp.win-amd64-cpython-311/Release/.ninja_deps ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7fadf53ce27c1cc064c4c4e7d4acdca36f011728eb7a355d2fe876d06d11ef89
3
+ size 1473980
o-voxel/build/temp.win-amd64-cpython-311/Release/.ninja_log ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ninja log v7
2
+ 43 5089 7920314585696679 C:/Users/opsiclear/Desktop/projects/Trellis2_multi_image_conditioning/o-voxel/build/temp.win-amd64-cpython-311/Release/src/serialize/z_order.obj aba9bdfd7758963
3
+ 40 5094 7920314585696679 C:/Users/opsiclear/Desktop/projects/Trellis2_multi_image_conditioning/o-voxel/build/temp.win-amd64-cpython-311/Release/src/serialize/hilbert.obj 96320bdff7b77437
4
+ 30 12370 7920314585499614 C:/Users/opsiclear/Desktop/projects/Trellis2_multi_image_conditioning/o-voxel/build/temp.win-amd64-cpython-311/Release/src/io/svo.obj 2237e66b874990a
5
+ 23 12418 7920314585499614 C:/Users/opsiclear/Desktop/projects/Trellis2_multi_image_conditioning/o-voxel/build/temp.win-amd64-cpython-311/Release/src/io/filter_neighbor.obj 41021b78b504c47e
6
+ 26 12470 7920314585499614 C:/Users/opsiclear/Desktop/projects/Trellis2_multi_image_conditioning/o-voxel/build/temp.win-amd64-cpython-311/Release/src/io/filter_parent.obj 471c5c41ea624cff
7
+ 13 13565 7920314585421492 C:/Users/opsiclear/Desktop/projects/Trellis2_multi_image_conditioning/o-voxel/build/temp.win-amd64-cpython-311/Release/src/convert/volumetic_attr.obj a880e2e3fea2c1dc
8
+ 16 14155 7920314585421492 C:/Users/opsiclear/Desktop/projects/Trellis2_multi_image_conditioning/o-voxel/build/temp.win-amd64-cpython-311/Release/src/ext.obj c49c64d83f84cba7
9
+ 9 22492 7920314585385751 C:/Users/opsiclear/Desktop/projects/Trellis2_multi_image_conditioning/o-voxel/build/temp.win-amd64-cpython-311/Release/src/convert/flexible_dual_grid.obj 387210cbde44cf56
10
+ 36 39184 7920314585658621 C:/Users/opsiclear/Desktop/projects/Trellis2_multi_image_conditioning/o-voxel/build/temp.win-amd64-cpython-311/Release/src/serialize/api.obj 9d1bef8355fab5c1
11
+ 19 39211 7920314585489571 C:/Users/opsiclear/Desktop/projects/Trellis2_multi_image_conditioning/o-voxel/build/temp.win-amd64-cpython-311/Release/src/hash/hash.obj ca81a4c30cd1e199
12
+ 33 40641 7920314585629483 C:/Users/opsiclear/Desktop/projects/Trellis2_multi_image_conditioning/o-voxel/build/temp.win-amd64-cpython-311/Release/src/rasterize/rasterize.obj cacdf260d45d5cc
o-voxel/build/temp.win-amd64-cpython-311/Release/build.ninja ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ninja_required_version = 1.3
2
+ cxx = cl
3
+ nvcc = C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.0\bin\nvcc
4
+
5
+ cflags = /nologo /O2 /W3 /GL /DNDEBUG /MD -IC:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\third_party/eigen -IC:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\.venv\Lib\site-packages\torch\include -IC:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\.venv\Lib\site-packages\torch\include\torch\csrc\api\include "-IC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.0\include" -IC:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\.venv\include -IC:\Users\opsiclear\AppData\Roaming\uv\python\cpython-3.11.13-windows-x86_64-none\include -IC:\Users\opsiclear\AppData\Roaming\uv\python\cpython-3.11.13-windows-x86_64-none\Include "-IC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\include" "-IC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\ATLMFC\include" "-IC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\VS\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.26100.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\um" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\shared" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\winrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\cppwinrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um" /MD /wd4819 /wd4251 /wd4244 /wd4267 /wd4275 /wd4018 /wd4190 /wd4624 /wd4067 /wd4068 /EHsc
6
+ post_cflags = /O2 /std:c++20 -DTORCH_API_INCLUDE_EXTENSION_H -DTORCH_EXTENSION_NAME=_C
7
+ cuda_cflags = -std=c++17 -Xcompiler /MD -Xcompiler /wd4819 -Xcompiler /wd4251 -Xcompiler /wd4244 -Xcompiler /wd4267 -Xcompiler /wd4275 -Xcompiler /wd4018 -Xcompiler /wd4190 -Xcompiler /wd4624 -Xcompiler /wd4067 -Xcompiler /wd4068 -Xcompiler /EHsc --use-local-env -Xcudafe --diag_suppress=base_class_has_different_dll_interface -Xcudafe --diag_suppress=field_without_dll_interface -Xcudafe --diag_suppress=dll_interface_conflict_none_assumed -Xcudafe --diag_suppress=dll_interface_conflict_dllexport_assumed -IC:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\third_party/eigen -IC:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\.venv\Lib\site-packages\torch\include -IC:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\.venv\Lib\site-packages\torch\include\torch\csrc\api\include "-IC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.0\include" -IC:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\.venv\include -IC:\Users\opsiclear\AppData\Roaming\uv\python\cpython-3.11.13-windows-x86_64-none\include -IC:\Users\opsiclear\AppData\Roaming\uv\python\cpython-3.11.13-windows-x86_64-none\Include "-IC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\include" "-IC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\ATLMFC\include" "-IC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\VS\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.26100.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\um" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\shared" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\winrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.26100.0\\cppwinrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um"
8
+ cuda_post_cflags = -D__CUDA_NO_HALF_OPERATORS__ -D__CUDA_NO_HALF_CONVERSIONS__ -D__CUDA_NO_BFLOAT16_CONVERSIONS__ -D__CUDA_NO_HALF2_OPERATORS__ --expt-relaxed-constexpr -O3 -std=c++20 -DTORCH_API_INCLUDE_EXTENSION_H -DTORCH_EXTENSION_NAME=_C -gencode=arch=compute_120,code=compute_120 -gencode=arch=compute_120,code=sm_120
9
+ cuda_dlink_post_cflags =
10
+ sycl_dlink_post_cflags =
11
+ ldflags =
12
+
13
+ rule compile
14
+ command = cl /showIncludes $cflags -c $in /Fo$out $post_cflags
15
+ deps = msvc
16
+
17
+ rule cuda_compile
18
+ depfile = $out.d
19
+ deps = gcc
20
+ command = $nvcc --generate-dependencies-with-compile --dependency-output $out.d $cuda_cflags -c $in -o $out $cuda_post_cflags
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+ build C$:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\build\temp.win-amd64-cpython-311\Release\src/convert/flexible_dual_grid.obj: compile C$:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\src\convert\flexible_dual_grid.cpp
29
+ build C$:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\build\temp.win-amd64-cpython-311\Release\src/convert/volumetic_attr.obj: compile C$:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\src\convert\volumetic_attr.cpp
30
+ build C$:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\build\temp.win-amd64-cpython-311\Release\src/ext.obj: compile C$:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\src\ext.cpp
31
+ build C$:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\build\temp.win-amd64-cpython-311\Release\src/hash/hash.obj: cuda_compile C$:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\src\hash\hash.cu
32
+ build C$:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\build\temp.win-amd64-cpython-311\Release\src/io/filter_neighbor.obj: compile C$:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\src\io\filter_neighbor.cpp
33
+ build C$:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\build\temp.win-amd64-cpython-311\Release\src/io/filter_parent.obj: compile C$:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\src\io\filter_parent.cpp
34
+ build C$:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\build\temp.win-amd64-cpython-311\Release\src/io/svo.obj: compile C$:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\src\io\svo.cpp
35
+ build C$:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\build\temp.win-amd64-cpython-311\Release\src/rasterize/rasterize.obj: cuda_compile C$:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\src\rasterize\rasterize.cu
36
+ build C$:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\build\temp.win-amd64-cpython-311\Release\src/serialize/api.obj: cuda_compile C$:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\src\serialize\api.cu
37
+ build C$:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\build\temp.win-amd64-cpython-311\Release\src/serialize/hilbert.obj: cuda_compile C$:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\src\serialize\hilbert.cu
38
+ build C$:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\build\temp.win-amd64-cpython-311\Release\src/serialize/z_order.obj: cuda_compile C$:\Users\opsiclear\Desktop\projects\Trellis2_multi_image_conditioning\o-voxel\src\serialize\z_order.cu
39
+
40
+
41
+
42
+
43
+
44
+
45
+
46
+
o-voxel/build/temp.win-amd64-cpython-311/Release/src/convert/_C.cp311-win_amd64.exp ADDED
Binary file (25.2 kB). View file
 
o-voxel/build/temp.win-amd64-cpython-311/Release/src/convert/_C.cp311-win_amd64.lib ADDED
Binary file (42.9 kB). View file
 
o-voxel/build/temp.win-amd64-cpython-311/Release/src/convert/flexible_dual_grid.obj ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:199b03ff7c85fe0c41817df7fb0ac4b69ba5fced59da8298955daaad564dddd2
3
+ size 101177043
o-voxel/build/temp.win-amd64-cpython-311/Release/src/convert/volumetic_attr.obj ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:881ae0ab39efe22c1cadae7db8483ce3c076f66b6654a0d16d333e86540e3c84
3
+ size 54681553
o-voxel/build/temp.win-amd64-cpython-311/Release/src/ext.obj ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:25482cf27a40cae07b0d0cce67e03d390dafbb1a53bc668c0bd4ad03fc8a41c7
3
+ size 60112845
o-voxel/build/temp.win-amd64-cpython-311/Release/src/hash/hash.obj ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2e7aad51236896fdf39da8586753443ad460ba3827580120f0fa7e36a79c9fc2
3
+ size 3310522
o-voxel/build/temp.win-amd64-cpython-311/Release/src/io/filter_neighbor.obj ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6d1b9b3e1de6293d8a846594b4666662c7713c8656988343ec237760a23a8184
3
+ size 49303588
o-voxel/build/temp.win-amd64-cpython-311/Release/src/io/filter_parent.obj ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:be06f0928225aaa96f784d6c54e43cd248fc9077b3ff3761bab35c78188a5f91
3
+ size 49318336
o-voxel/build/temp.win-amd64-cpython-311/Release/src/io/svo.obj ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:84252f221ffdca2a21050f669078f59080986b7a623fbd4d4f25358c35c9e125
3
+ size 49340082
o-voxel/build/temp.win-amd64-cpython-311/Release/src/rasterize/rasterize.obj ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5c5b1d508b498de72395b0aee27f4d8db3f53463c5a3c5ec5f0f30b8652c5c3f
3
+ size 3082508
o-voxel/build/temp.win-amd64-cpython-311/Release/src/serialize/api.obj ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0b1750282d12d2693bda53c40f7ff1996e502116ddc5b49d9f73459120133302
3
+ size 3021052
o-voxel/build/temp.win-amd64-cpython-311/Release/src/serialize/hilbert.obj ADDED
Binary file (52 kB). View file
 
o-voxel/build/temp.win-amd64-cpython-311/Release/src/serialize/z_order.obj ADDED
Binary file (49.9 kB). View file
 
o-voxel/examples/mesh2ovox.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import o_voxel
3
+ import utils
4
+
5
+ RES = 512
6
+
7
+ asset = utils.get_helmet()
8
+
9
+ # 0. Normalize asset to unit cube
10
+ aabb = asset.bounding_box.bounds
11
+ center = (aabb[0] + aabb[1]) / 2
12
+ scale = 0.99999 / (aabb[1] - aabb[0]).max() # To avoid numerical issues
13
+ asset.apply_translation(-center)
14
+ asset.apply_scale(scale)
15
+
16
+ # 1. Geometry Voxelization (Flexible Dual Grid)
17
+ # Returns: occupied indices, dual vertices (QEF solution), and edge intersected
18
+ mesh = asset.to_mesh()
19
+ vertices = torch.from_numpy(mesh.vertices).float()
20
+ faces = torch.from_numpy(mesh.faces).long()
21
+ voxel_indices, dual_vertices, intersected = o_voxel.convert.mesh_to_flexible_dual_grid(
22
+ vertices, faces,
23
+ grid_size=RES, # Resolution
24
+ aabb=[[-0.5,-0.5,-0.5],[0.5,0.5,0.5]], # Axis-aligned bounding box
25
+ face_weight=1.0, # Face term weight in QEF
26
+ boundary_weight=0.2, # Boundary term weight in QEF
27
+ regularization_weight=1e-2, # Regularization term weight in QEF
28
+ timing=True
29
+ )
30
+ ## sort to ensure align between geometry and material voxelization
31
+ vid = o_voxel.serialize.encode_seq(voxel_indices)
32
+ mapping = torch.argsort(vid)
33
+ voxel_indices = voxel_indices[mapping]
34
+ dual_vertices = dual_vertices[mapping]
35
+ intersected = intersected[mapping]
36
+
37
+ # 2. Material Voxelization (Volumetric Attributes)
38
+ # Returns: dict containing 'base_color', 'metallic', 'roughness', etc.
39
+ voxel_indices_mat, attributes = o_voxel.convert.textured_mesh_to_volumetric_attr(
40
+ asset,
41
+ grid_size=RES,
42
+ aabb=[[-0.5,-0.5,-0.5],[0.5,0.5,0.5]],
43
+ timing=True
44
+ )
45
+ ## sort to ensure align between geometry and material voxelization
46
+ vid_mat = o_voxel.serialize.encode_seq(voxel_indices_mat)
47
+ mapping_mat = torch.argsort(vid_mat)
48
+ attributes = {k: v[mapping_mat] for k, v in attributes.items()}
49
+
50
+ # Save to compressed .vxz format
51
+ ## packing
52
+ dual_vertices = dual_vertices * RES - voxel_indices
53
+ dual_vertices = (torch.clamp(dual_vertices, 0, 1) * 255).type(torch.uint8)
54
+ intersected = (intersected[:, 0:1] + 2 * intersected[:, 1:2] + 4 * intersected[:, 2:3]).type(torch.uint8)
55
+ attributes['dual_vertices'] = dual_vertices
56
+ attributes['intersected'] = intersected
57
+ o_voxel.io.write("ovoxel_helmet.vxz", voxel_indices, attributes)
o-voxel/examples/ovox2glb.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import o_voxel
3
+
4
+ RES = 512
5
+
6
+ # Load data
7
+ coords, data = o_voxel.io.read("ovoxel_helmet.vxz")
8
+ dual_vertices = data['dual_vertices']
9
+ intersected = data['intersected']
10
+ base_color = data['base_color']
11
+ metallic = data['metallic']
12
+ roughness = data['roughness']
13
+ alpha = data['alpha']
14
+
15
+ # Depack
16
+ dual_vertices = dual_vertices / 255
17
+ intersected = torch.cat([
18
+ intersected % 2,
19
+ intersected // 2 % 2,
20
+ intersected // 4 % 2,
21
+ ], dim=-1).bool()
22
+
23
+ # Extract Mesh
24
+ # O-Voxel connects dual vertices to form quads, optionally splitting them
25
+ # based on geometric features.
26
+ rec_verts, rec_faces = o_voxel.convert.flexible_dual_grid_to_mesh(
27
+ coords.cuda(),
28
+ dual_vertices.cuda(),
29
+ intersected.cuda(),
30
+ split_weight=None, # Auto-split based on min angle if None
31
+ grid_size=RES,
32
+ aabb=[[-0.5,-0.5,-0.5],[0.5,0.5,0.5]],
33
+ )
34
+
35
+ # Post-process
36
+ attr_volume = torch.cat([base_color.cuda(), metallic.cuda(), roughness.cuda(), alpha.cuda()], dim=-1) / 255
37
+ attr_layout = {'base_color': slice(0,3), 'metallic': slice(3,4), 'roughness': slice(4,5), 'alpha': slice(5,6)}
38
+ mesh = o_voxel.postprocess.to_glb(
39
+ vertices=rec_verts,
40
+ faces=rec_faces,
41
+ attr_volume=attr_volume,
42
+ coords=coords.cuda(),
43
+ attr_layout=attr_layout,
44
+ grid_size=RES,
45
+ aabb=[[-0.5,-0.5,-0.5],[0.5,0.5,0.5]],
46
+ decimation_target=100000,
47
+ texture_size=2048,
48
+ verbose=True,
49
+ )
50
+
51
+ # Save as glb
52
+ mesh.export("rec_helmet.glb")
o-voxel/examples/ovox2mesh.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import o_voxel
3
+ import trimesh
4
+ import trimesh.visual
5
+
6
+ RES = 512
7
+
8
+ # Load data
9
+ coords, data = o_voxel.io.read("ovoxel_helmet.vxz")
10
+ dual_vertices = data['dual_vertices']
11
+ intersected = data['intersected']
12
+ base_color = data['base_color']
13
+ metallic = data['metallic']
14
+ roughness = data['roughness']
15
+ alpha = data['alpha']
16
+
17
+ # Depack
18
+ dual_vertices = dual_vertices / 255
19
+ intersected = torch.cat([
20
+ intersected % 2,
21
+ intersected // 2 % 2,
22
+ intersected // 4 % 2,
23
+ ], dim=-1).bool()
24
+
25
+ # Extract Mesh
26
+ # O-Voxel connects dual vertices to form quads, optionally splitting them
27
+ # based on geometric features.
28
+ rec_verts, rec_faces = o_voxel.convert.flexible_dual_grid_to_mesh(
29
+ coords.cuda(),
30
+ dual_vertices.cuda(),
31
+ intersected.cuda(),
32
+ split_weight=None, # Auto-split based on min angle if None
33
+ grid_size=RES,
34
+ aabb=[[-0.5,-0.5,-0.5],[0.5,0.5,0.5]],
35
+ )
36
+
37
+ # Save as ply
38
+ visual = trimesh.visual.ColorVisuals(
39
+ vertex_colors=base_color,
40
+ )
41
+ mesh = trimesh.Trimesh(
42
+ vertices=rec_verts.cpu(), faces=rec_faces.cpu(), visual=visual,
43
+ process=False
44
+ )
45
+ mesh.export("rec_helmet.ply")
o-voxel/examples/render_ovox.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import numpy as np
3
+ import imageio
4
+ import o_voxel
5
+ import utils3d
6
+
7
+ RES = 512
8
+
9
+ # Load data
10
+ coords, data = o_voxel.io.read("ovoxel_helmet.vxz")
11
+ position = (coords / RES - 0.5).cuda()
12
+ base_color = (data['base_color'] / 255).cuda()
13
+
14
+ # Setup camera
15
+ extr = utils3d.extrinsics_look_at(
16
+ eye=torch.tensor([1.2, 0.5, 1.2]),
17
+ look_at=torch.tensor([0.0, 0.0, 0.0]),
18
+ up=torch.tensor([0.0, 1.0, 0.0])
19
+ ).cuda()
20
+ intr = utils3d.intrinsics_from_fov_xy(
21
+ fov_x=torch.deg2rad(torch.tensor(45.0)),
22
+ fov_y=torch.deg2rad(torch.tensor(45.0)),
23
+ ).cuda()
24
+
25
+ # Render
26
+ renderer = o_voxel.rasterize.VoxelRenderer(
27
+ rendering_options={"resolution": 512, "ssaa": 2}
28
+ )
29
+ output = renderer.render(
30
+ position=position, # Voxel centers
31
+ attrs=base_color, # Color/Opacity etc.
32
+ voxel_size=1.0/RES,
33
+ extrinsics=extr,
34
+ intrinsics=intr
35
+ )
36
+ image = np.clip(
37
+ output.attr.permute(1, 2, 0).cpu().numpy() * 255, 0, 255
38
+ ).astype(np.uint8)
39
+ imageio.imwrite("ovoxel_helmet_visualization.png", image)
o-voxel/examples/utils.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import tarfile
4
+ import trimesh
5
+
6
+ HELMET_URL = "https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/refs/heads/main/2.0/DamagedHelmet/glTF-Binary/DamagedHelmet.glb"
7
+ CACHE_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), "cache")
8
+
9
+
10
+ def download_file(url, path):
11
+ print(f"Downloading from {url} ...")
12
+ resp = requests.get(url, stream=True)
13
+ resp.raise_for_status()
14
+
15
+ with open(path, "wb") as f:
16
+ for chunk in resp.iter_content(chunk_size=8192):
17
+ f.write(chunk)
18
+
19
+ print(f"Saved to {path}")
20
+
21
+
22
+ def get_helmet() -> trimesh.Trimesh:
23
+ HELMET_PATH = os.path.join(CACHE_DIR, "helmet.glb")
24
+ if not os.path.exists(HELMET_PATH):
25
+ os.makedirs(CACHE_DIR, exist_ok=True)
26
+ download_file(HELMET_URL, HELMET_PATH)
27
+ return trimesh.load(HELMET_PATH)
o-voxel/o_voxel.egg-info/PKG-INFO ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Metadata-Version: 2.1
2
+ Name: o_voxel
3
+ Version: 0.0.1
4
+ Summary: All about voxel.
5
+ Author-email: Jianfeng Xiang <belljig@outlook.com>
6
+ Requires-Python: >=3.8
7
+ Requires-Dist: torch
8
+ Requires-Dist: numpy
9
+ Requires-Dist: plyfile
10
+ Requires-Dist: trimesh
11
+ Requires-Dist: tqdm
12
+ Requires-Dist: zstandard
13
+ Requires-Dist: easydict
14
+ Requires-Dist: cumesh@ git+https://github.com/JeffreyXiang/CuMesh.git
15
+ Requires-Dist: flex_gemm@ git+https://github.com/JeffreyXiang/FlexGEMM.git
o-voxel/o_voxel.egg-info/SOURCES.txt ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ README.md
2
+ pyproject.toml
3
+ setup.py
4
+ o_voxel/__init__.py
5
+ o_voxel/postprocess.py
6
+ o_voxel/rasterize.py
7
+ o_voxel/serialize.py
8
+ o_voxel.egg-info/PKG-INFO
9
+ o_voxel.egg-info/SOURCES.txt
10
+ o_voxel.egg-info/dependency_links.txt
11
+ o_voxel.egg-info/requires.txt
12
+ o_voxel.egg-info/top_level.txt
13
+ o_voxel/convert/__init__.py
14
+ o_voxel/convert/flexible_dual_grid.py
15
+ o_voxel/convert/volumetic_attr.py
16
+ o_voxel/io/__init__.py
17
+ o_voxel/io/npz.py
18
+ o_voxel/io/ply.py
19
+ o_voxel/io/vxz.py
20
+ src/ext.cpp
21
+ src/convert/flexible_dual_grid.cpp
22
+ src/convert/volumetic_attr.cpp
23
+ src/hash/hash.cu
24
+ src/io/filter_neighbor.cpp
25
+ src/io/filter_parent.cpp
26
+ src/io/svo.cpp
27
+ src/rasterize/rasterize.cu
28
+ src/serialize/api.cu
29
+ src/serialize/hilbert.cu
30
+ src/serialize/z_order.cu
o-voxel/o_voxel.egg-info/dependency_links.txt ADDED
@@ -0,0 +1 @@
 
 
1
+
o-voxel/o_voxel.egg-info/requires.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ torch
2
+ numpy
3
+ plyfile
4
+ trimesh
5
+ tqdm
6
+ zstandard
7
+ easydict
8
+ cumesh@ git+https://github.com/JeffreyXiang/CuMesh.git
9
+ flex_gemm@ git+https://github.com/JeffreyXiang/FlexGEMM.git
o-voxel/o_voxel.egg-info/top_level.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ o_voxel
o-voxel/o_voxel/__init__.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ from . import (
2
+ convert,
3
+ io,
4
+ postprocess,
5
+ rasterize,
6
+ serialize
7
+ )
o-voxel/o_voxel/convert/__init__.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ from .flexible_dual_grid import *
2
+ from .volumetic_attr import *
o-voxel/o_voxel/convert/flexible_dual_grid.py ADDED
@@ -0,0 +1,283 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import *
2
+ import numpy as np
3
+ import torch
4
+ from .. import _C
5
+
6
+ __all__ = [
7
+ "mesh_to_flexible_dual_grid",
8
+ "flexible_dual_grid_to_mesh",
9
+ ]
10
+
11
+
12
+ def _init_hashmap(grid_size, capacity, device):
13
+ VOL = (grid_size[0] * grid_size[1] * grid_size[2]).item()
14
+
15
+ # If the number of elements in the tensor is less than 2^32, use uint32 as the hashmap type, otherwise use uint64.
16
+ if VOL < 2**32:
17
+ hashmap_keys = torch.full((capacity,), torch.iinfo(torch.uint32).max, dtype=torch.uint32, device=device)
18
+ elif VOL < 2**64:
19
+ hashmap_keys = torch.full((capacity,), torch.iinfo(torch.uint64).max, dtype=torch.uint64, device=device)
20
+ else:
21
+ raise ValueError(f"The spatial size is too large to fit in a hashmap. Get volumn {VOL} > 2^64.")
22
+
23
+ hashmap_vals = torch.empty((capacity,), dtype=torch.uint32, device=device)
24
+
25
+ return hashmap_keys, hashmap_vals
26
+
27
+
28
+ @torch.no_grad()
29
+ def mesh_to_flexible_dual_grid(
30
+ vertices: torch.Tensor,
31
+ faces: torch.Tensor,
32
+ voxel_size: Union[float, list, tuple, np.ndarray, torch.Tensor] = None,
33
+ grid_size: Union[int, list, tuple, np.ndarray, torch.Tensor] = None,
34
+ aabb: Union[list, tuple, np.ndarray, torch.Tensor] = None,
35
+ face_weight: float = 1.0,
36
+ boundary_weight: float = 1.0,
37
+ regularization_weight: float = 0.1,
38
+ timing: bool = False,
39
+ ) -> Union[torch.Tensor, torch.Tensor, torch.Tensor]:
40
+ """
41
+ Voxelize a mesh into a sparse voxel grid.
42
+
43
+ Args:
44
+ vertices (torch.Tensor): The vertices of the mesh.
45
+ faces (torch.Tensor): The faces of the mesh.
46
+ voxel_size (float, list, tuple, np.ndarray, torch.Tensor): The size of each voxel.
47
+ grid_size (int, list, tuple, np.ndarray, torch.Tensor): The size of the grid.
48
+ NOTE: One of voxel_size and grid_size must be provided.
49
+ aabb (list, tuple, np.ndarray, torch.Tensor): The axis-aligned bounding box of the mesh.
50
+ If not provided, it will be computed automatically.
51
+ face_weight (float): The weight of the face term in the QEF when solving the dual vertices.
52
+ boundary_weight (float): The weight of the boundary term in the QEF when solving the dual vertices.
53
+ regularization_weight (float): The weight of the regularization term in the QEF when solving the dual vertices.
54
+ timing (bool): Whether to time the voxelization process.
55
+
56
+ Returns:
57
+ torch.Tensor: The indices of the voxels that are occupied by the mesh.
58
+ The shape of the tensor is (N, 3), where N is the number of occupied voxels.
59
+ torch.Tensor: The dual vertices of the mesh.
60
+ torch.Tensor: The intersected flag of each voxel.
61
+ """
62
+
63
+ # Load mesh
64
+ vertices = vertices.float()
65
+ faces = faces.int()
66
+
67
+ # Voxelize settings
68
+ assert voxel_size is not None or grid_size is not None, "Either voxel_size or grid_size must be provided"
69
+
70
+ if voxel_size is not None:
71
+ if isinstance(voxel_size, float):
72
+ voxel_size = [voxel_size, voxel_size, voxel_size]
73
+ if isinstance(voxel_size, (list, tuple)):
74
+ voxel_size = np.array(voxel_size)
75
+ if isinstance(voxel_size, np.ndarray):
76
+ voxel_size = torch.tensor(voxel_size, dtype=torch.float32)
77
+ assert isinstance(voxel_size, torch.Tensor), f"voxel_size must be a float, list, tuple, np.ndarray, or torch.Tensor, but got {type(voxel_size)}"
78
+ assert voxel_size.dim() == 1, f"voxel_size must be a 1D tensor, but got {voxel_size.shape}"
79
+ assert voxel_size.size(0) == 3, f"voxel_size must have 3 elements, but got {voxel_size.size(0)}"
80
+
81
+ if grid_size is not None:
82
+ if isinstance(grid_size, int):
83
+ grid_size = [grid_size, grid_size, grid_size]
84
+ if isinstance(grid_size, (list, tuple)):
85
+ grid_size = np.array(grid_size)
86
+ if isinstance(grid_size, np.ndarray):
87
+ grid_size = torch.tensor(grid_size, dtype=torch.int32)
88
+ assert isinstance(grid_size, torch.Tensor), f"grid_size must be an int, list, tuple, np.ndarray, or torch.Tensor, but got {type(grid_size)}"
89
+ assert grid_size.dim() == 1, f"grid_size must be a 1D tensor, but got {grid_size.shape}"
90
+ assert grid_size.size(0) == 3, f"grid_size must have 3 elements, but got {grid_size.size(0)}"
91
+
92
+ if aabb is not None:
93
+ if isinstance(aabb, (list, tuple)):
94
+ aabb = np.array(aabb)
95
+ if isinstance(aabb, np.ndarray):
96
+ aabb = torch.tensor(aabb, dtype=torch.float32)
97
+ assert isinstance(aabb, torch.Tensor), f"aabb must be a list, tuple, np.ndarray, or torch.Tensor, but got {type(aabb)}"
98
+ assert aabb.dim() == 2, f"aabb must be a 2D tensor, but got {aabb.shape}"
99
+ assert aabb.size(0) == 2, f"aabb must have 2 rows, but got {aabb.size(0)}"
100
+ assert aabb.size(1) == 3, f"aabb must have 3 columns, but got {aabb.size(1)}"
101
+
102
+ # Auto adjust aabb
103
+ if aabb is None:
104
+ min_xyz = vertices.min(dim=0).values
105
+ max_xyz = vertices.max(dim=0).values
106
+
107
+ if voxel_size is not None:
108
+ padding = torch.ceil((max_xyz - min_xyz) / voxel_size) * voxel_size - (max_xyz - min_xyz)
109
+ min_xyz -= padding * 0.5
110
+ max_xyz += padding * 0.5
111
+ if grid_size is not None:
112
+ padding = (max_xyz - min_xyz) / (grid_size - 1)
113
+ min_xyz -= padding * 0.5
114
+ max_xyz += padding * 0.5
115
+
116
+ aabb = torch.stack([min_xyz, max_xyz], dim=0).float().cuda()
117
+
118
+ # Fill voxel size or grid size
119
+ if voxel_size is None:
120
+ voxel_size = (aabb[1] - aabb[0]) / grid_size
121
+ if grid_size is None:
122
+ grid_size = ((aabb[1] - aabb[0]) / voxel_size).round().int()
123
+
124
+ # subdivide mesh
125
+ vertices = vertices - aabb[0].reshape(1, 3)
126
+ grid_range = torch.stack([torch.zeros_like(grid_size), grid_size], dim=0).int()
127
+
128
+ ret = _C.mesh_to_flexible_dual_grid_cpu(
129
+ vertices,
130
+ faces,
131
+ voxel_size,
132
+ grid_range,
133
+ face_weight,
134
+ boundary_weight,
135
+ regularization_weight,
136
+ timing,
137
+ )
138
+
139
+ return ret
140
+
141
+
142
+ def flexible_dual_grid_to_mesh(
143
+ coords: torch.Tensor,
144
+ dual_vertices: torch.Tensor,
145
+ intersected_flag: torch.Tensor,
146
+ split_weight: Union[torch.Tensor, None],
147
+ aabb: Union[list, tuple, np.ndarray, torch.Tensor],
148
+ voxel_size: Union[float, list, tuple, np.ndarray, torch.Tensor] = None,
149
+ grid_size: Union[int, list, tuple, np.ndarray, torch.Tensor] = None,
150
+ train: bool = False,
151
+ ):
152
+ """
153
+ Extract mesh from sparse voxel structures using flexible dual grid.
154
+
155
+ Args:
156
+ coords (torch.Tensor): The coordinates of the voxels.
157
+ dual_vertices (torch.Tensor): The dual vertices.
158
+ intersected_flag (torch.Tensor): The intersected flag.
159
+ split_weight (torch.Tensor): The split weight of each dual quad. If None, the algorithm
160
+ will split based on minimum angle.
161
+ aabb (list, tuple, np.ndarray, torch.Tensor): The axis-aligned bounding box of the mesh.
162
+ voxel_size (float, list, tuple, np.ndarray, torch.Tensor): The size of each voxel.
163
+ grid_size (int, list, tuple, np.ndarray, torch.Tensor): The size of the grid.
164
+ NOTE: One of voxel_size and grid_size must be provided.
165
+ train (bool): Whether to use training mode.
166
+
167
+ Returns:
168
+ vertices (torch.Tensor): The vertices of the mesh.
169
+ faces (torch.Tensor): The faces of the mesh.
170
+ """
171
+ # Static variables
172
+ if not hasattr(flexible_dual_grid_to_mesh, "edge_neighbor_voxel_offset"):
173
+ flexible_dual_grid_to_mesh.edge_neighbor_voxel_offset = torch.tensor([
174
+ [[0, 0, 0], [0, 0, 1], [0, 1, 1], [0, 1, 0]], # x-axis
175
+ [[0, 0, 0], [1, 0, 0], [1, 0, 1], [0, 0, 1]], # y-axis
176
+ [[0, 0, 0], [0, 1, 0], [1, 1, 0], [1, 0, 0]], # z-axis
177
+ ], dtype=torch.int, device=coords.device).unsqueeze(0)
178
+ if not hasattr(flexible_dual_grid_to_mesh, "quad_split_1"):
179
+ flexible_dual_grid_to_mesh.quad_split_1 = torch.tensor([0, 1, 2, 0, 2, 3], dtype=torch.long, device=coords.device, requires_grad=False)
180
+ if not hasattr(flexible_dual_grid_to_mesh, "quad_split_2"):
181
+ flexible_dual_grid_to_mesh.quad_split_2 = torch.tensor([0, 1, 3, 3, 1, 2], dtype=torch.long, device=coords.device, requires_grad=False)
182
+ if not hasattr(flexible_dual_grid_to_mesh, "quad_split_train"):
183
+ flexible_dual_grid_to_mesh.quad_split_train = torch.tensor([0, 1, 4, 1, 2, 4, 2, 3, 4, 3, 0, 4], dtype=torch.long, device=coords.device, requires_grad=False)
184
+
185
+ # AABB
186
+ if isinstance(aabb, (list, tuple)):
187
+ aabb = np.array(aabb)
188
+ if isinstance(aabb, np.ndarray):
189
+ aabb = torch.tensor(aabb, dtype=torch.float32, device=coords.device)
190
+ assert isinstance(aabb, torch.Tensor), f"aabb must be a list, tuple, np.ndarray, or torch.Tensor, but got {type(aabb)}"
191
+ assert aabb.dim() == 2, f"aabb must be a 2D tensor, but got {aabb.shape}"
192
+ assert aabb.size(0) == 2, f"aabb must have 2 rows, but got {aabb.size(0)}"
193
+ assert aabb.size(1) == 3, f"aabb must have 3 columns, but got {aabb.size(1)}"
194
+
195
+ # Voxel size
196
+ if voxel_size is not None:
197
+ if isinstance(voxel_size, float):
198
+ voxel_size = [voxel_size, voxel_size, voxel_size]
199
+ if isinstance(voxel_size, (list, tuple)):
200
+ voxel_size = np.array(voxel_size)
201
+ if isinstance(voxel_size, np.ndarray):
202
+ voxel_size = torch.tensor(voxel_size, dtype=torch.float32, device=coords.device)
203
+ grid_size = ((aabb[1] - aabb[0]) / voxel_size).round().int()
204
+ else:
205
+ assert grid_size is not None, "Either voxel_size or grid_size must be provided"
206
+ if isinstance(grid_size, int):
207
+ grid_size = [grid_size, grid_size, grid_size]
208
+ if isinstance(grid_size, (list, tuple)):
209
+ grid_size = np.array(grid_size)
210
+ if isinstance(grid_size, np.ndarray):
211
+ grid_size = torch.tensor(grid_size, dtype=torch.int32, device=coords.device)
212
+ voxel_size = (aabb[1] - aabb[0]) / grid_size
213
+ assert isinstance(voxel_size, torch.Tensor), f"voxel_size must be a float, list, tuple, np.ndarray, or torch.Tensor, but got {type(voxel_size)}"
214
+ assert voxel_size.dim() == 1, f"voxel_size must be a 1D tensor, but got {voxel_size.shape}"
215
+ assert voxel_size.size(0) == 3, f"voxel_size must have 3 elements, but got {voxel_size.size(0)}"
216
+ assert isinstance(grid_size, torch.Tensor), f"grid_size must be an int, list, tuple, np.ndarray, or torch.Tensor, but got {type(grid_size)}"
217
+ assert grid_size.dim() == 1, f"grid_size must be a 1D tensor, but got {grid_size.shape}"
218
+ assert grid_size.size(0) == 3, f"grid_size must have 3 elements, but got {grid_size.size(0)}"
219
+
220
+ # Extract mesh
221
+ N = dual_vertices.shape[0]
222
+ mesh_vertices = (coords.float() + dual_vertices) / (2 * N) - 0.5
223
+
224
+ # Store active voxels into hashmap
225
+ hashmap = _init_hashmap(grid_size, 2 * N, device=coords.device)
226
+ _C.hashmap_insert_3d_idx_as_val_cuda(*hashmap, torch.cat([torch.zeros_like(coords[:, :1]), coords], dim=-1), *grid_size.tolist())
227
+
228
+ # Find connected voxels
229
+ edge_neighbor_voxel = coords.reshape(N, 1, 1, 3) + flexible_dual_grid_to_mesh.edge_neighbor_voxel_offset # (N, 3, 4, 3)
230
+ connected_voxel = edge_neighbor_voxel[intersected_flag] # (M, 4, 3)
231
+ M = connected_voxel.shape[0]
232
+ connected_voxel_hash_key = torch.cat([
233
+ torch.zeros((M * 4, 1), dtype=torch.int, device=coords.device),
234
+ connected_voxel.reshape(-1, 3)
235
+ ], dim=1)
236
+ connected_voxel_indices = _C.hashmap_lookup_3d_cuda(*hashmap, connected_voxel_hash_key, *grid_size.tolist()).reshape(M, 4).int()
237
+ connected_voxel_valid = (connected_voxel_indices != 0xffffffff).all(dim=1)
238
+ quad_indices = connected_voxel_indices[connected_voxel_valid].int() # (L, 4)
239
+ L = quad_indices.shape[0]
240
+
241
+ # Construct triangles
242
+ if not train:
243
+ mesh_vertices = (coords.float() + dual_vertices) * voxel_size + aabb[0].reshape(1, 3)
244
+ if split_weight is None:
245
+ # if split 1
246
+ atempt_triangles_0 = quad_indices[:, flexible_dual_grid_to_mesh.quad_split_1]
247
+ normals0 = torch.cross(mesh_vertices[atempt_triangles_0[:, 1]] - mesh_vertices[atempt_triangles_0[:, 0]], mesh_vertices[atempt_triangles_0[:, 2]] - mesh_vertices[atempt_triangles_0[:, 0]])
248
+ normals1 = torch.cross(mesh_vertices[atempt_triangles_0[:, 2]] - mesh_vertices[atempt_triangles_0[:, 1]], mesh_vertices[atempt_triangles_0[:, 3]] - mesh_vertices[atempt_triangles_0[:, 1]])
249
+ align0 = (normals0 * normals1).sum(dim=1, keepdim=True).abs()
250
+ # if split 2
251
+ atempt_triangles_1 = quad_indices[:, flexible_dual_grid_to_mesh.quad_split_2]
252
+ normals0 = torch.cross(mesh_vertices[atempt_triangles_1[:, 1]] - mesh_vertices[atempt_triangles_1[:, 0]], mesh_vertices[atempt_triangles_1[:, 2]] - mesh_vertices[atempt_triangles_1[:, 0]])
253
+ normals1 = torch.cross(mesh_vertices[atempt_triangles_1[:, 2]] - mesh_vertices[atempt_triangles_1[:, 1]], mesh_vertices[atempt_triangles_1[:, 3]] - mesh_vertices[atempt_triangles_1[:, 1]])
254
+ align1 = (normals0 * normals1).sum(dim=1, keepdim=True).abs()
255
+ # select split
256
+ mesh_triangles = torch.where(align0 > align1, atempt_triangles_0, atempt_triangles_1).reshape(-1, 3)
257
+ else:
258
+ split_weight_ws = split_weight[quad_indices]
259
+ split_weight_ws_02 = split_weight_ws[:, 0] * split_weight_ws[:, 2]
260
+ split_weight_ws_13 = split_weight_ws[:, 1] * split_weight_ws[:, 3]
261
+ mesh_triangles = torch.where(
262
+ split_weight_ws_02 > split_weight_ws_13,
263
+ quad_indices[:, flexible_dual_grid_to_mesh.quad_split_1],
264
+ quad_indices[:, flexible_dual_grid_to_mesh.quad_split_2]
265
+ ).reshape(-1, 3)
266
+ else:
267
+ assert split_weight is not None, "split_weight must be provided in training mode"
268
+ mesh_vertices = (coords.float() + dual_vertices) * voxel_size + aabb[0].reshape(1, 3)
269
+ quad_vs = mesh_vertices[quad_indices]
270
+ mean_v02 = (quad_vs[:, 0] + quad_vs[:, 2]) / 2
271
+ mean_v13 = (quad_vs[:, 1] + quad_vs[:, 3]) / 2
272
+ split_weight_ws = split_weight[quad_indices]
273
+ split_weight_ws_02 = split_weight_ws[:, 0] * split_weight_ws[:, 2]
274
+ split_weight_ws_13 = split_weight_ws[:, 1] * split_weight_ws[:, 3]
275
+ mid_vertices = (
276
+ split_weight_ws_02 * mean_v02 +
277
+ split_weight_ws_13 * mean_v13
278
+ ) / (split_weight_ws_02 + split_weight_ws_13)
279
+ mesh_vertices = torch.cat([mesh_vertices, mid_vertices], dim=0)
280
+ quad_indices = torch.cat([quad_indices, torch.arange(N, N + L, device='cuda').unsqueeze(1)], dim=1)
281
+ mesh_triangles = quad_indices[:, flexible_dual_grid_to_mesh.quad_split_train].reshape(-1, 3)
282
+
283
+ return mesh_vertices, mesh_triangles