FreeFOAM The Cross-Platform CFD Toolkit
TRIsurfaceFormatCore.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 \*---------------------------------------------------------------------------*/
25 
26 #include "TRIsurfaceFormat.H"
27 #include <OpenFOAM/IFstream.H>
28 #include <OpenFOAM/IOmanip.H>
29 #include <OpenFOAM/IStringStream.H>
30 #include <OpenFOAM/Map.H>
31 
32 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33 
34 Foam::fileFormats::TRIsurfaceFormatCore::TRIsurfaceFormatCore
35 (
36  const fileName& filename
37 )
38 :
39  sorted_(true),
40  points_(0),
41  zoneIds_(0),
42  sizes_(0)
43 {
44  read(filename);
45 }
46 
47 
48 // * * * * * * * * * * * * * * * * Destructors * * * * * * * * * * * * * * * //
49 
51 {}
52 
53 
54 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
55 
56 bool Foam::fileFormats::TRIsurfaceFormatCore::read
57 (
58  const fileName& filename
59 )
60 {
61  this->clear();
62  sorted_ = true;
63 
64  IFstream is(filename);
65  if (!is.good())
66  {
68  (
69  "fileFormats::TRIsurfaceFormatCore::read(const fileName&)"
70  )
71  << "Cannot read file " << filename
72  << exit(FatalError);
73  }
74 
75  // uses similar structure as STL, just some points
76  // the rest of the reader resembles the STL binary reader
77  DynamicList<point> dynPoints;
78  DynamicList<label> dynZones;
79  DynamicList<label> dynSizes;
80  HashTable<label> lookup;
81 
82  // place faces without a group in zone0
83  label zoneI = 0;
84  dynSizes.append(zoneI);
85  lookup.insert("zoneI", zoneI);
86 
87  while (is.good())
88  {
89  string line = this->getLineNoComment(is);
90 
91  // handle continuations ?
92  // if (line[line.size()-1] == '\\')
93  // {
94  // line.substr(0, line.size()-1);
95  // line += this->getLineNoComment(is);
96  // }
97 
98  IStringStream lineStream(line);
99 
100  point p
101  (
102  readScalar(lineStream),
103  readScalar(lineStream),
104  readScalar(lineStream)
105  );
106 
107  if (!lineStream) break;
108 
109  dynPoints.append(p);
110  dynPoints.append
111  (
112  point
113  (
114  readScalar(lineStream),
115  readScalar(lineStream),
116  readScalar(lineStream)
117  )
118  );
119  dynPoints.append
120  (
121  point
122  (
123  readScalar(lineStream),
124  readScalar(lineStream),
125  readScalar(lineStream)
126  )
127  );
128 
129  // zone/colour in .tri file starts with 0x. Skip.
130  // ie, instead of having 0xFF, skip 0 and leave xFF to
131  // get read as a word and name it "zoneFF"
132 
133  char zero;
134  lineStream >> zero;
135 
136  word rawName(lineStream);
137  word name("zone" + rawName(1, rawName.size()-1));
138 
140  if (fnd != lookup.end())
141  {
142  if (zoneI != fnd())
143  {
144  // group appeared out of order
145  sorted_ = false;
146  }
147  zoneI = fnd();
148  }
149  else
150  {
151  zoneI = dynSizes.size();
152  lookup.insert(name, zoneI);
153  dynSizes.append(0);
154  }
155 
156  dynZones.append(zoneI);
157  dynSizes[zoneI]++;
158  }
159 
160  // skip empty groups
161  label nZone = 0;
162  forAll(dynSizes, zoneI)
163  {
164  if (dynSizes[zoneI])
165  {
166  if (nZone != zoneI)
167  {
168  dynSizes[nZone] = dynSizes[zoneI];
169  }
170  nZone++;
171  }
172  }
173  // truncate addressed size
174  dynSizes.setCapacity(nZone);
175 
176  // transfer to normal lists
177  points_.transfer(dynPoints);
178  zoneIds_.transfer(dynZones);
179  sizes_.transfer(dynSizes);
180 
181  return true;
182 }
183 
184 
185 // ************************ vim: set sw=4 sts=4 et: ************************ //