File size: 1,138 Bytes
90ca518
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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()