1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2008 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)
50 // If the file is compressed, decompress it before reading.
51 if (!STLfilePtr->good() && file(STLfileName + ".gz"))
54 STLfilePtr.reset(new igzstream((STLfileName + ".gz").c_str()));
56 istream& STLfile = STLfilePtr();
60 FatalErrorIn("triSurface::readSTLBINARY(const fileName&)")
61 << "Cannot read file " << STLfileName
62 << " or file " << STLfileName + ".gz"
66 // Read the STL header
67 char header[STLheaderSize];
68 STLfile.read(header, STLheaderSize);
70 // Check that stream is OK, if not this maybe an ASCII file
76 // Read the number of triangles in the STl file
77 // (note: read as int so we can check whether >2^31)
79 STLfile.read(reinterpret_cast<char*>(&nTris), sizeof(unsigned int));
81 // Check that stream is OK and number of triangles is positive,
82 // if not this maybe an ASCII file
83 if (!STLfile || nTris < 0)
88 // Compare the size of the file with that expected from the number of tris
89 // If the comparison is not sensible then it maybe an ASCII file
92 label triDataSize = Foam::size(STLfileName) - 80;
94 if (nTris < triDataSize/50 || nTris > triDataSize/25)
100 // Everything OK so go ahead and read the triangles.
102 // Allocate storage for raw points
103 pointField rawPoints(3*nTris);
105 // Allocate storage for triangles
110 // Read the triangles
113 // Read an STL triangle
114 STLtriangle stlTri(STLfile);
116 // Set the rawPoints to the vertices of the STL triangle
117 // and set the point labels of the labelledTri
118 rawPoints[rawPointI] = stlTri.a();
119 operator[](i)[0] = rawPointI++;
121 rawPoints[rawPointI] = stlTri.b();
122 operator[](i)[1] = rawPointI++;
124 rawPoints[rawPointI] = stlTri.c();
125 operator[](i)[2] = rawPointI++;
127 operator[](i).region() = stlTri.region();
132 stitchTriangles(rawPoints);
138 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
140 } // End namespace Foam
142 // ************************************************************************* //