简体   繁体   English

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

[英]Probing/sampling/interpolating VTK data using python TVTK or MayaVi

I would like to visualise a VTK data file (OpenFOAM output) using python. 我想使用python可视化VTK数据文件(OpenFOAM输出)。 The plot I would like to make is a 1-d line plot of a quantity between two endpoints. 我想要绘制的图是两个端点之间的数量的一维线图。 To do so, the unstructured data should be interpolated on the points which lie between the two endpoints. 为此,应在两个端点之间的点上插入非结构化数据。

I've used the package Mayavi to visualise the VTK data. 我已经使用Mayavi包来可视化VTK数据。 At the mayavi webpage there is a description of probing a single value from a scalarfield. mayavi网页上有一个探测scalarfield中单个值的描述。 This function does not work on a VTK file. 此功能不适用于VTK文件。

Also I've found a delaunay3d method (mlab.pipeline.delaunay3d) at the mayavi webpage . 我也在mayavi网页上找到了delaunay3d方法(mlab.pipeline.delaunay3d)。 I did not get this one to work either. 我也没有得到这个。

Could anyone advise me how to interpolate my data? 谁能告诉我如何插入我的数据?

Eventually, I found the answer to my own question. 最终,我找到了自己问题的答案。 Just in case anyone visits this topic ans has the same problems, I will post my solution. 万一有人访问这个主题ans有同样的问题,我会发布我的解决方案。 I've used the vtkProbeFilter to interpolate my VTK-data. 我已经使用vtkProbeFilter来插入我的VTK数据。 After the interpolation I've transformed the VTK-line to a numpy array for plotting convenience. 在插值之后,我将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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM