initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / surfMesh / surfaceFormats / ofs / OFSsurfaceFormat.C
blob1df692c528e72b5e5f191d7859896b1a5e25805b
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "OFSsurfaceFormat.H"
28 #include "IFstream.H"
29 #include "IStringStream.H"
30 #include "ListOps.H"
32 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
34 template<class Face>
35 Foam::fileFormats::OFSsurfaceFormat<Face>::OFSsurfaceFormat
37     const fileName& filename
40     read(filename);
44 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
46 template<class Face>
47 bool Foam::fileFormats::OFSsurfaceFormat<Face>::read
49     const fileName& filename
52     this->clear();
54     IFstream is(filename);
55     if (!is.good())
56     {
57         FatalErrorIn
58         (
59             "fileFormats::OFSsurfaceFormat::read(const fileName&)"
60         )
61             << "Cannot read file " << filename
62             << exit(FatalError);
63     }
65     // read surfZones:
66     is >> this->storedZones();
68     // read points:
69     is >> this->storedPoints();
71     // must triangulate?
72     if (MeshedSurface<Face>::isTri())
73     {
74         // read faces as 'face' and transcribe to 'triFace'
75         List<face> faceLst(is);
77         MeshedSurface<face> surf
78         (
79             xferMove(this->storedPoints()),
80             xferMove(faceLst),
81             xferMove(this->storedZones())
82         );
84         this->transcribe(surf);
85     }
86     else
87     {
88         // read faces directly
89         is >> this->storedFaces();
90     }
92     return true;
96 template<class Face>
97 bool Foam::fileFormats::OFSsurfaceFormat<Face>::read
99     Istream& is,
100     pointField& pointLst,
101     List<Face>& faceLst,
102     List<surfZone>& zoneLst
105     if (!is.good())
106     {
107         FatalErrorIn
108         (
109             "fileFormats::OFSsurfaceFormat::read"
110             "(Istream&, pointField&, List<Face>&, List<surfZone>&)"
111         )
112             << "read error "
113             << exit(FatalError);
114     }
116     // read surfZones:
117     is >> zoneLst;
119     // read points:
120     is >> pointLst;
122     // must triangulate?
123     if (MeshedSurface<Face>::isTri())
124     {
125         // read faces as 'face' and transcribe to 'triFace'
126         List<face> origFaces(is);
128         MeshedSurface<face> origSurf
129         (
130             xferMove(pointLst),
131             xferMove(origFaces),
132             xferMove(zoneLst)
133         );
135         MeshedSurface<Face> surf;
136         surf.transcribe(origSurf);
137     }
138     else
139     {
140         // read faces directly
141         is >> faceLst;
142     }
144     return true;
148 template<class Face>
149 bool Foam::fileFormats::OFSsurfaceFormat<Face>::read
151     Istream& is,
152     MeshedSurface<Face>& surf
155     surf.clear();
157     if (!is.good())
158     {
159         FatalErrorIn
160         (
161             "fileFormats::OFSsurfaceFormat::read"
162             "(Istream&, MeshedSurface<Face>&)"
163         )
164             << "read error "
165             << exit(FatalError);
166     }
168     pointField pointLst;
169     List<Face> faceLst;
170     List<surfZone> zoneLst;
172     read(is, pointLst, faceLst, zoneLst);
174     surf.reset
175     (
176         xferMove(pointLst),
177         xferMove(faceLst),
178         xferMove(zoneLst)
179     );
181     return true;
185 template<class Face>
186 bool Foam::fileFormats::OFSsurfaceFormat<Face>::read
188     Istream& is,
189     UnsortedMeshedSurface<Face>& surf
192     surf.clear();
193     MeshedSurface<Face> origSurf(is);
194     surf.transfer(origSurf);
196     return true;
201 template<class Face>
202 void Foam::fileFormats::OFSsurfaceFormat<Face>::write
204     const fileName& filename,
205     const MeshedSurfaceProxy<Face>& surf
208     const List<Face>&  faceLst = surf.faces();
209     const List<label>& faceMap = surf.faceMap();
211     OFstream os(filename);
212     if (!os.good())
213     {
214         FatalErrorIn
215         (
216             "fileFormats::OFSsurfaceFormat::write"
217             "(const fileName&, const MeshedSurfaceProxy<Face>&)"
218         )
219             << "Cannot open file for writing " << filename
220             << exit(FatalError);
221     }
224     OFSsurfaceFormatCore::writeHeader(os, surf.points(), surf.surfZones());
226     const List<surfZone>& zones = surf.surfZones();
227     const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
229     if (useFaceMap)
230     {
231         os  << "\n// faces:"  << nl
232             << faceLst.size() << token::BEGIN_LIST << nl;
234         label faceI = 0;
235         forAll(zones, zoneI)
236         {
237             // Print all faces belonging to this zone
238             const surfZone& zone = zones[zoneI];
240             forAll(zone, localFaceI)
241             {
242                 os << faceLst[faceMap[faceI++]] << nl;
243             }
244         }
245         os << token::END_LIST << nl;
246     }
247     else
248     {
249         os  << "\n// faces:"  << nl << faceLst << nl;
250     }
252     IOobject::writeDivider(os);
254     // Check state of Ostream
255     os.check("OFSsurfaceFormat<Face>::write(Ostream&)");
259 // ************************************************************************* //