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

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