3

I have the following VTK file, which is generate by OpenFOAM:

# 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

It is a flat cutting plane of a 3D domain. There is 4 points, which create two triangles. On each points, the vector U is defined. I can get the number of points and the points with the following code:

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)

Unfortunately, I haven't found a possibility to get the list of triangles (the polygons) and the list of data (the vectors U). Can somebody help me on this matter?

Marcel

Marcel
  • 464
  • 1
  • 6
  • 12

1 Answers1

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

Gretchen
  • 2,274
  • 17
  • 16