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