Skip to content

Commit f62d016

Browse files
authored
Merge pull request #64 from dave3d/VTKDocstring
DOC: use Claude to improve the VTK docstrings
2 parents c57b0f7 + d2873ae commit f62d016

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

SimpleITK/utilities/vtk.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
#
1717
# ========================================================================
1818

19-
import SimpleITK as sitk
2019
import logging
20+
21+
import SimpleITK as sitk
2122
import vtk
2223
import vtk.util.numpy_support as vtknp
2324

@@ -35,14 +36,17 @@ def sitk2vtk(image: sitk.Image) -> vtk.vtkImageData:
3536
VTK images are fundamentally 3D, so 2D images are made 3D with
3637
a Z dimension of 1.
3738
38-
:param image: Image to convert.
39-
:return: A VTK image.
39+
:param image: SimpleITK image to convert (2D or 3D).
40+
:type image: sitk.Image
41+
:returns: A VTK image (vtkImageData) with the same data and metadata.
42+
:rtype: vtk.vtkImageData
43+
:raises ValueError: If the image is not 2D or 3D.
4044
"""
4145

4246
size = list(image.GetSize())
4347
if len(size) > 3:
4448
raise ValueError(
45-
"Conversion only supports 2D and 3D images, got {len(size)}D image"
49+
f"Conversion only supports 2D and 3D images, got {len(size)}D image"
4650
)
4751

4852
origin = image.GetOrigin()
@@ -73,6 +77,8 @@ def sitk2vtk(image: sitk.Image) -> vtk.vtkImageData:
7377
vtk_image.SetSpacing(spacing)
7478
vtk_image.SetOrigin(origin)
7579
vtk_image.SetExtent(0, size[0] - 1, 0, size[1] - 1, 0, size[2] - 1)
80+
81+
# Set direction matrix if supported by VTK version
7682
if vtk.vtkVersion.GetVTKMajorVersion() < 9:
7783
logger.warning(
7884
"VTK version <9 does not support direction matrix which is ignored"
@@ -93,13 +99,19 @@ def vtk2sitk(image: vtk.vtkImageData) -> sitk.Image:
9399
"""Convert a VTK image to a SimpleITK image.
94100
95101
Note that VTK images are fundamentally 3D, even if the Z
96-
dimension is 1.
102+
dimension is 1. The direction matrix is only copied for VTK
103+
version 9 or higher.
97104
98-
:param image: Image to convert.
99-
:return: A SimpleITK image.
105+
:param image: VTK image (vtkImageData) to convert.
106+
:type image: vtk.vtkImageData
107+
:returns: A SimpleITK image with the same data and metadata.
108+
:rtype: sitk.Image
100109
"""
101-
sd = image.GetPointData().GetScalars()
102-
npdata = vtknp.vtk_to_numpy(sd)
110+
# Extract scalar data and convert to numpy array
111+
scalar_data = image.GetPointData().GetScalars()
112+
npdata = vtknp.vtk_to_numpy(scalar_data)
113+
114+
# VTK uses C-order (XYZ), SimpleITK uses Fortran-order (ZYX)
103115
dims = list(image.GetDimensions())
104116
dims.reverse()
105117
ncomp = image.GetNumberOfScalarComponents()
@@ -108,13 +120,14 @@ def vtk2sitk(image: vtk.vtkImageData) -> sitk.Image:
108120

109121
npdata.shape = tuple(dims)
110122

123+
# Create SimpleITK image and set metadata
111124
sitk_image = sitk.GetImageFromArray(npdata)
112125
sitk_image.SetSpacing(image.GetSpacing())
113126
sitk_image.SetOrigin(image.GetOrigin())
114-
# By default, direction is identity.
115127

128+
# Set direction matrix if supported by VTK version
129+
# By default, direction is identity
116130
if vtk.vtkVersion.GetVTKMajorVersion() >= 9:
117-
# Copy the direction matrix into a list
118131
dir_mat = image.GetDirectionMatrix()
119132
direction = [0] * 9
120133
dir_mat.DeepCopy(direction, dir_mat)

0 commit comments

Comments
 (0)