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

readTRI.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     TRI (triangle) file reader. Comes out of e.g. AC3D.
00027     lines are 9 floats (3 points, each 3 floats) followed by hex colour.
00028     Is converted into regions: regions numbered from 0 up, each colour is
00029     region.
00030     Most of reading/stitching taken from STL reader.
00031     
00032 \*---------------------------------------------------------------------------*/
00033 
00034 #include "triSurface.H"
00035 #include "STLpoint.H"
00036 #include "SLList.H"
00037 #include "IFstream.H"
00038 #include "readHexLabel.H"
00039 #include "stringList.H"
00040 
00041 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00042 
00043 namespace Foam
00044 {
00045 
00046 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00047 
00048 bool triSurface::readTRI(const fileName& TRIfileName)
00049 {
00050     IFstream TRIfile(TRIfileName);
00051 
00052     if (!TRIfile.good())
00053     {
00054         FatalErrorIn("triSurface::readTRI(const fileName&)")
00055             << "Cannot read file " << TRIfileName
00056             << exit(FatalError);
00057     }
00058 
00059     SLList<STLpoint> STLpoints;
00060     SLList<label> STLlabels;
00061     HashTable<label, string> STLsolidNames;
00062 
00063     // Max region number so far
00064     label maxRegion = 0;
00065 
00066     while(TRIfile)
00067     {
00068         string line = getLineNoComment(TRIfile);
00069 
00070         if (line.empty())
00071         {
00072             break;
00073         }
00074 
00075         IStringStream lineStream(line);
00076 
00077         STLpoint p
00078         (
00079             readScalar(lineStream),
00080             readScalar(lineStream),
00081             readScalar(lineStream)
00082         );
00083 
00084         if (!lineStream) break;
00085 
00086         STLpoints.append(p);
00087         STLpoints.append
00088         (
00089             STLpoint
00090             (
00091                 readScalar(lineStream),
00092                 readScalar(lineStream),
00093                 readScalar(lineStream)
00094             )
00095         );
00096         STLpoints.append
00097         (
00098             STLpoint
00099             (
00100                 readScalar(lineStream),
00101                 readScalar(lineStream),
00102                 readScalar(lineStream)
00103             )
00104         );
00105 
00106         // Region/colour in .tri file starts with 0x. Skip.
00107 
00108         char zero;
00109         lineStream >> zero;
00110 
00111         word rawSolidName(lineStream);
00112 
00113         word solidName("patch" + rawSolidName(1, rawSolidName.size()-1));
00114 
00115         label region  = -1;
00116 
00117         HashTable<label, string>::const_iterator findName =
00118             STLsolidNames.find(solidName);
00119 
00120         if (findName != STLsolidNames.end())
00121         {
00122             region = findName();
00123         }
00124         else
00125         {
00126             Pout<< "Mapping triangle colour 0" << rawSolidName
00127                 << " to region " << maxRegion << " name " << solidName
00128                 << endl;
00129 
00130             region = maxRegion++;
00131             STLsolidNames.insert(solidName, region);
00132         }
00133         STLlabels.append(region);
00134     }
00135 
00136 
00137     pointField rawPoints(STLpoints.size());
00138 
00139     label i = 0;
00140     for
00141     (
00142         SLList<STLpoint>::iterator iter = STLpoints.begin();
00143         iter != STLpoints.end();
00144         ++iter
00145     )
00146     {
00147         rawPoints[i++] = *iter;
00148     }
00149 
00150     setSize(STLlabels.size());
00151 
00152     label pointI = 0;
00153     SLList<label>::iterator iter = STLlabels.begin();
00154     forAll (*this, i)
00155     {
00156         operator[](i)[0] = pointI++;
00157         operator[](i)[1] = pointI++;
00158         operator[](i)[2] = pointI++;
00159         operator[](i).region() = *iter;
00160         ++iter;
00161     }
00162 
00163     stitchTriangles(rawPoints);
00164 
00165     // Convert solidNames into regionNames
00166     stringList names(STLsolidNames.toc());
00167 
00168     patches_.setSize(names.size());
00169 
00170     forAll(names, nameI)
00171     {
00172         patches_[nameI].name() = names[nameI];
00173         patches_[nameI].geometricType() = "empty";
00174     }
00175 
00176     return true;
00177 }
00178 
00179 
00180 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00181 
00182 } // End namespace Foam
00183 
00184 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd