Skip to content

Commit

Permalink
fix: update default parameters for mesh_fix
Browse files Browse the repository at this point in the history
Set verbose and joincomp parameters to True by default in the mesh_fix function to improve clarity and ensure consistent behavior.
  • Loading branch information
liblaf committed May 5, 2024
1 parent 31a8dc7 commit d625a56
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
6 changes: 6 additions & 0 deletions src/mkit/io/_meshio.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ def load_meshio(filename: StrPath) -> meshio.Mesh:
def as_meshio(mesh: AnyMesh, **kwargs: Unpack[Attrs]) -> meshio.Mesh:
match mesh:
case meshio.Mesh():
if point_data := kwargs.get("point_data"):
mesh.point_data.update(point_data)
if cell_data := kwargs.get("cell_data"):
mesh.cell_data.update(cell_data)
if field_data := kwargs.get("field_data"):
mesh.field_data.update(field_data)
return mesh
case pytorch3d.structures.Meshes():
return pytorch3d_to_meshio(mesh, **kwargs)
Expand Down
4 changes: 2 additions & 2 deletions src/mkit/ops/mesh_fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
def mesh_fix(
mesh: trimesh.Trimesh,
*,
verbose: bool = False,
joincomp: bool = False,
verbose: bool = True,
joincomp: bool = True,
remove_smallest_components: bool = True,
) -> trimesh.Trimesh:
mesh_pv: pv.PolyData = mkit.io.as_pyvista(mesh)
Expand Down
13 changes: 7 additions & 6 deletions src/mkit/ops/tetgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
from mkit.typing import StrPath


def tetgen(mesh: meshio.Mesh) -> meshio.Mesh:
def tetgen(mesh: meshio.Mesh, *, verbose: bool = True) -> meshio.Mesh:
"""
Args:
mesh: input mesh
holes: (N, 3) float
Returns:
tetrahedral mesh
Expand All @@ -24,10 +25,10 @@ def tetgen(mesh: meshio.Mesh) -> meshio.Mesh:
tmpdir = pathlib.Path(tmpdir)
input_file: pathlib.Path = tmpdir / "mesh.smesh"
save_smesh(input_file, mesh)
subprocess.run(
["tetgen", "-p", "-q", "-O", "-z", "-k", "-C", "-V", input_file],
check=True,
)
args: list[str] = ["tetgen", "-p", "-q", "-O", "-z", "-k", "-C"]
if verbose:
args.append("-V")
subprocess.run([*args, input_file], check=True)
tetra_mesh: meshio.Mesh = meshio.read(tmpdir / "mesh.1.vtk")
points: npt.NDArray[np.floating] = tetra_mesh.points
tetra: npt.NDArray[np.intp] = tetra_mesh.get_cells_type("tetra")
Expand Down Expand Up @@ -109,7 +110,7 @@ def save_smesh(file: StrPath, mesh: meshio.Mesh) -> None:
fprint()
fprint("# Part 3 - hole list")
fprint("# <# of holes>")
holes: npt.NDArray[np.float64] = mesh.field_data.get("holes", [])
holes: npt.NDArray[np.floating] = np.asarray(mesh.field_data.get("holes", []))
fprint(len(holes))
fprint("# <hole #> <x> <y> <z>")
for hole_id, hole in enumerate(holes):
Expand Down
1 change: 1 addition & 0 deletions tasks/register/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ $(TEMPLATE_DIR)/01-face.ply: $(TEMPLATE_DIR)/00-face.ply $(srcdir)/template/face
$(TEMPLATE_DIR)/01-skull.vtu $(TEMPLATE_DIR)/01-mandible.vtu $(TEMPLATE_DIR)/01-maxilla.vtu &: $(TEMPLATE_DIR)/00-skull.ply $(srcdir)/template/skull/process.py
python "$(lastword $^)" --output="$(TEMPLATE_DIR)/01-skull.vtu" --mandible="$(TEMPLATE_DIR)/01-mandible.vtu" --maxilla="$(TEMPLATE_DIR)/01-maxilla.vtu" "$<"

# Trim template face according to target face
ground: $(TEMPLATE_DIR)/00-face.ply $(TARGET_DIR)/01-face.ply $(srcdir)/template/face/ground.py
python "$(lastword $^)" "$<" "$(word 2,$^)"

Expand Down
39 changes: 35 additions & 4 deletions tasks/tetgen/src/create.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
import pathlib
from typing import Annotated

import meshio
import mkit.cli
import mkit.io
import mkit.ops.ray
import mkit.ops.tetgen
import numpy as np
import scipy.interpolate
import trimesh
import typer
from numpy import typing as npt


def main(
face_file: Annotated[pathlib.Path, typer.Option("--face", exists=True)],
mandible_file: Annotated[pathlib.Path, typer.Option("--mandible", exists=True)],
maxilla_file: Annotated[pathlib.Path, typer.Option("--maxilla", exists=True)],
*,
output_file: Annotated[
pathlib.Path, typer.Option("-o", "--output", dir_okay=False, writable=True)
],
) -> None:
pass
face: trimesh.Trimesh = trimesh.creation.icosphere(radius=1.0)
pre_skull: trimesh.Trimesh = trimesh.creation.icosphere(radius=0.5)
post_skull: trimesh.Trimesh = pre_skull.copy()
post_skull.vertices[post_skull.vertices[:, 1] < 0.0] += [0.0, -0.1, 0.0]
tri: trimesh.Trimesh = trimesh.util.concatenate([face, pre_skull])
tet: meshio.Mesh = mkit.ops.tetgen.tetgen(
mkit.io.as_meshio(
tri, field_data={"holes": [mkit.ops.ray.find_inner_point(pre_skull)]}
)
)
closest: npt.NDArray[np.floating]
distance: npt.NDArray[np.floating]
triangle_id: npt.NDArray[np.integer]
closest, distance, triangle_id = pre_skull.nearest.on_surface(tet.points)
mask_skull: npt.NDArray[np.bool_] = distance < 1e-6
disp: npt.NDArray[np.floating] = np.full(tet.points.shape, np.nan)
disp[mask_skull] = scipy.interpolate.griddata(
pre_skull.vertices,
post_skull.vertices - pre_skull.vertices,
tet.points[mask_skull],
method="nearest",
)
mkit.io.save(output_file, tet, point_data={"disp": disp})


if __name__ == "__main__":
Expand Down

0 comments on commit d625a56

Please sign in to comment.