vtkInterface.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import vtk, qt, ctk, slicer
  2. import numpy as np
  3. class vtkInterface:
  4. def __init__(self, parent):
  5. parent.title = "vtk Interface"
  6. parent.categories = ["Examples"]
  7. parent.dependencies = []
  8. parent.contributors = ["Andrej Studen (FMF/JSI)"] # replace with "Firstname Lastname (Org)"
  9. parent.helpText = """
  10. Convert native numpy data structures to vtk
  11. """
  12. parent.acknowledgementText = """
  13. This module was developed within the frame of the ARRS sponsored medical
  14. physics research programe to investigate quantitative measurements of cardiac
  15. function using sestamibi-like tracers
  16. """ # replace with organization, grant and thanks.
  17. self.parent = parent
  18. def numpyToVTK3D(numpy_array, origin, spacing):
  19. v=vtk.vtkImageData()
  20. v.SetDimensions(
  21. numpy_array.shape[0],
  22. numpy_array.shape[1],
  23. numpy_array.shape[2])
  24. v.SetOrigin(origin[0], origin[1], origin[2])
  25. v.SetSpacing(spacing[0], spacing[1], spacing[2])
  26. scalars = vtk.vtkShortArray()
  27. for k in range(0,numpy_array.shape[2]):
  28. kOffset = k * numpy_array.shape[1]*numpy_array.shape[0]
  29. for j in range(0,numpy_array.shape[1]):
  30. jOffset = j * numpy_array.shape[0]
  31. for i in range(0,numpy_array.shape[0]):
  32. offset = i + jOffset + kOffset;
  33. scalars.InsertTuple1(offset,numpy_array[i,j,k])
  34. v.GetPointData().SetScalars(scalars)
  35. return v