FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

writeSTL.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software: you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include <triSurface/triSurface.H>
00027 #include "STLtriangle.H"
00028 #include <OpenFOAM/primitivePatch.H>
00029 #include <OpenFOAM/HashTable.H>
00030 #include <triSurface/hashSignedLabel.H>
00031 
00032 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00033 
00034 namespace Foam
00035 {
00036 
00037 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00038 
00039 void triSurface::writeSTLASCII(Ostream& os) const
00040 {
00041     labelList faceMap;
00042 
00043     surfacePatchList myPatches(calcPatches(faceMap));
00044 
00045     label faceIndex = 0;
00046     forAll(myPatches, patchI)
00047     {
00048         // Print all faces belonging to this region
00049         const surfacePatch& patch = myPatches[patchI];
00050 
00051         os << "solid " << patch.name() << endl;
00052 
00053         for
00054         (
00055             label patchFaceI = 0;
00056             patchFaceI < patch.size();
00057             patchFaceI++
00058         )
00059         {
00060             const label faceI = faceMap[faceIndex++];
00061 
00062             const vector& n = faceNormals()[faceI];
00063 
00064             os  << "  facet normal "
00065                 << n.x() << ' ' << n.y() << ' ' << n.z() << endl;
00066             os  << "    outer loop" << endl;
00067 
00068             const labelledTri& f = (*this)[faceI];
00069             const point& pa = points()[f[0]];
00070             const point& pb = points()[f[1]];
00071             const point& pc = points()[f[2]];
00072 
00073             os  << "       vertex "
00074                 << pa.x() << ' ' << pa.y() << ' ' << pa.z() << endl;
00075             os  << "       vertex "
00076                 << pb.x() << ' ' << pb.y() << ' ' << pb.z() << endl;
00077             os  << "       vertex "
00078                 << pc.x() << ' ' << pc.y() << ' ' << pc.z() << endl;
00079             os
00080                 << "    endloop" << endl;
00081             os
00082                 << "  endfacet" << endl;
00083         }
00084 
00085         os << "endsolid " << patch.name() << endl;
00086     }
00087 }
00088 
00089 
00090 void triSurface::writeSTLBINARY(std::ostream& os) const
00091 {
00092     // Write the STL header
00093     string header("Foam binary STL", STLheaderSize);
00094     os.write(header.c_str(), STLheaderSize);
00095 
00096     label nTris = size();
00097     os.write(reinterpret_cast<char*>(&nTris), sizeof(unsigned int));
00098 
00099     const vectorField& normals = faceNormals();
00100 
00101     forAll(*this, faceI)
00102     {
00103         const labelledTri& f = (*this)[faceI];
00104 
00105         // Convert vector into STL single precision
00106         STLpoint n(normals[faceI]);
00107         STLpoint pa(points()[f[0]]);
00108         STLpoint pb(points()[f[1]]);
00109         STLpoint pc(points()[f[2]]);
00110 
00111         STLtriangle stlTri(n, pa, pb, pc, f.region());
00112 
00113         stlTri.write(os);
00114     }
00115 }
00116 
00117 
00118 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00119 
00120 } // End namespace Foam
00121 
00122 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines