Commit 4cbbe3fd authored by Mark OLESEN's avatar Mark OLESEN
Browse files

ENH: add VTP output support for surfMesh

- use proxy writer for triSurface writing
parent a2e978d4
......@@ -159,12 +159,16 @@ int main(int argc, char *argv[])
if
(
!MeshedSurface<face>::canRead(importName, true)
||
(
!optStdout
&& !MeshedSurface<face>::canWriteType(exportName.ext(), true)
)
!args.optionFound("triSurface")
&&
(
!MeshedSurface<face>::canRead(importName, true)
||
(
!optStdout
&& !MeshedSurface<face>::canWriteType(exportName.ext(), true)
)
)
)
{
return 1;
......
......@@ -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
......
......@@ -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<class Face>
void Foam::fileFormats::VTKsurfaceFormat<Face>::writeHeaderPolygons
void Foam::fileFormats::VTKsurfaceFormat<Face>::writePolys
(
Ostream& os,
const UList<Face>& faceLst
foamVtkOutput::formatter& format,
const UList<Face>& faces
)
{
label nNodes = 0;
// connectivity count without additional storage (done internally)
label nConnectivity = 0;
for (const auto& f : faces)
{
nConnectivity += f.size();
}
forAll(faceLst, facei)
foamVtkOutput::legacy::beginPolys
(
format.os(),
faces.size(),
nConnectivity
);
// legacy: size + connectivity together
// [nPts, id1, id2, ..., nPts, id1, id2, ...]
for (const Face& f : faces)
{
nNodes += faceLst[facei].size();
format.write(f.size()); // The size prefix
foamVtkOutput::writeList(format, f);
}
os << nl
<< "POLYGONS " << faceLst.size() << ' '
<< faceLst.size() + nNodes << nl;
format.flush();
}
......@@ -115,31 +143,37 @@ bool Foam::fileFormats::VTKsurfaceFormat<Face>::read
// Assume all faces in zone0 unless a region field is present
labelList zones(faces.size(), 0);
if (reader.cellData().foundObject<scalarIOField>("region"))
for (auto fieldName : { "region", "STLSolidLabeling" })
{
const scalarIOField& region =
reader.cellData().lookupObject<scalarIOField>
(
"region"
);
forAll(region, i)
const labelIOField* lptr =
reader.cellData().lookupObjectPtr<labelIOField>(fieldName);
if (lptr)
{
zones[i] = label(region[i]);
label i = 0;
for (const auto& region : *lptr)
{
zones[i++] = label(region);
}
break;
}
}
else if (reader.cellData().foundObject<scalarIOField>("STLSolidLabeling"))
{
const scalarIOField& region =
reader.cellData().lookupObject<scalarIOField>
(
"STLSolidLabeling"
);
forAll(region, i)
const scalarIOField* sptr =
reader.cellData().lookupObjectPtr<scalarIOField>(fieldName);
if (sptr)
{
zones[i] = label(region[i]);
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<Face>::write
const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
OFstream os(filename);
if (!os.good())
{
FatalErrorInFunction
<< "Cannot open file for writing " << filename
<< exit(FatalError);
}
std::ofstream os(filename.c_str());
autoPtr<foamVtkOutput::formatter> format =
foamVtkOutput::newFormatter(os, fmtType);
writeHeader(os, pointLst);
writeHeaderPolygons(os, faceLst);
writeHeader(format(), pointLst);
label faceIndex = 0;
forAll(zones, zoneI)
if (useFaceMap)
{
const surfZone& zone = zones[zoneI];
// connectivity count without additional storage (done internally)
label nConnectivity = 0;
for (const auto& f : faceLst)
{
nConnectivity += f.size();
}
if (useFaceMap)
foamVtkOutput::legacy::beginPolys
(
format().os(),
faceLst.size(),
nConnectivity
);
label faceIndex = 0;
for (const surfZone& zone : zones)
{
forAll(zone, localFacei)
forAll(zone, i)
{
const Face& f = faceLst[faceMap[faceIndex++]];
os << f.size();
forAll(f, fp)
{
os << ' ' << f[fp];
}
os << ' ' << nl;
}
}
else
{
forAll(zone, localFacei)
{
const Face& f = faceLst[faceIndex++];
os << f.size();
forAll(f, fp)
{
os << ' ' << f[fp];
}
os << ' ' << nl;
format().write(f.size()); // The size prefix
foamVtkOutput::writeList(format(), f);
}
}
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<Face>::write
const UnsortedMeshedSurface<Face>& surf
)
{
OFstream os(filename);
if (!os.good())
{
FatalErrorInFunction
<< "Cannot open file for writing " << filename
<< exit(FatalError);
}
std::ofstream os(filename.c_str());
autoPtr<foamVtkOutput::formatter> format =
foamVtkOutput::newFormatter(os, fmtType);
const List<Face>& faceLst = surf.surfFaces();
writeHeader(format(), surf.points());
writeHeader(os, surf.points());
writeHeaderPolygons(os, faceLst);
forAll(faceLst, facei)
{
const Face& f = faceLst[facei];
os << f.size();
forAll(f, fp)
{
os << ' ' << f[fp];
}
os << ' ' << nl;
}
// Easy to write polys without a faceMap
writePolys(format(), surf.surfFaces());
writeTail(os, surf.zoneIds());
// Write regions (zones) as CellData
writeCellData(format(), surf.zoneIds());
}
......
......@@ -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<Face>&);
//- Write polygons
static void writePolys
(
foamVtkOutput::formatter& format,
const UList<Face>& faces
);
//- Disallow default bitwise copy construct
VTKsurfaceFormat(const VTKsurfaceFormat<Face>&) = 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<Face>&
const fileName& filename,
const MeshedSurfaceProxy<Face>& surf
);
//- Write UnsortedMeshedSurface, the output remains unsorted
static void write
(
const fileName&,
const UnsortedMeshedSurface<Face>&
const fileName& fileName,
const UnsortedMeshedSurface<Face>& 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
......
......@@ -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;
// Write vertex coords
os << "POINTS " << pointLst.size() << " float" << nl;
forAll(pointLst, ptI)
{
const point& pt = pointLst[ptI];
foamVtkOutput::legacy::fileHeader
(
format,
("surface written " + clock::dateTime()),
vtkFileTag::POLY_DATA
);
os << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
}
foamVtkOutput::legacy::beginPoints(format.os(), pts.size());
foamVtkOutput::writeList(format, pts);
format.flush();
}
void Foam::fileFormats::VTKsurfaceFormatCore::writeTail
void Foam::fileFormats::VTKsurfaceFormatCore::writeCellData
(
Ostream& os,
const UList<surfZone>& zoneLst
foamVtkOutput::formatter& format,
const UList<surfZone>& 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;
forAll(zoneLst, zoneI)
foamVtkOutput::legacy::dataHeader
(
format.os(),
vtkFileTag::CELL_DATA,
nFaces,
1 // Only one field
);
foamVtkOutput::legacy::intField
(
format.os(),
"region",
1, // nComponent
nFaces
);
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;
forAll(zoneIds, facei)
{
if (facei)
{
if (facei % 20)
{
os << ' ';
}
else
{
os << nl;
}
}
os << zoneIds[facei] + 1;
}
os << nl;
// Zone ids as CellData
// 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();
}
......
......@@ -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<surfZone>&);
//- Write regions (zones) information as CellData
static void writeCellData
(
foamVtkOutput::formatter& format,
const UList<surfZone>& 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);
};
......
/*---------------------------------------------------------------------------*\
========= |
\\ / 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 <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "VTPsurfaceFormat.H"
#include "OFstream.H"
#include "foamVtkOutput.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //