Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Rotation applied in surface_image_stencil() based on image orientation #147

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/deepali/utils/simpleitk/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def __init__(
if center is None:
origin = (0.0,) * ndim
else:
offset = np.array(0.5 * n if n > 0 else 0 for n in self._int_size(size))
rotation = np.array(self.direction).reshape(ndim, ndim)
offset = np.array([0.5 * n if n > 0 else 0 for n in self._int_size(size)])
rotation = np.array(direction).reshape(ndim, ndim)
scaling = np.diag(spacing)
coords: NDArray = np.asanyarray(center) - np.matmul(rotation @ scaling, offset)
origin = tuple(float(x) for x in coords)
Expand All @@ -68,7 +68,7 @@ def __init__(
@property
def center(self) -> Tuple[float, ...]:
r"""Get grid center point coordinates in world space."""
offset = np.array(0.5 * n if n > 0 else 0 for n in self.size)
offset = np.array([0.5 * n if n > 0 else 0 for n in self.size])
coords: NDArray = self.origin + np.matmul(self.transform[:-1, :-1], offset)
return tuple(float(x) for x in coords)

Expand Down
24 changes: 13 additions & 11 deletions src/deepali/utils/vtk/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
vtkImageData,
vtkImageStencilData,
vtkImageStencilToImage,
vtkMatrixToLinearTransform,
vtkPolyData,
vtkPolyDataToImageStencil,
vtkTransform,
vtkTransformPolyDataFilter,
)

Expand Down Expand Up @@ -42,27 +42,29 @@ def surface_mesh_grid(*mesh: vtkPolyData, resolution: Optional[float] = None) ->


def surface_image_stencil(mesh: vtkPolyData, grid: Grid) -> vtkImageStencilData:
r"""Convert vtkPolyData surface mesh to image stencil."""
max_index = [n - 1 for n in grid.size().tolist()]

rot = np.eye(4, dtype=np.float)
rot[:3, :3] = np.array(grid.direction).reshape(3, 3)
rot = numpy_to_vtk_matrix4x4(rot)

transform = vtkMatrixToLinearTransform()
transform.SetInput(rot)

r"""Convert vtkPolyData surface mesh to image stencil."""
# Create the transform
transform = vtkTransform()
transform.Translate(grid.center().tolist())
transform.Concatenate(numpy_to_vtk_matrix4x4(grid.direction().numpy().T)) # type: ignore
transform.Translate(grid.center().neg().tolist())

# Apply the transform to the polydata
transformer = vtkTransformPolyDataFilter()
transformer.SetInputData(mesh)
transformer.SetTransform(transform)

# Convert the transformed polydata to an image stencil
grid = Grid(size=grid.size(), spacing=grid.spacing(), center=grid.center())
max_index = [n - 1 for n in grid.size()]
converter = vtkPolyDataToImageStencil()
converter.SetInputConnection(transformer.GetOutputPort())
converter.SetOutputOrigin(grid.origin().tolist())
converter.SetOutputSpacing(grid.spacing().tolist())
converter.SetOutputWholeExtent([0, max_index[0], 0, max_index[1], 0, max_index[2]])
converter.Update()

# Get the output stencil
stencil = vtkImageStencilData()
stencil.DeepCopy(converter.GetOutput())
return stencil
Expand Down
8 changes: 5 additions & 3 deletions src/deepali/utils/vtk/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@

def numpy_to_vtk_matrix4x4(arr: np.ndarray) -> vtkMatrix4x4:
"""Create vtkMatrix4x4 from NumPy array."""
assert arr.shape == (4, 4)
if arr.shape not in [(3, 3), (3, 4), (4, 4)]:
raise ValueError("numpy_to_vtk_matrix4x4() 'arr' must have shape (3, 3), (3, 4), or (4, 4)")
matrix = vtkMatrix4x4()
for i in range(4):
for j in range(4):
matrix.Identity()
for i in range(arr.shape[0]):
for j in range(arr.shape[1]):
matrix.SetElement(i, j, arr[i, j])
return matrix

Expand Down