vtkAlgorithm.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. from vtkmodules.vtkCommonCore import vtkInformation
  2. from vtkmodules.vtkCommonDataModel import vtkDataObject
  3. from vtkmodules.vtkCommonExecutionModel import vtkAlgorithm
  4. from vtkmodules.vtkCommonExecutionModel import vtkDemandDrivenPipeline
  5. from vtkmodules.vtkCommonExecutionModel import vtkStreamingDemandDrivenPipeline
  6. from vtkmodules.vtkFiltersPython import vtkPythonAlgorithm
  7. class VTKAlgorithm(object):
  8. """This is a superclass which can be derived to implement
  9. Python classes that work with vtkPythonAlgorithm. It implements
  10. Initialize(), ProcessRequest(), FillInputPortInformation() and
  11. FillOutputPortInformation().
  12. Initialize() sets the input and output ports based on data
  13. members.
  14. ProcessRequest() calls RequestXXX() methods to implement
  15. various pipeline passes.
  16. FillInputPortInformation() and FillOutputPortInformation() set
  17. the input and output types based on data members.
  18. """
  19. def __init__(self, nInputPorts=1, inputType='vtkDataSet',
  20. nOutputPorts=1, outputType='vtkPolyData'):
  21. """Sets up default NumberOfInputPorts, NumberOfOutputPorts,
  22. InputType and OutputType that are used by various initialization
  23. methods."""
  24. self.NumberOfInputPorts = nInputPorts
  25. self.NumberOfOutputPorts = nOutputPorts
  26. self.InputType = inputType
  27. self.OutputType = outputType
  28. def Initialize(self, vtkself):
  29. """Sets up number of input and output ports based on
  30. NumberOfInputPorts and NumberOfOutputPorts."""
  31. vtkself.SetNumberOfInputPorts(self.NumberOfInputPorts)
  32. vtkself.SetNumberOfOutputPorts(self.NumberOfOutputPorts)
  33. def GetInputData(self, inInfo, i, j):
  34. """Convenience method that returns an input data object
  35. given a vector of information objects and two indices."""
  36. return inInfo[i].GetInformationObject(j).Get(vtkDataObject.DATA_OBJECT())
  37. def GetOutputData(self, outInfo, i):
  38. """Convenience method that returns an output data object
  39. given an information object and an index."""
  40. return outInfo.GetInformationObject(i).Get(vtkDataObject.DATA_OBJECT())
  41. def RequestDataObject(self, vtkself, request, inInfo, outInfo):
  42. """Overwritten by subclass to manage data object creation.
  43. There is not need to overwrite this class if the output can
  44. be created based on the OutputType data member."""
  45. return 1
  46. def RequestInformation(self, vtkself, request, inInfo, outInfo):
  47. """Overwritten by subclass to provide meta-data to downstream
  48. pipeline."""
  49. return 1
  50. def RequestUpdateExtent(self, vtkself, request, inInfo, outInfo):
  51. """Overwritten by subclass to modify data request going
  52. to upstream pipeline."""
  53. return 1
  54. def RequestData(self, vtkself, request, inInfo, outInfo):
  55. """Overwritten by subclass to execute the algorithm."""
  56. raise NotImplementedError('RequestData must be implemented')
  57. def ProcessRequest(self, vtkself, request, inInfo, outInfo):
  58. """Splits a request to RequestXXX() methods."""
  59. if request.Has(vtkDemandDrivenPipeline.REQUEST_DATA_OBJECT()):
  60. return self.RequestDataObject(vtkself, request, inInfo, outInfo)
  61. elif request.Has(vtkDemandDrivenPipeline.REQUEST_INFORMATION()):
  62. return self.RequestInformation(vtkself, request, inInfo, outInfo)
  63. elif request.Has(vtkStreamingDemandDrivenPipeline.REQUEST_UPDATE_EXTENT()):
  64. return self.RequestUpdateExtent(vtkself, request, inInfo, outInfo)
  65. elif request.Has(vtkDemandDrivenPipeline.REQUEST_DATA()):
  66. return self.RequestData(vtkself, request, inInfo, outInfo)
  67. return 1
  68. def FillInputPortInformation(self, vtkself, port, info):
  69. """Sets the required input type to InputType."""
  70. info.Set(vtkAlgorithm.INPUT_REQUIRED_DATA_TYPE(), self.InputType)
  71. return 1
  72. def FillOutputPortInformation(self, vtkself, port, info):
  73. """Sets the default output type to OutputType."""
  74. info.Set(vtkDataObject.DATA_TYPE_NAME(), self.OutputType)
  75. return 1
  76. class VTKPythonAlgorithmBase(vtkPythonAlgorithm):
  77. """This is a superclass which can be derived to implement
  78. Python classes that act as VTK algorithms in a VTK pipeline.
  79. It implements ProcessRequest(), FillInputPortInformation() and
  80. FillOutputPortInformation().
  81. ProcessRequest() calls RequestXXX() methods to implement
  82. various pipeline passes.
  83. FillInputPortInformation() and FillOutputPortInformation() set
  84. the input and output types based on data members.
  85. Common use is something like this:
  86. class HDF5Source(VTKPythonAlgorithmBase):
  87. def __init__(self):
  88. VTKPythonAlgorithmBase.__init__(self,
  89. nInputPorts=0,
  90. nOutputPorts=1, outputType='vtkImageData')
  91. def RequestInformation(self, request, inInfo, outInfo):
  92. f = h5py.File("foo.h5", 'r')
  93. dims = f['RTData'].shape[::-1]
  94. info = outInfo.GetInformationObject(0)
  95. info.Set(vtkmodules.vtkCommonExecutionModel.vtkStreamingDemandDrivenPipeline.WHOLE_EXTENT(),
  96. (0, dims[0]-1, 0, dims[1]-1, 0, dims[2]-1), 6)
  97. return 1
  98. def RequestData(self, request, inInfo, outInfo):
  99. f = h5py.File("foo.h5", 'r')
  100. data = f['RTData'][:]
  101. output = dsa.WrapDataObject(vtkmodules.vtkCommonDataModel.vtkImageData.GetData(outInfo))
  102. output.SetDimensions(data.shape)
  103. output.PointData.append(data.flatten(), 'RTData')
  104. output.PointData.SetActiveScalars('RTData')
  105. return 1
  106. alg = HDF5Source()
  107. cf = vtkmodules.vtkFiltersCore.vtkContourFilter()
  108. cf.SetInputConnection(alg.GetOutputPort())
  109. cf.Update()
  110. """
  111. class InternalAlgorithm(object):
  112. "Internal class. Do not use."
  113. def Initialize(self, vtkself):
  114. pass
  115. def FillInputPortInformation(self, vtkself, port, info):
  116. return vtkself.FillInputPortInformation(port, info)
  117. def FillOutputPortInformation(self, vtkself, port, info):
  118. return vtkself.FillOutputPortInformation(port, info)
  119. def ProcessRequest(self, vtkself, request, inInfo, outInfo):
  120. return vtkself.ProcessRequest(request, inInfo, outInfo)
  121. def __init__(self, nInputPorts=1, inputType='vtkDataSet',
  122. nOutputPorts=1, outputType='vtkPolyData'):
  123. """Sets up default NumberOfInputPorts, NumberOfOutputPorts,
  124. InputType and OutputType that are used by various methods.
  125. Make sure to call this method from any subclass' __init__"""
  126. self.SetPythonObject(VTKPythonAlgorithmBase.InternalAlgorithm())
  127. self.SetNumberOfInputPorts(nInputPorts)
  128. self.SetNumberOfOutputPorts(nOutputPorts)
  129. self.InputType = inputType
  130. self.OutputType = outputType
  131. def GetInputData(self, inInfo, i, j):
  132. """Convenience method that returns an input data object
  133. given a vector of information objects and two indices."""
  134. return inInfo[i].GetInformationObject(j).Get(vtkDataObject.DATA_OBJECT())
  135. def GetOutputData(self, outInfo, i):
  136. """Convenience method that returns an output data object
  137. given an information object and an index."""
  138. return outInfo.GetInformationObject(i).Get(vtkDataObject.DATA_OBJECT())
  139. def FillInputPortInformation(self, port, info):
  140. """Sets the required input type to InputType."""
  141. info.Set(vtkAlgorithm.INPUT_REQUIRED_DATA_TYPE(), self.InputType)
  142. return 1
  143. def FillOutputPortInformation(self, port, info):
  144. """Sets the default output type to OutputType."""
  145. info.Set(vtkDataObject.DATA_TYPE_NAME(), self.OutputType)
  146. return 1
  147. def ProcessRequest(self, request, inInfo, outInfo):
  148. """Splits a request to RequestXXX() methods."""
  149. if request.Has(vtkDemandDrivenPipeline.REQUEST_DATA_OBJECT()):
  150. return self.RequestDataObject(request, inInfo, outInfo)
  151. elif request.Has(vtkDemandDrivenPipeline.REQUEST_INFORMATION()):
  152. return self.RequestInformation(request, inInfo, outInfo)
  153. elif request.Has(vtkStreamingDemandDrivenPipeline.REQUEST_UPDATE_EXTENT()):
  154. return self.RequestUpdateExtent(request, inInfo, outInfo)
  155. elif request.Has(vtkDemandDrivenPipeline.REQUEST_DATA()):
  156. return self.RequestData(request, inInfo, outInfo)
  157. return 1
  158. def RequestDataObject(self, request, inInfo, outInfo):
  159. """Overwritten by subclass to manage data object creation.
  160. There is not need to overwrite this class if the output can
  161. be created based on the OutputType data member."""
  162. return 1
  163. def RequestInformation(self, request, inInfo, outInfo):
  164. """Overwritten by subclass to provide meta-data to downstream
  165. pipeline."""
  166. return 1
  167. def RequestUpdateExtent(self, request, inInfo, outInfo):
  168. """Overwritten by subclass to modify data request going
  169. to upstream pipeline."""
  170. return 1
  171. def RequestData(self, request, inInfo, outInfo):
  172. """Overwritten by subclass to execute the algorithm."""
  173. raise NotImplementedError('RequestData must be implemented')