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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2016-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "TRIsurfaceFormat.H"
30 #include "TRIReader.H"
31 #include "OFstream.H"
32 #include "ListOps.H"
33 
34 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
35 
36 template<class Face>
38 (
39  Ostream& os,
40  const UList<point>& pts,
41  const Face& f,
42  const label zoneI
43 )
44 {
45  // simple triangulation about f[0].
46  // better triangulation should have been done before
47  const point& p0 = pts[f[0]];
48  for (label fp1 = 1; fp1 < f.size() - 1; ++fp1)
49  {
50  const label fp2 = f.fcIndex(fp1);
51 
52  const point& p1 = pts[f[fp1]];
53  const point& p2 = pts[f[fp2]];
54 
55  os << p0.x() << ' ' << p0.y() << ' ' << p0.z() << ' '
56  << p1.x() << ' ' << p1.y() << ' ' << p1.z() << ' '
57  << p2.x() << ' ' << p2.y() << ' ' << p2.z() << ' '
58  // zone as colour
59  << "0x" << hex << zoneI << dec << nl;
60  }
61 }
62 
63 
64 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
65 
66 template<class Face>
68 (
69  const fileName& filename
70 )
71 {
72  read(filename);
73 }
74 
75 
76 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
77 
78 template<class Face>
80 (
81  const fileName& filename
82 )
83 {
84  this->clear();
85 
86  // Read in the values
87  TRIReader reader(filename);
88 
89  // Get the map for stitched surface points
90  labelList pointMap;
91  const label nUniquePoints = reader.mergePointsMap(pointMap);
92 
93  const auto& readpts = reader.points();
94 
95  // Assign points
96  pointField& pointLst = this->storedPoints();
97  pointLst.setSize(nUniquePoints);
98  forAll(readpts, pointi)
99  {
100  pointLst[pointMap[pointi]] = readpts[pointi];
101  }
102 
103  // Retrieve the original zone information
104  List<label> sizes(std::move(reader.sizes()));
105  List<label> zoneIds(std::move(reader.zoneIds()));
106 
107  // Generate the (sorted) faces
108  List<Face> faceLst(zoneIds.size());
109 
110  if (reader.sorted())
111  {
112  // Already sorted - generate directly
113  forAll(faceLst, facei)
114  {
115  const label startPt = 3*facei;
116  faceLst[facei] = Face
117  {
118  pointMap[startPt],
119  pointMap[startPt+1],
120  pointMap[startPt+2]
121  };
122  }
123  }
124  else
125  {
126  // Determine the sorted order:
127  // use sortedOrder directly (the intermediate list is discared anyhow)
128  labelList faceMap(sortedOrder(zoneIds));
129 
130  // Generate sorted faces
131  forAll(faceMap, facei)
132  {
133  const label startPt = 3*faceMap[facei];
134  faceLst[facei] = Face
135  {
136  pointMap[startPt],
137  pointMap[startPt+1],
138  pointMap[startPt+2]
139  };
140  }
141  }
142  zoneIds.clear();
143 
144  // Transfer:
145  this->storedFaces().transfer(faceLst);
146 
147  this->addZones(sizes);
148  this->addZonesToFaces(); // for labelledTri
149 
150  return true;
151 }
152 
153 
154 template<class Face>
156 (
157  const fileName& filename,
158  const MeshedSurfaceProxy<Face>& surf,
159  const dictionary& options
160 )
161 {
162  const UList<point>& pointLst = surf.points();
163  const UList<Face>& faceLst = surf.surfFaces();
164  const UList<label>& faceMap = surf.faceMap();
165 
166  const surfZoneList zones =
167  (
168  surf.surfZones().empty()
169  ? surfaceFormatsCore::oneZone(faceLst)
170  : surf.surfZones()
171  );
172 
173  const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
174 
175  OFstream os(filename);
176  if (!os.good())
177  {
179  << "Cannot open file for writing " << filename
180  << exit(FatalError);
181  }
182 
183  label faceIndex = 0;
184  label zoneIndex = 0;
185  for (const surfZone& zone : zones)
186  {
187  const label nLocalFaces = zone.size();
188 
189  if (useFaceMap)
190  {
191  for (label i=0; i<nLocalFaces; ++i)
192  {
193  const Face& f = faceLst[faceMap[faceIndex++]];
194  writeShell(os, pointLst, f, zoneIndex);
195  }
196  }
197  else
198  {
199  for (label i=0; i<nLocalFaces; ++i)
200  {
201  const Face& f = faceLst[faceIndex++];
202  writeShell(os, pointLst, f, zoneIndex);
203  }
204  }
205 
206  ++zoneIndex;
207  }
208 }
209 
210 
211 template<class Face>
213 (
214  const fileName& filename,
215  const UnsortedMeshedSurface<Face>& surf,
216  const dictionary& options
217 )
218 {
219  const UList<point>& pointLst = surf.points();
220  const UList<Face>& faceLst = surf.surfFaces();
221 
222  OFstream os(filename);
223  if (!os.good())
224  {
226  << "Cannot open file for writing " << filename
227  << exit(FatalError);
228  }
229 
230  // A single zone needs no sorting
231  if (surf.zoneToc().size() == 1)
232  {
233  const UList<label>& zoneIds = surf.zoneIds();
234 
235  forAll(faceLst, facei)
236  {
237  writeShell(os, pointLst, faceLst[facei], zoneIds[facei]);
238  }
239  }
240  else
241  {
243  List<surfZone> zoneLst = surf.sortedZones(faceMap);
244 
245  label faceIndex = 0;
246  label zoneIndex = 0;
247  for (const surfZone& zone : zoneLst)
248  {
249  const label nLocalFaces = zone.size();
250 
251  for (label i=0; i<nLocalFaces; ++i)
252  {
253  const Face& f = faceLst[faceMap[faceIndex++]];
254  writeShell(os, pointLst, f, zoneIndex);
255  }
256 
257  ++zoneIndex;
258  }
259  }
260 }
261 
262 
263 // ************************************************************************* //
hex
const cellModel & hex
Definition: createBlockMesh.H:1
Foam::faceMap
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
Definition: blockMeshMergeFast.C:94
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::MeshedSurfaceProxy::useFaceMap
bool useFaceMap() const
Use faceMap?
Definition: MeshedSurfaceProxy.H:186
TRIReader.H
Foam::zone
Base class for mesh zones.
Definition: zone.H:63
Foam::MeshedSurfaceProxy::points
const pointField & points() const
Return const access to the points.
Definition: MeshedSurfaceProxy.H:160
Foam::fileFormats::TRIsurfaceFormat::TRIsurfaceFormat
TRIsurfaceFormat(const fileName &filename)
Construct from file name.
Definition: TRIsurfaceFormat.C:68
Foam::fileFormats::TRIsurfaceFormat::read
virtual bool read(const fileName &filename)
Read from file.
Definition: TRIsurfaceFormat.C:80
Foam::fileFormats::TRIReader::sizes
List< label > & sizes()
The list of zone sizes in the order of their first appearance.
Definition: TRIReader.H:139
Foam::fileFormats::TRIReader
TRI (triangle) file reader.
Definition: TRIReader.H:61
Foam::MeshedSurfaceProxy
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats.
Definition: MeshedSurface.H:78
Foam::MeshedSurfaceProxy::faceMap
const labelUList & faceMap() const
Const access to the faceMap, zero-sized when unused.
Definition: MeshedSurfaceProxy.H:180
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
OFstream.H
Foam::UnsortedMeshedSurface::zoneIds
virtual const labelList & zoneIds() const
Return const access to the zone ids.
Definition: UnsortedMeshedSurface.H:300
TRIsurfaceFormat.H
Foam::label
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:62
Foam::Field< vector >
Foam::UnsortedMeshedSurface
A surface geometry mesh, in which the surface zone information is conveyed by the 'zoneId' associated...
Definition: MeshedSurface.H:79
Foam::dec
IOstream & dec(IOstream &io)
Definition: IOstream.H:423
Foam::fileFormats::TRIReader::zoneIds
List< label > & zoneIds()
Return full access to the zones.
Definition: TRIReader.H:127
Foam::fileFormats::TRIsurfaceFormat
Provide a means of reading/writing .tri format.
Definition: TRIsurfaceFormat.H:60
Foam::fileFormats::TRIReader::points
List< STLpoint > & points()
Return full access to the points.
Definition: TRIReader.H:121
Foam::UnsortedMeshedSurface::zoneToc
const List< surfZoneIdentifier > & zoneToc() const
Return const access to the zone table-of-contents.
Definition: UnsortedMeshedSurface.H:306
Foam::blockMeshTools::read
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:33
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:99
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
clear
patchWriters clear()
Foam::nl
constexpr char nl
Definition: Ostream.H:372
f
labelList f(nPoints)
Foam::List< label >
Foam::surfZone
A surface zone on a MeshedSurface.
Definition: surfZone.H:65
Foam::fileFormats::TRIReader::sorted
bool sorted() const
File read was already sorted.
Definition: TRIReader.H:115
Foam::fileFormats::TRIReader::mergePointsMap
label mergePointsMap(labelList &pointMap) const
Calculate merge points mapping, return old to new pointMap.
Definition: TRIReader.C:193
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
Foam::List::clear
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:115
ListOps.H
Various functions to operate on Lists.
Foam::fileFormats::TRIsurfaceFormat::write
static void write(const fileName &filename, const MeshedSurfaceProxy< Face > &surf, const dictionary &options=dictionary::null)
Write surface mesh components by proxy.
Definition: TRIsurfaceFormat.C:156
Foam::sortedOrder
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
Foam::MeshedSurfaceProxy::surfFaces
const UList< Face > & surfFaces() const
Return const access to the faces.
Definition: MeshedSurfaceProxy.H:166
p0
const volScalarField & p0
Definition: EEqn.H:36
Foam::point
vector point
Point is a vector.
Definition: point.H:43
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:216
Foam::UnsortedMeshedSurface::sortedZones
surfZoneList sortedZones(labelList &faceMap) const
Sort faces according to zoneIds.
Definition: UnsortedMeshedSurface.C:462
Foam::MeshedSurfaceProxy::surfZones
const UList< surfZone > & surfZones() const
Const access to the surface zones.
Definition: MeshedSurfaceProxy.H:174