readGTS.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 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "triSurface.H"
29 #include "IFstream.H"
30 #include "IStringStream.H"
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
38 
39 bool triSurface::readGTS(const fileName& GTSfileName)
40 {
41  IFstream GTSfile(GTSfileName);
42 
43  if (!GTSfile.good())
44  {
45  FatalErrorIn("triSurface::readGTS(const fileName&)")
46  << "Cannot read file " << GTSfileName
47  << exit(FatalError);
48  }
49 
50  // Read header
51  label nPoints, nEdges, nElems;
52 
53  string line = getLineNoComment(GTSfile);
54  {
55  IStringStream lineStream(line);
56  lineStream >> nPoints >> nEdges >> nElems;
57  }
58 
59  // Read points
60  pointField& points_ = const_cast<pointField&>(points());
61  points_.setSize(nPoints);
62 
63  forAll(points_, pointi)
64  {
65  scalar x, y, z;
66  line = getLineNoComment(GTSfile);
67  {
68  IStringStream lineStream(line);
69  lineStream >> x >> y >> z;
70  }
71  points_[pointi] = point(x, y, z);
72  }
73 
74  // Read edges (Foam indexing)
76  forAll(edges, edgei)
77  {
78  label start, end;
79  line = getLineNoComment(GTSfile);
80  {
81  IStringStream lineStream(line);
82  lineStream >> start >> end;
83  }
84  edges[edgei] = edge(start - 1, end - 1);
85  }
86 
87  // Read triangles. Convert references to edges into pointlabels
88  setSize(nElems);
89  forAll(*this, trianglei)
90  {
91  label e0Label, e1Label, e2Label;
92  label region = 0;
93 
94  line = getLineNoComment(GTSfile);
95  {
96  IStringStream lineStream(line);
97  lineStream >> e0Label >> e1Label >> e2Label;
98 
99  // Optional region number: read first, then check state on stream
100  if (lineStream)
101  {
102  label num;
103  lineStream >> num;
104  if (!lineStream.bad())
105  {
106  region = num;
107  }
108  }
109  }
110 
111  // Determine ordering of edges e0, e1
112  // common:common vertex, shared by e0 and e1
113  // e0Far:vertex on e0 which is not common
114  // e1Far: ,, e1 ,,
115  const edge& e0 = edges[e0Label - 1];
116  const edge& e1 = edges[e1Label - 1];
117  const edge& e2 = edges[e2Label - 1];
118 
119  label common01 = e0.commonVertex(e1);
120  if (common01 == -1)
121  {
122  FatalErrorIn("triSurface::readGTS(const fileName&)")
123  << "Edges 0 and 1 of triangle " << trianglei
124  << " do not share a point.\n"
125  << " edge0:" << e0 << endl
126  << " edge1:" << e1
127  << exit(FatalError);
128  }
129 
130  label e0Far = e0.otherVertex(common01);
131  label e1Far = e1.otherVertex(common01);
132 
133  label common12 = e1.commonVertex(e2);
134  if (common12 == -1)
135  {
136  FatalErrorIn("triSurface::readGTS(const fileName&)")
137  << "Edges 1 and 2 of triangle " << trianglei
138  << " do not share a point.\n"
139  << " edge1:" << e1 << endl
140  << " edge2:" << e2
141  << exit(FatalError);
142  }
143  label e2Far = e2.otherVertex(common12);
144 
145  // Does edge2 sit between edge1 and 0?
146  if ((common12 != e1Far) || (e2Far != e0Far))
147  {
148  FatalErrorIn("triSurface::readGTS(const fileName&)")
149  << "Edges of triangle " << trianglei
150  << " reference more than three points.\n"
151  << " edge0:" << e0 << endl
152  << " edge1:" << e1 << endl
153  << " edge2:" << e2 << endl
154  << exit(FatalError);
155  }
156 
157  operator[](trianglei) = labelledTri(e0Far, common01, e1Far, region);
158  }
159 
160  // Construct patch names
162 
163  return true;
164 }
165 
166 
167 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
168 
169 } // End namespace Foam
170 
171 // ************************************************************************* //
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
Foam::PrimitivePatch< labelledTri, List, pointField, point >::edges
const edgeList & edges() const
Return list of edges, address into LOCAL point list.
Definition: PrimitivePatchTemplate.C:212
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::PrimitivePatch< labelledTri, List, pointField, point >::nEdges
label nEdges() const
Return number of edges in patch.
Definition: PrimitivePatchTemplate.H:299
Foam::edge
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:58
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::triSurface::readGTS
bool readGTS(const fileName &)
Definition: readGTS.C:39
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
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::IOstream::bad
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:351
Foam::IStringStream
Input from memory buffer stream.
Definition: IStringStream.H:49
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::edge::commonVertex
label commonVertex(const edge &a) const
Return common vertex.
Definition: edgeI.H:121
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::triSurface::setDefaultPatches
void setDefaultPatches()
Sets default values for patches.
Definition: triSurface.C:587
Foam::List< labelledTri >::setSize
void setSize(const label)
Reset size of List.
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
Foam::PrimitivePatch< labelledTri, List, pointField, point >::points_
pointField points_
Reference to global list of points.
Definition: PrimitivePatchTemplate.H:118
FatalErrorIn
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:313
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::edge::otherVertex
label otherVertex(const label a) const
Given one vertex, return the other.
Definition: edgeI.H:103
Foam::triSurface::getLineNoComment
static string getLineNoComment(IFstream &)
Read non-comment line.
Definition: triSurface.C:172
y
scalar y
Definition: LISASMDCalcMethod1.H:14