def test_auto_hole_detection(self): tri = triangle(); tri.points = np.array([ [0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.2, 0.2], [0.8, 0.2], [0.8, 0.8], [0.2, 0.8] ]); tri.segments = np.array([ [0, 1], [1, 2], [2, 3], [3, 0], [5, 4], [6, 5], [7, 6], [4, 7] ]); tri.auto_hole_detection = True; tri.verbosity = 0; tri.run(); mesh = tri.mesh; self.assertEqual(2, mesh.num_boundary_loops);
def test_auto_hole_detection(self): tri = triangle(); tri.points = np.array([ [0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.2, 0.2], [0.8, 0.2], [0.8, 0.8], [0.2, 0.8] ]); tri.segments = np.array([ [0, 1], [1, 2], [2, 3], [3, 0], [5, 4], [6, 5], [7, 6], [4, 7] ]); tri.auto_hole_detection = True; tri.verbosity = 0; tri.run(); mesh = tri.mesh; self.assertEqual(2, mesh.num_boundary_loops);
def test_holes(self): tri = triangle() tri.points = np.array([ [0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.2, 0.2], [0.8, 0.2], [0.8, 0.8], [0.2, 0.8] ]) tri.segments = np.array([ [0, 1], [1, 2], [2, 3], [3, 0], [5, 4], [6, 5], [7, 6], [4, 7] ]) tri.holes = np.array([ [0.5, 0.5] ]) tri.verbosity = 0 tri.run() mesh = tri.mesh self.assertEqual(2, mesh.num_boundary_loops)
def totalMass(self): points = [[-self.a, -self.a, -self.a], [-self.a, -self.a, self.a], [-self.a, self.a, -self.a], [-self.a, self.a, self.a], [self.a, -self.a, -self.a], [self.a, -self.a, self.a], [self.a, self.a, -self.a], [self.a, self.a, self.a]] facets = [[0, 1, 3, 2], [2, 3, 7, 6], [4, 5, 7, 6], [0, 1, 5, 4], [0, 2, 6, 4], [1, 3, 7, 5]] baseTetra = scipy.spatial.Delaunay(points).simplices triangles = [] for facet in facets: triangleMesh = pymesh.triangle() triangleMesh.points = np.array(points)[facet] triangleMesh.verbosity = 0 triangleMesh.max_num_steiner_points = 0 triangleMesh.run() triangles.append(np.array(facet)[triangleMesh.mesh.faces]) triangles = np.vstack(triangles) tetgen = pymesh.tetgen() tetgen.points = points tetgen.triangles = triangles tetgen.tetrahedra = baseTetra tetgen.verbosity = 0 tetgen.max_tet_volume = 0.05 tetgen.run() mesh = tetgen.mesh tetra = np.array(mesh.vertices)[np.array(mesh.elements)].transpose( [1, 0, 2]) scheme = quadpy.t3._keast.keast_9() integrals = scheme.integrate(partial(self.rho, i=0), tetra) return integrals.sum()
def test_point_cloud(self): tri = triangle() tri.points = np.array([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]]) tri.verbosity = 0 tri.split_boundary = False tri.max_num_steiner_points = 0 tri.run() self.assertEqual(4, len(tri.vertices)) self.assertEqual(2, len(tri.faces))
def test_convex_hull(self): tri = triangle() tri.points = np.array([[0.0, 0.0], [1.0, 0.0], [0.1, 0.1], [0.0, 1.0]]) tri.keep_convex_hull = True tri.verbosity = 0 tri.max_num_steiner_points = 0 tri.run() self.assertEqual(4, len(tri.vertices)) self.assertEqual(3, len(tri.faces))
def test_pslg(self): tri = triangle() tri.points = np.array([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]]) tri.segments = np.array([[0, 2], [1, 3]]) tri.verbosity = 0 tri.split_boundary = False tri.max_num_steiner_points = 0 tri.keep_convex_hull = True tri.run() self.assertEqual(5, len(tri.vertices)) self.assertEqual(4, len(tri.faces))
def main(): args = parse_args() mesh = pymesh.load_mesh(args.input_mesh) engine = pymesh.triangle() engine.points = mesh.vertices engine.triangles = mesh.faces engine.max_area = args.max_area engine.split_boundary = not args.no_split_boundary if args.no_steiner_points: engine.max_num_steiner_points = 0 engine.run() mesh = engine.mesh pymesh.save_mesh(args.output_mesh, mesh)
def test_convex_hull(self): tri = triangle(); tri.points = np.array([ [0.0, 0.0], [1.0, 0.0], [0.1, 0.1], [0.0, 1.0] ]); tri.keep_convex_hull = True; tri.verbosity = 0; tri.max_num_steiner_points = 0; tri.run(); self.assertEqual(4, len(tri.vertices)); self.assertEqual(3, len(tri.faces));
def test_point_cloud(self): tri = triangle(); tri.points = np.array([ [0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0] ]); tri.verbosity = 0; tri.split_boundary = False; tri.max_num_steiner_points = 0; tri.run(); self.assertEqual(4, len(tri.vertices)); self.assertEqual(2, len(tri.faces));
def triangulate(points): """ triangulate the plane for operation and visualization """ num_points = len(points) indices = np.arange(num_points, dtype=np.int) segments = np.vstack((indices, np.roll(indices, -1))).T tri = pymesh.triangle() tri.points = np.array(points) tri.segments = segments tri.verbosity = 0 tri.run() return tri.mesh
def main(): args = parse_args(); wires = pymesh.wires.WireNetwork(); wires.load_from_file(args.input_svg); tri = pymesh.triangle(); tri.points = wires.vertices; tri.segments = wires.edges; tri.run(); mesh = tri.mesh; regions = tri.regions; mesh.add_attribute("regions"); mesh.set_attribute("regions", regions); pymesh.save_mesh(args.output_mesh, mesh, *mesh.attribute_names);
def test_pslg(self): tri = triangle(); tri.points = np.array([ [0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0] ]); tri.segments = np.array([ [0, 2], [1, 3] ]); tri.verbosity = 0; tri.split_boundary = False; tri.max_num_steiner_points = 0; tri.keep_convex_hull = True; tri.run(); self.assertEqual(5, len(tri.vertices)); self.assertEqual(4, len(tri.faces));
def test_2D(self): tri = pymesh.triangle() tri.points = np.array([ [0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0], ]) tri.keep_convex_hull = True tri.max_area = 0.0001 tri.verbosity = 0 tri.run() mesh = tri.mesh self.assertLess(0, mesh.num_faces) solver = pymesh.HarmonicSolver.create(mesh) bd_indices = mesh.boundary_vertices.ravel() # f(x,y) = ln(x^2 + y^2) is known to be harmonic. target_solution = np.log( np.square(numpy.linalg.norm(mesh.vertices + 1.0, axis=1))) bd_values = target_solution[bd_indices] solver.boundary_indices = bd_indices solver.boundary_values = bd_values solver.order = 1 solver.pre_process() solver.solve() sol = solver.solution.ravel() #mesh.add_attribute("target_solution"); #mesh.set_attribute("target_solution", target_solution); #mesh.add_attribute("solution"); #mesh.set_attribute("solution", sol); #pymesh.save_mesh("tmp.msh", mesh, *mesh.attribute_names); self.assertEqual(mesh.num_vertices, len(sol)) self.assert_array_almost_equal(target_solution, sol, 3)
def triangulate(wires, logger): bbox_min, bbox_max = wires.bbox tol = norm(bbox_max - bbox_min) / 20 engine = pymesh.triangle() engine.points = wires.vertices engine.segments = wires.edges engine.conforming_delaunay = True engine.max_area = tol**2 engine.verbosity = 0 start_time = time() engine.run() finish_time = time() t = finish_time - start_time logger.info("Triangle running time: {}".format(t)) mesh = engine.mesh mesh.add_attribute("cell") mesh.set_attribute("cell", engine.regions) return mesh
def integrateOverCell(self, tup, f, scheme=quadpy.t3._keast.keast_9()): i, tup2 = tup vertices, facets = tup2 if (vertices == np.array(None)).all(): if f(self.X[0].reshape(3, 1, 1), 0).shape == (1, 1): return float(0) else: return np.zeros(3) tetrahedrons = scipy.spatial.Delaunay(vertices).simplices volumes = self.tetraVolume(vertices[tetrahedrons]) goodTetra = tetrahedrons[ np.logical_not(np.isclose(volumes, 0, atol=1e-11, rtol=1.e-5)), :] triangles = [] for facet in facets: triangleMesh = pymesh.triangle() triangleMesh.verbosity = 0 triangleMesh.points = np.array(vertices)[facet] triangleMesh.max_num_steiner_points = 0 triangleMesh.run() triangles.append(np.array(facet)[triangleMesh.mesh.faces]) triangles = np.vstack(triangles) tetgen = pymesh.tetgen() tetgen.points = vertices tetgen.verbosity = 0 tetgen.triangles = triangles tetgen.max_tet_volume = 0.05 tetgen.tetrahedra = goodTetra tetgen.run() mesh = tetgen.mesh tetra = np.array(mesh.vertices)[np.array(mesh.elements)] volumes = self.tetraVolume(tetra) goodTetra = tetra[ np.logical_not(np.isclose(volumes, 0, atol=1e-8, rtol=1e-8) ), :].transpose([1, 0, 2]) integrals = scheme.integrate(partial(f, i=i), goodTetra) if len(integrals.shape) == 2: return integrals.sum(1) return integrals.sum(0)
def integrateOverFace(self, tup, scheme=quadpy.t2._wandzura_xiao.wandzura_xiao_6()): key, vertices = tup polygon = pyny.Polygon(vertices, False) param = polygon.get_parametric(True, tolerance=0.001) param /= np.linalg.norm(param[:3]) i = np.argmax(np.abs(param[:3])) if self.eqTol(param[i], 0, 0.001): raise ArithmeticError('Face is not planar') reducedVertex = np.delete(vertices, i, axis=1) triangles = scipy.spatial.Delaunay(reducedVertex).simplices triangleMesh = pymesh.triangle() triangleMesh.verbosity = 0 triangleMesh.points = np.array(reducedVertex) triangleMesh.triangles = triangles triangleMesh.max_area = 0.05 triangleMesh.run() mesh = triangleMesh.mesh integrals = scheme.integrate( partial(self.rhoface, param=param, i=i), mesh.vertices[mesh.elements].transpose([1, 0, 2])) return key, integrals.sum(0)
def conforming_triangulate(wires, logger, auto_hole_detection): bbox_min, bbox_max = wires.bbox; tol = norm(bbox_max - bbox_min) / 20; engine = pymesh.triangle(); engine.points = wires.vertices; engine.segments = wires.edges; engine.conforming_delaunay = True; engine.max_area = tol**2; engine.verbosity = 0; engine.auto_hole_detection = auto_hole_detection; start_time = time(); engine.run(); finish_time = time(); t = finish_time - start_time; logger.info("Triangle running time: {}".format(t)); mesh = engine.mesh; mesh.add_attribute("cell"); mesh.set_attribute("cell", engine.regions); return mesh;
def teddy_algorithm(points): hex_tri = pymesh.triangle() hex_tri.points = points hex_tri.split_boundary = False hex_tri.verbosity = 0 hex_tri.keep_convex_hull = True hex_tri.run() print("-----points-----") print(hex_tri.points) print("-----vertices-----") print(hex_tri.vertices) print("-----faces-----") print(hex_tri.faces) hex_mesh_tri = hex_tri.mesh neighbors = construct_neighbors(hex_tri.points) print(neighbors) labeled_triangles = label(hex_tri.faces, neighbors) print(labeled_triangles) ret = fanning(hex_tri.faces, neighbors, hex_tri.points, labeled_triangles) hex_new_points = ret[0] hex_new_faces = ret[1] hex_axis = ret[2] hex_old_points = [] hex_old_faces = [] for i in range(0, len(hex_tri.points)): hex_old_points.append(list(hex_tri.points[i])) for i in range(0, len(hex_tri.faces)): hex_old_faces.append(list(hex_tri.faces[i])) print("old points {}".format(hex_old_points)) updated_faces, updated_points = retriangulate(hex_old_faces, hex_new_faces, hex_new_points, hex_axis, neighbors, labeled_triangles) elevate(updated_points, updated_faces, hex_axis, neighbors)
def constrained_triangulate(wires, logger, auto_hole_detection): bbox_min, bbox_max = wires.bbox tol = norm(bbox_max - bbox_min) / 20 engine = pymesh.triangle() engine.points = wires.vertices engine.segments = wires.edges engine.conforming_delaunay = False engine.max_num_steiner_points = 0 engine.split_boundary = False engine.verbosity = 0 engine.auto_hole_detection = auto_hole_detection start_time = time() engine.run() finish_time = time() t = finish_time - start_time logger.info("Triangle running time: {}".format(t)) mesh = engine.mesh mesh.add_attribute("cell") mesh.set_attribute("cell", engine.regions) return mesh