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-2026 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 "triSurfacePointMesh.H"
39 #include "PrimitivePatch.H"
40 #include "pointField.H"
41 #include "labelledTri.H"
42 #include "boolList.H"
44 #include "surfacePatchList.H"
45 #include "triFaceList.H"
46 #include "triadField.H"
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 class triSurface;
54 class Time;
55 class IFstream;
56 
57 // Forward declaration of friend functions and operators
58 Ostream& operator<<(Ostream&, const triSurface&);
59 
60 
61 /*---------------------------------------------------------------------------*\
62  Class triSurface Declaration
63 \*---------------------------------------------------------------------------*/
64 
65 class triSurface
66 :
67  public triSurfacePointMesh,
68  public PrimitivePatch<::Foam::List<labelledTri>, pointField>
69 {
70  // Private Typedefs
71 
72  typedef labelledTri Face;
74  ParentType;
75 
76 
77  // Private Data
78 
79  //- The number of bytes in the STL header
80  static const int STLheaderSize = 80;
81 
82  //- Patch information (face ordering nFaces/startFace only used
83  // during reading and writing)
85 
86 
87  // Demand driven private data.
88 
89  //- Edge-face addressing (sorted)
90  mutable labelListList* sortedEdgeFacesPtr_;
91 
92  //- Label of face that 'owns' edge (i.e. e.vec() is righthanded walk
93  // along face)
94  mutable labelList* edgeOwnerPtr_;
95 
96 
97  // Private Member Functions
98 
99  //- Calculate sorted edgeFaces
100  void calcSortedEdgeFaces() const;
101 
102  //- Calculate owner
103  void calcEdgeOwner() const;
104 
105  //- Sort faces according to region. Returns patch list
106  // and sets faceMap to index of labelledTri inside *this.
107  surfacePatchList calcPatches(labelList& faceMap) const;
108 
109  //- Sets default values for patches
110  void setDefaultPatches();
111 
112  //- Function to stitch the triangles by removing duplicate points.
113  // Returns true if any points merged
114  bool stitchTriangles
115  (
116  const scalar tol = small,
117  const bool verbose = false
118  );
119 
120  scalar pointNormalWeight
121  (
122  const triFace& f,
123  const label pi,
124  const vector& fa,
125  const pointField& points
126  ) const;
127 
128  //- Return the surface point normals
129  tmp<vectorField> weightedPointNormals() const;
130 
131  //- Return the curvature of surface at the points
132  tmp<triadField> pointCoordSys(const vectorField& pointNormals) const;
133 
134 
135  //- Read in Foam format
136  bool read(Istream&);
137 
138  //- Generic read routine. Chooses reader based on extension.
139  bool read(const fileName&, const word& ext, const bool check = true);
140 
141  bool readSTL(const fileName&);
142  bool readSTLASCII(const fileName&);
143  bool readSTLBINARY(const fileName&);
144  bool readGTS(const fileName&);
145  bool readOBJ(const fileName&);
146  bool readOFF(const fileName&);
147  bool readTRI(const fileName&);
148  bool readAC(const fileName&);
149  bool readNAS(const fileName&);
150  bool readVTK(const fileName&);
151 
152  //- Generic write routine. Chooses writer based on extension.
153  void write(const fileName&, const word& ext, const bool sort) const;
154 
155  //- Write to Ostream in ASCII STL format.
156  // Each region becomes 'solid' 'endsolid' block.
157  void writeSTLASCII(const bool writeSorted, Ostream&) const;
158 
159  //- Write to std::ostream in BINARY STL format
160  void writeSTLBINARY(std::ostream&) const;
161 
162  //- Write to Ostream in GTS (Gnu Tri Surface library)
163  // format.
164  void writeGTS(const bool writeSorted, Ostream&) const;
165 
166  //- Write to Ostream in OBJ (Lightwave) format.
167  // writeSorted=true: sort faces acc. to region and write as single
168  // group. =false: write in normal order.
169  void writeOBJ(const bool writeSorted, Ostream&) const;
170 
171  //- Write to Ostream in OFF (Geomview) format.
172  // writeSorted=true: sort faces acc. to region and write as single
173  // group. =false: write in normal order.
174  void writeOFF(const bool writeSorted, Ostream&) const;
175 
176  //- Write to VTK legacy format.
177  void writeVTK(const bool writeSorted, Ostream&) const;
178 
179  //- Write to Ostream in TRI (AC3D) format
180  // Ac3d .tri format (unmerged triangle format)
181  void writeTRI(const bool writeSorted, Ostream&) const;
182 
183  //- Write to Ostream in SMESH (tetgen) format
184  void writeSMESH(const bool writeSorted, Ostream&) const;
185 
186  //- Write to Ostream in AC3D format. Always sorted by patch.
187  void writeAC(Ostream&) const;
188 
189 
190  // Static private functions
191 
192  //- Convert faces to labelledTri. All get same region.
193  static List<labelledTri> convertToTri
194  (
195  const faceList&,
196  const label defaultRegion = 0
197  );
198 
199  //- Convert triFaces to labelledTri. All get same region.
200  static List<labelledTri> convertToTri
201  (
202  const triFaceList&,
203  const label defaultRegion = 0
204  );
205 
206  //- Helper function to print triangle info
207  static void printTriangle
208  (
209  Ostream&,
210  const Foam::string& pre,
211  const labelledTri&,
212  const pointField&
213  );
214 
215  //- Read non-comment line
216  static string getLineNoComment(IFstream&);
217 
218 
219 protected:
220 
221  // Protected Member Functions
222 
223  //- Non-const access to global points
225  {
226  return const_cast<pointField&>(ParentType::points());
227  }
228 
229  //- Non-const access to the faces
231  {
232  return static_cast<List<Face>&>(*this);
233  }
234 
235 
236 public:
237 
238  // Public Typedefs
239 
240  //- Mesh type
241  typedef triSurface Mesh;
242 
243 
244  //- Runtime type information
245  ClassName("triSurface");
246 
247 
248  // Static
249 
250  //- Name of triSurface directory to use.
251  static fileName triSurfInstance(const Time&);
252 
253 
254  // Constructors
255 
256  //- Construct null
257  triSurface();
258 
259  //- Construct from triangles, patches, points.
260  triSurface
261  (
262  const List<labelledTri>&,
264  const pointField&
265  );
266 
267  //- Construct from triangles, patches, points. Reuse storage.
268  triSurface
269  (
272  pointField&,
273  const bool reuse
274  );
275 
276  //- Construct from triangles, patches, points.
277  triSurface
278  (
281  pointField&&
282  );
283 
284  //- Construct from triangles, points. Set patchnames to default.
285  triSurface(const List<labelledTri>&, const pointField&);
286 
287  //- Construct from triangles, points. Set region to 0 and default
288  // patchName.
289  triSurface(const triFaceList&, const pointField&);
290 
291  //- Construct from file name (uses extension to determine type)
292  triSurface(const fileName&);
293 
294  //- Construct from Istream
296 
297  //- Construct from objectRegistry
298  triSurface(const Time& d);
299 
300  //- Copy constructor
301  triSurface(const triSurface&);
302 
303  //- Move constructor
305 
306 
307  //- Destructor
308  virtual ~triSurface();
309 
310  void clearOut();
311 
312  void clearTopology();
313 
314  void clearPatchMeshAddr();
315 
316 
317  // Member Functions
318 
319  // Access
320 
321  const geometricSurfacePatchList& patches() const
322  {
323  return patches_;
324  }
325 
327  {
328  return patches_;
329  }
330 
331  //- Return edge-face addressing sorted (for edges with more than
332  // 2 faces) according to the angle around the edge.
333  // Orientation is anticlockwise looking from
334  // edge.vec(localPoints())
335  const labelListList& sortedEdgeFaces() const;
336 
337  //- If 2 face neighbours: label of face where ordering of edge
338  // is consistent with righthand walk.
339  // If 1 neighbour: label of only face.
340  // If >2 neighbours: undetermined.
341  const labelList& edgeOwner() const;
342 
344 
345 
346  // Edit
347 
348  //- Move points
349  virtual void setPoints(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,
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,
392  ) const;
393 
394  //- Return new surface. Returns pointMap, faceMap from
395  // subsetMeshMap
397  (
398  const boolList& include,
399  labelList& pointMap,
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 #include "triSurfacePointMeshI.H"
450 
451 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
452 
453 #endif
454 
455 // ************************************************************************* //
Input from file stream.
Definition: IFstream.H:85
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A list of faces which address into the list of points.
const Field< PointType > & pointNormals() const
Return point normals for patch.
const Field< PointType > & points() const
Return reference to global points.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
Named list of face indices representing a sub-set of the mesh faces.
Definition: faceZone.H:66
A class for handling file names.
Definition: fileName.H:82
Triangle with additional region number.
Definition: labelledTri.H:60
A class for handling character strings derived from std::string.
Definition: string.H:79
A class for managing temporary objects.
Definition: tmp.H:55
A triangular face using a FixedList of labels corresponding to mesh vertices.
Definition: triFace.H:71
triSurface point mesh for fields on triSurface points
label size() const
Return size.
Triangulated surface description with patch information.
Definition: triSurface.H:68
virtual void scalePoints(const scalar)
Scale points. A non-positive factor is ignored.
Definition: triSurface.C:761
static fileName triSurfInstance(const Time &)
Name of triSurface directory to use.
Definition: triSurface.C:51
triSurface()
Construct null.
Definition: triSurface.C:535
triSurface subsetMesh(const boolList &include, labelList &pointMap, labelList &faceMap) const
Return new surface. Returns pointMap, faceMap from.
Definition: triSurface.C:1101
friend Ostream & operator<<(Ostream &, const triSurface &)
pointField & storedPoints()
Non-const access to global points.
Definition: triSurface.H:223
label markZones(const boolList &borderEdge, labelList &faceZone) const
(size and) fills faceZone with zone of face. Zone is area
Definition: triSurface.C:1010
tmp< scalarField > curvature() const
Return the curvature of surface at the points.
Definition: triSurface.C:1155
void operator=(const triSurface &)
Definition: triSurface.C:1397
void cleanup(const bool verbose)
Remove non-valid triangles.
Definition: triSurface.C:932
void clearTopology()
Definition: triSurface.C:703
const geometricSurfacePatchList & patches() const
Definition: triSurface.H:320
void checkTriangles(const bool verbose)
Check/remove duplicate/degenerate triangles.
Definition: triSurface.C:777
void checkEdges(const bool verbose)
Check triply (or more) connected edges.
Definition: triSurface.C:906
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:947
void writeStats(Ostream &) const
Write some statistics.
Definition: triSurface.C:1364
faceList faces() const
Return the list of triangles as a faceList.
Definition: triSurface.C:1142
const labelList & edgeOwner() const
If 2 face neighbours: label of face where ordering of edge.
Definition: triSurface.C:737
void subsetMeshMap(const boolList &include, labelList &pointMap, labelList &faceMap) const
'Create' sub mesh, including only faces for which
Definition: triSurface.C:1055
List< Face > & storedFaces()
Non-const access to the faces.
Definition: triSurface.H:229
triSurface Mesh
Mesh type.
Definition: triSurface.H:240
void clearPatchMeshAddr()
Definition: triSurface.C:711
const labelListList & sortedEdgeFaces() const
Return edge-face addressing sorted (for edges with more than.
Definition: triSurface.C:726
ClassName("triSurface")
Runtime type information.
virtual ~triSurface()
Destructor.
Definition: triSurface.C:695
virtual void setPoints(const pointField &)
Move points.
Definition: triSurface.C:748
A class for handling words, derived from string.
Definition: word.H:63
Namespace for OpenFOAM.
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 sort(UList< T > &)
Definition: UList.C:115
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
labelList f(nPoints)