How can I read a VTK file in a Python data structure?

I have some VTK files that look like this:

# vtk DataFile Version 1.0 Line representation of vtk ASCII DATASET POLYDATA POINTS 30 FLOAT 234 462 35 233 463 35 231 464 35 232 464 35 229 465 35 [...] LINES 120 360 2 0 1 2 0 1 2 1 0 2 1 3 2 1 0 2 1 3 2 2 5 2 2 3 [...] 

I would like to get two lists from these VTK files: edgeList and verticesList:

  • edgeList must contain edges in the form (FromVerticeIndex, ToVerticeIndex, Weight) -tuples
  • verticesList must contain vertices as (x, y, z) -lines. Index is the index specified in edgeList

I do not know how to extract this using standard-vtk-python library. I still:

 import sys, vtk filename = "/home/graphs/g000231.vtk" reader = vtk.vtkSTLReader() reader.SetFileName(filename) reader.Update() idList = vtk.vtkIdList() polyDataOutput = reader.GetOutput() print polyDataOutput.GetPoints().GetData() 

Maybe my python-vtk code doesn't make sense. I would prefer to use the vtk library and not use self-written code fragments.

Here is my self-written code snippet. It works, but it would be better if I could use the vtk library for this:

 import re def readVTKtoGraph(filename): """ Specification of VTK-files: http://www.vtk.org/VTK/img/file-formats.pdf - page 4 """ f = open(filename) lines = f.readlines() f.close() verticeList = [] edgeList = [] lineNr = 0 pattern = re.compile('([\d]+) ([\d]+) ([\d]+)') while "POINTS" not in lines[lineNr]: lineNr += 1 while "LINES" not in lines[lineNr]: lineNr += 1 m = pattern.match(lines[lineNr]) if m != None: x = float(m.group(1)) y = float(m.group(2)) z = float(m.group(3)) verticeList.append((x,y,z)) while lineNr < len(lines)-1: lineNr += 1 m = pattern.match(lines[lineNr]) nrOfPoints = m.group(1) vertice1 = int(m.group(2)) vertice2 = int(m.group(3)) gewicht = 1.0 edgeList.append((vertice1, vertice2, gewicht)) return (verticeList, edgeList) 
+7
source share
2 answers

I do not use VTK with Python, but this reader should be able to read this file: http://www.vtk.org/Wiki/VTK/Examples/Cxx/IO/GenericDataObjectReader

and here is an example of using a VTK reader in Python: http://www.vtk.org/Wiki/VTK/Examples/Python/STLReader

+1
source

STLreader is suitable for reading STL files. If you have a .vtk file and want to read information about the grid (nodes, elements and their coordinates), you should use a different reader ( vtkXMLReader or vtkDataReader , both contain structured and unstructured grid support). Then use the vtk_to_numpy function from the VTK package.

A sample code would look like this:

 from vtk import * from vtk.util.numpy_support import vtk_to_numpy # load a vtk file as input reader = vtk.vtkXMLUnstructuredGridReader() reader.SetFileName("my_input_data.vtk") reader.Update() #Grab a scalar from the vtk file my_vtk_array = reader.GetOutput().GetPointData().GetArray("my_scalar_name") #Get the coordinates of the nodes and the scalar values nodes_nummpy_array = vtk_to_numpy(nodes_vtk_array) my_numpy_array = vtk_to_numpy(my_vtk_array ) x,y,z= nodes_nummpy_array[:,0] , nodes_nummpy_array[:,1] , nodes_nummpy_array[:,2] 
+4
source

All Articles