Spaces:
Paused
Paused
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,37 +1,23 @@
|
|
| 1 |
import pymeshlab
|
| 2 |
import trimesh
|
| 3 |
-
import
|
| 4 |
|
| 5 |
def mesh_to_pymesh(vertices, faces):
|
| 6 |
-
mesh = pymeshlab.Mesh(
|
| 7 |
-
vertex_matrix=np.asarray(vertices, dtype=np.float64),
|
| 8 |
-
face_matrix=np.asarray(faces, dtype=np.int32),
|
| 9 |
-
)
|
| 10 |
ms = pymeshlab.MeshSet()
|
| 11 |
ms.add_mesh(mesh)
|
| 12 |
return ms
|
| 13 |
|
| 14 |
-
def pymesh_to_trimesh(mesh
|
| 15 |
-
verts = mesh.vertex_matrix()
|
| 16 |
-
faces = mesh.face_matrix()
|
| 17 |
-
return trimesh.Trimesh(
|
| 18 |
-
vertices=verts,
|
| 19 |
-
faces=faces,
|
| 20 |
-
process=False, # IMPORTANT
|
| 21 |
-
)
|
| 22 |
|
| 23 |
-
def simplify_mesh(mesh: trimesh.Trimesh, n_faces
|
| 24 |
-
if mesh.faces.shape[0]
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
targetfacenum=n_faces,
|
| 32 |
-
preserveboundary=True,
|
| 33 |
-
preservenormal=True,
|
| 34 |
-
preservetopology=True,
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
return pymesh_to_trimesh(ms.current_mesh())
|
|
|
|
| 1 |
import pymeshlab
|
| 2 |
import trimesh
|
| 3 |
+
import open3d as o3d
|
| 4 |
|
| 5 |
def mesh_to_pymesh(vertices, faces):
|
| 6 |
+
mesh = pymeshlab.Mesh(vertex_matrix=vertices, face_matrix=faces)
|
|
|
|
|
|
|
|
|
|
| 7 |
ms = pymeshlab.MeshSet()
|
| 8 |
ms.add_mesh(mesh)
|
| 9 |
return ms
|
| 10 |
|
| 11 |
+
def pymesh_to_trimesh(mesh):
|
| 12 |
+
verts = mesh.vertex_matrix()#.tolist()
|
| 13 |
+
faces = mesh.face_matrix()#.tolist()
|
| 14 |
+
return trimesh.Trimesh(vertices=verts, faces=faces) #, vID, fID
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
def simplify_mesh(mesh: trimesh.Trimesh, n_faces):
|
| 17 |
+
if mesh.faces.shape[0] > n_faces:
|
| 18 |
+
ms = mesh_to_pymesh(mesh.vertices, mesh.faces)
|
| 19 |
+
ms.meshing_merge_close_vertices()
|
| 20 |
+
ms.meshing_decimation_quadric_edge_collapse(targetfacenum = n_faces)
|
| 21 |
+
return pymesh_to_trimesh(ms.current_mesh())
|
| 22 |
+
else:
|
| 23 |
+
return mesh
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|