Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read a VTK file into a Python datastructure?

Tags:

python

vtk

I have some VTK-files, which 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 out of these VTK-files: edgesList and verticesList:

  • edgesList should contain the edges as (FromVerticeIndex, ToVerticeIndex, Weight)-tuples
  • verticesList should contain the vertices as (x,y,z)-tuples. The index is the index mentioned in edgesList

I have no idea how to extract this with the standard-vtk-python library. I got so far:

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()

Its possible that my python-vtk-code doesn't make sense. I would prefer to use the vtk library and not use any self-written pieces of code.

Here is my self-written piece of code. 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)
like image 425
Martin Thoma Avatar asked Jul 13 '11 19:07

Martin Thoma


People also ask

How do I open a VTK file?

If you cannot open your VTK file correctly, try to right-click or long-press the file. Then click "Open with" and choose an application. You can also display a VTK file directly in the browser: Just drag the file onto this browser window and drop it.

How does VTK work?

VTK uses a data flow approach to transform informational data into graphical data. There are two basic types of objects involved: vtkDataObject. represents data of various types: Image data, Rectilinear Grid, Structured Grid, Unstructured Points, Polygonal Data, Unstructured Grid.

What is VTK format?

The simple legacy VTK format consists of five sections. Section 1: Contains the file version and identifier. Section 2: Contains the title (or header) that describes the data. This is 256 characters terminated by a newline character. Section 3: Contains the file format (that is, ASCII or BINARY).


1 Answers

STLreader is suitable to read STL files. If you have a .vtk file and wish to read to grid information (nodes, elements and their coordinates) you have to use another reader (either 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:

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]
like image 51
SAAD Avatar answered Oct 14 '22 20:10

SAAD