initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / triSurface / triSurface / interfaces / GTS / readGTS.C
blob88275394ad1491e92044fff1c8de76a74d1db4f6
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Description
27 \*---------------------------------------------------------------------------*/
29 #include "triSurface.H"
30 #include "IFstream.H"
31 #include "IStringStream.H"
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 namespace Foam
38 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
40 bool triSurface::readGTS(const fileName& GTSfileName)
42     IFstream GTSfile(GTSfileName);
44     if (!GTSfile.good())
45     {
46         FatalErrorIn("triSurface::readGTS(const fileName&)")
47             << "Cannot read file " << GTSfileName
48             << exit(FatalError);
49     }
51     // Read header
52     label nPoints, nEdges, nElems;
54     string line = getLineNoComment(GTSfile);
55     {
56         IStringStream lineStream(line);
57         lineStream >> nPoints >> nEdges >> nElems;
58     }
60     // Read points
61     pointField& points_ = const_cast<pointField&>(points());
62     points_.setSize(nPoints);
64     forAll(points_, pointi)
65     {
66         scalar x, y, z;
67         line = getLineNoComment(GTSfile);
68         {
69             IStringStream lineStream(line);
70             lineStream >> x >> y >> z;
71         }
72         points_[pointi] = point(x, y, z);
73     }
75     // Read edges (Foam indexing)
76     edgeList edges(nEdges);
77     forAll(edges, edgei)
78     {
79         label start, end;
80         line = getLineNoComment(GTSfile);
81         {
82             IStringStream lineStream(line);
83             lineStream >> start >> end;
84         }
85         edges[edgei] = edge(start - 1, end - 1);
86     }
88     // Read triangles. Convert references to edges into pointlabels
89     setSize(nElems);
90     forAll(*this, trianglei)
91     {
92         label e0Label, e1Label, e2Label;
93         label region = 0;
95         line = getLineNoComment(GTSfile);
96         {
97             IStringStream lineStream(line);
98             lineStream >> e0Label >> e1Label >> e2Label;
100             // Optional region number: read first, then check state on stream
101             if (lineStream)
102             {
103                 label num;
104                 lineStream >> num;
105                 if (!lineStream.bad())
106                 {
107                     region = num;
108                 }
109             }
110         }
112         // Determine ordering of edges e0, e1
113         //  common:common vertex, shared by e0 and e1
114         //  e0Far:vertex on e0 which is not common
115         //  e1Far: ,,       e1  ,,
116         const edge& e0 = edges[e0Label - 1];
117         const edge& e1 = edges[e1Label - 1];
118         const edge& e2 = edges[e2Label - 1];
120         label common01 = e0.commonVertex(e1);
121         if (common01 == -1)
122         {
123             FatalErrorIn("triSurface::readGTS(const fileName&)")
124                 << "Edges 0 and 1 of triangle " << trianglei
125                 << " do not share a point.\n"
126                 << "    edge0:" << e0 << endl
127                 << "    edge1:" << e1
128                 << exit(FatalError);
129         }
131         label e0Far = e0.otherVertex(common01);
132         label e1Far = e1.otherVertex(common01);
134         label common12 = e1.commonVertex(e2);
135         if (common12 == -1)
136         {
137             FatalErrorIn("triSurface::readGTS(const fileName&)")
138                 << "Edges 1 and 2 of triangle " << trianglei
139                 << " do not share a point.\n"
140                 << "    edge1:" << e1 << endl
141                 << "    edge2:" << e2
142                 << exit(FatalError);
143         }
144         label e2Far = e2.otherVertex(common12);
146         // Does edge2 sit between edge1 and 0?
147         if ((common12 != e1Far) || (e2Far != e0Far))
148         {
149             FatalErrorIn("triSurface::readGTS(const fileName&)")
150                 << "Edges of triangle " << trianglei
151                 << " reference more than three points.\n"
152                 << "    edge0:" << e0 << endl
153                 << "    edge1:" << e1 << endl
154                 << "    edge2:" << e2 << endl
155                 << exit(FatalError);
156         }
158         operator[](trianglei) = labelledTri(e0Far, common01, e1Far, region);
159     }
161     // Construct patch names
162     setDefaultPatches();
164     return true;
168 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
170 } // End namespace Foam
172 // ************************************************************************* //