使用python TVTK或MayaVi探测/采样/插值VTK数据

使用python TVTK或MayaVi探测/采样/插值VTK数据,python,interpolation,enthought,mayavi,probe,Python,Interpolation,Enthought,Mayavi,Probe,我想使用python可视化VTK数据文件(OpenFOAM输出)。我想做的图是两个端点之间的一维线图。为此,应在两个端点之间的点上插值非结构化数据 我使用了Mayavi软件包来可视化VTK数据。最后,有一个从scalarfield探测单个值的描述。此函数不适用于VTK文件 我还发现了一个delaunay3d方法(mlab.pipeline.delaunay3d)。我也没能让它工作 谁能告诉我如何插值我的数据吗?最终,我找到了我自己问题的答案。万一有人访问这个主题时遇到同样的问题,我会发布我的解决

我想使用python可视化VTK数据文件(OpenFOAM输出)。我想做的图是两个端点之间的一维线图。为此,应在两个端点之间的点上插值非结构化数据

我使用了Mayavi软件包来可视化VTK数据。最后,有一个从scalarfield探测单个值的描述。此函数不适用于VTK文件

我还发现了一个delaunay3d方法(mlab.pipeline.delaunay3d)。我也没能让它工作


谁能告诉我如何插值我的数据吗?

最终,我找到了我自己问题的答案。万一有人访问这个主题时遇到同样的问题,我会发布我的解决方案。我已经使用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()

你的问题非常不清楚。你能更好地解释你想做什么吗?没有把问题说清楚的借口。我有一个VTK数据文件(OpenFOAM输出),我想对两点之间的量做一个一维线图。短语“对两点之间的量做一个一维线图意味着”几乎是对你在问题中所说内容的字面重述,我仍然不知道它是什么意思。你的vtk文件中的数据代表什么?你想测量什么?我vtk文件中的数据代表流体域中的压力。我想在三维空间中,沿着一条线来可视化压力的变化。然后我会得到一个图,在x轴上是沿着直线的距离,在y轴上是直线上该点的压力。在中,可以看到带有线条的三维体积。以及直线上的插值。在本例中,我使用的是paraView,它不允许编写脚本。此外,它不能很好地捕捉不连续性。非常感谢。你帮我节省了很多时间!