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