1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd.
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/>.
25 Read obj line (not surface!) file and convert into vtk.
27 \*---------------------------------------------------------------------------*/
33 #include "IStringStream.H"
35 #include "DynamicList.H"
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 string getLine(std::ifstream& is)
48 std::getline(is, line);
50 while (line.size() && line[0] == '#');
56 // Read space-separated vertices (with optional '/' arguments)
57 labelList parseVertices(const string& line)
59 DynamicList<label> verts;
61 // Assume 'l' is followed by space.
62 string::size_type endNum = 1;
66 string::size_type startNum = line.find_first_not_of(' ', endNum);
68 if (startNum == string::npos)
73 endNum = line.find(' ', startNum);
76 if (endNum != string::npos)
78 vertexSpec = line.substr(startNum, endNum-startNum);
82 vertexSpec = line.substr(startNum, line.size() - startNum);
85 string::size_type slashPos = vertexSpec.find('/');
88 if (slashPos != string::npos)
90 IStringStream intStream(vertexSpec.substr(0, slashPos));
96 IStringStream intStream(vertexSpec);
100 verts.append(vertI - 1);
104 return verts.shrink();
110 int main(int argc, char *argv[])
112 argList::noParallel();
113 argList::validArgs.append("OBJ file");
114 argList::validArgs.append("output VTK file");
115 argList args(argc, argv);
117 const fileName objName = args[1];
118 const fileName outName = args[2];
120 std::ifstream OBJfile(objName.c_str());
124 FatalErrorIn(args.executable())
125 << "Cannot read file " << objName << exit(FatalError);
129 DynamicList<point> points;
130 DynamicList<labelList> polyLines;
131 DynamicList<labelList> polygons;
133 bool hasWarned = false;
136 while (OBJfile.good())
138 string line = getLine(OBJfile);
142 IStringStream lineStream(line);
150 lineStream >> x >> y >> z;
152 points.append(point(x, y, z));
156 polyLines.append(parseVertices(line));
160 polygons.append(parseVertices(line));
168 WarningIn(args.executable())
169 << "Unrecognized OBJ command " << cmd << nl
170 << "In line " << lineStream.str()
171 << " at linenumber " << lineNo << nl
172 << "Only recognized commands are 'v' and 'l'.\n"
173 << "If this is a surface command use surfaceConvert instead"
174 << " to convert to a file format that can be read by VTK"
182 // Write as vtk 'polydata' file
186 OFstream outFile(outName);
189 << "# vtk DataFile Version 2.0\n"
192 << "DATASET POLYDATA\n"
193 << "POINTS " << points.size() << " float\n";
197 const point& pt = points[i];
199 outFile << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
203 forAll(polyLines, polyI)
205 nItems += polyLines[polyI].size() + 1;
209 << "LINES " << polyLines.size() << ' ' << nItems << nl;
211 forAll(polyLines, polyI)
213 const labelList& line = polyLines[polyI];
215 outFile << line.size();
219 outFile << ' ' << line[i];
226 forAll(polygons, polyI)
228 nItems += polygons[polyI].size() + 1;
232 << "POLYGONS " << polygons.size() << ' ' << nItems << nl;
234 forAll(polygons, polyI)
236 const labelList& line = polygons[polyI];
238 outFile << line.size();
242 outFile << ' ' << line[i];
249 << "POINT_DATA " << points.size() << nl
250 << "SCALARS pointID float 1\n"
251 << "LOOKUP_TABLE default\n";
267 Info<< "End\n" << endl;
273 // ************************************************************************* //