python自定义vtk算法 - vtkPythonAlgorithm is great

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())

Reference

vtkPythonAlgorithm is great

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值