python自定义vtk算法
vtkPythonAlgorithm is great
使用vtkPythonAlgorithm
- 定义vtkPythonAlgorithm对象
- 通过SetPythonObject()使用自定义算法对象
- 调用Initialize(self, vtkself)把vtkPythonAlgorithm引用传递给自定义算法对象
- 将以下方法委托给自定义算法对象
- ProcessRequest(self, vtkself, request, inInfo, outInfo)
- FillInputPortInformation(self, vtkself, port, info)
- FillOutputPortInformation(self, vtkself, port, info)
自定义算法对象需要实现 Initialize(self, vtkself),ProcessRequest(self, vtkself, request, inInfo, outInfo),
FillInputPortInformation(self, vtkself, port, info),FillOutputPortInformation(self, vtkself, port, info)
import vtk
class MyAlgorithm(object):
# 设置输入输出的个数
def Initialize(self, vtkself):
vtkself.SetNumberOfInputPorts(1)
vtkself.SetNumberOfOutputPorts(1)
# 设置输入信息
def FillInputPortInformation(self, vtkself, port, info):
info.Set(vtk.vtkAlgorithm.INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet")
return 1
# 设置输出信息
def FillOutputPortInformation(self, vtkself, port, info):
info.Set(vtk.vtkDataObject.DATA_TYPE_NAME(), "vtkPolyData")
return 1
# def ProcessRequest(self, vtkself, request, inInfo, outInfo):
# if request.Has(vtk.vtkDemandDrivenPipeline.REQUEST_DATA()):
# print ('I am supposed to execute')
# return 1
# 处理数据的流程
def ProcessRequest(self, vtkself, request, inInfo, outInfo):
if request.Has(vtk.vtkDemandDrivenPipeline.REQUEST_DATA()):
inp = inInfo[0].GetInformationObject(0).Get(vtk.vtkDataObject.DATA_OBJECT())
#inp = vtk.vtkDataSet.GetData(inInfo[0])
opt = outInfo.GetInformationObject(0).Get(vtk.vtkDataObject.DATA_OBJECT())
#opt = vtk.vtkPolyData.GetData(outInfo)
cf = vtk.vtkContourFilter()
cf.SetInputData(inp)
cf.SetValue(0, 200)
sf = vtk.vtkShrinkPolyData()
sf.SetInputConnection(cf.GetOutputPort())
sf.Update()
opt.ShallowCopy(sf.GetOutput())
return 1
# 测试
w = vtk.vtkRTAnalyticSource()
pa = vtk.vtkPythonAlgorithm()
pa.SetPythonObject(MyAlgorithm())
pa.SetInputConnection(w.GetOutputPort())
pa.Update()
print( pa.GetOutputDataObject(0).GetClassName())
print( pa.GetOutputDataObject(0).GetNumberOfCells())
使用VTKPythonAlgorithmBase
VTKPythonAlgorithmBase是 vtkPythonAlgorithm的子类,方便于定义vtk算法。
import vtk
from vtk.util.vtkAlgorithm import VTKPythonAlgorithmBase
class ContourShrink(VTKPythonAlgorithmBase):
# 构造函数设置输入输出个数和参数
def __init__(self):
VTKPythonAlgorithmBase.__init__(self,
nInputPorts=1, inputType='vtkDataSet',
nOutputPorts=1, outputType='vtkPolyData')
self.__ShrinkFactor = 0.5
self.__ContourValue = 200
def SetShrinkFactor(self, factor):
if factor != self.__ShrinkFactor:
self.__ShrinkFactor = factor
self.Modified()
def GetShrinkFactor(self):
return self.__ShrinkFactor
def SetContourValue(self, value):
if value != self.__ContourValue:
self.__ContourValue = value
self.Modified()
def GetContourValue(self):
return self.__ContourValue
def RequestData(self, request, inInfo, outInfo):
print ('Executing')
inp = vtk.vtkDataSet.GetData(inInfo[0])
opt = vtk.vtkPolyData.GetData(outInfo)
cf = vtk.vtkContourFilter()
cf.SetInputData(inp)
cf.SetValue(0, self.__ContourValue)
sf = vtk.vtkShrinkPolyData()
sf.SetShrinkFactor(self.__ShrinkFactor)
sf.SetInputConnection(cf.GetOutputPort())
sf.Update()
opt.ShallowCopy(sf.GetOutput())
return 1
# 测试
w = vtk.vtkRTAnalyticSource()
pa = ContourShrink()
pa.SetInputConnection(w.GetOutputPort())
pa.Update()
print (pa.GetOutputDataObject(0).GetClassName())
print (pa.GetOutputDataObject(0).GetNumberOfCells())
pa.SetShrinkFactor(0.7)
pa.SetContourValue(100)
pa.Update()
print (pa.GetOutputDataObject(0).GetClassName())
print (pa.GetOutputDataObject(0).GetNumberOfCells())