使用Python TVTK或MayaVi探究/采样/插值VTK数据

7
我希望使用Python来可视化一个VTK数据文件(OpenFOAM输出)。我想要制作的图形是两个端点之间量的一维线性图。为了做到这一点,非结构化数据应该插值到位于两个端点之间的点上。
我已经使用Mayavi包来可视化VTK数据。在Mayavi网页上,有一个从标量场探测单个值的描述。但是此函数不适用于VTK文件。
另外,我在Mayavi网页上找到了一个Delaunay3d方法(mlab.pipeline.delaunay3d),但我也无法使其工作。
请问有谁能指导我如何插值我的数据呢?

你的问题非常不清楚。你能更好地解释一下你想做什么吗? - aestrivex
不清楚表达问题的借口。我有一个VTK数据文件(OpenFOAM输出),我想在两个点之间制作一个数量的1D线图。 - Newfarmer
“制作两点之间数量的一维线性图”这句话几乎是你在问题中所说的话的逐字重述,但我仍然不知道它的含义。你的vtk文件中的数据代表什么?你试图测量什么? - aestrivex
VTK文件中的数据表示流体域中的压力。我想在3D空间中沿着一条线显示压力变化。然后,我将得到一个图表,其中x轴表示沿该线的距离,y轴表示该线上该点的压力。在这张图片中,您可以看到带有线条的3D体积。在这里是一条插值线。对于这个例子,我使用的是ParaView,它不允许脚本编写。此外,它不能很好地捕捉不连续性。 - Newfarmer
1个回答

16

最终,我找到了自己问题的答案。以防有人访问此主题并遇到相同的问题,我将发布我的解决方案。我使用vtkProbeFilter来插值我的VTK数据。在插值后,我将VTK线转换为numpy数组以便于绘图。

#!/usr/bin/env python
import numpy as np
from vtk.util import numpy_support as VN
from matplotlib import pyplot as plt
import vtk

def readVTK(filename):
    #read the vtk file with an unstructured grid
    reader = vtk.vtkUnstructuredGridReader()
    reader.SetFileName(filename)
    reader.ReadAllVectorsOn()
    reader.ReadAllScalarsOn()
    reader.Update()
    return reader

def createLine(p1,p2,numPoints):
    # Create the line along which you want to sample
    line = vtk.vtkLineSource()
    line.SetResolution(numPoints)
    line.SetPoint1(p1)
    line.SetPoint2(p2)
    line.Update()
    return line

def probeOverLine(line,reader):
    #Interpolate the data from the VTK-file on the created line.
    data = reader.GetOutput()
    # vtkProbeFilter, the probe line is the input, and the underlying dataset is the source.
    probe = vtk.vtkProbeFilter()
    probe.SetInputConnection(line.GetOutputPort())
    probe.SetSource(data)
    probe.Update()
    #get the data from the VTK-object (probe) to an numpy array
    q=VN.vtk_to_numpy(probe.GetOutput().GetPointData().GetArray('U'))
    numPoints = probe.GetOutput().GetNumberOfPoints() # get the number of points on the line
    #intialise the points on the line    
    x = np.zeros(numPoints)
    y = np.zeros(numPoints)
    z = np.zeros(numPoints)
    points = np.zeros((numPoints , 3))
    #get the coordinates of the points on the line
    for i in range(numPoints):
        x[i],y[i],z[i] = probe.GetOutput().GetPoint(i)
        points[i,0]=x[i]
        points[i,1]=y[i]
        points[i,2]=z[i]
    return points,q

def setZeroToNaN(array):
    # In case zero-values in the data, these are set to NaN.
    array[array==0]=np.nan
    return array

#Define the filename of VTK file
filename='a-VTK-file.vtk'

#Set the points between which the line is constructed.
p1=[0.0,-0.1,0.0]
p2=[0.0,-0.1,1.0]

#Define the numer of interpolation points
numPoints=100

reader = readVTK(filename) # read the VTKfile
line=createLine(p1,p2,numPoints) # Create the line
points,U =  probeOverLine(line,reader) # interpolate the data over the line

U = setZeroToNaN(U) # Set the zero's to NaN's
plt.plot(points[:,2],U[:,0]) #plot the data
plt.show()

非常感谢。你帮我节省了很多时间! - fern17

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接