MeshKernel
CasulliDeRefinement.hpp
1 //---- GPL ---------------------------------------------------------------------
2 //
3 // Copyright (C) Stichting Deltares, 2011-2024.
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 
30 #include <array>
31 #include <vector>
32 
33 #include "MeshKernel/Mesh2D.hpp"
34 #include "MeshKernel/Polygons.hpp"
35 #include "MeshKernel/UndoActions/UndoAction.hpp"
36 
37 namespace meshkernel
38 {
41  {
42  public:
46  [[nodiscard]] static std::unique_ptr<UndoAction> Compute(Mesh2D& mesh);
47 
52  [[nodiscard]] static std::unique_ptr<UndoAction> Compute(Mesh2D& mesh, const Polygons& polygon);
53 
57  static std::vector<Point> ElementsToDelete(const Mesh2D& mesh, const Polygons& polygon);
58 
59  private:
61  static constexpr UInt maxIterationCount = 1000;
62 
64  static constexpr UInt maximumSize = 1000;
65 
71  enum class ElementType
72  {
73  WasNodeFirst = 1, //< front, 'A' face (used to be node, delete it): 1
74  WasEdgeFirst = 2, //< front, 'B' face (used to be edge, keep it): 2
75  WasFace = 3, //< 'C' face (used to be face, keep it): 3
76  WasNodeAfter = -1, //< not in front, 'A' face: -1
77  WasEdgeAfter = -2, //< not in front, 'B' face: -2
78  Unassigned = 0 //< not assigned a value 0
79  };
80 
82  enum class ResultIndicator
83  {
84  ReturnFromFunction, //< Return (false) from the current function
85  BreakInnerLoop, //< Break the inner loop
86  ContinueInnerLoop //< Continue in the inner loop
87  };
88 
90  static bool ElementIsSeed(const Mesh2D& mesh,
91  const std::vector<int>& nodeTypes,
92  const UInt element);
93 
95  static UInt FindElementSeedIndex(const Mesh2D& mesh,
96  const std::vector<int>& nodeTypes);
97 
99  static void FindDirectlyConnectedFaces(const Mesh2D& mesh,
100  const UInt elementId,
101  std::vector<UInt>& connected);
102 
104  static void FindIndirectlyConnectedFaces(const Mesh2D& mesh,
105  const UInt elementId,
106  const std::vector<UInt>& directlyConnected,
107  std::vector<UInt>& indirectlyConnected);
108 
110  static void AssignDirectlyConnected(const std::vector<UInt>& directlyConnected,
111  std::array<int, 2>& edgeFaces,
112  UInt& neighbouringElementId);
113 
115  static void AssignIndirectlyConnected(const std::vector<UInt>& indirectlyConnected,
116  std::array<int, 2>& edgeFaces,
117  const UInt neighbouringElementId);
118 
120  static void FindAdjacentFaces(const Mesh2D& mesh,
121  const std::vector<UInt>& directlyConnected,
122  const std::vector<UInt>& indirectlyConnected,
123  std::vector<std::array<int, 2>>& edgeFaces);
124 
126  static void FindSurroundingFaces(const Mesh2D& mesh,
127  const UInt elementId,
128  std::vector<UInt>& directlyConnected,
129  std::vector<UInt>& indirectlyConnected,
130  std::vector<std::array<int, 2>>& edgeFaces);
131 
133  static void UpdateFaceMaskDirectlyConnectedNodeFirst(const std::vector<UInt>& directlyConnected,
134  const Mesh2D& mesh,
135  const std::vector<UInt>& frontIndex,
136  std::vector<UInt>& frontIndexCopy,
137  std::vector<ElementType>& faceMask);
138 
140  static void UpdateFaceMaskIndirectlyConnectedNodeFirst(const std::vector<UInt>& directlyConnected,
141  const Mesh2D& mesh,
142  std::vector<ElementType>& faceMask);
143 
145  static void UpdateFaceMaskDirectlyConnectedEdgeFirst(const std::vector<UInt>& directlyConnected,
146  const Mesh2D& mesh,
147  const std::vector<UInt>& frontIndex,
148  std::vector<UInt>& frontIndexCopy,
149  std::vector<ElementType>& faceMask);
150 
152  static void UpdateFaceMaskIndirectlyConnectedEdgeFirst(const std::vector<UInt>& indirectlyConnected,
153  const Mesh2D& mesh,
154  const std::vector<UInt>& frontIndex,
155  std::vector<UInt>& frontIndexCopy,
156  std::vector<ElementType>& faceMask);
157 
159  static std::vector<ElementType> InitialiseElementType(const Mesh2D& mesh,
160  const std::vector<int>& nodeTypes);
161 
163  static bool ElementCannotBeDeleted(const Mesh2D& mesh,
164  const std::vector<int>& nodeTypes,
165  const Polygons& polygon,
166  const UInt elementId);
167 
169  static Point ComputeNewNodeCoordinates(const Mesh2D& mesh,
170  const std::vector<int>& nodeTypes,
171  const UInt nodeId);
172 
174  static UInt GetElementIndex(const std::array<int, 2>& edgeFaces,
175  const UInt index);
176 
178  static std::tuple<UInt, UInt> FindCommonEdge(Mesh2D& mesh,
179  const UInt leftElementId,
180  const UInt rightElementId,
181  const UInt connectedElementId);
182 
184  [[nodiscard]] static bool UpdateDirectlyConnectedElements(Mesh2D& mesh,
185  const UInt elementId,
186  const std::vector<UInt>& directlyConnected,
187  const std::vector<std::array<int, 2>>& edgeFaces);
188 
190  static bool UpdateDirectlyConnectedTriangleElements(Mesh2D& mesh,
191  const UInt index,
192  const UInt connectedElementId,
193  const std::vector<std::array<int, 2>>& edgeFaces);
194 
198  static void UpdateDirectlyConnectedNonTriangleElements(Mesh2D& mesh,
199  const UInt index,
200  const UInt elementId,
201  const UInt connectedElementId);
202 
204  static int GetNodeCode(const Mesh2D& mesh,
205  const std::vector<int>& nodeTypes,
206  const UInt elementId);
207 
211  static void AddElementToList(const Mesh& mesh,
212  const std::vector<UInt>& frontList,
213  std::vector<UInt>& frontListCopy,
214  const UInt elementId);
215 
217  static void RedirectNodesOfConnectedElements(Mesh2D& mesh,
218  const UInt elementId,
219  const UInt nodeId,
220  const std::vector<UInt>& indirectlyConnected);
221 
223  static ResultIndicator RemoveBoundaryNodeAndEdge(Mesh2D& mesh,
224  const Polygons& polygon,
225  const std::vector<int>& nodeTypes,
226  const UInt faceNodeIndex,
227  const UInt connectedElementId,
228  const UInt edgeId,
229  const UInt previousEdgeId,
230  const UInt nodeId);
231 
233  [[nodiscard]] static bool RemoveUnwantedBoundaryNodes(Mesh2D& mesh,
234  const std::vector<int>& nodeTypes,
235  const Polygons& polygon,
236  const std::vector<UInt>& indirectlyConnected);
237 
239  [[nodiscard]] static bool DeleteElement(Mesh2D& mesh,
240  std::vector<int>& nodeTypes,
241  const Polygons& polygon,
242  const UInt elementId,
243  const std::vector<UInt>& directlyConnected,
244  const std::vector<UInt>& indirectlyConnected,
245  const std::vector<std::array<int, 2>>& edgeFaces);
246 
250  [[nodiscard]] static bool CleanUpEdge(Mesh2D& mesh, const UInt edgeId);
251 
253  [[nodiscard]] static bool DoDeRefinement(Mesh2D& mesh, const Polygons& polygon);
254 
258  static std::vector<int> ComputeNodeTypes(const Mesh2D& mesh, const Polygons& polygon);
259  };
260 
261 } // namespace meshkernel
meshkernel::CasulliDeRefinement::ElementsToDelete
static std::vector< Point > ElementsToDelete(const Mesh2D &mesh, const Polygons &polygon)
Compute centres of elements to be deleted.
meshkernel::Mesh2D
A class derived from Mesh, which describes unstructures 2d meshes.
Definition: Mesh2D.hpp:55
meshkernel::Point
A struct describing a point in a two-dimensional space.
Definition: Point.hpp:40
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::Polygons
A class containing a list of polygonaly enclosed regions.
Definition: Polygons.hpp:44
meshkernel::CasulliDeRefinement::Compute
static std::unique_ptr< UndoAction > Compute(Mesh2D &mesh)
Compute the Casulli de-refinement for the entire mesh.
meshkernel::Mesh
A class describing an unstructured mesh. This class contains the shared functionality between 1d or 2...
Definition: Mesh.hpp:98
meshkernel::CasulliDeRefinement
Compute the Casulli de-refinement for a mesh (derefine_mesh).
Definition: CasulliDeRefinement.hpp:40