initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / triSurface / triSurface / interfaces / STL / readSTLBINARY.C
blobf6da67f41b1b0e7866cfdc45aaa516bcad53adc7
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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
25 Description
27 \*---------------------------------------------------------------------------*/
29 #include "triSurface.H"
30 #include "STLtriangle.H"
31 #include "IFstream.H"
32 #include "OSspecific.H"
33 #include "gzstream.h"
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 namespace Foam
40 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
42 bool triSurface::readSTLBINARY(const fileName& STLfileName)
44     bool compressed = false;
46     autoPtr<istream> STLfilePtr
47     (
48         new ifstream(STLfileName.c_str(), std::ios::binary)
49     );
50     // If the file is compressed, decompress it before reading.
51     if (!STLfilePtr->good() && file(STLfileName + ".gz"))
52     {
53         compressed = true;
54         STLfilePtr.reset(new igzstream((STLfileName + ".gz").c_str()));
55     }
56     istream& STLfile = STLfilePtr();
58     if (!STLfile.good())
59     {
60         FatalErrorIn("triSurface::readSTLBINARY(const fileName&)")
61             << "Cannot read file " << STLfileName
62             << " or file " << STLfileName + ".gz"
63             << exit(FatalError);
64     }
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
71     if (!STLfile)
72     {
73         return false;
74     }
76     // Read the number of triangles in the STl file
77     // (note: read as int so we can check whether >2^31)
78     int nTris;
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)
84     {
85         return false;
86     }
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
90     if (!compressed)
91     {
92         label triDataSize = Foam::size(STLfileName) - 80;
94         if (nTris < triDataSize/50 || nTris > triDataSize/25)
95         {
96             return false;
97         }
98     }
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
106     setSize(nTris);
108     label rawPointI = 0;
110     // Read the triangles
111     forAll(*this, i)
112     {
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();
128     }
130     //STLfile.close();
132     stitchTriangles(rawPoints);
134     return true;
138 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
140 } // End namespace Foam
142 // ************************************************************************* //