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 nas/NASCore.C
fire/FIRECore.C fire/FIRECore.C
starcd/STARCDCore.C starcd/STARCDCore.C
stl/STLCore.C
stl/STLReader.C
stl/STLReaderASCII.L
vtk/foamVtkCore.C vtk/foamVtkCore.C
vtk/format/foamVtkAppendBase64Formatter.C vtk/format/foamVtkAppendBase64Formatter.C

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) 2016 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -23,36 +23,75 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "STLsurfaceFormatCore.H" #include "STLCore.H"
#include "gzstream.h" #include "gzstream.h"
#include "OSspecific.H" #include "OSspecific.H"
#include "Map.H"
#include "IFstream.H" #include "IFstream.H"
#include "Ostream.H"
#undef DEBUG_STLBINARY // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // //! \cond fileScope
// check binary by getting the header and number of facets // 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 // this seems to work better than the old token-based method
// - some programs (eg, pro-STAR) have 'solid' as the first word in // - some programs (eg, pro-STAR) have 'solid' as the first word in
// the binary header. // the binary header.
// - using wordToken can cause an abort if non-word (binary) content // - using wordToken can cause an abort if non-word (binary) content
// is detected ... this is not exactly what we want. // is detected ... this is not exactly what we want.
int Foam::fileFormats::STLsurfaceFormatCore::detectBINARY int Foam::fileFormats::STLCore::detectBinaryHeader
( (
const fileName& filename const fileName& filename
) )
{ {
off_t dataFileSize = Foam::fileSize(filename); bool compressed = false;
autoPtr<istream> streamPtr
(
new ifstream(filename.c_str(), std::ios::binary)
);
IFstream str(filename, IOstream::BINARY); // If the file is compressed, decompress it before further checking.
istream& is = str().stdStream(); 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 // Read the STL header
char header[headerSize]; char header[STLHeaderSize];
is.read(header, headerSize); is.read(header, STLHeaderSize);
// Check that stream is OK, if not this may be an ASCII file // Check that stream is OK, if not this may be an ASCII file
if (!is.good()) if (!is.good())
@ -60,7 +99,7 @@ int Foam::fileFormats::STLsurfaceFormatCore::detectBINARY
return 0; return 0;
} }
// Read the number of triangles in the STl file // Read the number of triangles in the STL file
// (note: read as int so we can check whether >2^31) // (note: read as int so we can check whether >2^31)
int nTris; int nTris;
is.read(reinterpret_cast<char*>(&nTris), sizeof(unsigned int)); is.read(reinterpret_cast<char*>(&nTris), sizeof(unsigned int));
@ -74,33 +113,73 @@ int Foam::fileFormats::STLsurfaceFormatCore::detectBINARY
( (
!is !is
|| nTris < 0 || nTris < 0
|| nTris < (dataFileSize - headerSize)/50
|| nTris > (dataFileSize - headerSize)/25
) )
{ {
return 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 // looks like it might be BINARY, return number of triangles
return nTris; return nTris;
} }
bool Foam::fileFormats::STLsurfaceFormatCore::readBINARY Foam::autoPtr<std::istream>
Foam::fileFormats::STLCore::readBinaryHeader
( (
istream& is, const fileName& filename,
const off_t dataFileSize label& nTrisEstimated
) )
{ {
sorted_ = true; 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 // Read the STL header
char header[headerSize]; char header[STLHeaderSize];
is.read(header, headerSize); is.read(header, STLHeaderSize);
// Check that stream is OK, if not this may be an ASCII file // Check that stream is OK, if not this may be an ASCII file
if (!is.good()) if (!is.good())
{ {
streamPtr.clear();
FatalErrorInFunction FatalErrorInFunction
<< "problem reading header, perhaps file is not binary " << "problem reading header, perhaps file is not binary "
<< exit(FatalError); << exit(FatalError);
@ -116,156 +195,56 @@ bool Foam::fileFormats::STLsurfaceFormatCore::readBINARY
// //
// Also compare the file size with that expected from the number of tris // 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 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 if
( (
!is nTris < int(dataFileSize - STLHeaderSize)/50
|| nTris < 0 || nTris > int(dataFileSize - STLHeaderSize)/25
|| nTris < int(dataFileSize - headerSize)/50
|| nTris > int(dataFileSize - headerSize)/25
) )
{ {
bad = true;
}
}
if (bad)
{
streamPtr.clear();
FatalErrorInFunction FatalErrorInFunction
<< "problem reading number of triangles, perhaps file is not binary" << "problem reading number of triangles, perhaps file is not binary"
<< exit(FatalError); << exit(FatalError);
} }
#ifdef DEBUG_STLBINARY nTrisEstimated = nTris;
Info<< "# " << nTris << " facets" << endl; return streamPtr;
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 an 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();
// interprete 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;
}
Info<< " facet normal " << stlTri.normal() << nl
<< " outer loop" << nl
<< " vertex " << stlTri.a() << nl
<< " vertex " << stlTri.b() << nl
<< " vertex " << stlTri.c() << nl
<< " outer loop" << nl
<< " endfacet" << endl;
#endif
}
names_.clear();
sizes_.transfer(dynSizes);
return true;
} }
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // void Foam::fileFormats::STLCore::writeBinaryHeader
Foam::fileFormats::STLsurfaceFormatCore::STLsurfaceFormatCore
(
const fileName& filename
)
:
sorted_(true),
points_(0),
zoneIds_(0),
names_(0),
sizes_(0)
{
off_t dataFileSize = Foam::fileSize(filename);
// auto-detect ascii/binary
if (detectBINARY(filename))
{
readBINARY
(
IFstream(filename, IOstream::BINARY)().stdStream(),
dataFileSize
);
}
else
{
readASCII
(
IFstream(filename)().stdStream(),
dataFileSize
);
}
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::fileFormats::STLsurfaceFormatCore::~STLsurfaceFormatCore()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::fileFormats::STLsurfaceFormatCore::writeHeaderBINARY
( (
ostream& os, ostream& os,
unsigned int nTris unsigned int nTris
) )
{ {
// STL header with extra information about nTris // STL header with extra information about nTris
char header[headerSize]; char header[STLHeaderSize];
sprintf(header, "STL binary file %u facets", nTris); sprintf(header, "STL binary file %u facets", nTris);
// avoid trailing junk // avoid trailing junk
for (size_t i = strlen(header); i < headerSize; ++i) for (size_t i = strlen(header); i < STLHeaderSize; ++i)
{ {
header[i] = 0; header[i] = 0;
} }
os.write(header, headerSize); os.write(header, STLHeaderSize);
os.write(reinterpret_cast<char*>(&nTris), sizeof(unsigned int)); 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

@ -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) 2016 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -22,22 +22,22 @@ License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Class Class
Foam::fileFormats::STLsurfaceFormatCore Foam::fileFormats::STLReader
Description Description
Internal class used by the STLsurfaceFormat Internal class used by the STLsurfaceFormat
SourceFiles SourceFiles
STLsurfaceFormatCore.C STLReader.C
STLsurfaceFormatASCII.L STLsurfaceFormatASCII.L
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#ifndef STLsurfaceFormatCore_H #ifndef STLReader_H
#define STLsurfaceFormatCore_H #define STLReader_H
#include "STLtriangle.H" #include "STLCore.H"
#include "triFace.H" #include "labelledTri.H"
#include "IFstream.H" #include "IFstream.H"
#include "Ostream.H" #include "Ostream.H"
@ -49,10 +49,12 @@ namespace fileFormats
{ {
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class STLsurfaceFormatCore Declaration Class fileFormats::STLReader Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
class STLsurfaceFormatCore class STLReader
:
public STLCore
{ {
// Private Data // Private Data
@ -73,44 +75,37 @@ class STLsurfaceFormatCore
// Private Member Functions // Private Member Functions
//- Disallow default bitwise copy construct
STLsurfaceFormatCore(const STLsurfaceFormatCore&);
//- Disallow default bitwise assignment
void operator=(const STLsurfaceFormatCore&);
//- Determine the file type
static int detectBINARY(const fileName&);
//- Read ASCII //- Read ASCII
bool readASCII(istream&, const off_t); bool readASCII(const fileName&);
//- Read BINARY //- Read BINARY
bool readBINARY(istream&, const off_t); 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: public:
// Static Data
//- The number of bytes in the STL binary header
static const unsigned int headerSize = 80;
// Static Member Functions
//- Write "STL binary file" and number of triangles to stream
static void writeHeaderBINARY(ostream&, unsigned int);
// Constructors // Constructors
//- Read from file, filling in the information //- Read from file, filling in the information
STLsurfaceFormatCore(const fileName&); STLReader(const fileName&);
//- Read from file, filling in the information.
// Manually selected choice of ascii/binary/detect.
STLReader(const fileName&, const STLFormat&);
//- Destructor //- Destructor
~STLsurfaceFormatCore(); ~STLReader();
// Member Functions // Member Functions

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) 2016 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -31,7 +31,8 @@ License
------ local definitions ------ local definitions
\* ------------------------------------------------------------------------ */ \* ------------------------------------------------------------------------ */
#include "STLsurfaceFormatCore.H" #include "STLReader.H"
#include "OSspecific.H"
using namespace Foam; using namespace Foam;
@ -97,32 +98,32 @@ public:
// Access // Access
//- Do all the solid groups appear in order //- Do all the solid groups appear in order
bool sorted() const inline bool sorted() const
{ {
return sorted_; return sorted_;
} }
//- A list of points corresponding to a pointField //- A list of points corresponding to a pointField
DynamicList<point>& points() inline DynamicList<point>& points()
{ {
return points_; return points_;
} }
//- A list of facet IDs (group IDs) //- A list of facet IDs (group IDs)
// corresponds to the number of triangles // corresponds to the number of triangles
DynamicList<label>& facets() inline DynamicList<label>& facets()
{ {
return facets_; return facets_;
} }
//- Names //- Names
DynamicList<word>& names() inline DynamicList<word>& names()
{ {
return names_; return names_;
} }
//- Sizes //- Sizes
DynamicList<label>& sizes() inline DynamicList<label>& sizes()
{ {
return sizes_; return sizes_;
} }
@ -390,20 +391,27 @@ endsolid {space}("endsolid"|"ENDSOLID")({some_space}{word})*
// //
// member function // member function
// //
bool Foam::fileFormats::STLsurfaceFormatCore::readASCII bool Foam::fileFormats::STLReader::readASCII
( (
istream& is, const fileName& filename
const off_t dataFileSize
) )
{ {
IFstream is(filename);
if (!is)
{
FatalErrorInFunction
<< "file " << filename << " not found"
<< exit(FatalError);
}
// Create the lexer with the approximate number of vertices in the STL // Create the lexer with the approximate number of vertices in the STL
// from the file size // from the file size
STLASCIILexer lexer(&is, dataFileSize/400); STLASCIILexer lexer(&(is.stdStream()), Foam::fileSize(filename)/400);
while (lexer.lex() != 0) {} while (lexer.lex() != 0) {}
sorted_ = lexer.sorted(); sorted_ = lexer.sorted();
// transfer to normal lists // Transfer to normal lists
points_.transfer(lexer.points()); points_.transfer(lexer.points());
zoneIds_.transfer(lexer.facets()); zoneIds_.transfer(lexer.facets());
names_.transfer(lexer.names()); names_.transfer(lexer.names());
@ -413,5 +421,5 @@ bool Foam::fileFormats::STLsurfaceFormatCore::readASCII
} }
/* ------------------------------------------------------------------------ *\ /* ------------------------------------------------------------------------ *\
------ End of STLfileFormatASCII.L ------ End of STLReaderASCII.L
\* ------------------------------------------------------------------------ */ \* ------------------------------------------------------------------------ */

View File

@ -2,8 +2,8 @@
========= | ========= |
\\ / 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-2013 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -27,14 +27,13 @@ Class
Description Description
A vertex point representation for STL files. A vertex point representation for STL files.
SourceFiles
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#ifndef STLpoint_H #ifndef STLpoint_H
#define STLpoint_H #define STLpoint_H
#include "point.H" #include "point.H"
#include "floatVector.H"
#include "Istream.H" #include "Istream.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -80,11 +79,13 @@ public:
// Member Operators // Member Operators
//- Conversion to point #ifdef WM_DP
//- Conversion to double-precision point
inline operator point() const inline operator point() const
{ {
return point(x(), y(), z()); return point(x(), y(), z());
} }
#endif
}; };

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) 2016 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -45,12 +45,9 @@ namespace Foam
{ {
// Forward declaration of friend functions and operators // Forward declaration of friend functions and operators
class STLtriangle; class STLtriangle;
Ostream& operator<<(Ostream&, const STLtriangle&); Ostream& operator<<(Ostream&, const STLtriangle&);
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class STLtriangle Declaration Class STLtriangle Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -62,7 +59,7 @@ class STLtriangle
//- Attribute is 16-bit //- Attribute is 16-bit
typedef unsigned short STLattrib; typedef unsigned short STLattrib;
//- The face normal, many programs write zore or other junk //- The face normal, many programs write zero or other junk
STLpoint normal_; STLpoint normal_;
//- The three points defining the triangle //- The three points defining the triangle
@ -113,7 +110,30 @@ public:
// Write // Write
//- Write to ostream (binary) //- Write to ostream (binary)
inline void write(ostream&); 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 // Ostream operator

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) 2016 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -23,6 +23,8 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "triPointRef.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline Foam::STLtriangle::STLtriangle() inline Foam::STLtriangle::STLtriangle()
@ -91,10 +93,61 @@ inline void Foam::STLtriangle::read(istream& is)
} }
inline void Foam::STLtriangle::write(ostream& os) inline void Foam::STLtriangle::write(ostream& os) const
{ {
os.write(reinterpret_cast<char*>(this), 4*sizeof(STLpoint)); os.write(reinterpret_cast<const char*>(this), 4*sizeof(STLpoint));
os.write(reinterpret_cast<char*>(&attrib_), sizeof(STLattrib)); 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);
} }

View File

@ -32,9 +32,7 @@ $(surfaceFormats)/off/OFFsurfaceFormatRunTime.C
$(surfaceFormats)/smesh/SMESHsurfaceFormatRunTime.C $(surfaceFormats)/smesh/SMESHsurfaceFormatRunTime.C
$(surfaceFormats)/starcd/STARCDsurfaceFormatCore.C $(surfaceFormats)/starcd/STARCDsurfaceFormatCore.C
$(surfaceFormats)/starcd/STARCDsurfaceFormatRunTime.C $(surfaceFormats)/starcd/STARCDsurfaceFormatRunTime.C
$(surfaceFormats)/stl/STLsurfaceFormatCore.C
$(surfaceFormats)/stl/STLsurfaceFormatRunTime.C $(surfaceFormats)/stl/STLsurfaceFormatRunTime.C
$(surfaceFormats)/stl/STLsurfaceFormatASCII.L
$(surfaceFormats)/tri/TRIsurfaceFormatCore.C $(surfaceFormats)/tri/TRIsurfaceFormatCore.C
$(surfaceFormats)/tri/TRIsurfaceFormatRunTime.C $(surfaceFormats)/tri/TRIsurfaceFormatRunTime.C
$(surfaceFormats)/vtk/VTKsurfaceFormatCore.C $(surfaceFormats)/vtk/VTKsurfaceFormatCore.C

View File

@ -51,20 +51,17 @@ inline void Foam::fileFormats::STLsurfaceFormat<Face>::writeShell
const point& p0 = pointLst[f[0]]; const point& p0 = pointLst[f[0]];
for (label fp1 = 1; fp1 < f.size() - 1; ++fp1) for (label fp1 = 1; fp1 < f.size() - 1; ++fp1)
{ {
label fp2 = f.fcIndex(fp1); const label fp2 = f.fcIndex(fp1);
const point& p1 = pointLst[f[fp1]]; // Write ASCII
const point& p2 = pointLst[f[fp2]]; STLtriangle::write
(
// write STL triangle os,
os << " facet normal " norm,
<< norm.x() << ' ' << norm.y() << ' ' << norm.z() << nl p0,
<< " outer loop\n" pointLst[f[fp1]],
<< " vertex " << p0.x() << ' ' << p0.y() << ' ' << p0.z() << nl pointLst[f[fp2]]
<< " vertex " << p1.x() << ' ' << p1.y() << ' ' << p1.z() << nl );
<< " vertex " << p2.x() << ' ' << p2.y() << ' ' << p2.z() << nl
<< " endloop\n"
<< " endfacet" << endl;
} }
} }
@ -92,18 +89,17 @@ inline void Foam::fileFormats::STLsurfaceFormat<Face>::writeShell
const point& p0 = pointLst[f[0]]; const point& p0 = pointLst[f[0]];
for (label fp1 = 1; fp1 < f.size() - 1; ++fp1) for (label fp1 = 1; fp1 < f.size() - 1; ++fp1)
{ {
label fp2 = f.fcIndex(fp1); const label fp2 = f.fcIndex(fp1);
STLtriangle stlTri // Write BINARY
STLtriangle
( (
norm, norm,
p0, p0,
pointLst[f[fp1]], pointLst[f[fp1]],
pointLst[f[fp2]], pointLst[f[fp2]],
zoneI zoneI
); ).write(os);
stlTri.write(os);
} }
} }
@ -131,7 +127,7 @@ bool Foam::fileFormats::STLsurfaceFormat<Face>::read
this->clear(); this->clear();
// read in the values // read in the values
STLsurfaceFormatCore reader(filename); STLReader reader(filename);
// transfer points // transfer points
this->storedPoints().transfer(reader.points()); this->storedPoints().transfer(reader.points());
@ -273,7 +269,7 @@ void Foam::fileFormats::STLsurfaceFormat<Face>::writeBinary
// Write the STL header // Write the STL header
unsigned int nTris = surf.nTriangles(); unsigned int nTris = surf.nTriangles();
STLsurfaceFormatCore::writeHeaderBINARY(os, nTris); STLCore::writeBinaryHeader(os, nTris);
label faceIndex = 0; label faceIndex = 0;
forAll(zones, zoneI) forAll(zones, zoneI)
@ -379,7 +375,7 @@ void Foam::fileFormats::STLsurfaceFormat<Face>::writeBinary
// Write the STL header // Write the STL header
unsigned int nTris = surf.nTriangles(); unsigned int nTris = surf.nTriangles();
STLsurfaceFormatCore::writeHeaderBINARY(os, nTris); STLCore::writeBinaryHeader(os, nTris);
// always write unsorted // always write unsorted
forAll(faceLst, facei) forAll(faceLst, facei)
@ -402,10 +398,20 @@ void Foam::fileFormats::STLsurfaceFormat<Face>::write
const MeshedSurfaceProxy<Face>& surf const MeshedSurfaceProxy<Face>& surf
) )
{ {
const word ext = filename.ext(); // Auto-detect ASCII/BINARY extension
write(filename, surf, STLCore::DETECT);
}
// handle 'stlb' as binary directly
if (ext == "stlb") template<class Face>
void Foam::fileFormats::STLsurfaceFormat<Face>::write
(
const fileName& filename,
const MeshedSurfaceProxy<Face>& surf,
const STLFormat& format
)
{
if (STLCore::isBinaryName(filename, format))
{ {
writeBinary(filename, surf); writeBinary(filename, surf);
} }
@ -423,10 +429,20 @@ void Foam::fileFormats::STLsurfaceFormat<Face>::write
const UnsortedMeshedSurface<Face>& surf const UnsortedMeshedSurface<Face>& surf
) )
{ {
word ext = filename.ext(); // Auto-detect ASCII/BINARY extension
write(filename, surf, STLCore::DETECT);
}
// handle 'stlb' as binary directly
if (ext == "stlb") template<class Face>
void Foam::fileFormats::STLsurfaceFormat<Face>::write
(
const fileName& filename,
const UnsortedMeshedSurface<Face>& surf,
const STLFormat& format
)
{
if (STLCore::isBinaryName(filename, format))
{ {
writeBinary(filename, surf); writeBinary(filename, surf);
} }

View File

@ -25,7 +25,7 @@ Class
Foam::fileFormats::STLsurfaceFormat Foam::fileFormats::STLsurfaceFormat
Description Description
Provide a means of reading/writing STL files (ASCII and binary). Provide a means of reading/writing STL files (ASCII and BINARY).
Note Note
For efficiency, the zones are sorted before creating the faces. For efficiency, the zones are sorted before creating the faces.
@ -40,7 +40,7 @@ SourceFiles
#ifndef STLsurfaceFormat_H #ifndef STLsurfaceFormat_H
#define STLsurfaceFormat_H #define STLsurfaceFormat_H
#include "STLsurfaceFormatCore.H" #include "STLReader.H"
#include "MeshedSurface.H" #include "MeshedSurface.H"
#include "MeshedSurfaceProxy.H" #include "MeshedSurfaceProxy.H"
#include "UnsortedMeshedSurface.H" #include "UnsortedMeshedSurface.H"
@ -59,7 +59,8 @@ namespace fileFormats
template<class Face> template<class Face>
class STLsurfaceFormat class STLsurfaceFormat
: :
public MeshedSurface<Face> public MeshedSurface<Face>,
public STLCore
{ {
// Private Member Functions // Private Member Functions
@ -80,6 +81,7 @@ class STLsurfaceFormat
const label zoneI const label zoneI
); );
//- Disallow default bitwise copy construct //- Disallow default bitwise copy construct
STLsurfaceFormat(const STLsurfaceFormat<Face>&); STLsurfaceFormat(const STLsurfaceFormat<Face>&);
@ -132,6 +134,15 @@ public:
// as ASCII or BINARY, depending on the extension // as ASCII or BINARY, depending on the extension
static void write(const fileName&, const MeshedSurfaceProxy<Face>&); static void write(const fileName&, const MeshedSurfaceProxy<Face>&);
//- Write surface mesh components by proxy
// as ASCII or BINARY or dependent on the extension
static void write
(
const fileName&,
const MeshedSurfaceProxy<Face>&,
const STLFormat&
);
//- Write UnsortedMeshedSurface (as ASCII) sorted by zone //- Write UnsortedMeshedSurface (as ASCII) sorted by zone
static void writeAscii static void writeAscii
( (
@ -150,6 +161,15 @@ public:
// as ASCII or BINARY, depending on the extension // as ASCII or BINARY, depending on the extension
static void write(const fileName&, const UnsortedMeshedSurface<Face>&); static void write(const fileName&, const UnsortedMeshedSurface<Face>&);
//- Write UnsortedMeshedSurface
// as ASCII or BINARY or dependent on the extension
static void write
(
const fileName&,
const UnsortedMeshedSurface<Face>&,
const STLFormat&
);
//- Read from file //- Read from file
virtual bool read(const fileName&); virtual bool read(const fileName&);