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