initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / triSurface / triSurface / interfaces / STL / writeSTL.C
blob110679f0b0eea1e00b514071621efc6935fe6c7a
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 \*---------------------------------------------------------------------------*/
27 #include "triSurface.H"
28 #include "STLtriangle.H"
29 #include "primitivePatch.H"
30 #include "HashTable.H"
31 #include "hashSignedLabel.H"
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 namespace Foam
38 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
40 void triSurface::writeSTLASCII(Ostream& os) const
42     labelList faceMap;
44     surfacePatchList myPatches(calcPatches(faceMap));
46     label faceIndex = 0;
47     forAll(myPatches, patchI)
48     {
49         // Print all faces belonging to this region
50         const surfacePatch& patch = myPatches[patchI];
52         os << "solid " << patch.name() << endl;
54         for
55         (
56             label patchFaceI = 0;
57             patchFaceI < patch.size();
58             patchFaceI++
59         )
60         {
61             const label faceI = faceMap[faceIndex++];
63             const vector& n = faceNormals()[faceI];
65             os  << "  facet normal "
66                 << n.x() << ' ' << n.y() << ' ' << n.z() << endl;
67             os  << "    outer loop" << endl;
69             const labelledTri& f = (*this)[faceI];
70             const point& pa = points()[f[0]];
71             const point& pb = points()[f[1]];
72             const point& pc = points()[f[2]];
74             os  << "       vertex "
75                 << pa.x() << ' ' << pa.y() << ' ' << pa.z() << endl;
76             os  << "       vertex "
77                 << pb.x() << ' ' << pb.y() << ' ' << pb.z() << endl;
78             os  << "       vertex "
79                 << pc.x() << ' ' << pc.y() << ' ' << pc.z() << endl;
80             os
81                 << "    endloop" << endl;
82             os
83                 << "  endfacet" << endl;
84         }
86         os << "endsolid " << patch.name() << endl;
87     }
91 void triSurface::writeSTLBINARY(std::ostream& os) const
93     // Write the STL header
94     string header("Foam binary STL", STLheaderSize);
95     os.write(header.c_str(), STLheaderSize);
97     label nTris = size();
98     os.write(reinterpret_cast<char*>(&nTris), sizeof(unsigned int));
100     const vectorField& normals = faceNormals();
102     forAll(*this, faceI)
103     {
104         const labelledTri& f = (*this)[faceI];
106         // Convert vector into STL single precision
107         STLpoint n(normals[faceI]);
108         STLpoint pa(points()[f[0]]);
109         STLpoint pb(points()[f[1]]);
110         STLpoint pc(points()[f[2]]);
112         STLtriangle stlTri(n, pa, pb, pc, f.region());
114         stlTri.write(os);
115     }
119 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
121 } // End namespace Foam
123 // ************************************************************************* //