Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
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
00082 labelList parseVertices(const string& line)
00083 {
00084 DynamicList<label> verts;
00085
00086
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
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
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
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
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