FreeFOAM The Cross-Platform CFD Toolkit
readOFF.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Description
25  Geomview OFF polyList format. Does triangulation.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include <triSurface/triSurface.H>
30 #include <OpenFOAM/IFstream.H>
31 #include <OpenFOAM/IStringStream.H>
32 
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 
38 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
39 
40 bool triSurface::readOFF(const fileName& OFFfileName)
41 {
42  IFstream OFFfile(OFFfileName);
43 
44  if (!OFFfile.good())
45  {
46  FatalErrorIn("triSurface::readOFF(const fileName&)")
47  << "Cannot read file " << OFFfileName
48  << exit(FatalError);
49  }
50 
51  // Read header
52  string hdr = getLineNoComment(OFFfile);
53  if (hdr != "OFF")
54  {
55  FatalErrorIn("triSurface::readOFF(const fileName&)")
56  << "OFF file " << OFFfileName
57  << " does not start with 'OFF'"
58  << exit(FatalError);
59  }
60 
61 
62  label nPoints, nEdges, nElems;
63 
64  string line = getLineNoComment(OFFfile);
65  {
66  IStringStream lineStream(line);
67  lineStream >> nPoints >> nElems >> nEdges;
68  }
69 
70  // Read points
71  pointField points(nPoints);
72 
73  forAll(points, pointi)
74  {
75  scalar x, y, z;
76  line = getLineNoComment(OFFfile);
77  {
78  IStringStream lineStream(line);
79  lineStream >> x >> y >> z;
80  }
81  points[pointi] = point(x, y, z);
82  }
83 
84  // Read faces & triangulate them,
85  DynamicList<labelledTri> tris(nElems);
86 
87  for (label faceI = 0; faceI < nElems; faceI++)
88  {
89  line = getLineNoComment(OFFfile);
90  {
91  IStringStream lineStream(line);
92 
93  label nVerts;
94  lineStream >> nVerts;
95 
96  face f(nVerts);
97 
98  forAll(f, fp)
99  {
100  lineStream >> f[fp];
101  }
102 
103  // Triangulate.
104  if (nVerts == 3)
105  {
106  tris.append(labelledTri(f[0], f[1], f[2], 0));
107  }
108  else if (nVerts == 4)
109  {
110  tris.append(labelledTri(f[0], f[1], f[2], 0));
111  tris.append(labelledTri(f[2], f[3], f[0], 0));
112  }
113  else
114  {
115  faceList triFaces(f.nTriangles(points));
116 
117  label nTri = 0;
118 
119  f.triangles(points, nTri, triFaces);
120 
121  forAll(triFaces, triFaceI)
122  {
123  const face& f = triFaces[triFaceI];
124 
125  tris.append(labelledTri(f[0], f[1], f[2], 0));
126  }
127  }
128  }
129  }
130 
131  tris.shrink();
132 
133  *this = triSurface(tris, points);
134 
135  return true;
136 }
137 
138 
139 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
140 
141 } // End namespace Foam
142 
143 // ************************ vim: set sw=4 sts=4 et: ************************ //