MeshSlicer#
- class medicalimageanalysis.utils.mesh.slicer.MeshSlicer(mesh, surface_mesh=True, silence=True)[source]
Bases:
objectPersistent vtkPlaneCutter for static ROI surface meshes.
The cutter and its spatial index are built once in __init__. Each slice_transformed call only moves the plane, so repeated slicing while scrolling or rigid dragging stays fast.
- mesh
Surface being cut, in its own (untransformed) frame.
- Type:
pv.PolyData
- plane
Plane shared with the cutter; updated on every slice.
- Type:
vtk.vtkPlane
- cutter
Persistent cutter bound to mesh and plane.
- Type:
vtk.vtkPlaneCutter
Examples
Scroll axially through a sphere:
>>> import numpy as np >>> import pyvista as pv >>> from medicalimageanalysis.utils.mesh.slicer import MeshSlicer >>> slicer = MeshSlicer(pv.Sphere(radius=10.0)) >>> cuts = [slicer.slice_transformed((0, 0, 1), (0, 0, z), np.eye(4)) for z in range(-8, 9, 2)] >>> len(cuts) 9
Slice an ROI with the rigid registration currently applied to it:
>>> import medicalimageanalysis as mia >>> image = mia.Data.image[mia.Data.image_list()[0]] >>> slicer = MeshSlicer(image.roi['Liver'].mesh) >>> rigid = np.eye(4) >>> rigid[:3, 3] = (5.0, -2.0, 0.0) >>> cut = slicer.slice_transformed(normal=(0, 0, 1), origin=(0, 0, 120.0), matrix=rigid)
- slice_transformed(normal, origin, matrix)[source]
Slice the rigidly transformed mesh with a world-space plane.
The plane is moved into the mesh frame by the inverse of matrix, the static mesh is cut, and the cut is moved back by matrix.
- Parameters:
normal (array-like (3,)) – Plane normal in world space.
origin (array-like (3,)) – A point on the plane in world space, in mm.
matrix (np.ndarray (4, 4)) – Rigid registration matrix mapping mesh space to world. Use np.eye(4) when no registration is applied.
- Returns:
Intersection lines in world space, in mm. Empty PolyData if the plane misses the mesh.
- Return type:
pv.PolyData
Examples
Axial cut through the center of a 10 mm radius sphere:
>>> import numpy as np >>> import pyvista as pv >>> from medicalimageanalysis.utils.mesh.slicer import MeshSlicer >>> sphere = pv.Sphere(radius=10.0, theta_resolution=64, phi_resolution=64) >>> slicer = MeshSlicer(sphere) >>> cut = slicer.slice_transformed(normal=(0, 0, 1), origin=(0, 0, 0), matrix=np.eye(4)) >>> round(float(np.linalg.norm(cut.points[:, :2], axis=1).max()), 1) 10.0
Sphere moved 5 mm in z; cutting at z = 5 mm hits its new equator:
>>> rigid = np.eye(4) >>> rigid[:3, 3] = (0.0, 0.0, 5.0) >>> cut = slicer.slice_transformed(normal=(0, 0, 1), origin=(0, 0, 5), matrix=rigid) >>> round(float(cut.points[:, 2].mean()), 3) 5.0 >>> round(float(np.linalg.norm(cut.points[:, :2], axis=1).max()), 1) 10.0
A plane outside the mesh returns an empty result:
>>> slicer.slice_transformed(normal=(0, 0, 1), origin=(0, 0, 20), matrix=np.eye(4)).n_points 0
- update_mesh(new_mesh, surface_mesh=True)[source]
Point the cutter at a new mesh.
Call this only when the geometry itself changes (re-contour, smooth, decimate). A rigid move does not need it; pass the matrix to slice_transformed instead.
- Parameters:
new_mesh (pv.PolyData | pv.UnstructuredGrid) – Replacement mesh, in mm.
surface_mesh (bool) – True if new_mesh is already a surface. False extracts the outer surface first.
- Return type:
None
Examples
Swap a 10 mm sphere for a 5 mm one:
>>> import numpy as np >>> import pyvista as pv >>> from medicalimageanalysis.utils.mesh.slicer import MeshSlicer >>> slicer = MeshSlicer(pv.Sphere(radius=10.0, theta_resolution=64, phi_resolution=64)) >>> slicer.update_mesh(pv.Sphere(radius=5.0, theta_resolution=64, phi_resolution=64)) >>> cut = slicer.slice_transformed(normal=(0, 0, 1), origin=(0, 0, 0), matrix=np.eye(4)) >>> round(float(np.linalg.norm(cut.points, axis=1).max()), 1) 5.0