readOBJ.C
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 #include "triSurface.H"
00030 #include "IFstream.H"
00031 #include "IStringStream.H"
00032
00033
00034
00035 namespace Foam
00036 {
00037
00038
00039
00040 bool triSurface::readOBJ(const fileName& OBJfileName)
00041 {
00042 IFstream OBJfile(OBJfileName);
00043
00044 if (!OBJfile.good())
00045 {
00046 FatalErrorIn("triSurface::readOBJ(const fileName&)")
00047 << "Cannot read file " << OBJfileName
00048 << exit(FatalError);
00049 }
00050
00051 DynamicList<point> points;
00052 DynamicList<labelledTri> faces;
00053 HashTable<label> groupToPatch;
00054
00055 label groupID = 0;
00056 label maxGroupID = 0;
00057
00058 while (OBJfile.good())
00059 {
00060 string line = getLineNoComment(OBJfile);
00061
00062 if (line[line.size()-1] == '\\')
00063 {
00064 line.substr(0, line.size()-1);
00065 line += getLineNoComment(OBJfile);
00066 }
00067
00068
00069 IStringStream lineStream(line);
00070 word cmd;
00071 lineStream >> cmd;
00072
00073 if (cmd == "v")
00074 {
00075 scalar x, y, z;
00076
00077 lineStream >> x >> y >> z;
00078
00079 points.append(point(x, y, z));
00080 }
00081 else if (cmd == "g")
00082 {
00083 word group;
00084
00085 lineStream >> group;
00086
00087 HashTable<label>::const_iterator findGroup =
00088 groupToPatch.find(group);
00089
00090 if (findGroup != groupToPatch.end())
00091 {
00092 groupID = findGroup();
00093 }
00094 else
00095 {
00096 groupID = maxGroupID;
00097
00098 groupToPatch.insert(group, groupID);
00099
00100 maxGroupID++;
00101 }
00102 }
00103 else if (cmd == "f")
00104 {
00105 DynamicList<label> verts;
00106
00107
00108 string::size_type endNum = 1;
00109
00110 while(true)
00111 {
00112 string::size_type startNum =
00113 line.find_first_not_of(' ', endNum);
00114
00115 if (startNum == string::npos)
00116 {
00117 break;
00118 }
00119
00120 endNum = line.find(' ', startNum);
00121
00122 string vertexSpec;
00123 if (endNum != string::npos)
00124 {
00125 vertexSpec = line.substr(startNum, endNum-startNum);
00126 }
00127 else
00128 {
00129 vertexSpec = line.substr(startNum, line.size() - startNum);
00130 }
00131
00132 string::size_type slashPos = vertexSpec.find('/');
00133
00134 label vertI = 0;
00135 if (slashPos != string::npos)
00136 {
00137 IStringStream intStream(vertexSpec.substr(0, slashPos));
00138
00139 intStream >> vertI;
00140 }
00141 else
00142 {
00143 IStringStream intStream(vertexSpec);
00144
00145 intStream >> vertI;
00146 }
00147 verts.append(vertI - 1);
00148 }
00149
00150 verts.shrink();
00151
00152
00153
00154 for (label fp = 1; fp < verts.size() - 1; fp++)
00155 {
00156 label fp1 = verts.fcIndex(fp);
00157
00158 labelledTri tri(verts[0], verts[fp], verts[fp1], groupID);
00159
00160 faces.append(tri);
00161 }
00162 }
00163 }
00164
00165 points.shrink();
00166 faces.shrink();
00167
00168
00169 geometricSurfacePatchList patches(maxGroupID);
00170
00171 if (maxGroupID == 0)
00172 {
00173
00174 patches.setSize(1);
00175 patches[0] = geometricSurfacePatch("empty", "patch0", 0);
00176 }
00177 else
00178 {
00179 for
00180 (
00181 HashTable<label>::const_iterator iter = groupToPatch.begin();
00182 iter != groupToPatch.end();
00183 ++iter
00184 )
00185 {
00186 patches[iter()] = geometricSurfacePatch
00187 (
00188 "empty",
00189 iter.key(),
00190 iter()
00191 );
00192 }
00193 }
00194
00195
00196
00197 pointField allPoints(points.xfer());
00198
00199
00200 *this = triSurface(faces, patches, allPoints, true);
00201
00202 return true;
00203 }
00204
00205
00206
00207
00208 }
00209
00210
Copyright © 2000-2009 OpenCFD Ltd