Spaces:
Running on Zero
Running on Zero
File size: 1,681 Bytes
820f6f8 | 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 | import trimesh
try: # kiui is only used by postprocess_mesh (mesh cleaning/decimation); the demo
# pipeline only calls filter_mesh, so keep this import lazy/optional.
from kiui.mesh_utils import clean_mesh, decimate_mesh
except ImportError: # pragma: no cover
def clean_mesh(*args, **kwargs):
raise ImportError("kiui is required for postprocess_mesh but is not installed")
def decimate_mesh(*args, **kwargs):
raise ImportError("kiui is required for postprocess_mesh but is not installed")
def postprocess_mesh(mesh: trimesh.Trimesh, decimate_target=100000):
vertices = mesh.vertices
triangles = mesh.faces
if vertices.shape[0] > 0 and triangles.shape[0] > 0:
vertices, triangles = clean_mesh(vertices, triangles, remesh=False, min_f=25, min_d=5)
if decimate_target > 0 and triangles.shape[0] > decimate_target:
vertices, triangles = decimate_mesh(vertices, triangles, decimate_target, optimalplacement=False)
if vertices.shape[0] > 0 and triangles.shape[0] > 0:
vertices, triangles = clean_mesh(vertices, triangles, remesh=False, min_f=25, min_d=5)
mesh.vertices = vertices
mesh.faces = triangles
return mesh
def filter_mesh(mesh):
submeshes = mesh.split(only_watertight=False)
if len(submeshes) == 1:
return submeshes[0]
face_counts = [len(m.faces) for m in submeshes]
max_faces = max(face_counts)
filtered = [m for m in submeshes if len(m.faces) > max_faces*0.1]
if len(filtered) == 1:
return filtered[0]
merged = trimesh.util.concatenate(filtered)
merged.merge_vertices()
_ = merged.vertex_normals
return merged
|