generated from ashawkey/nerf_template
-
Notifications
You must be signed in to change notification settings - Fork 18
/
meshutils.py
251 lines (176 loc) · 7.96 KB
/
meshutils.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import numpy as np
import pymeshlab as pml
def isotropic_explicit_remeshing(verts, faces):
_ori_vert_shape = verts.shape
_ori_face_shape = faces.shape
m = pml.Mesh(verts, faces)
ms = pml.MeshSet()
ms.add_mesh(m, 'mesh') # will copy!
# filters
# ms.apply_coord_taubin_smoothing()
ms.meshing_isotropic_explicit_remeshing(iterations=3, targetlen=pml.Percentage(1))
# extract mesh
m = ms.current_mesh()
verts = m.vertex_matrix()
faces = m.face_matrix()
print(f'[INFO] isotropic explicit remesh: {_ori_vert_shape} --> {verts.shape}, {_ori_face_shape} --> {faces.shape}')
return verts, faces
def decimate_mesh(verts, faces, target, backend='pymeshlab', remesh=False, optimalplacement=True):
# optimalplacement: default is True, but for flat mesh must turn False to prevent spike artifect.
_ori_vert_shape = verts.shape
_ori_face_shape = faces.shape
if backend == 'pyfqmr':
import pyfqmr
solver = pyfqmr.Simplify()
solver.setMesh(verts, faces)
solver.simplify_mesh(target_count=target, preserve_border=False, verbose=False)
verts, faces, normals = solver.getMesh()
else:
m = pml.Mesh(verts, faces)
ms = pml.MeshSet()
ms.add_mesh(m, 'mesh') # will copy!
# filters
# ms.meshing_decimation_clustering(threshold=pml.Percentage(1))
ms.meshing_decimation_quadric_edge_collapse(targetfacenum=int(target), optimalplacement=optimalplacement)
if remesh:
# ms.apply_coord_taubin_smoothing()
ms.meshing_isotropic_explicit_remeshing(iterations=3, targetlen=pml.Percentage(1))
# extract mesh
m = ms.current_mesh()
verts = m.vertex_matrix()
faces = m.face_matrix()
print(f'[INFO] mesh decimation: {_ori_vert_shape} --> {verts.shape}, {_ori_face_shape} --> {faces.shape}')
return verts, faces
def remove_masked_trigs(verts, faces, mask, dilation=5):
# mask: 0 == keep, 1 == remove
_ori_vert_shape = verts.shape
_ori_face_shape = faces.shape
m = pml.Mesh(verts, faces, f_scalar_array=mask) # mask as the quality
ms = pml.MeshSet()
ms.add_mesh(m, 'mesh') # will copy!
# select faces
ms.compute_selection_by_condition_per_face(condselect='fq == 0') # select kept faces
# dilate to aviod holes...
for _ in range(dilation):
ms.apply_selection_dilatation()
ms.apply_selection_inverse(invfaces=True) # invert
# delete faces
ms.meshing_remove_selected_faces()
# clean unref verts
ms.meshing_remove_unreferenced_vertices()
# extract
m = ms.current_mesh()
verts = m.vertex_matrix()
faces = m.face_matrix()
print(f'[INFO] mesh mask trigs: {_ori_vert_shape} --> {verts.shape}, {_ori_face_shape} --> {faces.shape}')
return verts, faces
def remove_masked_verts(verts, faces, mask):
# mask: 0 == keep, 1 == remove
_ori_vert_shape = verts.shape
_ori_face_shape = faces.shape
m = pml.Mesh(verts, faces, v_scalar_array=mask) # mask as the quality
ms = pml.MeshSet()
ms.add_mesh(m, 'mesh') # will copy!
# select verts
ms.compute_selection_by_condition_per_vertex(condselect='q == 1')
# delete verts and connected faces
ms.meshing_remove_selected_vertices()
# extract
m = ms.current_mesh()
verts = m.vertex_matrix()
faces = m.face_matrix()
print(f'[INFO] mesh mask verts: {_ori_vert_shape} --> {verts.shape}, {_ori_face_shape} --> {faces.shape}')
return verts, faces
def remove_selected_verts(verts, faces, query='(x < 1) && (x > -1) && (y < 1) && (y > -1) && (z < 1 ) && (z > -1)'):
_ori_vert_shape = verts.shape
_ori_face_shape = faces.shape
m = pml.Mesh(verts, faces)
ms = pml.MeshSet()
ms.add_mesh(m, 'mesh') # will copy!
# select verts
ms.compute_selection_by_condition_per_vertex(condselect=query)
# delete verts and connected faces
ms.meshing_remove_selected_vertices()
# extract
m = ms.current_mesh()
verts = m.vertex_matrix()
faces = m.face_matrix()
print(f'[INFO] mesh remove verts: {_ori_vert_shape} --> {verts.shape}, {_ori_face_shape} --> {faces.shape}')
return verts, faces
def clean_mesh(verts, faces, v_pct=1, min_f=8, min_d=5, repair=True, remesh=True):
# verts: [N, 3]
# faces: [N, 3]
_ori_vert_shape = verts.shape
_ori_face_shape = faces.shape
m = pml.Mesh(verts, faces)
ms = pml.MeshSet()
ms.add_mesh(m, 'mesh') # will copy!
# filters
ms.meshing_remove_unreferenced_vertices() # verts not refed by any faces
if v_pct > 0:
ms.meshing_merge_close_vertices(threshold=pml.Percentage(v_pct)) # 1/10000 of bounding box diagonal
ms.meshing_remove_duplicate_faces() # faces defined by the same verts
ms.meshing_remove_null_faces() # faces with area == 0
if min_d > 0:
ms.meshing_remove_connected_component_by_diameter(mincomponentdiag=pml.Percentage(min_d))
if min_f > 0:
ms.meshing_remove_connected_component_by_face_number(mincomponentsize=min_f)
if repair:
# ms.meshing_remove_t_vertices(method=0, threshold=40, repeat=True)
ms.meshing_repair_non_manifold_edges(method=0)
ms.meshing_repair_non_manifold_vertices(vertdispratio=0)
if remesh:
# ms.apply_coord_taubin_smoothing()
ms.meshing_isotropic_explicit_remeshing(iterations=3, targetlen=pml.Percentage(1))
# extract mesh
m = ms.current_mesh()
verts = m.vertex_matrix()
faces = m.face_matrix()
print(f'[INFO] mesh cleaning: {_ori_vert_shape} --> {verts.shape}, {_ori_face_shape} --> {faces.shape}')
return verts, faces
def decimate_and_refine_mesh(verts, faces, mask, decimate_ratio=0.1, refine_size=0.01, refine_remesh_size=0.02):
# verts: [N, 3]
# faces: [M, 3]
# mask: [M], 0 denotes do nothing, 1 denotes decimation, 2 denotes subdivision
_ori_vert_shape = verts.shape
_ori_face_shape = faces.shape
m = pml.Mesh(verts, faces, f_scalar_array=mask)
ms = pml.MeshSet()
ms.add_mesh(m, 'mesh') # will copy!
# decimate and remesh
ms.compute_selection_by_condition_per_face(condselect='fq == 1')
if decimate_ratio > 0:
ms.meshing_decimation_quadric_edge_collapse(targetfacenum=int((1 - decimate_ratio) * (mask == 1).sum()), selected=True)
if refine_remesh_size > 0:
ms.meshing_isotropic_explicit_remeshing(iterations=3, targetlen=pml.AbsoluteValue(refine_remesh_size), selectedonly=True)
# repair
ms.set_selection_none(allfaces=True)
ms.meshing_repair_non_manifold_edges(method=0)
ms.meshing_repair_non_manifold_vertices(vertdispratio=0)
# refine
if refine_size > 0:
ms.compute_selection_by_condition_per_face(condselect='fq == 2')
ms.meshing_surface_subdivision_midpoint(threshold=pml.AbsoluteValue(refine_size), selected=True)
# ms.meshing_isotropic_explicit_remeshing(iterations=3, targetlen=pml.AbsoluteValue(refine_size), selectedonly=True)
# extract mesh
m = ms.current_mesh()
verts = m.vertex_matrix()
faces = m.face_matrix()
print(f'[INFO] mesh decimating & subdividing: {_ori_vert_shape} --> {verts.shape}, {_ori_face_shape} --> {faces.shape}')
return verts, faces
# in meshutils.py
def select_bad_and_flat_faces_by_normal(verts, faces, usear=False, aratio=0.02, nfratio_bad=120, nfratio_flat=5):
m = pml.Mesh(verts, faces)
ms = pml.MeshSet()
ms.add_mesh(m, 'mesh')
ms.compute_selection_bad_faces(usear=usear, aratio=aratio, usenf=True, nfratio=nfratio_bad)
m = ms.current_mesh()
bad_faces_mask = m.face_selection_array()
# print('bad_faces_mask cnt: ', sum(bad_faces_mask * 1.0))
ms.set_selection_none(allfaces=True)
ms.compute_selection_bad_faces(usear=usear, aratio=aratio, usenf=True, nfratio=nfratio_flat)
m = ms.current_mesh()
flat_faces_mask = m.face_selection_array() == False # reverse
# print('flat_faces_mask cnt: ', sum(flat_faces_mask * 1.0))
ms.set_selection_none(allfaces=True)
return bad_faces_mask, flat_faces_mask