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