VTK Data Formats and Reading in Data
Data Formats
VTK supports a wide variety of file formats, including its own file formats that are used in computational analysis.
Simple Legacy Formats
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).
- Section 4: Contains structure or type of dataset. Supported dataset formats are described below.
- Section 5: Contains dataset attributes.
# vtk DataFile Version 2.0 (Section 1) ASCII string describing the data goes here. (Section 2) ASCII | BINARY (Section 3) DATASET type (Section 4) ... POINT_DATA n (Section 5) ... CELL_DATA n ...
Dataset Grid Format
VTK supports five different dataset formats:
- STRUCTURED_POINTS
- STRUCTURED_GRID
- RECTILINEAR_GRID
- POLYDATA
- UNSTRUCTURED_GRID
Structured Points
The legacy VTK format support 1-D, 2-D, and 3-D structured points datasets. The dimensions Nx, Ny, and Nz must be a value greater than zero. Additionally, the data spacing values Sx, Sy, and Sz must be a value greater than zero.
DATASET STRUCTURED_POINTS DIMENSIONS nx ny nz ORIGIN x y z SPACING sx sy sz
Structured Grid
The legacy VTK format support 1-D, 2-D, and 3-D structured grid datasets. The dimensions Nx, Ny, and Nz must be a value greater than zero. The POINTS section is used to define the x-, y-, z-values for each point.
DATASET STRUCTURED_GRID DIMENSIONS nx ny nz POINTS n dataType p0x p0y p0z ...
Rectilinear Grid
A Rectilinear Grid is a data set that has a regular topology in I, J, K space, but may have non-uniform spacing in X, Y, Z space.
DATASET RECTILINEAR_GRID DIMENSIONS nx ny nz X_COORDINATES nx dataType x0 x1 ... x(nx-1) Y_COORDINATES ny dataType y0 y1 ... y(ny-1) Z_COORDINATES nz dataType z0 z1 ... z(nz-1)
Polygonal Data
DATASET POLYDATA POINTS n dataType p0x p0y p0z... VERTICES n size numPoints0, i0, j0, k0, ... ... LINES n size numPoints0, i0, j0, k0, ...... POLYGONS n size numPoints0, i0, j0, k0, ... ... TRIANGLE_STRIPS n size numPoints0, i0, j0, k0, ... ...
Unstructured Grid
DATASET UNSTRUCTURED_GRID POINTS n dataType p0x p0y p0z ... CELLS n size numPoints0, i, j, k, l, ... ... CELL_TYPES n type0 ...
Dataset Variables Formats
The scalar data format starts with the SCALARS keyword, followed by the user-defined scalar name (e.g., temperature), the type (e.g., float), and the number of components (e.g., 1). This is followed by the LOOKUP_TABLE keyword and the user defined lookup table name (e.g., default).
SCALARS scalar_name type NumberOfComponents LOOKUP_TABLE table_name
The vector data format starts with the VECTORS keyword, followed by the user defined vector name (e.g., velocity, vorticity, etc.,), and the type (e.g., float).
VECTORS vector_name type
Examples
The following is a simple example of a structured points file. The data set contains one scalar data field, named scalar_name. The data set contains one vector data field as well that is named, appropriately enough, vector_name.
# vtk DataFile Version 2.0 Cube example ASCII DATASET STRUCTURED_POINTS DIMENSIONS 3 3 3 ORIGIN 0 0 0 SPACING 1 1 1 POINT_DATA 27 SCALARS scalar_name float 1 LOOKUP_TABLE default 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0 19.0 20.0 21.0 22.0 23.0 24.0 25.0 26.0 27.0 VECTORS vector_name float 1.0 27.0 1.0 2.0 26.0 2.0 3.0 25.0 3.0 4.0 24.0 4.0 5.0 23.0 5.0 6.0 22.0 6.0 7.0 21.0 7.0 8.0 20.0 8.0 9.0 19.0 9.0 10.0 18.0 10.0 11.0 17.0 11.0 12.0 16.0 12.0 13.0 15.0 13.0 14.0 14.0 14.0 15.0 13.0 15.0 16.0 12.0 16.0 17.0 11.0 17.0 18.0 10.0 18.0 19.0 9.0 19.0 20.0 8.0 20.0 21.0 7.0 21.0 22.0 6.0 22.0 23.0 5.0 23.0 24.0 4.0 24.0 25.0 3.0 25.0 26.0 2.0 26.0 27.0 1.0 27.0
The cube example can not only be written as structured points, but also as a rectilinear grid file. It uses the same scalar and vector data as shown above. This time, the spacing is not uniform in the X-, Y-, or Z-dimensions.
# vtk DataFile Version 2.0 Rectangular solid example ASCII DATASET RECTILINEAR_GRID DIMENSIONS 3 3 3 X_COORDINATES 3 float 0.0 0.25 0.5 Y_COORDINATES 3 float 0.0 0.5 1.0 Z_COORDINATES 3 float 0.0 0.75 1.5 POINT_DATA 27 SCALARS scalar_name float 1 LOOKUP_TABLE default ...
Reading in Data
Following is a Python example of how to read Structured Points data into VTK.
#!/usr/bin/python
from vtk import * ;
# Create the reader for the data
Reader = vtkStructuredPointsReader()
Reader.SetFileName("/path/to/filename.vtk")
Following is a TCL example of how to read Structured Points data into VTK.
package require vtk
package require vtkinteraction
# Create the reader for the data
vtkStructuredPointsReader reader
reader SetFileName "/path/to/filename.vtk"

