ENH: consolidate surfaceFormats for reading/writing triSurface (issue #294)

- eliminates previous code duplication and improves maintainability
This commit is contained in:
Mark Olesen
2017-11-20 14:55:36 +01:00
parent 5947f9a337
commit 3ef8906a66
111 changed files with 2646 additions and 4610 deletions

View File

@ -40,12 +40,6 @@ Foam::fileFormats::FIRECore::file3dExtensions
};
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::fileFormats::FIRECore::FIRECore()
{}
// * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * * //
Foam::label Foam::fileFormats::FIRECore::readPoints

View File

@ -108,7 +108,7 @@ protected:
// Protected Member Functions
//- Construct null
FIRECore();
FIRECore() = default;
//- Read points.

View File

@ -2,8 +2,8 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -24,11 +24,27 @@ License
\*---------------------------------------------------------------------------*/
#include "NASCore.H"
#include "IOmanip.H"
#include "Ostream.H"
#include "parsing.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const Foam::Enum
<
Foam::fileFormats::NASCore::fieldFormat
>
Foam::fileFormats::NASCore::fieldFormatNames
{
{ fieldFormat::SHORT, "short" },
{ fieldFormat::LONG, "long" },
{ fieldFormat::FREE, "free" },
};
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
Foam::scalar Foam::fileFormats::NASCore::readNasScalar(const string& str)
Foam::scalar Foam::fileFormats::NASCore::readNasScalar(const std::string& str)
{
const auto signPos = str.find_last_of("+-");
@ -54,7 +70,7 @@ Foam::scalar Foam::fileFormats::NASCore::readNasScalar(const string& str)
if
(
readScalar(str.substr(0, signPos), value) // Mantissa
&& readInt(str.substr(signPos), exponent) // Exponent (with sign)
&& readInt(str.substr(signPos), exponent) // Exponent (with sign)
)
{
// Note: this does not catch underflow/overflow
@ -74,10 +90,98 @@ Foam::scalar Foam::fileFormats::NASCore::readNasScalar(const string& str)
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
std::string Foam::fileFormats::NASCore::nextNasField
(
const std::string& str,
std::string::size_type& pos,
std::string::size_type len
)
{
const auto beg = pos;
const auto end = str.find(',', pos);
Foam::fileFormats::NASCore::NASCore()
{}
if (end == std::string::npos)
{
pos = beg + len; // Continue after field width
}
else
{
len = (end - beg); // Efffective width
pos = end + 1; // Continue after comma
}
return str.substr(beg, len);
}
void Foam::fileFormats::NASCore::setPrecision
(
Ostream& os,
const fieldFormat format
)
{
os.setf(ios_base::scientific);
// Capitalise the E marker
os.setf(ios_base::uppercase);
const label offset = 7;
label prec = 16 - offset;
switch (format)
{
case fieldFormat::SHORT :
{
prec = 8 - offset;
break;
}
case fieldFormat::LONG :
case fieldFormat::FREE :
{
prec = 16 - offset;
break;
}
}
os.precision(prec);
}
Foam::Ostream& Foam::fileFormats::NASCore::writeKeyword
(
Ostream& os,
const word& keyword,
const fieldFormat format
)
{
os.setf(ios_base::left);
switch (format)
{
case fieldFormat::SHORT :
{
os << setw(8) << keyword;
break;
}
case fieldFormat::LONG :
{
os << setw(8) << word(keyword + '*');
break;
}
case fieldFormat::FREE :
{
os << keyword;
break;
}
}
os.unsetf(ios_base::left);
return os;
}
// ************************************************************************* //

View File

@ -2,8 +2,8 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -37,11 +37,16 @@ SourceFiles
#include "scalar.H"
#include "string.H"
#include "Enum.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// Forward declarations
class Ostream;
namespace fileFormats
{
@ -53,23 +58,64 @@ class NASCore
{
public:
// Public Member Functions
//- Extract numbers from things like "-2.358-8" (same as "-2.358e-8")
static scalar readNasScalar(const string& str);
//- Extract numbers from things like "-2.358-8" (same as "-2.358e-8")
// \deprecated use readNasScalar instead (deprecated Sep 2017)
inline static scalar parseNASCoord(const string& str)
//- File field formats
enum fieldFormat
{
return readNasScalar(str);
}
SHORT, //<! Short format (field width = 8)
LONG, //<! Long format (field width = 16)
FREE //<! Free format (comma-separated fields)
};
//- Selection names for the NASTRAN file field formats
static const Enum<fieldFormat> fieldFormatNames;
// Constructors
//- Construct null
NASCore();
NASCore() = default;
// Public Static Member Functions
//- Extract numbers from things like "-2.358-8" (same as "-2.358e-8")
static scalar readNasScalar(const std::string& str);
//- Extract numbers from things like "-2.358-8" (same as "-2.358e-8")
// \deprecated use readNasScalar instead (deprecated Sep 2017)
inline static scalar parseNASCoord(const std::string& str)
{
return readNasScalar(str);
}
//- A string::substr() to handle fixed-format and free-format NASTRAN.
// Returns the substr to the next comma (if found) or the given length
//
// \param str The string to extract from
// \param pos On input, the position of the first character of the
// substring. On output, advances to the next position to use.
// \param len The fixed-format length to use if a comma is not found.
static std::string nextNasField
(
const std::string& str,
std::string::size_type& pos,
std::string::size_type len
);
//- Set output stream precision and format flags
static void setPrecision(Ostream& os, const fieldFormat format);
//- Write initial keyword (eg, 'GRID' or 'GRID*') followed by the
//- requisite number of spaces for the field-width
static Ostream& writeKeyword
(
Ostream& os,
const word& keyword,
const fieldFormat format
);
};

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -27,6 +27,7 @@ License
#include "ListOps.H"
#include "clock.H"
#include "PackedBoolList.H"
#include "DynamicList.H"
#include "StringStream.H"
#include "OSspecific.H"
@ -85,12 +86,6 @@ Foam::fileFormats::STARCDCore::starToFoamFaceAddr =
};
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::fileFormats::STARCDCore::STARCDCore()
{}
// * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * * //
bool Foam::fileFormats::STARCDCore::readHeader
@ -173,8 +168,8 @@ void Foam::fileFormats::STARCDCore::removeFiles(const fileName& base)
Foam::label Foam::fileFormats::STARCDCore::readPoints
(
IFstream& is,
pointField& points,
labelList& ids
List<point>& points,
List<label>& ids
)
{
label maxId = 0;
@ -219,7 +214,7 @@ Foam::label Foam::fileFormats::STARCDCore::readPoints
void Foam::fileFormats::STARCDCore::writePoints
(
Ostream& os,
const pointField& points,
const UList<point>& points,
const scalar scaleFactor
)
{

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 | Copyright (C) 2016 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -37,9 +37,10 @@ SourceFiles
#include "IFstream.H"
#include "Enum.H"
#include "pointField.H"
#include "Map.H"
#include "point.H"
#include "FixedList.H"
#include "List.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -133,13 +134,13 @@ protected:
// Constructors
//- Construct null
STARCDCore();
STARCDCore() = default;
//- Read header and check signature PROSTAR_(CELL|VERTEX|BOUNDARY)
static bool readHeader(IFstream&, const enum fileHeader);
static bool readHeader(IFstream& is, const enum fileHeader header);
//- Write header for fileType (CELL|VERTEX|BOUNDARY)
static void writeHeader(Ostream&, const enum fileHeader);
static void writeHeader(Ostream& os, const enum fileHeader header);
public:
@ -150,7 +151,7 @@ public:
static fileName starFileName
(
const fileName& baseName,
const enum fileExt
const enum fileExt ext
);
@ -173,16 +174,16 @@ public:
// \endverbatim
static label readPoints
(
IFstream&,
pointField&,
labelList& ids
IFstream& is,
List<point>& points,
List<label>& ids
);
//- Write header and points to (.vrt) file, optionally with scaling
static void writePoints
(
Ostream&,
const pointField&,
Ostream& os,
const UList<point>& points,
const scalar scaleFactor = 1.0
);

View File

@ -59,21 +59,20 @@ static bool startsWithSolid(const char header[STLHeaderSize])
//! \endcond
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::fileFormats::STLCore::STLCore()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::fileFormats::STLCore::isBinaryName
(
const fileName& filename,
const STLFormat& format
const STLFormat format
)
{
return (format == UNKNOWN ? (filename.ext() == "stlb") : format == BINARY);
return
(
format == STLFormat::UNKNOWN
? (filename.ext() == "stlb")
: format == STLFormat::BINARY
);
}

View File

@ -69,11 +69,12 @@ protected:
// Protected Member Functions
//- Detect 'stlb' extension as binary
//- Detect 'stlb' extension as binary when format = UNKNOWN.
// Otherwise test if format == BINARY.
static bool isBinaryName
(
const fileName& filename,
const STLFormat& format
const STLFormat format
);
@ -98,7 +99,7 @@ protected:
// Constructors
//- Construct null
STLCore();
STLCore() = default;
};

View File

@ -38,7 +38,7 @@ bool Foam::fileFormats::STLReader::readBINARY
)
{
sorted_ = true;
format_ = UNKNOWN;
format_ = STLFormat::UNKNOWN;
label nTris = 0;
autoPtr<istream> streamPtr = readBinaryHeader(filename, nTris);
@ -125,7 +125,7 @@ bool Foam::fileFormats::STLReader::readBINARY
names_.clear();
sizes_.transfer(dynSizes);
format_ = BINARY;
format_ = STLFormat::BINARY;
return true;
}
@ -133,10 +133,15 @@ bool Foam::fileFormats::STLReader::readBINARY
bool Foam::fileFormats::STLReader::readFile
(
const fileName& filename,
const STLFormat& format
const STLFormat format
)
{
if (format == UNKNOWN ? detectBinaryHeader(filename) : format == BINARY)
if
(
format == STLFormat::UNKNOWN
? detectBinaryHeader(filename)
: format == STLFormat::BINARY
)
{
return readBINARY(filename);
}
@ -159,17 +164,17 @@ Foam::fileFormats::STLReader::STLReader
zoneIds_(),
names_(),
sizes_(),
format_(STLCore::UNKNOWN)
format_(STLFormat::UNKNOWN)
{
// Auto-detect ASCII/BINARY format
readFile(filename, STLCore::UNKNOWN);
readFile(filename, STLFormat::UNKNOWN);
}
Foam::fileFormats::STLReader::STLReader
(
const fileName& filename,
const STLFormat& format
const STLFormat format
)
:
sorted_(true),
@ -177,7 +182,7 @@ Foam::fileFormats::STLReader::STLReader
zoneIds_(),
names_(),
sizes_(),
format_(STLCore::UNKNOWN)
format_(STLFormat::UNKNOWN)
{
// Manually specified ASCII/BINARY format
readFile(filename, format);
@ -199,7 +204,7 @@ void Foam::fileFormats::STLReader::clear()
zoneIds_.clear();
names_.clear();
sizes_.clear();
format_ = UNKNOWN;
format_ = STLFormat::UNKNOWN;
}
@ -215,7 +220,7 @@ Foam::label Foam::fileFormats::STLReader::mergePointsMap
return mergePointsMap
(
(format_ == BINARY ? 10 : 100) * doubleScalarSMALL,
(format_ == STLFormat::BINARY ? 10 : 100) * doubleScalarSMALL,
pointMap
);
}

View File

@ -85,7 +85,7 @@ class STLReader
bool readBINARY(const fileName& filename);
//- Read ASCII or BINARY
bool readFile(const fileName& filename, const STLFormat& format);
bool readFile(const fileName& filename, const STLFormat format);
//- Disallow default bitwise copy construct
@ -105,7 +105,7 @@ public:
//- Read from file, filling in the information.
// Manually selected choice of ASCII/BINARY/UNKNOWN(detect) formats.
STLReader(const fileName& filename, const STLFormat& format);
STLReader(const fileName& filename, const STLFormat format);
//- Destructor

View File

@ -411,7 +411,7 @@ bool Foam::fileFormats::STLReader::readASCII
const fileName& filename
)
{
format_ = UNKNOWN;
format_ = STLFormat::UNKNOWN;
IFstream is(filename);
if (!is)
@ -434,7 +434,7 @@ bool Foam::fileFormats::STLReader::readASCII
names_.transfer(lexer.names());
sizes_.transfer(lexer.sizes());
format_ = ASCII;
format_ = STLFormat::ASCII;
return true;
}