triSurface.H
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-2022 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 Class
25  Foam::triSurface
26 
27 Description
28  Triangulated surface description with patch information.
29 
30 SourceFiles
31  triSurface.C
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #ifndef triSurface_H
36 #define triSurface_H
37 
38 #include "PrimitivePatch.H"
39 #include "pointField.H"
40 #include "labelledTri.H"
41 #include "boolList.H"
43 #include "surfacePatchList.H"
44 #include "triFaceList.H"
45 #include "triadField.H"
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 
52 class Time;
53 class IFstream;
54 
55 
56 // Forward declaration of friend functions and operators
57 
58 class triSurface;
59 
60 Ostream& operator<<(Ostream&, const triSurface&);
61 
62 
63 /*---------------------------------------------------------------------------*\
64  Class triSurface Declaration
65 \*---------------------------------------------------------------------------*/
66 
67 class triSurface
68 :
69  public PrimitivePatch<::Foam::List<labelledTri>, pointField>
70 {
71  // Private Typedefs
72 
73  //- Typedefs for convenience
74  typedef labelledTri Face;
76  ParentType;
77 
78 
79  // Private Data
80 
81  //- The number of bytes in the STL header
82  static const int STLheaderSize = 80;
83 
84  //- Patch information (face ordering nFaces/startFace only used
85  // during reading and writing)
87 
88 
89  // Demand driven private data.
90 
91  //- Edge-face addressing (sorted)
92  mutable labelListList* sortedEdgeFacesPtr_;
93 
94  //- Label of face that 'owns' edge (i.e. e.vec() is righthanded walk
95  // along face)
96  mutable labelList* edgeOwnerPtr_;
97 
98 
99  // Private Member Functions
100 
101  //- Calculate sorted edgeFaces
102  void calcSortedEdgeFaces() const;
103 
104  //- Calculate owner
105  void calcEdgeOwner() const;
106 
107  //- Sort faces according to region. Returns patch list
108  // and sets faceMap to index of labelledTri inside *this.
109  surfacePatchList calcPatches(labelList& faceMap) const;
110 
111  //- Sets default values for patches
112  void setDefaultPatches();
113 
114  //- Function to stitch the triangles by removing duplicate points.
115  // Returns true if any points merged
116  bool stitchTriangles
117  (
118  const scalar tol = small,
119  const bool verbose = false
120  );
121 
122  scalar pointNormalWeight
123  (
124  const triFace& f,
125  const label pi,
126  const vector& fa,
127  const pointField& points
128  ) const;
129 
130  //- Return the surface point normals
131  tmp<vectorField> weightedPointNormals() const;
132 
133  //- Return the curvature of surface at the points
134  tmp<triadField> pointCoordSys(const vectorField& pointNormals) const;
135 
136 
137  //- Read in Foam format
138  bool read(Istream&);
139 
140  //- Generic read routine. Chooses reader based on extension.
141  bool read(const fileName&, const word& ext, const bool check = true);
142 
143  bool readSTL(const fileName&);
144  bool readSTLASCII(const fileName&);
145  bool readSTLBINARY(const fileName&);
146  bool readGTS(const fileName&);
147  bool readOBJ(const fileName&);
148  bool readOFF(const fileName&);
149  bool readTRI(const fileName&);
150  bool readAC(const fileName&);
151  bool readNAS(const fileName&);
152  bool readVTK(const fileName&);
153 
154  //- Generic write routine. Chooses writer based on extension.
155  void write(const fileName&, const word& ext, const bool sort) const;
156 
157  //- Write to Ostream in ASCII STL format.
158  // Each region becomes 'solid' 'endsolid' block.
159  void writeSTLASCII(const bool writeSorted, Ostream&) const;
160 
161  //- Write to std::ostream in BINARY STL format
162  void writeSTLBINARY(std::ostream&) const;
163 
164  //- Write to Ostream in GTS (Gnu Tri Surface library)
165  // format.
166  void writeGTS(const bool writeSorted, Ostream&) const;
167 
168  //- Write to Ostream in OBJ (Lightwave) format.
169  // writeSorted=true: sort faces acc. to region and write as single
170  // group. =false: write in normal order.
171  void writeOBJ(const bool writeSorted, Ostream&) const;
172 
173  //- Write to Ostream in OFF (Geomview) format.
174  // writeSorted=true: sort faces acc. to region and write as single
175  // group. =false: write in normal order.
176  void writeOFF(const bool writeSorted, Ostream&) const;
177 
178  //- Write to VTK legacy format.
179  void writeVTK(const bool writeSorted, Ostream&) const;
180 
181  //- Write to Ostream in TRI (AC3D) format
182  // Ac3d .tri format (unmerged triangle format)
183  void writeTRI(const bool writeSorted, Ostream&) const;
184 
185  //- Write to Ostream in SMESH (tetgen) format
186  void writeSMESH(const bool writeSorted, Ostream&) const;
187 
188  //- Write to Ostream in AC3D format. Always sorted by patch.
189  void writeAC(Ostream&) const;
190 
191 
192  // Static private functions
193 
194  //- Convert faces to labelledTri. All get same region.
195  static List<labelledTri> convertToTri
196  (
197  const faceList&,
198  const label defaultRegion = 0
199  );
200 
201  //- Convert triFaces to labelledTri. All get same region.
202  static List<labelledTri> convertToTri
203  (
204  const triFaceList&,
205  const label defaultRegion = 0
206  );
207 
208  //- Helper function to print triangle info
209  static void printTriangle
210  (
211  Ostream&,
212  const Foam::string& pre,
213  const labelledTri&,
214  const pointField&
215  );
216 
217  //- Read non-comment line
218  static string getLineNoComment(IFstream&);
219 
220 
221 protected:
222 
223  // Protected Member Functions
224 
225  //- Non-const access to global points
227  {
228  return const_cast<pointField&>(ParentType::points());
229  }
230 
231  //- Non-const access to the faces
233  {
234  return static_cast<List<Face>&>(*this);
235  }
236 
237 
238 public:
239 
240  // Public Typedefs
241 
242  //- Placeholder only, but do not remove - it is needed for GeoMesh
243  typedef bool BoundaryMesh;
244 
245 
246  //- Runtime type information
247  ClassName("triSurface");
248 
249 
250  // Static
251 
252  //- Name of triSurface directory to use.
253  static fileName triSurfInstance(const Time&);
254 
255 
256  // Constructors
257 
258  //- Construct null
259  triSurface();
260 
261  //- Construct from triangles, patches, points.
262  triSurface
263  (
264  const List<labelledTri>&,
266  const pointField&
267  );
268 
269  //- Construct from triangles, patches, points. Reuse storage.
270  triSurface
271  (
274  pointField&,
275  const bool reuse
276  );
277 
278  //- Construct from triangles, patches, points.
279  triSurface
280  (
283  pointField&&
284  );
285 
286  //- Construct from triangles, points. Set patchnames to default.
287  triSurface(const List<labelledTri>&, const pointField&);
288 
289  //- Construct from triangles, points. Set region to 0 and default
290  // patchName.
291  triSurface(const triFaceList&, const pointField&);
292 
293  //- Construct from file name (uses extension to determine type)
294  triSurface(const fileName&);
295 
296  //- Construct from Istream
298 
299  //- Construct from objectRegistry
300  triSurface(const Time& d);
301 
302  //- Copy constructor
303  triSurface(const triSurface&);
304 
305  //- Move constructor
307 
308 
309  //- Destructor
310  virtual ~triSurface();
311 
312  void clearOut();
313 
314  void clearTopology();
315 
316  void clearPatchMeshAddr();
317 
318 
319  // Member Functions
320 
321  // Access
323  const geometricSurfacePatchList& patches() const
324  {
325  return patches_;
326  }
329  {
330  return patches_;
331  }
332 
333  //- Return edge-face addressing sorted (for edges with more than
334  // 2 faces) according to the angle around the edge.
335  // Orientation is anticlockwise looking from
336  // edge.vec(localPoints())
337  const labelListList& sortedEdgeFaces() const;
338 
339  //- If 2 face neighbours: label of face where ordering of edge
340  // is consistent with righthand walk.
341  // If 1 neighbour: label of only face.
342  // If >2 neighbours: undetermined.
343  const labelList& edgeOwner() const;
344 
345 
346  // Edit
347 
348  //- Move points
349  virtual void movePoints(const pointField&);
350 
351  //- Scale points. A non-positive factor is ignored
352  virtual void scalePoints(const scalar);
353 
354  //- Check/remove duplicate/degenerate triangles
355  void checkTriangles(const bool verbose);
356 
357  //- Check triply (or more) connected edges.
358  void checkEdges(const bool verbose);
359 
360  //- Remove non-valid triangles
361  void cleanup(const bool verbose);
362 
363  //- Fill faceZone with currentZone for every face reachable
364  // from facei without crossing edge marked in borderEdge.
365  // Note: faceZone has to be sized nFaces before calling this fun.
366  void markZone
367  (
368  const boolList& borderEdge,
369  const label facei,
370  const label currentZone,
372  ) const;
373 
374  //- (size and) fills faceZone with zone of face. Zone is area
375  // reachable by edge crossing without crossing borderEdge
376  // (bool for every edge in surface). Returns number of zones.
378  (
379  const boolList& borderEdge,
380  labelList& faceZone
381  ) const;
382 
383  //- 'Create' sub mesh, including only faces for which
384  // boolList entry is true
385  // Sets: pointMap: from new to old localPoints
386  // faceMap: new to old faces
387  void subsetMeshMap
388  (
389  const boolList& include,
390  labelList& pointMap,
391  labelList& faceMap
392  ) const;
393 
394  //- Return new surface. Returns pointMap, faceMap from
395  // subsetMeshMap
397  (
398  const boolList& include,
399  labelList& pointMap,
400  labelList& faceMap
401  ) const;
402 
403 
404  // Conversion
405 
406  //- Return the list of triangles as a faceList
407  faceList faces() const;
408 
409 
410  // Analysis
411 
412  //- Return the curvature of surface at the points
413  tmp<scalarField> curvature() const;
414 
415 
416  // Write
417 
418  //- Write to Ostream in simple FOAM format
419  void write(Ostream&) const;
420 
421  //- Generic write routine. Chooses writer based on extension.
422  void write(const fileName&, const bool sortByRegion = false) const;
423 
424  //- Write to database
425  void write(const Time&) const;
426 
427  //- Write some statistics
428  void writeStats(Ostream&) const;
429 
430 
431  // Member Operators
432 
433  void operator=(const triSurface&);
434  void operator=(triSurface&&);
435 
436 
437  // Ostream Operator
438 
439  friend Ostream& operator<<(Ostream&, const triSurface&);
440 };
441 
442 
443 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
444 
445 } // End namespace Foam
446 
447 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
448 
449 #endif
450 
451 // ************************************************************************* //
void cleanup(const bool verbose)
Remove non-valid triangles.
Definition: triSurface.C:920
A class for handling file names.
Definition: fileName.H:79
friend Ostream & operator<<(Ostream &, const triSurface &)
void operator=(const triSurface &)
Definition: triSurface.C:1385
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
tmp< scalarField > curvature() const
Return the curvature of surface at the points.
Definition: triSurface.C:1143
virtual void scalePoints(const scalar)
Scale points. A non-positive factor is ignored.
Definition: triSurface.C:749
void clearPatchMeshAddr()
Definition: triSurface.C:699
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
virtual void movePoints(const pointField &)
Move points.
Definition: triSurface.C:736
A list of faces which address into the list of points.
static fileName triSurfInstance(const Time &)
Name of triSurface directory to use.
Definition: triSurface.C:50
ClassName("triSurface")
Runtime type information.
const labelListList & sortedEdgeFaces() const
Return edge-face addressing sorted (for edges with more than.
Definition: triSurface.C:714
void writeStats(Ostream &) const
Write some statistics.
Definition: triSurface.C:1352
A class for handling words, derived from string.
Definition: word.H:59
A triangular face using a FixedList of labels corresponding to mesh vertices.
Definition: triFace.H:68
void checkTriangles(const bool verbose)
Check/remove duplicate/degenerate triangles.
Definition: triSurface.C:765
void sort(UList< T > &)
Definition: UList.C:115
List< Face > & storedFaces()
Non-const access to the faces.
Definition: triSurface.H:231
void checkEdges(const bool verbose)
Check triply (or more) connected edges.
Definition: triSurface.C:894
const Field< PointType > & points() const
Return reference to global points.
Triangle with additional region number.
Definition: labelledTri.H:57
pointField & storedPoints()
Non-const access to global points.
Definition: triSurface.H:225
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
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:935
Input from file stream.
Definition: IFstream.H:81
labelList f(nPoints)
const Field< PointType > & pointNormals() const
Return point normals for patch.
const geometricSurfacePatchList & patches() const
Definition: triSurface.H:322
triSurface()
Construct null.
Definition: triSurface.C:534
void subsetMeshMap(const boolList &include, labelList &pointMap, labelList &faceMap) const
&#39;Create&#39; sub mesh, including only faces for which
Definition: triSurface.C:1043
Ostream & operator<<(Ostream &, const ensightPart &)
bool BoundaryMesh
Placeholder only, but do not remove - it is needed for GeoMesh.
Definition: triSurface.H:242
faceList faces() const
Return the list of triangles as a faceList.
Definition: triSurface.C:1130
A subset of mesh faces organised as a primitive patch.
Definition: faceZone.H:65
label markZones(const boolList &borderEdge, labelList &faceZone) const
(size and) fills faceZone with zone of face. Zone is area
Definition: triSurface.C:998
virtual ~triSurface()
Destructor.
Definition: triSurface.C:683
void clearTopology()
Definition: triSurface.C:691
A class for managing temporary objects.
Definition: PtrList.H:53
triSurface subsetMesh(const boolList &include, labelList &pointMap, labelList &faceMap) const
Return new surface. Returns pointMap, faceMap from.
Definition: triSurface.C:1089
Triangulated surface description with patch information.
Definition: triSurface.H:66
const labelList & edgeOwner() const
If 2 face neighbours: label of face where ordering of edge.
Definition: triSurface.C:725
A class for handling character strings derived from std::string.
Definition: string.H:76
Namespace for OpenFOAM.