FreeFOAM The Cross-Platform CFD Toolkit
TRIsurfaceFormat.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "TRIsurfaceFormat.H"
27 #include <OpenFOAM/ListOps.H>
28 
29 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
30 
31 template<class Face>
33 (
34  Ostream& os,
35  const pointField& pointLst,
36  const Face& f,
37  const label zoneI
38 )
39 {
40  // simple triangulation about f[0].
41  // better triangulation should have been done before
42  const point& p0 = pointLst[f[0]];
43  for (label fp1 = 1; fp1 < f.size() - 1; ++fp1)
44  {
45  label fp2 = f.fcIndex(fp1);
46 
47  const point& p1 = pointLst[f[fp1]];
48  const point& p2 = pointLst[f[fp2]];
49 
50  os << p0.x() << ' ' << p0.y() << ' ' << p0.z() << ' '
51  << p1.x() << ' ' << p1.y() << ' ' << p1.z() << ' '
52  << p2.x() << ' ' << p2.y() << ' ' << p2.z() << ' '
53  // zone as colour
54  << "0x" << hex << zoneI << dec << endl;
55  }
56 }
57 
58 
59 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
60 
61 template<class Face>
63 (
64  const fileName& filename
65 )
66 {
67  read(filename);
68 }
69 
70 
71 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
72 
73 template<class Face>
75 (
76  const fileName& filename
77 )
78 {
79  this->clear();
80 
81  // read in the values
82  TRIsurfaceFormatCore reader(filename);
83 
84  // transfer points
85  this->storedPoints().transfer(reader.points());
86 
87  // retrieve the original zone information
88  List<label> sizes(reader.sizes().xfer());
89  List<label> zoneIds(reader.zoneIds().xfer());
90 
91  // generate the (sorted) faces
92  List<Face> faceLst(zoneIds.size());
93 
94  if (reader.sorted())
95  {
96  // already sorted - generate directly
97  forAll(faceLst, faceI)
98  {
99  const label startPt = 3*faceI;
100  faceLst[faceI] = triFace(startPt, startPt+1, startPt+2);
101  }
102  }
103  else
104  {
105  // unsorted - determine the sorted order:
106  // avoid SortableList since we discard the main list anyhow
107  List<label> faceMap;
108  sortedOrder(zoneIds, faceMap);
109 
110  // generate sorted faces
111  forAll(faceMap, faceI)
112  {
113  const label startPt = 3*faceMap[faceI];
114  faceLst[faceI] = triFace(startPt, startPt+1, startPt+2);
115  }
116  }
117  zoneIds.clear();
118 
119  // transfer:
120  this->storedFaces().transfer(faceLst);
121 
122  this->addZones(sizes);
123  this->stitchFaces(SMALL);
124  return true;
125 }
126 
127 
128 template<class Face>
130 (
131  const fileName& filename,
132  const MeshedSurfaceProxy<Face>& surf
133 )
134 {
135  const pointField& pointLst = surf.points();
136  const List<Face>& faceLst = surf.faces();
137  const List<label>& faceMap = surf.faceMap();
138 
139  const List<surfZone>& zones =
140  (
141  surf.surfZones().size() > 1
142  ? surf.surfZones()
143  : oneZone(faceLst)
144  );
145 
146  const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
147 
148  OFstream os(filename);
149  if (!os.good())
150  {
152  (
153  "fileFormats::TRIsurfaceFormat::write"
154  "(const fileName&, const MeshedSurfaceProxy<Face>&)"
155  )
156  << "Cannot open file for writing " << filename
157  << exit(FatalError);
158  }
159 
160  label faceIndex = 0;
161  forAll(zones, zoneI)
162  {
163  const surfZone& zone = zones[zoneI];
164 
165  if (useFaceMap)
166  {
167  forAll(zone, localFaceI)
168  {
169  const Face& f = faceLst[faceMap[faceIndex++]];
170  writeShell(os, pointLst, f, zoneI);
171  }
172  }
173  else
174  {
175  forAll(zone, localFaceI)
176  {
177  const Face& f = faceLst[faceIndex++];
178  writeShell(os, pointLst, f, zoneI);
179  }
180  }
181  }
182 }
183 
184 
185 template<class Face>
187 (
188  const fileName& filename,
189  const UnsortedMeshedSurface<Face>& surf
190 )
191 {
192  const pointField& pointLst = surf.points();
193  const List<Face>& faceLst = surf.faces();
194 
195  OFstream os(filename);
196  if (!os.good())
197  {
199  (
200  "fileFormats::TRIsurfaceFormat::write"
201  "(const fileName&, const UnsortedMeshedSurface<Face>&)"
202  )
203  << "Cannot open file for writing " << filename
204  << exit(FatalError);
205  }
206 
207 
208  // a single zone needs no sorting
209  if (surf.zoneToc().size() == 1)
210  {
211  const List<label>& zoneIds = surf.zoneIds();
212 
213  forAll(faceLst, faceI)
214  {
215  writeShell(os, pointLst, faceLst[faceI], zoneIds[faceI]);
216  }
217  }
218  else
219  {
220  labelList faceMap;
221  List<surfZone> zoneLst = surf.sortedZones(faceMap);
222 
223  label faceIndex = 0;
224  forAll(zoneLst, zoneI)
225  {
226  forAll(zoneLst[zoneI], localFaceI)
227  {
228  const Face& f = faceLst[faceMap[faceIndex++]];
229  writeShell(os, pointLst, f, zoneI);
230  }
231  }
232  }
233 }
234 
235 
236 // ************************ vim: set sw=4 sts=4 et: ************************ //