FreeFOAM The Cross-Platform CFD Toolkit
readGTS.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) 1991-2010 OpenCFD Ltd.
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 
26 \*---------------------------------------------------------------------------*/
27 
28 #include <triSurface/triSurface.H>
29 #include <OpenFOAM/IFstream.H>
30 #include <OpenFOAM/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)
75  edgeList edges(nEdges);
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
161  setDefaultPatches();
162 
163  return true;
164 }
165 
166 
167 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
168 
169 } // End namespace Foam
170 
171 // ************************ vim: set sw=4 sts=4 et: ************************ //