initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / triSurface / triSurface / interfaces / OFF / readOFF.C
blob6f00389615af96ab2df85dd0beeb8220466eb084
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Description
26     Geomview OFF polyList format. Does triangulation.
28 \*---------------------------------------------------------------------------*/
30 #include "triSurface.H"
31 #include "IFstream.H"
32 #include "IStringStream.H"
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 namespace Foam
39 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
41 bool triSurface::readOFF(const fileName& OFFfileName)
43     IFstream OFFfile(OFFfileName);
45     if (!OFFfile.good())
46     {
47         FatalErrorIn("triSurface::readOFF(const fileName&)")
48             << "Cannot read file " << OFFfileName
49             << exit(FatalError);
50     }
52     // Read header
53     string hdr = getLineNoComment(OFFfile);
54     if (hdr != "OFF")
55     {
56         FatalErrorIn("triSurface::readOFF(const fileName&)")
57             << "OFF file " << OFFfileName
58             << " does not start with 'OFF'"
59             << exit(FatalError);
60     }
63     label nPoints, nEdges, nElems;
65     string line = getLineNoComment(OFFfile);
66     {
67         IStringStream lineStream(line);
68         lineStream >> nPoints >> nElems >> nEdges;
69     }
71     // Read points
72     pointField points(nPoints);
74     forAll(points, pointi)
75     {
76         scalar x, y, z;
77         line = getLineNoComment(OFFfile);
78         {
79             IStringStream lineStream(line);
80             lineStream >> x >> y >> z;
81         }
82         points[pointi] = point(x, y, z);
83     }
85     // Read faces & triangulate them,
86     DynamicList<labelledTri> tris(nElems);
88     for (label faceI = 0; faceI < nElems; faceI++)
89     {
90         line = getLineNoComment(OFFfile);
91         {
92             IStringStream lineStream(line);
94             label nVerts;
95             lineStream >> nVerts;
97             face f(nVerts);
99             forAll(f, fp)
100             {
101                 lineStream >> f[fp];
102             }
104             // Triangulate.
105             if (nVerts == 3)
106             {
107                 tris.append(labelledTri(f[0], f[1], f[2], 0));
108             }
109             else if (nVerts == 4)
110             {
111                 tris.append(labelledTri(f[0], f[1], f[2], 0));
112                 tris.append(labelledTri(f[2], f[3], f[0], 0));
113             }
114             else
115             {
116                 faceList triFaces(f.nTriangles(points));
118                 label nTri = 0;
120                 f.triangles(points, nTri, triFaces);
122                 forAll(triFaces, triFaceI)
123                 {
124                     const face& f = triFaces[triFaceI];
126                     tris.append(labelledTri(f[0], f[1], f[2], 0));
127                 }
128             }
129         }
130     }
132     tris.shrink();
134     *this = triSurface(tris, points);
136     return true;
140 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
142 } // End namespace Foam
144 // ************************************************************************* //