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

writeOBJ.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 Description
00026     Lightwave OBJ format.
00027 
00028     Note: Java obj loader does not support '#' on line
00029 
00030 \*---------------------------------------------------------------------------*/
00031 
00032 #include "triSurface.H"
00033 
00034 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00035 
00036 namespace Foam
00037 {
00038 
00039 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00040 
00041 void triSurface::writeOBJ(const bool writeSorted, Ostream& os) const
00042 {
00043     // Write header
00044     os  << "# Wavefront OBJ file" << endl
00045         << "# Regions:" << endl;
00046 
00047     labelList faceMap;
00048 
00049     surfacePatchList myPatches(calcPatches(faceMap));
00050 
00051     const pointField& ps = points();
00052 
00053     // Print patch names as comment
00054     forAll(myPatches, patchI)
00055     {
00056         os  << "#     " << patchI << "    "
00057             << myPatches[patchI].name() << endl;
00058     }
00059     os  << "#" << endl;
00060 
00061     os  << "# points    : " << ps.size() << endl
00062         << "# triangles : " << size() << endl
00063         << "#" << endl;
00064 
00065 
00066     // Write vertex coords
00067     forAll(ps, pointi)
00068     {
00069         os  << "v "
00070             << ps[pointi].x() << ' '
00071             << ps[pointi].y() << ' '
00072             << ps[pointi].z() << endl;
00073     }
00074 
00075     if (writeSorted)
00076     {
00077         label faceIndex = 0;
00078 
00079         forAll(myPatches, patchI)
00080         {
00081             // Print all faces belonging to this patch
00082 
00083             os << "g " << myPatches[patchI].name() << endl;
00084 
00085             for
00086             (
00087                 label patchFaceI = 0;
00088                 patchFaceI < myPatches[patchI].size();
00089                 patchFaceI++
00090             )
00091             {
00092                 const label faceI = faceMap[faceIndex++];
00093 
00094                 os  << "f "
00095                     << operator[](faceI)[0] + 1 << ' '
00096                     << operator[](faceI)[1] + 1 << ' '
00097                     << operator[](faceI)[2] + 1
00098                     //<< "  # " << operator[](faceI).region()
00099                     << endl;
00100             }
00101         }
00102     }
00103     else
00104     {
00105         forAll(*this, faceI)
00106         {
00107             os  << "f "
00108                 << operator[](faceI)[0] + 1 << ' '
00109                 << operator[](faceI)[1] + 1 << ' '
00110                 << operator[](faceI)[2] + 1
00111                 //<< "  # " << operator[](faceI).region()
00112                 << endl;
00113         }
00114     }
00115 }
00116 
00117 
00118 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00119 
00120 } // End namespace Foam
00121 
00122 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd