XYZ Data with VTK

From Core Physics Wiki
Jump to: navigation, search

Visualizing XYZ data vith Legacy VTK files

VTK is a visualization toolkit produced by Kitware VTK.

This page does not describe using the VTK toolkit itself, but instead discusses how you can create Legacy VTK data files to view your data in one of the open visualization tools like Visit or Paraview.

The Legacy VTK data file is ASCII and it is very easy to create the file directly from the program that created the data (usually Fortran or Python in my case).

There are other VTK data file formats available, including a binary file format and an XML file format. These other file formats have their advantage, but the ASCII file is the easiest to create.

There are two steps to creating a VTK file. The first step is to generate the "grid" which shows where the data belongs, and the second step is to write the data itself. This page only describes how to create a VTK file using "point" data with (x,y,z) coordinates. VTK can handle other types of grids, but this page will only describe point data.

An example of creating figures with hexahedron mesh is shown at Create grid with VTK.


For this example, I will start with a text file that has a list of (x,y,z) coordinates that define the outline of a teapot. The text data file was downloaded from the VTK website at Teapot Data. The data file includes 2082 coordinates.


The first step in creating the VTK file is to write the file header and data point description to a text file:

# vtk DataFile Version 2.0
Example Teapot data file
   3.50000E+00   2.19615E+00   1.80000E+01
   3.45947E+00   2.28939E+00   1.80538E+01
   3.45117E+00   2.35599E+00   1.80923E+01
   3.46924E+00   2.39596E+00   1.81154E+01
   3.50781E+00   2.40928E+00   1.81230E+01
   3.56103E+00   2.39596E+00   1.81154E+01
   3.62305E+00   2.35600E+00   1.80923E+01
   3.68799E+00   2.28939E+00   1.80538E+01
   3.75000E+00   2.19615E+00   1.80000E+01
   3.43000E+00   1.83959E+00   1.86176E+01

  (continue with remainder of 2082 data coordinates)
  • The first line defines this file as a VTK data file.
  • The second line is an arbitrary title
  • The third line defines this file as ASCII format
  • The fourth line defines the data as POLYDATA. There are many different data types available, but this format is used to define point data.
  • The fifth line defines how many data points there are and that they are type FLOAT.
  • The remaining lines are the (x,y,z) coordinates for each of the 2082 data points.


The next step is to define the data at each grid point. In the visualization, the data will correspond to the color of each data point.

If you just want to see the grid, you can simply add values of "1.0" for each data point. In this example, I will assign the data as the z-coordinate of the point.

SCALARS Z-dimension float

 (continue with remainder of 2082 data values in the first dataset)
  • the first line of this block says that 2082 data points will follow. These data points correspond to the 2082 coordinates listed above.
  • the second line defines these as SCALAR values, gives a user-defined title of "Z-dimension", and then defines the type as "float"
  • the third line shows that the data is a LOOKUP_TABLE (i.e. one-to-one correspondence between data points and coordinates)
  • the fourth and remaining lines are the data itself

There can be multiple data sets in the same file. In this example, we wil will include another data set that shows the X-dimensions of the data points.

Additional datsets are added by including more data blocks, but without the "POINTS_DATA" line. This "POINTS_DATA" line should only be included once per file.

SCALARS X-dimension float

  (continue with remainder of 2082 data values in the second dataset)

This is it! You can include as many sets of data that you need.

Complete output

The complete VTK output file is found at teapot.vtk

Visualizing data with Visit

Once the VTK file has been created, it can be visualized in several different visualization software packages including Visit, Paraview, or many other packages.

To view the data with Visit, look at the Visit quick start

The final teapot image:

Teapot Image

Here is another example of a Torus:


Other Examples

Some other VTK example files are at John Burkardt homepage (scroll to bottom of page for examples)