1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
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
13 the Free Software Foundation, either version 3 of the License, or
14 (at your 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, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "OBJedgeFormat.H"
29 #include "IStringStream.H"
34 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
36 Foam::fileFormats::OBJedgeFormat::OBJedgeFormat
38 const fileName& filename
45 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
47 bool Foam::fileFormats::OBJedgeFormat::read
49 const fileName& filename
54 IFstream is(filename);
59 "fileFormats::OBJedgeFormat::read(const fileName&)"
61 << "Cannot read file " << filename
65 DynamicList<point> dynPoints;
66 DynamicList<edge> dynEdges;
67 DynamicList<label> dynUsedPoints;
71 string line = this->getLineNoComment(is);
73 // handle continuations
74 if (line[line.size()-1] == '\\')
76 line.substr(0, line.size()-1);
77 line += this->getLineNoComment(is);
81 IStringStream lineStream(line);
88 lineStream >> x >> y >> z;
90 dynPoints.append(point(x, y, z));
91 dynUsedPoints.append(-1);
97 // Assume 'l' is followed by space.
98 string::size_type endNum = 1;
101 for (int count = 0; count < 2; ++count)
103 string::size_type startNum =
104 line.find_first_not_of(' ', endNum);
106 if (startNum == string::npos)
111 endNum = line.find(' ', startNum);
114 if (endNum != string::npos)
116 vertexSpec = line.substr(startNum, endNum-startNum);
120 vertexSpec = line.substr(startNum, line.size() - startNum);
123 string::size_type slashPos = vertexSpec.find('/');
126 if (slashPos != string::npos)
128 IStringStream intStream(vertexSpec.substr(0, slashPos));
134 IStringStream intStream(vertexSpec);
139 edgeRead[nVerts++] = (vertI - 1); // change to zero-offset
144 dynUsedPoints[edgeRead[0]] = edgeRead[0];
145 dynUsedPoints[edgeRead[1]] = edgeRead[1];
147 dynEdges.append(edgeRead);
152 // cull unused points
155 forAll(dynPoints, pointI)
157 if (dynUsedPoints[pointI] >= 0)
161 dynPoints[nUsed] = dynPoints[pointI];
162 dynUsedPoints[pointI] = nUsed; // new position
168 dynPoints.setSize(nUsed);
170 // transfer to normal lists
171 storedPoints().transfer(dynPoints);
173 // renumber edge vertices
174 if (nUsed != dynUsedPoints.size())
176 forAll(dynEdges, edgeI)
178 edge& e = dynEdges[edgeI];
180 e[0] = dynUsedPoints[e[0]];
181 e[1] = dynUsedPoints[e[1]];
184 storedEdges().transfer(dynEdges);
190 void Foam::fileFormats::OBJedgeFormat::write
192 const fileName& filename,
196 const pointField& pointLst = mesh.points();
197 const edgeList& edgeLst = mesh.edges();
199 OFstream os(filename);
204 "fileFormats::OBJedgeFormat::write"
205 "(const fileName&, const edgeMesh&)"
207 << "Cannot open file for writing " << filename
212 os << "# Wavefront OBJ file written " << clock::dateTime().c_str() << nl
213 << "o " << os.name().lessExt().name() << nl
215 << "# points : " << pointLst.size() << nl
216 << "# lines : " << edgeLst.size() << nl;
219 << "# <points count=\"" << pointLst.size() << "\">" << nl;
221 // Write vertex coords
222 forAll(pointLst, ptI)
224 const point& p = pointLst[ptI];
226 os << "v " << p.x() << ' ' << p.y() << ' ' << p.z() << nl;
229 os << "# </points>" << nl
231 << "# <edges count=\"" << edgeLst.size() << "\">" << endl;
233 // Write line connectivity
234 forAll(edgeLst, edgeI)
236 const edge& e = edgeLst[edgeI];
238 os << "l " << (e[0] + 1) << " " << (e[1] + 1) << nl;
240 os << "# </edges>" << endl;
244 // ************************************************************************* //