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