SMESHsurfaceFormat.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 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 "SMESHsurfaceFormat.H"
27 #include "clock.H"
28 #include "IFstream.H"
29 #include "OFstream.H"
30 #include "Ostream.H"
31 
32 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33 
34 template<class Face>
36 {}
37 
38 
39 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
40 
41 template<class Face>
43 (
44  const fileName& filename,
45  const MeshedSurfaceProxy<Face>& surf
46 )
47 {
48  const pointField& pointLst = surf.points();
49  const List<Face>& faceLst = surf.faces();
50  const List<label>& faceMap = surf.faceMap();
51 
52  const List<surfZone>& zones =
53  (
54  surf.surfZones().empty()
55  ? surfaceFormatsCore::oneZone(faceLst)
56  : surf.surfZones()
57  );
58 
59  const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
60 
61 
62  OFstream os(filename);
63  if (!os.good())
64  {
66  << "Cannot open file for writing " << filename
67  << exit(FatalError);
68  }
69 
70 
71  // Write header
72  os << "# tetgen .smesh file written " << clock::dateTime().c_str() << nl
73  << "# <points count=\"" << pointLst.size() << "\">" << nl
74  << pointLst.size() << " 3" << nl; // 3: dimensions
75 
76  // Write vertex coords
77  forAll(pointLst, ptI)
78  {
79  const point& pt = pointLst[ptI];
80 
81  os << ptI << ' ' << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
82  }
83  os << "# </points>" << nl
84  << nl
85  << "# <faces count=\"" << faceLst.size() << "\">" << endl;
86 
87  os << faceLst.size() << " 1" << endl; // one attribute: zone number
88 
89 
90  label faceIndex = 0;
91  forAll(zones, zoneI)
92  {
93  const surfZone& zone = zones[zoneI];
94 
95  if (useFaceMap)
96  {
97  forAll(zone, localFacei)
98  {
99  const Face& f = faceLst[faceMap[faceIndex++]];
100 
101  os << f.size();
102  forAll(f, fp)
103  {
104  os << ' ' << f[fp];
105  }
106  os << ' ' << zoneI << endl;
107  }
108  }
109  else
110  {
111  forAll(zones[zoneI], localFacei)
112  {
113  const Face& f = faceLst[faceIndex++];
114 
115  os << f.size();
116  forAll(f, fp)
117  {
118  os << ' ' << f[fp];
119  }
120  os << ' ' << zoneI << endl;
121  }
122  }
123  }
124 
125  // write tail
126 
127  os << "# </faces>" << nl
128  << nl
129  << "# no holes or regions:" << nl
130  << '0' << nl // holes
131  << '0' << endl; // regions
132 }
133 
134 
135 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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
A class for handling file names.
Definition: fileName.H:79
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
Output to file stream.
Definition: OFstream.H:82
A surface zone on a MeshedSurface.
Definition: surfZone.H:62
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
const Cmpt & z() const
Definition: VectorI.H:87
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
const Cmpt & y() const
Definition: VectorI.H:81
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
bool useFaceMap() const
Use faceMap?
Base class for zones.
Definition: zone.H:57
const List< Face > & faces() const
Return const access to the faces.
const List< surfZone > & surfZones() const
Const access to the surface zones.
const Cmpt & x() const
Definition: VectorI.H:75
static const char nl
Definition: Ostream.H:260
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats...
Definition: MeshedSurface.H:73
labelList f(nPoints)
const pointField & points() const
Return const access to the points.
const List< label > & faceMap() const
Const access to the faceMap, zero-sized when unused.
static void write(const fileName &, const MeshedSurfaceProxy< Face > &)
Write surface mesh components by proxy.