mesh_to_contours#

medicalimageanalysis.utils.roi.to_contours.mesh_to_contours(poly, origin, spacing, direction, positions=None, as_list=False, direction_rows=True)[source]

Cut a closed mesh into contours with exact planes normal to the image k axis.

Parameters:
  • poly (vtk.vtkPolyData) – Closed surface mesh in world coordinates (a pv.PolyData works too).

  • origin (tuple[float, float, float]) – World (x, y, z) of voxel (0, 0, 0).

  • spacing (tuple[float, float, float]) – Voxel spacing (sx, sy, sz) in mm. Only sz is used.

  • direction (np.ndarray) – 3x3 image direction matrix.

  • positions (array-like | None) – Cut positions in mm along k from origin. If None, every CT slice centre the mesh spans is cut. Use contour_positions to cut at the planes of an existing contour list.

  • as_list (bool) – If True, return a flat list of rings instead of a dictionary.

  • direction_rows (bool) – Layout of direction, forwarded to normalize_direction.

Returns:

{slice_index: [(n, 3) world rings]} keyed by the nearest CT slice, or a flat list of rings when as_list=True.

Return type:

dict[int, list[np.ndarray]] | list[np.ndarray]

Examples

Cut a sphere at every CT slice it spans:

>>> import numpy as np
>>> import pyvista as pv
>>> from medicalimageanalysis.utils.roi.to_contours import mesh_to_contours
>>> mesh = pv.Sphere(radius=10.0, center=(16.0, 16.0, 16.0))
>>> contours = mesh_to_contours(
...     mesh,
...     origin=(0.0, 0.0, 0.0),
...     spacing=(1.0, 1.0, 2.0),
...     direction=np.eye(3),
... )
>>> ring = contours[8][0]          # slice 8 -> z = 16 mm
>>> ring.shape[1]
3

Cut back at the planes the original contours were drawn on:

>>> from medicalimageanalysis.utils.roi.to_contours import contour_positions
>>> positions = contour_positions(original_rings, origin, direction)
>>> rings = mesh_to_contours(
...     mesh, origin, spacing, direction,
...     positions=positions,
...     as_list=True,
... )