def generateMesh(n_points): points = [] # Simple 2D for initial testing for i in range(n_points): r = random.gauss(10,0.05) theta = random.vonmisesvariate(math.pi,0) phi = random.vonmisesvariate(math.pi, 0) x= r * math.sin(theta) * math.cos(phi) y= r * math.sin(theta) * math.sin(phi) z= r * math.cos(theta) points.append([x,y,z]) # Now just to check we have the target number of unique points nppoints = np.asarray(points) meshgen = pymesh.tetgen(); meshgen.points = nppoints; meshgen.merge_coplanar = True meshgen.coarsening = False meshgen.verbosity = 0 meshgen.run() mesh = pymesh.subdivide(meshgen.mesh, order = 5, method="loop") mesh = meshgen.mesh return(mesh)
def make_high_res_template_from_low_res(self): """ This function takes a path to the orginal shapenet model and subsample it nicely """ import pymesh if not(self.point_translation or self.patch_deformation): self.template[0].template_learned_HR = self.template[0].vertex_HR if self.patch_deformation: templates = self.get_patch_deformation_template(high_res = True) self.template[0].template_learned_HR = templates[0] if self.point_translation: templates = self.get_points_translation_template() if self.dim_template == 3: template_points = templates[0].cpu().clone().detach().numpy() obj1 = pymesh.form_mesh(vertices=template_points, faces=self.template[0].mesh.faces) if len(obj1.vertices)<100000: obj1 = pymesh.split_long_edges(obj1, 0.02)[0] while len(obj1.vertices)<100000: obj1 = pymesh.subdivide(obj1) self.template[0].mesh_HR = obj1 self.template[0].template_learned_HR = torch.from_numpy(obj1.vertices).cuda().float() self.template[0].num_vertex_HR = self.template[0].template_learned_HR.size(0) print(f"Make high res template with {self.template[0].num_vertex_HR} points.")
def link(path1): """ This function takes a path to the orginal shapenet model and subsample it nicely """ obj1 = pymesh.load_mesh(path1) if len(obj1.vertices) < 10000: obj1 = pymesh.split_long_edges(obj1, 0.02)[0] while len(obj1.vertices) < 10000: obj1 = pymesh.subdivide(obj1) new_mesh = pymesh.form_mesh( normalize_points.BoundingBox(torch.from_numpy(obj1.vertices)).numpy(), obj1.faces) return new_mesh
def remesh(path1): """ This function takes a path to the orginal shapenet model and subsample it nicely """ obj1 = pymesh.load_mesh(path1) obj1, info = pymesh.remove_isolated_vertices(obj1) print("Removed {} isolated vertices".format(info["num_vertex_removed"])) obj1, info = pymesh.remove_duplicated_vertices(obj1) print("Merged {} duplicated vertices".format(info["num_vertex_merged"])) obj1, _ = pymesh.remove_degenerated_triangles(obj1) if len(obj1.vertices) < 5000: while len(obj1.vertices) < 5000: obj1 = pymesh.subdivide(obj1) obj1 = pymesh.form_mesh(obj1.vertices, obj1.faces) return obj1
def extract_straight_skeleton(wires, logger, remove_holes): wires = uniform_sampling(wires); mesh = constrained_triangulate(wires, logger, remove_holes); mesh = pymesh.subdivide(mesh, order=1, method="simple"); bd_vertices = mesh.boundary_vertices; on_skeleton = np.ones(mesh.num_vertices, dtype=bool); on_skeleton[bd_vertices] = False; vertices = mesh.vertices; faces = mesh.faces; junction_triangles = np.all(on_skeleton[faces], axis=1) junction_centers = np.mean(vertices[faces[junction_triangles,:],:], axis=1); #skeleton_samples = np.vstack( # (vertices[on_skeleton,:], junction_centers)); skeleton_samples = vertices[on_skeleton, :]; vertices = np.vstack((wires.vertices, skeleton_samples)); wires.load(vertices, wires.edges); mesh = constrained_triangulate(wires, logger, remove_holes); return mesh;
def extract_straight_skeleton(wires, logger, remove_holes): wires = uniform_sampling(wires) mesh = constrained_triangulate(wires, logger, remove_holes) mesh = pymesh.subdivide(mesh, order=1, method="simple") bd_vertices = mesh.boundary_vertices on_skeleton = np.ones(mesh.num_vertices, dtype=bool) on_skeleton[bd_vertices] = False vertices = mesh.vertices faces = mesh.faces junction_triangles = np.all(on_skeleton[faces], axis=1) junction_centers = np.mean(vertices[faces[junction_triangles, :], :], axis=1) #skeleton_samples = np.vstack( # (vertices[on_skeleton,:], junction_centers)); skeleton_samples = vertices[on_skeleton, :] vertices = np.vstack((wires.vertices, skeleton_samples)) wires.load(vertices, wires.edges) mesh = constrained_triangulate(wires, logger, remove_holes) return mesh
def main(): args = parse_args() mesh = pymesh.load_mesh(args.input_mesh) mesh = pymesh.subdivide(mesh, args.order, args.method) pymesh.save_mesh(args.output_mesh, mesh)
def main(): args = parse_args(); mesh = pymesh.load_mesh(args.input_mesh); mesh = pymesh.subdivide(mesh, args.order, args.method); pymesh.save_mesh(args.output_mesh, mesh);
""" Script Name : meshSubdivision Author : BoyceTian Created : 2018/7/20 Version : 1.0 Description : PURPOSE : An example for subdividing meshes by using pymesh INPUTS : - filepath : Path of original mesh Type of data: .stl - filenew : Path of subdivided mesh Type of data: stl - mesh : original mesh Type of data: str - mesh_new : subdivided mesh OUTPUTS : """ import pymesh # get the path of the original mesh and set the path of the Subdivided mesh filepath = "./Spleen_3D-interpolation.stl" filenew = "./Spleen_3D-interpolation_new.stl" # get the original mesh mesh = pymesh.load_mesh(filepath) # subdivide the mesh mesh_new = pymesh.subdivide(mesh, order=3, method='simple') # write the subdivided mesh as a new file pymesh.save_mesh(filenew, mesh_new)