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

OFFsurfaceFormat.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 "OFFsurfaceFormat.H"
00028 #include "clock.H"
00029 #include "IFstream.H"
00030 #include "IStringStream.H"
00031 #include "Ostream.H"
00032 #include "OFstream.H"
00033 
00034 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00035 
00036 template<class Face>
00037 Foam::fileFormats::OFFsurfaceFormat<Face>::OFFsurfaceFormat
00038 (
00039     const fileName& filename
00040 )
00041 {
00042     read(filename);
00043 }
00044 
00045 
00046 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00047 
00048 template<class Face>
00049 bool Foam::fileFormats::OFFsurfaceFormat<Face>::read
00050 (
00051     const fileName& filename
00052 )
00053 {
00054     const bool mustTriangulate = this->isTri();
00055     this->clear();
00056 
00057     IFstream is(filename);
00058     if (!is.good())
00059     {
00060         FatalErrorIn
00061         (
00062             "fileFormats::OFFsurfaceFormat::read(const fileName&)"
00063         )
00064             << "Cannot read file " << filename
00065             << exit(FatalError);
00066     }
00067 
00068     // Read header
00069     string hdr = this->getLineNoComment(is);
00070     if (hdr != "OFF")
00071     {
00072         FatalErrorIn
00073         (
00074             "fileFormats::OFFsurfaceFormat::read(const fileName&)"
00075         )
00076             << "OFF file " << filename << " does not start with 'OFF'"
00077             << exit(FatalError);
00078     }
00079 
00080 
00081     // get dimensions
00082     label nPoints, nElems, nEdges;
00083 
00084     string line = this->getLineNoComment(is);
00085     {
00086         IStringStream lineStream(line);
00087         lineStream >> nPoints >> nElems >> nEdges;
00088     }
00089 
00090     // Read points
00091     pointField pointLst(nPoints);
00092     forAll(pointLst, pointI)
00093     {
00094         scalar x, y, z;
00095         line = this->getLineNoComment(is);
00096         {
00097             IStringStream lineStream(line);
00098             lineStream >> x >> y >> z;
00099         }
00100         pointLst[pointI] = point(x, y, z);
00101     }
00102 
00103     // Read faces - ignore optional zone information
00104     // use a DynamicList for possible on-the-fly triangulation
00105     DynamicList<Face>  dynFaces(nElems);
00106 
00107     for (label faceI = 0; faceI < nElems; ++faceI)
00108     {
00109         line = this->getLineNoComment(is);
00110 
00111         {
00112             IStringStream lineStream(line);
00113 
00114             label nVerts;
00115             lineStream >> nVerts;
00116 
00117             List<label> verts(nVerts);
00118 
00119             forAll(verts, vertI)
00120             {
00121                 lineStream >> verts[vertI];
00122             }
00123 
00124             UList<label>& f = static_cast<UList<label>&>(verts);
00125 
00126             if (mustTriangulate && f.size() > 3)
00127             {
00128                 // simple face triangulation about f[0]
00129                 // cannot use face::triangulation (points may be incomplete)
00130                 for (label fp1 = 1; fp1 < f.size() - 1; fp1++)
00131                 {
00132                     label fp2 = f.fcIndex(fp1);
00133 
00134                     dynFaces.append(triFace(f[0], f[fp1], f[fp2]));
00135                 }
00136             }
00137             else
00138             {
00139                 dynFaces.append(Face(f));
00140             }
00141         }
00142     }
00143 
00144     // transfer to normal lists, no zone information
00145     reset(pointLst.xfer(), dynFaces.xfer(), Xfer<surfZoneList>());
00146 
00147     return true;
00148 }
00149 
00150 
00151 template<class Face>
00152 void Foam::fileFormats::OFFsurfaceFormat<Face>::write
00153 (
00154     const fileName& filename,
00155     const MeshedSurfaceProxy<Face>& surf
00156 )
00157 {
00158     const pointField& pointLst = surf.points();
00159     const List<Face>&  faceLst = surf.faces();
00160     const List<label>& faceMap = surf.faceMap();
00161     const List<surfZone>& zoneLst = surf.surfZones();
00162 
00163     OFstream os(filename);
00164     if (!os.good())
00165     {
00166         FatalErrorIn
00167         (
00168             "fileFormats::OFFsurfaceFormat::write"
00169             "(const fileName&, const MeshedSurfaceProxy<Face>&)"
00170         )
00171             << "Cannot open file for writing " << filename
00172             << exit(FatalError);
00173     }
00174 
00175     // Write header
00176     os  << "OFF" << endl
00177         << "# Geomview OFF file written " << clock::dateTime().c_str() << nl
00178         << nl
00179         << "# points : " << pointLst.size() << nl
00180         << "# faces  : " << faceLst.size() << nl
00181         << "# zones  : " << zoneLst.size() << nl;
00182 
00183     // Print zone names as comment
00184     forAll(zoneLst, zoneI)
00185     {
00186         os  << "#   " << zoneI << "  " << zoneLst[zoneI].name()
00187             << "  (nFaces: " << zoneLst[zoneI].size() << ")" << nl;
00188     }
00189 
00190     os  << nl
00191         << "# nPoints  nFaces  nEdges" << nl
00192         << pointLst.size() << ' ' << faceLst.size() << ' ' << 0 << nl
00193         << nl
00194         << "# <points count=\"" << pointLst.size() << "\">" << endl;
00195 
00196     // Write vertex coords
00197     forAll(pointLst, ptI)
00198     {
00199         os  << pointLst[ptI].x() << ' '
00200             << pointLst[ptI].y() << ' '
00201             << pointLst[ptI].z() << " #" << ptI << endl;
00202     }
00203 
00204     os  << "# </points>" << nl
00205         << nl
00206         << "# <faces count=\"" << faceLst.size() << "\">" << endl;
00207 
00208     label faceIndex = 0;
00209     forAll(zoneLst, zoneI)
00210     {
00211         os << "# <zone name=\"" << zoneLst[zoneI].name() << "\">" << endl;
00212 
00213         if (surf.useFaceMap())
00214         {
00215             forAll(zoneLst[zoneI], localFaceI)
00216             {
00217                 const Face& f = faceLst[faceMap[faceIndex++]];
00218 
00219                 os << f.size();
00220                 forAll(f, fp)
00221                 {
00222                     os << ' ' << f[fp];
00223                 }
00224 
00225                 // add optional zone information
00226                 os << ' ' << zoneI << endl;
00227             }
00228         }
00229         else
00230         {
00231             forAll(zoneLst[zoneI], localFaceI)
00232             {
00233                 const Face& f = faceLst[faceIndex++];
00234 
00235                 os << f.size();
00236                 forAll(f, fp)
00237                 {
00238                     os << ' ' << f[fp];
00239                 }
00240 
00241                 // add optional zone information
00242                 os << ' ' << zoneI << endl;
00243             }
00244         }
00245         os << "# </zone>" << endl;
00246     }
00247     os << "# </faces>" << endl;
00248 }
00249 
00250 
00251 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd