OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

objToVTK.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026     Read obj line (not surface!) file and convert into vtk.
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 #include "argList.H"
00031 #include "OFstream.H"
00032 #include <fstream>
00033 #include <sstream>
00034 #include "IStringStream.H"
00035 #include "point.H"
00036 #include "DynamicList.H"
00037 
00038 
00039 using namespace Foam;
00040 
00041 
00042 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00043 
00044 string getLine(std::ifstream& is)
00045 {
00046     string line;
00047     do
00048     {
00049         std::getline(is, line);
00050     }
00051     while (line.size() && line[0] == '#');
00052 
00053     return line;
00054 }
00055 
00056 
00057 // Read space-separated vertices (with optional '/' arguments)
00058 labelList parseVertices(const string& line)
00059 {
00060     DynamicList<label> verts;
00061 
00062     // Assume 'l' is followed by space.
00063     string::size_type endNum = 1;
00064 
00065     do
00066     {
00067         string::size_type startNum = line.find_first_not_of(' ', endNum);
00068 
00069         if (startNum == string::npos)
00070         {
00071             break;
00072         }
00073 
00074         endNum = line.find(' ', startNum);
00075 
00076         string vertexSpec;
00077         if (endNum != string::npos)
00078         {
00079             vertexSpec = line.substr(startNum, endNum-startNum);
00080         }
00081         else
00082         {
00083             vertexSpec = line.substr(startNum, line.size() - startNum);
00084         }
00085 
00086         string::size_type slashPos = vertexSpec.find('/');
00087 
00088         label vertI = 0;
00089         if (slashPos != string::npos)
00090         {
00091             IStringStream intStream(vertexSpec.substr(0, slashPos));
00092 
00093             intStream >> vertI;
00094         }
00095         else
00096         {
00097             IStringStream intStream(vertexSpec);
00098 
00099             intStream >> vertI;
00100         }
00101         verts.append(vertI - 1);
00102     }
00103     while (true);
00104 
00105     return verts.shrink();
00106 }
00107 
00108 
00109 // Main program:
00110 
00111 int main(int argc, char *argv[])
00112 {
00113     argList::noParallel();
00114     argList::validArgs.clear();
00115     argList::validArgs.append("OBJ file");
00116     argList::validArgs.append("output VTK file");
00117     argList::argList args(argc, argv);
00118 
00119     fileName objName(args.additionalArgs()[0]);
00120     fileName outName(args.additionalArgs()[1]);
00121 
00122     std::ifstream OBJfile(objName.c_str());
00123 
00124     if (!OBJfile.good())
00125     {
00126         FatalErrorIn(args.executable())
00127             << "Cannot read file " << objName << exit(FatalError);
00128     }
00129 
00130     // Points and lines
00131     DynamicList<point> points;
00132     DynamicList<labelList> polyLines;
00133     DynamicList<labelList> polygons;
00134 
00135     bool hasWarned = false;
00136 
00137     label lineNo = 0;
00138     while (OBJfile.good())
00139     {
00140         string line = getLine(OBJfile);
00141         lineNo++;
00142 
00143         // Read first word
00144         IStringStream lineStream(line);
00145         word cmd;
00146         lineStream >> cmd;
00147 
00148         if (cmd == "v")
00149         {
00150             scalar x, y, z;
00151 
00152             lineStream >> x >> y >> z;
00153 
00154             points.append(point(x, y, z));
00155         }
00156         else if (cmd == "l")
00157         {
00158             polyLines.append(parseVertices(line));
00159         }
00160         else if (cmd == "f")
00161         {
00162             polygons.append(parseVertices(line));
00163         }
00164         else if (cmd != "")
00165         {
00166             if (!hasWarned)
00167             {
00168                 hasWarned = true;
00169 
00170                 WarningIn(args.executable())
00171                     << "Unrecognized OBJ command " << cmd << nl
00172                     << "In line " << lineStream.str()
00173                     << " at linenumber " << lineNo << nl
00174                     << "Only recognized commands are 'v' and 'l'.\n"
00175                     << "If this is a surface command use surfaceConvert instead"
00176                     << " to convert to a file format that can be read by VTK"
00177                     << endl;
00178             }
00179         }
00180     }
00181 
00182 
00183     //
00184     // Write as vtk 'polydata' file
00185     //
00186 
00187 
00188     OFstream outFile(outName);
00189 
00190     outFile
00191         << "# vtk DataFile Version 2.0\n"
00192         << objName << nl
00193         << "ASCII\n"
00194         << "DATASET POLYDATA\n"
00195         << "POINTS " << points.size() << " float\n";
00196 
00197     forAll(points, i)
00198     {
00199         const point& pt = points[i];
00200 
00201         outFile << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
00202     }
00203 
00204     label nItems = 0;
00205     forAll(polyLines, polyI)
00206     {
00207         nItems += polyLines[polyI].size() + 1;
00208     }
00209 
00210     outFile
00211         << "LINES " << polyLines.size() << ' ' << nItems << nl;
00212 
00213     forAll(polyLines, polyI)
00214     {
00215         const labelList& line = polyLines[polyI];
00216 
00217         outFile << line.size();
00218 
00219         forAll(line, i)
00220         {
00221             outFile << ' ' << line[i];
00222         }
00223         outFile << nl;
00224     }
00225 
00226 
00227     nItems = 0;
00228     forAll(polygons, polyI)
00229     {
00230         nItems += polygons[polyI].size() + 1;
00231     }
00232 
00233     outFile
00234         << "POLYGONS " << polygons.size() << ' ' << nItems << nl;
00235 
00236     forAll(polygons, polyI)
00237     {
00238         const labelList& line = polygons[polyI];
00239 
00240         outFile << line.size();
00241 
00242         forAll(line, i)
00243         {
00244             outFile << ' ' << line[i];
00245         }
00246         outFile << nl;
00247     }
00248 
00249 
00250     outFile
00251         << "POINT_DATA " << points.size() << nl
00252         << "SCALARS pointID float 1\n"
00253         << "LOOKUP_TABLE default\n";
00254 
00255     forAll(points, i)
00256     {
00257         outFile << i;
00258 
00259         if ((i % 10) == 1)
00260         {
00261             outFile << nl;
00262         }
00263         else
00264         {
00265             outFile << ' ';
00266         }
00267     }
00268 
00269     Info << "End\n" << endl;
00270 
00271     return 0;
00272 }
00273 
00274 
00275 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd