vtkInterface.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. #print '.',
  29. kOffset = k * numpy_array.shape[1]*numpy_array.shape[0]
  30. for j in range(0,numpy_array.shape[1]):
  31. jOffset = j * numpy_array.shape[0]
  32. for i in range(0,numpy_array.shape[0]):
  33. offset = i + jOffset + kOffset;
  34. scalars.InsertTuple1(offset,numpy_array[i,j,k])
  35. #print
  36. v.GetPointData().SetScalars(scalars)
  37. return v
  38. def completeOrientation(orientation):
  39. o=orientation
  40. o.append(o[1]*o[5]-o[2]*o[4])#0,3
  41. o.append(o[2]*o[3]-o[0]*o[5])#1,4
  42. o.append(o[0]*o[4]-o[1]*o[3])#2,5
  43. return o