TRIsurfaceFormat.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 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  << "Cannot open file for writing " << filename
153  << exit(FatalError);
154  }
155 
156  label faceIndex = 0;
157  forAll(zones, zoneI)
158  {
159  const surfZone& zone = zones[zoneI];
160 
161  if (useFaceMap)
162  {
163  forAll(zone, localFacei)
164  {
165  const Face& f = faceLst[faceMap[faceIndex++]];
166  writeShell(os, pointLst, f, zoneI);
167  }
168  }
169  else
170  {
171  forAll(zone, localFacei)
172  {
173  const Face& f = faceLst[faceIndex++];
174  writeShell(os, pointLst, f, zoneI);
175  }
176  }
177  }
178 }
179 
180 
181 template<class Face>
183 (
184  const fileName& filename,
185  const UnsortedMeshedSurface<Face>& surf
186 )
187 {
188  const pointField& pointLst = surf.points();
189  const List<Face>& faceLst = surf.faces();
190 
191  OFstream os(filename);
192  if (!os.good())
193  {
195  << "Cannot open file for writing " << filename
196  << exit(FatalError);
197  }
198 
199 
200  // a single zone needs no sorting
201  if (surf.zoneToc().size() == 1)
202  {
203  const List<label>& zoneIds = surf.zoneIds();
204 
205  forAll(faceLst, facei)
206  {
207  writeShell(os, pointLst, faceLst[facei], zoneIds[facei]);
208  }
209  }
210  else
211  {
213  List<surfZone> zoneLst = surf.sortedZones(faceMap);
214 
215  label faceIndex = 0;
216  forAll(zoneLst, zoneI)
217  {
218  forAll(zoneLst[zoneI], localFacei)
219  {
220  const Face& f = faceLst[faceMap[faceIndex++]];
221  writeShell(os, pointLst, f, zoneI);
222  }
223  }
224  }
225 }
226 
227 
228 // ************************************************************************* //
A surface geometry mesh, in which the surface zone information is conveyed by the &#39;zoneId&#39; associated...
Definition: MeshedSurface.H:74
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
List< label > & zoneIds()
Return full access to the zones.
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 sortedOrder(const UList< T > &, labelList &order)
Generate the (stable) sort order for the list.
A class for handling file names.
Definition: fileName.H:69
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
IOstream & hex(IOstream &io)
Definition: IOstream.H:564
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
Xfer< List< T > > xfer()
Transfer contents to the Xfer container.
Definition: ListI.H:177
tUEqn clear()
Output to file stream.
Definition: OFstream.H:82
A surface zone on a MeshedSurface.
Definition: surfZone.H:62
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
const List< label > & zoneIds() const
Return const access to the zone ids.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
pointField & points()
Return full access to the points.
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
Various functions to operate on Lists.
bool useFaceMap() const
Use faceMap?
List< label > & sizes()
The list of zone sizes in the order of their first appearance.
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
Base class for zones.
Definition: zone.H:57
const List< Face > & faces() const
Return const access to the faces.
face triFace(3)
Provide a means of reading/writing .tri format.
const Field< point > & points() const
Return reference to global points.
const List< surfZone > & surfZones() const
Const access to the surface zones.
const Cmpt & x() const
Definition: VectorI.H:75
Internal class used by the TRIsurfaceFormat.
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats...
Definition: MeshedSurface.H:73
virtual bool read(const fileName &)
Read from file.
const List< Face > & faces() const
Return const access to the faces.
const List< surfZoneIdentifier > & zoneToc() const
Return const access to the zone table-of-contents.
surfZoneList sortedZones(labelList &faceMap) const
Sort faces according to zoneIds.
vector point
Point is a vector.
Definition: point.H:41
const pointField & points() const
Return const access to the points.
const List< label > & faceMap() const
Const access to the faceMap, zero-sized when unused.
IOstream & dec(IOstream &io)
Definition: IOstream.H:558
static void write(const fileName &, const MeshedSurfaceProxy< Face > &)
Write surface mesh components by proxy.
bool sorted() const
File read was already sorted.