FreeFOAM The Cross-Platform CFD Toolkit
writeSTL.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 \*---------------------------------------------------------------------------*/
25 
26 #include <triSurface/triSurface.H>
27 #include "STLtriangle.H"
29 #include <OpenFOAM/HashTable.H>
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
38 
39 void triSurface::writeSTLASCII(Ostream& os) const
40 {
41  labelList faceMap;
42 
43  surfacePatchList myPatches(calcPatches(faceMap));
44 
45  label faceIndex = 0;
46  forAll(myPatches, patchI)
47  {
48  // Print all faces belonging to this region
49  const surfacePatch& patch = myPatches[patchI];
50 
51  os << "solid " << patch.name() << endl;
52 
53  for
54  (
55  label patchFaceI = 0;
56  patchFaceI < patch.size();
57  patchFaceI++
58  )
59  {
60  const label faceI = faceMap[faceIndex++];
61 
62  const vector& n = faceNormals()[faceI];
63 
64  os << " facet normal "
65  << n.x() << ' ' << n.y() << ' ' << n.z() << endl;
66  os << " outer loop" << endl;
67 
68  const labelledTri& f = (*this)[faceI];
69  const point& pa = points()[f[0]];
70  const point& pb = points()[f[1]];
71  const point& pc = points()[f[2]];
72 
73  os << " vertex "
74  << pa.x() << ' ' << pa.y() << ' ' << pa.z() << endl;
75  os << " vertex "
76  << pb.x() << ' ' << pb.y() << ' ' << pb.z() << endl;
77  os << " vertex "
78  << pc.x() << ' ' << pc.y() << ' ' << pc.z() << endl;
79  os
80  << " endloop" << endl;
81  os
82  << " endfacet" << endl;
83  }
84 
85  os << "endsolid " << patch.name() << endl;
86  }
87 }
88 
89 
90 void triSurface::writeSTLBINARY(std::ostream& os) const
91 {
92  // Write the STL header
93  string header("Foam binary STL", STLheaderSize);
94  os.write(header.c_str(), STLheaderSize);
95 
96  label nTris = size();
97  os.write(reinterpret_cast<char*>(&nTris), sizeof(unsigned int));
98 
99  const vectorField& normals = faceNormals();
100 
101  forAll(*this, faceI)
102  {
103  const labelledTri& f = (*this)[faceI];
104 
105  // Convert vector into STL single precision
106  STLpoint n(normals[faceI]);
107  STLpoint pa(points()[f[0]]);
108  STLpoint pb(points()[f[1]]);
109  STLpoint pc(points()[f[2]]);
110 
111  STLtriangle stlTri(n, pa, pb, pc, f.region());
112 
113  stlTri.write(os);
114  }
115 }
116 
117 
118 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
119 
120 } // End namespace Foam
121 
122 // ************************ vim: set sw=4 sts=4 et: ************************ //