triSurface.H
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 Class
28  Foam::triSurface
29 
30 Description
31  Triangulated surface description with patch information.
32 
33 SourceFiles
34  triSurface.C
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef triSurface_H
39 #define triSurface_H
40 
41 #include "PrimitivePatch.H"
42 #include "pointField.H"
43 #include "labelledTri.H"
44 #include "boolList.H"
46 #include "surfacePatchList.H"
47 #include "triFaceList.H"
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 namespace Foam
52 {
53 
54 // Forward declarations
55 
56 class Time;
57 class IFstream;
58 class surfZone;
59 class triSurface;
60 
61 template<class Face> class MeshedSurface;
62 
63 Istream& operator>>(Istream&, triSurface&);
64 Ostream& operator<<(Ostream&, const triSurface&);
65 
66 
67 /*---------------------------------------------------------------------------*\
68  Class triSurface Declaration
69 \*---------------------------------------------------------------------------*/
70 
71 class triSurface
72 :
73  public PrimitivePatch<labelledTri, ::Foam::List, pointField, point>
74 {
75  // Private typedefs
76 
77  //- Typedefs for convenience
78  typedef labelledTri Face;
79  typedef PrimitivePatch
80  <
83  pointField,
84  point
85  >
86  ParentType;
87 
88 
89  // Private data
90 
91  //- Patch information
92  // (face ordering nFaces/startFace only used during reading, writing)
94 
95  static wordHashSet readTypes_;
96 
97  static wordHashSet writeTypes_;
98 
99 
100  // Demand driven private data.
101 
102  //- Edge-face addressing (sorted)
103  mutable labelListList* sortedEdgeFacesPtr_;
104 
105  //- Label of face that 'owns' edge (i.e. e.vec() is righthanded walk
106  //- along face)
107  mutable labelList* edgeOwnerPtr_;
108 
109 
110  // Private Member Functions
111 
112  //- Calculate sorted edgeFaces
113  void calcSortedEdgeFaces() const;
114 
115  //- Calculate owner
116  void calcEdgeOwner() const;
117 
118  //- Sort faces according to region.
119  // Returns patch list and sets faceMap to index of labelledTri
120  // inside *this.
121  surfacePatchList calcPatches(labelList& faceMap) const;
122 
123  //- Sets default values for patches
124  void setDefaultPatches();
125 
126  //- Function to stitch the triangles by removing duplicate points.
127  // Returns true if any points merged
128  bool stitchTriangles
129  (
130  const scalar tol = SMALL,
131  const bool verbose = false
132  );
133 
134  //- Read in OpenFOAM format
135  bool read(Istream& is);
136 
137  //- Read in STL format
138  bool readSTL(const fileName& filename, bool forceBinary=false);
139 
140  //- Generic read routine. Chooses reader based on extension.
141  bool read
142  (
143  const fileName& filename,
144  const word& ext,
145  const bool check = true
146  );
147 
148  //- Write STL ASCII format.
149  // Each region becomes a 'solid' 'endsolid' block.
150  void writeSTLASCII(const fileName& filename, const bool sort) const;
151 
152  //- Write STL BINARY format
153  void writeSTLBINARY(const fileName& filename) const;
154 
155  //- Write GTS (Gnu Tri Surface library) format.
156  void writeGTS(const fileName& filename, const bool sort) const;
157 
158  //- Generic write routine. Chooses writer based on extension.
159  // The sort option may not have an effect.
160  void write
161  (
162  const fileName& filename,
163  const word& ext,
164  const bool sort
165  ) const;
166 
167 
168  // Static private functions
169 
170  //- Convert faces to labelledTri. All get same region.
171  static List<labelledTri> convertToTri
172  (
173  const faceList& faces,
174  const label defaultRegion = 0
175  );
176 
177  //- Convert triFaces to labelledTri. All get same region.
178  static List<labelledTri> convertToTri
179  (
180  const triFaceList& faces,
181  const label defaultRegion = 0
182  );
183 
184  //- Helper function to print triangle info
185  static void printTriangle
186  (
187  Ostream& os,
188  const string& pre,
189  const labelledTri& f,
190  const pointField& points
191  );
192 
193 
194 protected:
195 
196  // Protected Member Functions
197 
198  //- Non-const access to global points
200  {
201  return const_cast<pointField&>(ParentType::points());
202  }
203 
204  //- Non-const access to the faces
206  {
207  return static_cast<List<Face>&>(*this);
208  }
209 
210 
211 public:
212 
213  // Public Typedefs
214 
215  //- Placeholder only, but do not remove - it is needed for GeoMesh
216  typedef bool BoundaryMesh;
217 
218  //- Runtime type information
219  ClassName("triSurface");
220 
221 
222  // Static
223 
224  //- Name of triSurface directory to use.
225  static fileName triSurfInstance(const Time&);
226 
227  //- Can we read this file format?
228  static bool canRead(const fileName& name, const bool verbose=false);
229 
230  //- Can we read this file format?
231  static bool canReadType(const word& ext, const bool verbose=false);
232 
233  //- Can we write this file format?
234  static bool canWriteType(const word& ext, const bool verbose=false);
235 
236  //- Known readable file-types
237  static const wordHashSet& readTypes();
238 
239  //- Known writable file-types
240  static const wordHashSet& writeTypes();
241 
242 
243  // Constructors
244 
245  //- Construct null
246  triSurface();
247 
248  //- Copy construct
249  triSurface(const triSurface& surf);
250 
251  //- Move construct
252  triSurface(triSurface&& surf);
253 
254  //- Construct from triangles, patches, points.
255  triSurface
256  (
257  const List<labelledTri>& triangles,
259  const pointField& pts
260  );
261 
262  //- Construct from triangles, patches, points. Reuse storage.
263  triSurface
264  (
265  List<labelledTri>& triangles,
267  pointField& pts,
268  const bool reuse
269  );
270 
271  //- Construct from triangles, points.
272  //- Set patch names to default.
273  triSurface
274  (
275  const List<labelledTri>& triangles,
276  const pointField& pts
277  );
278 
279  //- Construct from triangles, points.
280  //- Set region to 0 and default patchName.
281  triSurface
282  (
283  const triFaceList& triangles,
284  const pointField& pts
285  );
286 
287  //- Construct from file name (uses extension to determine type).
288  // Optional (positive, non-zero) point scaling is possible.
289  triSurface(const fileName& name, const scalar scaleFactor = -1);
290 
291  //- Construct from file name (uses extension to determine type)
292  triSurface
293  (
294  const fileName& name,
295  const word& ext,
296  const scalar scaleFactor = -1
297  );
298 
299  //- Construct from Istream
300  triSurface(Istream& is);
301 
302  //- Construct from objectRegistry
303  triSurface(const Time& d);
304 
305 
306  //- Destructor
307  virtual ~triSurface();
308 
309 
310  // Member Functions
311 
312  void clearOut();
313 
314  void clearTopology();
315 
316  void clearPatchMeshAddr();
317 
318  void swap(triSurface& surf);
319 
320 
321  // Access
322 
323  const geometricSurfacePatchList& patches() const
324  {
325  return patches_;
326  }
327 
329  {
330  return patches_;
331  }
332 
333  //- Return const access to the faces
334  inline const List<labelledTri>& surfFaces() const
335  {
336  return static_cast<const List<labelledTri>&>(*this);
337  }
338 
339  //- Return edge-face addressing sorted (for edges with more than
340  // 2 faces) according to the angle around the edge.
341  // Orientation is anticlockwise looking from
342  // edge.vec(localPoints())
343  const labelListList& sortedEdgeFaces() const;
344 
345  //- If 2 face neighbours: label of face where ordering of edge
346  // is consistent with righthand walk.
347  // If 1 neighbour: label of only face.
348  // If >2 neighbours: undetermined.
349  const labelList& edgeOwner() const;
350 
351 
352  //- Face area vectors (normals)
353  inline const vectorField& Sf() const
354  {
355  return ParentType::faceAreas();
356  }
357 
358  //- Face area magnitudes
359  inline const scalarField& magSf() const
360  {
361  return ParentType::magFaceAreas();
362  }
363 
364  //- Face centres
365  inline const vectorField& Cf() const
366  {
367  return ParentType::faceCentres();
368  }
369 
370 
371  // Interoperability with other surface mesh classes
372 
373  //- Sort faces according to zoneIds
374  // Returns a surfZoneList and sets faceMap to index within faces()
375  // (i.e. map from original,unsorted to sorted)
377 
378  //- Create a list of faces from the triFaces
379  void triFaceFaces(List<face>& plainFaceList) const;
380 
381 
382  // Edit
383 
384  //- Move points
385  virtual void movePoints(const pointField& pts);
386 
387  //- Swap points. Similar to movePoints, but returns the old points
388  virtual void swapPoints(pointField& pts);
389 
390  //- Scale points. A non-positive factor is ignored.
391  virtual void scalePoints(const scalar scaleFactor);
392 
393  //- Check/remove duplicate/degenerate triangles
394  void checkTriangles(const bool verbose);
395 
396  //- Check triply (or more) connected edges.
397  void checkEdges(const bool verbose);
398 
399  //- Remove non-valid triangles
400  void cleanup(const bool verbose);
401 
402  //- Fill faceZone with currentZone for every face reachable
403  // from facei without crossing edge marked in borderEdge.
404  // Note: faceZone has to be sized nFaces before calling this fun.
405  void markZone
406  (
407  const boolList& borderEdge,
408  const label facei,
409  const label currentZone,
411  ) const;
412 
413  //- (size and) fills faceZone with zone of face. Zone is area
414  // reachable by edge crossing without crossing borderEdge
415  // (bool for every edge in surface). Returns number of zones.
417  (
418  const boolList& borderEdge,
420  ) const;
421 
422  //- 'Create' sub mesh
423  //
424  // \param[in] include the faces to select
425  // \param[out] pointMap from new to old localPoints
426  // \param[out] faceMap from new to old faces
427  void subsetMeshMap
428  (
429  const boolList& include,
430  labelList& pointMap,
432  ) const;
433 
434  //- Return new surface
435  //
436  // \param[in] include the faces to select
437  // \param[out] pointMap from subsetMeshMap
438  // \param[out] faceMap from subsetMeshMap
440  (
441  const boolList& include,
442  labelList& pointMap,
444  ) const;
445 
446  //- Return new surface
447  //
448  // \param[in] include the faces to select
449  triSurface subsetMesh(const boolList& include) const;
450 
451 
452  //- Swap the list of faces being addressed
453  void swapFaces(List<labelledTri>& faceLst);
454 
455  //- Alter contents by transferring (triangles, points) components.
456  // Patch information is small and therefore just copied.
457  void transfer(triSurface& surf);
458 
459  //- Alter contents by transferring (triangles, points) components.
460  // Patch information is small and therefore just copied.
462 
463 
464  // Write
465 
466  //- Write to Ostream in simple FOAM format
467  void write(Ostream& os) const;
468 
469  //- Generic write routine. Chooses writer based on extension.
470  void write(const fileName&, const bool sortByRegion = false) const;
471 
472  //- Write to database
473  void write(const Time& d) const;
474 
475  //- Write some statistics
476  void writeStats(Ostream& os) const;
477 
478 
479  // Member operators
480 
481  //- Copy assignment
482  void operator=(const triSurface& surf);
483 
484  //- Move assignment
485  void operator=(triSurface&& surf);
486 
487  //- Move assignment
489 
490 
491  // IOstream Operators
492 
493  friend Istream& operator>>(Istream&, triSurface&);
494  friend Ostream& operator<<(Ostream&, const triSurface&);
495 };
496 
497 
498 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
499 
500 } // End namespace Foam
501 
502 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
503 
504 #endif
505 
506 // ************************************************************************* //
Foam::triSurface::writeStats
void writeStats(Ostream &os) const
Write some statistics.
Definition: triSurfaceIO.C:327
Foam::pointField
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:44
surfacePatchList.H
Foam::triSurface::ClassName
ClassName("triSurface")
Runtime type information.
geometricSurfacePatchList.H
Foam::triSurface::subsetMeshMap
void subsetMeshMap(const boolList &include, labelList &pointMap, labelList &faceMap) const
'Create' sub mesh
Definition: triSurface.C:778
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::faceMap
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
Definition: blockMeshMergeFast.C:94
Foam::PrimitivePatch< labelledTri, ::Foam::List, pointField, point >::points
const Field< point > & points() const
Return reference to global points.
Definition: PrimitivePatch.H:300
boolList.H
Foam::triSurface::clearOut
void clearOut()
Definition: triSurface.C:542
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::triSurface::Sf
const vectorField & Sf() const
Face area vectors (normals)
Definition: triSurface.H:352
Foam::triSurface::markZone
void markZone(const boolList &borderEdge, const label facei, const label currentZone, labelList &faceZone) const
Fill faceZone with currentZone for every face reachable.
Definition: triSurface.C:674
Foam::triSurface::writeTypes
static const wordHashSet & writeTypes()
Known writable file-types.
Definition: triSurfaceIO.C:60
Foam::triSurface::triFaceFaces
void triFaceFaces(List< face > &plainFaceList) const
Create a list of faces from the triFaces.
Definition: triSurface.C:660
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:228
Foam::triSurface::clearPatchMeshAddr
void clearPatchMeshAddr()
Definition: triSurface.C:536
Foam::triSurface::patches
const geometricSurfacePatchList & patches() const
Definition: triSurface.H:322
Foam::HashSet< word >
Foam::triSurface::~triSurface
virtual ~triSurface()
Destructor.
Definition: triSurface.C:520
Foam::triSurface::operator>>
friend Istream & operator>>(Istream &, triSurface &)
Foam::triSurface::patches
geometricSurfacePatchList & patches()
Definition: triSurface.H:327
Foam::PrimitivePatch< labelledTri, ::Foam::List, pointField, point >::faceAreas
const Field< point > & faceAreas() const
Return face area vectors for patch.
Definition: PrimitivePatch.C:537
Foam::triSurface::Cf
const vectorField & Cf() const
Face centres.
Definition: triSurface.H:364
Foam::triSurface::swapFaces
void swapFaces(List< labelledTri > &faceLst)
Swap the list of faces being addressed.
Definition: triSurface.C:870
Foam::triSurface::scalePoints
virtual void scalePoints(const scalar scaleFactor)
Scale points. A non-positive factor is ignored.
Definition: triSurface.C:614
Foam::triSurface::checkTriangles
void checkTriangles(const bool verbose)
Check/remove duplicate/degenerate triangles.
Definition: triSurface.C:155
Foam::triSurface::subsetMesh
triSurface subsetMesh(const boolList &include, labelList &pointMap, labelList &faceMap) const
Return new surface.
Definition: triSurface.C:822
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::triSurface::movePoints
virtual void movePoints(const pointField &pts)
Move points.
Definition: triSurface.C:588
Foam::Field< vector >
PrimitivePatch.H
Foam::triSurface
Triangulated surface description with patch information.
Definition: triSurface.H:70
Foam::triSurface::swapPoints
virtual void swapPoints(pointField &pts)
Swap points. Similar to movePoints, but returns the old points.
Definition: triSurface.C:601
Foam::faceZone
A subset of mesh faces organised as a primitive patch.
Definition: faceZone.H:65
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::sort
void sort(UList< T > &a)
Definition: UList.C:241
Foam::triSurface::canWriteType
static bool canWriteType(const word &ext, const bool verbose=false)
Can we write this file format?
Definition: triSurfaceIO.C:85
Foam::triSurface::transfer
void transfer(triSurface &surf)
Alter contents by transferring (triangles, points) components.
Definition: triSurface.C:878
Foam::triSurface::triSurface
triSurface()
Construct null.
Definition: triSurface.C:405
Foam::triSurface::storedPoints
pointField & storedPoints()
Non-const access to global points.
Definition: triSurface.H:198
Foam::triSurface::sortedZones
List< surfZone > sortedZones(labelList &faceMap) const
Sort faces according to zoneIds.
Definition: triSurface.C:646
Foam::triSurface::storedFaces
List< Face > & storedFaces()
Non-const access to the faces.
Definition: triSurface.H:204
Foam::triSurface::clearTopology
void clearTopology()
Definition: triSurface.C:528
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::triSurface::edgeOwner
const labelList & edgeOwner() const
If 2 face neighbours: label of face where ordering of edge.
Definition: triSurface.C:577
Foam::triSurface::cleanup
void cleanup(const bool verbose)
Remove non-valid triangles.
Definition: triSurface.C:631
pointField.H
Foam::triSurface::surfFaces
const List< labelledTri > & surfFaces() const
Return const access to the faces.
Definition: triSurface.H:333
Foam::triSurface::markZones
label markZones(const boolList &borderEdge, labelList &faceZone) const
(size and) fills faceZone with zone of face. Zone is area
Definition: triSurface.C:733
Foam::triSurface::magSf
const scalarField & magSf() const
Face area magnitudes.
Definition: triSurface.H:358
Foam::triSurface::swap
void swap(triSurface &surf)
Definition: triSurface.C:550
Foam::triSurface::BoundaryMesh
bool BoundaryMesh
Placeholder only, but do not remove - it is needed for GeoMesh.
Definition: triSurface.H:215
f
labelList f(nPoints)
Foam::Vector< scalar >
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:102
Foam::triSurface::operator<<
friend Ostream & operator<<(Ostream &, const triSurface &)
Foam::PrimitivePatch< labelledTri, ::Foam::List, pointField, point >::faceCentres
const Field< point > & faceCentres() const
Return face centres for patch.
Definition: PrimitivePatch.C:517
Foam::labelledTri
Triangle with additional region number.
Definition: labelledTri.H:58
Foam::PrimitivePatch< labelledTri, ::Foam::List, pointField, point >::magFaceAreas
const Field< scalar > & magFaceAreas() const
Return face area magnitudes for patch.
Definition: PrimitivePatch.C:557
Foam::triSurface::canReadType
static bool canReadType(const word &ext, const bool verbose=false)
Can we read this file format?
Definition: triSurfaceIO.C:73
Foam::triSurface::canRead
static bool canRead(const fileName &name, const bool verbose=false)
Can we read this file format?
Definition: triSurfaceIO.C:97
Foam::triSurface::readTypes
static const wordHashSet & readTypes()
Known readable file-types.
Definition: triSurfaceIO.C:46
Foam::triSurface::sortedEdgeFaces
const labelListList & sortedEdgeFaces() const
Return edge-face addressing sorted (for edges with more than.
Definition: triSurface.C:566
labelledTri.H
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::triSurface::operator=
void operator=(const triSurface &surf)
Copy assignment.
Definition: triSurface.C:916
triFaceList.H
Foam::MeshedSurface
A surface geometry mesh with zone information, not to be confused with the similarly named surfaceMes...
Definition: triSurfaceTools.H:80
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102
Foam::triSurface::triSurfInstance
static fileName triSurfInstance(const Time &)
Name of triSurface directory to use.
Definition: triSurface.C:46
Foam::PrimitivePatch
A list of faces which address into the list of points.
Definition: PrimitivePatch.H:90
Foam::triSurface::checkEdges
void checkEdges(const bool verbose)
Check triply (or more) connected edges.
Definition: triSurface.C:274