我想使用 python 可视化一个 VTK 数据文件(OpenFOAM 输出)。我想要制作的图是两个端点之间数量的一维线图。为此,应在位于两个端点之间的点上对非结构化数据进行插值。
我使用 Mayavi 包来可视化 VTK 数据。在mayavi 网页上有关于从标量域探测单个值的描述。此功能不适用于 VTK 文件。
我还在mayavi 网页上找到了一个 delaunay3d 方法(mlab.pipeline.delaunay3d) 。我也没有让这个工作。
谁能告诉我如何插入我的数据?
我想使用 python 可视化一个 VTK 数据文件(OpenFOAM 输出)。我想要制作的图是两个端点之间数量的一维线图。为此,应在位于两个端点之间的点上对非结构化数据进行插值。
我使用 Mayavi 包来可视化 VTK 数据。在mayavi 网页上有关于从标量域探测单个值的描述。此功能不适用于 VTK 文件。
我还在mayavi 网页上找到了一个 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()