3

我有以下由OpenFOAM生成的 VTK 文件:

# vtk DataFile Version 2.0
sampleSurface
ASCII
DATASET POLYDATA
POINTS 4 float
0.0 0.0 0.0
1.0 0.0 0.0
0.0 1.0 0.0
1.0 1.0 0.0

POLYGONS 2 8
3 0 1 2
3 2 1 3

POINT_DATA 4
FIELD attributes 1
U 3 4 float
1.0 2.0 3.0
1.0 2.0 3.0
1.0 2.0 3.0
1.0 2.0 3.0

它是一个 3D 域的平面切割平面。有 4 个点,形成两个三角形。在每个点上,定义了向量 U。我可以使用以下代码获取点数和点数:

import vtk
reader = vtk.vtkPolyDataReader()
reader.SetFileName('myVTKfile.vtk')
reader.ReadAllScalarsOn()
reader.ReadAllVectorsOn()
reader.ReadAllTensorsOn()
reader.Update()
vtkdata = reader.GetOutput()

print vtkdata.GetNumberOfPoints()
print vtkdata.GetPoint(0)

不幸的是,我还没有找到获取三角形列表(多边形)和数据列表(向量 U)的可能性。有人可以帮我解决这个问题吗?

马塞尔

4

1 回答 1

3

For pointwise data (such as scalars and vectors), you can access it via:

pointData = vtkdata.GetPointData()
vectorData = pointData.GetVectors()

vectorData will then contain a vtkDataArray that you can work with.

For geometry data, you use the GetVerts, GetLines, GetPolys (triangles, quads, and other polys), and possibly GetStrips (for triangle strips) methods. For triangles that haven't been glommed together into triangle strips, you can access the data with:

polyCells = vtkdata.GetPolys()
numPolys = polyCells.GetNumberOfCells() #Number of polygons (triangles in your case)

Accessing the cell data (which is just a list of point indices) from within python is a little bit of a pain and has evidently changed since I last wrote anything with VTK. (back with VTK 5.x). At the very least, you can get the cell array and scan it as described here: http://vtk.1045678.n5.nabble.com/vtkCellArray-in-python-td3348424.html

于 2014-10-22T17:18:32.443 回答