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

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