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

readOFF.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 Description
00025     Geomview OFF polyList format. Does triangulation.
00026 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include <triSurface/triSurface.H>
00030 #include <OpenFOAM/IFstream.H>
00031 #include <OpenFOAM/IStringStream.H>
00032 
00033 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00034 
00035 namespace Foam
00036 {
00037 
00038 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00039 
00040 bool triSurface::readOFF(const fileName& OFFfileName)
00041 {
00042     IFstream OFFfile(OFFfileName);
00043 
00044     if (!OFFfile.good())
00045     {
00046         FatalErrorIn("triSurface::readOFF(const fileName&)")
00047             << "Cannot read file " << OFFfileName
00048             << exit(FatalError);
00049     }
00050 
00051     // Read header
00052     string hdr = getLineNoComment(OFFfile);
00053     if (hdr != "OFF")
00054     {
00055         FatalErrorIn("triSurface::readOFF(const fileName&)")
00056             << "OFF file " << OFFfileName
00057             << " does not start with 'OFF'"
00058             << exit(FatalError);
00059     }
00060 
00061 
00062     label nPoints, nEdges, nElems;
00063 
00064     string line = getLineNoComment(OFFfile);
00065     {
00066         IStringStream lineStream(line);
00067         lineStream >> nPoints >> nElems >> nEdges;
00068     }
00069 
00070     // Read points
00071     pointField points(nPoints);
00072 
00073     forAll(points, pointi)
00074     {
00075         scalar x, y, z;
00076         line = getLineNoComment(OFFfile);
00077         {
00078             IStringStream lineStream(line);
00079             lineStream >> x >> y >> z;
00080         }
00081         points[pointi] = point(x, y, z);
00082     }
00083 
00084     // Read faces & triangulate them,
00085     DynamicList<labelledTri> tris(nElems);
00086 
00087     for (label faceI = 0; faceI < nElems; faceI++)
00088     {
00089         line = getLineNoComment(OFFfile);
00090         {
00091             IStringStream lineStream(line);
00092 
00093             label nVerts;
00094             lineStream >> nVerts;
00095 
00096             face f(nVerts);
00097 
00098             forAll(f, fp)
00099             {
00100                 lineStream >> f[fp];
00101             }
00102 
00103             // Triangulate.
00104             if (nVerts == 3)
00105             {
00106                 tris.append(labelledTri(f[0], f[1], f[2], 0));
00107             }
00108             else if (nVerts == 4)
00109             {
00110                 tris.append(labelledTri(f[0], f[1], f[2], 0));
00111                 tris.append(labelledTri(f[2], f[3], f[0], 0));
00112             }
00113             else
00114             {
00115                 faceList triFaces(f.nTriangles(points));
00116 
00117                 label nTri = 0;
00118 
00119                 f.triangles(points, nTri, triFaces);
00120 
00121                 forAll(triFaces, triFaceI)
00122                 {
00123                     const face& f = triFaces[triFaceI];
00124 
00125                     tris.append(labelledTri(f[0], f[1], f[2], 0));
00126                 }
00127             }
00128         }
00129     }
00130 
00131     tris.shrink();
00132 
00133     *this = triSurface(tris, points);
00134 
00135     return true;
00136 }
00137 
00138 
00139 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00140 
00141 } // End namespace Foam
00142 
00143 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines