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

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-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 Description
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 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
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         // Read first word
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             // Assume 'f' is followed by space.
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             // Do simple face triangulation around f[0].
00152             // Cannot use face::triangulation since no complete points yet.
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     // Convert groupToPatch to patchList.
00168     geometricSurfacePatchList patches(maxGroupID);
00169 
00170     if (maxGroupID == 0)
00171     {
00172         // Generate default patch
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     // Transfer DynamicLists to straight ones.
00196     pointField allPoints(points.xfer());
00197 
00198     // Create triSurface
00199     *this = triSurface(faces, patches, allPoints, true);
00200 
00201     return true;
00202 }
00203 
00204 
00205 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00206 
00207 } // End namespace Foam
00208 
00209 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines