You can visualize the 3D geometry of the X-ray detector, camera pose, and CT volume using PyVista. This is useful for debugging the spatial relationship between the source, detector, and subject.
To enable visualization, ensure you have pyvista, trame, and vtk installed. The visualization process typically involves:
- Extracting a mesh from the CT volume using
drr_to_mesh or labelmap_to_mesh. - Generating the camera and detector geometry using
img_to_mesh. - Plotting the components (mesh, camera frustum, detector plane with DRR texture, and the principal ray) using a
pyvista.Plotter.
import pyvista
import torch
from diffdrr.data import load_example_ct
from diffdrr.drr import DRR
from diffdrr.pose import convert
from diffdrr.visualization import drr_to_mesh, img_to_mesh, labelmap_to_mesh, plot_drr
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# 1. Load subject and create mesh
subject = load_example_ct()
ct_mesh = drr_to_mesh(subject, "surface_nets", threshold=225)
# 2. Initialize DRR and create pose
drr = DRR(subject, sdd=1020.0, height=200, delx=2.0).to(device)
rot = torch.tensor([[45.0, 30.0, 0.0]], device=device) / 180 * torch.pi
xyz = torch.tensor([[0.0, 800.0, 0.0]], device=device)
pose = convert(rot, xyz, parameterization="euler_angles", convention="ZXY")
# 3. Get geometry for plotting
camera, detector, texture, principal_ray = img_to_mesh(drr, pose)
# 4. Plot
plotter = pyvista.Plotter()
plotter.add_mesh(ct_mesh)
plotter.add_mesh(camera, show_edges=True)
plotter.add_mesh(detector, texture=texture)
plotter.show()