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 e554ec3
Showing 1 changed file with 13 additions and 11 deletions.
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

0 comments on commit e554ec3

Please sign in to comment.