Commit 408f252d authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: regularize surfaceFormat writers (#1600)

- use local branching for face maps to reduce code complexity
  and duplication
parent 431c9632
......@@ -128,7 +128,7 @@ void Foam::MeshedSurfaceProxy<Face>::write
}
// write surfMesh/points
// Write surfMesh/points
{
pointIOField io
(
......@@ -154,7 +154,7 @@ void Foam::MeshedSurfaceProxy<Face>::write
}
// write surfMesh/faces
// Write surfMesh/faces
{
faceCompactIOList io
(
......@@ -187,7 +187,7 @@ void Foam::MeshedSurfaceProxy<Face>::write
}
// write surfMesh/surfZones
// Write surfMesh/surfZones
{
surfZoneIOList io
(
......@@ -244,7 +244,7 @@ inline Foam::label Foam::MeshedSurfaceProxy<Face>::nTriangles() const
}
label nTri = 0;
for (const Face& f : this->surfFaces())
for (const auto& f : faces_)
{
nTri += f.nTriangles();
}
......
......@@ -41,9 +41,6 @@ SourceFiles
#define MeshedSurfaceProxy_H
#include "pointField.H"
#include "labelledTri.H"
#include "HashSet.H"
#include "ListOps.H"
#include "surfZoneList.H"
#include "surfaceFormatsCore.H"
#include "runTimeSelectionTables.H"
......@@ -55,7 +52,6 @@ namespace Foam
{
// Forward Declarations
template<class Face> class MeshedSurface;
/*---------------------------------------------------------------------------*\
......@@ -156,19 +152,19 @@ public:
// Access
//- The surface size is the number of faces
inline label size() const
label size() const
{
return faces_.size();
}
//- Return const access to the points
inline const pointField& points() const
const pointField& points() const
{
return points_;
}
//- Return const access to the faces
inline const UList<Face>& surfFaces() const
const UList<Face>& surfFaces() const
{
return faces_;
}
......@@ -176,19 +172,19 @@ public:
//- Const access to the surface zones.
// If zones are defined, they must be contiguous and cover the
// entire surface
inline const UList<surfZone>& surfZones() const
const UList<surfZone>& surfZones() const
{
return zones_;
}
//- Const access to the faceMap, zero-sized when unused
inline const labelUList& faceMap() const
const labelUList& faceMap() const
{
return faceMap_;
}
//- Use faceMap?
inline bool useFaceMap() const
//- Can/should use faceMap?
bool useFaceMap() const
{
return faceMap_.size() == faces_.size();
}
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2019 OpenCFD Ltd.
Copyright (C) 2016-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -60,13 +60,19 @@ Foam::mergedSurf::mergedSurf
(
const pointField& unmergedPoints,
const faceList& unmergedFaces,
const labelList& originalIds,
const labelList& origZoneIds,
const scalar mergeDim
)
:
mergedSurf()
{
merge(unmergedPoints, unmergedFaces, originalIds, mergeDim);
merge
(
unmergedPoints,
unmergedFaces,
origZoneIds,
mergeDim
);
}
......@@ -82,8 +88,9 @@ void Foam::mergedSurf::clear()
{
points_.clear();
faces_.clear();
zones_.clear();
pointsMap_.clear();
zoneIds_.clear();
}
......@@ -111,7 +118,14 @@ bool Foam::mergedSurf::merge
const scalar mergeDim
)
{
return merge(unmergedPoints, unmergedFaces, labelList(), mergeDim);
return
merge
(
unmergedPoints,
unmergedFaces,
labelList(),
mergeDim
);
}
......@@ -119,7 +133,7 @@ bool Foam::mergedSurf::merge
(
const pointField& unmergedPoints,
const faceList& unmergedFaces,
const labelList& originalIds,
const labelList& origZoneIds,
const scalar mergeDim
)
{
......@@ -143,9 +157,9 @@ bool Foam::mergedSurf::merge
);
// Now handle zone/region information
// Now handle per-face information
globalIndex::gatherOp(originalIds, zones_);
globalIndex::gatherOp(origZoneIds, zoneIds_);
return true;
}
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2018 OpenCFD Ltd.
Copyright (C) 2016-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -56,31 +56,33 @@ class mergedSurf
{
pointField points_;
faceList faces_;
labelList zones_;
labelList pointsMap_;
labelList zoneIds_;
labelList faceIds_;
public:
// Constructors
//- Construct null
//- Default construct
mergedSurf() = default;
//- Copy construct null
//- Copy construct
mergedSurf(const mergedSurf&) = default;
//- Move construct
mergedSurf(mergedSurf&&) = default;
//- Construct and merge.
//- Construct and merge
mergedSurf
(
const meshedSurf& unmergedSurface,
const scalar mergeDim
);
//- Construct and merge.
//- Construct and merge
mergedSurf
(
const pointField& unmergedPoints,
......@@ -93,7 +95,7 @@ public:
(
const pointField& unmergedPoints,
const faceList& unmergedFaces,
const labelList& originalIds,
const labelList& origZoneIds,
const scalar mergeDim
);
......@@ -128,7 +130,7 @@ public:
//- Per-face zone/region information
virtual const labelList& zoneIds() const
{
return zones_;
return zoneIds_;
}
//- Map for reordered points (old-to-new)
......@@ -163,7 +165,7 @@ public:
(
const pointField& unmergedPoints,
const faceList& unmergedFaces,
const labelList& originalIds,
const labelList& origZoneIds,
const scalar mergeDim
);
......@@ -175,7 +177,6 @@ public:
//- Move assignment
mergedSurf& operator=(mergedSurf&&) = default;
};
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2019 OpenCFD Ltd.
Copyright (C) 2016-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -57,8 +57,8 @@ public:
// Constructors
//- Construct null
meshedSurf() = default;
//- Default construct
constexpr meshedSurf() noexcept = default;
//- Destructor
......@@ -86,7 +86,7 @@ public:
Class meshedSurf::emptySurface Declaration
\*---------------------------------------------------------------------------*/
//- A meshedSurf class with no faces, points or zoneId
//- A concrete meshedSurf class without faces, points, etc.
class meshedSurf::emptySurface
:
public meshedSurf
......@@ -95,8 +95,8 @@ public:
// Constructors
//- Construct null
emptySurface() = default;
//- Default construct
constexpr emptySurface() noexcept = default;
//- Destructor
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2019 OpenCFD Ltd.
Copyright (C) 2016-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -52,33 +52,33 @@ class meshedSurfRef
{
std::reference_wrapper<const pointField> points_;
std::reference_wrapper<const faceList> faces_;
std::reference_wrapper<const labelList> ids_;
std::reference_wrapper<const labelList> zoneIds_;
public:
// Constructors
//- Construct null
//- Default construct
meshedSurfRef()
:
points_(std::cref<pointField>(pointField::null())),
faces_(std::cref<faceList>(faceList::null())),
ids_(std::cref<labelList>(labelList::null()))
zoneIds_(std::cref<labelList>(labelList::null()))
{}
//- Construct from components
meshedSurfRef
(
const pointField& pts,
const faceList& fcs,
const labelList& ids = labelList::null()
const pointField& pointLst,
const faceList& faceLst,
const labelList& zoneIdLst = labelList::null()
)
:
points_(std::cref<pointField>(pts)),
faces_(std::cref<faceList>(fcs)),
ids_(std::cref<labelList>(ids))
points_(std::cref<pointField>(pointLst)),
faces_(std::cref<faceList>(faceLst)),
zoneIds_(std::cref<labelList>(zoneIdLst))
{}
......@@ -103,7 +103,7 @@ public:
//- Per-face zone/region information.
virtual const labelList& zoneIds() const
{
return ids_.get();
return zoneIds_.get();
}
//- Remove all references by redirecting to null objects
......@@ -111,20 +111,20 @@ public:
{
points_ = std::cref<pointField>(pointField::null());
faces_ = std::cref<faceList>(faceList::null());
ids_ = std::cref<labelList>(labelList::null());
zoneIds_ = std::cref<labelList>(labelList::null());
}
//- Reset components
void reset
(
const pointField& pts,
const faceList& fcs,
const labelList& ids = labelList::null()
const pointField& pointLst,
const faceList& faceLst,
const labelList& zoneIdLst = labelList::null()
)
{
points_ = std::cref<pointField>(pts);
faces_ = std::cref<faceList>(fcs);
ids_ = std::cref<labelList>(ids);
points_ = std::cref<pointField>(pointLst);
faces_ = std::cref<faceList>(faceLst);
zoneIds_ = std::cref<labelList>(zoneIdLst);
}
};
......
......@@ -51,13 +51,14 @@ bool Foam::fileFormats::AC3DsurfaceFormat<Face>::read
const fileName& filename
)
{
// Clear everything
this->clear();
IFstream is(filename);
if (!is.good())
{
FatalErrorInFunction
<< "Cannot read file " << filename
<< "Cannot read file " << filename << nl
<< exit(FatalError);
}
......@@ -84,7 +85,7 @@ bool Foam::fileFormats::AC3DsurfaceFormat<Face>::read
if (!cueTo(is, "OBJECT", args) || args != "world")
{
FatalErrorInFunction
<< "Cannot find \"OBJECT world\" in file " << filename
<< "Cannot find 'OBJECT world' in file " << filename << nl
<< exit(FatalError);
}
......@@ -119,8 +120,8 @@ bool Foam::fileFormats::AC3DsurfaceFormat<Face>::read
if (!readCmd(is, cmd, args))
{
FatalErrorInFunction
<< "Did not read up to \"kids 0\" while reading zone "
<< zoneI << " from file " << filename
<< "Did not read up to 'kids 0' while reading zone "
<< zoneI << " from file " << filename << nl
<< exit(FatalError);
}
......@@ -332,7 +333,7 @@ void Foam::fileFormats::AC3DsurfaceFormat<Face>::write
if (!os.good())
{
FatalErrorInFunction
<< "Cannot open file for writing " << filename
<< "Cannot write file " << filename << nl
<< exit(FatalError);
}
......@@ -354,7 +355,7 @@ void Foam::fileFormats::AC3DsurfaceFormat<Face>::write
{
if (useFaceMap)
{
SubList<label> zoneMap(surf.faceMap(), zone.size(), zone.start());
SubList<label> zoneMap(surf.faceMap(), zone.range());
PrimitivePatch<Face, UIndirectList, const pointField&> patch
(
UIndirectList<Face>(faceLst, zoneMap),
......@@ -367,7 +368,7 @@ void Foam::fileFormats::AC3DsurfaceFormat<Face>::write
{
PrimitivePatch<Face, UList, const pointField&> patch
(
SubList<Face>(faceLst, zone.size(), zone.start()),
SubList<Face>(faceLst, zone.range()),
pointLst
);
......@@ -395,7 +396,7 @@ void Foam::fileFormats::AC3DsurfaceFormat<Face>::write
if (!os.good())
{
FatalErrorInFunction
<< "Cannot open file for writing " << filename
<< "Cannot write file " << filename << nl
<< exit(FatalError);
}
......@@ -421,7 +422,7 @@ void Foam::fileFormats::AC3DsurfaceFormat<Face>::write
label zoneIndex = 0;
for (const surfZone& zone : zoneLst)
{
SubList<label> zoneMap(faceMap, zone.size(), zone.start());
SubList<label> zoneMap(faceMap, zone.range());
PrimitivePatch<Face, UIndirectList, const pointField&> patch
(
UIndirectList<Face>(surf.surfFaces(), zoneMap),
......
......@@ -183,31 +183,26 @@ void Foam::fileFormats::FLMAsurfaceFormat<Face>::write
// determine the number of faces by counting the
// tri/quads/triangulated) faces in each zone
label nFaces = 0;
List<label> zoneCount(zones.size());
labelList zoneCount(zones.size());
{
label faceIndex = 0;
forAll(zones, zoneI)
forAll(zones, zonei)
{
const surfZone& zone = zones[zoneI];
const surfZone& zone = zones[zonei];
label selCount = 0;
if (useFaceMap)
for (label nLocal = zone.size(); nLocal--; ++faceIndex)
{
forAll(zone, localFaceI)
{
selCount += countFaces(faceLst[faceMap[faceIndex++]]);
}
}
else
{
forAll(zone, localFaceI)
{
selCount += countFaces(faceLst[faceIndex++]);
}
const label facei =
(useFaceMap ? faceMap[faceIndex] : faceIndex);
const Face& f = faceLst[facei];
selCount += countFaces(f);
}
zoneCount[zoneI] = selCount;
zoneCount[zonei] = selCount;
nFaces += selCount;
}
}
......@@ -239,21 +234,14 @@ void Foam::fileFormats::FLMAsurfaceFormat<Face>::write
label faceIndex = 0;
for (const surfZone& zone : zones)
{
const label nLocalFaces = zone.size();
if (useFaceMap)
{
for (label i=0; i<nLocalFaces; ++i)
{
writeShell(os, faceLst[faceMap[faceIndex++]]);
}
}
else
for (label nLocal = zone.size(); nLocal--; ++faceIndex)
{
for (label i=0; i<nLocalFaces; ++i)
{
writeShell(os, faceLst[faceIndex++]);
}
const label facei =
(useFaceMap ? faceMap[faceIndex] : faceIndex);
const Face& f = faceLst[facei];
writeShell(os, f);
}
}
newline(os);
......@@ -269,21 +257,14 @@ void Foam::fileFormats::FLMAsurfaceFormat<Face>::write
label faceIndex = 0;
for (const surfZone& zone : zones)
{
const label nLocalFaces = zone.size();
if (useFaceMap)
for (label nLocal = zone.size(); nLocal--; ++faceIndex)
{
for (label i=0; i<nLocalFaces; ++i)
{
writeType(os, faceLst[faceMap[faceIndex++]]);
}
}
else
{
for (label i=0; i<nLocalFaces; ++i)
{
writeType(os, faceLst[faceIndex++]);
}
const label facei =
(useFaceMap ? faceMap[faceIndex] : faceIndex);
const Face& f = faceLst[facei];
writeType(os, f);
}
}
newline(os);
......@@ -296,10 +277,10 @@ void Foam::fileFormats::FLMAsurfaceFormat<Face>::write
newline(os);
label faceIndex = 0;
forAll(zones, zoneI)
forAll(zones, zonei)
{
const surfZone& zone = zones[zoneI];
const label selCount = zoneCount[zoneI];
const surfZone& zone = zones[zonei];
const label selCount = zoneCount[zonei];
putFireString(os, zone.name());
putFireLabel(os, static_cast<int>(FIRECore::cellSelection));
......@@ -345,7 +326,7 @@ void Foam::fileFormats::FLMAsurfaceFormat<Face>::write
else
{
FatalErrorInFunction
<< "Cannot open file for writing " << filename
<< "Cannot write file " << filename << nl
<< exit(FatalError);
}
}
......
......@@ -57,7 +57,7 @@ bool Foam::fileFormats::GTSsurfaceFormat<Face>::checkIfTriangulated
if (nNonTris)
{
FatalErrorInFunction
<< "Surface has " << nNonTris << "/" << faceLst.size()
<< "Surface has " << nNonTris << '/' << faceLst.size()
<< " non-triangulated faces - not writing!" << endl;
}
......@@ -86,13 +86,14 @@ bool Foam::fileFormats::GTSsurfaceFormat<Face>::read
const fileName& filename
)
{
// Clear everything
this->clear();
IFstream is(filename);
if (!is.good())
{
FatalErrorInFunction
<< "Cannot read file " << filename
<< "Cannot read file " << filename << nl
<< exit(FatalError);
}
......@@ -109,14 +110,14 @@ bool Foam::fileFormats::GTSsurfaceFormat<Face>::read
}
// write directly into the lists:
pointField& pointLst = this->storedPoints();