File size: 1,118 Bytes
2b1896f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 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,
    )