triSurfaceIO.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2017-2020 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "triSurface.H"
29 #include "Fstream.H"
30 #include "Time.H"
31 #include "boundBox.H"
32 #include "bitSet.H"
33 #include "surfZoneList.H"
34 #include "surfaceFormatsCore.H"
35 #include "MeshedSurfaceProxy.H"
36 #include "MeshedSurface.H"
37 
38 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
39 
41 {
42  wordHashSet known
43  (
46  );
47 
48  // Additional hard-coded entry points, but do not need
49  // {"stl", "stlb"}
50  // since they are shadowed by *MeshedSurface
51 
52  known.insert("ftr");
53 
54  return known;
55 }
56 
57 
59 {
60  wordHashSet known
61  (
63  );
64 
65  // Additional hard-coded entry points, but do not need
66  // {"gts", "stl", "stlb"}
67  // since they are shadowed by MeshedSurfaceProxy
68 
69  known.insert("ftr");
70 
71  return known;
72 }
73 
74 
75 bool Foam::triSurface::canReadType(const word& fileType, bool verbose)
76 {
78  (
79  readTypes(),
80  fileType,
81  verbose,
82  "reading"
83  );
84 }
85 
86 
87 bool Foam::triSurface::canWriteType(const word& fileType, bool verbose)
88 {
90  (
91  writeTypes(),
92  fileType,
93  verbose,
94  "writing"
95  );
96 }
97 
98 
99 bool Foam::triSurface::canRead(const fileName& name, bool verbose)
100 {
101  word ext(name.ext());
102  if (ext == "gz")
103  {
104  ext = name.lessExt().ext();
105  }
106  return canReadType(ext, verbose);
107 }
108 
109 
111 (
112  const IOobject& io,
113  const fileName& f,
114  const bool isGlobal
115 )
116 {
118 }
119 
120 
122 (
123  const IOobject& io,
124  const bool isGlobal
125 )
126 {
127  return fileFormats::surfaceFormatsCore::checkFile(io, isGlobal);
128 }
129 
130 
132 (
133  const IOobject& io,
134  const dictionary& dict,
135  const bool isGlobal
136 )
137 {
138  return fileFormats::surfaceFormatsCore::checkFile(io, dict, isGlobal);
139 }
140 
141 
143 (
144  const IOobject& io,
145  const bool isGlobal
146 )
147 {
148  return fileFormats::surfaceFormatsCore::findFile(io, isGlobal);
149 }
150 
151 
153 (
154  const IOobject& io,
155  const dictionary& dict,
156  const bool isGlobal
157 )
158 {
159  return fileFormats::surfaceFormatsCore::findFile(io, dict, isGlobal);
160 }
161 
162 
163 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
164 
165 bool Foam::triSurface::readNative(Istream& is)
166 {
167  // Read triangles, points from Istream
168  is >> patches_ >> storedPoints() >> storedFaces();
169 
170  return true;
171 }
172 
173 
174 void Foam::triSurface::writeNative(Ostream& os) const
175 {
176  os << patches() << nl;
177 
178  //Note: Write with global point numbering
179  os << points() << nl
180  << static_cast<const List<labelledTri>&>(*this) << nl;
181 
183 }
184 
185 
186 bool Foam::triSurface::read
187 (
188  const fileName& name,
189  const word& fileType,
190  const bool check
191 )
192 {
193  if (check && !exists(name))
194  {
196  << "No such file " << name << nl
197  << exit(FatalError);
198  }
199 
200  this->clear();
201  transfer(*New(name, fileType));
202  return true;
203 }
204 
205 
207 (
208  const fileName& name,
209  const word& fileType,
210  const bool sortByRegion
211 ) const
212 {
213  if (fileType.empty())
214  {
215  // Handle empty/missing type
216 
217  const word ext(name.ext());
218 
219  if (ext.empty())
220  {
222  << "Cannot determine format from filename" << nl
223  << " " << name << nl
224  << exit(FatalError);
225  }
226 
227  write(name, ext, sortByRegion);
228  return;
229  }
230 
231 
232  // Hard-coded writers
233 
234  if (fileType == "ftr")
235  {
236  OFstream os(name);
237  writeNative(os);
238  }
239  else if (fileType == "stl")
240  {
241  writeSTLASCII(name, sortByRegion);
242  }
243  else if (fileType == "stlb")
244  {
245  writeSTLBINARY(name);
246  }
247  else if (fileType == "gts")
248  {
249  writeGTS(name, sortByRegion);
250  }
252  {
254  List<surfZone> zoneLst = this->sortedZones(faceMap);
255 
257  (
258  this->points(),
259  this->surfFaces(),
260  zoneLst,
261  faceMap
262  );
263 
264  proxy.write(name, fileType);
265  }
266  else
267  {
269  << "Unknown surface format " << fileType
270  << " for writing file " << name << nl
271  << "Valid types:" << nl
272  << " " << flatOutput(writeTypes().sortedToc()) << nl
273  << exit(FatalError);
274  }
275 }
276 
277 
278 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
279 
281 :
282  triSurface()
283 {
284  readNative(is);
285 
286  setDefaultPatches();
287 }
288 
289 
291 :
292  triSurface()
293 {
294  IFstream is
295  (
296  d.path()/triSurfInstance(d)/typeName/(d.caseName() + ".ftr")
297  );
298 
299  readNative(is);
300 
301  setDefaultPatches();
302 }
303 
304 
306 (
307  const IOobject& io,
308  const dictionary& dict,
309  const bool isGlobal
310 )
311 :
312  triSurface()
313 {
314  fileName fName(checkFile(io, dict, isGlobal));
315 
316  read(fName, dict.getOrDefault<word>("fileType", word::null));
317 
318  scalePoints(dict.getOrDefault<scalar>("scale", 0));
319 
320  setDefaultPatches();
321 }
322 
323 
324 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
325 
327 (
328  const fileName& name,
329  const bool sortByRegion
330 ) const
331 {
332  write(name, name.ext(), sortByRegion);
333 }
334 
335 
337 {
338  writeNative(os);
339 }
340 
341 
342 void Foam::triSurface::write(const Time& d) const
343 {
344  OFstream os
345  (
346  d.path()/triSurfInstance(d)/typeName/(d.caseName() + ".ftr")
347  );
348 
349  writeNative(os);
350 }
351 
352 
354 {
355  // Unfortunately nPoints constructs meshPoints() so do compact version
356  // ourselves.
357 
358  bitSet pointIsUsed(points().size());
359 
361  labelHashSet regionsUsed;
362 
363  for (const auto& f : *this)
364  {
365  regionsUsed.insert(f.region());
366 
367  for (const label pointi : f)
368  {
369  if (pointIsUsed.set(pointi))
370  {
371  bb.add(points()[pointi]);
372  }
373  }
374  }
375 
376  os << "Triangles : " << size()
377  << " in " << regionsUsed.size() << " region(s)" << nl
378  << "Vertices : " << pointIsUsed.count() << nl
379  << "Bounding Box : " << bb << endl;
380 }
381 
382 
383 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
384 
386 {
387  s.clearOut();
388  s.readNative(is);
389  s.setDefaultPatches();
390  return is;
391 }
392 
393 
395 {
396  s.writeNative(os);
397  return os;
398 }
399 
400 
401 // ************************************************************************* //
Foam::triSurface::writeStats
void writeStats(Ostream &os) const
Write some statistics.
Definition: triSurfaceIO.C:353
Foam::word::lessExt
word lessExt() const
Return word without extension (part before last .)
Definition: word.C:113
Foam::fileFormats::surfaceFormatsCore::checkFile
static fileName checkFile(const IOobject &io, const bool isGlobal=true)
Return fileName to load IOobject from.
Definition: surfaceFormatsCore.C:257
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
MeshedSurfaceProxy.H
Foam::exists
bool exists(const fileName &name, const bool checkGzip=true, const bool followLink=true)
Does the name exist (as DIRECTORY or FILE) in the file system?
Definition: MSwindows.C:625
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::faceMap
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
Definition: blockMeshMergeTopological.C:94
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
s
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputSpray.H:25
Foam::bitSet
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:63
Foam::triSurface::relativeFilePath
static fileName relativeFilePath(const IOobject &io, const fileName &f, const bool isGlobal=true)
Return fileName.
Definition: triSurfaceIO.C:111
Foam::IFstream
Input from file stream, using an ISstream.
Definition: IFstream.H:53
Foam::boundBox::invertedBox
static const boundBox invertedBox
A large inverted boundBox: min/max == +/- ROOTVGREAT.
Definition: boundBox.H:86
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Foam::bitSet::set
void set(const bitSet &bitset)
Set specified bits from another bitset.
Definition: bitSetI.H:574
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::triSurface::canReadType
static bool canReadType(const word &fileType, bool verbose=false)
Can we read this file format?
Definition: triSurfaceIO.C:75
Foam::word::ext
word ext() const
Return file name extension (part after last .)
Definition: word.C:126
Foam::MeshedSurfaceProxy::write
static void write(const fileName &name, const MeshedSurfaceProxy &surf, IOstreamOption streamOpt=IOstreamOption(), const dictionary &options=dictionary::null)
Write to file, select based on its extension.
Definition: MeshedSurfaceProxy.C:64
triSurface.H
Foam::HashSet< word, Hash< word > >
bitSet.H
Foam::bitSet::count
unsigned int count(const bool on=true) const
Count number of bits set.
Definition: bitSetI.H:499
Foam::MeshedSurfaceProxy
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats.
Definition: MeshedSurface.H:82
Foam::fileFormats::surfaceFormatsCore::checkSupport
static bool checkSupport(const wordHashSet &available, const word &fileType, const bool verbose=false, const char *functionName=nullptr)
Verbose checking of fileType in the list of available types.
Definition: surfaceFormatsCore.C:326
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::triSurface::checkFile
static fileName checkFile(const IOobject &io, const bool isGlobal=true)
Return fileName to load IOobject from.
Definition: triSurfaceIO.C:122
Foam::triSurface::writeTypes
static wordHashSet writeTypes()
Known writable file-types, including via friends or proxies.
Definition: triSurfaceIO.C:58
Foam::check
static void check(const int retVal, const char *what)
Definition: ptscotchDecomp.C:80
Foam::triSurface::write
void write(Ostream &os) const
Write to Ostream in simple OpenFOAM format.
Definition: triSurfaceIO.C:336
Foam::triSurface
Triangulated surface description with patch information.
Definition: triSurface.H:76
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::UnsortedMeshedSurface
A surface geometry mesh, in which the surface zone information is conveyed by the 'zoneId' associated...
Definition: MeshedSurface.H:83
Foam::triSurface::triSurface
triSurface()
Default construct.
Definition: triSurface.C:432
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam::flatOutput
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:216
Foam::triSurface::canWriteType
static bool canWriteType(const word &fileType, bool verbose=false)
Can we write this file format?
Definition: triSurfaceIO.C:87
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
boundBox.H
Foam::triSurface::findFile
static fileName findFile(const IOobject &io, const bool isGlobal=true)
Definition: triSurfaceIO.C:143
Time.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::Time::caseName
const fileName & caseName() const
Return case name.
Definition: TimePathsI.H:62
clear
patchWriters clear()
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::Time::path
fileName path() const
Return path.
Definition: Time.H:358
Fstream.H
f
labelList f(nPoints)
Foam::List< label >
Foam::fileFormats::surfaceFormatsCore::relativeFilePath
static fileName relativeFilePath(const IOobject &io, const fileName &f, const bool isGlobal=true)
Return fileName.
Definition: surfaceFormatsCore.C:178
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::HashSet::insert
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition: HashSet.H:191
Foam::word::null
static const word null
An empty word.
Definition: word.H:80
patches
const polyBoundaryMesh & patches
Definition: convertProcessorPatches.H:65
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Definition: foamVtkOutputTemplates.C:36
Foam::boundBox
A bounding box defined in terms of min/max extrema points.
Definition: boundBox.H:63
surfZoneList.H
Foam::triSurface::readTypes
static wordHashSet readTypes()
Known readable file-types, including via friends or proxies.
Definition: triSurfaceIO.C:40
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::fileFormats::surfaceFormatsCore::findFile
static fileName findFile(const IOobject &io, const bool isGlobal=true)
Definition: surfaceFormatsCore.C:204
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::dictionary::getOrDefault
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:148
Foam::MeshedSurface
A surface geometry mesh with zone information, not to be confused with the similarly named surfaceMes...
Definition: triSurfaceTools.H:80
surfaceFormatsCore.H
Foam::triSurface::triSurfInstance
static fileName triSurfInstance(const Time &)
Name of triSurface directory to use.
Definition: triSurface.C:73
MeshedSurface.H
Foam::triSurface::canRead
static bool canRead(const fileName &name, bool verbose=false)
Can we read this file format?
Definition: triSurfaceIO.C:99
Foam::boundBox::add
void add(const boundBox &bb)
Extend to include the second box.
Definition: boundBoxI.H:191