OFFsurfaceFormat.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 "OFFsurfaceFormat.H"
30 #include "clock.H"
31 #include "Fstream.H"
32 #include "StringStream.H"
33 #include "faceTraits.H"
34 
35 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
36 
37 template<class Face>
39 (
40  const fileName& filename
41 )
42 {
43  read(filename);
44 }
45 
46 
47 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
48 
49 template<class Face>
51 (
52  const fileName& filename
53 )
54 {
55  this->clear();
56 
57  IFstream is(filename);
58  if (!is.good())
59  {
61  << "Cannot read file " << filename
62  << exit(FatalError);
63  }
64 
65  // Read header
66  string hdr = this->getLineNoComment(is);
67  if (hdr != "OFF")
68  {
70  << "OFF file " << filename << " does not start with 'OFF'"
71  << exit(FatalError);
72  }
73 
74 
75  // get dimensions
76  label nPoints, nElems, nEdges;
77 
78  string line = this->getLineNoComment(is);
79  {
80  IStringStream lineStream(line);
81  lineStream >> nPoints >> nElems >> nEdges;
82  }
83 
84  // Read points
85  pointField pointLst(nPoints);
86  forAll(pointLst, pointi)
87  {
88  scalar x, y, z;
89  line = this->getLineNoComment(is);
90  {
91  IStringStream lineStream(line);
92  lineStream >> x >> y >> z;
93  }
94  pointLst[pointi] = point(x, y, z);
95  }
96 
97  // Read faces - ignore optional zone information
98  // use a DynamicList for possible on-the-fly triangulation
99  DynamicList<Face> dynFaces(nElems);
100 
101  for (label facei = 0; facei < nElems; ++facei)
102  {
103  line = this->getLineNoComment(is);
104 
105  {
106  IStringStream lineStream(line);
107 
108  label nVerts;
109  lineStream >> nVerts;
110 
111  List<label> verts(nVerts);
112 
113  forAll(verts, vertI)
114  {
115  lineStream >> verts[vertI];
116  }
117 
118  const labelUList& f = static_cast<const labelUList&>(verts);
119 
120  if (faceTraits<Face>::isTri() && f.size() > 3)
121  {
122  // simple face triangulation about f[0]
123  // cannot use face::triangulation (points may be incomplete)
124  for (label fp1 = 1; fp1 < f.size() - 1; fp1++)
125  {
126  label fp2 = f.fcIndex(fp1);
127 
128  dynFaces.append(Face{f[0], f[fp1], f[fp2]});
129  }
130  }
131  else
132  {
133  dynFaces.append(Face(f));
134  }
135  }
136  }
137 
138  // Transfer to normal lists, no zone information
139  MeshedSurface<Face> surf(std::move(pointLst), std::move(dynFaces));
140 
141  this->swap(surf);
142 
143  return true;
144 }
145 
146 
147 template<class Face>
149 (
150  const fileName& filename,
151  const MeshedSurfaceProxy<Face>& surf,
152  const dictionary&
153 )
154 {
155  const UList<point>& pointLst = surf.points();
156  const UList<Face>& faceLst = surf.surfFaces();
157  const UList<label>& faceMap = surf.faceMap();
158  const UList<surfZone>& zoneLst = surf.surfZones();
159 
160  OFstream os(filename);
161  if (!os.good())
162  {
164  << "Cannot open file for writing " << filename
165  << exit(FatalError);
166  }
167 
168  // Write header
169  os << "OFF" << endl
170  << "# Geomview OFF file written " << clock::dateTime().c_str() << nl
171  << nl
172  << "# points : " << pointLst.size() << nl
173  << "# faces : " << faceLst.size() << nl
174  << "# zones : " << zoneLst.size() << nl;
175 
176  // Print zone names as comment
177  forAll(zoneLst, zoneI)
178  {
179  os << "# " << zoneI << " " << zoneLst[zoneI].name()
180  << " (nFaces: " << zoneLst[zoneI].size() << ")" << nl;
181  }
182 
183  os << nl
184  << "# nPoints nFaces nEdges" << nl
185  << pointLst.size() << ' ' << faceLst.size() << ' ' << 0 << nl
186  << nl
187  << "# <points count=\"" << pointLst.size() << "\">" << endl;
188 
189  // Write vertex coords
190  forAll(pointLst, ptI)
191  {
192  os << pointLst[ptI].x() << ' '
193  << pointLst[ptI].y() << ' '
194  << pointLst[ptI].z() << " #" << ptI << endl;
195  }
196 
197  os << "# </points>" << nl
198  << nl
199  << "# <faces count=\"" << faceLst.size() << "\">" << endl;
200 
201  label faceIndex = 0;
202  forAll(zoneLst, zoneI)
203  {
204  os << "# <zone name=\"" << zoneLst[zoneI].name() << "\">" << endl;
205 
206  const label nLocalFaces = zoneLst[zoneI].size();
207 
208  if (surf.useFaceMap())
209  {
210  for (label i=0; i<nLocalFaces; ++i)
211  {
212  const Face& f = faceLst[faceMap[faceIndex++]];
213 
214  os << f.size();
215  for (const label verti : f)
216  {
217  os << ' ' << verti;
218  }
219 
220  // add optional zone information
221  os << ' ' << zoneI << endl;
222  }
223  }
224  else
225  {
226  for (label i=0; i<nLocalFaces; ++i)
227  {
228  const Face& f = faceLst[faceIndex++];
229 
230  os << f.size();
231  for (const label verti : f)
232  {
233  os << ' ' << verti;
234  }
235 
236  // add optional zone information
237  os << ' ' << zoneI << endl;
238  }
239  }
240  os << "# </zone>" << endl;
241  }
242  os << "# </faces>" << endl;
243 }
244 
245 
246 // ************************************************************************* //
Foam::fileFormats::OFFsurfaceFormat::read
virtual bool read(const fileName &filename)
Read from file.
Definition: OFFsurfaceFormat.C:51
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::IFstream
Input from file stream, using an ISstream.
Definition: IFstream.H:97
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:57
OFFsurfaceFormat.H
Foam::MeshedSurfaceProxy::points
const pointField & points() const
Return const access to the points.
Definition: MeshedSurfaceProxy.H:160
StringStream.H
Input/output from string buffers.
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::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
Foam::fileFormats::OFFsurfaceFormat::OFFsurfaceFormat
OFFsurfaceFormat(const fileName &filename)
Construct from file name.
Definition: OFFsurfaceFormat.C:39
nPoints
label nPoints
Definition: gmvOutputHeader.H:2
Foam::faceTraits
Traits class for faces.
Definition: faceTraits.H:50
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
Foam::Field< vector >
clock.H
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
Definition: DynamicListI.H:472
Foam::blockMeshTools::read
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:33
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::IStringStream
Input from string buffer, using a ISstream.
Definition: StringStream.H:112
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
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
clear
patchWriters clear()
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Fstream.H
Input/output from file streams.
f
labelList f(nPoints)
Foam::List< label >
Foam::UList< label >
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::line
A line primitive.
Definition: line.H:59
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::fileFormats::OFFsurfaceFormat::write
static void write(const fileName &filename, const MeshedSurfaceProxy< Face > &surf, const dictionary &options=dictionary::null)
Write surface mesh components by proxy.
Definition: OFFsurfaceFormat.C:149
Foam::point
vector point
Point is a vector.
Definition: point.H:43
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:216
Foam::MeshedSurface
A surface geometry mesh with zone information, not to be confused with the similarly named surfaceMes...
Definition: triSurfaceTools.H:80
faceTraits.H
y
scalar y
Definition: LISASMDCalcMethod1.H:14
Foam::MeshedSurfaceProxy::surfZones
const UList< surfZone > & surfZones() const
Const access to the surface zones.
Definition: MeshedSurfaceProxy.H:174