Surface Mesh Tools

Surfaces from labelmaps and masks, surface remeshing, smoothing and combining, and conversions from surfaces back to images.

Module Reference

Tools for creating and manipulating surface meshes.

This module provides the ProcessSurfaceMesh class: surfaces from labelmaps and masks, surface remeshing, smoothing and combining, and the conversions from surfaces back to images (masks, labelmaps, distance maps, deformation fields). Tetrahedral meshes live in ProcessVolumetricMesh; the GPU primitives both use live in ProcessMeshKernels.

class monai_physio.process_surface_mesh.ProcessSurfaceMesh(log_level=20)[source]

Tools for creating and manipulating surface meshes.

LABEL_SURFACE_PAD = 3
__init__(log_level=20)[source]

Initialize ProcessSurfaceMesh.

Parameters:

log_level (int | str) – Logging level (default: logging.INFO)

extract_label_surfaces(labelmap_image, isotropic_spacing_mm=None, distance_sigma_mm=None, smoothing_iterations=30, label_ids=None, surface_reduction_rate=0.0)[source]

Extract each label’s closed surface, conforming with its neighbors.

Contouring each label on its own traces its voxel block edges: on anisotropic data the result terraces at the slice pitch, and a wall between two labels is meshed twice, in copies that do not match. This extracts the labels together instead:

  1. The labelmap is resampled onto an isotropic grid with a label-aware Gaussian vote over a physical-space kernel rather than a nearest voxel. That is what removes the terracing: a boundary between two slices lands between them instead of on one of them.

  2. Every label, plus the background, gets a signed distance map on that grid, and each voxel goes to the label whose map is smallest. The assignment is a partition, so no gap or overlap can arise.

  3. Label L’s surface is the zero level of the margin between the closest and the second closest map, negated where L is the closest. On a wall between L and M that field is the negation of M’s, so marching cubes puts identical vertices on both surfaces and the two meet exactly, including where three labels meet.

  4. The surfaces are merged, which welds those coincident vertices, then Taubin-smoothed as one mesh. Smoothing therefore moves a shared vertex once and the surfaces stay in contact.

Structures thinner than the interpolation kernel lose volume, coronary arteries most of all; the fraction of each label’s voxel volume that the surface encloses is logged at debug level.

Parameters:
  • labelmap_image (image) – Multi-label image; every non-zero label present is extracted. A binary mask yields a single surface.

  • isotropic_spacing_mm (Optional[float]) – Edge length of the isotropic grid the surfaces are contoured on, which sets both their smoothness and their triangle count. None uses the labelmap’s finest spacing.

  • distance_sigma_mm (Optional[float]) – Blur applied to the distance maps, which is what takes the voxel facets out of the contoured surface. None uses the isotropic spacing; raising it smooths further and thins the smallest structures.

  • smoothing_iterations (int) – Taubin smoothing iterations (0 disables).

  • label_ids (Optional[Sequence[int]]) – Labels to extract; every other label is treated as background. None extracts every label. To extract a union of labels as one surface, relabel them to one id first.

  • surface_reduction_rate (float) – Fraction of each surface’s triangles to remove by remesh_and_smooth_surface() (0.0 disables). Each label is remeshed on its own, so neighboring surfaces no longer share their vertices; lower the triangle count with a coarser isotropic_spacing_mm instead to keep them matching.

Return type:

dict[int, PolyData]

Returns:

Label id → that label’s closed, outward-oriented surface, whose field_data['SegmentationLabelIds'] holds the id. A label too small to survive the isotropic grid is left out, so the mapping is empty when the labelmap holds no non-zero label and may be missing labels that it does hold.

static is_watertight(surface)[source]

Report whether every edge of surface is shared by exactly two faces.

A surface with no faces has no edge that fails the test, so it is reported as not watertight rather than vacuously watertight.

Return type:

bool

remesh_and_smooth_surface(surface, surface_reduction_rate=0.0, smoothing_iterations=0)[source]

Optionally remesh then smooth a surface (no-op when disabled).

Reduction is isotropic remeshing (ACVD, via pyacvd) rather than decimation: the surface is re-tiled with uniform, well-shaped triangles at the requested resolution. decimate_pro reaches the same triangle count but leaves a watertight input non-watertight; ACVD does not.

Remeshing rebuilds the topology and so discards cell data, exactly as decimate_pro did: per-cell SegmentationLabelIds (which structure each triangle belongs to, as combine_surfaces() writes it) is transferred back onto the new cells from their nearest original cell so anatomy materials and structure ids still apply. Smoothing uses non-shrinking Taubin smoothing, which only moves points and therefore preserves cells and their labels. It is told to move non-manifold points too, since on a combined surface the walls where structures meet are non-manifold and VTK pins those points otherwise; on a manifold surface the setting has nothing to act on.

Parameters:
  • surface (PolyData) – Input surface.

  • surface_reduction_rate (float) – Fraction of triangles to remove (0.0 disables).

  • smoothing_iterations (int) – Taubin smoothing iterations (0 disables).

Return type:

PolyData

Returns:

The remeshed and/or smoothed surface.

static extract_surface(mesh)[source]

Extract the surface of a mesh.

Parameters:

mesh (DataSet) – Input mesh (PolyData is returned unchanged; any other DataSet is passed through extract_surface).

Returns:

The surface of the mesh.

Return type:

pv.PolyData

static transform_contours(contours, tfm, with_deformation_magnitude=False)[source]

Transform contours using a given transform.

Parameters:

tfm (itk.Transform) – The transform to use

Returns:

The transformed contours with deformation magnitude

Return type:

pv.PolyData

merge_meshes(meshes)[source]

Merge multiple fixed meshes into a single mesh.

Returns:

Merged mesh

Return type:

pv.PolyData

static create_reference_image(mesh, spatial_resolution=0.5, buffer_factor=0.25, ptype=itk.F)[source]

Create a reference image from a mesh.

Return type:

Image

static create_mask_from_mesh(mesh, reference_image)[source]
Return type:

itk.Image

create_labelmap_from_meshes(meshes, reference_image)[source]

Create a labelmap from a list of meshes.

Return type:

itk.Image

static sample_mesh_faces(mesh, max_spacing)[source]

Return mesh points supplemented by samples across the triangle faces.

Rasterizing vertices alone leaves gaps between them on meshes that are coarse relative to the voxel size, which makes a distance map built from them ripple. Adding barycentric samples dense enough that consecutive samples are closer than max_spacing closes those gaps.

Parameters:
  • mesh (DataSet) – Source mesh; its surface is triangulated if needed.

  • max_spacing (float) – Target spacing between samples, in mm.

Return type:

ndarray

Returns:

(n, 3) array of sample points, starting with the mesh’s own points.

create_distance_map(mesh, reference_image, squared_distance=False, negative_inside=True, zero_inside=False, norm_to_max_distance=0.0, sample_faces=True)[source]

Compute a distance map of a mesh on the reference image’s grid.

Parameters:
  • mesh (pv.DataSet | pv.UnstructuredGrid) – Mesh whose surface the distances are measured to.

  • reference_image (itk.Image) – Image defining the output grid.

  • squared_distance (bool) – Sign-preserving square of the result. Default: False

  • negative_inside (bool) – Keep the signed output. Default: True

  • zero_inside (bool) – Clip negative values to zero before anything else. Default: False

  • norm_to_max_distance (float) – If non-zero, divide by this value and clip to [-1, 1]. Default: 0.0 (distances stay in mm)

  • sample_faces (bool) – Rasterize samples across the triangle faces as well as the vertices, so that coarse meshes do not leave gaps in the rasterized surface. Default: True

Return type:

itk.Image

Returns:

ITK image of distances on the reference grid.

static create_deformation_field(points, point_displacements, reference_image, blur_sigma=2.5, ptype=itk.D)[source]

Create a displacement map from model points and displacements.

Return type:

Image

static combine_surfaces(surfaces)[source]

Merge surfaces into one mesh that still tells its structures apart.

Each cell gains a SegmentationLabelIds (int32) entry naming the label it came from, taken from its surface’s single-entry field_data['SegmentationLabelIds']; a surface holding several ids, such as a group surface, is tagged 0. ConvertVTKToUSD splits on that array, giving one prim and one anatomy material per structure. The per-cell Color survives the merge; per-object field_data (AnatomyGroup, SegmentationLabelNames, AnatomyColor) cannot, since one mesh holds one value per key.

Raises:

ValueError – If surfaces is empty.

Return type:

PolyData

Navigation

Transform Tools | Utility Modules | Volumetric Mesh Tools