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"
42 #include "geometricSurfacePatchList.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)
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.
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.
183  (
184  const faceList&,
185  const label defaultRegion = 0
186  );
187 
188  //- Convert triFaces to labelledTri. All get same region.
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
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
306 
307  const geometricSurfacePatchList& patches() const
308  {
309  return patches_;
310  }
311 
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,
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,
376  ) const;
377 
378  //- Return new surface. Returns pointMap, faceMap from
379  // subsetMeshMap
381  (
382  const boolList& include,
383  labelList& pointMap,
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 // ************************************************************************* //
Foam::triSurface::writeGTS
void writeGTS(const bool writeSorted, Ostream &) const
Write to Ostream in GTS (Gnu Tri Surface library)
Definition: writeGTS.C:35
Foam::pointField
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
Foam::triSurface::convertToTri
static List< labelledTri > convertToTri(const faceList &, const label defaultRegion=0)
Convert faces to labelledTri. All get same region.
Definition: triSurface.C:95
Foam::triSurface::ClassName
ClassName("triSurface")
Runtime type information.
Foam::triSurface::subsetMeshMap
void subsetMeshMap(const boolList &include, labelList &pointMap, labelList &faceMap) const
'Create' sub mesh, including only faces for which
Definition: triSurface.C:955
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
Foam::faceMap
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
Definition: blockMeshMergeFast.C:90
Foam::PrimitivePatch::points
const Field< PointType > & points() const
Return reference to global points.
Definition: PrimitivePatchTemplate.H:282
boolList.H
Foam::triSurface::clearOut
void clearOut()
Clear all data.
Definition: triSurface.C:757
Foam::triSurface::writeStats
void writeStats(Ostream &) const
Write some statistics.
Definition: triSurface.C:1089
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::triSurface::readSTLBINARY
bool readSTLBINARY(const fileName &)
Definition: readSTLBINARY.C:41
Foam::triSurface::STLheaderSize
static const int STLheaderSize
The number of bytes in the STL header.
Definition: triSurface.H:79
Foam::triSurface::checkTriangles
void checkTriangles(const bool verbose=false)
Check/remove duplicate/degenerate triangles.
Definition: triSurface.C:186
Foam::IFstream
Input from file stream.
Definition: IFstream.H:81
Foam::triSurface::read
bool read(Istream &)
Read in Foam format.
Definition: triSurface.C:349
Foam::triSurface::writeOBJ
void writeOBJ(const bool writeSorted, Ostream &) const
Write to Ostream in OBJ (Lightwave) format.
Definition: writeOBJ.C:40
Foam::triSurface::writeDX
void writeDX(const bool, Ostream &) const
For DX writing.
Definition: writeDX.C:125
Foam::triSurface::readOBJ
bool readOBJ(const fileName &)
Definition: readOBJ.C:39
Foam::triSurface::writeVTK
void writeVTK(const bool writeSorted, Ostream &) const
Write to VTK legacy format.
Definition: writeVTK.C:35
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:74
Foam::triSurface::clearPatchMeshAddr
void clearPatchMeshAddr()
Clear patch addressing.
Definition: triSurface.C:751
Foam::triSurface::patches
const geometricSurfacePatchList & patches() const
Definition: triSurface.H:306
Foam::triSurface::readGTS
bool readGTS(const fileName &)
Definition: readGTS.C:39
Foam::triSurface::readTRI
bool readTRI(const fileName &)
Definition: readTRI.C:47
Foam::Xfer
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
Foam::triSurface::~triSurface
~triSurface()
Definition: triSurface.C:735
Foam::triSurface::patches
geometricSurfacePatchList & patches()
Definition: triSurface.H:311
Foam::triSurface::movePoints
virtual void movePoints(const pointField &)
Move points.
Definition: triSurface.C:789
Foam::triSurface::writeSTLASCII
void writeSTLASCII(Ostream &) const
Write to Ostream in ASCII STL format.
Definition: writeSTL.C:39
Foam::triSurface::cleanup
void cleanup(const bool verbose=false)
Remove non-valid triangles.
Definition: triSurface.C:820
Foam::triSurface::subsetMesh
triSurface subsetMesh(const boolList &include, labelList &pointMap, labelList &faceMap) const
Return new surface. Returns pointMap, faceMap from.
Definition: triSurface.C:1013
Foam::label
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
Foam::Field
Pre-declare SubField and related Field type.
Definition: Field.H:57
PrimitivePatch.H
Foam::triSurface
Triangulated surface description with patch information.
Definition: triSurface.H:57
Foam::faceZone
A subset of mesh faces organised as a primitive patch.
Definition: faceZone.H:64
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::triSurface::readSTLASCII
bool readSTLASCII(const fileName &)
Foam::triSurface::edgeOwnerPtr_
labelList * edgeOwnerPtr_
Label of face that 'owns' edge (i.e. e.vec() is righthanded walk.
Definition: triSurface.H:93
Foam::triSurface::Face
labelledTri Face
Typedefs for convenience.
Definition: triSurface.H:64
Foam::triSurface::writeAC
void writeAC(Ostream &) const
Write to Ostream in AC3D format. Always sorted by patch.
Definition: writeAC.C:38
Foam::triSurface::markZone
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:837
Foam::triSurface::operator=
void operator=(const triSurface &)
Definition: triSurface.C:1122
Foam::triSurface::triSurface
triSurface()
Construct null.
Definition: triSurface.C:608
Foam::triSurface::storedPoints
pointField & storedPoints()
Non-const access to global points.
Definition: triSurface.H:212
Foam::triSurface::writeDXGeometry
void writeDXGeometry(const bool, Ostream &) const
Definition: writeDX.C:41
Foam::triSurface::ParentType
PrimitivePatch< labelledTri, ::Foam::List, pointField, point > ParentType
Definition: triSurface.H:72
Foam::triSurface::calcSortedEdgeFaces
void calcSortedEdgeFaces() const
Calculate sorted edgeFaces.
Definition: triSurfaceAddressing.C:42
Foam::triSurface::readNAS
bool readNAS(const fileName &)
Definition: readNAS.C:72
Foam::triSurface::storedFaces
List< Face > & storedFaces()
Non-const access to the faces.
Definition: triSurface.H:218
Foam::triSurface::ParentType
PrimitivePatch< labelledTri, Foam::List, pointField, point > ParentType
Definition: triSurface.H:73
Foam::triSurface::calcEdgeOwner
void calcEdgeOwner() const
Calculate owner.
Definition: triSurfaceAddressing.C:129
Foam::triSurface::clearTopology
void clearTopology()
Clear topology.
Definition: triSurface.C:743
Foam::triSurface::readOFF
bool readOFF(const fileName &)
Definition: readOFF.C:40
Foam::triSurface::writeDXTrailer
void writeDXTrailer(Ostream &) const
Definition: writeDX.C:112
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::triSurface::readAC
bool readAC(const fileName &)
Definition: readAC.C:124
Foam::triSurface::edgeOwner
const labelList & edgeOwner() const
If 2 face neighbours: label of face where ordering of edge.
Definition: triSurface.C:777
Foam::triSurface::calcPatches
surfacePatchList calcPatches(labelList &faceMap) const
Sort faces according to region. Returns patch list.
Definition: triSurface.C:501
pointField.H
Foam::triSurface::setDefaultPatches
void setDefaultPatches()
Sets default values for patches.
Definition: triSurface.C:587
Foam::triSurface::markZones
label markZones(const boolList &borderEdge, labelList &faceZone) const
(size and) fills faceZone with zone of face. Zone is area
Definition: triSurface.C:906
Foam::triSurface::readSTL
bool readSTL(const fileName &)
Definition: readSTL.C:36
Foam::triSurface::BoundaryMesh
bool BoundaryMesh
Placeholder only, but do not remove - it is needed for GeoMesh.
Definition: triSurface.H:229
Foam::triSurface::checkEdges
void checkEdges(const bool verbose=false)
Check triply (or more) connected edges.
Definition: triSurface.C:320
Foam::triSurface::scalePoints
virtual void scalePoints(const scalar &)
Scale points. A non-positive factor is ignored.
Definition: triSurface.C:803
Foam::triSurface::patches_
geometricSurfacePatchList patches_
Patch information (face ordering nFaces/startFace only used.
Definition: triSurface.H:83
Foam::Vector< scalar >
Foam::List
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
Foam::triSurface::operator<<
friend Ostream & operator<<(Ostream &, const triSurface &)
Foam::triSurface::stitchTriangles
bool stitchTriangles(const pointField &rawPoints, const scalar tol=SMALL, const bool verbose=false)
Function to stitch the triangles by removing duplicate points.
Definition: stitchTriangles.C:38
Foam::triSurface::sortedEdgeFacesPtr_
labelListList * sortedEdgeFacesPtr_
Edge-face addressing (sorted)
Definition: triSurface.H:89
Foam::triSurface::writeOFF
void writeOFF(const bool writeSorted, Ostream &) const
Write to Ostream in OFF (Geomview) format.
Definition: writeOFF.C:35
Foam::labelledTri
Triangle with additional region number.
Definition: labelledTri.H:49
Foam::triSurface::writeSTLBINARY
void writeSTLBINARY(std::ostream &) const
Write to std::ostream in BINARY STL format.
Definition: writeSTL.C:95
Foam::triSurface::readVTK
bool readVTK(const fileName &)
Definition: readVTK.C:36
Foam::triSurface::sortedEdgeFaces
const labelListList & sortedEdgeFaces() const
Return edge-face addressing sorted (for edges with more than.
Definition: triSurface.C:766
Foam::sort
void sort(UList< T > &)
Definition: UList.C:107
labelledTri.H
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
triFaceList.H
Foam::triSurface::write
void write(const fileName &, const word &ext, const bool sort) const
Generic write routine. Chooses writer based on extension.
Definition: triSurface.C:433
Foam::triSurface::writeTRI
void writeTRI(const bool writeSorted, Ostream &) const
Write to Ostream in TRI (AC3D) format.
Definition: writeTRI.C:36
Foam::triSurface::getLineNoComment
static string getLineNoComment(IFstream &)
Read non-comment line.
Definition: triSurface.C:172
Foam::triSurface::triSurfInstance
static fileName triSurfInstance(const Time &)
Name of triSurface directory to use.
Definition: triSurface.C:43
Foam::triSurface::printTriangle
static void printTriangle(Ostream &, const Foam::string &pre, const labelledTri &, const pointField &)
Helper function to print triangle info.
Definition: triSurface.C:156
Foam::triSurface::writeSMESH
void writeSMESH(const bool writeSorted, Ostream &) const
Write to Ostream in SMESH (tetgen) format.
Definition: writeSMESH.C:35
Foam::PrimitivePatch
A list of faces which address into the list of points.
Definition: PrimitivePatchTemplate.H:88