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

View File

@ -37,6 +37,8 @@ $(surfaceFormats)/tri/TRIsurfaceFormatCore.C
$(surfaceFormats)/tri/TRIsurfaceFormatRunTime.C $(surfaceFormats)/tri/TRIsurfaceFormatRunTime.C
$(surfaceFormats)/vtk/VTKsurfaceFormatCore.C $(surfaceFormats)/vtk/VTKsurfaceFormatCore.C
$(surfaceFormats)/vtk/VTKsurfaceFormatRunTime.C $(surfaceFormats)/vtk/VTKsurfaceFormatRunTime.C
$(surfaceFormats)/vtp/VTPsurfaceFormatCore.C
$(surfaceFormats)/vtp/VTPsurfaceFormatRunTime.C
$(surfaceFormats)/x3d/X3DsurfaceFormatCore.C $(surfaceFormats)/x3d/X3DsurfaceFormatCore.C
$(surfaceFormats)/x3d/X3DsurfaceFormatRunTime.C $(surfaceFormats)/x3d/X3DsurfaceFormatRunTime.C

View File

@ -28,26 +28,54 @@ License
#include "scalarIOField.H" #include "scalarIOField.H"
#include "faceTraits.H" #include "faceTraits.H"
#include "OFstream.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 * * * * * * * * * * * // // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
template<class Face> template<class Face>
void Foam::fileFormats::VTKsurfaceFormat<Face>::writeHeaderPolygons void Foam::fileFormats::VTKsurfaceFormat<Face>::writePolys
( (
Ostream& os, foamVtkOutput::formatter& format,
const UList<Face>& faceLst const UList<Face>& faces
) )
{ {
label nNodes = 0; // connectivity count without additional storage (done internally)
label nConnectivity = 0;
forAll(faceLst, facei) for (const auto& f : faces)
{ {
nNodes += faceLst[facei].size(); nConnectivity += f.size();
} }
os << nl foamVtkOutput::legacy::beginPolys
<< "POLYGONS " << faceLst.size() << ' ' (
<< faceLst.size() + nNodes << nl; 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 // Assume all faces in zone0 unless a region field is present
labelList zones(faces.size(), 0); labelList zones(faces.size(), 0);
if (reader.cellData().foundObject<scalarIOField>("region"))
for (auto fieldName : { "region", "STLSolidLabeling" })
{ {
const scalarIOField& region = const labelIOField* lptr =
reader.cellData().lookupObject<scalarIOField> reader.cellData().lookupObjectPtr<labelIOField>(fieldName);
(
"region" if (lptr)
);
forAll(region, i)
{ {
zones[i] = label(region[i]); label i = 0;
} for (const auto& region : *lptr)
} {
else if (reader.cellData().foundObject<scalarIOField>("STLSolidLabeling")) zones[i++] = label(region);
{ }
const scalarIOField& region = break;
reader.cellData().lookupObject<scalarIOField> }
(
"STLSolidLabeling" const scalarIOField* sptr =
); reader.cellData().lookupObjectPtr<scalarIOField>(fieldName);
forAll(region, i)
{ if (sptr)
zones[i] = label(region[i]); {
label i = 0;
for (const auto& region : *sptr)
{
zones[i++] = label(region);
}
break;
} }
} }
// Create zone names // Create zone names
const label nZones = max(zones)+1; const label nZones = max(zones)+1;
wordList zoneNames(nZones); wordList zoneNames(nZones);
@ -240,54 +274,54 @@ void Foam::fileFormats::VTKsurfaceFormat<Face>::write
const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1); const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
OFstream os(filename); std::ofstream os(filename.c_str());
if (!os.good())
{
FatalErrorInFunction
<< "Cannot open file for writing " << filename
<< exit(FatalError);
}
autoPtr<foamVtkOutput::formatter> format =
foamVtkOutput::newFormatter(os, fmtType);
writeHeader(os, pointLst); writeHeader(format(), pointLst);
writeHeaderPolygons(os, faceLst);
label faceIndex = 0;
forAll(zones, zoneI)
{
const surfZone& zone = zones[zoneI];
if (useFaceMap) 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++]]; const Face& f = faceLst[faceMap[faceIndex++]];
os << f.size(); format().write(f.size()); // The size prefix
forAll(f, fp) foamVtkOutput::writeList(format(), f);
{
os << ' ' << f[fp];
} }
os << ' ' << nl;
} }
format().flush();
} }
else else
{ {
forAll(zone, localFacei) // Easy to write polys without a faceMap
{ writePolys(format(), faceLst);
const Face& f = faceLst[faceIndex++];
os << f.size();
forAll(f, fp)
{
os << ' ' << f[fp];
}
os << ' ' << nl;
}
}
} }
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 const UnsortedMeshedSurface<Face>& surf
) )
{ {
OFstream os(filename); std::ofstream os(filename.c_str());
if (!os.good())
{
FatalErrorInFunction
<< "Cannot open file for writing " << filename
<< exit(FatalError);
}
autoPtr<foamVtkOutput::formatter> format =
foamVtkOutput::newFormatter(os, fmtType);
const List<Face>& faceLst = surf.surfFaces(); writeHeader(format(), surf.points());
writeHeader(os, surf.points()); // Easy to write polys without a faceMap
writeHeaderPolygons(os, faceLst); writePolys(format(), surf.surfFaces());
forAll(faceLst, facei) // Write regions (zones) as CellData
{ writeCellData(format(), surf.zoneIds());
const Face& f = faceLst[facei];
os << f.size();
forAll(f, fp)
{
os << ' ' << f[fp];
}
os << ' ' << nl;
}
writeTail(os, surf.zoneIds());
} }

View File

@ -3,7 +3,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) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -60,8 +60,13 @@ class VTKsurfaceFormat
{ {
// Private Member Functions // Private Member Functions
//- Write header information about number of polygon points //- Write polygons
static void writeHeaderPolygons(Ostream&, const UList<Face>&); static void writePolys
(
foamVtkOutput::formatter& format,
const UList<Face>& faces
);
//- Disallow default bitwise copy construct //- Disallow default bitwise copy construct
VTKsurfaceFormat(const VTKsurfaceFormat<Face>&) = delete; VTKsurfaceFormat(const VTKsurfaceFormat<Face>&) = delete;
@ -75,7 +80,7 @@ public:
// Constructors // Constructors
//- Construct from file name //- Construct from file name
VTKsurfaceFormat(const fileName&); VTKsurfaceFormat(const fileName& filename);
// Selectors // Selectors
@ -102,19 +107,19 @@ public:
//- Write surface mesh components by proxy //- Write surface mesh components by proxy
static void write static void write
( (
const fileName&, const fileName& filename,
const MeshedSurfaceProxy<Face>& const MeshedSurfaceProxy<Face>& surf
); );
//- Write UnsortedMeshedSurface, the output remains unsorted //- Write UnsortedMeshedSurface, the output remains unsorted
static void write static void write
( (
const fileName&, const fileName& fileName,
const UnsortedMeshedSurface<Face>& const UnsortedMeshedSurface<Face>& surf
); );
//- Read from file //- Read from file
virtual bool read(const fileName&); virtual bool read(const fileName& filename);
//- Write object file //- Write object file
virtual void write(const fileName& name) const virtual void write(const fileName& name) const

View File

@ -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) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -25,102 +25,103 @@ License
#include "VTKsurfaceFormatCore.H" #include "VTKsurfaceFormatCore.H"
#include "clock.H" #include "clock.H"
#include "foamVtkOutput.H"
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
void Foam::fileFormats::VTKsurfaceFormatCore::writeHeader void Foam::fileFormats::VTKsurfaceFormatCore::writeHeader
( (
Ostream& os, foamVtkOutput::formatter& format,
const pointField& pointLst const pointField& pts
) )
{ {
// Write header foamVtkOutput::legacy::fileHeader
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];
os << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
}
}
void Foam::fileFormats::VTKsurfaceFormatCore::writeTail
( (
Ostream& os, format,
const UList<surfZone>& zoneLst ("surface written " + clock::dateTime()),
vtkFileTag::POLY_DATA
);
foamVtkOutput::legacy::beginPoints(format.os(), pts.size());
foamVtkOutput::writeList(format, pts);
format.flush();
}
void Foam::fileFormats::VTKsurfaceFormatCore::writeCellData
(
foamVtkOutput::formatter& format,
const UList<surfZone>& zones
) )
{ {
// Zone ids as CellData
// Number of faces covered by the zones
label nFaces = 0; label nFaces = 0;
forAll(zoneLst, zoneI) for (const auto& z : zones)
{ {
nFaces += zoneLst[zoneI].size(); nFaces += z.size();
} }
// Print zone numbers foamVtkOutput::legacy::dataHeader
os << nl
<< "CELL_DATA " << nFaces << nl
<< "FIELD attributes 1" << nl
<< "region 1 " << nFaces << " float" << nl;
forAll(zoneLst, zoneI)
{
forAll(zoneLst[zoneI], localFacei)
{
if (localFacei)
{
if (localFacei % 20)
{
os << ' ';
}
else
{
os << nl;
}
}
os << zoneI + 1;
}
os << nl;
}
}
void Foam::fileFormats::VTKsurfaceFormatCore::writeTail
( (
Ostream& os, 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(zone, i)
{
format.write(zoneId);
}
++zoneId;
}
format.flush();
}
void Foam::fileFormats::VTKsurfaceFormatCore::writeCellData
(
foamVtkOutput::formatter& format,
const labelUList& zoneIds const labelUList& zoneIds
) )
{ {
// Print zone numbers // Zone ids as CellData
os << nl
<< "CELL_DATA " << zoneIds.size() << nl
<< "FIELD attributes 1" << nl
<< "region 1 " << zoneIds.size() << " float" << nl;
forAll(zoneIds, facei) // Number of faces
{ const label nFaces = zoneIds.size();
if (facei)
{ foamVtkOutput::legacy::dataHeader
if (facei % 20) (
{ format.os(),
os << ' '; vtkFileTag::CELL_DATA,
} nFaces,
else 1 // Only one field
{ );
os << nl;
} foamVtkOutput::legacy::intField
} (
os << zoneIds[facei] + 1; format.os(),
} "region",
os << nl; 1, // nComponent
nFaces
);
foamVtkOutput::writeList(format, zoneIds);
format.flush();
} }

View File

@ -3,7 +3,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) 2011 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -36,7 +36,7 @@ SourceFiles
#define VTKsurfaceFormatCore_H #define VTKsurfaceFormatCore_H
#include "MeshedSurface.H" #include "MeshedSurface.H"
#include "Ostream.H" #include "foamVtkFormatter.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -58,15 +58,24 @@ protected:
//- Write header information with points //- Write header information with points
static void writeHeader static void writeHeader
( (
Ostream&, foamVtkOutput::formatter& format,
const pointField& const pointField& pts
); );
//- Write trailing information with zone information //- Write regions (zones) information as CellData
static void writeTail(Ostream&, const UList<surfZone>&); 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 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -24,159 +24,144 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "triSurface.H" #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 * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void triSurface::writeVTK(const bool writeSorted, Ostream& os) const void Foam::triSurface::writeVTK
(
const bool writeSorted,
std::ostream& os
) const
{ {
// Write header autoPtr<foamVtkOutput::formatter> format =
os << "# vtk DataFile Version 2.0" << nl foamVtkOutput::newFormatter(os, fmtType);
<< "triSurface" << nl
<< "ASCII" << nl
<< "DATASET POLYDATA"
<< nl;
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 const label nFaces = faceLst.size();
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;
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; labelList faceMap;
surfacePatchList patches(calcPatches(faceMap)); surfacePatchList patches(calcPatches(faceMap));
if (writeSorted) const bool useFaceMap = (writeSorted && patches.size() > 1);
if (useFaceMap)
{ {
label faceIndex = 0; label faceIndex = 0;
for (const surfacePatch& patch : patches)
forAll(patches, patchi)
{ {
// 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; os,
patchFacei < patches[patchi].size(); vtkFileTag::CELL_DATA,
patchFacei++ nFaces,
) 1 // Only one field
{ );
if (faceIndex > 0 && (faceIndex % 10) == 0)
{
os << nl;
}
else
{
os << ' ';
}
const label facei = faceMap[faceIndex++]; foamVtkOutput::legacy::intField
(
os << "3 " os,
<< operator[](facei)[0] << ' ' "region",
<< operator[](facei)[1] << ' ' 1, // nComponent
<< operator[](facei)[2]; nFaces
} );
}
os << nl;
// Print region numbers
os << "CELL_DATA " << size() << nl;
os << "FIELD attributes 1" << nl;
os << "region 1 " << size() << " float" << nl;
faceIndex = 0; faceIndex = 0;
for (const surfacePatch& patch : patches)
forAll(patches, patchi)
{ {
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; os,
patchFacei < patches[patchi].size(); vtkFileTag::CELL_DATA,
patchFacei++ faceLst.size(),
) 1 // Only one field
{ );
if (faceIndex > 0 && (faceIndex % 10) == 0)
{
os << nl;
}
else
{
os << ' ';
}
const label facei = faceMap[faceIndex++]; foamVtkOutput::legacy::intField
(
os,
"region",
1, // nComponent
faceLst.size()
);
os << operator[](facei).region(); for (const Face& f : faceLst)
}
}
os << nl;
}
else
{ {
forAll(*this, facei) format().write(f.region());
{
if (facei > 0 && (facei % 10) == 0)
{
os << nl;
} }
else format().flush();
{
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;
} }
} }
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* // // ************************************************************************* //

View File

@ -33,6 +33,7 @@ License
#include "PackedBoolList.H" #include "PackedBoolList.H"
#include "surfZoneList.H" #include "surfZoneList.H"
#include "surfaceFormatsCore.H" #include "surfaceFormatsCore.H"
#include "MeshedSurfaceProxy.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -52,7 +53,9 @@ const Foam::wordHashSet Foam::triSurface::readTypes_
const Foam::wordHashSet Foam::triSurface::writeTypes_ 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 FatalErrorInFunction
<< "unknown file extension " << ext << "unknown file extension " << ext
<< ". Supported extensions are '.ftr', '.stl', '.stlb', '.gts'" << " for reading file " << name
<< ", '.obj', '.ac', '.off', '.nas', '.tri' and '.vtk'" << ". Supported extensions:" << nl
<< " " << flatOutput(readTypes_.sortedToc()) << nl
<< exit(FatalError); << exit(FatalError);
return false; return false;
@ -504,7 +508,7 @@ void Foam::triSurface::write
} }
else if (ext == "stlb") else if (ext == "stlb")
{ {
ofstream outFile(name.c_str(), std::ios::binary); std::ofstream outFile(name.c_str(), std::ios::binary);
writeSTLBINARY(outFile); writeSTLBINARY(outFile);
} }
@ -522,7 +526,27 @@ void Foam::triSurface::write
} }
else if (ext == "vtk") 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") else if (ext == "tri")
{ {
@ -540,10 +564,9 @@ void Foam::triSurface::write
{ {
FatalErrorInFunction FatalErrorInFunction
<< "unknown file extension " << ext << "unknown file extension " << ext
<< " for file " << name << " for writing file " << name
<< ". Supported extensions are '.ftr', '.stl', '.stlb', " << ". Supported extensions:" << nl
<< "'.gts', '.obj', '.vtk'" << " " << flatOutput(writeTypes_.sortedToc()) << nl
<< ", '.off', '.smesh', '.ac' and '.tri'"
<< exit(FatalError); << exit(FatalError);
} }
} }

View File

@ -167,7 +167,7 @@ class triSurface
void writeOFF(const bool writeSorted, Ostream&) const; void writeOFF(const bool writeSorted, Ostream&) const;
//- Write to VTK legacy format. //- 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 //- Write to Ostream in TRI (AC3D) format
// Ac3d .tri format (unmerged triangle format) // Ac3d .tri format (unmerged triangle format)
@ -330,6 +330,12 @@ public:
return patches_; 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 //- Return edge-face addressing sorted (for edges with more than
// 2 faces) according to the angle around the edge. // 2 faces) according to the angle around the edge.
// Orientation is anticlockwise looking from // Orientation is anticlockwise looking from
@ -449,12 +455,8 @@ public:
//- Write to database //- Write to database
void write(const Time&) const; 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 //- Write some statistics
void writeStats(Ostream&) const; void writeStats(Ostream& os) const;
// Member operators // Member operators