FreeFOAM The Cross-Platform CFD Toolkit
readSTLBINARY.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Description
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include <triSurface/triSurface.H>
29 #include "STLtriangle.H"
30 #include <OpenFOAM/IFstream.H>
31 #include <OpenFOAM/OSspecific.H>
32 #include <OpenFOAM/gzstream.h>
33 
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38 
39 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
40 
41 bool triSurface::readSTLBINARY(const fileName& STLfileName)
42 {
43  bool compressed = false;
44 
45  autoPtr<istream> STLfilePtr
46  (
47  new ifstream(STLfileName.c_str(), std::ios::binary)
48  );
49 
50  // If the file is compressed, decompress it before reading.
51  if (!STLfilePtr->good() && isFile(STLfileName + ".gz", false))
52  {
53  compressed = true;
54  STLfilePtr.reset(new igzstream((STLfileName + ".gz").c_str()));
55  }
56  istream& STLfile = STLfilePtr();
57 
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  }
65 
66  // Read the STL header
67  char header[STLheaderSize];
68  STLfile.read(header, STLheaderSize);
69 
70  // Check that stream is OK, if not this maybe an ASCII file
71  if (!STLfile)
72  {
73  return false;
74  }
75 
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));
80 
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  }
87 
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 dataFileSize = Foam::fileSize(STLfileName) - 80;
93 
94  if (nTris < dataFileSize/50 || nTris > dataFileSize/25)
95  {
96  return false;
97  }
98  }
99 
100  // Everything OK so go ahead and read the triangles.
101 
102  // Allocate storage for raw points
103  pointField rawPoints(3*nTris);
104 
105  // Allocate storage for triangles
106  setSize(nTris);
107 
108  label rawPointI = 0;
109 
110  // Read the triangles
111  forAll(*this, i)
112  {
113  // Read an STL triangle
114  STLtriangle stlTri(STLfile);
115 
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++;
120 
121  rawPoints[rawPointI] = stlTri.b();
122  operator[](i)[1] = rawPointI++;
123 
124  rawPoints[rawPointI] = stlTri.c();
125  operator[](i)[2] = rawPointI++;
126 
127  operator[](i).region() = stlTri.region();
128  }
129 
130  //STLfile.close();
131 
132  stitchTriangles(rawPoints);
133 
134  return true;
135 }
136 
137 
138 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
139 
140 } // End namespace Foam
141 
142 // ************************ vim: set sw=4 sts=4 et: ************************ //