readOFF.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | foam-extend: Open Source CFD
4  \\ / O peration | Version: 3.2
5  \\ / A nd | Web: http://www.foam-extend.org
6  \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9  This file is part of foam-extend.
10 
11  foam-extend 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 3 of the License, or (at your
14  option) any later version.
15 
16  foam-extend is distributed in the hope that it will be useful, but
17  WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with foam-extend. 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 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
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
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 // ************************************************************************* //
Foam::PrimitivePatch< labelledTri, List, pointField, point >::points
const Field< point > & points() const
Return reference to global points.
Definition: PrimitivePatchTemplate.H:282
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::IFstream
Input from file stream.
Definition: IFstream.H:81
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:56
Foam::PrimitivePatch< labelledTri, List, pointField, point >::nEdges
label nEdges() const
Return number of edges in patch.
Definition: PrimitivePatchTemplate.H:299
IStringStream.H
Foam::label
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
Foam::Field
Pre-declare SubField and related Field type.
Definition: Field.H:57
Foam::triSurface::triSurface
triSurface()
Construct null.
Definition: triSurface.C:608
Foam::DynamicList::shrink
DynamicList< T, SizeInc, SizeMult, SizeDiv > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:258
IFstream.H
Foam::PrimitivePatch< labelledTri, List, pointField, point >::nPoints
label nPoints() const
Return number of points supporting patch faces.
Definition: PrimitivePatchTemplate.H:293
Foam::FatalError
error FatalError
Foam::triSurface::readOFF
bool readOFF(const fileName &)
Definition: readOFF.C:40
Foam::IStringStream
Input from memory buffer stream.
Definition: IStringStream.H:49
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::DynamicList::append
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
f
labelList f(nPoints)
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::line
A line primitive.
Definition: line.H:56
Foam::labelledTri
Triangle with additional region number.
Definition: labelledTri.H:49
FatalErrorIn
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:313
Foam::face
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:75
Foam::point
vector point
Point is a vector.
Definition: point.H:41
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Foam::triSurface::getLineNoComment
static string getLineNoComment(IFstream &)
Read non-comment line.
Definition: triSurface.C:172
y
scalar y
Definition: LISASMDCalcMethod1.H:14