initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / surfMesh / surfaceFormats / tri / TRIsurfaceFormat.C
blobf1db14b73dee324fe8f18e672752f1e1a61e9f8b
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 "TRIsurfaceFormat.H"
28 #include "ListOps.H"
30 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
32 template<class Face>
33 inline void Foam::fileFormats::TRIsurfaceFormat<Face>::writeShell
35     Ostream& os,
36     const pointField& pointLst,
37     const Face& f,
38     const label zoneI
41     // simple triangulation about f[0].
42     // better triangulation should have been done before
43     const point& p0 = pointLst[f[0]];
44     for (label fp1 = 1; fp1 < f.size() - 1; ++fp1)
45     {
46         label fp2 = f.fcIndex(fp1);
48         const point& p1 = pointLst[f[fp1]];
49         const point& p2 = pointLst[f[fp2]];
51         os  << p0.x() << ' ' << p0.y() << ' ' << p0.z() << ' '
52             << p1.x() << ' ' << p1.y() << ' ' << p1.z() << ' '
53             << p2.x() << ' ' << p2.y() << ' ' << p2.z() << ' '
54             // zone as colour
55             << "0x" << hex << zoneI << dec << endl;
56     }
60 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
62 template<class Face>
63 Foam::fileFormats::TRIsurfaceFormat<Face>::TRIsurfaceFormat
65     const fileName& filename
68     read(filename);
72 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
74 template<class Face>
75 bool Foam::fileFormats::TRIsurfaceFormat<Face>::read
77     const fileName& filename
80     this->clear();
82     // read in the values
83     TRIsurfaceFormatCore reader(filename);
85     // transfer points
86     this->storedPoints().transfer(reader.points());
88     // retrieve the original zone information
89     List<label> sizes(reader.sizes().xfer());
90     List<label> zoneIds(reader.zoneIds().xfer());
92     // generate the (sorted) faces
93     List<Face> faceLst(zoneIds.size());
95     if (reader.sorted())
96     {
97         // already sorted - generate directly
98         forAll(faceLst, faceI)
99         {
100             const label startPt = 3*faceI;
101             faceLst[faceI] = triFace(startPt, startPt+1, startPt+2);
102         }
103     }
104     else
105     {
106         // unsorted - determine the sorted order:
107         // avoid SortableList since we discard the main list anyhow
108         List<label> faceMap;
109         sortedOrder(zoneIds, faceMap);
111         // generate sorted faces
112         forAll(faceMap, faceI)
113         {
114             const label startPt = 3*faceMap[faceI];
115             faceLst[faceI] = triFace(startPt, startPt+1, startPt+2);
116         }
117     }
118     zoneIds.clear();
120     // transfer:
121     this->storedFaces().transfer(faceLst);
123     this->addZones(sizes);
124     this->stitchFaces(SMALL);
125     return true;
129 template<class Face>
130 void Foam::fileFormats::TRIsurfaceFormat<Face>::write
132     const fileName& filename,
133     const MeshedSurfaceProxy<Face>& surf
136     const pointField& pointLst = surf.points();
137     const List<Face>&  faceLst = surf.faces();
138     const List<label>& faceMap = surf.faceMap();
140     const List<surfZone>& zones =
141     (
142         surf.surfZones().size() > 1
143       ? surf.surfZones()
144       : oneZone(faceLst)
145     );
147     const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
149     OFstream os(filename);
150     if (!os.good())
151     {
152         FatalErrorIn
153         (
154             "fileFormats::TRIsurfaceFormat::write"
155             "(const fileName&, const MeshedSurfaceProxy<Face>&)"
156         )
157             << "Cannot open file for writing " << filename
158             << exit(FatalError);
159     }
161     label faceIndex = 0;
162     forAll(zones, zoneI)
163     {
164         const surfZone& zone = zones[zoneI];
166         if (useFaceMap)
167         {
168             forAll(zone, localFaceI)
169             {
170                 const Face& f = faceLst[faceMap[faceIndex++]];
171                 writeShell(os, pointLst, f, zoneI);
172             }
173         }
174         else
175         {
176             forAll(zone, localFaceI)
177             {
178                 const Face& f = faceLst[faceIndex++];
179                 writeShell(os, pointLst, f, zoneI);
180             }
181         }
182     }
186 template<class Face>
187 void Foam::fileFormats::TRIsurfaceFormat<Face>::write
189     const fileName& filename,
190     const UnsortedMeshedSurface<Face>& surf
193     const pointField& pointLst = surf.points();
194     const List<Face>& faceLst  = surf.faces();
196     OFstream os(filename);
197     if (!os.good())
198     {
199         FatalErrorIn
200         (
201             "fileFormats::TRIsurfaceFormat::write"
202             "(const fileName&, const UnsortedMeshedSurface<Face>&)"
203         )
204             << "Cannot open file for writing " << filename
205             << exit(FatalError);
206     }
209     // a single zone needs no sorting
210     if (surf.zoneToc().size() == 1)
211     {
212         const List<label>& zoneIds  = surf.zoneIds();
214         forAll(faceLst, faceI)
215         {
216             writeShell(os, pointLst, faceLst[faceI], zoneIds[faceI]);
217         }
218     }
219     else
220     {
221         labelList faceMap;
222         List<surfZone> zoneLst = surf.sortedZones(faceMap);
224         label faceIndex = 0;
225         forAll(zoneLst, zoneI)
226         {
227             forAll(zoneLst[zoneI], localFaceI)
228             {
229                 const Face& f = faceLst[faceMap[faceIndex++]];
230                 writeShell(os, pointLst, f, zoneI);
231             }
232         }
233     }
237 // ************************************************************************* //