initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / triSurface / triSurface / interfaces / TRI / readTRI.C
blobc0452e46aafe3e058e0f30f8dede8a28c3e1170f
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
26     TRI (triangle) file reader. Comes out of e.g. AC3D.
27     lines are 9 floats (3 points, each 3 floats) followed by hex colour.
28     Is converted into regions: regions numbered from 0 up, each colour is
29     region.
30     Most of reading/stitching taken from STL reader.
31     
32 \*---------------------------------------------------------------------------*/
34 #include "triSurface.H"
35 #include "STLpoint.H"
36 #include "SLList.H"
37 #include "IFstream.H"
38 #include "readHexLabel.H"
39 #include "stringList.H"
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 namespace Foam
46 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
48 bool triSurface::readTRI(const fileName& TRIfileName)
50     IFstream TRIfile(TRIfileName);
52     if (!TRIfile.good())
53     {
54         FatalErrorIn("triSurface::readTRI(const fileName&)")
55             << "Cannot read file " << TRIfileName
56             << exit(FatalError);
57     }
59     SLList<STLpoint> STLpoints;
60     SLList<label> STLlabels;
61     HashTable<label, string> STLsolidNames;
63     // Max region number so far
64     label maxRegion = 0;
66     while(TRIfile)
67     {
68         string line = getLineNoComment(TRIfile);
70         if (line.empty())
71         {
72             break;
73         }
75         IStringStream lineStream(line);
77         STLpoint p
78         (
79             readScalar(lineStream),
80             readScalar(lineStream),
81             readScalar(lineStream)
82         );
84         if (!lineStream) break;
86         STLpoints.append(p);
87         STLpoints.append
88         (
89             STLpoint
90             (
91                 readScalar(lineStream),
92                 readScalar(lineStream),
93                 readScalar(lineStream)
94             )
95         );
96         STLpoints.append
97         (
98             STLpoint
99             (
100                 readScalar(lineStream),
101                 readScalar(lineStream),
102                 readScalar(lineStream)
103             )
104         );
106         // Region/colour in .tri file starts with 0x. Skip.
108         char zero;
109         lineStream >> zero;
111         word rawSolidName(lineStream);
113         word solidName("patch" + rawSolidName(1, rawSolidName.size()-1));
115         label region  = -1;
117         HashTable<label, string>::const_iterator findName =
118             STLsolidNames.find(solidName);
120         if (findName != STLsolidNames.end())
121         {
122             region = findName();
123         }
124         else
125         {
126             Pout<< "Mapping triangle colour 0" << rawSolidName
127                 << " to region " << maxRegion << " name " << solidName
128                 << endl;
130             region = maxRegion++;
131             STLsolidNames.insert(solidName, region);
132         }
133         STLlabels.append(region);
134     }
137     pointField rawPoints(STLpoints.size());
139     label i = 0;
140     for
141     (
142         SLList<STLpoint>::iterator iter = STLpoints.begin();
143         iter != STLpoints.end();
144         ++iter
145     )
146     {
147         rawPoints[i++] = *iter;
148     }
150     setSize(STLlabels.size());
152     label pointI = 0;
153     SLList<label>::iterator iter = STLlabels.begin();
154     forAll (*this, i)
155     {
156         operator[](i)[0] = pointI++;
157         operator[](i)[1] = pointI++;
158         operator[](i)[2] = pointI++;
159         operator[](i).region() = *iter;
160         ++iter;
161     }
163     stitchTriangles(rawPoints);
165     // Convert solidNames into regionNames
166     stringList names(STLsolidNames.toc());
168     patches_.setSize(names.size());
170     forAll(names, nameI)
171     {
172         patches_[nameI].name() = names[nameI];
173         patches_[nameI].geometricType() = "empty";
174     }
176     return true;
180 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
182 } // End namespace Foam
184 // ************************************************************************* //