In the end, I found the answer to my question. Just in case, if someone visits this topic, ans have the same problems, I will post my solution. I used vtkProbeFilter to interpolate my VTK data. After interpolation, I converted the VTK string to a numpy array for easy plotting.
import numpy as np
from vtk.util import numpy_support as VN
from matplotlib import pyplot as plt
import vtk
def readVTK(filename):
reader = vtk.vtkUnstructuredGridReader()
reader.SetFileName(filename)
reader.ReadAllVectorsOn()
reader.ReadAllScalarsOn()
reader.Update()
return reader
def createLine(p1,p2,numPoints):
line = vtk.vtkLineSource()
line.SetResolution(numPoints)
line.SetPoint1(p1)
line.SetPoint2(p2)
line.Update()
return line
def probeOverLine(line,reader):
data = reader.GetOutput()
probe = vtk.vtkProbeFilter()
probe.SetInputConnection(line.GetOutputPort())
probe.SetSource(data)
probe.Update()
q=VN.vtk_to_numpy(probe.GetOutput().GetPointData().GetArray('U'))
numPoints = probe.GetOutput().GetNumberOfPoints()
x = np.zeros(numPoints)
y = np.zeros(numPoints)
z = np.zeros(numPoints)
points = np.zeros((numPoints , 3))
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):
array[array==0]=np.nan
return array
filename='a-VTK-file.vtk'
p1=[0.0,-0.1,0.0]
p2=[0.0,-0.1,1.0]
numPoints=100
reader = readVTK(filename)
line=createLine(p1,p2,numPoints)
points,U = probeOverLine(line,reader)
U = setZeroToNaN(U)
plt.plot(points[:,2],U[:,0])
plt.show()
source
share