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

readAC.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     Reader for .ac files generated by AC3D.
00028 
00029     See http://www.ac3d.org/ac3d/man/ac3dfileformat.html
00030 
00031 \*---------------------------------------------------------------------------*/
00032 
00033 #include "triSurface.H"
00034 #include "IFstream.H"
00035 #include "IStringStream.H"
00036 #include "transform.H"
00037 #include "tensor.H"
00038 
00039 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00040 
00041 namespace Foam
00042 {
00043 
00044 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00045 
00046 static label parseInt(const string& str)
00047 {
00048     IStringStream intStream(str);
00049 
00050     label a;
00051 
00052     intStream >> a;
00053 
00054     return a;
00055 }
00056 
00057 
00058 static bool readCmd(IFstream& ACfile, string& cmd, string& args)
00059 {
00060     if (ACfile.good())
00061     {
00062         string line;
00063         ACfile.getLine(line);
00064 
00065         string::size_type space = line.find(' ');
00066 
00067         if (space != string::npos)
00068         {
00069             cmd  = line.substr(0, space);
00070             args = line.substr(space+1);
00071 
00072             return true;
00073         }
00074     }
00075     return false;
00076 }
00077 
00078 
00079 // Read up to line starting with cmd. Sets args to rest of line.
00080 // Returns true if found, false if stream is not good anymore.
00081 static bool readUpto
00082 (
00083     const string& cmd,
00084     IFstream& ACfile,
00085     string& args
00086 )
00087 {
00088     while (ACfile.good())
00089     {
00090         string line;
00091         ACfile.getLine(line);
00092 
00093         string::size_type space = line.find(' ');
00094 
00095         if (space != string::npos && line.substr(0, space) == cmd)
00096         {
00097             args = line.substr(space+1);            
00098             return true;
00099         }
00100     }
00101     return false;
00102 }
00103 
00104 
00105 // Likewise but throws error if cmd not found
00106 static void readUpto
00107 (
00108     const string& cmd,
00109     IFstream& ACfile,
00110     string& args,
00111     const string errorMsg
00112 )
00113 {
00114     if (!readUpto(cmd, ACfile, args))
00115     {
00116         FatalErrorIn("triSurface::readAC(const fileName&)")
00117             << "Cannot find command " << cmd
00118             << errorMsg << exit(FatalError);
00119     }
00120 }
00121 
00122 
00123 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00124 
00125 bool triSurface::readAC(const fileName& ACfileName)
00126 {
00127     IFstream ACfile(ACfileName);
00128 
00129     if (!ACfile.good())
00130     {
00131         FatalErrorIn("triSurface::readAC(const fileName&)")
00132             << "Cannot read file " << ACfileName
00133             << exit(FatalError);
00134     }
00135 
00136     string line;
00137     ACfile.getLine(line);
00138 
00139     string version = line.substr(4);
00140 
00141     if (version != "b")
00142     {
00143         WarningIn("bool triSurface::readAC(const fileName& ACfileName)")
00144             << "When reading AC3D file " << ACfileName
00145             << " read header " << line << " with version " << version
00146             << endl << "Only tested reading with version 'b'."
00147             << " This might give problems" << endl;
00148     }
00149 
00150     string cmd;
00151 
00152     string args;
00153 
00154     if (!readUpto("OBJECT", ACfile, args) || (args != "world"))
00155     {
00156         FatalErrorIn("bool triSurface::readAC(const fileName& ACfileName)")
00157             << "Cannot find \"OBJECT world\" in file " << ACfileName
00158             << exit(FatalError);
00159     }
00160 
00161     // Number of kids = patches
00162 
00163     readUpto("kids", ACfile, args, "");
00164 
00165     label nPatches = parseInt(args);
00166 
00167     // Storage for patches and unmerged points and faces
00168 
00169     DynamicList<point> points;
00170     DynamicList<labelledTri> faces;
00171     geometricSurfacePatchList patches(nPatches);
00172 
00173 
00174     // Start of vertices for object/patch
00175     label patchStartVert = 0;
00176 
00177     for (label patchI = 0; patchI < nPatches; patchI++)
00178     {
00179         readUpto("OBJECT", ACfile, args, " while reading patch " + patchI);
00180 
00181         // Object global values
00182         string patchName = string("patch") + name(patchI);
00183         label nVerts = 0;
00184         tensor rot(I);
00185         vector loc(0, 0, 0);
00186 
00187         // Read all info for current patch
00188         while (ACfile.good())
00189         {
00190             // Read line and get first word. If end of file break since
00191             // patch should always end with 'kids' command ?not sure.
00192             if (!readCmd(ACfile, cmd, args))
00193             {
00194                 FatalErrorIn("triSurface::readAC(const fileName&)")
00195                     << "Did not read up to \"kids 0\" while reading patch "
00196                     << patchI << " from file " << ACfileName
00197                     << exit(FatalError);
00198             }
00199 
00200             if (cmd == "name")
00201             {
00202                 IStringStream nameStream(args);
00203 
00204                 nameStream >> patchName;
00205             }
00206             else if (cmd == "rot")
00207             {
00208                 // rot %f %f %f  %f %f %f  %f %f %f
00209                 IStringStream lineStream(args);
00210 
00211                 lineStream
00212                     >> rot.xx() >> rot.xy() >> rot.xz()
00213                     >> rot.yx() >> rot.yy() >> rot.yz()
00214                     >> rot.zx() >> rot.zy() >> rot.zz();
00215 
00216                 WarningIn("triSurface::readAC(const fileName&)")
00217                     << "rot (rotation tensor) command not implemented"
00218                     << "Line:" << cmd << ' ' << args << endl
00219                     << "while reading patch " << patchI << endl;
00220             }
00221             else if (cmd == "loc")
00222             {
00223                 IStringStream lineStream(args);
00224 
00225                 lineStream >> loc.x() >> loc.y() >> loc.z();
00226             }
00227             else if (cmd == "numvert")
00228             {
00229                 nVerts = parseInt(args);
00230 
00231                 for (label vertI = 0; vertI < nVerts; vertI++)
00232                 {
00233                     ACfile.getLine(line);
00234 
00235                     IStringStream lineStream(line);
00236 
00237                     point pt;
00238 
00239                     lineStream >> pt.x() >> pt.y() >> pt.z();
00240 
00241                     // Offset with current translation vector
00242                     points.append(pt+loc);
00243                 }
00244             }
00245             else if (cmd == "numsurf")
00246             {
00247                 label nTris = parseInt(args);
00248 
00249                 for (label triI = 0; triI < nTris; triI++)
00250                 {
00251                     static string errorMsg =
00252                         string(" while reading face ")
00253                       + name(triI) + " on patch " + name(patchI)
00254                       + " from file " + ACfileName;
00255 
00256                     readUpto("SURF", ACfile, args, errorMsg);
00257                     readUpto("mat", ACfile, args, errorMsg);
00258                     readUpto("refs", ACfile, args, errorMsg);
00259 
00260                     label size = parseInt(args);
00261 
00262                     if (size != 3)
00263                     {
00264                         FatalErrorIn("triSurface::readAC(const fileName&)")
00265                             << "Can only read surfaces with 3 vertices."
00266                             << endl
00267                             << "Detected " << size << " when reading triangle "
00268                             << triI << " of patch " << patchI
00269                             << exit(FatalError);
00270                     }
00271 
00272                     ACfile.getLine(line);
00273 
00274                     label v0 = parseInt(line);
00275 
00276                     ACfile.getLine(line);
00277 
00278                     label v1 = parseInt(line);
00279 
00280                     ACfile.getLine(line);
00281 
00282                     label v2 = parseInt(line);
00283 
00284                     faces.append
00285                     (
00286                         labelledTri 
00287                         (
00288                             v0 + patchStartVert,
00289                             v1 + patchStartVert,
00290                             v2 + patchStartVert,
00291                             patchI
00292                         )
00293                     );
00294                 }
00295 
00296                 // Done the current patch. Increment the offset vertices are
00297                 // stored at
00298                 patchStartVert += nVerts;
00299             }
00300             else if (cmd == "kids")
00301             {
00302                 // 'kids' denotes the end of the current patch.
00303 
00304                 label nKids = parseInt(args);
00305 
00306                 if (nKids != 0)
00307                 {
00308                     FatalErrorIn("triSurface::readAC(const fileName&)")
00309                         << "Can only read objects without kids."
00310                         << " Encountered " << nKids << " kids when"
00311                         << " reading patch " << patchI
00312                         << exit(FatalError);
00313                 }
00314 
00315                 patches[patchI] =
00316                     geometricSurfacePatch
00317                     (
00318                         "empty",
00319                         word(patchName),
00320                         patchI
00321                     );
00322 
00323                 // Stop reading current patch
00324                 break;
00325             }
00326         }
00327     }
00328 
00329     faces.shrink();
00330 
00331     // Transfer DynamicLists to straight ones.
00332     pointField allPoints(points.xfer());
00333 
00334     *this = triSurface(faces, patches, allPoints, true);
00335 
00336     stitchTriangles(allPoints);
00337 
00338     return true;
00339 }
00340 
00341 
00342 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00343 
00344 } // End namespace Foam
00345 
00346 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd