MeshKernel
ConnectMeshes.hpp
1 //---- GPL ---------------------------------------------------------------------
2 //
3 // Copyright (C) Stichting Deltares, 2011-2023.
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation version 3.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // contact: delft3d.support@deltares.nl
18 // Stichting Deltares
19 // P.O. Box 177
20 // 2600 MH Delft, The Netherlands
21 //
22 // All indications and logos of, and references to, "Delft3D" and "Deltares"
23 // are registered trademarks of Stichting Deltares, and remain the property of
24 // Stichting Deltares. All rights reserved.
25 //
26 //------------------------------------------------------------------------------
27 
28 #pragma once
29 #include <array>
30 #include <utility>
31 #include <vector>
32 
33 #include "MeshKernel/Definitions.hpp"
34 #include "MeshKernel/Mesh2D.hpp"
35 #include "MeshKernel/Point.hpp"
36 
37 namespace meshkernel
38 {
39 
44  // Functionality re-implemented from connectcurvilinearquadsddtype.f90
45  class ConnectMeshes final
46  {
47  public:
49  static constexpr double DefaultMaximumSeparationFraction = 0.4;
50 
54  static constexpr double minimumParallelDeviation = 0.9;
55 
62  [[nodiscard]] static std::unique_ptr<meshkernel::UndoAction> Compute(Mesh2D& mesh, const double separationFraction = DefaultMaximumSeparationFraction);
63 
64  private:
66  static constexpr UInt m_maximumNumberOfIrregularElementsAlongEdge = 5;
67 
69  enum class MergeIndicator
70  {
71  Initial,
72  DoMerge,
73  DoNotMerge
74  };
75 
77  using BoundedIntegerArray = std::array<UInt, m_maximumNumberOfIrregularElementsAlongEdge>;
78 
80  using NodesToMerge = std::pair<UInt, UInt>;
81 
83  struct IrregularEdgeInfo
84  {
86  BoundedIntegerArray hangingNodes{constants::missing::uintValue,
87  constants::missing::uintValue,
88  constants::missing::uintValue,
89  constants::missing::uintValue,
90  constants::missing::uintValue};
92  UInt edgeCount = 0;
94  UInt startNode = constants::missing::uintValue;
96  UInt endNode = constants::missing::uintValue;
97  };
98 
100  using IrregularEdgeInfoArray = std::vector<IrregularEdgeInfo>;
101 
111  static void AreEdgesAdjacent(const Mesh2D& mesh,
112  const double separationFraction,
113  const UInt edge1,
114  const UInt edge2,
115  bool& areAdjacent,
116  UInt& startNode,
117  UInt& endNode);
118 
124  static void GetQuadrilateralElementsOnDomainBoundary(const Mesh2D& mesh,
125  std::vector<UInt>& elementsOnDomainBoundary,
126  std::vector<UInt>& edgesOnDomainBoundary);
127 
135  static void GetOrderedDistanceFromPoint(const Mesh2D& mesh,
136  const std::vector<UInt>& nodeIndices,
137  const UInt numberOfNodes,
138  const Point& point,
139  BoundedIntegerArray& nearestNeighbours);
140 
146  static std::unique_ptr<meshkernel::UndoAction> MergeNodes(Mesh2D& mesh, const std::vector<NodesToMerge>& nodesToMerge, std::vector<MergeIndicator>& mergeIndicator);
147 
154  [[nodiscard]] static std::unique_ptr<meshkernel::UndoAction> FreeOneHangingNode(Mesh2D& mesh,
155  const BoundedIntegerArray& hangingNodes,
156  const UInt startNode,
157  const UInt endNode);
158 
167  [[nodiscard]] static std::unique_ptr<meshkernel::UndoAction> FreeTwoHangingNodes(Mesh2D& mesh,
168  const UInt faceId,
169  const UInt edgeId,
170  const BoundedIntegerArray& hangingNodes,
171  const UInt startNode,
172  const UInt endNode);
173 
182  [[nodiscard]] static std::unique_ptr<meshkernel::UndoAction> FreeThreeHangingNodes(Mesh2D& mesh,
183  const UInt faceId,
184  const UInt edgeId,
185  const BoundedIntegerArray& hangingNodes,
186  const UInt startNode,
187  const UInt endNode);
188 
197  [[nodiscard]] static std::unique_ptr<meshkernel::UndoAction> FreeFourHangingNodes(Mesh2D& mesh,
198  const UInt faceId,
199  const UInt edgeId,
200  const BoundedIntegerArray& hangingNodes,
201  const UInt startNode,
202  const UInt endNode);
203 
213  [[nodiscard]] static std::unique_ptr<meshkernel::UndoAction> FreeHangingNodes(Mesh2D& mesh,
214  const UInt numberOfHangingNodes,
215  const std::vector<UInt>& hangingNodesOnEdge,
216  const UInt faceId,
217  const Edge& boundaryEdge,
218  const Point& boundaryNode,
219  const UInt edgeId);
220 
227  static void GatherHangingNodeIds(const Mesh2D& mesh,
228  const double separationFraction,
229  const std::vector<UInt>& edgesOnDomainBoundary,
230  IrregularEdgeInfoArray& irregularEdges);
231 
241  static void GatherNodesToMerge(const UInt startNode,
242  const UInt endNode,
243  const Edge& boundaryEdge,
244  std::vector<NodesToMerge>& nodesToMerge,
245  std::vector<MergeIndicator>& mergeIndicator);
246 
255  static void GatherHangingNodes(const UInt primaryStartNode,
256  const UInt primaryEndNode,
257  const Edge& irregularEdge,
258  std::vector<UInt>& hangingNodesOnEdge,
259  UInt& numberOfHangingNodes,
260  std::vector<MergeIndicator>& mergeIndicator);
261  };
262 
263 } // namespace meshkernel
meshkernel::ConnectMeshes
Connects grids across element faces.
Definition: ConnectMeshes.hpp:45
meshkernel::Mesh2D
A class derived from Mesh, which describes unstructures 2d meshes.
Definition: Mesh2D.hpp:55
meshkernel::ConnectMeshes::DefaultMaximumSeparationFraction
static constexpr double DefaultMaximumSeparationFraction
The default and maximum value of the fraction of the edge length used to determine if edges are adjac...
Definition: ConnectMeshes.hpp:49
meshkernel
Contains the logic of the C++ static library.
Definition: AveragingInterpolation.hpp:36
meshkernel::UInt
std::uint32_t UInt
Integer type used when indexing mesh graph entities.
Definition: Definitions.hpp:38
meshkernel::Edge
std::pair< UInt, UInt > Edge
Describes an edge with two indices.
Definition: Entities.hpp:50
meshkernel::ConnectMeshes::minimumParallelDeviation
static constexpr double minimumParallelDeviation
The cos of the minimum angle between two lines to be considered parallel or almost parallel.
Definition: ConnectMeshes.hpp:54
meshkernel::ConnectMeshes::Compute
static std::unique_ptr< meshkernel::UndoAction > Compute(Mesh2D &mesh, const double separationFraction=DefaultMaximumSeparationFraction)
Connect grids.