VTK/Writing VTK files using python: Difference between revisions

From KitwarePublic
< VTK
Jump to navigationJump to search
Line 17: Line 17:
And believe me, this is far easier to write them using TVTK than "pure" VTK ;-)
And believe me, this is far easier to write them using TVTK than "pure" VTK ;-)


But if one you wikiers, think that this link is irrelevant (not "pure" VTK), no problem,
But if one of you, wikiers, think that this link is irrelevant (not "pure" VTK), no problem,
you can remove it, I won't blame you ;-)
you can remove it, I won't blame you ;-)



Revision as of 20:51, 26 April 2009

Before starting, you are strongly advised to read the VTK file formats documentation here.

Introduction

The purpose of this wiki is to show how one can write VTK files using python. Why using python ? Because it's beautiful & powerful, of course ! ;-) Obviously, you can use your favourite programming language to write such files. Mine is python. No more, no less. In the following, the data you want to write to a VTK file are supposed to arise from scipy/numpy arrays. Please browse scipy.org to get more information if needed.

Before writing VTK files, you have to know that there are two kinds of VTK files: "legacy" VTK files and "XML" VTK files. The first kind is quite obsolete now, but still widely used. Although it is easier to write than the XML kind (understand: with low-level instructions like "printf" in C or "print" in python), this wiki will focus on the last kind, the XML format.

Datasets format

Note: Well, I have initiated this wiki and never found time to write some materials to it :-(

Meanwhile, Gaël wrote a really cool doc on VTK Data Structures, in the Mayavi Users Guide.

These pages show how one can easily write snipped code to create theses datasets, using TVTK. And believe me, this is far easier to write them using TVTK than "pure" VTK ;-)

But if one of you, wikiers, think that this link is irrelevant (not "pure" VTK), no problem, you can remove it, I won't blame you ;-)

Cheers,



As you should already be aware if you have read the VTK file formats documentation, VTK can handle several kinds of datasets, for points data and cells data. The primary are:

  • StructuredPoints (aka ImageData, see later);
  • RectilinearGrid;
  • StructuredGrid;
  • UnstructuredGrid;
  • PolyData.

Briefly:

  • StructuredPoints is the "simplest" format: you only have to set the mesh dimensions (nx, ny, nz), mesh origin (x0, y0, z0) and cell dimensions (dx, dy, dz). Thus, points data are regularly and uniformly spaced.

StructuredPoints dataset

  • RectilinearGrid is also a regularly spaced points data but spacing can be not uniform. Thus, you have to specify the nodes coordinates along the three axes, Ox, Oy & Oz.

RectilinearGrid dataset

  • StructuredGrid is a not regular and not uniform points data. You have to specify the coordinates for all mesh nodes.

StructuredGrid dataset

  • UnstructuredGrid is a not regular and not uniform points data, like the StructuredGrid, but can handle all cell types (see VTK file formats documentation for cell types available).

File:Unstructured grid.png

  • PolyData can be used for any polygonal data. Thus, you have to specify the coordinates for the points and the polygons defined by the points index.

PolyData dataset

"legacy"

First, the "legacy" VTK format has only one file extension for all kind of datasets format: ".vtk", which is not the case for the "XML" as you will see later. Writing "legacy" VTK files is well documented in the VTK file formats documentation and is quite straightforward. You have just to set a few parameters, such as:

  • file format (ASCII or binary);
  • data format (char, int, float, double, etc...)
  • specific information related to the kind of the dataset you want to use.

Note: The following issue is frequently asked on the vtkusers mailing-list, so please read it carefully before complaining about errors while reading your VTK file after you wrote it. If you want to write your data in binary format in your "legacy" VTK file (XML files are not concerned), you have to write it in BigEndian, and not in LittleEndian, even if you use an intel or amd box which are known to be in LittleEndian. So, you have to convert your data in BigEndian before writing them in your VTK file. This is explicitly mentionned in the VTK file formats documentation.

Using python, you can write you "legacy" VTK files with your own methods or use pyvtk.

"XML Format"

The new XML format is somewhat more complex, but ultimately more flexible. Different types of data have different file extensions. Recently, a simple open-source Python class for writing unstructured data in the VTK XML format has been released. Although this class is probably too specialized for general-purpose use, it is an easily understood example that you can use to create your own classes. It utilized the built-in Python tool xml.dom.minidom to simplify the creation of XML. It also generates a .pvd file, which allows a series of VTK files to be read into Paraview simultaneously.