readSTLBINARY.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | foam-extend: Open Source CFD
4  \\ / O peration | Version: 3.2
5  \\ / A nd | Web: http://www.foam-extend.org
6  \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9  This file is part of foam-extend.
10 
11  foam-extend 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 3 of the License, or (at your
14  option) any later version.
15 
16  foam-extend is distributed in the hope that it will be useful, but
17  WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
23 
24 Description
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "triSurface.H"
29 #include "STLtriangle.H"
30 #include "IFstream.H"
31 #include "OSspecific.H"
32 #include "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 // ************************************************************************* //
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::triSurface::readSTLBINARY
bool readSTLBINARY(const fileName &)
Definition: readSTLBINARY.C:41
Foam::triSurface::STLheaderSize
static const int STLheaderSize
The number of bytes in the STL header.
Definition: triSurface.H:79
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::STLtriangle::region
unsigned short region() const
Definition: STLtriangleI.H:82
STLtriangle.H
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::Field
Pre-declare SubField and related Field type.
Definition: Field.H:57
IFstream.H
Foam::FatalError
error FatalError
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::isFile
bool isFile(const fileName &, const bool checkGzip=true)
Does the name exist as a FILE in the file system?
Definition: POSIX.C:622
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::List< labelledTri >::setSize
void setSize(const label)
Reset size of List.
Foam::autoPtr
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
Foam::STLtriangle::b
const STLpoint & b() const
Definition: STLtriangleI.H:72
Foam::STLtriangle::c
const STLpoint & c() const
Definition: STLtriangleI.H:77
Foam::STLtriangle
A triangle representation for STL files.
Definition: STLtriangle.H:48
Foam::triSurface::stitchTriangles
bool stitchTriangles(const pointField &rawPoints, const scalar tol=SMALL, const bool verbose=false)
Function to stitch the triangles by removing duplicate points.
Definition: stitchTriangles.C:38
Foam::autoPtr::reset
void reset(T *=0)
If object pointer already set, delete object and set to given.
Definition: autoPtrI.H:114
FatalErrorIn
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:313
Foam::fileSize
off_t fileSize(const fileName &)
Return size of file.
Definition: POSIX.C:629
Foam::STLtriangle::a
const STLpoint & a() const
Definition: STLtriangleI.H:67