ENH: surfMesh support for reading compressed binary stl files (issue #294)

- increases coverage.

STYLE: relocate some core pieces into fileFormats
This commit is contained in:
Mark Olesen
2016-11-13 23:04:38 +01:00
parent 88876e46c1
commit b799b5d65d
12 changed files with 639 additions and 243 deletions

View File

@ -10,6 +10,9 @@ ensight/type/ensightPTraits.C
nas/NASCore.C
fire/FIRECore.C
starcd/STARCDCore.C
stl/STLCore.C
stl/STLReader.C
stl/STLReaderASCII.L
vtk/foamVtkCore.C
vtk/format/foamVtkAppendBase64Formatter.C

View File

@ -0,0 +1,251 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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.
-------------------------------------------------------------------------------
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 "STLCore.H"
#include "gzstream.h"
#include "OSspecific.H"
#include "IFstream.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
//! \cond fileScope
// The number of bytes in the STL binary header
static const unsigned STLHeaderSize = 80;
//! \endcond
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::fileFormats::STLCore::STLCore()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::fileFormats::STLCore::isBinaryName
(
const fileName& filename,
const STLFormat& format
)
{
return (format == DETECT ? (filename.ext() == "stlb") : format == BINARY);
}
// Check binary by getting the header and number of facets
// this seems to work better than the old token-based method
// - some programs (eg, pro-STAR) have 'solid' as the first word in
// the binary header.
// - using wordToken can cause an abort if non-word (binary) content
// is detected ... this is not exactly what we want.
int Foam::fileFormats::STLCore::detectBinaryHeader
(
const fileName& filename
)
{
bool compressed = false;
autoPtr<istream> streamPtr
(
new ifstream(filename.c_str(), std::ios::binary)
);
// If the file is compressed, decompress it before further checking.
if (!streamPtr->good() && isFile(filename + ".gz", false))
{
compressed = true;
streamPtr.reset(new igzstream((filename + ".gz").c_str()));
}
istream& is = streamPtr();
if (!is.good())
{
FatalErrorInFunction
<< "Cannot read file " << filename
<< " or file " << filename + ".gz"
<< exit(FatalError);
}
// Read the STL header
char header[STLHeaderSize];
is.read(header, STLHeaderSize);
// Check that stream is OK, if not this may be an ASCII file
if (!is.good())
{
return 0;
}
// Read the number of triangles in the STL file
// (note: read as int so we can check whether >2^31)
int nTris;
is.read(reinterpret_cast<char*>(&nTris), sizeof(unsigned int));
// Check that stream is OK and number of triangles is positive,
// if not this may be an ASCII file
//
// Also compare the file size with that expected from the number of tris
// If the comparison is not sensible then it may be an ASCII file
if
(
!is
|| nTris < 0
)
{
return 0;
}
else if (!compressed)
{
const off_t dataFileSize = Foam::fileSize(filename);
if
(
nTris < int(dataFileSize - STLHeaderSize)/50
|| nTris > int(dataFileSize - STLHeaderSize)/25
)
{
return 0;
}
}
// looks like it might be BINARY, return number of triangles
return nTris;
}
Foam::autoPtr<std::istream>
Foam::fileFormats::STLCore::readBinaryHeader
(
const fileName& filename,
label& nTrisEstimated
)
{
bool bad = false;
bool compressed = false;
nTrisEstimated = 0;
autoPtr<istream> streamPtr
(
new ifstream(filename.c_str(), std::ios::binary)
);
// If the file is compressed, decompress it before reading.
if (!streamPtr->good() && isFile(filename + ".gz", false))
{
compressed = true;
streamPtr.reset(new igzstream((filename + ".gz").c_str()));
}
istream& is = streamPtr();
if (!is.good())
{
streamPtr.clear();
FatalErrorInFunction
<< "Cannot read file " << filename
<< " or file " << filename + ".gz"
<< exit(FatalError);
}
// Read the STL header
char header[STLHeaderSize];
is.read(header, STLHeaderSize);
// Check that stream is OK, if not this may be an ASCII file
if (!is.good())
{
streamPtr.clear();
FatalErrorInFunction
<< "problem reading header, perhaps file is not binary "
<< exit(FatalError);
}
// Read the number of triangles in the STl file
// (note: read as int so we can check whether >2^31)
int nTris;
is.read(reinterpret_cast<char*>(&nTris), sizeof(unsigned int));
// Check that stream is OK and number of triangles is positive,
// if not this maybe an ASCII file
//
// Also compare the file size with that expected from the number of tris
// If the comparison is not sensible then it may be an ASCII file
if (!is || nTris < 0)
{
bad = true;
}
else if (!compressed)
{
const off_t dataFileSize = Foam::fileSize(filename);
if
(
nTris < int(dataFileSize - STLHeaderSize)/50
|| nTris > int(dataFileSize - STLHeaderSize)/25
)
{
bad = true;
}
}
if (bad)
{
streamPtr.clear();
FatalErrorInFunction
<< "problem reading number of triangles, perhaps file is not binary"
<< exit(FatalError);
}
nTrisEstimated = nTris;
return streamPtr;
}
void Foam::fileFormats::STLCore::writeBinaryHeader
(
ostream& os,
unsigned int nTris
)
{
// STL header with extra information about nTris
char header[STLHeaderSize];
sprintf(header, "STL binary file %u facets", nTris);
// avoid trailing junk
for (size_t i = strlen(header); i < STLHeaderSize; ++i)
{
header[i] = 0;
}
os.write(header, STLHeaderSize);
os.write(reinterpret_cast<char*>(&nTris), sizeof(unsigned int));
}
// ************************************************************************* //

View File

@ -0,0 +1,115 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016 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::STLCore
Description
Core routines used when reading/writing STL files.
SourceFiles
STLCore.C
\*---------------------------------------------------------------------------*/
#ifndef STLCore_H
#define STLCore_H
#include "STLpoint.H"
#include "STLtriangle.H"
#include "autoPtr.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace fileFormats
{
/*---------------------------------------------------------------------------*\
Class fileFormats::STLCore Declaration
\*---------------------------------------------------------------------------*/
class STLCore
{
public:
// Public data types
//- Enumeration for the format of data in the stream
enum STLFormat
{
ASCII, //!< ASCII
BINARY, //!< BINARY
DETECT //!< Detect based on (input) content or (output) extension
};
protected:
// Protected Member Functions
//- Detect 'stlb' extension as binary
static bool isBinaryName
(
const fileName& filename,
const STLFormat& format
);
//- Check contents to detect if the file is a binary STL.
// Return the estimated number of triangles or 0 on error.
static int detectBinaryHeader(const fileName&);
//- Read STL binary file header.
// Return the opened file stream and estimated number of triangles.
// The stream is invalid and number of triangles is 0 on error.
static autoPtr<std::istream> readBinaryHeader
(
const fileName& filename,
label& nTrisEstimated
);
//- Write STL binary file and number of triangles to stream
static void writeBinaryHeader(ostream&, unsigned int);
// Constructors
//- Construct null
STLCore();
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace fileFormats
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -0,0 +1,188 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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.
-------------------------------------------------------------------------------
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 "STLReader.H"
#include "Map.H"
#include "IFstream.H"
#undef DEBUG_STLBINARY
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
bool Foam::fileFormats::STLReader::readBINARY
(
const fileName& filename
)
{
sorted_ = true;
label nTris = 0;
autoPtr<istream> streamPtr = readBinaryHeader(filename, nTris);
if (!streamPtr.valid())
{
FatalErrorInFunction
<< "Error reading file " << filename
<< " or file " << filename + ".gz"
<< exit(FatalError);
}
istream& is = streamPtr();
#ifdef DEBUG_STLBINARY
Info<< "# " << nTris << " facets" << endl;
label prevZone = -1;
#endif
points_.setSize(3*nTris);
zoneIds_.setSize(nTris);
Map<label> lookup;
DynamicList<label> dynSizes;
label ptI = 0;
label zoneI = -1;
forAll(zoneIds_, facei)
{
// Read STL triangle
STLtriangle stlTri(is);
// transcribe the vertices of the STL triangle -> points
points_[ptI++] = stlTri.a();
points_[ptI++] = stlTri.b();
points_[ptI++] = stlTri.c();
// interpret STL attribute as a zone
const label origId = stlTri.attrib();
Map<label>::const_iterator fnd = lookup.find(origId);
if (fnd != lookup.end())
{
if (zoneI != fnd())
{
// group appeared out of order
sorted_ = false;
}
zoneI = fnd();
}
else
{
zoneI = dynSizes.size();
lookup.insert(origId, zoneI);
dynSizes.append(0);
}
zoneIds_[facei] = zoneI;
dynSizes[zoneI]++;
#ifdef DEBUG_STLBINARY
if (prevZone != zoneI)
{
if (prevZone != -1)
{
Info<< "endsolid zone" << prevZone << nl;
}
prevZone = zoneI;
Info<< "solid zone" << prevZone << nl;
}
stlTri.print(Info);
#endif
}
#ifdef DEBUG_STLBINARY
if (prevZone != -1)
{
Info<< "endsolid zone" << prevZone << nl;
}
#endif
names_.clear();
sizes_.transfer(dynSizes);
return true;
}
bool Foam::fileFormats::STLReader::readFile
(
const fileName& filename,
const STLFormat& format
)
{
if (format == DETECT ? detectBinaryHeader(filename) : format == BINARY)
{
return readBINARY(filename);
}
else
{
return readASCII(filename);
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::fileFormats::STLReader::STLReader
(
const fileName& filename
)
:
sorted_(true),
points_(),
zoneIds_(),
names_(),
sizes_()
{
// Auto-detect ASCII/BINARY format
readFile(filename, STLCore::DETECT);
}
Foam::fileFormats::STLReader::STLReader
(
const fileName& filename,
const STLFormat& format
)
:
sorted_(true),
points_(),
zoneIds_(),
names_(),
sizes_()
{
// Manually specified ASCII/BINARY format
readFile(filename, format);
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::fileFormats::STLReader::~STLReader()
{}
// ************************************************************************* //

View File

@ -0,0 +1,164 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
-------------------------------------------------------------------------------
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::STLReader
Description
Internal class used by the STLsurfaceFormat
SourceFiles
STLReader.C
STLsurfaceFormatASCII.L
\*---------------------------------------------------------------------------*/
#ifndef STLReader_H
#define STLReader_H
#include "STLCore.H"
#include "labelledTri.H"
#include "IFstream.H"
#include "Ostream.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace fileFormats
{
/*---------------------------------------------------------------------------*\
Class fileFormats::STLReader Declaration
\*---------------------------------------------------------------------------*/
class STLReader
:
public STLCore
{
// Private Data
bool sorted_;
//- The points supporting the facets
pointField points_;
//- The zones associated with the faces
List<label> zoneIds_;
//- The solid names, in the order of their first appearance
List<word> names_;
//- The solid count, in the order of their first appearance
List<label> sizes_;
// Private Member Functions
//- Read ASCII
bool readASCII(const fileName&);
//- Read BINARY
bool readBINARY(const fileName&);
//- Read ASCII or BINARY
bool readFile(const fileName&, const STLFormat&);
//- Disallow default bitwise copy construct
STLReader(const STLReader&) = delete;
//- Disallow default bitwise assignment
void operator=(const STLReader&) = delete;
public:
// Constructors
//- Read from file, filling in the information
STLReader(const fileName&);
//- Read from file, filling in the information.
// Manually selected choice of ascii/binary/detect.
STLReader(const fileName&, const STLFormat&);
//- Destructor
~STLReader();
// Member Functions
//- File read was already sorted
bool sorted() const
{
return sorted_;
}
//- Flush all values
void clear()
{
sorted_ = true;
points_.clear();
zoneIds_.clear();
names_.clear();
sizes_.clear();
}
//- Return full access to the points
pointField& points()
{
return points_;
}
//- Return full access to the zoneIds
List<label>& zoneIds()
{
return zoneIds_;
}
//- The list of solid names in the order of their first appearance
List<word>& names()
{
return names_;
}
//- The list of solid sizes in the order of their first appearance
List<label>& sizes()
{
return sizes_;
}
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace fileFormats
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -0,0 +1,425 @@
/*--------------------------------*- C++ -*----------------------------------*\
========= |
\\ / 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.
-------------------------------------------------------------------------------
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/>.
\*---------------------------------------------------------------------------*/
%{
#undef yyFlexLexer
/* ------------------------------------------------------------------------ *\
------ local definitions
\* ------------------------------------------------------------------------ */
#include "STLReader.H"
#include "OSspecific.H"
using namespace Foam;
// Dummy yyFlexLexer::yylex() to keep the linker happy. It is not called
//! \cond dummy
int yyFlexLexer::yylex()
{
FatalErrorInFunction
<< "Should not have called this function"
<< abort(FatalError);
return 0;
}
//! \endcond
// Dummy yywrap to keep yylex happy at compile time.
// It is called by yylex but is not used as the mechanism to change file.
// See <<EOF>>
//! \cond dummy
#if YY_FLEX_MINOR_VERSION < 6 && YY_FLEX_SUBMINOR_VERSION < 34
extern "C" int yywrap()
#else
int yyFlexLexer::yywrap()
#endif
{
return 1;
}
//! \endcond
//- A lexer for parsing STL ASCII files.
// Returns DynamicList(s) of points and facets (zoneIds).
// The facets are within a solid/endsolid grouping
class STLASCIILexer
:
public yyFlexLexer
{
// Private data
bool sorted_;
label groupID_; // current solid group
label lineNo_;
word startError_;
DynamicList<point> points_;
DynamicList<label> facets_;
DynamicList<word> names_;
DynamicList<label> sizes_;
HashTable<label> lookup_;
public:
// Constructors
//- From input stream and the approximate number of vertices in the STL
STLASCIILexer(istream* is, const label approxNpoints);
// Member Functions
//- The lexer function itself
int lex();
// Access
//- Do all the solid groups appear in order
inline bool sorted() const
{
return sorted_;
}
//- A list of points corresponding to a pointField
inline DynamicList<point>& points()
{
return points_;
}
//- A list of facet IDs (group IDs)
// corresponds to the number of triangles
inline DynamicList<label>& facets()
{
return facets_;
}
//- Names
inline DynamicList<word>& names()
{
return names_;
}
//- Sizes
inline DynamicList<label>& sizes()
{
return sizes_;
}
};
STLASCIILexer::STLASCIILexer(istream* is, const label approxNpoints)
:
yyFlexLexer(is),
sorted_(true),
groupID_(-1),
lineNo_(1),
points_(approxNpoints),
facets_(approxNpoints)
{}
/* ------------------------------------------------------------------------ *\
------ cppLexer::yylex()
\* ------------------------------------------------------------------------ */
#define YY_DECL int STLASCIILexer::lex()
%}
one_space [ \t\f\r]
space {one_space}*
some_space {one_space}+
alpha [_A-Za-z]
digit [0-9]
integer {digit}+
signedInteger [-+]?{integer}
word ([[:alnum:]]|[[:punct:]])*
string {word}({some_space}{word})*
exponent_part [eE][-+]?{digit}+
fractional_constant [-+]?(({digit}*"."{digit}+)|({digit}+"."?))
floatNum (({fractional_constant}{exponent_part}?)|({digit}+{exponent_part}))
x {floatNum}
y {floatNum}
z {floatNum}
solid {space}("solid"|"SOLID"){space}
color {space}("color"|"COLOR"){some_space}{floatNum}{some_space}{floatNum}{some_space}{floatNum}{space}
facet {space}("facet"|"FACET"){space}
normal {space}("normal"|"NORMAL"){space}
point {space}{x}{some_space}{y}{some_space}{z}{space}
outerloop {space}("outer"{some_space}"loop")|("OUTER"{some_space}"LOOP"){space}
vertex {space}("vertex"|"VERTEX"){space}
endloop {space}("endloop"|"ENDLOOP"){space}
endfacet {space}("endfacet"|"ENDFACET"){space}
endsolid {space}("endsolid"|"ENDSOLID")({some_space}{word})*
/* ------------------------------------------------------------------------ *\
----- Exclusive start states -----
\* ------------------------------------------------------------------------ */
%option stack
%x readSolidName
%x readFacet
%x readNormal
%x readVertices
%x readVertex
%x stlError
%%
%{
// End of read character pointer returned by strtof
// char* endPtr;
STLpoint normal;
STLpoint vertex;
label cmpt = 0; // component index used for reading vertex
static const char* stateNames[7] =
{
"reading solid",
"reading solid name",
"reading facet",
"reading normal",
"reading vertices",
"reading vertex",
"error"
};
static const char* stateExpects[7] =
{
"'solid', 'color', 'facet' or 'endsolid'",
"<string>",
"'normal', 'outer loop' or 'endfacet'",
"<float> <float> <float>",
"'vertex' or 'endloop'",
"<float> <float> <float>",
""
};
%}
/* ------------------------------------------------------------------------ *\
------ Start Lexing ------
\* ------------------------------------------------------------------------ */
/* ------ Reading control header ------ */
{solid} {
BEGIN(readSolidName);
}
<readSolidName>{string} {
word name(Foam::string::validate<word>(YYText()));
HashTable<label>::const_iterator fnd = lookup_.find(name);
if (fnd != lookup_.end())
{
if (groupID_ != fnd())
{
// group appeared out of order
sorted_ = false;
}
groupID_ = fnd();
}
else
{
groupID_ = sizes_.size();
lookup_.insert(name, groupID_);
names_.append(name);
sizes_.append(0);
}
BEGIN(INITIAL);
}
<readSolidName>{space}\n {
word name("solid");
HashTable<label>::const_iterator fnd = lookup_.find(name);
if (fnd != lookup_.end())
{
if (groupID_ != fnd())
{
// group appeared out of order
sorted_ = false;
}
groupID_ = fnd();
}
else
{
groupID_ = sizes_.size();
lookup_.insert(name, groupID_);
names_.append(name);
sizes_.append(0);
}
lineNo_++;
BEGIN(INITIAL);
}
{color} {
}
{facet} {
BEGIN(readFacet);
}
<readFacet>{normal} {
BEGIN(readNormal);
}
<readNormal>{point} {
/*
skip reading normals:
normal.x() = strtof(YYText(), &endPtr);
normal.y() = strtof(endPtr, &endPtr);
normal.z() = strtof(endPtr, &endPtr);
normals_.append(normal);
*/
BEGIN(readFacet);
}
<readFacet>{outerloop} {
BEGIN(readVertices);
}
<readVertices>{vertex} {
BEGIN(readVertex);
}
<readVertex>{space}{signedInteger}{space} {
vertex[cmpt++] = atol(YYText());
if (cmpt == 3)
{
cmpt = 0;
points_.append(vertex);
BEGIN(readVertices);
}
}
<readVertex>{space}{floatNum}{space} {
vertex[cmpt++] = atof(YYText());
if (cmpt == 3)
{
cmpt = 0;
points_.append(vertex);
BEGIN(readVertices);
}
}
<readVertices>{endloop} {
BEGIN(readFacet);
}
<readFacet>{endfacet} {
facets_.append(groupID_);
sizes_[groupID_]++;
BEGIN(INITIAL);
}
{endsolid} {
}
/* ------------------ Ignore remaining space and \n s. -------------------- */
<*>{space} {}
<*>\n { lineNo_++; }
/* ------------------- Any other characters are errors -------------------- */
<*>. {
startError_ = YYText();
yy_push_state(stlError);
}
/* ---------------------------- Error handler ----------------------------- */
<stlError>.* {
yy_pop_state();
FatalErrorInFunction
<< "while " << stateNames[YY_START] << " on line " << lineNo_ << nl
<< " expected " << stateExpects[YY_START]
<< " but found '" << startError_.c_str() << YYText() << "'"
<< exit(FatalError);
}
/* ------------------------ On EOF terminate ---------------------------- */
<<EOF>> {
yyterminate();
}
%%
//
// member function
//
bool Foam::fileFormats::STLReader::readASCII
(
const fileName& filename
)
{
IFstream is(filename);
if (!is)
{
FatalErrorInFunction
<< "file " << filename << " not found"
<< exit(FatalError);
}
// Create the lexer with the approximate number of vertices in the STL
// from the file size
STLASCIILexer lexer(&(is.stdStream()), Foam::fileSize(filename)/400);
while (lexer.lex() != 0) {}
sorted_ = lexer.sorted();
// Transfer to normal lists
points_.transfer(lexer.points());
zoneIds_.transfer(lexer.facets());
names_.transfer(lexer.names());
sizes_.transfer(lexer.sizes());
return true;
}
/* ------------------------------------------------------------------------ *\
------ End of STLReaderASCII.L
\* ------------------------------------------------------------------------ */

View File

@ -0,0 +1,100 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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.
-------------------------------------------------------------------------------
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::STLpoint
Description
A vertex point representation for STL files.
\*---------------------------------------------------------------------------*/
#ifndef STLpoint_H
#define STLpoint_H
#include "point.H"
#include "floatVector.H"
#include "Istream.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
/*---------------------------------------------------------------------------*\
Class STLpoint Declaration
\*---------------------------------------------------------------------------*/
class STLpoint
:
public Vector<float>
{
public:
// Constructors
//- Construct null
inline STLpoint()
{}
//- Construct from components
inline STLpoint(float x, float y, float z)
:
Vector<float>(x, y, z)
{}
//- Construct from point
inline STLpoint(const point& pt)
:
Vector<float>(float(pt.x()), float(pt.y()), float(pt.z()))
{}
//- Construct from istream
inline STLpoint(Istream& is)
:
Vector<float>(is)
{}
// Member Operators
#ifdef WM_DP
//- Conversion to double-precision point
inline operator point() const
{
return point(x(), y(), z());
}
#endif
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -0,0 +1,157 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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.
-------------------------------------------------------------------------------
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::STLtriangle
Description
A triangle representation for STL files.
SourceFiles
STLtriangleI.H
\*---------------------------------------------------------------------------*/
#ifndef STLtriangle_H
#define STLtriangle_H
#include "STLpoint.H"
#include "Istream.H"
#include "Ostream.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// Forward declaration of friend functions and operators
class STLtriangle;
Ostream& operator<<(Ostream&, const STLtriangle&);
/*---------------------------------------------------------------------------*\
Class STLtriangle Declaration
\*---------------------------------------------------------------------------*/
class STLtriangle
{
// Private data
//- Attribute is 16-bit
typedef unsigned short STLattrib;
//- The face normal, many programs write zero or other junk
STLpoint normal_;
//- The three points defining the triangle
STLpoint a_, b_, c_;
//- The attribute information could for colour or solid id, etc
STLattrib attrib_;
public:
// Constructors
//- Construct null
inline STLtriangle();
//- Construct from components
inline STLtriangle
(
const STLpoint& normal,
const STLpoint& a,
const STLpoint& b,
const STLpoint& c,
unsigned short attrib
);
//- Construct from istream (read binary)
inline STLtriangle(istream&);
// Member Functions
// Access
inline const STLpoint& normal() const;
inline const STLpoint& a() const;
inline const STLpoint& b() const;
inline const STLpoint& c() const;
inline unsigned short attrib() const;
// Read
//- Read from istream (binary)
inline void read(istream&);
// Write
//- Write to ostream (binary)
inline void write(ostream&) const;
//- Write to Ostream (ASCII)
inline Ostream& print(Ostream& os) const;
//- Write components to Ostream (ASCII)
inline static void write
(
Ostream& os,
const vector& norm,
const point& pt0,
const point& pt1,
const point& pt2
);
//- Write components to Ostream (ASCII), calculating the normal
inline static void write
(
Ostream& os,
const point& pt0,
const point& pt1,
const point& pt2
);
// Ostream operator
inline friend Ostream& operator<<(Ostream&, const STLtriangle&);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "STLtriangleI.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -0,0 +1,168 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
-------------------------------------------------------------------------------
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 "triPointRef.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline Foam::STLtriangle::STLtriangle()
{}
inline Foam::STLtriangle::STLtriangle
(
const STLpoint& normal,
const STLpoint& a,
const STLpoint& b,
const STLpoint& c,
unsigned short attrib
)
:
normal_(normal),
a_(a),
b_(b),
c_(c),
attrib_(attrib)
{}
inline Foam::STLtriangle::STLtriangle(istream& is)
{
read(is);
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
inline const Foam::STLpoint& Foam::STLtriangle::normal() const
{
return normal_;
}
inline const Foam::STLpoint& Foam::STLtriangle::a() const
{
return a_;
}
inline const Foam::STLpoint& Foam::STLtriangle::b() const
{
return b_;
}
inline const Foam::STLpoint& Foam::STLtriangle::c() const
{
return c_;
}
inline unsigned short Foam::STLtriangle::attrib() const
{
return attrib_;
}
inline void Foam::STLtriangle::read(istream& is)
{
is.read(reinterpret_cast<char*>(this), 4*sizeof(STLpoint));
is.read(reinterpret_cast<char*>(&attrib_), sizeof(STLattrib));
}
inline void Foam::STLtriangle::write(ostream& os) const
{
os.write(reinterpret_cast<const char*>(this), 4*sizeof(STLpoint));
os.write(reinterpret_cast<const char*>(&attrib_), sizeof(STLattrib));
}
inline Foam::Ostream& Foam::STLtriangle::print(Ostream& os) const
{
os << " facet normal "
<< normal_.x() << ' ' << normal_.y() << ' ' << normal_.z() << nl
<< " outer loop" << nl
<< " vertex " << a_.x() << ' ' << a_.y() << ' ' << a_.z() << nl
<< " vertex " << b_.x() << ' ' << b_.y() << ' ' << b_.z() << nl
<< " vertex " << c_.x() << ' ' << c_.y() << ' ' << c_.z() << nl
<< " endloop" << nl
<< " endfacet" << nl;
return os;
}
inline void Foam::STLtriangle::write
(
Ostream& os,
const vector& norm,
const point& pt0,
const point& pt1,
const point& pt2
)
{
os << " facet normal "
<< norm.x() << ' ' << norm.y() << ' ' << norm.z() << nl
<< " outer loop" << nl
<< " vertex " << pt0.x() << ' ' << pt0.y() << ' ' << pt0.z() << nl
<< " vertex " << pt1.x() << ' ' << pt1.y() << ' ' << pt1.z() << nl
<< " vertex " << pt2.x() << ' ' << pt2.y() << ' ' << pt2.z() << nl
<< " endloop" << nl
<< " endfacet" << nl;
}
inline void Foam::STLtriangle::write
(
Ostream& os,
const point& pt0,
const point& pt1,
const point& pt2
)
{
// calculate the normal ourselves
vector norm = triPointRef(pt0, pt1, pt2).normal();
norm /= mag(norm) + VSMALL;
write(os, norm, pt0, pt1, pt2);
}
// * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
inline Foam::Ostream& Foam::operator<<(Ostream& os, const STLtriangle& t)
{
os << t.normal_ << token::SPACE
<< t.a_ << token::SPACE
<< t.b_ << token::SPACE
<< t.c_ << token::SPACE
<< t.attrib_;
return os;
}
// ************************************************************************* //