Introduction
The legacy .vtk format is the simple file format of the
Visualization Toolkit (VTK), also read and written by ParaView and many
simulation and meshing tools. One file holds one dataset: points and cells or a regular grid, followed by
named arrays of values on the points and cells. VTK also has an XML family of formats
(.vtu, .vtp and others) with different syntax.
File layout
| Part | Contents |
|---|---|
| 1. Identifier | # vtk DataFile Version x.x. VTK checks the first 22 characters exactly, then reads major.minor. |
| 2. Title | One line of free text, at most 256 characters. |
| 3. Encoding | ASCII or BINARY. |
| 4. Geometry | DATASET type and the statements for that type. Optional. |
| 5. Attributes | POINT_DATA n and CELL_DATA n sections, in either order. Optional. |
Keywords are not case-sensitive and are separated by white space. Indices start at 0. Geometry comes before attributes, and each count n must equal the number of points or cells of the geometry.
Versions
The version in the first line has passed through 1.0, 2.0, 3.0, 4.x, 5.0 and 5.1. VTK writes 5.1 by default
and can still write 4.2. The main difference is how cell arrays are stored from version
5. Version 1.0 used ASPECT_RATIO for what is now SPACING, and files before 2.3 had
no component count on SCALARS.
ASCII and binary
Keyword lines are text in both encodings. In a binary file only the data values are binary: they start straight after the newline that ends the preceding keyword line and are stored big-endian.
Data types
| Name | Binary form |
|---|---|
bit | Packed eight to a byte, first value in the highest bit; the array is padded to a whole byte. |
char, signed_char, unsigned_char | 1 byte. |
short, unsigned_short | 2 bytes. |
int, unsigned_int | 4 bytes. |
vtkidtype | Written as a 4-byte integer. |
long, unsigned_long | 4 or 8 bytes, the size of long on the system. VTK no longer writes them. |
vtktypeint64, vtktypeuint64 | 8 bytes. |
float | 4-byte IEEE. |
double | 8-byte IEEE. |
string, utf8_string | Strings. |
variant | Written as text: a VTK type number and the encoded value for each element. |
Strings and names
In an ASCII file a string array has one element per line, with characters such as spaces percent-encoded
(%20). Array names are encoded the same way, since a name may not contain white space. In a
binary file each string has a length prefix whose top two bits give the prefix size; the remaining bits,
big-endian, are the length, followed by that many bytes.
| Top two bits of the first byte | Prefix size | Length bits |
|---|---|---|
| 11 | 1 byte | 6 |
| 10 | 2 bytes | 14 |
| 01 | 4 bytes | 30 |
| 00 | 8 bytes | 62 |
Dataset types
In structured datasets the values are ordered with x changing fastest, then y, then
z. A FIELD block may also appear among the geometry statements and holds data that
belongs to the whole dataset.
STRUCTURED_POINTS
DATASET STRUCTURED_POINTS
DIMENSIONS nx ny nz
ORIGIN x y z
SPACING sx sy sz
STRUCTURED_GRID
DATASET STRUCTURED_GRID
DIMENSIONS nx ny nz
POINTS n dataType
x y z …
RECTILINEAR_GRID
DATASET RECTILINEAR_GRID
DIMENSIONS nx ny nz
X_COORDINATES nx dataType
x0 x1 …
Y_COORDINATES ny dataType
y0 y1 …
Z_COORDINATES nz dataType
z0 z1 …
POLYDATA
POINTS, then any of VERTICES, LINES, POLYGONS and
TRIANGLE_STRIPS, each a cell array.
UNSTRUCTURED_GRID
POINTS, then CELLS (a cell array) and
CELL_TYPES n followed by one type number per cell. In binary files the
type numbers are 4-byte integers.
FIELD
A file may hold only a data object with no geometry: a field block, written as
FIELD name numArrays, with no point or cell sections.
Cell arrays
Before version 5
The keyword takes the number of cells and the total number of integers that follow. Each cell is its point
count followed by that many point indices. All are int.
POLYGONS 2 8
3 0 1 2
3 2 1 3
Version 5.0 and later
The two numbers are the length of the offsets array (cells + 1) and the length of the connectivity array. Then come the two arrays, each with its data type. Cell i uses the connectivity entries from offset i up to offset i + 1. If the first number is less than 1 the array is empty and nothing follows.
POLYGONS 3 6
OFFSETS vtktypeint64
0 3 6
CONNECTIVITY vtktypeint64
0 1 2 2 1 3
Cell type numbers
| Number | Cell |
|---|---|
| 0 | Empty cell |
| 1, 2 | Vertex, poly-vertex |
| 3, 4 | Line, poly-line |
| 5, 6 | Triangle, triangle strip |
| 7 | Polygon |
| 8, 9 | Pixel, quadrilateral |
| 10 | Tetrahedron |
| 11, 12 | Voxel, hexahedron |
| 13, 14 | Wedge, pyramid |
| 15, 16 | Pentagonal prism, hexagonal prism |
| 21, 22, 23, 24 | Quadratic edge, triangle, quadrilateral, tetrahedron |
| 25, 26, 27 | Quadratic hexahedron, wedge, pyramid |
| 28, 29 | Biquadratic quadrilateral, triquadratic hexahedron |
| 30, 31 | Quadratic-linear quadrilateral, quadratic-linear wedge |
| 32, 33, 34 | Biquadratic-quadratic wedge, biquadratic-quadratic hexahedron, biquadratic triangle |
| 35, 36, 37 | Cubic line, quadratic polygon, triquadratic pyramid |
| 41, 42 | Convex point set, polyhedron |
| 60, 61, 62 | Higher-order curve, triangle, quadrilateral |
| 64, 65, 66, 67 | Higher-order tetrahedron, wedge, pyramid (placeholder), hexahedron |
| 68 to 74 | Lagrange curve, triangle, quadrilateral, tetrahedron, hexahedron, wedge, pyramid (placeholder) |
| 75 to 81 | Bézier curve, triangle, quadrilateral, tetrahedron, hexahedron, wedge, pyramid (placeholder) |
Dataset attributes
Inside a POINT_DATA or CELL_DATA section any number of the following may appear. The
number of tuples n is that of the section.
| Statement | Data that follows |
|---|---|
SCALARS name dataType [numComp]LOOKUP_TABLE tableName | n tuples of 1 to 4 components (default 1). The table name is default for the built-in table. |
COLOR_SCALARS name nValues | n × nValues colour values: floats from 0 to 1 in ASCII, unsigned bytes in binary. |
LOOKUP_TABLE name size | size red, green, blue, alpha entries: floats 0 to 1 in ASCII, 4 unsigned bytes in binary. Alpha 0 is transparent. |
VECTORS name dataType | n × 3 values. |
NORMALS name dataType | n × 3 values, unit length. |
TEXTURE_COORDINATES name dim dataType | n × dim values, dim 1 to 3. |
TENSORS name dataType | n 3×3 matrices, row by row (9 values). |
TENSORS6 name dataType | n symmetric tensors of 6 values. |
GLOBAL_IDS name dataType | n identifiers. |
PEDIGREE_IDS name dataType | n identifiers. |
EDGE_FLAGS name dataType | n flags; point data only. |
FIELD name numArrays | Named arrays of any shape. |
Field blocks
Each array in a field block has a header line arrayName numComponents numTuples dataType
followed by its values. An entry that is just NULL_ARRAY is an empty slot. In files before
version 4, VTK renames an unsigned-char array called vtkGhostLevels to its current ghost array
and converts its non-zero values.
Array metadata
An array’s values may be followed by a METADATA line and a block that ends at a blank line. It
can contain:
COMPONENT_NAMES, followed by one percent-encoded name per component on its own line.INFORMATION n, followed by n keys. Each starts withNAME keyName LOCATION classNameand has aDATAline; for a vector key the line gives the length before the values. Keys VTK does not know are skipped with a warning.
Example
# vtk DataFile Version 2.0
Two triangles
ASCII
DATASET UNSTRUCTURED_GRID
POINTS 4 float
0 0 0 1 0 0 0 1 0 1 1 0
CELLS 2 8
3 0 1 2
3 2 1 3
CELL_TYPES 2
5
5
POINT_DATA 4
SCALARS height float 1
LOOKUP_TABLE default
0.0 0.5 0.5 1.0
Recognising a file
The file begins with the 22 characters # vtk DataFile Version, and its third line is
ASCII or BINARY.
Graphs, tables and composite datasets
These dataset kinds are read by VTK’s other legacy readers. Names are written with %XX for
bytes such as spaces.
DIRECTED_GRAPH,UNDIRECTED_GRAPHandMOLECULE:POINTS,VERTICES n,EDGES nfollowed by a source and target vertex for each edge, thenVERTEX_DATAandEDGE_DATAattributes. A molecule (points are atoms, edges are bonds) can addLATTICE_A,LATTICE_B,LATTICE_CandLATTICE_ORIGIN, three numbers each.TREE:POINTSandEDGES nas child and parent pairs; the tree has n+1 vertices.TABLE: field data, thenROW_DATA nand the columns as attributes.MULTIBLOCK,MULTIPIECE,PARTITIONED:CHILDREN n, then for each child a lineCHILD type [name], a complete legacy file and a lineENDCHILD. Type −1 is an empty child. Composite children nest.PARTITIONED_COLLECTION: the same, thenDATAASSEMBLY 0or1; after 1 a one-value string array holds the assembly as XML.OVERLAPPING_AMRandHIERARCHICAL_BOX:GRID_DESCRIPTION n,ORIGIN x y z,LEVELS nwith a lineblocks sx sy szper level,AMRBOXES tuples componentswith an integer array (low and high cell indices of each box), thenCHILD level index…ENDCHILDblocks holding image data. VTK’s reader does not readNON_OVERLAPPING_AMR.
Orders and numbers
- The six components of
TENSORS6are XX, YY, ZZ, XY, YZ, XZ. - A
variantarray is text even in a binary file: a type number and an encoded value for each entry. The numbers are 1 bit, 2 char, 3 unsigned char, 4 short, 5 unsigned short, 6 int, 7 unsigned int, 8 long, 9 unsigned long, 10 float, 11 double, 12 id type, 13 string, 15 signed char, 16 long long, 17 unsigned long long, 20 variant. - The cell list entry of a polyhedron (type 42) is: its length, the number of faces, then for each face the number of points and the point numbers.
- In files before version 5.1, Lagrange and Bézier hexahedra list the points of the last two edges parallel to the third axis in the opposite order; VTK swaps them on reading.
- The size of
longin a binary file is that of the system that wrote it: 8 bytes on 64-bit Linux and macOS, 4 on Windows. - After
METADATA,INFORMATION nintroduces n keys, each a lineNAME name LOCATION classand aDATAline: one number or encoded string; for number vectors the length and the values; for string vectors the length, then one line per string. A blank line ends the block.
In the explorer
- Every keyword block and array is in the tree, and every point, cell and array value is listed in tables. ASCII files also show as text.
- Structured, unstructured, polygonal, graph, tree and molecule datasets are drawn in 3D, coloured by cell type, by any array or component, by colour scalars or through a lookup table. Solids show their outer faces; every face of every cell is a further view. Large meshes are split into parts of 600,000 drawn items, which together have everything.
- Composite and AMR files open block by block and as one joined view.
- Binary
longis read as 8 bytes; the 4-byte reading is offered as the other variant. - Files up to 256 MB are read. Image and rectilinear grids of more than 100,000,000 points have no 3D view or point list.
Not described
Known, not yet covered
- Reference: the
cell_gridandstatistical_modeldatasets, and the point ordering of each cell type. - Viewer:
cell_gridandstatistical_modeldatasets are not read. - Viewer: cells of types 51–56, 60–67, 74 and 81 are drawn as their points only.
- Viewer: Bézier faces of degree above 12 are drawn on their control points; Lagrange and Bézier triangles of 7 points, tetrahedra of 15 and wedges of 21 on their quadratic points; polygons of more than 2,000 corners as a fan; convex point sets of more than 64 points as points.
- Viewer: clicking in a 3D view does not select the cell, and ghost (blanked) points and cells are drawn like the others.
Links
Software
- ParaView – visualisation program built on VTK (Windows, Linux, macOS)
Developers
- Visualization Toolkit – the library that defines the format (C++, Python)
Reference
- VTK legacy file formats – the official description
- vtkDataReader.cxx – the shared reader
- vtkCellType.h – cell type numbers
- VTK IO/Legacy – the readers of graphs, trees, tables and composite datasets
- vtkType.h and vtkMath.h – type numbers and symmetric tensor order