ENH: add VTP output support for surfMesh

- use proxy writer for triSurface writing
This commit is contained in:
Mark Olesen
2017-06-02 12:07:43 +02:00
parent a2e978d43e
commit 4cbbe3fd59
14 changed files with 1185 additions and 410 deletions

View File

@ -158,6 +158,9 @@ int main(int argc, char *argv[])
}
if
(
!args.optionFound("triSurface")
&&
(
!MeshedSurface<face>::canRead(importName, true)
||
@ -166,6 +169,7 @@ int main(int argc, char *argv[])
&& !MeshedSurface<face>::canWriteType(exportName.ext(), true)
)
)
)
{
return 1;
}

View File

@ -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

View File

@ -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;
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<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]);
}
}
else if (reader.cellData().foundObject<scalarIOField>("STLSolidLabeling"))
{
const scalarIOField& region =
reader.cellData().lookupObject<scalarIOField>
(
"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<scalarIOField>(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<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);
label faceIndex = 0;
forAll(zones, zoneI)
{
const surfZone& zone = zones[zoneI];
writeHeader(format(), pointLst);
if (useFaceMap)
{
forAll(zone, localFacei)
// connectivity count without additional storage (done internally)
label nConnectivity = 0;
for (const auto& f : faceLst)
{
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];
format().write(f.size()); // The size prefix
foamVtkOutput::writeList(format(), f);
}
os << ' ' << nl;
}
format().flush();
}
else
{
forAll(zone, localFacei)
{
const Face& f = faceLst[faceIndex++];
os << f.size();
forAll(f, fp)
{
os << ' ' << f[fp];
}
os << ' ' << nl;
}
}
// 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);
// 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());
}

View File

@ -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
@ -102,19 +107,19 @@ public:
//- 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

View File

@ -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<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;
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 << ' ';
format.write(zoneId);
}
else
{
os << nl;
}
}
os << zoneI + 1;
}
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();
}

View File

@ -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);
};

View File

@ -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 <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#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<class Face>
void Foam::fileFormats::VTPsurfaceFormat<Face>::writePolys
(
foamVtkOutput::formatter& format,
const UList<Face>& faces
)
{
format.tag(vtkFileTag::POLYS);
//
// 'connectivity'
//
{
uint64_t payLoad = 0;
for (const auto& f : faces)
{
payLoad += f.size();
}
format.openDataArray<label>("connectivity")
.closeTag();
format.writeSize(payLoad * sizeof(label));
for (const Face& f : faces)
{
foamVtkOutput::writeList(format, f);
}
format.flush();
format.endDataArray();
}
//
// 'offsets' (connectivity offsets)
//
{
const uint64_t payLoad(faces.size() * sizeof(label));
format
.openDataArray<label>("offsets")
.closeTag();
format.writeSize(payLoad);
label off = 0;
for (const auto& f : faces)
{
off += f.size();
format.write(off);
}
format.flush();
format.endDataArray();
}
format.endTag(vtkFileTag::POLYS);
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Face>
Foam::fileFormats::VTPsurfaceFormat<Face>::VTPsurfaceFormat()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Face>
void Foam::fileFormats::VTPsurfaceFormat<Face>::write
(
const fileName& filename,
const MeshedSurfaceProxy<Face>& surf
)
{
const pointField& pointLst = surf.points();
const List<Face>& faceLst = surf.surfFaces();
const List<label>& faceMap = surf.faceMap();
const List<surfZone>& zones =
(
surf.surfZones().empty()
? surfaceFormatsCore::oneZone(faceLst)
: surf.surfZones()
);
const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
std::ofstream os(filename.c_str(), std::ios::binary);
autoPtr<foamVtkOutput::formatter> format =
foamVtkOutput::newFormatter(os, fmtType);
writeHeader(format(), pointLst, faceLst.size());
if (useFaceMap)
{
format().tag(vtkFileTag::POLYS);
//
// 'connectivity'
//
{
uint64_t payLoad = 0;
for (const auto& f : faceLst)
{
payLoad += f.size();
}
format().openDataArray<label>("connectivity")
.closeTag();
format().writeSize(payLoad * sizeof(label));
label faceIndex = 0;
for (const surfZone& zone : zones)
{
forAll(zone, i)
{
const Face& f = faceLst[faceMap[faceIndex++]];
foamVtkOutput::writeList(format(), f);
}
}
format().flush();
format().endDataArray();
}
//
// 'offsets' (connectivity offsets)
//
{
const uint64_t payLoad(faceLst.size() * sizeof(label));
format()
.openDataArray<label>("offsets")
.closeTag();
format().writeSize(payLoad);
label off = 0, faceIndex = 0;
for (const surfZone& zone : zones)
{
forAll(zone, i)
{
const Face& f = faceLst[faceMap[faceIndex++]];
off += f.size();
format().write(off);
}
}
format().flush();
format().endDataArray();
}
format().endTag(vtkFileTag::POLYS);
}
else
{
// Easy to write polys without a faceMap
writePolys(format(), faceLst);
}
// Write regions (zones) as CellData
if (zones.size() > 1)
{
writeCellData(format(), zones);
}
writeFooter(format());
}
template<class Face>
void Foam::fileFormats::VTPsurfaceFormat<Face>::write
(
const fileName& filename,
const UnsortedMeshedSurface<Face>& surf
)
{
std::ofstream os(filename.c_str(), std::ios::binary);
autoPtr<foamVtkOutput::formatter> format =
foamVtkOutput::newFormatter(os, fmtType);
const List<Face>& faceLst = surf.surfFaces();
writeHeader(format(), surf.points(), faceLst.size());
// Easy to write polys without a faceMap
writePolys(format(), faceLst);
// Write regions (zones) as CellData
writeCellData(format(), surf.zoneIds());
writeFooter(format());
}
// ************************************************************************* //

View File

@ -0,0 +1,132 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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/>.
Class
Foam::fileFormats::VTPsurfaceFormat
Description
Provide a means of writing VTP (xml) format.
The output is never sorted by zone.
SourceFiles
VTPsurfaceFormat.C
\*---------------------------------------------------------------------------*/
#ifndef VTPsurfaceFormat_H
#define VTPsurfaceFormat_H
#include "MeshedSurface.H"
#include "MeshedSurfaceProxy.H"
#include "UnsortedMeshedSurface.H"
#include "VTPsurfaceFormatCore.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace fileFormats
{
/*---------------------------------------------------------------------------*\
Class VTPsurfaceFormat Declaration
\*---------------------------------------------------------------------------*/
template<class Face>
class VTPsurfaceFormat
:
public MeshedSurface<Face>,
public VTPsurfaceFormatCore
{
// Private Member Functions
//- Write polygons
static void writePolys
(
foamVtkOutput::formatter& format,
const UList<Face>& faces
);
//- Disallow default bitwise copy construct
VTPsurfaceFormat(const VTPsurfaceFormat<Face>&) = delete;
//- Disallow default bitwise assignment
void operator=(const VTPsurfaceFormat<Face>&) = delete;
public:
// Constructors
//- Construct null
VTPsurfaceFormat();
//- Destructor
virtual ~VTPsurfaceFormat()
{}
// Member Functions
// Write
//- Write surface mesh components by proxy
static void write
(
const fileName& filename,
const MeshedSurfaceProxy<Face>& surf
);
//- Write UnsortedMeshedSurface, the output remains unsorted
static void write
(
const fileName& filename,
const UnsortedMeshedSurface<Face>& surf
);
//- Write object file
virtual void write(const fileName& name) const
{
write(name, MeshedSurfaceProxy<Face>(*this));
}
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace fileFormats
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
#include "VTPsurfaceFormat.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -0,0 +1,149 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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 "VTPsurfaceFormatCore.H"
#include "clock.H"
#include "foamVtkOutput.H"
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
void Foam::fileFormats::VTPsurfaceFormatCore::writeHeader
(
foamVtkOutput::formatter& format,
const pointField& pts,
const label nFaces
)
{
// XML (inline)
format
.xmlHeader()
.xmlComment("surface written " + clock::dateTime())
.beginVTKFile(vtkFileTag::POLY_DATA, "0.1");
// <Piece>
format
.openTag(vtkFileTag::PIECE)
( "NumberOfPoints", pts.size() )
( "NumberOfPolys", nFaces )
.closeTag();
// Points
const uint64_t payLoad = (pts.size()*3* sizeof(float));
format.tag(vtkFileTag::POINTS)
.openDataArray<float, 3>(vtkFileTag::POINTS)
.closeTag();
format.writeSize(payLoad);
foamVtkOutput::writeList(format, pts);
format.flush();
format
.endDataArray()
.endTag(vtkFileTag::POINTS);
}
void Foam::fileFormats::VTPsurfaceFormatCore::writeFooter
(
foamVtkOutput::formatter& format
)
{
// Slight cheat. </Piece> too
format.endTag(Foam::vtkFileTag::PIECE);
format.endTag(vtkFileTag::POLY_DATA)
.endVTKFile();
}
void Foam::fileFormats::VTPsurfaceFormatCore::writeCellData
(
foamVtkOutput::formatter& format,
const UList<surfZone>& zones
)
{
// Zone ids as CellData
// Number of faces covered by the zones
uint64_t payLoad = 0;
for (const auto& z : zones)
{
payLoad += z.size();
}
format.tag(vtkFileTag::CELL_DATA);
format.openDataArray<label>("region")
.closeTag();
format.writeSize(payLoad * sizeof(label));
label zoneId = 0;
for (const surfZone& zone : zones)
{
forAll(zone, i)
{
format.write(zoneId);
}
++zoneId;
}
format.flush();
format.endDataArray();
format.endTag(vtkFileTag::CELL_DATA);
}
void Foam::fileFormats::VTPsurfaceFormatCore::writeCellData
(
foamVtkOutput::formatter& format,
const labelUList& zoneIds
)
{
// Zone ids as CellData
format.tag(vtkFileTag::CELL_DATA);
format.openDataArray<label>("region")
.closeTag();
const uint64_t payLoad(zoneIds.size() * sizeof(label));
format.writeSize(payLoad);
foamVtkOutput::writeList(format, zoneIds);
format.flush();
format.endDataArray();
format.endTag(vtkFileTag::CELL_DATA);
}
// ************************************************************************* //

View File

@ -0,0 +1,95 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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/>.
Class
Foam::fileFormats::VTPsurfaceFormatCore
Description
Internal class used by the VTPsurfaceFormat
SourceFiles
VTPsurfaceFormatCore.C
\*---------------------------------------------------------------------------*/
#ifndef VTPsurfaceFormatCore_H
#define VTPsurfaceFormatCore_H
#include "MeshedSurface.H"
#include "foamVtkFormatter.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace fileFormats
{
/*---------------------------------------------------------------------------*\
Class VTPsurfaceFormatCore Declaration
\*---------------------------------------------------------------------------*/
class VTPsurfaceFormatCore
{
protected:
// Protected Member Functions
//- Write file header information with points
static void writeHeader
(
foamVtkOutput::formatter& format,
const pointField& pts,
const label nFaces
);
//- Write file footer
static void writeFooter(foamVtkOutput::formatter& format);
//- 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
);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace fileFormats
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -0,0 +1,98 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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 "addToRunTimeSelectionTable.H"
#include "addToMemberFunctionSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace fileFormats
{
// write MeshedSurfaceProxy
addNamedTemplatedToMemberFunctionSelectionTable
(
MeshedSurfaceProxy,
VTPsurfaceFormat,
face,
write,
fileExtension,
vtp
);
addNamedTemplatedToMemberFunctionSelectionTable
(
MeshedSurfaceProxy,
VTPsurfaceFormat,
triFace,
write,
fileExtension,
vtp
);
addNamedTemplatedToMemberFunctionSelectionTable
(
MeshedSurfaceProxy,
VTPsurfaceFormat,
labelledTri,
write,
fileExtension,
vtp
);
// write UnsortedMeshedSurface
addNamedTemplatedToMemberFunctionSelectionTable
(
UnsortedMeshedSurface,
VTPsurfaceFormat,
face,
write,
fileExtension,
vtp
);
addNamedTemplatedToMemberFunctionSelectionTable
(
UnsortedMeshedSurface,
VTPsurfaceFormat,
triFace,
write,
fileExtension,
vtp
);
addNamedTemplatedToMemberFunctionSelectionTable
(
UnsortedMeshedSurface,
VTPsurfaceFormat,
labelledTri,
write,
fileExtension,
vtp
);
}
}
// ************************************************************************* //

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -24,159 +24,144 @@ License
\*---------------------------------------------------------------------------*/
#include "triSurface.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;
namespace Foam
{
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void triSurface::writeVTK(const bool writeSorted, Ostream& os) const
void Foam::triSurface::writeVTK
(
const bool writeSorted,
std::ostream& os
) const
{
// Write header
os << "# vtk DataFile Version 2.0" << nl
<< "triSurface" << nl
<< "ASCII" << nl
<< "DATASET POLYDATA"
<< nl;
autoPtr<foamVtkOutput::formatter> format =
foamVtkOutput::newFormatter(os, fmtType);
const pointField& ps = points();
// Header
foamVtkOutput::legacy::fileHeader
(
format(),
"triSurface",
vtkFileTag::POLY_DATA
);
os << "POINTS " << ps.size() << " float" << nl;
const pointField& pts = this->points();
const auto& faceLst = this->surfFaces();
// Write vertex coords
forAll(ps, pointi)
{
if (pointi > 0 && (pointi % 10) == 0)
{
os << nl;
}
else
{
os << ' ';
}
os << ps[pointi].x() << ' '
<< ps[pointi].y() << ' '
<< ps[pointi].z();
}
os << nl;
const label nFaces = faceLst.size();
os << "POLYGONS " << size() << ' ' << 4*size() << nl;
// Points
foamVtkOutput::legacy::beginPoints(os, pts.size());
foamVtkOutput::writeList(format(), pts);
format().flush();
// connectivity count (without additional storage)
// is simply 3 * nFaces
foamVtkOutput::legacy::beginPolys(os, nFaces, 3*nFaces);
labelList faceMap;
surfacePatchList patches(calcPatches(faceMap));
if (writeSorted)
const bool useFaceMap = (writeSorted && patches.size() > 1);
if (useFaceMap)
{
label faceIndex = 0;
forAll(patches, patchi)
for (const surfacePatch& patch : patches)
{
// Print all faces belonging to this patch
forAll(patch, i)
{
const Face& f = faceLst[faceMap[faceIndex++]];
for
format().write(3); // The size prefix
foamVtkOutput::writeList(format(), f);
}
}
format().flush();
// Write regions (zones) as CellData
if (patches.size() > 1)
{
foamVtkOutput::legacy::dataHeader
(
label patchFacei = 0;
patchFacei < patches[patchi].size();
patchFacei++
)
{
if (faceIndex > 0 && (faceIndex % 10) == 0)
{
os << nl;
}
else
{
os << ' ';
}
os,
vtkFileTag::CELL_DATA,
nFaces,
1 // Only one field
);
const label facei = faceMap[faceIndex++];
os << "3 "
<< operator[](facei)[0] << ' '
<< operator[](facei)[1] << ' '
<< operator[](facei)[2];
}
}
os << nl;
// Print region numbers
os << "CELL_DATA " << size() << nl;
os << "FIELD attributes 1" << nl;
os << "region 1 " << size() << " float" << nl;
foamVtkOutput::legacy::intField
(
os,
"region",
1, // nComponent
nFaces
);
faceIndex = 0;
forAll(patches, patchi)
for (const surfacePatch& patch : patches)
{
for
forAll(patch, i)
{
const Face& f = faceLst[faceMap[faceIndex++]];
format().write(f.region());
}
}
format().flush();
}
}
else
{
// No faceMap (unsorted)
for (const Face& f : faceLst)
{
format().write(3); // The size prefix
foamVtkOutput::writeList(format(), f);
}
format().flush();
// Write regions (zones) as CellData
foamVtkOutput::legacy::dataHeader
(
label patchFacei = 0;
patchFacei < patches[patchi].size();
patchFacei++
)
{
if (faceIndex > 0 && (faceIndex % 10) == 0)
{
os << nl;
}
else
{
os << ' ';
}
os,
vtkFileTag::CELL_DATA,
faceLst.size(),
1 // Only one field
);
const label facei = faceMap[faceIndex++];
foamVtkOutput::legacy::intField
(
os,
"region",
1, // nComponent
faceLst.size()
);
os << operator[](facei).region();
}
}
os << nl;
}
else
for (const Face& f : faceLst)
{
forAll(*this, facei)
{
if (facei > 0 && (facei % 10) == 0)
{
os << nl;
format().write(f.region());
}
else
{
os << ' ';
}
os << "3 "
<< operator[](facei)[0] << ' '
<< operator[](facei)[1] << ' '
<< operator[](facei)[2];
}
os << nl;
os << "CELL_DATA " << size() << nl;
os << "FIELD attributes 1" << nl;
os << "region 1 " << size() << " float" << nl;
forAll(*this, facei)
{
if (facei > 0 && (facei % 10) == 0)
{
os << nl;
}
else
{
os << ' ';
}
os << operator[](facei).region();
}
os << nl;
format().flush();
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* //

View File

@ -33,6 +33,7 @@ License
#include "PackedBoolList.H"
#include "surfZoneList.H"
#include "surfaceFormatsCore.H"
#include "MeshedSurfaceProxy.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -52,7 +53,9 @@ const Foam::wordHashSet Foam::triSurface::readTypes_
const Foam::wordHashSet Foam::triSurface::writeTypes_
{
"ftr", "stl", "stlb", "gts", "obj", "off", "tri", "ac", "smesh", "vtk"
"ftr", "stl", "stlb", "gts", "obj", "off", "tri", "ac", "smesh", "vtk",
// via proxy:
"inp", "vtp"
};
@ -477,8 +480,9 @@ bool Foam::triSurface::read
{
FatalErrorInFunction
<< "unknown file extension " << ext
<< ". Supported extensions are '.ftr', '.stl', '.stlb', '.gts'"
<< ", '.obj', '.ac', '.off', '.nas', '.tri' and '.vtk'"
<< " for reading file " << name
<< ". Supported extensions:" << nl
<< " " << flatOutput(readTypes_.sortedToc()) << nl
<< exit(FatalError);
return false;
@ -504,7 +508,7 @@ void Foam::triSurface::write
}
else if (ext == "stlb")
{
ofstream outFile(name.c_str(), std::ios::binary);
std::ofstream outFile(name.c_str(), std::ios::binary);
writeSTLBINARY(outFile);
}
@ -522,7 +526,27 @@ void Foam::triSurface::write
}
else if (ext == "vtk")
{
writeVTK(sort, OFstream(name)());
std::ofstream outFile(name.c_str(), std::ios::binary);
writeVTK(sort, outFile);
}
else if
(
ext == "inp" // STARCD
|| ext == "vtp" // VTK
)
{
labelList faceMap;
List<surfZone> zoneLst = this->sortedZones(faceMap);
MeshedSurfaceProxy<labelledTri> proxy
(
this->points(),
this->surfFaces(),
zoneLst,
faceMap
);
proxy.write(name, ext);
}
else if (ext == "tri")
{
@ -540,10 +564,9 @@ void Foam::triSurface::write
{
FatalErrorInFunction
<< "unknown file extension " << ext
<< " for file " << name
<< ". Supported extensions are '.ftr', '.stl', '.stlb', "
<< "'.gts', '.obj', '.vtk'"
<< ", '.off', '.smesh', '.ac' and '.tri'"
<< " for writing file " << name
<< ". Supported extensions:" << nl
<< " " << flatOutput(writeTypes_.sortedToc()) << nl
<< exit(FatalError);
}
}

View File

@ -167,7 +167,7 @@ class triSurface
void writeOFF(const bool writeSorted, Ostream&) const;
//- Write to VTK legacy format.
void writeVTK(const bool writeSorted, Ostream&) const;
void writeVTK(const bool writeSorted, std::ostream& os) const;
//- Write to Ostream in TRI (AC3D) format
// Ac3d .tri format (unmerged triangle format)
@ -330,6 +330,12 @@ public:
return patches_;
}
//- Return const access to the faces
inline const List<labelledTri>& surfFaces() const
{
return static_cast<const List<labelledTri>&>(*this);
}
//- Return edge-face addressing sorted (for edges with more than
// 2 faces) according to the angle around the edge.
// Orientation is anticlockwise looking from
@ -449,12 +455,8 @@ public:
//- Write to database
void write(const Time&) const;
//- Write to Ostream in OpenDX format
void writeDX(const scalarField&, Ostream&) const;
void writeDX(const vectorField&, Ostream&) const;
//- Write some statistics
void writeStats(Ostream&) const;
void writeStats(Ostream& os) const;
// Member operators