-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
180 lines (147 loc) · 7.74 KB
/
test.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import pypgo
import numpy as np
import trimesh
import os
import tetgen
def get_surface_vf(faces):
# get surface faces
org_triangles = np.vstack(
[
faces[:, [1, 2, 3]],
faces[:, [0, 3, 2]],
faces[:, [0, 1, 3]],
faces[:, [0, 2, 1]],
]
)
# Sort each triangle's vertices to avoid duplicates due to ordering
triangles = np.sort(org_triangles, axis=1)
unique_triangles, tri_idx, counts = np.unique(
triangles, axis=0, return_index=True, return_counts=True
)
once_tri_id = counts == 1
surface_triangles = unique_triangles[once_tri_id]
surface_vertices = np.unique(surface_triangles)
vertex_mapping = {vertex_id: i for i,
vertex_id in enumerate(surface_vertices)}
mapped_triangles = np.vectorize(vertex_mapping.get)(
org_triangles[tri_idx][once_tri_id]
)
return surface_vertices, mapped_triangles
def matching(save_folder):
density = 1000
E = 500000 # e547b70f8bb549e3ad5cea9a4eed800d
nu = 0.45
tetmesh = pypgo.create_tetmesh_from_file(os.path.join(save_folder, "reconstruct_res.veg"))
tet_vtx = pypgo.get_tetmesh_vertex_positions(tetmesh)
tet_elem = pypgo.get_tetmesh_element_indices(tetmesh)
tetmesh = pypgo.create_tetmesh(tet_vtx.flatten(), tet_elem.flatten(), E, nu, density)
pypgo.save_tetmesh_to_file(tetmesh, os.path.join(save_folder, "inverse_opt_init.veg"))
surf_vid, surf_fid = get_surface_vf(tet_elem)
surfmesh_init = trimesh.Trimesh(
vertices=tet_vtx[surf_vid], faces=surf_fid)
surfmesh_init.export(os.path.join(save_folder, "inverse_opt_init.obj"))
# get fixed vtx
os.makedirs(save_folder, exist_ok=True)
min_y = np.min(tet_vtx[:, 2])
fixed_vertices = np.where(tet_vtx[:, 2] < min_y + 5e-3)[0]
fixed_vtx_file = os.path.join(save_folder, "fixed.txt")
np.savetxt(fixed_vtx_file, fixed_vertices, fmt='%d')
# static eq w/o opt
tetmesh_static_eq = pypgo.run_quasi_static_sim(os.path.join(
save_folder, "inverse_opt_init.veg"), fixed_vtx_file, os.path.join(save_folder, "static_eq_debug.veg"))
tetmesh_static_eq_vtx = pypgo.get_tetmesh_vertex_positions(
tetmesh_static_eq)
surfmesh_static_eq = trimesh.Trimesh(
vertices=tetmesh_static_eq_vtx[surf_vid], faces=surf_fid)
surfmesh_static_eq.export(os.path.join(
save_folder, "static_eq_debug.obj"))
## inverse opt
step_size = 0.2
verbose = 0
pypgo.inverse_plasticity_opt(os.path.join(save_folder, "inverse_opt_init.veg"), fixed_vtx_file, save_folder, step_size, verbose)
# save surface meshes
tetmesh_opt = pypgo.create_tetmesh_from_file(os.path.join(save_folder, "opt.veg"))
tetmesh_opt_vtx = pypgo.get_tetmesh_vertex_positions(tetmesh_opt)
tetmesh_opt_elem = pypgo.get_tetmesh_element_indices(tetmesh_opt)
surf_vid, surf_fid = get_surface_vf(tetmesh_opt_elem)
surfmesh_opt = trimesh.Trimesh(
vertices=tetmesh_opt_vtx[surf_vid], faces=surf_fid)
surfmesh_opt.export(os.path.join(save_folder, "opt.obj"))
tetmesh_opt_rest = pypgo.create_tetmesh_from_file(os.path.join(save_folder, "opt_rest.veg"))
tetmesh_opt_rest_vtx = pypgo.get_tetmesh_vertex_positions(tetmesh_opt_rest)
tetmesh_opt_rest_elem = pypgo.get_tetmesh_element_indices(tetmesh_opt_rest)
surf_vid, surf_fid = get_surface_vf(tetmesh_opt_rest_elem)
surfmesh_opt_rest = trimesh.Trimesh(
vertices=tetmesh_opt_rest_vtx[surf_vid], faces=surf_fid)
surfmesh_opt_rest.export(os.path.join(save_folder, "opt_rest.obj"))
def stablize(save_folder):
density = 1000
E = 100000000 # unicorn
nu = 0.45
tetmesh = pypgo.create_tetmesh_from_file(os.path.join(save_folder, "reconstruct_res.veg"))
tet_vtx = pypgo.get_tetmesh_vertex_positions(tetmesh)
tet_elem = pypgo.get_tetmesh_element_indices(tetmesh)
tetmesh = pypgo.create_tetmesh(tet_vtx.flatten(), tet_elem.flatten(), E, nu, density)
pypgo.save_tetmesh_to_file(tetmesh, os.path.join(save_folder, "make_it_stand_init.veg"))
surf_vid, surf_fid = get_surface_vf(tet_elem)
surfmesh_init = trimesh.Trimesh(
vertices=tet_vtx[surf_vid], faces=surf_fid)
surfmesh_init.export(os.path.join(save_folder, "make_it_stand_init.obj"))
#### flatten base
### only for unicorn
surfmesh_init = trimesh.load(os.path.join(save_folder, "make_it_stand_init.obj"))
# rotate surfmesh around x axis by -90 degree
surfmesh_init.apply_transform(trimesh.transformations.rotation_matrix(
np.radians(-90), [1, 0, 0]))
surfmesh_init.export(os.path.join(save_folder, "make_it_stand_init.obj"))
init_trimesh = trimesh.load_mesh(os.path.join(save_folder, "make_it_stand_init.obj"))
init_trimeshgeo = pypgo.create_trimeshgeo(init_trimesh.vertices.flatten(), init_trimesh.faces.flatten())
min_z = pypgo.stablity_preprocess(init_trimeshgeo, os.path.join(save_folder, "make_it_stand_init_flattened.obj"))
surfmesh_flattened = trimesh.load_mesh(os.path.join(
save_folder, "make_it_stand_init_flattened.obj"))
tetgen_init = tetgen.TetGen(
surfmesh_flattened.vertices, surfmesh_flattened.faces)
tet_vtx, tet_elem = tetgen_init.tetrahedralize(minratio=1.1, mindihedral=2, quality=False)
tetmesh = pypgo.create_tetmesh(tet_vtx.flatten(), tet_elem.flatten(), E, nu, density)
pypgo.save_tetmesh_to_file(tetmesh, os.path.join(save_folder, "make_it_stand_init_flattened.veg"))
# get fixed vtx
os.makedirs(save_folder, exist_ok=True)
min_y = np.min(tet_vtx[:, 2])
# fixed_vertices = np.where(tet_vtx[:, 2] < min_y + 5e-3)[0]
fixed_vertices = np.where(tet_vtx[:, 2] < min_y + 1e-3)[0]
fixed_vtx_file = os.path.join(save_folder, "fixed.txt")
np.savetxt(fixed_vtx_file, fixed_vertices, fmt='%d')
verbose = 0
pypgo.stability_opt(os.path.join(save_folder, "make_it_stand_init_flattened.veg"), fixed_vtx_file, save_folder, verbose)
# save surface meshes
tetmesh_opt = pypgo.create_tetmesh_from_file(os.path.join(save_folder, "stand_opt.veg"))
tetmesh_opt_vtx = pypgo.get_tetmesh_vertex_positions(tetmesh_opt)
tetmesh_opt_elem = pypgo.get_tetmesh_element_indices(tetmesh_opt)
surf_vid, surf_fid = get_surface_vf(tetmesh_opt_elem)
surfmesh_opt = trimesh.Trimesh(
vertices=tetmesh_opt_vtx[surf_vid], faces=surf_fid)
surfmesh_opt.export(os.path.join(save_folder, "stand_opt.obj"))
tetmesh_opt_rest = pypgo.create_tetmesh_from_file(os.path.join(save_folder, "stand_opt_rest.veg"))
tetmesh_opt_rest_vtx = pypgo.get_tetmesh_vertex_positions(tetmesh_opt_rest)
tetmesh_opt_rest_elem = pypgo.get_tetmesh_element_indices(tetmesh_opt_rest)
surf_vid, surf_fid = get_surface_vf(tetmesh_opt_rest_elem)
surfmesh_opt_rest = trimesh.Trimesh(
vertices=tetmesh_opt_rest_vtx[surf_vid], faces=surf_fid)
surfmesh_opt_rest.export(os.path.join(save_folder, "stand_opt_rest.obj"))
## scale the mesh for rigid-ipc
scale = 50
surfmesh_opt_rest = trimesh.load_mesh(os.path.join(save_folder, "stand_opt_rest.obj"))
surfmesh_opt_rest.vertices *= scale
surfmesh_opt_rest.export(os.path.join(save_folder, "stand_opt_rest_scaled.obj"))
surfmesh_flattened = trimesh.load_mesh(os.path.join(save_folder, "make_it_stand_init_flattened.obj"))
surfmesh_flattened.vertices *= scale
surfmesh_flattened.export(os.path.join(save_folder, "make_it_stand_init_flattened_scaled.obj"))
if __name__ == "__main__":
# matching
mesh_name = "e547b70f8bb549e3ad5cea9a4eed800d"
save_folder = os.path.join("examples", "neurips2024", mesh_name)
matching(save_folder)
# stability
mesh_name = "unicorn"
save_folder = os.path.join("examples", "neurips2024", mesh_name)
stablize(save_folder)