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
- 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:
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.
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.
Label
L’s surface is the zero level of the margin between the closest and the second closest map, negated whereLis the closest. On a wall betweenLandMthat field is the negation ofM’s, so marching cubes puts identical vertices on both surfaces and the two meet exactly, including where three labels meet.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.Noneuses 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.Noneuses 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.Noneextracts 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 byremesh_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:
- 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:
- 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_proreaches 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_prodid: per-cellSegmentationLabelIds(which structure each triangle belongs to, ascombine_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.
- 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 throughextract_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
- 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_spacingcloses those gaps.
- 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-entryfield_data['SegmentationLabelIds']; a surface holding several ids, such as a group surface, is tagged0.ConvertVTKToUSDsplits on that array, giving one prim and one anatomy material per structure. The per-cellColorsurvives the merge; per-objectfield_data(AnatomyGroup,SegmentationLabelNames,AnatomyColor) cannot, since one mesh holds one value per key.- Raises:
ValueError – If surfaces is empty.
- Return type:
PolyData
Navigation