contours_to_mesh#

medicalimageanalysis.utils.roi.to_mesh.contours_to_mesh(contours, origin, spacing, direction, reduction=0.5, interpolate=False, as_polydata=True, direction_rows=True, **kw)[source]

Build a closed surface mesh from contours.

Each separate region gets its own field (and its own h); the combined mesh is decimated once at the end. By default only contours on adjacent image slices are joined, so missing slices leave gaps.

Parameters:
  • contours (list[np.ndarray]) – World-space (n, 3) rings.

  • origin (tuple[float, float, float]) – World (x, y, z) of voxel (0, 0, 0) of the grid the contours were drawn on.

  • spacing (tuple[float, float, float]) – Voxel spacing (sx, sy, sz) in mm of the grid the contours were drawn on.

  • direction (np.ndarray) – 3x3 image direction matrix of the grid the contours were drawn on.

  • reduction (float) – Target decimation fraction (0 = no decimation, 0.5 = remove ~half the triangles).

  • interpolate (bool) – How far apart contour planes can be and still be joined. False (default): only contours on adjacent image slices are joined, using the slice spacing from spacing and direction. A missing slice leaves a gap, and each side is capped half a slice past its last contour. origin, spacing and direction must describe the grid the contours were drawn on (for RTSTRUCT, the referenced image). True: the slice spacing is the median spacing between contoured slices, so missing slices are filled by shape interpolation. Use this for deliberately sparse contours, contours drawn on a different grid, or sagittal / coronal contours. Gaps larger than 1.5x that spacing are still left open.

  • as_polydata (bool) – If True, return vtkPolyData. If False, return (points, triangles) arrays.

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

  • **kw – _contourmesh options: m, c, gap_factor, pad, workers, mid, flat_caps, cap_radius.

Returns:

Closed surface mesh, or (points, triangles) when as_polydata=False.

Return type:

vtk.vtkPolyData | tuple[np.ndarray, np.ndarray]

Examples

Mesh a stack of contours:

>>> import numpy as np
>>> import pyvista as pv
>>> from medicalimageanalysis.utils.roi.to_contours import mask_to_contours
>>> from medicalimageanalysis.utils.roi.to_mesh import contours_to_mesh
>>> origin, spacing, direction = (0.0, 0.0, 0.0), (1.0, 1.0, 2.0), np.eye(3)
>>> box = np.zeros((8, 32, 32), dtype=np.uint8)
>>> box[2:6, 8:24, 8:24] = 1
>>> rings = mask_to_contours(box, origin, spacing, direction, as_list=True)
>>> mesh = pv.wrap(contours_to_mesh(rings, origin, spacing, direction))
>>> mesh.is_manifold
True

Missing slices are left as gaps by default; interpolate=True fills them:

>>> box = np.zeros((8, 32, 32), dtype=np.uint8)
>>> box[2:7, 8:24, 8:24] = 1
>>> by_slice = mask_to_contours(box, origin, spacing, direction)
>>> sparse = by_slice[2] + by_slice[4]
>>> gapped = pv.wrap(contours_to_mesh(sparse, origin, spacing, direction))
>>> filled = pv.wrap(contours_to_mesh(sparse, origin, spacing, direction,
...                                   interpolate=True))
>>> len(np.unique(gapped.connectivity().point_data['RegionId']))
2
>>> len(np.unique(filled.connectivity().point_data['RegionId']))
1

Keep every triangle (no decimation) and get raw arrays:

>>> points, triangles = contours_to_mesh(
...     rings, origin, spacing, direction,
...     reduction=0,
...     as_polydata=False,
... )
>>> points.shape[1], triangles.shape[1]
(3, 3)

Use multiple worker threads for large structures:

>>> mesh = contours_to_mesh(rings, origin, spacing, direction, workers=8)