initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / triSurface / triSurface / interfaces / STL / readSTLBINARY.C
blobf2a615fe9ff5d35bf86e9bcba3a1980d9e039480
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 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     );
51     // If the file is compressed, decompress it before reading.
52     if (!STLfilePtr->good() && isFile(STLfileName + ".gz", false))
53     {
54         compressed = true;
55         STLfilePtr.reset(new igzstream((STLfileName + ".gz").c_str()));
56     }
57     istream& STLfile = STLfilePtr();
59     if (!STLfile.good())
60     {
61         FatalErrorIn("triSurface::readSTLBINARY(const fileName&)")
62             << "Cannot read file " << STLfileName
63             << " or file " << STLfileName + ".gz"
64             << exit(FatalError);
65     }
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
72     if (!STLfile)
73     {
74         return false;
75     }
77     // Read the number of triangles in the STl file
78     // (note: read as int so we can check whether >2^31)
79     int nTris;
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)
85     {
86         return false;
87     }
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
91     if (!compressed)
92     {
93         label dataFileSize = Foam::fileSize(STLfileName) - 80;
95         if (nTris < dataFileSize/50 || nTris > dataFileSize/25)
96         {
97             return false;
98         }
99     }
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
107     setSize(nTris);
109     label rawPointI = 0;
111     // Read the triangles
112     forAll(*this, i)
113     {
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();
129     }
131     //STLfile.close();
133     stitchTriangles(rawPoints);
135     return true;
139 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
141 } // End namespace Foam
143 // ************************************************************************* //