1. Tunç Bahçecioğlu
  2. xvtkw



A c++ library to create paraview files (.vtu and .pvd)


Paraview files consist of a series of ".vtu" files that store information for a part or whole of a timestep and an index file with extension ".pvd". XvtkW library has to main objects VtuFile and PvdFile. First you create your VtuFile objects then add them to a PvdFile object.

Creating VTU files

#include <xvtkw\VtuFile.h>
#include <xvtkw\PvdFile.h>
// ...
xvtkw::VtuFile<float> vtu1(xvtkw::ByteOrder::LittleEndian);
xvtkw::VtuFile<float> vtu2(xvtkw::ByteOrder::LittleEndian);

The template parameter of the VtuFile determines toe coordinate type of the points of the model. It is usually either float or double. Constructer takes a ByteOrder type which is either LittleEndian or BigEndian which should depend on the platform you use. For Windows it is generally LittleEndian and for mac it is BigEndian. (Since the library currently does not support binary formats, it is used for filling file parameters in paraview files.)

Creating the model

The first part of a VTU file is the model which consists of points and cells. Cells are geometries defined by a type and a series of points that define the edge coordinates of the geometry.

// Pushing points to a vtu file
vtu1.points.push_back(xvtkw::Point<float>(0.f, 0.f, 0.f));
vtu1.points.push_back(xvtkw::Point<float>(10.f, 0.f, 0.f));
vtu1.points.push_back(xvtkw::Point<float>(10.f, 10.f, 0.f));
// Pushing cells to a vtu file
vtu1.cells.push_back(xvtkw::Cell(xvtkw::VtuCellType::Line, { 0, 1 }));
vtu1.cells.push_back(xvtkw::Cell(xvtkw::VtuCellType::Line, { 1, 2 }));

Creating points is straight forward. You push a xvtkw::Point object that is constructed with x, y, z coordinates to the points vector of the VtuFile object. In similar fashion you push a xvtkw::Cell object to the cells member of the VtuFile object to create cells. the first parameter of the Cell constructor defines the geometrical shape of the cell and the second parameter defines the point sequence of the cell. Note that first created point has the id 0 and other points are given ids in sequentially increasing fashion. The cell type resemble the related enum in the fhe file: https://github.com/Kitware/VTK/blob/master/Common/DataModel/vtkCellType.h

Creating datasets

The second part of a VTU file is the datasets associated with points and cells.

// Define a point dataset
xvtkw::DataSet point_displacements_1(xvtkw::VtuType::Float64, 3, xvtkw::DataSetAttribute::Vectors);
point_displacements_1.add_to_data<double>({ 0, 0, 0, 0.5, 0, 0, 0.5, 0, 0 });
vtu1.point_datasets["Displacements"] = std::move(point_displacements_1);

// Define a cell dataset
xvtkw::DataSet cell_partitions_1(xvtkw::VtuType::Int32, 1, xvtkw::DataSetAttribute::None);
cell_partitions_1.add_to_data({ 1, 1 });
vtu1.cell_datasets["Partition"] = std::move(cell_partitions_1);

vtkw::DataSet object defines a general dataset. Its constructor has three arguments, first one defines the type of the dataset, second one defines the number of values requried to define the data for each point or cell. The thirs argument assigns an attribute to the dataset which is one of the following:Scalars, Vectors, Normals, Tensors, TCoords. See VTK documentation for more information.

The add_to_data Function is used to push data to the dataset. You can push all the data (six numbers for three points in the example) using an initializer_list or you can use a series of calls to push data one by one. The data should follow the point or cell creation sequence.

After creating the dataset VtuFile::point_datasets or VtuFile::cell_datasets to associate the dataset with a string name and push them to VtuFile.

Writing VtuFiles

xvtkw::to_file(vtu1, "d:\\temp\\vtu1.vtu");

Creating PVD files

xvtkw::PvdFile pvd(xvtkw::ByteOrder::LittleEndian);
pvd.collection.push_back(xvtkw::PvdItem(0, 0, "d:\\temp\\vtu1.vtu"));
pvd.collection.push_back(xvtkw::PvdItem(0, 1, "d:\\temp\\vtu2.vtu"));
xvtkw::to_file(pvd, "d:\\temp\\vtu.pvd");

First you create a PvdFile object similar to VtuFile creation. Then you add your VTU files to the PVD index. This is done using PvdItem objects. The first argument of its constructor is a float number defining the timestep. Second is the part of the model that VTU file defines. Finally third argument is the file name. Then to_file Function is used to write the PVD file.


  • added: Dataset attributes
  • changed: VtuCellType enumeration to match recent VTK