CV2D.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) 2013-2020 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::CV2D
26 
27 Description
28  Conformal-Voronoi 2D automatic mesher with grid or read initial points
29  and point position relaxation with optional "squarification".
30 
31  There are a substantial number of options to this mesher read from
32  CV2DMesherDict file e.g.:
33 
34  // Min cell size used in tolerances when inserting points for
35  // boundary conforming.
36  // Also used to as the grid spacing usind in insertGrid.
37  minCellSize 0.05;
38 
39  // Feature angle used to inser feature points
40  // 0 = all features, 180 = no features
41  featureAngle 45;
42 
43  // Maximum quadrant angle allowed at a concave corner before
44  // additional "mitering" lines are added
45  maxQuadAngle 110;
46 
47  // Should the mesh be square-dominated or of unbiased hexagons
48  squares yes;
49 
50  // Near-wall region where cells are aligned with the wall specified as a
51  // number of cell layers
52  nearWallAlignedDist 3;
53 
54  // Chose if the cell orientation should relax during the iterations
55  // or remain fixed to the x-y directions
56  relaxOrientation no;
57 
58  // Insert near-boundary point mirror or point-pairs
59  insertSurfaceNearestPointPairs yes;
60 
61  // Mirror near-boundary points rather than insert point-pairs
62  mirrorPoints no;
63 
64  // Insert point-pairs vor dual-cell vertices very near the surface
65  insertSurfaceNearPointPairs yes;
66 
67  // Choose if to randomise the initial grid created by insertGrid.
68  randomiseInitialGrid yes;
69 
70  // Perturbation fraction, 1 = cell-size.
71  randomPerturbation 0.1;
72 
73  // Number of relaxation iterations.
74  nIterations 5;
75 
76  // Relaxation factor at the start of the iteration sequence.
77  // 0.5 is a sensible maximum and < 0.2 converges better.
78  relaxationFactorStart 0.8;
79 
80  // Relaxation factor at the end of the iteration sequence.
81  // Should be <= relaxationFactorStart
82  relaxationFactorEnd 0;
83 
84  writeInitialTriangulation no;
85  writeFeatureTriangulation no;
86  writeNearestTriangulation no;
87  writeInsertedPointPairs no;
88  writeFinalTriangulation yes;
89 
90  // Maximum number of iterations used in boundaryConform.
91  maxBoundaryConformingIter 5;
92 
93  minEdgeLenCoeff 0.5;
94  maxNotchLenCoeff 0.3;
95  minNearPointDistCoeff 0.25;
96  ppDistCoeff 0.05;
97 
98 SourceFiles
99  CGALTriangulation2Ddefs.H
100  indexedVertex.H
101  indexedFace.H
102  CV2DI.H
103  CV2D.C
104  CV2DIO.C
105  tolerances.C
106  controls.C
107  insertFeaturePoints.C
108  insertSurfaceNearestPointPairs.C
109  insertSurfaceNearPointPairs.C
110  insertBoundaryConformPointPairs.C
111 
112 \*---------------------------------------------------------------------------*/
113 
114 #ifndef CV2D_H
115 #define CV2D_H
117 #define CGAL_INEXACT
118 #define CGAL_HIERARCHY
119 
120 #include "CGALTriangulation2Ddefs.H"
121 #include "Time.H"
122 #include "point2DFieldFwd.H"
123 #include "dictionary.H"
124 #include "Switch.H"
125 #include "PackedBoolList.H"
126 #include "EdgeMap.H"
127 #include "cv2DControls.H"
128 #include "tolerances.H"
129 #include "meshTools.H"
130 #include "triSurface.H"
131 #include "searchableSurfaces.H"
132 #include "conformationSurfaces.H"
133 #include "relaxationModel.H"
135 
136 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
137 
138 namespace Foam
139 {
140 
141 /*---------------------------------------------------------------------------*\
142  Class CV2D Declaration
143 \*---------------------------------------------------------------------------*/
145 class CV2D
146 :
147  public Delaunay
148 {
149  //- Use the Foam::point in preference to the CGAL point
150  typedef Foam::point point;
151 
152  // Private Data
153 
154  //- The time registry of the application
155  const Time& runTime_;
156 
157  mutable Random rndGen_;
158 
159  //- The surface to mesh
160  // const querySurface& qSurf_;
161  //- All geometry of the meshing process, including surfaces to be
162  // conformed to and those to be used for refinement
163  searchableSurfaces allGeometry_;
164 
165  conformationSurfaces qSurf_;
166 
167  //- Meshing controls
168  cv2DControls controls_;
169 
170  //- The cell size control object
171  cellSizeAndAlignmentControls cellSizeControl_;
172 
173  //- Relaxation coefficient model. Runtime selectable.
174  autoPtr<relaxationModel> relaxationModel_;
175 
176  //- z-level
177  scalar z_;
178 
179  //- Keep track of the start of the internal points
180  label startOfInternalPoints_;
181 
182  //- Keep track of the start of the surface point-pairs
183  label startOfSurfacePointPairs_;
184 
185  //- Keep track of the boundary conform point-pairs
186  // stored after the insertion of the surface point-pairs in case
187  // the boundary conform function is called more than once without
188  // removing and inserting the surface point-pairs
189  label startOfBoundaryConformPointPairs_;
190 
191  //- Store the feature points
192  std::list<Vb> featurePoints_;
193 
194  //- Temporary storage for a dual-cell
195  static const label maxNvert = 20;
196  mutable point2D vertices[maxNvert+1];
197 
198 
199  // Private Member Functions
200 
201  //- Insert point and return it's index
202  inline label insertPoint
203  (
204  const point2D& pt,
205  const label type
206  );
207 
208  //- Insert point and return it's index
209  inline label insertPoint
210  (
211  const point2D& pt,
212  const label index,
213  const label type
214  );
215 
216  inline label insertPoint
217  (
218  const Point& p,
219  const label index,
220  const label type
221  );
222 
223  inline bool insertMirrorPoint
224  (
225  const point2D& nearSurfPt,
226  const point2D& surfPt
227  );
228 
229  //- Insert a point-pair at a distance ppDist either side of
230  // surface point point surfPt in the direction n
231  inline void insertPointPair
232  (
233  const scalar mirrorDist,
234  const point2D& surfPt,
235  const vector2D& n
236  );
237 
238  //- Create the initial mesh from the bounding-box
239  void insertBoundingBox();
240 
241  //- Check if a point is within a line.
242  bool on2DLine(const point2D& p, const linePointRef& line);
243 
244  //- Insert point groups at the feature points.
245  void insertFeaturePoints();
246 
247  //- Re-insert point groups at the feature points.
248  void reinsertFeaturePoints();
249 
250  //- Insert point-pairs at the given set of points using the surface
251  // normals corresponding to the given set of surface triangles
252  // and write the inserted point locations to the given file.
253  void insertPointPairs
254  (
255  const DynamicList<point2D>& nearSurfacePoints,
256  const DynamicList<point2D>& surfacePoints,
257  const DynamicList<label>& surfaceTris,
258  const DynamicList<label>& surfaceHits,
259  const fileName fName
260  );
261 
262  //- Check to see if dual cell specified by given vertex iterator
263  // intersects the boundary and hence reqires a point-pair.
264  bool dualCellSurfaceIntersection
265  (
266  const Triangulation::Finite_vertices_iterator& vit
267  ) const;
268 
269  //- Insert point-pairs at the nearest points on the surface to the
270  // control vertex of dual-cells which intersect the boundary in order
271  // to provide a boundary-layer mesh.
272  // NB: This is not guaranteed to close the boundary
273  void insertSurfaceNearestPointPairs();
274 
275  //- Insert point-pairs at small dual-cell edges on the surface in order
276  // to improve the boundary-layer mesh generated by
277  // insertSurfaceNearestPointPairs.
278  void insertSurfaceNearPointPairs();
279 
280  //- Insert point-pair and correcting the Finite_vertices_iterator
281  // to account for the additional vertices
282  void insertPointPair
283  (
284  Triangulation::Finite_vertices_iterator& vit,
285  const point2D& p,
286  const label trii,
287  const label hitSurface
288  );
289 
290  //- Insert point-pair at the best intersection point between the lines
291  // from the dual-cell real centroid and it's vertices and the surface.
292  bool insertPointPairAtIntersection
293  (
294  Triangulation::Finite_vertices_iterator& vit,
295  const point2D& defVert,
296  const point2D vertices[],
297  const scalar maxProtSize
298  );
299 
300  //- Insert point-pairs corresponding to dual-cells which intersect
301  // the boundary surface
302  label insertBoundaryConformPointPairs(const fileName& fName);
303 
304  void markNearBoundaryPoints();
305 
306  //- Restore the Delaunay constraint
307  void fast_restore_Delaunay(Vertex_handle vh);
308 
309  // Flip operations used by fast_restore_Delaunay
310  void external_flip(Face_handle& f, int i);
311  bool internal_flip(Face_handle& f, int i);
312 
313  //- Write all the faces and all the triangles at a particular stage.
314  void write(const word& stage) const;
315 
316 
317 public:
318 
319  //- Runtime type information
320  ClassName("CV2D");
321 
322 
323  // Constructors
324 
325  //- Construct for given surface
326  CV2D(const Time& runTime, const dictionary& controlDict);
327 
328  //- Disallow default bitwise copy construction
329  CV2D(const CV2D&) = delete;
330 
331 
332  //- Destructor
333  ~CV2D();
334 
335 
336  // Member Functions
337 
338  // Access
339 
340  inline const cv2DControls& meshControls() const;
341 
342 
343  // Conversion functions between point2D, point and Point
344 
345  inline const point2D& toPoint2D(const point&) const;
346  inline const point2DField toPoint2D(const pointField&) const;
347  inline point toPoint3D(const point2D&) const;
348 
349  #ifdef CGAL_INEXACT
350  typedef const point2D& point2DFromPoint;
351  typedef const Point& PointFromPoint2D;
352  #else
353  typedef point2D point2DFromPoint;
354  typedef Point PointFromPoint2D;
355  #endif
356 
357  inline point2DFromPoint toPoint2D(const Point&) const;
358  inline PointFromPoint2D toPoint(const point2D&) const;
359  inline point toPoint3D(const Point&) const;
360 
361 
362  // Point insertion
363 
364  //- Create the initial mesh from the given internal points.
365  // Points must be inside the boundary by at least nearness
366  // otherwise they are ignored.
367  void insertPoints
368  (
369  const point2DField& points,
370  const scalar nearness
371  );
372 
373  //- Create the initial mesh from the internal points in the given
374  // file. Points outside the geometry are ignored.
375  void insertPoints(const fileName& pointFileName);
376 
377  //- Create the initial mesh as a regular grid of points.
378  // Points outside the geometry are ignored.
379  void insertGrid();
380 
381  //- Insert all surface point-pairs from
382  // insertSurfaceNearestPointPairs and
383  // findIntersectionForOutsideCentroid
385 
386  //- Insert point-pairs where there are protrusions into
387  // or out of the surface
388  void boundaryConform();
389 
390 
391  // Point removal
392 
393  //- Remove the point-pairs introduced by insertSurfacePointPairs
394  // and boundaryConform
396 
397 
398  // Point motion
399 
400  inline void movePoint(const Vertex_handle& vh, const Point& P);
401 
402  //- Move the internal points to the given new locations and update
403  // the triangulation to ensure it is Delaunay
404  // void moveInternalPoints(const point2DField& newPoints);
405 
406  //- Calculate the displacements to create the new points
407  void newPoints();
408 
409  //- Extract patch names and sizes.
410  void extractPatches
411  (
413  labelList& patchSizes,
414  EdgeMap<label>& mapEdgesRegion,
415  EdgeMap<label>& indirectPatchEdge
416  ) const;
417 
418 
419  // Write
420 
421  //- Write internal points to .obj file
422  void writePoints(const fileName& fName, bool internalOnly) const;
423 
424  //- Write triangles as .obj file
425  void writeTriangles(const fileName& fName, bool internalOnly) const;
426 
427  //- Write dual faces as .obj file
428  void writeFaces(const fileName& fName, bool internalOnly) const;
429 
430  //- Calculates dual points (circumcentres of tets) and faces
431  // (point-cell walk of tets).
432  // Returns:
433  // - dualPoints (in triangle ordering)
434  // - dualFaces (compacted)
435  void calcDual
436  (
437  point2DField& dualPoints,
438  faceList& dualFaces,
440  labelList& patchSizes,
441  EdgeMap<label>& mapEdgesRegion,
442  EdgeMap<label>& indirectPatchEdge
443  ) const;
444 
445  //- Write patch
446  void writePatch(const fileName& fName) const;
447 
448  void write() const;
449 
450 
451  // Member Operators
452 
453  //- Disallow default bitwise assignment
454  void operator=(const CV2D&) = delete;
455 };
456 
457 
458 inline bool boundaryTriangle(const CV2D::Face_handle fc);
459 inline bool outsideTriangle(const CV2D::Face_handle fc);
460 
461 
462 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
463 
464 } // End namespace Foam
465 
466 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
467 
468 #include "CV2DI.H"
469 
470 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
471 
472 #endif
473 
474 // ************************************************************************* //
void operator=(const CV2D &)=delete
Disallow default bitwise assignment.
CGAL::Delaunay_triangulation_3< K, Tds, CompactLocator > Delaunay
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 line primitive.
Definition: line.H:56
A class for handling file names.
Definition: fileName.H:79
void writePatch(const fileName &fName) const
Write patch.
runTime controlDict().lookup("adjustTimeStep") >> adjustTimeStep
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:158
~CV2D()
Destructor.
void insertSurfacePointPairs()
Insert all surface point-pairs from.
engineTime & runTime
point toPoint3D(const point2D &) const
Definition: CV2DI.H:141
bool outsideTriangle(const CV2D::Face_handle fc)
Definition: CV2DI.H:216
const cv2DControls & meshControls() const
Definition: CV2DI.H:118
void newPoints()
Move the internal points to the given new locations and update.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
const point2D & toPoint2D(const point &) const
Definition: CV2DI.H:124
void insertGrid()
Create the initial mesh as a regular grid of points.
void writeFaces(const fileName &fName, bool internalOnly) const
Write dual faces as .obj file.
PointFromPoint2D toPoint(const point2D &) const
Definition: CV2DI.H:168
const pointField & points
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
Pre-declare SubField and related Field type.
Definition: Field.H:56
A class for handling words, derived from string.
Definition: word.H:59
wordList patchNames(nPatches)
Container for searchableSurfaces.
Random number generator.
Definition: Random.H:57
const point2D & point2DFromPoint
Definition: CV2D.H:349
void calcDual(point2DField &dualPoints, faceList &dualFaces, wordList &patchNames, labelList &patchSizes, EdgeMap< label > &mapEdgesRegion, EdgeMap< label > &indirectPatchEdge) const
Calculates dual points (circumcentres of tets) and faces.
void insertPoints(const point2DField &points, const scalar nearness)
Create the initial mesh from the given internal points.
void write() const
labelList f(nPoints)
Controls for the 2D CV mesh generator.
Definition: cv2DControls.H:58
void extractPatches(wordList &patchNames, labelList &patchSizes, EdgeMap< label > &mapEdgesRegion, EdgeMap< label > &indirectPatchEdge) const
Extract patch names and sizes.
void writeTriangles(const fileName &fName, bool internalOnly) const
Write triangles as .obj file.
ClassName("CV2D")
Runtime type information.
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
void movePoint(const Vertex_handle &vh, const Point &P)
Definition: CV2DI.H:182
label n
Conformal-Voronoi 2D automatic mesher with grid or read initial points and point position relaxation ...
Definition: CV2D.H:144
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
bool boundaryTriangle(const CV2D::Face_handle fc)
Definition: CV2DI.H:205
CGAL data structures used for 2D Delaunay meshing.
volScalarField & p
CV2D(const Time &runTime, const dictionary &controlDict)
Construct for given surface.
const Point & PointFromPoint2D
Definition: CV2D.H:350
void removeSurfacePointPairs()
Remove the point-pairs introduced by insertSurfacePointPairs.
void boundaryConform()
Insert point-pairs where there are protrusions into.
Namespace for OpenFOAM.
void writePoints(const fileName &fName, bool internalOnly) const
Write internal points to .obj file.