一个简单的管线:
import vtk
from vtk.util.vtkAlgorithm import VTKPythonAlgorithmBase
class MySource(VTKPythonAlgorithmBase):
def __init__(self):
VTKPythonAlgorithmBase.__init__(self,
nInputPorts=0,
nOutputPorts=1, outputType='vtkPolyData')
def RequestInformation(self, request, inInfo, outInfo):
print "MySource RequestInformation:"
# print outInfo.GetInformationObject(0)
return 1
def RequestUpdateExtent(self, request, inInfo, outInfo):
print "MySource RequestUpdateExtent:"
# print outInfo.GetInformationObject(0)
return 1
def RequestData(self, request, inInfo, outInfo):
print "MySource RequestData:"
# print outInfo.GetInformationObject(0)
return 1
class MyFilter(VTKPythonAlgorithmBase):
def __init__(self):
VTKPythonAlgorithmBase.__init__(self,
nInputPorts=1, inputType='vtkPolyData',
nOutputPorts=1, outputType='vtkPolyData')
def RequestInformation(self, request, inInfo, outInfo):
print "MyFilter RequestInformation:"
# print outInfo.GetInformationObject(0)
return 1
def RequestUpdateExtent(self, request, inInfo, outInfo):
print "MyFilter RequestUpdateExtent:"
# print outInfo.GetInformationObject(0)
return 1
def RequestData(self, request, inInfo, outInfo):
print "MyFilter RequestData:"
# print outInfo.GetInformationObject(0)
return 1
测试:
s = MySource()
f = MyFilter()
f.SetInputConnection(s.GetOutputPort())
f.Update()
输出:
MySource RequestInformation:
MyFilter RequestInformation:
MyFilter RequestUpdateExtent:
MySource RequestUpdateExtent:
MySource RequestData:
MyFilter RequestData:
管线执行顺序
- RequestInformation
- RequestUpdateExtent ()
- RequestData
RequestInformation
RequestInformation
管线中元数据(meta-data
)的传递。主要包括一些时间戳和范围。从源到溯传递。
自定义传递的元数据
from vtk.util import keys
metaDataKey = keys.MakeKey(keys.DataObjectMetaDataKey, \
"a meta-data", "my module")
自定义元数据的值
class MySource(VTKPythonAlgorithmBase):
def RequestInformation(self, request, inInfo, outInfo):
print ("MySource RequestInformation:")
outInfo.GetInformationObject(0).Set(metaDataKey, vtk.vtkPolyData())
print (outInfo.GetInformationObject(0))
return 1
测试
import vtk
from vtk.util.vtkAlgorithm import VTKPythonAlgorithmBase
from vtk.util import keys
metaDataKey = keys.MakeKey(keys.DataObjectMetaDataKey, \
"a meta-data", "my module")
class MySource(VTKPythonAlgorithmBase):
def __init__(self):
VTKPythonAlgorithmBase.__init__(self,
nInputPorts=0,
nOutputPorts=1, outputType='vtkPolyData')
def RequestInformation(self, request, inInfo, outInfo):
print ("MySource RequestInformation:")
outInfo.GetInformationObject(0).Set(metaDataKey, vtk.vtkPolyData())
print (outInfo.GetInformationObject(0))
return 1
def RequestUpdateExtent(self, request, inInfo, outInfo):
print ("MySource RequestUpdateExtent:")
# print (outInfo.GetInformationObject(0))
return 1
def RequestData(self, request, inInfo, outInfo):
print ("MySource RequestData:")
# print (outInfo.GetInformationObject(0))
return 1
class MyFilter(VTKPythonAlgorithmBase):
def __init__(self):
VTKPythonAlgorithmBase.__init__(self,
nInputPorts=1, inputType='vtkPolyData',
nOutputPorts=1, outputType='vtkPolyData')
def RequestInformation(self, request, inInfo, outInfo):
print ("MyFilter RequestInformation:")
print (outInfo.GetInformationObject(0))
return 1
def RequestUpdateExtent(self, request, inInfo, outInfo):
print ("MyFilter RequestUpdateExtent:")
# print (outInfo.GetInformationObject(0))
return 1
def RequestData(self, request, inInfo, outInfo):
print ("MyFilter RequestData:")
# print (outInfo.GetInformationObject(0))
return 1
s = MySource()
f = MyFilter()
f.SetInputConnection(s.GetOutputPort())
f.Update()
输出
MySource RequestInformation:
vtkInformation (0000020914F670A0)
Debug: Off
Modified Time: 220
Reference Count: 3
Registered Events: (none)
PRODUCER: vtkCompositeDataPipeline(0000020914C7B4F0) port 0
CONSUMERS: vtkCompositeDataPipeline(0000020914C7D760) port 0
DATA_OBJECT: vtkPolyData(0000020914F6CEB0)
a meta-data: vtkPolyData(00000209124DD610)
MyFilter RequestInformation:
vtkInformation (0000020914F67EA0)
Debug: Off
Modified Time: 239
Reference Count: 2
Registered Events: (none)
PRODUCER: vtkCompositeDataPipeline(0000020914C7D760) port 0
DATA_OBJECT: vtkPolyData(0000020914F99180)
a meta-data: vtkPolyData(00000209124DD610)
MyFilter RequestUpdateExtent:
MySource RequestUpdateExtent:
MySource RequestData:
MyFilter RequestData:
在MyFilter
中的vtkInformation
当中可以看到自定义的元数据a meta-data: vtkPolyData(00000209124DD610)
在溯中对元数据进行修改
拷贝一份元数据,然后再添加一个字符数组。
class MyFilter(VTKPythonAlgorithmBase):
def RequestInformation(self, request, inInfo, outInfo):
print ("MyFilter RequestInformation:")
print (outInfo.GetInformationObject(0))
metaData = inInfo[0].GetInformationObject(0).Get(
metaDataKey)
newMetaData = metaData.NewInstance()
newMetaData.ShallowCopy(metaData)
someArray = vtk.vtkCharArray()
someArray.SetName("someArray")
newMetaData.GetFieldData().AddArray(someArray)
outInfo.GetInformationObject(0).Set(metaDataKey, newMetaData)
print (outInfo.GetInformationObject(0))
return 1
完整测试
import vtk
from vtk.util.vtkAlgorithm import VTKPythonAlgorithmBase
from vtk.util import keys
metaDataKey = keys.MakeKey(keys.DataObjectMetaDataKey, \
"a meta-data", "my module")
class MySource(VTKPythonAlgorithmBase):
def __init__(self):
VTKPythonAlgorithmBase.__init__(self,
nInputPorts=0,
nOutputPorts=1, outputType='vtkPolyData')
def RequestInformation(self, request, inInfo, outInfo):
print ("MySource RequestInformation:")
outInfo.GetInformationObject(0).Set(metaDataKey, vtk.vtkPolyData())
print (outInfo.GetInformationObject(0))
return 1
def RequestUpdateExtent(self, request, inInfo, outInfo):
print ("MySource RequestUpdateExtent:")
# print (outInfo.GetInformationObject(0))
return 1
def RequestData(self, request, inInfo, outInfo):
print ("MySource RequestData:")
# print (outInfo.GetInformationObject(0))
return 1
class MyFilter(VTKPythonAlgorithmBase):
def __init__(self):
VTKPythonAlgorithmBase.__init__(self,
nInputPorts=1, inputType='vtkPolyData',
nOutputPorts=1, outputType='vtkPolyData')
def RequestInformation(self, request, inInfo, outInfo):
print ("MyFilter RequestInformation:")
print (outInfo.GetInformationObject(0))
metaData = inInfo[0].GetInformationObject(0).Get(
metaDataKey)
newMetaData = metaData.NewInstance()
newMetaData.ShallowCopy(metaData)
someArray = vtk.vtkCharArray()
someArray.SetName("someArray")
newMetaData.GetFieldData().AddArray(someArray)
outInfo.GetInformationObject(0).Set(metaDataKey, newMetaData)
print (outInfo.GetInformationObject(0))
return 1
def RequestUpdateExtent(self, request, inInfo, outInfo):
print ("MyFilter RequestUpdateExtent:")
# print (outInfo.GetInformationObject(0))
return 1
def RequestData(self, request, inInfo, outInfo):
print ("MyFilter RequestData:")
# print (outInfo.GetInformationObject(0))
return 1
s = MySource()
f = MyFilter()
f.SetInputConnection(s.GetOutputPort())
f.Update()
输出:
(base) C:\Users\wangb\Documents\JupyterNoteBook>C:/Anaconda3/python.exe c:/Users/wangb/Documents/JupyterNoteBook/pipeline2.py
MySource RequestInformation:
vtkInformation (0000010668C600E0)
Debug: Off
Modified Time: 220
Reference Count: 3
Registered Events: (none)
PRODUCER: vtkCompositeDataPipeline(000001066895BF10) port 0
CONSUMERS: vtkCompositeDataPipeline(0000010668959320) port 0
DATA_OBJECT: vtkPolyData(0000010668C5ED40)
a meta-data: vtkPolyData(000001066818E9B0)
MyFilter RequestInformation:
vtkInformation (0000010668C5F560)
Debug: Off
Modified Time: 239
Reference Count: 2
Registered Events: (none)
PRODUCER: vtkCompositeDataPipeline(0000010668959320) port 0
DATA_OBJECT: vtkPolyData(00000106660FD9D0)
a meta-data: vtkPolyData(000001066818E9B0)
vtkInformation (0000010668C5F560)
Debug: Off
Modified Time: 264
Reference Count: 2
Registered Events: (none)
PRODUCER: vtkCompositeDataPipeline(0000010668959320) port 0
DATA_OBJECT: vtkPolyData(00000106660FD9D0)
a meta-data: vtkPolyData(0000010668C69800)
MyFilter RequestUpdateExtent:
MySource RequestUpdateExtent:
MySource RequestData:
MyFilter RequestData:
在MyFilter中会有两份元数据 a meta-data: vtkPolyData(000001066818E9B0)
和a meta-data: vtkPolyData(0000010668C69800)
示意图:
一个实际的例子
HDFSource
的RequestInformation
:
def RequestInformation(self, request, inInfo, outInfo):
f = h5py.File(self.__FileName, 'r')
dims = f['RTData'].shape[::-1]
info = outInfo.GetInformationObject(0)
# 元数据
info.Set(vtk.vtkStreamingDemandDrivenPipeline.WHOLE_EXTENT(),
(0, dims[0]-1, 0, dims[1]-1, 0, dims[2]-1), 6)
return 1
RequestSubset
的 RequestInformation
:
def RequestInformation(self, request, inInfo, outInfo):
info = outInfo.GetInformationObject(0)
# 元数据的修改
info.Set(vtk.vtkStreamingDemandDrivenPipeline.WHOLE_EXTENT(), \
self.__UpdateExtent, 6)
return 1
- HDF5Source 在 RequestInformation中创建meta-data,
- meta-data 被拷贝到 RequestSubset 的输出中,
- RequestSubset 在 RequestInformation中重写 meta-data 。