FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 Application
00025     objToVTK
00026 
00027 Description
00028     Read obj line (not surface!) file and convert into vtk.
00029 
00030 Usage
00031 
00032     - objToVTK [OPTIONS] <OBJ file> <output VTK file>
00033 
00034     @param <OBJ file> \n
00035     @todo Detailed description of argument.
00036 
00037     @param <output VTK file> \n
00038     @todo Detailed description of argument.
00039 
00040     @param -case <dir>\n
00041     Case directory.
00042 
00043     @param -help \n
00044     Display help message.
00045 
00046     @param -doc \n
00047     Display Doxygen API documentation page for this application.
00048 
00049     @param -srcDoc \n
00050     Display Doxygen source documentation page for this application.
00051 
00052 \*---------------------------------------------------------------------------*/
00053 
00054 #include <OpenFOAM/argList.H>
00055 #include <OpenFOAM/OFstream.H>
00056 #include <fstream>
00057 #include <sstream>
00058 #include <OpenFOAM/IStringStream.H>
00059 #include <OpenFOAM/point.H>
00060 #include <OpenFOAM/DynamicList.H>
00061 
00062 
00063 using namespace Foam;
00064 
00065 
00066 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00067 
00068 string getLine(std::ifstream& is)
00069 {
00070     string line;
00071     do
00072     {
00073         std::getline(is, line);
00074     }
00075     while (line.size() && line[0] == '#');
00076 
00077     return line;
00078 }
00079 
00080 
00081 // Read space-separated vertices (with optional '/' arguments)
00082 labelList parseVertices(const string& line)
00083 {
00084     DynamicList<label> verts;
00085 
00086     // Assume 'l' is followed by space.
00087     string::size_type endNum = 1;
00088 
00089     do
00090     {
00091         string::size_type startNum = line.find_first_not_of(' ', endNum);
00092 
00093         if (startNum == string::npos)
00094         {
00095             break;
00096         }
00097 
00098         endNum = line.find(' ', startNum);
00099 
00100         string vertexSpec;
00101         if (endNum != string::npos)
00102         {
00103             vertexSpec = line.substr(startNum, endNum-startNum);
00104         }
00105         else
00106         {
00107             vertexSpec = line.substr(startNum, line.size() - startNum);
00108         }
00109 
00110         string::size_type slashPos = vertexSpec.find('/');
00111 
00112         label vertI = 0;
00113         if (slashPos != string::npos)
00114         {
00115             IStringStream intStream(vertexSpec.substr(0, slashPos));
00116 
00117             intStream >> vertI;
00118         }
00119         else
00120         {
00121             IStringStream intStream(vertexSpec);
00122 
00123             intStream >> vertI;
00124         }
00125         verts.append(vertI - 1);
00126     }
00127     while (true);
00128 
00129     return verts.shrink();
00130 }
00131 
00132 
00133 // Main program:
00134 
00135 int main(int argc, char *argv[])
00136 {
00137     argList::noParallel();
00138     argList::validArgs.clear();
00139     argList::validArgs.append("OBJ file");
00140     argList::validArgs.append("output VTK file");
00141     argList args(argc, argv);
00142 
00143     fileName objName(args.additionalArgs()[0]);
00144     fileName outName(args.additionalArgs()[1]);
00145 
00146     std::ifstream OBJfile(objName.c_str());
00147 
00148     if (!OBJfile.good())
00149     {
00150         FatalErrorIn(args.executable())
00151             << "Cannot read file " << objName << exit(FatalError);
00152     }
00153 
00154     // Points and lines
00155     DynamicList<point> points;
00156     DynamicList<labelList> polyLines;
00157     DynamicList<labelList> polygons;
00158 
00159     bool hasWarned = false;
00160 
00161     label lineNo = 0;
00162     while (OBJfile.good())
00163     {
00164         string line = getLine(OBJfile);
00165         lineNo++;
00166 
00167         // Read first word
00168         IStringStream lineStream(line);
00169         word cmd;
00170         lineStream >> cmd;
00171 
00172         if (cmd == "v")
00173         {
00174             scalar x, y, z;
00175 
00176             lineStream >> x >> y >> z;
00177 
00178             points.append(point(x, y, z));
00179         }
00180         else if (cmd == "l")
00181         {
00182             polyLines.append(parseVertices(line));
00183         }
00184         else if (cmd == "f")
00185         {
00186             polygons.append(parseVertices(line));
00187         }
00188         else if (cmd != "")
00189         {
00190             if (!hasWarned)
00191             {
00192                 hasWarned = true;
00193 
00194                 WarningIn(args.executable())
00195                     << "Unrecognized OBJ command " << cmd << nl
00196                     << "In line " << lineStream.str()
00197                     << " at linenumber " << lineNo << nl
00198                     << "Only recognized commands are 'v' and 'l'.\n"
00199                     << "If this is a surface command use surfaceConvert instead"
00200                     << " to convert to a file format that can be read by VTK"
00201                     << endl;
00202             }
00203         }
00204     }
00205 
00206 
00207     //
00208     // Write as vtk 'polydata' file
00209     //
00210 
00211 
00212     OFstream outFile(outName);
00213 
00214     outFile
00215         << "# vtk DataFile Version 2.0\n"
00216         << objName << nl
00217         << "ASCII\n"
00218         << "DATASET POLYDATA\n"
00219         << "POINTS " << points.size() << " float\n";
00220 
00221     forAll(points, i)
00222     {
00223         const point& pt = points[i];
00224 
00225         outFile << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
00226     }
00227 
00228     label nItems = 0;
00229     forAll(polyLines, polyI)
00230     {
00231         nItems += polyLines[polyI].size() + 1;
00232     }
00233 
00234     outFile
00235         << "LINES " << polyLines.size() << ' ' << nItems << nl;
00236 
00237     forAll(polyLines, polyI)
00238     {
00239         const labelList& line = polyLines[polyI];
00240 
00241         outFile << line.size();
00242 
00243         forAll(line, i)
00244         {
00245             outFile << ' ' << line[i];
00246         }
00247         outFile << nl;
00248     }
00249 
00250 
00251     nItems = 0;
00252     forAll(polygons, polyI)
00253     {
00254         nItems += polygons[polyI].size() + 1;
00255     }
00256 
00257     outFile
00258         << "POLYGONS " << polygons.size() << ' ' << nItems << nl;
00259 
00260     forAll(polygons, polyI)
00261     {
00262         const labelList& line = polygons[polyI];
00263 
00264         outFile << line.size();
00265 
00266         forAll(line, i)
00267         {
00268             outFile << ' ' << line[i];
00269         }
00270         outFile << nl;
00271     }
00272 
00273 
00274     outFile
00275         << "POINT_DATA " << points.size() << nl
00276         << "SCALARS pointID float 1\n"
00277         << "LOOKUP_TABLE default\n";
00278 
00279     forAll(points, i)
00280     {
00281         outFile << i;
00282 
00283         if ((i % 10) == 1)
00284         {
00285             outFile << nl;
00286         }
00287         else
00288         {
00289             outFile << ' ';
00290         }
00291     }
00292 
00293     Info << "End\n" << endl;
00294 
00295     return 0;
00296 }
00297 
00298 
00299 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines