vtk管线入门2

一个简单的管线:

在这里插入图片描述

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)

示意图:
在这里插入图片描述

一个实际的例子

在这里插入图片描述

HDFSourceRequestInformation:

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

RequestSubsetRequestInformation :

def RequestInformation(self, request, inInfo, outInfo):
    info = outInfo.GetInformationObject(0)
    # 元数据的修改 
    info.Set(vtk.vtkStreamingDemandDrivenPipeline.WHOLE_EXTENT(), \
        self.__UpdateExtent, 6)
    return 1
  1. HDF5Source 在 RequestInformation中创建meta-data,
  2. meta-data 被拷贝到 RequestSubset 的输出中,
  3. RequestSubset 在 RequestInformation中重写 meta-data 。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值