mesh_longest_distance#

medicalimageanalysis.utils.roi.statistics.mesh_longest_distance(mesh, mode='all', array_shape=None, origin=None, spacing=None, direction=None, n_line_samples=20, exact_2d=False)[source]

Longest internal distance of a closed mesh, in 3D or per slice plane.

In ‘3d’ mode, hull vertex pairs are tested from longest to shortest and the first segment whose sample points all lie inside the mesh is returned. In the slice modes, the mesh is cut at every image slice position along that axis and contour_longest_distance is run on each cut.

Parameters:
  • mesh (pv.PolyData) – Closed surface mesh, already in mm.

  • mode (str) – ‘3d’, ‘axial’, ‘sagittal’, ‘coronal’, or ‘all’.

  • array_shape (tuple[int, int, int] | None) – Image size in x-y-z order. Required for every mode except ‘3d’.

  • origin (tuple[float, float, float] | None) – Image origin in mm (x-y-z). Required for every mode except ‘3d’.

  • spacing (tuple[float, float, float] | None) – Voxel spacing in mm (x-y-z). Required for every mode except ‘3d’.

  • direction (np.ndarray | None) – 3x3 matrix whose columns are the world direction of each array axis: physical = origin + direction @ (spacing * index). Identity if None.

  • n_line_samples (int) – Number of points sampled along a 3D candidate segment for the inside test.

  • exact_2d (bool) – Passed to contour_longest_distance as exact for the slice modes.

Returns:

Longest distance in mm. For mode=’all’, a dictionary with keys ‘3d’, ‘axial’, ‘sagittal’ and ‘coronal’.

Return type:

float | dict[str, float]

Raises:

ValueError – If the image geometry is missing for a slice mode, or mode is not recognized.

Examples

Longest 3D distance of a 10 mm radius sphere:

>>> import numpy as np
>>> import pyvista as pv
>>> from medicalimageanalysis.utils.roi.statistics import mesh_longest_distance
>>> mesh = pv.Sphere(radius=10.0)
>>> round(mesh_longest_distance(mesh, mode='3d'), 1)
20.0

Longest axial distance, cutting at the real CT slice positions (geometry in x-y-z order):

>>> axial = mesh_longest_distance(
...     mesh,
...     mode='axial',
...     array_shape=(32, 32, 16),
...     origin=(-16.0, -16.0, -16.0),
...     spacing=(1.0, 1.0, 2.0),
... )

Every measurement at once:

>>> result = mesh_longest_distance(
...     mesh,
...     mode='all',
...     array_shape=(32, 32, 16),
...     origin=(-16.0, -16.0, -16.0),
...     spacing=(1.0, 1.0, 2.0),
...     direction=np.eye(3),
... )
>>> sorted(result.keys())
['3d', 'axial', 'coronal', 'sagittal']