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

最终,我找到了自己问题的答案。只是万一有人访问这个主题和有同样的问题,我会张贴我的解决方案。我用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()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值