readOFF.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2021 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 "polygonTriangulate.H"
31 #include "IFstream.H"
32 #include "IStringStream.H"
33 
34 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
35 
36 bool Foam::triSurface::readOFF(const fileName& OFFfileName)
37 {
38  IFstream OFFfile(OFFfileName);
39 
40  if (!OFFfile.good())
41  {
43  << "Cannot read file " << OFFfileName
44  << exit(FatalError);
45  }
46 
47  // Read header
48  string hdr = getLineNoComment(OFFfile);
49  if (hdr != "OFF")
50  {
52  << "OFF file " << OFFfileName
53  << " does not start with 'OFF'"
54  << exit(FatalError);
55  }
56 
57 
58  label nPoints, nEdges, nElems;
59 
60  string line = getLineNoComment(OFFfile);
61  {
62  IStringStream lineStream(line);
63  lineStream >> nPoints >> nElems >> nEdges;
64  }
65 
66  // Read points
68 
69  forAll(points, pointi)
70  {
71  scalar x, y, z;
72  line = getLineNoComment(OFFfile);
73  {
74  IStringStream lineStream(line);
75  lineStream >> x >> y >> z;
76  }
77  points[pointi] = point(x, y, z);
78  }
79 
80  // Create a triangulation engine
81  polygonTriangulate triEngine;
82 
83  // Read faces & triangulate them,
84  DynamicList<labelledTri> tris(nElems);
85 
86  for (label facei = 0; facei < nElems; facei++)
87  {
88  line = getLineNoComment(OFFfile);
89  {
90  IStringStream lineStream(line);
91 
92  label nVerts;
93  lineStream >> nVerts;
94 
95  face f(nVerts);
96 
97  forAll(f, fp)
98  {
99  lineStream >> f[fp];
100  }
101 
102  // Triangulate.
103  if (nVerts == 3)
104  {
105  tris.append(labelledTri(f[0], f[1], f[2], 0));
106  }
107  else if (nVerts == 4)
108  {
109  tris.append(labelledTri(f[0], f[1], f[2], 0));
110  tris.append(labelledTri(f[2], f[3], f[0], 0));
111  }
112  else
113  {
114  triEngine.triangulate(UIndirectList<point>(points, f));
115 
116  forAll(triEngine.triPoints(), trii)
117  {
118  tris.append(labelledTri(triEngine.triPoints(trii, f), 0));
119  }
120  }
121  }
122  }
123 
124  tris.shrink();
125 
126  *this = triSurface(tris, points);
127 
128  return true;
129 }
130 
131 
132 // ************************************************************************* //
scalar y
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
label nEdges() const
Return number of edges in patch.
label nPoints() const
Return number of points supporting patch faces.
const Field< PointType > & points() const
Return reference to global points.
triSurface()
Construct null.
Definition: triSurface.C:535
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
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
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
vector point
Point is a vector.
Definition: point.H:41
error FatalError
labelList f(nPoints)