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 "OBJsurfaceFormat.H"
30 #include "IStringStream.H"
35 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
38 Foam::fileFormats::OBJsurfaceFormat<Face>::OBJsurfaceFormat
40 const fileName& filename
47 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
50 bool Foam::fileFormats::OBJsurfaceFormat<Face>::read
52 const fileName& filename
55 const bool mustTriangulate = this->isTri();
58 IFstream is(filename);
63 "fileFormats::OBJsurfaceFormat::read(const fileName&)"
65 << "Cannot read file " << filename
69 // assume that the groups are not intermixed
72 DynamicList<point> dynPoints;
73 DynamicList<Face> dynFaces;
74 DynamicList<label> dynZones;
75 DynamicList<word> dynNames;
76 DynamicList<label> dynSizes;
77 HashTable<label> lookup;
79 // place faces without a group in zone0
81 lookup.insert("zone0", zoneI);
82 dynNames.append("zone0");
87 string line = this->getLineNoComment(is);
89 // handle continuations
90 if (line[line.size()-1] == '\\')
92 line.substr(0, line.size()-1);
93 line += this->getLineNoComment(is);
97 IStringStream lineStream(line);
104 lineStream >> x >> y >> z;
105 dynPoints.append(point(x, y, z));
112 HashTable<label>::const_iterator fnd = lookup.find(name);
113 if (fnd != lookup.end())
117 // group appeared out of order
124 zoneI = dynSizes.size();
125 lookup.insert(name, zoneI);
126 dynNames.append(name);
132 DynamicList<label> dynVertices;
134 // Assume 'f' is followed by space.
135 string::size_type endNum = 1;
139 string::size_type startNum =
140 line.find_first_not_of(' ', endNum);
142 if (startNum == string::npos)
147 endNum = line.find(' ', startNum);
150 if (endNum != string::npos)
152 vertexSpec = line.substr(startNum, endNum-startNum);
156 vertexSpec = line.substr(startNum, line.size() - startNum);
159 string::size_type slashPos = vertexSpec.find('/');
162 if (slashPos != string::npos)
164 IStringStream intStream(vertexSpec.substr(0, slashPos));
170 IStringStream intStream(vertexSpec);
174 dynVertices.append(vertI - 1);
176 dynVertices.shrink();
178 UList<label>& f = static_cast<UList<label>&>(dynVertices);
180 if (mustTriangulate && f.size() > 3)
182 // simple face triangulation about f[0]
183 // points may be incomplete
184 for (label fp1 = 1; fp1 < f.size() - 1; fp1++)
186 label fp2 = f.fcIndex(fp1);
188 dynFaces.append(triFace(f[0], f[fp1], f[fp2]));
189 dynZones.append(zoneI);
195 dynFaces.append(Face(f));
196 dynZones.append(zoneI);
203 // transfer to normal lists
204 this->storedPoints().transfer(dynPoints);
206 sortFacesAndStore(dynFaces.xfer(), dynZones.xfer(), sorted);
208 // add zones, culling empty ones
209 this->addZones(dynSizes, dynNames, true);
215 void Foam::fileFormats::OBJsurfaceFormat<Face>::write
217 const fileName& filename,
218 const MeshedSurfaceProxy<Face>& surf
221 const pointField& pointLst = surf.points();
222 const List<Face>& faceLst = surf.faces();
223 const List<label>& faceMap = surf.faceMap();
225 // for no zones, suppress the group name
226 const List<surfZone>& zones =
228 surf.surfZones().size() > 1
230 : oneZone(faceLst, "")
233 const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
235 OFstream os(filename);
240 "fileFormats::OBJsurfaceFormat::write"
241 "(const fileName&, const MeshedSurfaceProxy<Face>&)"
243 << "Cannot open file for writing " << filename
248 os << "# Wavefront OBJ file written " << clock::dateTime().c_str() << nl
249 << "o " << os.name().lessExt().name() << nl
251 << "# points : " << pointLst.size() << nl
252 << "# faces : " << faceLst.size() << nl
253 << "# zones : " << zones.size() << nl;
255 // Print zone names as comment
258 os << "# " << zoneI << " " << zones[zoneI].name()
259 << " (nFaces: " << zones[zoneI].size() << ")" << nl;
263 << "# <points count=\"" << pointLst.size() << "\">" << nl;
265 // Write vertex coords
266 forAll(pointLst, ptI)
268 const point& pt = pointLst[ptI];
270 os << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
273 os << "# </points>" << nl
275 << "# <faces count=\"" << faceLst.size() << "\">" << endl;
281 const surfZone& zone = zones[zoneI];
283 if (zone.name().size())
285 os << "g " << zone.name() << endl;
290 forAll(zone, localFaceI)
292 const Face& f = faceLst[faceMap[faceIndex++]];
297 os << ' ' << f[fp] + 1;
304 forAll(zone, localFaceI)
306 const Face& f = faceLst[faceIndex++];
311 os << ' ' << f[fp] + 1;
317 os << "# </faces>" << endl;
321 // ************************************************************************* //