我想把三维标量数据写成*.vtu文件(使用python),以便以后在Paraview中获取并查看体积。我可以写出*.vtu文件,但是Paraview在加载它时崩溃了。我是否错误地使用了vtk的API?
我的方法。 (Python 2.7.12, VTK_MAJOR_VERSION 6, Paraview 5.0.1)
我遵循了我能找到的唯一使用PolyVertex对象的例子 here .
并得出了以下类。
import vtk
import numpy as np
class VtkPolyVertCloud(object):
def __init__(self):
self.points= vtk.vtkPoints()
self.grid = vtk.vtkUnstructuredGrid()
self.values = vtk.vtkDoubleArray()
self.values.SetName('point_values_array')
self.grid.SetPoints(self.points)
self.grid.GetPointData().SetScalars(self.values)
def add_polyVertex_cell(self, points, data):
adds points according to user-supplied numpy arrays
@param points: numpy array of 3d point coords -- points.shape = (npoints, 3)
@param data: scalar-valued data belonging to each point -- data.shape = (npoints,)
npts = points.shape[0]
pv = vtk.vtkPolyVertex()
pv.GetPointIds().SetNumberOfIds(npts)
for idx, point in enumerate(points):
pointID = self.points.InsertNextPoint(point)
pv.GetPointIds().SetId(idx, pointID)
self.values.InsertNextValue(data[idx])
self.grid.InsertNextCell(pv.GetCellType(), pv.GetPointIds())
我实例化了这个类,并试图将一个简单的随机PolyVertex单元写入XML文件。
def test_vtkPolyVertexCloud_writeToFile():
""" adds a set of polyvertices meant to represent a finite element """
pc = vtku.VtkPolyVertCloud()
points, data = np.random.rand(10, 3), np.random.rand(10)
pc.add_polyVertex_cell(points, data)
# write
fn = 'test_PolyVertexCloud.vtu'
writer = vtk.vtkUnstructuredGridWriter()
writer.SetFileName(fn)
writer.SetInputData(pc.grid)
writer.Write()
在Paraview中打开文件时,Paraview Gui没有反应,然后崩溃了。