预备知识
在熟练应用Programmable filter之前,用户需要了解 VTK 和ParaView的数据结构和处理机制。
简单来讲vtk就是mesh里存在field或array,一般点击Information就可以查看到:
在view里面点开Statistics Inspector还可以查看更多信息。
应用
下面片段取自一个坐标转换的filter,其中GetNumberOfPoints返回坐标点的数目,也可以用GetNumberOfCells()返回cell数目。
pdi读取了网格,通过pdi.GetPoint(i),则可以访问第i点的坐标。
from paraview import vtk
import math
pdi = self.GetUnstructuredGridInput()
# pdi = self.GetInput()
pdo = self.GetOutput()
newPoints = vtk.vtkPoints()
numPoints = pdi.GetNumberOfPoints()
for i in range(0, numPoints):
coord = pdi.GetPoint(i)
x, y, z = coord[:3]
newPoints.InsertPoint(i, x, y, z)
pdo.SetPoints(newPoints)
如果想访问数据,则可以利用
u = pdi.GetPointData().GetArray(“v”).GetValue(i)
得到数据。
其他参考案例
在Paraview文档里也有其他例子:
和将只存了6个数的tensor换成9个数的
源自: https://public.kitware.com/pipermail/vtkusers/2014-June/084386.html
from paraview import numpy_support as NS
import numpy as N
pdi = self.GetInputDataObject(0,0)
pdo = self.GetOutputDataObject(0)
pdo.ShallowCopy(pdi)
import math
m = pdi.GetPointData().GetArray('tensor6_array')
m2 = N.mat([[m.GetValue(0), m.GetValue(1), m.GetValue(2)], [m.GetValue(1),
m.GetValue(3), m.GetValue(4)], [m.GetValue(2), m.GetValue(4),
m.GetValue(5)]])
m3 = NS.numpy_to_vtk(m2)
m3.SetName('tensor9_array')
pdo.GetPointData().AddArray(m3)