-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmesh_color
More file actions
107 lines (78 loc) · 3.97 KB
/
mesh_color
File metadata and controls
107 lines (78 loc) · 3.97 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import numpy as np
import pymeshlab as pml
# Functions (unchanged from before)
def isotropic_explicit_remeshing(verts, faces, v_colors=None):
m = pml.Mesh(verts, faces, v_color_matrix=v_colors) if v_colors is not None else pml.Mesh(verts, faces)
ms = pml.MeshSet()
ms.add_mesh(m, 'mesh')
# Apply isotropic explicit remeshing
ms.meshing_isotropic_explicit_remeshing(iterations=3, targetlen=pml.Percentage(1))
# Extract the updated mesh
m = ms.current_mesh()
verts = m.vertex_matrix()
faces = m.face_matrix()
v_colors = m.vertex_color_matrix() if m.has_vertex_color() else None
return verts, faces, v_colors
def clean_mesh(verts, faces, v_colors=None, v_pct=1, min_f=8, min_d=5, repair=True, remesh=True):
m = pml.Mesh(verts, faces, v_color_matrix=v_colors) if v_colors is not None else pml.Mesh(verts, faces)
ms = pml.MeshSet()
ms.add_mesh(m, 'mesh')
# Cleaning process
ms.meshing_remove_unreferenced_vertices()
ms.meshing_merge_close_vertices(threshold=pml.Percentage(v_pct))
ms.meshing_remove_duplicate_faces()
ms.meshing_remove_null_faces()
if min_d > 0:
ms.meshing_remove_connected_component_by_diameter(mincomponentdiag=pml.Percentage(min_d))
if min_f > 0:
ms.meshing_remove_connected_component_by_face_number(mincomponentsize=min_f)
if repair:
ms.meshing_repair_non_manifold_edges(method=0)
ms.meshing_repair_non_manifold_vertices(vertdispratio=0)
if remesh:
ms.meshing_isotropic_explicit_remeshing(iterations=3, targetlen=pml.Percentage(1))
m = ms.current_mesh()
verts = m.vertex_matrix()
faces = m.face_matrix()
v_colors = m.vertex_color_matrix() if m.has_vertex_color() else None
return verts, faces, v_colors
def decimate_mesh(verts, faces, target, v_colors=None):
m = pml.Mesh(verts, faces, v_color_matrix=v_colors) if v_colors is not None else pml.Mesh(verts, faces)
ms = pml.MeshSet()
ms.add_mesh(m, 'mesh')
# Perform decimation
ms.meshing_decimation_quadric_edge_collapse(targetfacenum=int(target))
m = ms.current_mesh()
verts = m.vertex_matrix()
faces = m.face_matrix()
v_colors = m.vertex_color_matrix() if m.has_vertex_color() else None
return verts, faces, v_colors
# Main function to load, process, and save a mesh
def process_mesh(input_file, output_file, target_face_count=10000):
# Load the mesh
ms = pml.MeshSet()
ms.load_new_mesh(input_file)
# Extract the vertices, faces, and colors
mesh = ms.current_mesh()
verts = mesh.vertex_matrix()
faces = mesh.face_matrix()
v_colors = mesh.vertex_color_matrix() if mesh.has_vertex_color() else None
print(f'[INFO] Loaded mesh: vertices={verts.shape}, faces={faces.shape}, vertex colors={"present" if v_colors is not None else "absent"}')
# Clean the mesh
verts, faces, v_colors = clean_mesh(verts, faces, v_colors=v_colors)
# Optionally decimate the mesh (reduce the number of faces)
verts, faces, v_colors = decimate_mesh(verts, faces, target=target_face_count, v_colors=v_colors)
# Optionally remesh the mesh for uniformity
verts, faces, v_colors = isotropic_explicit_remeshing(verts, faces, v_colors=v_colors)
# Save the updated mesh to a new file (with colors if available)
updated_mesh = pml.Mesh(verts, faces, v_color_matrix=v_colors) if v_colors is not None else pml.Mesh(verts, faces)
ms.clear()
ms.add_mesh(updated_mesh)
ms.save_current_mesh(output_file)
print(f'[INFO] Saved updated mesh to: {output_file}')
# Example usage
if __name__ == '__main__':
input_mesh_file = '/home/frasiolas/Downloads/archive/nerf_synthetic/ship/aaaa/mesh.ply' # Path to your input mesh
output_mesh_file = '/home/frasiolas/Downloads/archive/nerf_synthetic/ship/aaaa/mesh_refined.ply' # Path where updated mesh will be saved
target_faces = 20000 # Example target face count for decimation (adjust as needed)
process_mesh(input_mesh_file, output_mesh_file, target_face_count=target_faces)