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

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