diff --git a/applications/utilities/surface/surfaceMeshConvertTesting/surfaceMeshConvertTesting.C b/applications/utilities/surface/surfaceMeshConvertTesting/surfaceMeshConvertTesting.C index 5f1c5e9a33..abdeec1c89 100644 --- a/applications/utilities/surface/surfaceMeshConvertTesting/surfaceMeshConvertTesting.C +++ b/applications/utilities/surface/surfaceMeshConvertTesting/surfaceMeshConvertTesting.C @@ -159,12 +159,16 @@ int main(int argc, char *argv[]) if ( - !MeshedSurface::canRead(importName, true) - || - ( - !optStdout - && !MeshedSurface::canWriteType(exportName.ext(), true) - ) + !args.optionFound("triSurface") + && + ( + !MeshedSurface::canRead(importName, true) + || + ( + !optStdout + && !MeshedSurface::canWriteType(exportName.ext(), true) + ) + ) ) { return 1; diff --git a/src/surfMesh/Make/files b/src/surfMesh/Make/files index 10d6171c20..2bf796be62 100644 --- a/src/surfMesh/Make/files +++ b/src/surfMesh/Make/files @@ -37,6 +37,8 @@ $(surfaceFormats)/tri/TRIsurfaceFormatCore.C $(surfaceFormats)/tri/TRIsurfaceFormatRunTime.C $(surfaceFormats)/vtk/VTKsurfaceFormatCore.C $(surfaceFormats)/vtk/VTKsurfaceFormatRunTime.C +$(surfaceFormats)/vtp/VTPsurfaceFormatCore.C +$(surfaceFormats)/vtp/VTPsurfaceFormatRunTime.C $(surfaceFormats)/x3d/X3DsurfaceFormatCore.C $(surfaceFormats)/x3d/X3DsurfaceFormatRunTime.C diff --git a/src/surfMesh/surfaceFormats/vtk/VTKsurfaceFormat.C b/src/surfMesh/surfaceFormats/vtk/VTKsurfaceFormat.C index b0a5d27114..98fed44463 100644 --- a/src/surfMesh/surfaceFormats/vtk/VTKsurfaceFormat.C +++ b/src/surfMesh/surfaceFormats/vtk/VTKsurfaceFormat.C @@ -28,26 +28,54 @@ License #include "scalarIOField.H" #include "faceTraits.H" #include "OFstream.H" +#include "foamVtkOutput.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +// File-scope constant. +// +// TODO: make this run-time selectable (ASCII | BINARY) +// - Legacy mode only + +static const Foam::foamVtkOutput::formatType fmtType = + Foam::foamVtkOutput::formatType::LEGACY_ASCII; + // Foam::foamVtkOutput::formatType::LEGACY_BASE64; + // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // template -void Foam::fileFormats::VTKsurfaceFormat::writeHeaderPolygons +void Foam::fileFormats::VTKsurfaceFormat::writePolys ( - Ostream& os, - const UList& faceLst + foamVtkOutput::formatter& format, + const UList& faces ) { - label nNodes = 0; - - forAll(faceLst, facei) + // connectivity count without additional storage (done internally) + label nConnectivity = 0; + for (const auto& f : faces) { - nNodes += faceLst[facei].size(); + nConnectivity += f.size(); } - os << nl - << "POLYGONS " << faceLst.size() << ' ' - << faceLst.size() + nNodes << nl; + foamVtkOutput::legacy::beginPolys + ( + format.os(), + faces.size(), + nConnectivity + ); + + + // legacy: size + connectivity together + // [nPts, id1, id2, ..., nPts, id1, id2, ...] + + for (const Face& f : faces) + { + format.write(f.size()); // The size prefix + foamVtkOutput::writeList(format, f); + } + + format.flush(); } @@ -115,31 +143,37 @@ bool Foam::fileFormats::VTKsurfaceFormat::read // Assume all faces in zone0 unless a region field is present labelList zones(faces.size(), 0); - if (reader.cellData().foundObject("region")) + + for (auto fieldName : { "region", "STLSolidLabeling" }) { - const scalarIOField& region = - reader.cellData().lookupObject - ( - "region" - ); - forAll(region, i) + const labelIOField* lptr = + reader.cellData().lookupObjectPtr(fieldName); + + if (lptr) { - zones[i] = label(region[i]); - } - } - else if (reader.cellData().foundObject("STLSolidLabeling")) - { - const scalarIOField& region = - reader.cellData().lookupObject - ( - "STLSolidLabeling" - ); - forAll(region, i) - { - zones[i] = label(region[i]); + label i = 0; + for (const auto& region : *lptr) + { + zones[i++] = label(region); + } + break; + } + + const scalarIOField* sptr = + reader.cellData().lookupObjectPtr(fieldName); + + if (sptr) + { + label i = 0; + for (const auto& region : *sptr) + { + zones[i++] = label(region); + } + break; } } + // Create zone names const label nZones = max(zones)+1; wordList zoneNames(nZones); @@ -240,54 +274,54 @@ void Foam::fileFormats::VTKsurfaceFormat::write const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1); - OFstream os(filename); - if (!os.good()) + std::ofstream os(filename.c_str()); + + autoPtr format = + foamVtkOutput::newFormatter(os, fmtType); + + writeHeader(format(), pointLst); + + if (useFaceMap) { - FatalErrorInFunction - << "Cannot open file for writing " << filename - << exit(FatalError); - } - - - writeHeader(os, pointLst); - writeHeaderPolygons(os, faceLst); - - label faceIndex = 0; - forAll(zones, zoneI) - { - const surfZone& zone = zones[zoneI]; - - if (useFaceMap) + // connectivity count without additional storage (done internally) + label nConnectivity = 0; + for (const auto& f : faceLst) { - forAll(zone, localFacei) + nConnectivity += f.size(); + } + + foamVtkOutput::legacy::beginPolys + ( + format().os(), + faceLst.size(), + nConnectivity + ); + + label faceIndex = 0; + for (const surfZone& zone : zones) + { + forAll(zone, i) { const Face& f = faceLst[faceMap[faceIndex++]]; - os << f.size(); - forAll(f, fp) - { - os << ' ' << f[fp]; - } - os << ' ' << nl; + format().write(f.size()); // The size prefix + foamVtkOutput::writeList(format(), f); } } - else - { - forAll(zone, localFacei) - { - const Face& f = faceLst[faceIndex++]; - os << f.size(); - forAll(f, fp) - { - os << ' ' << f[fp]; - } - os << ' ' << nl; - } - } + format().flush(); + } + else + { + // Easy to write polys without a faceMap + writePolys(format(), faceLst); } - writeTail(os, zones); + // Write regions (zones) as CellData + if (zones.size() > 1) + { + writeCellData(format(), zones); + } } @@ -298,33 +332,18 @@ void Foam::fileFormats::VTKsurfaceFormat::write const UnsortedMeshedSurface& surf ) { - OFstream os(filename); - if (!os.good()) - { - FatalErrorInFunction - << "Cannot open file for writing " << filename - << exit(FatalError); - } + std::ofstream os(filename.c_str()); + autoPtr format = + foamVtkOutput::newFormatter(os, fmtType); - const List& faceLst = surf.surfFaces(); + writeHeader(format(), surf.points()); - writeHeader(os, surf.points()); - writeHeaderPolygons(os, faceLst); + // Easy to write polys without a faceMap + writePolys(format(), surf.surfFaces()); - forAll(faceLst, facei) - { - const Face& f = faceLst[facei]; - - os << f.size(); - forAll(f, fp) - { - os << ' ' << f[fp]; - } - os << ' ' << nl; - } - - writeTail(os, surf.zoneIds()); + // Write regions (zones) as CellData + writeCellData(format(), surf.zoneIds()); } diff --git a/src/surfMesh/surfaceFormats/vtk/VTKsurfaceFormat.H b/src/surfMesh/surfaceFormats/vtk/VTKsurfaceFormat.H index 437eb24921..cd0fb232a3 100644 --- a/src/surfMesh/surfaceFormats/vtk/VTKsurfaceFormat.H +++ b/src/surfMesh/surfaceFormats/vtk/VTKsurfaceFormat.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -60,8 +60,13 @@ class VTKsurfaceFormat { // Private Member Functions - //- Write header information about number of polygon points - static void writeHeaderPolygons(Ostream&, const UList&); + //- Write polygons + static void writePolys + ( + foamVtkOutput::formatter& format, + const UList& faces + ); + //- Disallow default bitwise copy construct VTKsurfaceFormat(const VTKsurfaceFormat&) = delete; @@ -75,7 +80,7 @@ public: // Constructors //- Construct from file name - VTKsurfaceFormat(const fileName&); + VTKsurfaceFormat(const fileName& filename); // Selectors @@ -97,24 +102,24 @@ public: // Member Functions - // Write + // Write //- Write surface mesh components by proxy static void write ( - const fileName&, - const MeshedSurfaceProxy& + const fileName& filename, + const MeshedSurfaceProxy& surf ); //- Write UnsortedMeshedSurface, the output remains unsorted static void write ( - const fileName&, - const UnsortedMeshedSurface& + const fileName& fileName, + const UnsortedMeshedSurface& surf ); //- Read from file - virtual bool read(const fileName&); + virtual bool read(const fileName& filename); //- Write object file virtual void write(const fileName& name) const diff --git a/src/surfMesh/surfaceFormats/vtk/VTKsurfaceFormatCore.C b/src/surfMesh/surfaceFormats/vtk/VTKsurfaceFormatCore.C index 9a28648d0c..c251e1f621 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) 2011-2016 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2017 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -25,102 +25,103 @@ License #include "VTKsurfaceFormatCore.H" #include "clock.H" +#include "foamVtkOutput.H" // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // void Foam::fileFormats::VTKsurfaceFormatCore::writeHeader ( - Ostream& os, - const pointField& pointLst + foamVtkOutput::formatter& format, + const pointField& pts ) { - // Write header - os << "# vtk DataFile Version 2.0" << nl - << "surface written " << clock::dateTime().c_str() << nl - << "ASCII" << nl - << nl - << "DATASET POLYDATA" << nl; + foamVtkOutput::legacy::fileHeader + ( + format, + ("surface written " + clock::dateTime()), + vtkFileTag::POLY_DATA + ); - // Write vertex coords - os << "POINTS " << pointLst.size() << " float" << nl; - forAll(pointLst, ptI) - { - const point& pt = pointLst[ptI]; + foamVtkOutput::legacy::beginPoints(format.os(), pts.size()); - os << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl; - } + foamVtkOutput::writeList(format, pts); + format.flush(); } -void Foam::fileFormats::VTKsurfaceFormatCore::writeTail +void Foam::fileFormats::VTKsurfaceFormatCore::writeCellData ( - Ostream& os, - const UList& zoneLst + foamVtkOutput::formatter& format, + const UList& zones ) { + // Zone ids as CellData + + // Number of faces covered by the zones label nFaces = 0; - forAll(zoneLst, zoneI) + for (const auto& z : zones) { - nFaces += zoneLst[zoneI].size(); + nFaces += z.size(); } - // Print zone numbers - os << nl - << "CELL_DATA " << nFaces << nl - << "FIELD attributes 1" << nl - << "region 1 " << nFaces << " float" << nl; + foamVtkOutput::legacy::dataHeader + ( + format.os(), + vtkFileTag::CELL_DATA, + nFaces, + 1 // Only one field + ); + foamVtkOutput::legacy::intField + ( + format.os(), + "region", + 1, // nComponent + nFaces + ); - forAll(zoneLst, zoneI) + label zoneId = 0; + for (const surfZone& zone : zones) { - forAll(zoneLst[zoneI], localFacei) + forAll(zone, i) { - if (localFacei) - { - if (localFacei % 20) - { - os << ' '; - } - else - { - os << nl; - } - } - os << zoneI + 1; + format.write(zoneId); } - os << nl; + ++zoneId; } + format.flush(); } -void Foam::fileFormats::VTKsurfaceFormatCore::writeTail +void Foam::fileFormats::VTKsurfaceFormatCore::writeCellData ( - Ostream& os, + foamVtkOutput::formatter& format, const labelUList& zoneIds ) { - // Print zone numbers - os << nl - << "CELL_DATA " << zoneIds.size() << nl - << "FIELD attributes 1" << nl - << "region 1 " << zoneIds.size() << " float" << nl; + // Zone ids as CellData - forAll(zoneIds, facei) - { - if (facei) - { - if (facei % 20) - { - os << ' '; - } - else - { - os << nl; - } - } - os << zoneIds[facei] + 1; - } - os << nl; + // Number of faces + const label nFaces = zoneIds.size(); + + foamVtkOutput::legacy::dataHeader + ( + format.os(), + vtkFileTag::CELL_DATA, + nFaces, + 1 // Only one field + ); + + foamVtkOutput::legacy::intField + ( + format.os(), + "region", + 1, // nComponent + nFaces + ); + + foamVtkOutput::writeList(format, zoneIds); + format.flush(); } diff --git a/src/surfMesh/surfaceFormats/vtk/VTKsurfaceFormatCore.H b/src/surfMesh/surfaceFormats/vtk/VTKsurfaceFormatCore.H index 946440ddb0..3fdde4da2b 100644 --- a/src/surfMesh/surfaceFormats/vtk/VTKsurfaceFormatCore.H +++ b/src/surfMesh/surfaceFormats/vtk/VTKsurfaceFormatCore.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -36,7 +36,7 @@ SourceFiles #define VTKsurfaceFormatCore_H #include "MeshedSurface.H" -#include "Ostream.H" +#include "foamVtkFormatter.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -58,15 +58,24 @@ protected: //- Write header information with points static void writeHeader ( - Ostream&, - const pointField& + foamVtkOutput::formatter& format, + const pointField& pts ); - //- Write trailing information with zone information - static void writeTail(Ostream&, const UList&); + //- Write regions (zones) information as CellData + static void writeCellData + ( + foamVtkOutput::formatter& format, + const UList& zones + ); + + //- Write regions (zones) information as CellData + static void writeCellData + ( + foamVtkOutput::formatter& format, + const labelUList& zoneIds + ); - //- Write trailing information with zone Ids - static void writeTail(Ostream&, const labelUList& zoneIds); }; diff --git a/src/surfMesh/surfaceFormats/vtp/VTPsurfaceFormat.C b/src/surfMesh/surfaceFormats/vtp/VTPsurfaceFormat.C new file mode 100644 index 0000000000..8a84d1b459 --- /dev/null +++ b/src/surfMesh/surfaceFormats/vtp/VTPsurfaceFormat.C @@ -0,0 +1,251 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2017 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "VTPsurfaceFormat.H" +#include "OFstream.H" +#include "foamVtkOutput.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +// File-scope constant. +// +// TODO: make this run-time selectable +// - No append mode supported +// - Legacy mode is dispatched via 'VTKsurfaceFormat' instead + +static const Foam::foamVtkOutput::formatType fmtType = + Foam::foamVtkOutput::formatType::INLINE_ASCII; + // Foam::foamVtkOutput::formatType::INLINE_BASE64; + + +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +template +void Foam::fileFormats::VTPsurfaceFormat::writePolys +( + foamVtkOutput::formatter& format, + const UList& faces +) +{ + format.tag(vtkFileTag::POLYS); + + // + // 'connectivity' + // + { + uint64_t payLoad = 0; + for (const auto& f : faces) + { + payLoad += f.size(); + } + + format.openDataArray