1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
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
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "OFFsurfaceFormat.H"
30 #include "IStringStream.H"
34 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
37 Foam::fileFormats::OFFsurfaceFormat<Face>::OFFsurfaceFormat
39 const fileName& filename
46 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
49 bool Foam::fileFormats::OFFsurfaceFormat<Face>::read
51 const fileName& filename
54 const bool mustTriangulate = this->isTri();
57 IFstream is(filename);
62 "fileFormats::OFFsurfaceFormat::read(const fileName&)"
64 << "Cannot read file " << filename
69 string hdr = this->getLineNoComment(is);
74 "fileFormats::OFFsurfaceFormat::read(const fileName&)"
76 << "OFF file " << filename << " does not start with 'OFF'"
82 label nPoints, nElems, nEdges;
84 string line = this->getLineNoComment(is);
86 IStringStream lineStream(line);
87 lineStream >> nPoints >> nElems >> nEdges;
91 pointField pointLst(nPoints);
92 forAll(pointLst, pointI)
95 line = this->getLineNoComment(is);
97 IStringStream lineStream(line);
98 lineStream >> x >> y >> z;
100 pointLst[pointI] = point(x, y, z);
103 // Read faces - ignore optional zone information
104 // use a DynamicList for possible on-the-fly triangulation
105 DynamicList<Face> dynFaces(nElems);
107 for (label faceI = 0; faceI < nElems; ++faceI)
109 line = this->getLineNoComment(is);
112 IStringStream lineStream(line);
115 lineStream >> nVerts;
117 List<label> verts(nVerts);
121 lineStream >> verts[vertI];
124 UList<label>& f = static_cast<UList<label>&>(verts);
126 if (mustTriangulate && f.size() > 3)
128 // simple face triangulation about f[0]
129 // cannot use face::triangulation (points may be incomplete)
130 for (label fp1 = 1; fp1 < f.size() - 1; fp1++)
132 label fp2 = f.fcIndex(fp1);
134 dynFaces.append(triFace(f[0], f[fp1], f[fp2]));
139 dynFaces.append(Face(f));
144 // transfer to normal lists, no zone information
145 reset(pointLst.xfer(), dynFaces.xfer(), Xfer<surfZoneList>());
152 void Foam::fileFormats::OFFsurfaceFormat<Face>::write
154 const fileName& filename,
155 const MeshedSurfaceProxy<Face>& surf
158 const pointField& pointLst = surf.points();
159 const List<Face>& faceLst = surf.faces();
160 const List<label>& faceMap = surf.faceMap();
161 const List<surfZone>& zoneLst = surf.surfZones();
163 OFstream os(filename);
168 "fileFormats::OFFsurfaceFormat::write"
169 "(const fileName&, const MeshedSurfaceProxy<Face>&)"
171 << "Cannot open file for writing " << filename
177 << "# Geomview OFF file written " << clock::dateTime().c_str() << nl
179 << "# points : " << pointLst.size() << nl
180 << "# faces : " << faceLst.size() << nl
181 << "# zones : " << zoneLst.size() << nl;
183 // Print zone names as comment
184 forAll(zoneLst, zoneI)
186 os << "# " << zoneI << " " << zoneLst[zoneI].name()
187 << " (nFaces: " << zoneLst[zoneI].size() << ")" << nl;
191 << "# nPoints nFaces nEdges" << nl
192 << pointLst.size() << ' ' << faceLst.size() << ' ' << 0 << nl
194 << "# <points count=\"" << pointLst.size() << "\">" << endl;
196 // Write vertex coords
197 forAll(pointLst, ptI)
199 os << pointLst[ptI].x() << ' '
200 << pointLst[ptI].y() << ' '
201 << pointLst[ptI].z() << " #" << ptI << endl;
204 os << "# </points>" << nl
206 << "# <faces count=\"" << faceLst.size() << "\">" << endl;
209 forAll(zoneLst, zoneI)
211 os << "# <zone name=\"" << zoneLst[zoneI].name() << "\">" << endl;
213 if (surf.useFaceMap())
215 forAll(zoneLst[zoneI], localFaceI)
217 const Face& f = faceLst[faceMap[faceIndex++]];
225 // add optional zone information
226 os << ' ' << zoneI << endl;
231 forAll(zoneLst[zoneI], localFaceI)
233 const Face& f = faceLst[faceIndex++];
241 // add optional zone information
242 os << ' ' << zoneI << endl;
245 os << "# </zone>" << endl;
247 os << "# </faces>" << endl;
251 // ************************************************************************* //