trip_w_oblaka / utils.py
keeendaaa
Initial TripoSG Space app
2b1896f
import numpy as np
import open3d as o3d
import pymeshlab as pml
def simplify_mesh(mesh, target_face_num: int = 100000):
if mesh.faces.shape[0] <= target_face_num:
return mesh
vertices = mesh.vertices
faces = mesh.faces
ms = pml.MeshSet()
ms.add_mesh(pml.Mesh(vertices, faces))
ms.meshing_decimation_quadric_edge_collapse(
targetfacenum=int(target_face_num), preserveboundary=True
)
new_mesh = ms.current_mesh()
new_vertices = new_mesh.vertex_matrix()
new_faces = new_mesh.face_matrix()
o3d_mesh = o3d.geometry.TriangleMesh(
o3d.utility.Vector3dVector(new_vertices),
o3d.utility.Vector3iVector(new_faces),
)
o3d_mesh = o3d_mesh.remove_duplicated_vertices()
o3d_mesh = o3d_mesh.remove_degenerate_triangles()
o3d_mesh = o3d_mesh.remove_non_manifold_edges()
o3d_mesh = o3d_mesh.remove_unreferenced_vertices()
return mesh.__class__(
vertices=np.asarray(o3d_mesh.vertices),
faces=np.asarray(o3d_mesh.triangles),
vertex_normals=np.asarray(o3d_mesh.vertex_normals),
process=False,
)