OFSsurfaceFormat.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) 2011 OpenFOAM Foundation
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 "OFSsurfaceFormat.H"
27 #include "IFstream.H"
28 #include "IStringStream.H"
29 #include "ListOps.H"
30 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
33 template<class Face>
35 (
36  const fileName& filename
37 )
38 {
39  read(filename);
40 }
41 
42 
43 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
44 
45 template<class Face>
47 (
48  const fileName& filename
49 )
50 {
51  this->clear();
52 
53  IFstream is(filename);
54  if (!is.good())
55  {
57  (
58  "fileFormats::OFSsurfaceFormat::read(const fileName&)"
59  )
60  << "Cannot read file " << filename
61  << exit(FatalError);
62  }
63 
64  // read surfZones:
65  is >> this->storedZones();
66 
67  // read points:
68  is >> this->storedPoints();
69 
70  // must triangulate?
72  {
73  // read faces as 'face' and transcribe to 'triFace'
74  List<face> faceLst(is);
75 
77  (
78  xferMove(this->storedPoints()),
79  xferMove(faceLst),
80  xferMove(this->storedZones())
81  );
82 
83  this->transcribe(surf);
84  }
85  else
86  {
87  // read faces directly
88  is >> this->storedFaces();
89  }
90 
91  return true;
92 }
93 
94 
95 template<class Face>
97 (
98  Istream& is,
99  pointField& pointLst,
100  List<Face>& faceLst,
101  List<surfZone>& zoneLst
102 )
103 {
104  if (!is.good())
105  {
107  (
108  "fileFormats::OFSsurfaceFormat::read"
109  "(Istream&, pointField&, List<Face>&, List<surfZone>&)"
110  )
111  << "read error "
112  << exit(FatalError);
113  }
114 
115  // read surfZones:
116  is >> zoneLst;
117 
118  // read points:
119  is >> pointLst;
120 
121  // must triangulate?
123  {
124  // read faces as 'face' and transcribe to 'triFace'
125  List<face> origFaces(is);
126 
127  MeshedSurface<face> origSurf
128  (
129  xferMove(pointLst),
130  xferMove(origFaces),
131  xferMove(zoneLst)
132  );
133 
134  MeshedSurface<Face> surf;
135  surf.transcribe(origSurf);
136  }
137  else
138  {
139  // read faces directly
140  is >> faceLst;
141  }
142 
143  return true;
144 }
145 
146 
147 template<class Face>
149 (
150  Istream& is,
151  MeshedSurface<Face>& surf
152 )
153 {
154  surf.clear();
155 
156  if (!is.good())
157  {
159  (
160  "fileFormats::OFSsurfaceFormat::read"
161  "(Istream&, MeshedSurface<Face>&)"
162  )
163  << "read error "
164  << exit(FatalError);
165  }
166 
167  pointField pointLst;
168  List<Face> faceLst;
169  List<surfZone> zoneLst;
170 
171  read(is, pointLst, faceLst, zoneLst);
172 
173  surf.reset
174  (
175  xferMove(pointLst),
176  xferMove(faceLst),
177  xferMove(zoneLst)
178  );
179 
180  return true;
181 }
182 
183 
184 template<class Face>
186 (
187  Istream& is,
189 )
190 {
191  surf.clear();
192  MeshedSurface<Face> origSurf(is);
193  surf.transfer(origSurf);
194 
195  return true;
196 }
197 
198 
199 
200 template<class Face>
202 (
203  const fileName& filename,
204  const MeshedSurfaceProxy<Face>& surf
205 )
206 {
207  const List<Face>& faceLst = surf.faces();
208  const List<label>& faceMap = surf.faceMap();
209 
210  OFstream os(filename);
211  if (!os.good())
212  {
214  (
215  "fileFormats::OFSsurfaceFormat::write"
216  "(const fileName&, const MeshedSurfaceProxy<Face>&)"
217  )
218  << "Cannot open file for writing " << filename
219  << exit(FatalError);
220  }
221 
222 
223  OFSsurfaceFormatCore::writeHeader(os, surf.points(), surf.surfZones());
224 
225  const List<surfZone>& zones = surf.surfZones();
226  const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
227 
228  if (useFaceMap)
229  {
230  os << "\n// faces:" << nl
231  << faceLst.size() << token::BEGIN_LIST << nl;
232 
233  label faceI = 0;
234  forAll(zones, zoneI)
235  {
236  // Print all faces belonging to this zone
237  const surfZone& zone = zones[zoneI];
238 
239  forAll(zone, localFaceI)
240  {
241  os << faceLst[faceMap[faceI++]] << nl;
242  }
243  }
244  os << token::END_LIST << nl;
245  }
246  else
247  {
248  os << "\n// faces:" << nl << faceLst << nl;
249  }
250 
251  IOobject::writeDivider(os);
252 
253  // Check state of Ostream
254  os.check("OFSsurfaceFormat<Face>::write(Ostream&)");
255 }
256 
257 
258 // ************************************************************************* //
void transcribe(MeshedSurface< face > &)
Transfer points/zones and transcribe face -> triFace.
Output to file stream.
Definition: OFstream.H:81
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
bool useFaceMap() const
Use faceMap?
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats...
Definition: MeshedSurface.H:73
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Various functions to operate on Lists.
const pointField & points() const
Return const access to the points.
Xfer< T > xferMove(T &)
Input from file stream.
Definition: IFstream.H:81
const List< Face > & faces() const
Return const access to the faces.
virtual void clear()
Clear all storage.
virtual void clear()
Clear all storage.
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
static bool read(Istream &, pointField &, List< Face > &, List< surfZone > &)
Read surface mesh components.
static const char nl
Definition: Ostream.H:260
Base class for zones.
Definition: zone.H:57
const List< surfZone > & surfZones() const
Const access to the surface zones.
virtual void reset(const Xfer< pointField > &points, const Xfer< List< Face > > &faces, const Xfer< surfZoneList > &zones)
Reset primitive data (points, faces and zones)
#define forAll(list, i)
Definition: UList.H:421
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Provide a means of reading/writing the single-file OpenFOAM surface format.
void transfer(UnsortedMeshedSurface< Face > &)
Transfer the contents of the argument and annul the argument.
A surface zone on a MeshedSurface.
Definition: surfZone.H:62
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
A surface geometry mesh, in which the surface zone information is conveyed by the &#39;zoneId&#39; associated...
Definition: MeshedSurface.H:74
error FatalError
A class for handling file names.
Definition: fileName.H:69
A surface geometry mesh with zone information, not to be confused with the similarly named surfaceMes...
Definition: MeshedSurface.H:72
const List< label > & faceMap() const
Const access to the faceMap, zero-sized when unused.
bool read(const char *, int32_t &)
Definition: int32IO.C:87
UEqn clear()
static void write(const fileName &, const MeshedSurfaceProxy< Face > &)
Write surface mesh components by proxy.