Download utils/data_utils.py from MayMeHappy/mul: direct link, hf CLI and curl.
- Browser
- Download file 1.14 kB
-
https://huggingface.co/MayMeHappy/mul/resolve/main/utils/data_utils.py
- Command line
-
hf download hf://MayMeHappy/mul/utils/data_utils.py
-
curl -L -o data_utils.py https://huggingface.co/MayMeHappy/mul/resolve/main/utils/data_utils.py
1.14 kB
| import numpy as np | |
| import trimesh | |
| def discretize( | |
| t, | |
| continuous_range = (-1.0, 1.0), | |
| num_discrete: int = 128 | |
| ): | |
| lo, hi = continuous_range | |
| t = (t - lo) / (hi - lo) | |
| t *= num_discrete | |
| t -= 0.5 | |
| return t.round().astype(np.int32).clip(min = 0, max = num_discrete - 1) | |
| def undiscretize( | |
| t, | |
| continuous_range = (-1.0, 1.0), | |
| num_discrete: int = 128 | |
| ): | |
| lo, hi = continuous_range | |
| t = t + 0.5 | |
| t /= num_discrete | |
| return t * (hi - lo) + lo | |
| def normalize(mesh, eps: float = 1e-8): | |
| bounds = np.array([mesh.vertices.min(axis=0), mesh.vertices.max(axis=0)]) | |
| mesh.vertices = mesh.vertices - (bounds[0] + bounds[1])[None, :] / 2 | |
| # 数据中存在退化 mesh (bounds 范围为 0), 分母加 eps 避免除零产生 inf/NaN | |
| scale = (bounds[1] - bounds[0]).max() + eps | |
| mesh.vertices = 2 * (mesh.vertices / scale) | |
| mesh.vertices = mesh.vertices.clip(-1.0, 1.0) # 限制顶点坐标在[-1.0, 1.0] | |
| def clean_mesh(mesh): | |
| mesh.merge_vertices() | |
| mesh.update_faces(mesh.nondegenerate_faces()) | |
| mesh.update_faces(mesh.unique_faces()) | |
| mesh.remove_unreferenced_vertices() |