From 622f4722830b8c8a40f9a4f839d8804e2598a89e Mon Sep 17 00:00:00 2001 From: Jakub Knir Date: Tue, 28 Jan 2020 17:33:29 +0000 Subject: [PATCH] vtkSurfaceWriter: Rationalised writing to use vtkWriteOps avoiding code duplication and providing support for writing binary. --- .../writers/vtk/vtkSurfaceWriter.C | 200 ++++-------------- .../writers/vtk/vtkSurfaceWriter.H | 14 +- 2 files changed, 53 insertions(+), 161 deletions(-) diff --git a/src/sampling/sampledSurface/writers/vtk/vtkSurfaceWriter.C b/src/sampling/sampledSurface/writers/vtk/vtkSurfaceWriter.C index 8e4a30daa6..95029337bd 100644 --- a/src/sampling/sampledSurface/writers/vtk/vtkSurfaceWriter.C +++ b/src/sampling/sampledSurface/writers/vtk/vtkSurfaceWriter.C @@ -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 @@ -27,6 +27,7 @@ License #include "OFstream.H" #include "OSspecific.H" #include "makeSurfaceWriterMethods.H" +#include "vtkWriteOps.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -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 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; @@ -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& 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& 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& 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& 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& 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 -void Foam::vtkSurfaceWriter::writeData -( - Ostream& os, - const Field& values -) -{ - os << "1 " << values.size() << " float" << nl; - - forAll(values, elemI) - { - os << float(0) << nl; - } + vtkWriteOps::write(os, binary, polygons); } @@ -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 "; @@ -258,8 +134,21 @@ void Foam::vtkSurfaceWriter::writeTemplate << "FIELD attributes 1" << nl << fieldName << " "; - // Write data - writeData(os, values); + const label nComp = pTraits::nComponents; + + os << nComp << " " << values.size() << " float" << nl; + + List 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); } @@ -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); diff --git a/src/sampling/sampledSurface/writers/vtk/vtkSurfaceWriter.H b/src/sampling/sampledSurface/writers/vtk/vtkSurfaceWriter.H index 1ec9b6a727..af4c289caf 100644 --- a/src/sampling/sampledSurface/writers/vtk/vtkSurfaceWriter.H +++ b/src/sampling/sampledSurface/writers/vtk/vtkSurfaceWriter.H @@ -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 @@ -52,11 +52,12 @@ class vtkSurfaceWriter { // Private Member Functions - static void writeGeometry(Ostream&, const pointField&, const faceList&); - - template - static void writeData(Ostream&, const Field&); - + static void writeGeometry + ( + std::ostream&, + const pointField&, + const faceList& + ); //- Templated write operation template @@ -72,6 +73,7 @@ class vtkSurfaceWriter const bool verbose ) const; + public: //- Runtime type information