1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 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 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
26 Read obj line (not surface!) file and convert into vtk.
28 \*---------------------------------------------------------------------------*/
34 #include "IStringStream.H"
36 #include "DynamicList.H"
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 string getLine(std::ifstream& is)
49 std::getline(is, line);
51 while (line.size() && line[0] == '#');
57 // Read space-separated vertices (with optional '/' arguments)
58 labelList parseVertices(const string& line)
60 DynamicList<label> verts;
62 // Assume 'l' is followed by space.
63 string::size_type endNum = 1;
67 string::size_type startNum = line.find_first_not_of(' ', endNum);
69 if (startNum == string::npos)
74 endNum = line.find(' ', startNum);
77 if (endNum != string::npos)
79 vertexSpec = line.substr(startNum, endNum-startNum);
83 vertexSpec = line.substr(startNum, line.size() - startNum);
86 string::size_type slashPos = vertexSpec.find('/');
89 if (slashPos != string::npos)
91 IStringStream intStream(vertexSpec.substr(0, slashPos));
97 IStringStream intStream(vertexSpec);
101 verts.append(vertI - 1);
105 return verts.shrink();
111 int main(int argc, char *argv[])
113 argList::noParallel();
114 argList::validArgs.clear();
115 argList::validArgs.append("OBJ file");
116 argList::validArgs.append("output VTK file");
117 argList::argList args(argc, argv);
119 fileName objName(args.additionalArgs()[0]);
120 fileName outName(args.additionalArgs()[1]);
122 std::ifstream OBJfile(objName.c_str());
126 FatalErrorIn(args.executable())
127 << "Cannot read file " << objName << exit(FatalError);
131 DynamicList<point> points;
132 DynamicList<labelList> polyLines;
133 DynamicList<labelList> polygons;
135 bool hasWarned = false;
138 while (OBJfile.good())
140 string line = getLine(OBJfile);
144 IStringStream lineStream(line);
152 lineStream >> x >> y >> z;
154 points.append(point(x, y, z));
158 polyLines.append(parseVertices(line));
162 polygons.append(parseVertices(line));
170 WarningIn(args.executable())
171 << "Unrecognized OBJ command " << cmd << nl
172 << "In line " << lineStream.str()
173 << " at linenumber " << lineNo << nl
174 << "Only recognized commands are 'v' and 'l'.\n"
175 << "If this is a surface command use surfaceConvert instead"
176 << " to convert to a file format that can be read by VTK"
184 // Write as vtk 'polydata' file
188 OFstream outFile(outName);
191 << "# vtk DataFile Version 2.0\n"
194 << "DATASET POLYDATA\n"
195 << "POINTS " << points.size() << " float\n";
199 const point& pt = points[i];
201 outFile << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
205 forAll(polyLines, polyI)
207 nItems += polyLines[polyI].size() + 1;
211 << "LINES " << polyLines.size() << ' ' << nItems << nl;
213 forAll(polyLines, polyI)
215 const labelList& line = polyLines[polyI];
217 outFile << line.size();
221 outFile << ' ' << line[i];
228 forAll(polygons, polyI)
230 nItems += polygons[polyI].size() + 1;
234 << "POLYGONS " << polygons.size() << ' ' << nItems << nl;
236 forAll(polygons, polyI)
238 const labelList& line = polygons[polyI];
240 outFile << line.size();
244 outFile << ' ' << line[i];
251 << "POINT_DATA " << points.size() << nl
252 << "SCALARS pointID float 1\n"
253 << "LOOKUP_TABLE default\n";
269 Info << "End\n" << endl;
275 // ************************************************************************* //