How to write a set of points to VTK in OpenFOAM using legacy code

Sometimes geometrical data from a test needs to be visualized. This shows how to visualize this data using native OpenFOAM VTK output libraries.

Step-by-step guide

Application / library code

// VTK output dependencies.
#include "foamVtkLegacyAsciiFormatter.H"
#include "foamVtkOutput.H"
#include "pointList.H"
#include "point.H"
#include "List.H"
#include "pointList.H"
#include <fstream>
 
// Testing dependencies
 
// Only use this in application cod.
using namespace Foam;
using namespace Foam::vtk;
 
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
int main(int argc, char *argv[])
{
    // A set of test points. 
    pointList testPoints = {point(0,0,0), point(1,0,0),
                            point(1,1,0), point(0,1,0),
                            point(0.5,0.5,1)};
 
    // Open file for output in overwrite mode.
    std::ofstream vtkFile("square.vtk");
 
    // Initialize the VTK formatter, legacy in this case.
    legacyAsciiFormatter legacyFormat(vtkFile, 15);
 
    // Write the file header based on the chosen format (legacy VTK).
    legacy::fileHeader(legacyFormat, "testPoints", vtk::fileTag::POLY_DATA);
 
    // Write the points beginning line for legacy VTK. 
    legacy::beginPoints(vtkFile, testPoints.size());
 
    // Write the list of points using the legacy formatter.
    writeList(legacyFormat, testPoints);
 
    return 0;
}

Dependencies in Make/options

EXE_INC = \
    -std=c++1y \
    -I$(LIB_SRC)/OpenFOAM/lnInclude \
    -I$(LIB_SRC)/fileFormats/lnInclude
 
EXE_LIBS = \
    -lOpenFOAM \
    -lfileFormats

Avoid using namespace declarations in the library code, use fully declared names like ‘vtk::legacy::beginPoints’ instead, it is safer.

Source code of a working example is available on Bitbucket.

Visualization in Paraview:

Open square.vtk, then use the ‘Glyph’ filter and select ‘Sphere’ for ‘Glyph Type’. You should see the following visualization rendered as ‘Surface with Edges’:

foamWritePointsToVtk

See also