Spaces:
Paused
Paused
Add code/cube3d/mesh_utils/postprocessing.py
Browse files
code/cube3d/mesh_utils/postprocessing.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
try:
|
| 6 |
+
import pymeshlab
|
| 7 |
+
|
| 8 |
+
PYMESHLAB_AVAILABLE = True
|
| 9 |
+
except ImportError:
|
| 10 |
+
logging.warning(
|
| 11 |
+
"pymeshlab is not installed or could not be loaded. Please install it with `pip install pymeshlab`."
|
| 12 |
+
)
|
| 13 |
+
PYMESHLAB_AVAILABLE = False
|
| 14 |
+
from typing import Any
|
| 15 |
+
|
| 16 |
+
# Create stub class for typing
|
| 17 |
+
class pymeshlab:
|
| 18 |
+
MeshSet = Any
|
| 19 |
+
Mesh = Any
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def create_pymeshset(vertices: np.ndarray, faces: np.ndarray):
|
| 23 |
+
"""
|
| 24 |
+
Creates a MeshLab MeshSet given a list of vertices and faces.
|
| 25 |
+
"""
|
| 26 |
+
assert PYMESHLAB_AVAILABLE, "pymeshlab is not installed or could not be loaded."
|
| 27 |
+
# Initialize MeshSet and create pymeshlab.Mesh
|
| 28 |
+
mesh_set = pymeshlab.MeshSet()
|
| 29 |
+
input_mesh = pymeshlab.Mesh(vertex_matrix=vertices, face_matrix=faces)
|
| 30 |
+
mesh_set.add_mesh(input_mesh, "input_mesh")
|
| 31 |
+
logging.info("Mesh successfully added to pymeshlab MeshSet.")
|
| 32 |
+
return mesh_set
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def cleanup(ms: pymeshlab.MeshSet):
|
| 36 |
+
"""
|
| 37 |
+
General cleanup for a given Mesh. Removes degenerate elements from the
|
| 38 |
+
geometry.
|
| 39 |
+
"""
|
| 40 |
+
ms.meshing_remove_null_faces()
|
| 41 |
+
ms.meshing_remove_folded_faces()
|
| 42 |
+
ms.meshing_remove_duplicate_vertices()
|
| 43 |
+
ms.meshing_remove_duplicate_faces()
|
| 44 |
+
ms.meshing_remove_t_vertices()
|
| 45 |
+
ms.meshing_remove_unreferenced_vertices()
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def remove_floaters(ms: pymeshlab.MeshSet, threshold: float = 0.005):
|
| 49 |
+
"""
|
| 50 |
+
Remove any floating artifacts that exist from our mesh generation.
|
| 51 |
+
"""
|
| 52 |
+
assert PYMESHLAB_AVAILABLE, "pymeshlab is not installed or could not be loaded."
|
| 53 |
+
ms.meshing_remove_connected_component_by_diameter(
|
| 54 |
+
mincomponentdiag=pymeshlab.PercentageValue(15), removeunref=True
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def simplify_mesh(ms: pymeshlab.MeshSet, target_face_num: int):
|
| 59 |
+
"""
|
| 60 |
+
Simplify the mesh to the target number of faces.
|
| 61 |
+
"""
|
| 62 |
+
ms.meshing_decimation_quadric_edge_collapse(
|
| 63 |
+
targetfacenum=target_face_num,
|
| 64 |
+
qualitythr=0.4,
|
| 65 |
+
preservenormal=True,
|
| 66 |
+
autoclean=True,
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def save_mesh(ms: pymeshlab.MeshSet, output_path: str):
|
| 71 |
+
"""
|
| 72 |
+
Save the mesh to a file.
|
| 73 |
+
"""
|
| 74 |
+
ms.save_current_mesh(output_path)
|
| 75 |
+
logging.info(f"Mesh saved to {output_path}.")
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def postprocess_mesh(ms: pymeshlab.MeshSet, target_face_num: int, output_path: str):
|
| 79 |
+
"""
|
| 80 |
+
Postprocess the mesh to the target number of faces.
|
| 81 |
+
"""
|
| 82 |
+
cleanup(ms)
|
| 83 |
+
remove_floaters(ms)
|
| 84 |
+
simplify_mesh(ms, target_face_num)
|