import numpy as np from numpy.linalg import norm import pymesh """ Modified from: fixmesh.py - MaSIF Pablo Gainza - LPDI STI EPFL 2019 """ """ fixmesh.py: Regularize a protein surface mesh. - based on code from the PyMESH documentation. """ def fix_mesh(mesh, resolution, detail="normal"): bbox_min, bbox_max = mesh.bbox; diag_len = norm(bbox_max - bbox_min); if detail == "normal": target_len = diag_len * 5e-3; elif detail == "high": target_len = diag_len * 2.5e-3; elif detail == "low": target_len = diag_len * 1e-2; target_len = resolution #print("Target resolution: {} mm".format(target_len)); # PGC 2017: Remove duplicated vertices first mesh, _ = pymesh.remove_duplicated_vertices(mesh, 0.001) count = 0; print("Removing degenerated triangles") mesh, __ = pymesh.remove_degenerated_triangles(mesh, 100); mesh, __ = pymesh.split_long_edges(mesh, target_len); num_vertices = mesh.num_vertices; while True: mesh, __ = pymesh.collapse_short_edges(mesh, 1e-6); mesh, __ = pymesh.collapse_short_edges(mesh, target_len, preserve_feature=True); mesh, __ = pymesh.remove_obtuse_triangles(mesh, 150.0, 100); if mesh.num_vertices == num_vertices: break; num_vertices = mesh.num_vertices; #print("#v: {}".format(num_vertices)); count += 1; if count > 10: break; mesh = pymesh.resolve_self_intersection(mesh); mesh, __ = pymesh.remove_duplicated_faces(mesh); #mesh = pymesh.compute_outer_hull(mesh); ############ Added by Oscar Mendez Lucio ############## mesh = pymesh.compute_outer_hull(mesh, all_layers=True); num_nodes = [i.num_nodes for i in mesh] mesh = mesh[np.argmax(num_nodes)] ############################################################ mesh, __ = pymesh.remove_duplicated_faces(mesh); mesh, __ = pymesh.remove_obtuse_triangles(mesh, 179.0, 5); mesh, __ = pymesh.remove_isolated_vertices(mesh); mesh, _ = pymesh.remove_duplicated_vertices(mesh, 0.001) ############ Added by Oscar Mendez Lucio ############## mesh = pymesh.separate_mesh(mesh) num_nodes = [i.num_nodes for i in mesh] mesh = mesh[np.argmax(num_nodes)] ############################################################ return mesh