Skip to content

Commit

Permalink
fix: Rotation applied in surface_image_stencil() based on image orien…
Browse files Browse the repository at this point in the history
…tation
  • Loading branch information
aschuh-hf committed Oct 18, 2024
1 parent 1eab935 commit 0588872
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 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
stencil_grid = Grid(size=grid.size(), spacing=grid.spacing(), center=grid.center())
stencil_extent = [0, grid.size(0) - 1, 0, grid.size(1) - 1, 0, grid.size(2) - 1]
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.SetOutputOrigin(stencil_grid.origin().tolist())
converter.SetOutputSpacing(stencil_grid.spacing().tolist())
converter.SetOutputWholeExtent(stencil_extent)
converter.Update()

# Get the output stencil
stencil = vtkImageStencilData()
stencil.DeepCopy(converter.GetOutput())
return stencil
Expand Down

0 comments on commit 0588872

Please sign in to comment.