Commit 3ef8906a authored by Mark Olesen's avatar Mark Olesen
Browse files

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

- eliminates previous code duplication and improves maintainability
parent 5947f9a3
......@@ -64,7 +64,7 @@ int main(int argc, char *argv[])
{
argList::addNote
(
"convert between surface formats"
"convert between surface formats, using triSurface library components"
);
argList::noParallel();
......@@ -96,16 +96,6 @@ int main(int argc, char *argv[])
argList args(argc, argv);
if (args.optionFound("writePrecision"))
{
label writePrecision = args.optionRead<label>("writePrecision");
IOstream::defaultPrecision(writePrecision);
Sout.precision(writePrecision);
Info<< "Output write precision set to " << writePrecision << endl;
}
const fileName importName = args[1];
const fileName exportName = args[2];
......@@ -116,6 +106,26 @@ int main(int argc, char *argv[])
<< exit(FatalError);
}
// Check that reading/writing is supported
if
(
!triSurface::canRead(importName, true)
|| !triSurface::canWriteType(exportName.ext(), true)
)
{
return 1;
}
if (args.optionFound("writePrecision"))
{
label writePrecision = args.optionRead<label>("writePrecision");
IOstream::defaultPrecision(writePrecision);
Sout.precision(writePrecision);
Info<< "Output write precision set to " << writePrecision << endl;
}
const scalar scaleFactor = args.optionLookupOrDefault<scalar>("scale", -1);
Info<< "Reading : " << importName << endl;
......
......@@ -75,7 +75,7 @@ int main(int argc, char *argv[])
{
argList::addNote
(
"convert between surface formats"
"convert between surface formats, using MeshSurface library components"
);
argList::noParallel();
......@@ -133,7 +133,7 @@ int main(int argc, char *argv[])
<< exit(FatalError);
}
// check that reading/writing is supported
// Check that reading/writing is supported
if
(
!MeshedSurface<face>::canRead(importName, true)
......
......@@ -288,6 +288,10 @@ public:
// Member Functions
//- Suppress direct swapping, since storage containers may be const
void swap(PrimitivePatch&) = delete;
// Access
//- Return reference to global points
......@@ -320,9 +324,9 @@ public:
label nInternalEdges() const;
//- Is internal edge?
bool isInternalEdge(const label edgeI) const
bool isInternalEdge(const label edgei) const
{
return edgeI < nInternalEdges();
return edgei < nInternalEdges();
}
//- Return list of boundary points,
......
......@@ -365,13 +365,18 @@ namespace stringOps
const std::string& delim
);
//- Split string into sub-strings using a fixed field width
//- Split string into sub-strings using a fixed field width.
// Behaviour is ill-defined if width is zero.
// \param str the string to be split
// \param width the fixed field width for each sub-string
// \param start the optional offset of where to start the splitting.
// Any text prior to start is ignored in the operation.
template<class StringType>
Foam::SubStrings<StringType> splitFixed
(
const StringType& str,
const std::string::size_type width
const std::string::size_type width,
const std::string::size_type start = 0
);
//- Split string into sub-strings at whitespace (TAB, NL, VT, FF, CR, SPC)
......
......@@ -183,7 +183,8 @@ template<class StringType>
Foam::SubStrings<StringType> Foam::stringOps::splitFixed
(
const StringType& str,
const std::string::size_type width
const std::string::size_type width,
const std::string::size_type start
)
{
Foam::SubStrings<StringType> lst;
......@@ -195,7 +196,7 @@ Foam::SubStrings<StringType> Foam::stringOps::splitFixed
const auto len = str.size();
lst.reserve(1 + (len / width));
for (std::string::size_type pos = 0; pos < len; pos += width)
for (std::string::size_type pos = start; pos < len; pos += width)
{
const auto end = (pos + width);
......
......@@ -40,12 +40,6 @@ Foam::fileFormats::FIRECore::file3dExtensions
};
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::fileFormats::FIRECore::FIRECore()
{}
// * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * * //
Foam::label Foam::fileFormats::FIRECore::readPoints
......
......@@ -108,7 +108,7 @@ protected:
// Protected Member Functions
//- Construct null
FIRECore();
FIRECore() = default;
//- Read points.
......
......@@ -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);
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);
Foam::fileFormats::NASCore::NASCore()
{}
// 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;
}
// ************************************************************************* //
......@@ -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
//- File field formats
enum fieldFormat
{
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() = default;
// Public Static Member Functions
//- Extract numbers from things like "-2.358-8" (same as "-2.358e-8")
static scalar readNasScalar(const string& str);
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 string& str)
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
);
// Constructors
//- Construct null
NASCore();
};
......
......@@ -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
)
{
......
......@@ -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
);
......
......@@ -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
);
}
......
......@@ -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;
};
......
......@@ -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