triSurface.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | foam-extend: Open Source CFD
4  \\ / O peration | Version: 3.2
5  \\ / A nd | Web: http://www.foam-extend.org
6  \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9  This file is part of foam-extend.
10 
11  foam-extend is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by the
13  Free Software Foundation, either version 3 of the License, or (at your
14  option) any later version.
15 
16  foam-extend is distributed in the hope that it will be useful, but
17  WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with foam-extend. 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 "PrimitivePatchTemplate.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, List, pointField, point>
61 {
62  // Private typedefs
63 
64  // Typedefs for convenience
65  typedef labelledTri Face;
66 
67  typedef PrimitivePatch
68  <
70  Foam::List,
71  pointField,
72  point
73  >
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)
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.
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 pointField& rawPoints,
117  const scalar tol = SMALL,
118  const bool verbose = false
119  );
120 
121  //- Read in Foam format
122  bool read(Istream&);
123 
124  //- Generic read routine. Chooses reader based on extension.
125  bool read(const fileName&, const word& ext, const bool check = true);
126 
127  bool readSTL(const fileName&);
128  bool readSTLASCII(const fileName&);
129  bool readSTLBINARY(const fileName&);
130  bool readGTS(const fileName&);
131  bool readOBJ(const fileName&);
132  bool readOFF(const fileName&);
133  bool readTRI(const fileName&);
134  bool readAC(const fileName&);
135  bool readNAS(const fileName&);
136 
137  //- Generic write routine. Chooses writer based on extension.
138  void write(const fileName&, const word& ext, const bool sort) const;
139 
140  //- Write to Ostream in ASCII STL format.
141  // Each region becomes 'solid' 'endsolid' block.
142  void writeSTLASCII(Ostream&) const;
143 
144  //- Write to std::ostream in BINARY STL format
145  void writeSTLBINARY(std::ostream&) const;
146 
147  //- Write to Ostream in GTS (Gnu Tri Surface library)
148  // format.
149  void writeGTS(const bool writeSorted, Ostream&) const;
150 
151  //- Write to Ostream in OBJ (Lightwave) format.
152  // writeSorted=true: sort faces acc. to region and write as single
153  // group. =false: write in normal order.
154  void writeOBJ(const bool writeSorted, Ostream&) const;
155 
156  //- Write to Ostream in OFF (Geomview) format.
157  // writeSorted=true: sort faces acc. to region and write as single
158  // group. =false: write in normal order.
159  void writeOFF(const bool writeSorted, Ostream&) const;
160 
161  //- Write to VTK legacy format.
162  void writeVTK(const bool writeSorted, Ostream&) const;
163 
164  //- Write to Ostream in TRI (AC3D) format
165  // Ac3d .tri format (unmerged triangle format)
166  void writeTRI(const bool writeSorted, Ostream&) const;
167 
168  //- Write to Ostream in SMESH (tetgen) format
169  void writeSMESH(const bool writeSorted, Ostream&) const;
170 
171  //- Write to Ostream in AC3D format. Always sorted by patch.
172  void writeAC(Ostream&) const;
173 
174  //- For DX writing.
175  void writeDX(const bool, Ostream&) const;
176  void writeDXGeometry(const bool, Ostream&) const;
177  void writeDXTrailer(Ostream&) const;
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, points. Set patchnames to default.
266  triSurface(const List<labelledTri>&, const pointField&);
267 
268  //- Construct from triangles, points. Set region to 0 and default
269  // patchName.
270  triSurface(const triFaceList&, const pointField&);
271 
272  //- Construct from file name (uses extension to determine type)
273  triSurface(const fileName&);
274 
275  //- Construct from Istream
277 
278  //- Construct from objectRegistry
279  triSurface(const Time& d);
280 
281  //- Construct as copy
282  triSurface(const triSurface&);
283 
284 
285  // Destructor
286  ~triSurface();
287 
288  //- Clear all data
289  void clearOut();
290 
291  //- Clear topology
292  void clearTopology();
293 
294  //- Clear patch addressing
295  void clearPatchMeshAddr();
296 
297 
298  // Member Functions
299 
300  // Access
301 
302  const geometricSurfacePatchList& patches() const
303  {
304  return patches_;
305  }
306 
308  {
309  return patches_;
310  }
311 
312  //- Return edge-face addressing sorted (for edges with more than
313  // 2 faces) according to the angle around the edge.
314  // Orientation is anticlockwise looking from
315  // edge.vec(localPoints())
316  const labelListList& sortedEdgeFaces() const;
317 
318  //- If 2 face neighbours: label of face where ordering of edge
319  // is consistent with righthand walk.
320  // If 1 neighbour: label of only face.
321  // If >2 neighbours: undetermined.
322  const labelList& edgeOwner() const;
323 
324 
325  // Edit
326 
327  //- Move points
328  virtual void movePoints(const pointField&);
329 
330  //- Scale points. A non-positive factor is ignored
331  virtual void scalePoints(const scalar&);
332 
333  //- Check/remove duplicate/degenerate triangles
334  void checkTriangles(const bool verbose = false);
335 
336  //- Check triply (or more) connected edges.
337  void checkEdges(const bool verbose = false);
338 
339  //- Remove non-valid triangles
340  void cleanup(const bool verbose = false);
341 
342  //- Fill faceZone with currentZone for every face reachable
343  // from faceI without crossing edge marked in borderEdge.
344  // Note: faceZone has to be sized nFaces before calling this fun.
345  void markZone
346  (
347  const boolList& borderEdge,
348  const label faceI,
349  const label currentZone,
351  ) const;
352 
353  //- (size and) fills faceZone with zone of face. Zone is area
354  // reachable by edge crossing without crossing borderEdge
355  // (bool for every edge in surface). Returns number of zones.
357  (
358  const boolList& borderEdge,
360  ) const;
361 
362  //- 'Create' sub mesh, including only faces for which
363  // boolList entry is true
364  // Sets: pointMap: from new to old localPoints
365  // faceMap: new to old faces
366  void subsetMeshMap
367  (
368  const boolList& include,
369  labelList& pointMap,
371  ) const;
372 
373  //- Return new surface. Returns pointMap, faceMap from
374  // subsetMeshMap
376  (
377  const boolList& include,
378  labelList& pointMap,
380  ) const;
381 
382 
383  // Write
384 
385  //- Write to Ostream in simple FOAM format
386  void write(Ostream&) const;
387 
388  //- Generic write routine. Chooses writer based on extension.
389  void write(const fileName&, const bool sortByRegion = false) const;
390 
391  //- Write to database
392  void write(const Time&) const;
393 
394  //- Write to Ostream in OpenDX format
395  void writeDX(const scalarField&, Ostream&) const;
396  void writeDX(const vectorField&, Ostream&) const;
397 
398  //- Write some statistics
399  void writeStats(Ostream&) const;
400 
401 
402  // Member operators
403 
404  void operator=(const triSurface&);
405 
406 
407  // Ostream Operator
408 
409  friend Ostream& operator<<(Ostream&, const triSurface&);
410 };
411 
412 
413 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
414 
415 } // End namespace Foam
416 
417 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
418 
419 #endif
420 
421 // ************************************************************************* //
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:301
Foam::triSurface::readGTS
bool readGTS(const fileName &)
Definition: readGTS.C:39
Foam::triSurface::readTRI
bool readTRI(const fileName &)
Definition: readTRI.C:47
Foam::triSurface::~triSurface
~triSurface()
Definition: triSurface.C:735
Foam::triSurface::patches
geometricSurfacePatchList & patches()
Definition: triSurface.H:306
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
PrimitivePatchTemplate.H
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
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
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::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::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