SMESHsurfaceFormat.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2016-2017 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "SMESHsurfaceFormat.H"
30 #include "clock.H"
31 #include "OFstream.H"
32 
33 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
34 
35 template<class Face>
37 (
38  const fileName& filename,
39  const MeshedSurfaceProxy<Face>& surf,
40  const dictionary&
41 )
42 {
43  const UList<point>& pointLst = surf.points();
44  const UList<Face>& faceLst = surf.surfFaces();
45  const UList<label>& faceMap = surf.faceMap();
46 
47  const surfZoneList zones =
48  (
49  surf.surfZones().empty()
50  ? surfaceFormatsCore::oneZone(faceLst)
51  : surf.surfZones()
52  );
53 
54  const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
55 
56  OFstream os(filename);
57  if (!os.good())
58  {
60  << "Cannot open file for writing " << filename
61  << exit(FatalError);
62  }
63 
64 
65  // Write header
66  os << "# tetgen .smesh file written " << clock::dateTime().c_str() << nl
67  << "# <points count=\"" << pointLst.size() << "\">" << nl
68  << pointLst.size() << " 3" << nl; // 3: dimensions
69 
70  // Write vertex coords
71  forAll(pointLst, pti)
72  {
73  const point& pt = pointLst[pti];
74 
75  os << pti << ' ' << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
76  }
77  os << "# </points>" << nl
78  << nl
79  << "# <faces count=\"" << faceLst.size() << "\">" << endl;
80 
81  os << faceLst.size() << " 1" << endl; // one attribute: zone number
82 
83 
84  label faceIndex = 0;
85  label zoneIndex = 0;
86  for (const surfZone& zone : zones)
87  {
88  const label nLocalFaces = zone.size();
89 
90  if (useFaceMap)
91  {
92  for (label i=0; i<nLocalFaces; ++i)
93  {
94  const Face& f = faceLst[faceMap[faceIndex++]];
95 
96  os << f.size();
97  for (const label verti : f)
98  {
99  os << ' ' << verti;
100  }
101  os << ' ' << zoneIndex << nl;
102  }
103  }
104  else
105  {
106  for (label i=0; i<nLocalFaces; ++i)
107  {
108  const Face& f = faceLst[faceIndex++];
109 
110  os << f.size();
111  for (const label verti : f)
112  {
113  os << ' ' << verti;
114  }
115  os << ' ' << zoneIndex << nl;
116  }
117  }
118 
119  ++zoneIndex;
120  }
121 
122  // write tail
123 
124  os << "# </faces>" << nl
125  << nl
126  << "# no holes or regions:" << nl
127  << '0' << nl // holes
128  << '0' << endl; // regions
129 }
130 
131 
132 // ************************************************************************* //
Foam::Vector::x
const Cmpt & x() const
Access to the vector x component.
Definition: VectorI.H:78
Foam::faceMap
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
Definition: blockMeshMergeFast.C:94
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::MeshedSurfaceProxy::useFaceMap
bool useFaceMap() const
Use faceMap?
Definition: MeshedSurfaceProxy.H:186
Foam::zone
Base class for mesh zones.
Definition: zone.H:63
Foam::MeshedSurfaceProxy::points
const pointField & points() const
Return const access to the points.
Definition: MeshedSurfaceProxy.H:160
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::MeshedSurfaceProxy
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats.
Definition: MeshedSurface.H:78
Foam::Vector::z
const Cmpt & z() const
Access to the vector z component.
Definition: VectorI.H:90
Foam::MeshedSurfaceProxy::faceMap
const labelUList & faceMap() const
Const access to the faceMap, zero-sized when unused.
Definition: MeshedSurfaceProxy.H:180
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
OFstream.H
SMESHsurfaceFormat.H
Foam::label
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:62
clock.H
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:121
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:99
Foam::fileFormats::SMESHsurfaceFormat::write
static void write(const fileName &filename, const MeshedSurfaceProxy< Face > &surf, const dictionary &options=dictionary::null)
Write surface mesh components by proxy.
Definition: SMESHsurfaceFormat.C:37
Foam::Vector::y
const Cmpt & y() const
Access to the vector y component.
Definition: VectorI.H:84
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::nl
constexpr char nl
Definition: Ostream.H:372
f
labelList f(nPoints)
Foam::Vector< scalar >
Foam::List< surfZone >
Foam::surfZone
A surface zone on a MeshedSurface.
Definition: surfZone.H:65
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
Foam::UList::size
void size(const label n) noexcept
Override size to be inconsistent with allocated storage.
Definition: UListI.H:360
Foam::MeshedSurfaceProxy::surfFaces
const UList< Face > & surfFaces() const
Return const access to the faces.
Definition: MeshedSurfaceProxy.H:166
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:216
Foam::MeshedSurfaceProxy::surfZones
const UList< surfZone > & surfZones() const
Const access to the surface zones.
Definition: MeshedSurfaceProxy.H:174