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) 2011-2016 OpenFOAM Foundation
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.H"
30 #include "IFstream.H"
31 #include "IStringStream.H"
32 
33 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
34 
35 bool Foam::triSurface::readOFF(const fileName& OFFfileName)
36 {
37  IFstream OFFfile(OFFfileName);
38 
39  if (!OFFfile.good())
40  {
42  << "Cannot read file " << OFFfileName
43  << exit(FatalError);
44  }
45 
46  // Read header
47  string hdr = getLineNoComment(OFFfile);
48  if (hdr != "OFF")
49  {
51  << "OFF file " << OFFfileName
52  << " does not start with 'OFF'"
53  << exit(FatalError);
54  }
55 
56 
57  label nPoints, nEdges, nElems;
58 
59  string line = getLineNoComment(OFFfile);
60  {
61  IStringStream lineStream(line);
62  lineStream >> nPoints >> nElems >> nEdges;
63  }
64 
65  // Read points
66  pointField points(nPoints);
67 
68  forAll(points, pointi)
69  {
70  scalar x, y, z;
71  line = getLineNoComment(OFFfile);
72  {
73  IStringStream lineStream(line);
74  lineStream >> x >> y >> z;
75  }
76  points[pointi] = point(x, y, z);
77  }
78 
79  // Read faces & triangulate them,
80  DynamicList<labelledTri> tris(nElems);
81 
82  for (label facei = 0; facei < nElems; facei++)
83  {
84  line = getLineNoComment(OFFfile);
85  {
86  IStringStream lineStream(line);
87 
88  label nVerts;
89  lineStream >> nVerts;
90 
91  face f(nVerts);
92 
93  forAll(f, fp)
94  {
95  lineStream >> f[fp];
96  }
97 
98  // Triangulate.
99  if (nVerts == 3)
100  {
101  tris.append(labelledTri(f[0], f[1], f[2], 0));
102  }
103  else if (nVerts == 4)
104  {
105  tris.append(labelledTri(f[0], f[1], f[2], 0));
106  tris.append(labelledTri(f[2], f[3], f[0], 0));
107  }
108  else
109  {
110  faceList triFaces(f.nTriangles(points));
111 
112  label nTri = 0;
113 
114  f.triangles(points, nTri, triFaces);
115 
116  forAll(triFaces, triFacei)
117  {
118  const face& f = triFaces[triFacei];
119 
120  tris.append(labelledTri(f[0], f[1], f[2], 0));
121  }
122  }
123  }
124  }
125 
126  tris.shrink();
127 
128  *this = triSurface(tris, points);
129 
130  return true;
131 }
132 
133 
134 // ************************************************************************* //
label nPoints() const
Return number of points supporting patch faces.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
List< face > faceList
Definition: faceListFwd.H:43
const Field< point > & points() const
Return reference to global points.
scalar y
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
labelList f(nPoints)
label nEdges() const
Return number of edges in patch.
triSurface()
Construct null.
Definition: triSurface.C:594
vector point
Point is a vector.
Definition: point.H:41