1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software; you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 \*---------------------------------------------------------------------------*/
29 #include "triSurface.H"
30 #include "STLtriangle.H"
32 #include "OSspecific.H"
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
40 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
42 bool triSurface::readSTLBINARY(const fileName& STLfileName)
44 bool compressed = false;
46 autoPtr<istream> STLfilePtr
48 new ifstream(STLfileName.c_str(), std::ios::binary)
51 // If the file is compressed, decompress it before reading.
52 if (!STLfilePtr->good() && isFile(STLfileName + ".gz", false))
55 STLfilePtr.reset(new igzstream((STLfileName + ".gz").c_str()));
57 istream& STLfile = STLfilePtr();
61 FatalErrorIn("triSurface::readSTLBINARY(const fileName&)")
62 << "Cannot read file " << STLfileName
63 << " or file " << STLfileName + ".gz"
67 // Read the STL header
68 char header[STLheaderSize];
69 STLfile.read(header, STLheaderSize);
71 // Check that stream is OK, if not this maybe an ASCII file
77 // Read the number of triangles in the STl file
78 // (note: read as int so we can check whether >2^31)
80 STLfile.read(reinterpret_cast<char*>(&nTris), sizeof(unsigned int));
82 // Check that stream is OK and number of triangles is positive,
83 // if not this maybe an ASCII file
84 if (!STLfile || nTris < 0)
89 // Compare the size of the file with that expected from the number of tris
90 // If the comparison is not sensible then it maybe an ASCII file
93 label dataFileSize = Foam::fileSize(STLfileName) - 80;
95 if (nTris < dataFileSize/50 || nTris > dataFileSize/25)
101 // Everything OK so go ahead and read the triangles.
103 // Allocate storage for raw points
104 pointField rawPoints(3*nTris);
106 // Allocate storage for triangles
111 // Read the triangles
114 // Read an STL triangle
115 STLtriangle stlTri(STLfile);
117 // Set the rawPoints to the vertices of the STL triangle
118 // and set the point labels of the labelledTri
119 rawPoints[rawPointI] = stlTri.a();
120 operator[](i)[0] = rawPointI++;
122 rawPoints[rawPointI] = stlTri.b();
123 operator[](i)[1] = rawPointI++;
125 rawPoints[rawPointI] = stlTri.c();
126 operator[](i)[2] = rawPointI++;
128 operator[](i).region() = stlTri.region();
133 stitchTriangles(rawPoints);
139 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
141 } // End namespace Foam
143 // ************************************************************************* //