Commit b799b5d6 authored by Mark Olesen's avatar Mark Olesen
Browse files

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

- increases coverage.

STYLE: relocate some core pieces into fileFormats
parent 88876e46
......@@ -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
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -23,36 +23,75 @@ License
\*---------------------------------------------------------------------------*/
#include "STLsurfaceFormatCore.H"
#include "STLCore.H"
#include "gzstream.h"
#include "OSspecific.H"
#include "Map.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
// - 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::STLsurfaceFormatCore::detectBINARY
int Foam::fileFormats::STLCore::detectBinaryHeader
(
const fileName& filename
)
{
off_t dataFileSize = Foam::fileSize(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();
IFstream str(filename, IOstream::BINARY);
istream& is = str().stdStream();
if (!is.good())
{
FatalErrorInFunction
<< "Cannot read file " << filename
<< " or file " << filename + ".gz"
<< exit(FatalError);
}
// Read the STL header
char header[headerSize];
is.read(header, headerSize);
char header[STLHeaderSize];
is.read(header, STLHeaderSize);
// Check that stream is OK, if not this may be an ASCII file
if (!is.good())
......@@ -60,7 +99,7 @@ int Foam::fileFormats::STLsurfaceFormatCore::detectBINARY
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)
int nTris;
is.read(reinterpret_cast<char*>(&nTris), sizeof(unsigned int));
......@@ -74,33 +113,73 @@ int Foam::fileFormats::STLsurfaceFormatCore::detectBINARY
(
!is
|| nTris < 0
|| nTris < (dataFileSize - headerSize)/50
|| nTris > (dataFileSize - headerSize)/25
)
{
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;
}
bool Foam::fileFormats::STLsurfaceFormatCore::readBINARY
Foam::autoPtr<std::istream>
Foam::fileFormats::STLCore::readBinaryHeader
(
istream& is,
const off_t dataFileSize
const fileName& filename,
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
char header[headerSize];
is.read(header, headerSize);
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);
......@@ -116,156 +195,56 @@ bool Foam::fileFormats::STLsurfaceFormatCore::readBINARY
//
// 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
|| nTris < int(dataFileSize - headerSize)/50
|| nTris > int(dataFileSize - headerSize)/25
)
if (!is || nTris < 0)
{
FatalErrorInFunction
<< "problem reading number of triangles, perhaps file is not binary"
<< exit(FatalError);
bad = true;
}
#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)
else if (!compressed)
{
// 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();
const off_t dataFileSize = Foam::fileSize(filename);
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
(
nTris < int(dataFileSize - STLHeaderSize)/50
|| nTris > int(dataFileSize - STLHeaderSize)/25
)
{
if (prevZone != -1)
{
Info<< "endsolid zone" << prevZone << nl;
}
prevZone = zoneI;
Info<< "solid zone" << prevZone << nl;
bad = true;
}
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 * * * * * * * * * * * * * * //
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
if (bad)
{
readASCII
(
IFstream(filename)().stdStream(),
dataFileSize
);
}
}
streamPtr.clear();
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::fileFormats::STLsurfaceFormatCore::~STLsurfaceFormatCore()
{}
FatalErrorInFunction
<< "problem reading number of triangles, perhaps file is not binary"
<< exit(FatalError);
}
nTrisEstimated = nTris;
return streamPtr;
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::fileFormats::STLsurfaceFormatCore::writeHeaderBINARY
void Foam::fileFormats::STLCore::writeBinaryHeader
(
ostream& os,
unsigned int nTris
)
{
// STL header with extra information about nTris
char header[headerSize];
char header[STLHeaderSize];
sprintf(header, "STL binary file %u facets", nTris);
// avoid trailing junk
for (size_t i = strlen(header); i < headerSize; ++i)
for (size_t i = strlen(header); i < STLHeaderSize; ++i)
{
header[i] = 0;
}
os.write(header, headerSize);
os.write(header, STLHeaderSize);
os.write(reinterpret_cast<char*>(&nTris), sizeof(unsigned int));
}
......
/*---------------------------------------------------------------------------*\
========= |
\\ / 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
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / 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
(