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

TRIsurfaceFormatCore.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 \*---------------------------------------------------------------------------*/
00026 
00027 #include "TRIsurfaceFormat.H"
00028 #include "IFstream.H"
00029 #include "IOmanip.H"
00030 #include "IStringStream.H"
00031 #include "Map.H"
00032 
00033 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00034 
00035 Foam::fileFormats::TRIsurfaceFormatCore::TRIsurfaceFormatCore
00036 (
00037     const fileName& filename
00038 )
00039 :
00040     sorted_(true),
00041     points_(0),
00042     zoneIds_(0),
00043     sizes_(0)
00044 {
00045     read(filename);
00046 }
00047 
00048 
00049 // * * * * * * * * * * * * * * * * Destructors * * * * * * * * * * * * * * * //
00050 
00051 Foam::fileFormats::TRIsurfaceFormatCore::~TRIsurfaceFormatCore()
00052 {}
00053 
00054 
00055 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00056 
00057 bool Foam::fileFormats::TRIsurfaceFormatCore::read
00058 (
00059     const fileName& filename
00060 )
00061 {
00062     this->clear();
00063     sorted_ = true;
00064 
00065     IFstream is(filename);
00066     if (!is.good())
00067     {
00068         FatalErrorIn
00069         (
00070             "fileFormats::TRIsurfaceFormatCore::read(const fileName&)"
00071         )
00072             << "Cannot read file " << filename
00073             << exit(FatalError);
00074     }
00075 
00076     // uses similar structure as STL, just some points
00077     // the rest of the reader resembles the STL binary reader
00078     DynamicList<point> dynPoints;
00079     DynamicList<label> dynZones;
00080     DynamicList<label> dynSizes;
00081     HashTable<label>   lookup;
00082 
00083     // place faces without a group in zone0
00084     label zoneI = 0;
00085     dynSizes.append(zoneI);
00086     lookup.insert("zoneI", zoneI);
00087 
00088     while (is.good())
00089     {
00090         string line = this->getLineNoComment(is);
00091 
00092         // handle continuations ?
00093         //          if (line[line.size()-1] == '\\')
00094         //          {
00095         //              line.substr(0, line.size()-1);
00096         //              line += this->getLineNoComment(is);
00097         //          }
00098 
00099         IStringStream lineStream(line);
00100 
00101         point p
00102         (
00103             readScalar(lineStream),
00104             readScalar(lineStream),
00105             readScalar(lineStream)
00106         );
00107 
00108         if (!lineStream) break;
00109 
00110         dynPoints.append(p);
00111         dynPoints.append
00112         (
00113             point
00114             (
00115                 readScalar(lineStream),
00116                 readScalar(lineStream),
00117                 readScalar(lineStream)
00118             )
00119         );
00120         dynPoints.append
00121         (
00122             point
00123             (
00124                 readScalar(lineStream),
00125                 readScalar(lineStream),
00126                 readScalar(lineStream)
00127             )
00128         );
00129 
00130         // zone/colour in .tri file starts with 0x. Skip.
00131         // ie, instead of having 0xFF, skip 0 and leave xFF to
00132         // get read as a word and name it "zoneFF"
00133 
00134         char zero;
00135         lineStream >> zero;
00136 
00137         word rawName(lineStream);
00138         word name("zone" + rawName(1, rawName.size()-1));
00139 
00140         HashTable<label>::const_iterator fnd = lookup.find(name);
00141         if (fnd != lookup.end())
00142         {
00143             if (zoneI != fnd())
00144             {
00145                 // group appeared out of order
00146                 sorted_ = false;
00147             }
00148             zoneI = fnd();
00149         }
00150         else
00151         {
00152             zoneI = dynSizes.size();
00153             lookup.insert(name, zoneI);
00154             dynSizes.append(0);
00155         }
00156 
00157         dynZones.append(zoneI);
00158         dynSizes[zoneI]++;
00159     }
00160 
00161     // skip empty groups
00162     label nZone = 0;
00163     forAll(dynSizes, zoneI)
00164     {
00165         if (dynSizes[zoneI])
00166         {
00167             if (nZone != zoneI)
00168             {
00169                 dynSizes[nZone] = dynSizes[zoneI];
00170             }
00171             nZone++;
00172         }
00173     }
00174     // truncate addressed size
00175     dynSizes.setCapacity(nZone);
00176 
00177     // transfer to normal lists
00178     points_.transfer(dynPoints);
00179     zoneIds_.transfer(dynZones);
00180     sizes_.transfer(dynSizes);
00181 
00182     return true;
00183 }
00184 
00185 
00186 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd