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-2019 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  randomPurturbation 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  mutable vector2D edges[maxNvert+1];
198 
199 
200  // Private Member Functions
201 
202  //- Insert point and return it's index
203  inline label insertPoint
204  (
205  const point2D& pt,
206  const label type
207  );
208 
209  //- Insert point and return it's index
210  inline label insertPoint
211  (
212  const point2D& pt,
213  const label index,
214  const label type
215  );
216 
217  inline label insertPoint
218  (
219  const Point& p,
220  const label index,
221  const label type
222  );
223 
224  inline bool insertMirrorPoint
225  (
226  const point2D& nearSurfPt,
227  const point2D& surfPt
228  );
229 
230  //- Insert a point-pair at a distance ppDist either side of
231  // surface point point surfPt in the direction n
232  inline void insertPointPair
233  (
234  const scalar mirrorDist,
235  const point2D& surfPt,
236  const vector2D& n
237  );
238 
239  //- Create the initial mesh from the bounding-box
240  void insertBoundingBox();
241 
242  //- Check if a point is within a line.
243  bool on2DLine(const point2D& p, const linePointRef& line);
244 
245  //- Insert point groups at the feature points.
246  void insertFeaturePoints();
247 
248  //- Re-insert point groups at the feature points.
249  void reinsertFeaturePoints();
250 
251  //- Insert point-pairs at the given set of points using the surface
252  // normals corresponding to the given set of surface triangles
253  // and write the inserted point locations to the given file.
254  void insertPointPairs
255  (
256  const DynamicList<point2D>& nearSurfacePoints,
257  const DynamicList<point2D>& surfacePoints,
258  const DynamicList<label>& surfaceTris,
259  const DynamicList<label>& surfaceHits,
260  const fileName fName
261  );
262 
263  //- Check to see if dual cell specified by given vertex iterator
264  // intersects the boundary and hence reqires a point-pair.
265  bool dualCellSurfaceIntersection
266  (
267  const Triangulation::Finite_vertices_iterator& vit
268  ) const;
269 
270  //- Insert point-pairs at the nearest points on the surface to the
271  // control vertex of dual-cells which intersect the boundary in order
272  // to provide a boundary-layer mesh.
273  // NB: This is not guaranteed to close the boundary
274  void insertSurfaceNearestPointPairs();
275 
276  //- Insert point-pairs at small dual-cell edges on the surface in order
277  // to improve the boundary-layer mesh generated by
278  // insertSurfaceNearestPointPairs.
279  void insertSurfaceNearPointPairs();
280 
281  //- Insert point-pair and correcting the Finite_vertices_iterator
282  // to account for the additional vertices
283  void insertPointPair
284  (
285  Triangulation::Finite_vertices_iterator& vit,
286  const point2D& p,
287  const label trii,
288  const label hitSurface
289  );
290 
291  //- Insert point-pair at the best intersection point between the lines
292  // from the dual-cell real centroid and it's vertices and the surface.
293  bool insertPointPairAtIntersection
294  (
295  Triangulation::Finite_vertices_iterator& vit,
296  const point2D& defVert,
297  const point2D vertices[],
298  const scalar maxProtSize
299  );
300 
301  //- Insert point-pairs corresponding to dual-cells which intersect
302  // the boundary surface
303  label insertBoundaryConformPointPairs(const fileName& fName);
304 
305  void markNearBoundaryPoints();
306 
307  //- Restore the Delaunay constraint
308  void fast_restore_Delaunay(Vertex_handle vh);
309 
310  // Flip operations used by fast_restore_Delaunay
311  void external_flip(Face_handle& f, int i);
312  bool internal_flip(Face_handle& f, int i);
313 
314  //- Write all the faces and all the triangles at a particular stage.
315  void write(const word& stage) const;
316 
317 
318 public:
319 
320  //- Runtime type information
321  ClassName("CV2D");
322 
323 
324  // Constructors
325 
326  //- Construct for given surface
327  CV2D(const Time& runTime, const dictionary& controlDict);
328 
329  //- Disallow default bitwise copy construction
330  CV2D(const CV2D&) = delete;
331 
332 
333  //- Destructor
334  ~CV2D();
335 
336 
337  // Member Functions
338 
339  // Access
340 
341  inline const cv2DControls& meshControls() const;
342 
343 
344  // Conversion functions between point2D, point and Point
345 
346  inline const point2D& toPoint2D(const point&) const;
347  inline const point2DField toPoint2D(const pointField&) const;
348  inline point toPoint3D(const point2D&) const;
349 
350  #ifdef CGAL_INEXACT
351  typedef const point2D& point2DFromPoint;
352  typedef const Point& PointFromPoint2D;
353  #else
354  typedef point2D point2DFromPoint;
355  typedef Point PointFromPoint2D;
356  #endif
357 
358  inline point2DFromPoint toPoint2D(const Point&) const;
359  inline PointFromPoint2D toPoint(const point2D&) const;
360  inline point toPoint3D(const Point&) const;
361 
362 
363  // Point insertion
364 
365  //- Create the initial mesh from the given internal points.
366  // Points must be inside the boundary by at least nearness
367  // otherwise they are ignored.
368  void insertPoints
369  (
370  const point2DField& points,
371  const scalar nearness
372  );
373 
374  //- Create the initial mesh from the internal points in the given
375  // file. Points outside the geometry are ignored.
376  void insertPoints(const fileName& pointFileName);
377 
378  //- Create the initial mesh as a regular grid of points.
379  // Points outside the geometry are ignored.
380  void insertGrid();
381 
382  //- Insert all surface point-pairs from
383  // insertSurfaceNearestPointPairs and
384  // findIntersectionForOutsideCentroid
386 
387  //- Insert point-pairs where there are protrusions into
388  // or out of the surface
389  void boundaryConform();
390 
391 
392  // Point removal
393 
394  //- Remove the point-pairs introduced by insertSurfacePointPairs
395  // and boundaryConform
397 
398 
399  // Point motion
400 
401  inline void movePoint(const Vertex_handle& vh, const Point& P);
402 
403  //- Move the internal points to the given new locations and update
404  // the triangulation to ensure it is Delaunay
405  // void moveInternalPoints(const point2DField& newPoints);
406 
407  //- Calculate the displacements to create the new points
408  void newPoints();
409 
410  //- Extract patch names and sizes.
411  void extractPatches
412  (
414  labelList& patchSizes,
415  EdgeMap<label>& mapEdgesRegion,
416  EdgeMap<label>& indirectPatchEdge
417  ) const;
418 
419 
420  // Write
421 
422  //- Write internal points to .obj file
423  void writePoints(const fileName& fName, bool internalOnly) const;
424 
425  //- Write triangles as .obj file
426  void writeTriangles(const fileName& fName, bool internalOnly) const;
427 
428  //- Write dual faces as .obj file
429  void writeFaces(const fileName& fName, bool internalOnly) const;
430 
431  //- Calculates dual points (circumcentres of tets) and faces
432  // (point-cell walk of tets).
433  // Returns:
434  // - dualPoints (in triangle ordering)
435  // - dualFaces (compacted)
436  void calcDual
437  (
438  point2DField& dualPoints,
439  faceList& dualFaces,
441  labelList& patchSizes,
442  EdgeMap<label>& mapEdgesRegion,
443  EdgeMap<label>& indirectPatchEdge
444  ) const;
445 
446  //- Write patch
447  void writePatch(const fileName& fName) const;
448 
449  void write() const;
450 
451 
452  // Member Operators
453 
454  //- Disallow default bitwise assignment
455  void operator=(const CV2D&) = delete;
456 };
457 
458 
459 inline bool boundaryTriangle(const CV2D::Face_handle fc);
460 inline bool outsideTriangle(const CV2D::Face_handle fc);
461 
462 
463 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
464 
465 } // End namespace Foam
466 
467 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
468 
469 #include "CV2DI.H"
470 
471 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
472 
473 #endif
474 
475 // ************************************************************************* //
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:350
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:351
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.