From 9aaa2ccc1c32eb09b782196ef8709a7b0e283034 Mon Sep 17 00:00:00 2001 From: Tetsuo Koyama Date: Mon, 27 Jan 2025 12:27:16 +0900 Subject: [PATCH] Add Delaunay2 class to skgmsh to replace Delaunay (#442) --- examples/quad.py | 41 ++--------------------------------------- src/skgmsh/__init__.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 39 deletions(-) diff --git a/examples/quad.py b/examples/quad.py index d698f37c..693e8f8d 100644 --- a/examples/quad.py +++ b/examples/quad.py @@ -8,44 +8,7 @@ from __future__ import annotations -from pathlib import Path -import subprocess -from typing import Optional +import skgmsh as sg -import pyvista as pv -import shapely - - -class Delaunay2D: - """Delaunay2D class.""" - - def __init__(self, shell: shapely.Polygon, holes: Optional[list[shapely.Polygon]] = None) -> None: - """Create a Delaunay2D object.""" - self.shell = shapely.Polygon(shell) - self.holes = [shapely.Polygon(hole) for hole in holes] if holes else [] - self._generate_mesh() - - def _generate_mesh(self) -> None: - cell_size = 0.05 - with Path("quad.geo").open("w") as f: - for linearring in [self.shell.exterior, *list(self.shell.interiors), *self.holes]: - coords = linearring.coords[:-1].copy() - for i, coord in enumerate(coords): - x = coord[0] - y = coord[1] - z = coord[2] - f.write("Point(" + str(i + 1) + ") = {" + str(x) + "," + str(y) + "," + str(z) + "," + str(cell_size) + "};\n") - for i, _ in enumerate(coords[:-1]): - f.write("Line(" + str(i + 1) + ") = {" + str(i + 1) + ", " + str(i + 2) + "};\n") - f.write("Line(" + str(len(coords)) + ") = {" + str(len(coords)) + ", 1};\n") - f.write("Line Loop(1) = {") - for i in range(len(coords) - 1): - f.write(str(i + 1) + ", ") - f.write(str(len(coords)) + "};\n") - f.write("Plane Surface(1) = {1};\n") - subprocess.run(["gmsh", "quad.geo", "-2", "-o", "quad.vtk"], check=False) # noqa: S603, S607 - self.mesh = pv.read("quad.vtk") - - -alg = Delaunay2D([(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0)]) +alg = sg.Delaunay2D2([(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0)]) alg.mesh.plot(show_edges=True, color="w", cpos="xy") diff --git a/src/skgmsh/__init__.py b/src/skgmsh/__init__.py index ef3adafd..9ecd89e2 100644 --- a/src/skgmsh/__init__.py +++ b/src/skgmsh/__init__.py @@ -3,7 +3,10 @@ from __future__ import annotations import datetime +from pathlib import Path +import subprocess from typing import TYPE_CHECKING +from typing import Optional import gmsh from pygmsh.helpers import extract_to_meshio @@ -411,3 +414,34 @@ def cell_size(self: Delaunay3D) -> float | None: def cell_size(self: Delaunay3D, size: int) -> None: """Set the cell_size of the mesh.""" self._cell_size = size + + +class Delaunay2D2: + """Delaunay2D class.""" + + def __init__(self, shell: shapely.Polygon, holes: Optional[list[shapely.Polygon]] = None) -> None: + """Create a Delaunay2D object.""" + self.shell = shapely.Polygon(shell) + self.holes = [shapely.Polygon(hole) for hole in holes] if holes else [] + self._generate_mesh() + + def _generate_mesh(self) -> None: + cell_size = 0.05 + with Path("quad.geo").open("w") as f: + for linearring in [self.shell.exterior, *list(self.shell.interiors), *self.holes]: + coords = linearring.coords[:-1].copy() + for i, coord in enumerate(coords): + x = coord[0] + y = coord[1] + z = coord[2] + f.write("Point(" + str(i + 1) + ") = {" + str(x) + "," + str(y) + "," + str(z) + "," + str(cell_size) + "};\n") + for i, _ in enumerate(coords[:-1]): + f.write("Line(" + str(i + 1) + ") = {" + str(i + 1) + ", " + str(i + 2) + "};\n") + f.write("Line(" + str(len(coords)) + ") = {" + str(len(coords)) + ", 1};\n") + f.write("Line Loop(1) = {") + for i in range(len(coords) - 1): + f.write(str(i + 1) + ", ") + f.write(str(len(coords)) + "};\n") + f.write("Plane Surface(1) = {1};\n") + subprocess.run(["gmsh", "quad.geo", "-2", "-o", "quad.vtk"], check=False) # noqa: S603, S607 + self.mesh = pv.read("quad.vtk")