OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

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-2009 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 the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     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, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "triSurface.H"
00028 #include "STLtriangle.H"
00029 #include "primitivePatch.H"
00030 #include "HashTable.H"
00031 #include "hashSignedLabel.H"
00032 
00033 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00034 
00035 namespace Foam
00036 {
00037 
00038 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00039 
00040 void triSurface::writeSTLASCII(Ostream& os) const
00041 {
00042     labelList faceMap;
00043 
00044     surfacePatchList myPatches(calcPatches(faceMap));
00045 
00046     label faceIndex = 0;
00047     forAll(myPatches, patchI)
00048     {
00049         // Print all faces belonging to this region
00050         const surfacePatch& patch = myPatches[patchI];
00051 
00052         os << "solid " << patch.name() << endl;
00053 
00054         for
00055         (
00056             label patchFaceI = 0;
00057             patchFaceI < patch.size();
00058             patchFaceI++
00059         )
00060         {
00061             const label faceI = faceMap[faceIndex++];
00062 
00063             const vector& n = faceNormals()[faceI];
00064 
00065             os  << "  facet normal "
00066                 << n.x() << ' ' << n.y() << ' ' << n.z() << endl;
00067             os  << "    outer loop" << endl;
00068 
00069             const labelledTri& f = (*this)[faceI];
00070             const point& pa = points()[f[0]];
00071             const point& pb = points()[f[1]];
00072             const point& pc = points()[f[2]];
00073 
00074             os  << "       vertex "
00075                 << pa.x() << ' ' << pa.y() << ' ' << pa.z() << endl;
00076             os  << "       vertex "
00077                 << pb.x() << ' ' << pb.y() << ' ' << pb.z() << endl;
00078             os  << "       vertex "
00079                 << pc.x() << ' ' << pc.y() << ' ' << pc.z() << endl;
00080             os
00081                 << "    endloop" << endl;
00082             os
00083                 << "  endfacet" << endl;
00084         }
00085 
00086         os << "endsolid " << patch.name() << endl;
00087     }
00088 }
00089 
00090 
00091 void triSurface::writeSTLBINARY(std::ostream& os) const
00092 {
00093     // Write the STL header
00094     string header("Foam binary STL", STLheaderSize);
00095     os.write(header.c_str(), STLheaderSize);
00096 
00097     label nTris = size();
00098     os.write(reinterpret_cast<char*>(&nTris), sizeof(unsigned int));
00099 
00100     const vectorField& normals = faceNormals();
00101 
00102     forAll(*this, faceI)
00103     {
00104         const labelledTri& f = (*this)[faceI];
00105 
00106         // Convert vector into STL single precision
00107         STLpoint n(normals[faceI]);
00108         STLpoint pa(points()[f[0]]);
00109         STLpoint pb(points()[f[1]]);
00110         STLpoint pc(points()[f[2]]);
00111 
00112         STLtriangle stlTri(n, pa, pb, pc, f.region());
00113 
00114         stlTri.write(os);
00115     }
00116 }
00117 
00118 
00119 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00120 
00121 } // End namespace Foam
00122 
00123 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd