Commit 4380349f authored by Mark Olesen's avatar Mark Olesen Committed by Andrew Heather
Browse files

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.
parent 10a03ceb
...@@ -711,12 +711,7 @@ bool Foam::vtk::internalWriter::writeProcIDs() ...@@ -711,12 +711,7 @@ bool Foam::vtk::internalWriter::writeProcIDs()
// Per-processor ids // Per-processor ids
for (label proci=0; proci < Pstream::nProcs(); ++proci) for (label proci=0; proci < Pstream::nProcs(); ++proci)
{ {
label len = procMaps.localSize(proci); vtk::write(format(), proci, procMaps.localSize(proci));
while (len--)
{
format().write(proci);
}
} }
format().flush(); format().flush();
......
...@@ -587,13 +587,7 @@ void Foam::vtk::patchWriter::writePatchIDs() ...@@ -587,13 +587,7 @@ void Foam::vtk::patchWriter::writePatchIDs()
{ {
for (const label patchId : patchIDs_) for (const label patchId : patchIDs_)
{ {
label count = patches[patchId].size(); vtk::write(format(), patchId, patches[patchId].size());
const label val = patchId;
while (count--)
{
format().write(val);
}
} }
} }
...@@ -615,16 +609,13 @@ void Foam::vtk::patchWriter::writePatchIDs() ...@@ -615,16 +609,13 @@ void Foam::vtk::patchWriter::writePatchIDs()
fromSlave >> recv; 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]; const label len = recv[i];
++i; const label val = recv[i+1];
const label val = recv[i];
while (count--) vtk::write(format(), val, len);
{
format().write(val);
}
} }
} }
} }
...@@ -722,12 +713,7 @@ bool Foam::vtk::patchWriter::writeProcIDs() ...@@ -722,12 +713,7 @@ bool Foam::vtk::patchWriter::writeProcIDs()
// Per-processor ids // Per-processor ids
for (label proci=0; proci < Pstream::nProcs(); ++proci) for (label proci=0; proci < Pstream::nProcs(); ++proci)
{ {
label len = procSizes.localSize(proci); vtk::write(format(), proci, procSizes.localSize(proci));
while (len--)
{
format().write(proci);
}
} }
good = true; good = true;
...@@ -735,14 +721,7 @@ bool Foam::vtk::patchWriter::writeProcIDs() ...@@ -735,14 +721,7 @@ bool Foam::vtk::patchWriter::writeProcIDs()
} }
else else
{ {
const label proci = Pstream::myProcNo(); vtk::write(format(), Pstream::myProcNo(), nLocalFaces_);
label len = nLocalFaces_;
while (len--)
{
format().write(proci);
}
good = true; good = true;
} }
...@@ -811,16 +790,11 @@ bool Foam::vtk::patchWriter::writeNeighIDs() ...@@ -811,16 +790,11 @@ bool Foam::vtk::patchWriter::writeNeighIDs()
{ {
for (const label patchId : patchIDs_) for (const label patchId : patchIDs_)
{ {
label count = patches[patchId].size();
const auto* pp = isA<processorPolyPatch>(patches[patchId]); const auto* pp = isA<processorPolyPatch>(patches[patchId]);
const label val = (pp ? pp->neighbProcNo() : -1); const label val = (pp ? pp->neighbProcNo() : -1);
while (count--) vtk::write(format(), val, patches[patchId].size());
{
format().write(val);
}
} }
good = true; good = true;
...@@ -844,16 +818,13 @@ bool Foam::vtk::patchWriter::writeNeighIDs() ...@@ -844,16 +818,13 @@ bool Foam::vtk::patchWriter::writeNeighIDs()
fromSlave >> recv; 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]; const label len = recv[i];
++i; const label val = recv[i+1];
const label val = recv[i];
while (count--) vtk::write(format(), val, len);
{
format().write(val);
}
} }
} }
} }
......
...@@ -81,10 +81,7 @@ void Foam::vtk::fileWriter::writeUniform ...@@ -81,10 +81,7 @@ void Foam::vtk::fileWriter::writeUniform
if (format_) if (format_)
{ {
for (label i=0; i < nValues; ++i) vtk::write(format(), val, nValues);
{
vtk::write(format(), val);
}
} }
if (format_) if (format_)
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
...@@ -89,6 +89,22 @@ Foam::vtk::newFormatter ...@@ -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 void Foam::vtk::writeList
( (
vtk::formatter& fmt, vtk::formatter& fmt,
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
...@@ -84,48 +84,32 @@ namespace vtk ...@@ -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. //- Write a list of uint8_t values.
// The output does not include the payload size. // The output does not include the payload size.
void writeList void writeList(vtk::formatter& fmt, const UList<uint8_t>& values);
(
vtk::formatter& fmt,
const UList<uint8_t>& values
);
//- Write a list of uint8_t values. //- Write a list of uint8_t values.
// The output does not include the payload size. // The output does not include the payload size.
void writeListParallel void writeListParallel(vtk::formatter& fmt, const UList<uint8_t>& values);
(
vtk::formatter& fmt,
const UList<uint8_t>& values
);
//- Write a value component-wise. //- Component-wise write of a value (N times)
template<class Type> template<class Type>
inline void write inline void write(vtk::formatter& fmt, const Type& val, const label n=1);
(
vtk::formatter& fmt,
const Type& val
);
//- Write a list of values. //- Write a list of values.
// The output does not include the payload size. // The output does not include the payload size.
template<class Type> template<class Type>
void writeList void writeList(vtk::formatter& fmt, const UList<Type>& values);
(
vtk::formatter& fmt,
const UList<Type>& values
);
//- Write a list of values. //- Write a list of values.
// The output does not include the payload size. // The output does not include the payload size.
template<class Type, unsigned N> template<class Type, unsigned N>
void writeList void writeList(vtk::formatter& fmt, const FixedList<Type, N>& values);
(
vtk::formatter& fmt,
const FixedList<Type, N>& values
);
//- Write a list of values via indirect addressing. //- Write a list of values via indirect addressing.
...@@ -327,22 +311,6 @@ namespace legacy ...@@ -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 vtk
} // End namespace Foam } // End namespace Foam
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
...@@ -32,27 +32,54 @@ namespace vtk ...@@ -32,27 +32,54 @@ namespace vtk
//- Template specialization for label //- Template specialization for label
template<> 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 specialization for float
template<> 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 specialization for double
template<> 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 vtk
} // End namespace Foam } // End namespace Foam
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
...@@ -32,13 +32,18 @@ template<class Type> ...@@ -32,13 +32,18 @@ template<class Type>
inline void Foam::vtk::write inline void Foam::vtk::write
( (
vtk::formatter& fmt, vtk::formatter& fmt,
const Type& val const Type& val,
const label n
) )
{ {
const direction nCmpt = pTraits<Type>::nComponents; 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));
}
} }
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
...@@ -69,10 +69,8 @@ void Foam::functionObjects::vtkCloud::writeVerts ...@@ -69,10 +69,8 @@ void Foam::functionObjects::vtkCloud::writeVerts
format().beginDataArray<label>(vtk::dataArrayAttr::CONNECTIVITY); format().beginDataArray<label>(vtk::dataArrayAttr::CONNECTIVITY);
format().writeSize(payLoad); format().writeSize(payLoad);
for (label i=0; i < nTotParcels; ++i) vtk::writeIdentity(format(), nTotParcels);
{
format().write(i);
}
format().flush(); format().flush();
format().endDataArray(); format().endDataArray();
} }
...@@ -85,10 +83,8 @@ void Foam::functionObjects::vtkCloud::writeVerts ...@@ -85,10 +83,8 @@ void Foam::functionObjects::vtkCloud::writeVerts
format().beginDataArray<label>(vtk::dataArrayAttr::OFFSETS); format().beginDataArray<label>(vtk::dataArrayAttr::OFFSETS);
format().writeSize(payLoad); format().writeSize(payLoad);
for (label i=0; i < nTotParcels; ++i) vtk::writeIdentity(format(), nTotParcels, 1);
{
format().write(i+1);
}
format().flush(); format().flush();
format().endDataArray(); format().endDataArray();
} }
......
...@@ -43,9 +43,9 @@ Foam::pointField Foam::vtk::lagrangianWriter::positions() const ...@@ -43,9 +43,9 @@ Foam::pointField Foam::vtk::lagrangianWriter::positions() const
auto outIter = pts.begin(); auto outIter = pts.begin();
forAllConstIters(parcels, iter) for (const auto& p : parcels)
{ {
*outIter = iter().position(); *outIter = p.position();
++outIter; ++outIter;
} }
...@@ -74,10 +74,8 @@ void Foam::vtk::lagrangianWriter::writeVerts() ...@@ -74,10 +74,8 @@ void Foam::vtk::lagrangianWriter::writeVerts()
format().beginDataArray<label>(vtk::dataArrayAttr::CONNECTIVITY); format().beginDataArray<label>(vtk::dataArrayAttr::CONNECTIVITY);
format().writeSize(payLoad); format().writeSize(payLoad);
for (label i=0; i < nVerts; ++i) vtk::writeIdentity(format(), nVerts);
{
format().write(i);
}
format().flush(); format().flush();
format().endDataArray(); format().endDataArray();
} }
...@@ -90,10 +88,8 @@ void Foam::vtk::lagrangianWriter::writeVerts() ...@@ -90,10 +88,8 @@ void Foam::vtk::lagrangianWriter::writeVerts()
format().beginDataArray<label>(vtk::dataArrayAttr::OFFSETS); format().beginDataArray<label>(vtk::dataArrayAttr::OFFSETS);
format().writeSize(payLoad); format().writeSize(payLoad);
for (label i=0; i < nVerts; ++i) vtk::writeIdentity(format(), nVerts, 1);
{
format().write(i+1);
}
format().flush(); format().flush();
format().endDataArray(); format().endDataArray();
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2016-2017 OpenCFD Ltd. \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
...@@ -100,10 +100,8 @@ void Foam::lumpedPointMovement::writeForcesAndMomentsVTP ...@@ -100,10 +100,8 @@ void Foam::lumpedPointMovement::writeForcesAndMomentsVTP
format().beginDataArray<label>(vtk::dataArrayAttr::CONNECTIVITY); format().beginDataArray<label>(vtk::dataArrayAttr::CONNECTIVITY);
format().writeSize(payLoad); format().writeSize(payLoad);
for (label i=0; i<nPoints; ++i) vtk::writeIdentity(format(), nPoints);
{
format().write(i);
}
format().flush(); format().flush();
format().endDataArray(); format().endDataArray();
...@@ -119,10 +117,8 @@ void Foam::lumpedPointMovement::writeForcesAndMomentsVTP ...@@ -119,10 +117,8 @@ void Foam::lumpedPointMovement::writeForcesAndMomentsVTP
format().beginDataArray<label>(vtk::dataArrayAttr::OFFSETS); format().beginDataArray<label>(vtk::dataArrayAttr::OFFSETS);
format().writeSize(payLoad); format().writeSize(payLoad);
for (label i=0; i<nPoints; ++i) vtk::writeIdentity(format(), nPoints, 1);
{