readGTS.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-2018 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 \*---------------------------------------------------------------------------*/
25 
26 #include "triSurface.H"
27 #include "IFstream.H"
28 #include "IStringStream.H"
29 
30 
31 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
32 
33 bool Foam::triSurface::readGTS(const fileName& GTSfileName)
34 {
35  IFstream GTSfile(GTSfileName);
36 
37  if (!GTSfile.good())
38  {
40  << "Cannot read file " << GTSfileName
41  << exit(FatalError);
42  }
43 
44  // Read header
45  label nPoints, nEdges, nElems;
46 
47  string line = getLineNoComment(GTSfile);
48  {
49  IStringStream lineStream(line);
50  lineStream >> nPoints >> nEdges >> nElems;
51  }
52 
53  // Read points
54  pointField& points_ = const_cast<pointField&>(points());
55  points_.setSize(nPoints);
56 
57  forAll(points_, pointi)
58  {
59  scalar x, y, z;
60  line = getLineNoComment(GTSfile);
61  {
62  IStringStream lineStream(line);
63  lineStream >> x >> y >> z;
64  }
65  points_[pointi] = point(x, y, z);
66  }
67 
68  // Read edges (Foam indexing)
70  forAll(edges, edgei)
71  {
72  label start, end;
73  line = getLineNoComment(GTSfile);
74  {
75  IStringStream lineStream(line);
76  lineStream >> start >> end;
77  }
78  edges[edgei] = edge(start - 1, end - 1);
79  }
80 
81  // Read triangles. Convert references to edges into pointlabels
82  setSize(nElems);
83  forAll(*this, trianglei)
84  {
85  label e0Label, e1Label, e2Label;
86  label region = 0;
87 
88  line = getLineNoComment(GTSfile);
89  {
90  IStringStream lineStream(line);
91  lineStream >> e0Label >> e1Label >> e2Label;
92 
93  // Optional region number: read first, then check state on stream
94  if (lineStream)
95  {
96  label num;
97  lineStream >> num;
98  if (!lineStream.bad())
99  {
100  region = num;
101  }
102  }
103  }
104 
105  // Determine ordering of edges e0, e1
106  // common:common vertex, shared by e0 and e1
107  // e0Far:vertex on e0 which is not common
108  // e1Far: ,, e1 ,,
109  const edge& e0 = edges[e0Label - 1];
110  const edge& e1 = edges[e1Label - 1];
111  const edge& e2 = edges[e2Label - 1];
112 
113  label common01 = e0.commonVertex(e1);
114  if (common01 == -1)
115  {
117  << "Edges 0 and 1 of triangle " << trianglei
118  << " do not share a point.\n"
119  << " edge0:" << e0 << endl
120  << " edge1:" << e1
121  << exit(FatalError);
122  }
123 
124  label e0Far = e0.otherVertex(common01);
125  label e1Far = e1.otherVertex(common01);
126 
127  label common12 = e1.commonVertex(e2);
128  if (common12 == -1)
129  {
131  << "Edges 1 and 2 of triangle " << trianglei
132  << " do not share a point.\n"
133  << " edge1:" << e1 << endl
134  << " edge2:" << e2
135  << exit(FatalError);
136  }
137  label e2Far = e2.otherVertex(common12);
138 
139  // Does edge2 sit between edge1 and 0?
140  if ((common12 != e1Far) || (e2Far != e0Far))
141  {
143  << "Edges of triangle " << trianglei
144  << " reference more than three points.\n"
145  << " edge0:" << e0 << endl
146  << " edge1:" << e1 << endl
147  << " edge2:" << e2 << endl
148  << exit(FatalError);
149  }
150 
151  operator[](trianglei) = labelledTri(e0Far, common01, e1Far, region);
152  }
153 
154  // Construct patch names
155  setDefaultPatches();
156 
157  return true;
158 }
159 
160 
161 // ************************************************************************* //
scalar y
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
void setSize(const label)
Reset size of List.
Definition: List.C:281
label nEdges() const
Return number of edges in patch.
label nPoints() const
Return number of points supporting patch faces.
const edgeList & edges() const
Return list of edges, address into LOCAL point list.
const Field< PointType > & points() const
Return reference to global points.
T & operator[](const label)
Return element of UList.
Definition: UListI.H:167
iterator end()
Return an iterator to end traversing the UList.
Definition: UListI.H:224
#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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
vector point
Point is a vector.
Definition: point.H:41
error FatalError
List< edge > edgeList
Definition: edgeList.H:38