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

readOBJ.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 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include "triSurface.H"
00030 #include "IFstream.H"
00031 #include "IStringStream.H"
00032 
00033 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00034 
00035 namespace Foam
00036 {
00037 
00038 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
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         // Read first word
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             // Assume 'f' is followed by space.
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             // Do simple face triangulation around f[0].
00153             // Cannot use face::triangulation since no complete points yet.
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     // Convert groupToPatch to patchList.
00169     geometricSurfacePatchList patches(maxGroupID);
00170 
00171     if (maxGroupID == 0)
00172     {
00173         // Generate default patch
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     // Transfer DynamicLists to straight ones.
00197     pointField allPoints(points.xfer());
00198 
00199     // Create triSurface
00200     *this = triSurface(faces, patches, allPoints, true);
00201 
00202     return true;
00203 }
00204 
00205 
00206 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00207 
00208 } // End namespace Foam
00209 
00210 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd