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

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