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) 2011 OpenFOAM Foundation
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 "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
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().empty()
142  ? surfaceFormatsCore::oneZone(faceLst)
143  : surf.surfZones()
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  {
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 // ************************************************************************* //
Output to file stream.
Definition: OFstream.H:81
const List< surfZoneIdentifier > & zoneToc() const
Return const access to the zone table-of-contents.
static void write(const fileName &, const MeshedSurfaceProxy< Face > &)
Write surface mesh components by proxy.
vector point
Point is a vector.
Definition: point.H:41
List< label > & zoneIds()
Return full access to the zones.
bool sorted() const
File read was already sorted.
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
bool useFaceMap() const
Use faceMap?
Xfer< List< T > > xfer()
Transfer contents to the Xfer container.
Definition: ListI.H:90
void sortedOrder(const UList< T > &, labelList &order)
Generate the (stable) sort order for the list.
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats...
Definition: MeshedSurface.H:73
List< label > & sizes()
The list of zone sizes in the order of their first appearance.
const List< label > & zoneIds() const
Return const access to the zone ids.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Various functions to operate on Lists.
face triFace(3)
const pointField & points() const
Return const access to the points.
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
const List< Face > & faces() const
Return const access to the faces.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
const List< Face > & faces() const
Return const access to the faces.
Base class for zones.
Definition: zone.H:57
const List< surfZone > & surfZones() const
Const access to the surface zones.
Internal class used by the TRIsurfaceFormat.
#define forAll(list, i)
Definition: UList.H:421
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
const Cmpt & x() const
Definition: VectorI.H:65
virtual bool read(const fileName &)
Read from file.
IOstream & dec(IOstream &io)
Definition: IOstream.H:558
A surface zone on a MeshedSurface.
Definition: surfZone.H:62
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
IOstream & hex(IOstream &io)
Definition: IOstream.H:564
A surface geometry mesh, in which the surface zone information is conveyed by the &#39;zoneId&#39; associated...
Definition: MeshedSurface.H:74
error FatalError
A class for handling file names.
Definition: fileName.H:69
const List< label > & faceMap() const
Const access to the faceMap, zero-sized when unused.
bool read(const char *, int32_t &)
Definition: int32IO.C:87
Provide a means of reading/writing .tri format.
pointField & points()
Return full access to the points.
UEqn clear()
surfZoneList sortedZones(labelList &faceMap) const
Sort faces according to zoneIds.
const Field< point > & points() const
Return reference to global points.