Contour Tools

Contour extraction and processing utilities.

Module Reference

Tools for creating and manipulating contours.

class physiotwin4d.contour_tools.ContourTools(log_level=20)[source]

Tools for creating and manipulating contours.

__init__(log_level=20)[source]

Initialize ContourTools.

Parameters:

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

extract_contours(labelmap_image)[source]

Make contours from a labelmap image.

Parameters:

labelmap_image (itk.image) – The labelmap image to create contours from

Returns:

The contours as a PyVista PolyData object

Return type:

pv.PolyData

extract_mesh(surface, mesh_target_reduction=0.0)[source]

Generate a tetrahedral volume mesh (VTU) from a closed surface via netgen.

Optionally decimates the surface with pyvista.PolyDataFilters.decimate_pro() before meshing — netgen has no post-hoc decimation of its own, so a coarser input surface is the only way to get a coarser tetrahedral mesh out.

Builds netgen’s surface mesh directly from surface’s indexed points/triangles rather than round-tripping through an STL file. STL has no shared-vertex topology, so every triangle corner is written independently; float32 rounding during that round-trip can make two corners that were exactly the same point diverge by ~1e-6, which is well within netgen’s own “identical point” merge tolerance and makes its STL reader spin forever trying to reconcile them (observed hang on Taubin-smoothed surfaces, e.g. from extract_contours()). Adding points/triangles directly preserves the exact shared-vertex indices already present in surface, so there is nothing for netgen to reconcile.

Parameters:
  • surface (PolyData) – Closed, triangulated surface for one anatomy group (as returned by extract_contours()).

  • mesh_target_reduction (float) – Fraction in [0, 1) of surface triangles to remove via decimate_pro(mesh_target_reduction, preserve_topology=True) before meshing. 0.0 (default) skips decimation and meshes the surface as given.

Return type:

Optional[UnstructuredGrid]

Returns:

pyvista.UnstructuredGrid of tetrahedral cells, or None if the surface is empty or netgen produced no volume mesh from it.

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

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_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

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

itk.Image

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 save_surfaces(surfaces, output_dir, prefix='')[source]

Save each named surface to its own VTP file.

Parameters:
  • surfaces (dict[str, PolyData]) – Mapping of name → surface (e.g. the 'surfaces' value from WorkflowConvertImageToVTK.process()).

  • output_dir (str) – Directory to write files into (created if absent).

  • prefix (str) – Optional filename prefix. Each file is named {prefix}_{name}.vtp (or {name}.vtp when prefix is empty).

Return type:

dict[str, str]

Returns:

Mapping of name → absolute path of the saved file.

static save_meshes(meshes, output_dir, prefix='')[source]

Save each named volume mesh to its own VTU file.

Parameters:
  • meshes (dict[str, UnstructuredGrid]) – Mapping of name → mesh (e.g. the 'meshes' value from WorkflowConvertImageToVTK.process()).

  • output_dir (str) – Directory to write files into (created if absent).

  • prefix (str) – Optional filename prefix. Each file is named {prefix}_{name}.vtu (or {name}.vtu when prefix is empty).

Return type:

dict[str, str]

Returns:

Mapping of name → absolute path of the saved file.

static save_combined_surface(surfaces, output_dir, prefix='')[source]

Merge all named surfaces into a single VTP file.

The merged mesh retains per-cell Color (RGBA uint8) from each surface’s annotation, enabling colour-by-anatomy rendering in Paraview, PyVista, etc. Per-object field_data is not preserved in the merged file.

Parameters:
  • surfaces (dict[str, PolyData]) – Mapping of name → surface.

  • output_dir (str) – Directory to write the file into (created if absent).

  • prefix (str) – Optional filename prefix. Output is {prefix}_surfaces.vtp (or surfaces.vtp when prefix is empty).

Return type:

str

Returns:

Absolute path to the saved VTP file.

Raises:

ValueError – If surfaces is empty.

static save_combined_mesh(meshes, output_dir, prefix='')[source]

Merge all named volume meshes into a single VTU file.

The merged mesh retains per-cell Color (RGBA uint8) from each mesh’s annotation. Per-object field_data is not preserved in the merged file.

Parameters:
  • meshes (dict[str, UnstructuredGrid]) – Mapping of name → volume mesh.

  • output_dir (str) – Directory to write the file into (created if absent).

  • prefix (str) – Optional filename prefix. Output is {prefix}_meshes.vtu (or meshes.vtu when prefix is empty).

Return type:

str

Returns:

Absolute path to the saved VTU file.

Raises:

ValueError – If meshes is empty.

Navigation

Transform Tools | Utility Modules | nrrd_conversion | Test Tools