def fix_mesh(mesh, detail="normal"): bbox_min, bbox_max = mesh.bbox; diag_len = norm(bbox_max - bbox_min); if detail == "normal": target_len = diag_len * 1e-2; #target_len = diag_len * 5e-3; elif detail == "enormal": target_len = diag_len * 5e-3 elif detail == "high": target_len = diag_len * 3e-3 #target_len = diag_len * 2.5e-3; elif detail == "low": target_len = diag_len * 1e-2; elif detail == "ehigh": target_len = diag_len * 1e-3; print("Target resolution: {} mm".format(target_len)); count = 0; 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); if detail == "low": mesh, __ = pymesh.collapse_short_edges(mesh, target_len, preserve_feature=False); else: 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); mesh, __ = pymesh.remove_duplicated_faces(mesh); mesh, __ = pymesh.remove_obtuse_triangles(mesh, 179.0, 5); mesh, __ = pymesh.remove_isolated_vertices(mesh); return mesh;
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) 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) return mesh
def fix_mesh(mesh, target_len): bbox_min, bbox_max = mesh.bbox diag_len = np.linalg.norm(bbox_max - bbox_min) count = 0 print(" remove degenerated triangles") mesh, __ = pymesh.remove_degenerated_triangles(mesh, 100) print(" split long edges") mesh, __ = pymesh.split_long_edges(mesh, target_len) num_vertices = mesh.num_vertices while True: print(" pass %d" % count) print(" collapse short edges #1") mesh, __ = pymesh.collapse_short_edges(mesh, 1e-6) print(" collapse short edges #2") mesh, __ = pymesh.collapse_short_edges(mesh, target_len, preserve_feature=True) print(" remove obtuse triangles") mesh, __ = pymesh.remove_obtuse_triangles(mesh, 150.0, 100) print(" %d of %s vertices." % (num_vertices, mesh.num_vertices)) if mesh.num_vertices == num_vertices: break num_vertices = mesh.num_vertices count += 1 if count > 10: break print(" resolve self intersection") mesh = pymesh.resolve_self_intersection(mesh) print(" remove duplicated faces") mesh, __ = pymesh.remove_duplicated_faces(mesh) print(" computer outer hull") mesh = pymesh.compute_outer_hull(mesh) print(" remove duplicated faces") mesh, __ = pymesh.remove_duplicated_faces(mesh) print(" remove obtuse triangles") mesh, __ = pymesh.remove_obtuse_triangles(mesh, 179.0, 5) print(" remove isolated vertices") mesh, __ = pymesh.remove_isolated_vertices(mesh) return mesh
def __fix_mesh(self, mesh, improvement_thres=0.8): mesh, __ = pymesh.split_long_edges(mesh, self.mesh_target_len) num_vertices = len(mesh.vertices) for __ in range(10): mesh, __ = pymesh.collapse_short_edges(mesh, 1e-6) mesh, __ = pymesh.collapse_short_edges( mesh, self.mesh_target_len, preserve_feature=True ) mesh, __ = pymesh.remove_obtuse_triangles(mesh, 150.0, 100) if len(mesh.vertices) < num_vertices * improvement_thres: break mesh = pymesh.resolve_self_intersection(mesh) mesh, __ = pymesh.collapse_short_edges(mesh, 1e-6) mesh, __ = pymesh.remove_duplicated_faces(mesh) mesh, __ = pymesh.remove_duplicated_faces(mesh) mesh, __ = pymesh.remove_obtuse_triangles(mesh, 179.0, 5) mesh, __ = pymesh.remove_isolated_vertices(mesh) return mesh
def old_fix_mesh(vertices, faces, detail="normal"): bbox_min = np.amin(vertices, axis=0) bbox_max = np.amax(vertices, axis=0) 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 print("Target resolution: {} mm".format(target_len)) count = 0 vertices, faces = pymesh.split_long_edges(vertices, faces, target_len) num_vertices = len(vertices) while True: vertices, faces = pymesh.collapse_short_edges(vertices, faces, 1e-6) vertices, faces = pymesh.collapse_short_edges(vertices, faces, target_len, preserve_feature=True) vertices, faces = pymesh.remove_obtuse_triangles( vertices, faces, 150.0, 100) if num_vertices == len(vertices): break num_vertices = len(vertices) print("#v: {}".format(num_vertices)) count += 1 if count > 10: break vertices, faces = pymesh.resolve_self_intersection(vertices, faces) vertices, faces = pymesh.remove_duplicated_faces(vertices, faces) vertices, faces, _ = pymesh.compute_outer_hull(vertices, faces, False) vertices, faces = pymesh.remove_duplicated_faces(vertices, faces) vertices, faces = pymesh.remove_obtuse_triangles(vertices, faces, 179.0, 5) vertices, faces, voxels = pymesh.remove_isolated_vertices(vertices, faces) return vertices, faces
def fix_mesh(mesh, 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; print("Target resolution: {} mm".format(target_len)); count = 0; 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); mesh, __ = pymesh.remove_duplicated_faces(mesh); mesh, __ = pymesh.remove_obtuse_triangles(mesh, 179.0, 5); mesh, __ = pymesh.remove_isolated_vertices(mesh); return mesh;
def fix_mesh(mesh): mesh, __ = pymesh.remove_degenerated_triangles(mesh, 100) log_mesh(mesh, "Remove degenerate faces") mesh, __ = pymesh.collapse_short_edges(mesh, MIN_RES, preserve_feature=True) log_mesh(mesh, "Collapse short edges") mesh = pymesh.resolve_self_intersection(mesh) mesh, __ = pymesh.remove_duplicated_faces(mesh) log_mesh(mesh, "Remove self intersections") mesh = pymesh.compute_outer_hull(mesh) mesh, __ = pymesh.remove_duplicated_faces(mesh) log_mesh(mesh, "New hull, remove duplicates") mesh, __ = pymesh.remove_obtuse_triangles(mesh, 179.5, 5) log_mesh(mesh, "Remote obtuse faces") mesh, __ = pymesh.remove_isolated_vertices(mesh) log_mesh(mesh, "Remove isolated vertices") return mesh
def execute(self, context): scene = context.scene pymesh_props = scene.pymesh obj_a = context.active_object mesh_a = import_object(context, obj_a) pymesh_r, info = pymesh.remove_degenerated_triangles(mesh_a) pymesh_r, info = pymesh.remove_obtuse_triangles(pymesh_r) pymesh_r, info = pymesh.remove_duplicated_faces(pymesh_r) pymesh_r, info = pymesh.collapse_short_edges(pymesh_r) pymesh_r, info = pymesh.remove_duplicated_vertices(pymesh_r) pymesh_r, info = pymesh.remove_isolated_vertices(pymesh_r) off_name = "Py.Clean." + obj_a.name mesh_r = export_mesh(context, pymesh_r, off_name) add_to_scene(context, mesh_r) return {'FINISHED'}
def filter(self): mesh = pymesh.form_mesh(self.mesh.points.values, self.mesh.cells.values) mesh, _ = pymesh.remove_degenerated_triangles(mesh, self.max_iterations) mesh, _ = pymesh.split_long_edges(mesh, self.size) num_vertices = mesh.num_vertices for _ in range(self.max_iterations): mesh, _ = pymesh.collapse_short_edges(mesh, self.size, preserve_feature=True) mesh, _ = pymesh.remove_obtuse_triangles(mesh, self.max_angle, self.max_iterations) if mesh.num_vertices == num_vertices: break num_vertices = mesh.num_vertices return self.mesh.mesh_class()(mesh, parents=[self.mesh])
def fix_meshes(mesh, detail="normal"): """ A pipeline to optimise and fix mesh based on pymesh Mesh object. 1. A box is created around the mesh. 2. A target length is found based on diagonal of the mesh box. 3. You can choose between 3 level of details, normal details settings seems to be a good compromise between final mesh size and sufficient number of vertices. It highly depends on your final goal. 4. Remove degenerated triangles aka collinear triangles composed of 3 aligned points. The number of iterations is 5 and should remove all degenerated triangles 5. Remove isolated vertices, not connected to any faces or edges 6. Remove self intersection edges and faces which is not realistic 7. Remove duplicated faces 8. The removing of duplicated faces can leave some vertices alone, we will removed them 9. The calculation of outer hull volume is useful to be sure that the mesh is still ok 10. Remove obtuse triangles > 179 who is not realistic and increase computation time 11. We will remove potential duplicated faces again 12. And duplicated vertices again 13. Finally we will look if the mesh is broken or not. If yes we will try lower settings, if the lowest settings broke the mesh we will return the initial mesh. If not, we will return the optimised mesh. :param mesh: Pymesh Mesh object to optimise :param detail: string 'high', 'normal' or 'low' ('normal' as default), or float/int Settings to choose the targeting minimum length of edges :return: Pymesh Mesh object An optimised mesh or not depending on detail settings and mesh quality """ meshCopy = mesh # copy/pasta of pymesh script fix_mesh from qnzhou, see pymesh on GitHub bbox_min, bbox_max = mesh.bbox diag_len = np.linalg.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 elif detail is float or detail is int and detail > 0: target_len = diag_len * detail else: print( 'Details settings is invalid, must be "low", "normal", "high" or positive int or float' ) quit() count = 0 mesh, __ = pymesh.remove_degenerated_triangles(mesh, 5) mesh, __ = pymesh.split_long_edges(mesh, target_len) num_vertices = mesh.num_vertices while True: mesh, __ = pymesh.collapse_short_edges(mesh, target_len, preserve_feature=True) mesh, info = pymesh.remove_obtuse_triangles(mesh, 179.0, 5) if mesh.num_vertices == num_vertices: break num_vertices = mesh.num_vertices count += 1 if count > 10: break mesh, __ = pymesh.remove_duplicated_vertices(mesh) mesh, __ = pymesh.remove_isolated_vertices(mesh) mesh = pymesh.resolve_self_intersection(mesh) mesh, __ = pymesh.remove_duplicated_faces(mesh) mesh, __ = pymesh.remove_duplicated_vertices(mesh) mesh = pymesh.compute_outer_hull(mesh) mesh, __ = pymesh.remove_obtuse_triangles(mesh, 179.0, 5) mesh, __ = pymesh.remove_duplicated_faces(mesh) mesh, __ = pymesh.remove_isolated_vertices(mesh) if is_mesh_broken(mesh, meshCopy) is True: if detail == "high": print( f'The function fix_meshes broke mesh, trying with lower details settings' ) fix_meshes(meshCopy, detail="normal") return mesh if detail == "normal": print( f'The function fix_meshes broke mesh, trying with lower details settings' ) mesh = fix_meshes(meshCopy, detail="low") return mesh if detail == "low": print( f'The function fix_meshes broke mesh, no lower settings can be applied, no fix was done' ) return meshCopy else: return mesh