Skip to content

Commit

Permalink
vtkSurfaceWriter: Rationalised writing to use vtkWriteOps
Browse files Browse the repository at this point in the history
avoiding code duplication and providing support for writing binary.
  • Loading branch information
Jakub Knir committed Jan 28, 2020
1 parent 9ff1e2e commit 622f472
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 161 deletions.
200 changes: 45 additions & 155 deletions src/sampling/sampledSurface/writers/vtk/vtkSurfaceWriter.C
Expand Up @@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
Expand All @@ -27,6 +27,7 @@ License
#include "OFstream.H"
#include "OSspecific.H"
#include "makeSurfaceWriterMethods.H"
#include "vtkWriteOps.H"

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

Expand All @@ -35,34 +36,35 @@ namespace Foam
makeSurfaceWriterType(vtkSurfaceWriter);
}

static bool binary = false;

// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //

void Foam::vtkSurfaceWriter::writeGeometry
(
Ostream& os,
std::ostream& os,
const pointField& points,
const faceList& faces
)
{
// header
os
<< "# vtk DataFile Version 2.0" << nl
<< "sampleSurface" << nl
<< "ASCII" << nl
<< "DATASET POLYDATA" << nl;
// VTK header
vtkWriteOps::writeHeader(os, binary, "sampleSurface");
os << "DATASET POLYDATA" << nl;

// Write vertex coords
os << "POINTS " << points.size() << " double" << nl;
os << "POINTS " << points.size() << " float" << nl;

List<floatScalar> po(points.size()*3);
label ind = 0;
forAll(points, pointi)
{
const point& pt = points[pointi];
os << float(pt.x()) << ' '
<< float(pt.y()) << ' '
<< float(pt.z()) << nl;
forAll(pt, cmpt)
{
po[ind++] = float(pt[cmpt]);
}
}
os << nl;

vtkWriteOps::write(os, binary, po);

// Write faces
label nNodes = 0;
Expand All @@ -74,146 +76,18 @@ void Foam::vtkSurfaceWriter::writeGeometry
os << "POLYGONS " << faces.size() << ' '
<< faces.size() + nNodes << nl;

labelList polygons(faces.size() + nNodes);
ind = 0;
forAll(faces, facei)
{
const face& f = faces[facei];

os << f.size();
polygons[ind++] = f.size();
forAll(f, fp)
{
os << ' ' << f[fp];
}
os << nl;
}
}


namespace Foam
{

template<>
void Foam::vtkSurfaceWriter::writeData
(
Ostream& os,
const Field<scalar>& values
)
{
os << "1 " << values.size() << " float" << nl;

forAll(values, elemI)
{
if (elemI)
{
if (elemI % 10)
{
os << ' ';
}
else
{
os << nl;
}
}

os << float(values[elemI]);
}
os << nl;
}


template<>
void Foam::vtkSurfaceWriter::writeData
(
Ostream& os,
const Field<vector>& values
)
{
os << "3 " << values.size() << " float" << nl;

forAll(values, elemI)
{
const vector& v = values[elemI];
os << float(v[0]) << ' ' << float(v[1]) << ' ' << float(v[2])
<< nl;
}
}


template<>
void Foam::vtkSurfaceWriter::writeData
(
Ostream& os,
const Field<sphericalTensor>& values
)
{
os << "1 " << values.size() << " float" << nl;

forAll(values, elemI)
{
const sphericalTensor& v = values[elemI];
os << float(v[0]) << nl;
}
}


template<>
void Foam::vtkSurfaceWriter::writeData
(
Ostream& os,
const Field<symmTensor>& values
)
{
os << "6 " << values.size() << " float" << nl;

forAll(values, elemI)
{
const symmTensor& v = values[elemI];
os << float(v[0]) << ' ' << float(v[1]) << ' ' << float(v[2])
<< ' '
<< float(v[3]) << ' ' << float(v[4]) << ' ' << float(v[5])
<< nl;

polygons[ind++] = f[fp];
}
}


template<>
void Foam::vtkSurfaceWriter::writeData
(
Ostream& os,
const Field<tensor>& values
)
{
os << "9 " << values.size() << " float" << nl;

forAll(values, elemI)
{
const tensor& v = values[elemI];
os << float(v[0]) << ' ' << float(v[1]) << ' ' << float(v[2])
<< ' '
<< float(v[3]) << ' ' << float(v[4]) << ' ' << float(v[5])
<< ' '
<< float(v[6]) << ' ' << float(v[7]) << ' ' << float(v[8])
<< nl;
}
}

}


// Write generic field in vtk format
template<class Type>
void Foam::vtkSurfaceWriter::writeData
(
Ostream& os,
const Field<Type>& values
)
{
os << "1 " << values.size() << " float" << nl;

forAll(values, elemI)
{
os << float(0) << nl;
}
vtkWriteOps::write(os, binary, polygons);
}


Expand All @@ -235,16 +109,18 @@ void Foam::vtkSurfaceWriter::writeTemplate
mkDir(outputDir);
}

OFstream os(outputDir/fieldName + '_' + surfaceName + ".vtk");
const word filePath = outputDir/fieldName + '_' + surfaceName + ".vtk";

ofstream os(filePath, std::ios::binary);

if (verbose)
{
Info<< "Writing field " << fieldName << " to " << os.name() << endl;
Info<< "Writing field " << fieldName << " to " << filePath << endl;
}

writeGeometry(os, points, faces);

// start writing data
// Write data
if (isNodeValues)
{
os << "POINT_DATA ";
Expand All @@ -258,8 +134,21 @@ void Foam::vtkSurfaceWriter::writeTemplate
<< "FIELD attributes 1" << nl
<< fieldName << " ";

// Write data
writeData(os, values);
const label nComp = pTraits<Type>::nComponents;

os << nComp << " " << values.size() << " float" << nl;

List<floatScalar> vals(values.size()*nComp);
label ind = 0;
forAll(values, elemI)
{
for (direction cmpt=0; cmpt < nComp; ++cmpt)
{
vals[ind++] = component(values[elemI], cmpt);
}
}

vtkWriteOps::write(os, binary, vals);
}


Expand Down Expand Up @@ -293,18 +182,19 @@ void Foam::vtkSurfaceWriter::write
mkDir(outputDir);
}

OFstream os(outputDir/surfaceName + ".vtk");
word filePath = outputDir/surfaceName + ".vtk";
ofstream os(filePath, std::ios::binary);

if (verbose)
{
Info<< "Writing geometry to " << os.name() << endl;
Info<< "Writing geometry to " << filePath << endl;
}

writeGeometry(os, points, faces);
}


// create write methods
// Create write methods
defineSurfaceWriterWriteFields(Foam::vtkSurfaceWriter);


Expand Down
14 changes: 8 additions & 6 deletions src/sampling/sampledSurface/writers/vtk/vtkSurfaceWriter.H
Expand Up @@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
Expand Down Expand Up @@ -52,11 +52,12 @@ class vtkSurfaceWriter
{
// Private Member Functions

static void writeGeometry(Ostream&, const pointField&, const faceList&);

template<class Type>
static void writeData(Ostream&, const Field<Type>&);

static void writeGeometry
(
std::ostream&,
const pointField&,
const faceList&
);

//- Templated write operation
template<class Type>
Expand All @@ -72,6 +73,7 @@ class vtkSurfaceWriter
const bool verbose
) const;


public:

//- Runtime type information
Expand Down

0 comments on commit 622f472

Please sign in to comment.