Last modified: June 29, 2024
This article is written in: 🇺🇸
VTK offers a comprehensive suite of tools for reading and writing a variety of data formats. This includes the native VTK file formats (legacy and XML-based), as well as numerous third-party formats.
VTK supports an extensive range of data formats, including:
I. Legacy VTK File Format
Example:
# vtk DataFile Version 3.0
VTK Example Data
ASCII
DATASET POLYDATA
POINTS 8 float
0.0 0.0 0.0
1.0 0.0 0.0
1.0 1.0 0.0
0.0 1.0 0.0
0.0 0.0 1.0
1.0 0.0 1.0
1.0 1.0 1.0
0.0 1.0 1.0
POLYGONS 6 30
4 0 1 2 3
...
II. XML-Based VTK File Format
.vtp
for PolyData, .vtu
for UnstructuredGrid, and .vts
for StructuredGrid, among others.Example:
<?xml version="1.0"?>
<vtkfile byte_order="LittleEndian" type="PolyData" version="0.1">
<polydata>
<piece numberoflines="0" numberofpoints="8" numberofpolys="6" numberofstrips="0" numberofverts="0">
<points>
<dataarray format="ascii" numberofcomponents="3" type="Float32">
0.0 0.0 0.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 0.0 ...
</dataarray>
</points>
<polys>
<dataarray format="ascii" name="connectivity" type="Int32">
0 1 2 3 ...
</dataarray>
...
</polys>
</piece>
</polydata>
</vtkfile>
III. Third-Party File Formats
Example (STL):
solid vtkGenerated
facet normal 0 0 -1
outer loop
vertex 0.0 0.0 0.0
vertex 1.0 0.0 0.0
vertex 1.0 1.0 0.0
endloop
endfacet
...
endsolid vtkGenerated
Example (PLY):
ply
format ascii 1.0
element vertex 8
property float x
property float y
property float z
element face 6
property list uchar int vertex_indices
end_header
0.0 0.0 0.0
1.0 0.0 0.0
...
3 0 1 2
...
There is a suite of subclasses derived from vtkDataReader
and vtkDataWriter
. These subclasses are specialized for handling various VTK data structures, emphasizing efficient and accurate data manipulation. The design ensures flexibility in reading and writing different types of data while maintaining the robustness of data integrity and format compatibility.
Each subclass under vtkDataReader
and vtkDataWriter
is tailored for specific data structures, facilitating precise and optimized read/write operations:
Below is a Python script demonstrating how to read data from an STL file (common in 3D printing and modeling) and write it into VTK's native format.
import vtk
# Initialize an STL reader and set the file to read
stl_reader = vtk.vtkSTLReader()
stl_reader.SetFileName("input.stl")
# Set up a VTK writer and connect it to the output of the STL reader
vtk_writer = vtk.vtkPolyDataWriter()
vtk_writer.SetInputConnection(stl_reader.GetOutputPort())
vtk_writer.SetFileName("output.vtk")
# Execute the writing process to convert the STL file to a VTK file
vtk_writer.Write()
This script shows the straightforward approach of VTK in converting data between different formats, highlighting its powerful data processing capabilities.
A comparison of various readers and writers for different formats is provided below:
Format | Reader Class | Output Data Type | Writer Class | Input Data Type |
STL | vtkSTLReader |
vtkPolyData |
vtkSTLWriter |
vtkPolyData |
OBJ | vtkOBJReader |
vtkPolyData |
vtkOBJWriter |
vtkPolyData |
VTK (Legacy) | vtkUnstructuredGridReader |
vtkUnstructuredGrid |
vtkUnstructuredGridWriter |
vtkUnstructuredGrid |
vtkStructuredGridReader |
vtkStructuredGrid |
vtkStructuredGridWriter |
vtkStructuredGrid |
|
vtkPolyDataReader |
vtkPolyData |
vtkPolyDataWriter |
vtkPolyData |
|
vtkRectilinearGridReader |
vtkRectilinearGrid |
vtkRectilinearGridWriter |
vtkRectilinearGrid |
|
vtkStructuredPointsReader |
vtkStructuredPoints |
vtkStructuredPointsWriter |
vtkStructuredPoints |
|
VTU | vtkXMLUnstructuredGridReader |
vtkUnstructuredGrid |
vtkXMLUnstructuredGridWriter |
vtkUnstructuredGrid |
VTM | vtkXMLMultiBlockDataReader |
vtkMultiBlockDataSet |
vtkXMLMultiBlockDataWriter |
vtkMultiBlockDataSet |
OpenFOAM | vtkOpenFOAMReader |
vtkMultiBlockDataSet |
N/A | N/A |
EnSight | vtkEnSightGoldReader |
vtkMultiBlockDataSet |
N/A | N/A |