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

GTSsurfaceFormat.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 "GTSsurfaceFormat.H"
00027 #include <surfMesh/surfaceFormatsCore.H>
00028 #include <OpenFOAM/clock.H>
00029 #include <OpenFOAM/IFstream.H>
00030 #include <OpenFOAM/IStringStream.H>
00031 #include <OpenFOAM/Ostream.H>
00032 #include <OpenFOAM/OFstream.H>
00033 
00034 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00035 
00036 template<class Face>
00037 Foam::fileFormats::GTSsurfaceFormat<Face>::GTSsurfaceFormat
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::GTSsurfaceFormat<Face>::read
00050 (
00051     const fileName& filename
00052 )
00053 {
00054     this->clear();
00055 
00056     IFstream is(filename);
00057     if (!is.good())
00058     {
00059         FatalErrorIn
00060         (
00061             "fileFormats::GTSsurfaceFormat::read(const fileName&)"
00062         )
00063             << "Cannot read file " << filename
00064             << exit(FatalError);
00065     }
00066 
00067     // Read header
00068     string line = this->getLineNoComment(is);
00069 
00070     label nPoints, nEdges, nElems;
00071     {
00072         IStringStream lineStream(line);
00073         lineStream
00074             >> nPoints
00075             >> nEdges
00076             >> nElems;
00077     }
00078 
00079 
00080     // write directly into the lists:
00081     pointField&  pointLst = this->storedPoints();
00082     List<Face>&  faceLst  = this->storedFaces();
00083     List<label>& zoneIds  = this->storedZoneIds();
00084 
00085     pointLst.setSize(nPoints);
00086     faceLst.setSize(nElems);
00087     zoneIds.setSize(nElems);
00088 
00089     // Read points
00090     forAll(pointLst, pointI)
00091     {
00092         scalar x, y, z;
00093         line = this->getLineNoComment(is);
00094         {
00095             IStringStream lineStream(line);
00096             lineStream
00097                 >> x >> y >> z;
00098         }
00099 
00100         pointLst[pointI] = point(x, y, z);
00101     }
00102 
00103     // Read edges (Foam indexing)
00104     edgeList edges(nEdges);
00105     forAll(edges, edgei)
00106     {
00107         label beg, end;
00108         line = this->getLineNoComment(is);
00109         {
00110             IStringStream lineStream(line);
00111             lineStream
00112                 >> beg >> end;
00113         }
00114         edges[edgei] = edge(beg - 1, end - 1);
00115     }
00116 
00117 
00118     // Read triangles. Convert references to edges into pointlabels
00119     label maxZone = 0;
00120     forAll(faceLst, faceI)
00121     {
00122         label e0Label, e1Label, e2Label;
00123         label zoneI = 0;
00124 
00125         line = this->getLineNoComment(is);
00126         {
00127             IStringStream lineStream(line);
00128             lineStream
00129                 >> e0Label >> e1Label >> e2Label;
00130 
00131             // Optional zone number: read first, then check state on stream
00132             if (lineStream)
00133             {
00134                 label num;
00135                 lineStream >> num;
00136                 if (!lineStream.bad())
00137                 {
00138                     zoneI = num;
00139                     if (maxZone < zoneI)
00140                     {
00141                         maxZone = zoneI;
00142                     }
00143                 }
00144             }
00145         }
00146 
00147         // Determine ordering of edges e0, e1
00148         //  common: common vertex, shared by e0 and e1
00149         //  e0Far:  vertex on e0 which is not common
00150         //  e1Far:  vertex on e1 which is not common
00151         const edge& e0 = edges[e0Label - 1];
00152         const edge& e1 = edges[e1Label - 1];
00153         const edge& e2 = edges[e2Label - 1];
00154 
00155         label common01 = e0.commonVertex(e1);
00156         if (common01 == -1)
00157         {
00158             FatalErrorIn
00159             (
00160                 "fileFormats::GTSsurfaceFormat::read(const fileName&)"
00161             )
00162                 << "Edges 0 and 1 of triangle " << faceI
00163                 << " do not share a point.\n"
00164                 << "    edge0:" << e0 << nl
00165                 << "    edge1:" << e1
00166                 << exit(FatalError);
00167         }
00168 
00169         label e0Far = e0.otherVertex(common01);
00170         label e1Far = e1.otherVertex(common01);
00171 
00172         label common12 = e1.commonVertex(e2);
00173         if (common12 == -1)
00174         {
00175             FatalErrorIn
00176             (
00177                 "fileFormats::GTSsurfaceFormat::read(const fileName&)"
00178             )
00179                 << "Edges 1 and 2 of triangle " << faceI
00180                 << " do not share a point.\n"
00181                 << "    edge1:" << e1 << nl
00182                 << "    edge2:" << e2
00183                 << exit(FatalError);
00184         }
00185         label e2Far = e2.otherVertex(common12);
00186 
00187         // Does edge2 sit between edge1 and 0?
00188         if (common12 != e1Far || e2Far != e0Far)
00189         {
00190             FatalErrorIn
00191             (
00192                 "fileFormats::GTSsurfaceFormat::read(const fileName&)"
00193             )
00194                 << "Edges of triangle " << faceI
00195                 << " reference more than three points.\n"
00196                 << "    edge0:" << e0 << nl
00197                 << "    edge1:" << e1 << nl
00198                 << "    edge2:" << e2 << nl
00199                 << exit(FatalError);
00200         }
00201 
00202         faceLst[faceI] = triFace(e0Far, common01, e1Far);
00203         zoneIds[faceI] = zoneI;
00204     }
00205 
00206 
00207     List<surfZoneIdentifier> newZones(maxZone+1);
00208     forAll(newZones, zoneI)
00209     {
00210         newZones[zoneI] = surfZoneIdentifier
00211         (
00212             "zone" + ::Foam::name(zoneI),
00213             zoneI
00214         );
00215     }
00216 
00217     this->storedZoneToc().transfer(newZones);
00218 
00219     return true;
00220 }
00221 
00222 
00223 template<class Face>
00224 void Foam::fileFormats::GTSsurfaceFormat<Face>::write
00225 (
00226     const fileName& filename,
00227     const MeshedSurface<Face>& surf
00228 )
00229 {
00230     const pointField& pointLst = surf.points();
00231     const List<Face>& faceLst  = surf.faces();
00232 
00233     const List<surfZone>& zones =
00234     (
00235         surf.surfZones().size()
00236       ? surf.surfZones()
00237       : surfaceFormatsCore::oneZone(faceLst)
00238     );
00239 
00240     // check if output triangulation would be required
00241     // It is too annoying to triangulate on-the-fly
00242     // just issue a warning and get out
00243     if (!MeshedSurface<Face>::isTri())
00244     {
00245         label nNonTris = 0;
00246         forAll(faceLst, faceI)
00247         {
00248             if (faceLst[faceI].size() != 3)
00249             {
00250                 ++nNonTris;
00251             }
00252         }
00253 
00254         if (nNonTris)
00255         {
00256             FatalErrorIn
00257             (
00258                 "fileFormats::GTSsurfaceFormat::write"
00259                 "(const fileName&, const MeshedSurface<Face>&)"
00260             )
00261                 << "Surface has " << nNonTris << "/" << faceLst.size()
00262                 << " non-triangulated faces - not writing!" << endl;
00263             return;
00264         }
00265     }
00266 
00267 
00268     OFstream os(filename);
00269     if (!os.good())
00270     {
00271         FatalErrorIn
00272         (
00273             "fileFormats::GTSsurfaceFormat::write"
00274             "(const fileName&, const MeshedSurface<Face>&)"
00275         )
00276             << "Cannot open file for writing " << filename
00277             << exit(FatalError);
00278     }
00279 
00280 
00281     // Write header, print zone names as comment
00282     os  << "# GTS file" << nl
00283         << "# Zones:" << nl;
00284 
00285     forAll(zones, zoneI)
00286     {
00287         os  << "#     " << zoneI << "    "
00288             << zones[zoneI].name() << nl;
00289     }
00290     os  << "#" << nl;
00291 
00292     os  << "# nPoints  nEdges  nTriangles" << nl
00293         << pointLst.size() << ' ' << surf.nEdges() << ' '
00294         << surf.size() << endl;
00295 
00296 
00297     // Write vertex coords
00298     forAll(pointLst, pointI)
00299     {
00300         const point& pt = pointLst[pointI];
00301 
00302         os  << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
00303     }
00304 
00305 
00306     // Write edges.
00307     // Note: edges are in local point labels so convert
00308     const edgeList& es = surf.edges();
00309     const labelList& meshPts = surf.meshPoints();
00310 
00311     forAll(es, edgei)
00312     {
00313         os  << meshPts[es[edgei].start()] + 1 << ' '
00314             << meshPts[es[edgei].end()] + 1 << endl;
00315     }
00316 
00317     // Write faces in terms of edges.
00318     const labelListList& faceEs = surf.faceEdges();
00319 
00320     label faceIndex = 0;
00321     forAll(zones, zoneI)
00322     {
00323         const surfZone& zone = zones[zoneI];
00324 
00325         forAll(zone, localFaceI)
00326         {
00327             const labelList& fEdges = faceEs[faceIndex++];
00328 
00329             os  << fEdges[0] + 1 << ' '
00330                 << fEdges[1] + 1 << ' '
00331                 << fEdges[2] + 1 << ' '
00332                 << zoneI << endl;
00333         }
00334     }
00335 }
00336 
00337 
00338 template<class Face>
00339 void Foam::fileFormats::GTSsurfaceFormat<Face>::write
00340 (
00341     const fileName& filename,
00342     const UnsortedMeshedSurface<Face>& surf
00343 )
00344 {
00345     const pointField& pointLst   = surf.points();
00346     const List<Face>& faceLst    = surf.faces();
00347     const List<label>& zoneIds   = surf.zoneIds();
00348     const List<surfZoneIdentifier>& zoneToc = surf.zoneToc();
00349 
00350     // check if output triangulation would be required
00351     // It is too annoying to triangulate on-the-fly
00352     // just issue a warning and get out
00353     if (!MeshedSurface<Face>::isTri())
00354     {
00355         label nNonTris = 0;
00356         forAll(faceLst, faceI)
00357         {
00358             if (faceLst[faceI].size() != 3)
00359             {
00360                 ++nNonTris;
00361             }
00362         }
00363 
00364         if (nNonTris)
00365         {
00366             FatalErrorIn
00367             (
00368                 "fileFormats::GTSsurfaceFormat::write"
00369                 "(const fileName&, const UnsortedMeshedSurfaces<Face>&)"
00370             )
00371                 << "Surface has " << nNonTris << "/" << faceLst.size()
00372                 << " non-triangulated faces - not writing!" << endl;
00373             return;
00374         }
00375     }
00376 
00377 
00378     OFstream os(filename);
00379     if (!os.good())
00380     {
00381         FatalErrorIn
00382         (
00383             "fileFormats::GTSsurfaceFormat::write"
00384             "(const fileName&, const UnsortedMeshedSurface<Face>&)"
00385         )
00386             << "Cannot open file for writing " << filename
00387             << exit(FatalError);
00388     }
00389 
00390 
00391     // Write header, print zone names as comment
00392     os  << "# GTS file" << nl
00393         << "# Zones:" << nl;
00394 
00395     forAll(zoneToc, zoneI)
00396     {
00397         os  << "#     " << zoneI << "    "
00398             << zoneToc[zoneI].name() << nl;
00399     }
00400     os  << "#" << endl;
00401 
00402 
00403     os  << "# nPoints  nEdges  nTriangles" << nl
00404         << pointLst.size() << ' ' << surf.nEdges() << ' '
00405         << surf.size() << endl;
00406 
00407 
00408     // Write vertex coords
00409     forAll(pointLst, pointI)
00410     {
00411         os  << pointLst[pointI].x() << ' '
00412             << pointLst[pointI].y() << ' '
00413             << pointLst[pointI].z() << endl;
00414     }
00415 
00416 
00417     // Write edges.
00418     // Note: edges are in local point labels so convert
00419     const edgeList& es = surf.edges();
00420     const labelList& meshPts = surf.meshPoints();
00421 
00422     forAll(es, edgeI)
00423     {
00424         os  << meshPts[es[edgeI].start()] + 1 << ' '
00425             << meshPts[es[edgeI].end()] + 1 << endl;
00426     }
00427 
00428 
00429     // Write faces in terms of edges.
00430     const labelListList& faceEs = surf.faceEdges();
00431 
00432     forAll(faceLst, faceI)
00433     {
00434         const labelList& fEdges = faceEs[faceI];
00435 
00436         os  << fEdges[0] + 1 << ' '
00437             << fEdges[1] + 1 << ' '
00438             << fEdges[2] + 1 << ' '
00439             << zoneIds[faceI] << endl;
00440     }
00441 }
00442 
00443 
00444 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines