From 4380349f45e14872bbdd3d92e20937072c20b1fd Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@esi-group.com> Date: Thu, 11 Jul 2019 13:42:02 +0200 Subject: [PATCH] ENH: add vtk::writeIdentity() and optional repeat value for vtk::write() - An identity is often useful when generating connectivity and offset information. - The optional repeat value for vtk::write() allows it to also be used as a fill method. --- .../vtk/output/foamVtkInternalWriter.C | 7 +-- .../vtk/output/foamVtkPatchWriter.C | 59 +++++-------------- .../vtk/file/foamVtkFileWriterTemplates.C | 5 +- src/fileFormats/vtk/output/foamVtkOutput.C | 18 +++++- src/fileFormats/vtk/output/foamVtkOutput.H | 54 ++++------------- src/fileFormats/vtk/output/foamVtkOutputI.H | 41 ++++++++++--- .../vtk/output/foamVtkOutputTemplates.C | 13 ++-- .../lagrangian/vtkCloud/vtkCloud.C | 14 ++--- .../conversion/vtk/foamVtkLagrangianWriter.C | 16 ++--- .../lumpedPointMovementWriter.C | 20 +++---- .../lumpedPointStateWriter.C | 36 ++++------- .../surfaceFormats/vtk/VTKsurfaceFormatCore.C | 11 ++-- .../surfaceFormats/vtp/VTPsurfaceFormatCore.C | 11 ++-- 13 files changed, 127 insertions(+), 178 deletions(-) diff --git a/src/conversion/vtk/output/foamVtkInternalWriter.C b/src/conversion/vtk/output/foamVtkInternalWriter.C index af675ce176f..0d43a65c50f 100644 --- a/src/conversion/vtk/output/foamVtkInternalWriter.C +++ b/src/conversion/vtk/output/foamVtkInternalWriter.C @@ -711,12 +711,7 @@ bool Foam::vtk::internalWriter::writeProcIDs() // Per-processor ids for (label proci=0; proci < Pstream::nProcs(); ++proci) { - label len = procMaps.localSize(proci); - - while (len--) - { - format().write(proci); - } + vtk::write(format(), proci, procMaps.localSize(proci)); } format().flush(); diff --git a/src/conversion/vtk/output/foamVtkPatchWriter.C b/src/conversion/vtk/output/foamVtkPatchWriter.C index b47c59cb323..890b4fd3aed 100644 --- a/src/conversion/vtk/output/foamVtkPatchWriter.C +++ b/src/conversion/vtk/output/foamVtkPatchWriter.C @@ -587,13 +587,7 @@ void Foam::vtk::patchWriter::writePatchIDs() { for (const label patchId : patchIDs_) { - label count = patches[patchId].size(); - const label val = patchId; - - while (count--) - { - format().write(val); - } + vtk::write(format(), patchId, patches[patchId].size()); } } @@ -615,16 +609,13 @@ void Foam::vtk::patchWriter::writePatchIDs() fromSlave >> recv; - for (label i=0; i < recv.size(); ++i) + // Receive as [size, id] pairs + for (label i=0; i < recv.size(); i += 2) { - label count = recv[i]; - ++i; - const label val = recv[i]; + const label len = recv[i]; + const label val = recv[i+1]; - while (count--) - { - format().write(val); - } + vtk::write(format(), val, len); } } } @@ -722,12 +713,7 @@ bool Foam::vtk::patchWriter::writeProcIDs() // Per-processor ids for (label proci=0; proci < Pstream::nProcs(); ++proci) { - label len = procSizes.localSize(proci); - - while (len--) - { - format().write(proci); - } + vtk::write(format(), proci, procSizes.localSize(proci)); } good = true; @@ -735,14 +721,7 @@ bool Foam::vtk::patchWriter::writeProcIDs() } else { - const label proci = Pstream::myProcNo(); - - label len = nLocalFaces_; - - while (len--) - { - format().write(proci); - } + vtk::write(format(), Pstream::myProcNo(), nLocalFaces_); good = true; } @@ -811,16 +790,11 @@ bool Foam::vtk::patchWriter::writeNeighIDs() { for (const label patchId : patchIDs_) { - label count = patches[patchId].size(); - const auto* pp = isA<processorPolyPatch>(patches[patchId]); - const label val = (pp ? pp->neighbProcNo() : -1); + const label val = (pp ? pp->neighbProcNo() : -1); - while (count--) - { - format().write(val); - } + vtk::write(format(), val, patches[patchId].size()); } good = true; @@ -844,16 +818,13 @@ bool Foam::vtk::patchWriter::writeNeighIDs() fromSlave >> recv; - for (label i=0; i < recv.size(); ++i) + // Receive as [size, id] pairs + for (label i=0; i < recv.size(); i += 2) { - label count = recv[i]; - ++i; - const label val = recv[i]; + const label len = recv[i]; + const label val = recv[i+1]; - while (count--) - { - format().write(val); - } + vtk::write(format(), val, len); } } } diff --git a/src/fileFormats/vtk/file/foamVtkFileWriterTemplates.C b/src/fileFormats/vtk/file/foamVtkFileWriterTemplates.C index 606d522c4ae..00a51fbceb3 100644 --- a/src/fileFormats/vtk/file/foamVtkFileWriterTemplates.C +++ b/src/fileFormats/vtk/file/foamVtkFileWriterTemplates.C @@ -81,10 +81,7 @@ void Foam::vtk::fileWriter::writeUniform if (format_) { - for (label i=0; i < nValues; ++i) - { - vtk::write(format(), val); - } + vtk::write(format(), val, nValues); } if (format_) diff --git a/src/fileFormats/vtk/output/foamVtkOutput.C b/src/fileFormats/vtk/output/foamVtkOutput.C index 5311fd0db1d..25eeb501483 100644 --- a/src/fileFormats/vtk/output/foamVtkOutput.C +++ b/src/fileFormats/vtk/output/foamVtkOutput.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -89,6 +89,22 @@ Foam::vtk::newFormatter } +void Foam::vtk::writeIdentity +( + vtk::formatter& fmt, + const label len, + label start +) +{ + // No nComponents for label, so use fmt.write() directly + for (label i=0; i < len; ++i) + { + fmt.write(start); + ++start; + } +} + + void Foam::vtk::writeList ( vtk::formatter& fmt, diff --git a/src/fileFormats/vtk/output/foamVtkOutput.H b/src/fileFormats/vtk/output/foamVtkOutput.H index 37cc0b55405..832fa0049ab 100644 --- a/src/fileFormats/vtk/output/foamVtkOutput.H +++ b/src/fileFormats/vtk/output/foamVtkOutput.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -84,48 +84,32 @@ namespace vtk ); + //- Write an identity list of labels. + // The output does not include the payload size. + void writeIdentity(vtk::formatter& fmt, const label len, label start=0); + //- Write a list of uint8_t values. // The output does not include the payload size. - void writeList - ( - vtk::formatter& fmt, - const UList<uint8_t>& values - ); + void writeList(vtk::formatter& fmt, const UList<uint8_t>& values); //- Write a list of uint8_t values. // The output does not include the payload size. - void writeListParallel - ( - vtk::formatter& fmt, - const UList<uint8_t>& values - ); + void writeListParallel(vtk::formatter& fmt, const UList<uint8_t>& values); - //- Write a value component-wise. + //- Component-wise write of a value (N times) template<class Type> - inline void write - ( - vtk::formatter& fmt, - const Type& val - ); + inline void write(vtk::formatter& fmt, const Type& val, const label n=1); //- Write a list of values. // The output does not include the payload size. template<class Type> - void writeList - ( - vtk::formatter& fmt, - const UList<Type>& values - ); + void writeList(vtk::formatter& fmt, const UList<Type>& values); //- Write a list of values. // The output does not include the payload size. template<class Type, unsigned N> - void writeList - ( - vtk::formatter& fmt, - const FixedList<Type, N>& values - ); + void writeList(vtk::formatter& fmt, const FixedList<Type, N>& values); //- Write a list of values via indirect addressing. @@ -327,22 +311,6 @@ namespace legacy // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -//- Template specialization for symmTensor ordering -template<> -inline void write(vtk::formatter& fmt, const symmTensor& val) -{ - // symmTensor ( XX, XY, XZ, YY, YZ, ZZ ) - // VTK order ( XX, YY, ZZ, XY, YZ, XZ ) -> (0, 3, 5, 1, 4, 2) - - fmt.write(component(val, 0)); // XX - fmt.write(component(val, 3)); // YY - fmt.write(component(val, 5)); // ZZ - fmt.write(component(val, 1)); // XY - fmt.write(component(val, 4)); // YZ - fmt.write(component(val, 2)); // XZ -} - - } // End namespace vtk } // End namespace Foam diff --git a/src/fileFormats/vtk/output/foamVtkOutputI.H b/src/fileFormats/vtk/output/foamVtkOutputI.H index 40b5275737a..735277a0904 100644 --- a/src/fileFormats/vtk/output/foamVtkOutputI.H +++ b/src/fileFormats/vtk/output/foamVtkOutputI.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -32,27 +32,54 @@ namespace vtk //- Template specialization for label template<> -inline void write<label>(vtk::formatter& fmt, const label& val) +inline void write<label>(vtk::formatter& fmt, const label& val, const label n) { - fmt.write(val); + for (label i=0; i < n; ++i) + { + fmt.write(val); + } } //- Template specialization for float template<> -inline void write<float>(vtk::formatter& fmt, const float& val) +inline void write<float>(vtk::formatter& fmt, const float& val, const label n) { - fmt.write(val); + for (label i=0; i < n; ++i) + { + fmt.write(val); + } } //- Template specialization for double template<> -inline void write<double>(vtk::formatter& fmt, const double& val) +inline void write<double>(vtk::formatter& fmt, const double& val, const label n) { - fmt.write(val); + for (label i=0; i < n; ++i) + { + fmt.write(val); + } } + +//- Template specialization for symmTensor ordering +// VTK order is (XX, YY, ZZ, XY, YZ, XZ) +template<> +inline void write(vtk::formatter& fmt, const symmTensor& val, const label n) +{ + for (label i=0; i < n; ++i) + { + fmt.write(component(val, symmTensor::XX)); + fmt.write(component(val, symmTensor::YY)); + fmt.write(component(val, symmTensor::ZZ)); + fmt.write(component(val, symmTensor::XY)); + fmt.write(component(val, symmTensor::YZ)); + fmt.write(component(val, symmTensor::XZ)); + } +} + + } // End namespace vtk } // End namespace Foam diff --git a/src/fileFormats/vtk/output/foamVtkOutputTemplates.C b/src/fileFormats/vtk/output/foamVtkOutputTemplates.C index 46781934e87..ecbeff2ac9c 100644 --- a/src/fileFormats/vtk/output/foamVtkOutputTemplates.C +++ b/src/fileFormats/vtk/output/foamVtkOutputTemplates.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -32,13 +32,18 @@ template<class Type> inline void Foam::vtk::write ( vtk::formatter& fmt, - const Type& val + const Type& val, + const label n ) { const direction nCmpt = pTraits<Type>::nComponents; - for (direction cmpt=0; cmpt < nCmpt; ++cmpt) + + for (label i=0; i < n; ++i) { - fmt.write(component(val, cmpt)); + for (direction cmpt=0; cmpt < nCmpt; ++cmpt) + { + fmt.write(component(val, cmpt)); + } } } diff --git a/src/functionObjects/lagrangian/vtkCloud/vtkCloud.C b/src/functionObjects/lagrangian/vtkCloud/vtkCloud.C index 6fdb41b00bb..9abd8f09269 100644 --- a/src/functionObjects/lagrangian/vtkCloud/vtkCloud.C +++ b/src/functionObjects/lagrangian/vtkCloud/vtkCloud.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -69,10 +69,8 @@ void Foam::functionObjects::vtkCloud::writeVerts format().beginDataArray<label>(vtk::dataArrayAttr::CONNECTIVITY); format().writeSize(payLoad); - for (label i=0; i < nTotParcels; ++i) - { - format().write(i); - } + vtk::writeIdentity(format(), nTotParcels); + format().flush(); format().endDataArray(); } @@ -85,10 +83,8 @@ void Foam::functionObjects::vtkCloud::writeVerts format().beginDataArray<label>(vtk::dataArrayAttr::OFFSETS); format().writeSize(payLoad); - for (label i=0; i < nTotParcels; ++i) - { - format().write(i+1); - } + vtk::writeIdentity(format(), nTotParcels, 1); + format().flush(); format().endDataArray(); } diff --git a/src/lagrangian/intermediate/conversion/vtk/foamVtkLagrangianWriter.C b/src/lagrangian/intermediate/conversion/vtk/foamVtkLagrangianWriter.C index 792e3757bd8..dd2b3b3685f 100644 --- a/src/lagrangian/intermediate/conversion/vtk/foamVtkLagrangianWriter.C +++ b/src/lagrangian/intermediate/conversion/vtk/foamVtkLagrangianWriter.C @@ -43,9 +43,9 @@ Foam::pointField Foam::vtk::lagrangianWriter::positions() const auto outIter = pts.begin(); - forAllConstIters(parcels, iter) + for (const auto& p : parcels) { - *outIter = iter().position(); + *outIter = p.position(); ++outIter; } @@ -74,10 +74,8 @@ void Foam::vtk::lagrangianWriter::writeVerts() format().beginDataArray<label>(vtk::dataArrayAttr::CONNECTIVITY); format().writeSize(payLoad); - for (label i=0; i < nVerts; ++i) - { - format().write(i); - } + vtk::writeIdentity(format(), nVerts); + format().flush(); format().endDataArray(); } @@ -90,10 +88,8 @@ void Foam::vtk::lagrangianWriter::writeVerts() format().beginDataArray<label>(vtk::dataArrayAttr::OFFSETS); format().writeSize(payLoad); - for (label i=0; i < nVerts; ++i) - { - format().write(i+1); - } + vtk::writeIdentity(format(), nVerts, 1); + format().flush(); format().endDataArray(); } diff --git a/src/lumpedPointMotion/lumpedPointMovementWriter.C b/src/lumpedPointMotion/lumpedPointMovementWriter.C index 3cc17656a68..0a3266dfbcc 100644 --- a/src/lumpedPointMotion/lumpedPointMovementWriter.C +++ b/src/lumpedPointMotion/lumpedPointMovementWriter.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2016-2017 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -100,10 +100,8 @@ void Foam::lumpedPointMovement::writeForcesAndMomentsVTP format().beginDataArray<label>(vtk::dataArrayAttr::CONNECTIVITY); format().writeSize(payLoad); - for (label i=0; i<nPoints; ++i) - { - format().write(i); - } + vtk::writeIdentity(format(), nPoints); + format().flush(); format().endDataArray(); @@ -119,10 +117,8 @@ void Foam::lumpedPointMovement::writeForcesAndMomentsVTP format().beginDataArray<label>(vtk::dataArrayAttr::OFFSETS); format().writeSize(payLoad); - for (label i=0; i<nPoints; ++i) - { - format().write(i+1); - } + vtk::writeIdentity(format(), nPoints, 1); + format().flush(); format().endDataArray(); @@ -284,10 +280,8 @@ void Foam::lumpedPointMovement::writeZonesVTP format().beginDataArray<label>("zoneId"); format().writeSize(payLoad); - forAll(pp, facei) - { - format().write(zoneI); - } + vtk::write(format(), zoneI, pp.size()); + format().flush(); format().endDataArray(); diff --git a/src/lumpedPointMotion/lumpedPointStateWriter.C b/src/lumpedPointMotion/lumpedPointStateWriter.C index 1af7503e150..30bce4a051b 100644 --- a/src/lumpedPointMotion/lumpedPointStateWriter.C +++ b/src/lumpedPointMotion/lumpedPointStateWriter.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2016-2017 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -25,6 +25,7 @@ License #include "lumpedPointState.H" #include "OFstream.H" +#include "sliceRange.H" #include "axesRotation.H" #include "coordinateSystem.H" #include "foamVtkOutput.H" @@ -106,10 +107,8 @@ void Foam::lumpedPointState::writeVTP format().beginDataArray<label>(vtk::dataArrayAttr::CONNECTIVITY); format().writeSize(payLoad); - forAll(points_, i) - { - format().write(i); - } + vtk::writeIdentity(format(), points_.size()); + format().flush(); format().endDataArray(); @@ -125,10 +124,8 @@ void Foam::lumpedPointState::writeVTP format().beginDataArray<label>(vtk::dataArrayAttr::OFFSETS); format().writeSize(payLoad); - forAll(points_, i) - { - format().write(i+1); - } + vtk::writeIdentity(format(), points_.size(), 1); + format().flush(); format().endDataArray(); @@ -150,10 +147,8 @@ void Foam::lumpedPointState::writeVTP format().beginDataArray<label>(vtk::dataArrayAttr::CONNECTIVITY); format().writeSize(payLoad); - forAll(points_, i) - { - format().write(i); - } + vtk::writeIdentity(format(), points_.size()); + format().flush(); format().endDataArray(); @@ -256,10 +251,8 @@ void Foam::lumpedPointState::writeVTP format().beginDataArray<label>(vtk::dataArrayAttr::CONNECTIVITY); format().writeSize(payLoad); - for (label i=0; i < 4*nPolys; ++i) - { - format().write(i); - } + vtk::writeIdentity(format(), 4*nPolys); + format().flush(); format().endDataArray(); @@ -275,9 +268,8 @@ void Foam::lumpedPointState::writeVTP format().beginDataArray<label>(vtk::dataArrayAttr::OFFSETS); format().writeSize(payLoad); - for (label i=0; i < nPolys; ++i) + for (const label off : sliceRange(4, nPolys, 4)) { - const label off = 4 * (i+1); format().write(off); } format().flush(); @@ -297,10 +289,8 @@ void Foam::lumpedPointState::writeVTP format().beginDataArray<label>("zoneId"); format().writeSize(payLoad); - for (label i=0; i < nPolys; ++i) - { - format().write(i); - } + vtk::writeIdentity(format(), nPolys); + format().flush(); format().endDataArray(); diff --git a/src/surfMesh/surfaceFormats/vtk/VTKsurfaceFormatCore.C b/src/surfMesh/surfaceFormats/vtk/VTKsurfaceFormatCore.C index 00556c8f0c4..e74a9a81b2e 100644 --- a/src/surfMesh/surfaceFormats/vtk/VTKsurfaceFormatCore.C +++ b/src/surfMesh/surfaceFormats/vtk/VTKsurfaceFormatCore.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2004-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -86,7 +86,7 @@ void Foam::fileFormats::VTKsurfaceFormatCore::writeCellData // Number of faces covered by the zones label nFaces = 0; - for (const auto& z : zones) + for (const surfZone& z : zones) { nFaces += z.size(); } @@ -95,12 +95,9 @@ void Foam::fileFormats::VTKsurfaceFormatCore::writeCellData vtk::legacy::intField<1>(format, "region", nFaces); // 1 component label zoneId = 0; - for (const surfZone& zone : zones) + for (const surfZone& z : zones) { - forAll(zone, i) - { - format.write(zoneId); - } + vtk::write(format, zoneId, z.size()); ++zoneId; } format.flush(); diff --git a/src/surfMesh/surfaceFormats/vtp/VTPsurfaceFormatCore.C b/src/surfMesh/surfaceFormats/vtp/VTPsurfaceFormatCore.C index 5d927c347c8..893744a0abc 100644 --- a/src/surfMesh/surfaceFormats/vtp/VTPsurfaceFormatCore.C +++ b/src/surfMesh/surfaceFormats/vtp/VTPsurfaceFormatCore.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -121,7 +121,7 @@ void Foam::fileFormats::VTPsurfaceFormatCore::writeCellData // Number of faces covered by the zones label nFaces = 0; - for (const auto& z : zones) + for (const surfZone& z : zones) { nFaces += z.size(); } @@ -133,12 +133,9 @@ void Foam::fileFormats::VTPsurfaceFormatCore::writeCellData format.writeSize(payLoad); label zoneId = 0; - for (const surfZone& zone : zones) + for (const surfZone& z : zones) { - forAll(zone, i) - { - format.write(zoneId); - } + vtk::write(format, zoneId, z.size()); ++zoneId; } -- GitLab