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' cell (used to be node, delete it): 1
74  WasEdgeFirst = 2, //< front, 'B' cell (used to be link, keep it): 2
75  WasCell = 3, //< 'C' cell (used to be cell, keep it): 3
76  WasNodeAfter = -1, //< not in front, 'A' cell: -1
77  WasEdgeAfter = -2, //< not in front, 'B' cell: -2
78  Unassigned = 0 //< not assigned a value 0
79  };
80 
82  static bool ElementIsSeed(const Mesh2D& mesh,
83  const std::vector<int>& nodeTypes,
84  const UInt element);
85 
87  static UInt FindElementSeedIndex(const Mesh2D& mesh,
88  const std::vector<int>& nodeTypes);
89 
91  static void FindDirectlyConnectedCells(const Mesh2D& mesh,
92  const UInt elementId,
93  std::vector<UInt>& connected);
94 
96  static void FindIndirectlyConnectedCells(const Mesh2D& mesh,
97  const UInt elementId,
98  const std::vector<UInt>& directlyConnected,
99  std::vector<UInt>& indirectlyConnected);
100 
102  static void FindAdjacentCells(const Mesh2D& mesh,
103  const std::vector<UInt>& directlyConnected,
104  const std::vector<UInt>& indirectlyConnected,
105  std::vector<std::array<int, 2>>& kne);
106 
108  static void FindSurroundingCells(const Mesh2D& mesh,
109  const UInt elementId,
110  std::vector<UInt>& directlyConnected,
111  std::vector<UInt>& indirectlyConnected,
112  std::vector<std::array<int, 2>>& kne);
113 
115  static std::vector<ElementType> InitialiseElementType(const Mesh2D& mesh,
116  const std::vector<int>& nodeTypes);
117 
119  static bool ElementCannotBeDeleted(const Mesh2D& mesh,
120  const std::vector<int>& nodeTypes,
121  const Polygons& polygon,
122  const UInt elementId);
123 
125  static Point ComputeNewNodeCoordinates(const Mesh2D& mesh,
126  const std::vector<int>& nodeTypes,
127  const UInt nodeId);
128 
130  [[nodiscard]] static bool UpdateDirectlyConnectedElements(Mesh2D& mesh,
131  const UInt elementId,
132  const std::vector<UInt>& directlyConnected,
133  const std::vector<std::array<int, 2>>& kne);
134 
136  static bool UpdateDirectlyConnectedTriangleElements(Mesh2D& mesh,
137  const UInt index,
138  const UInt connectedElementId,
139  const std::vector<std::array<int, 2>>& kne);
140 
144  static void UpdateDirectlyConnectedNonTriangleElements(Mesh2D& mesh,
145  const UInt index,
146  const UInt elementId,
147  const UInt connectedElementId);
148 
150  static int GetNodeCode(const Mesh2D& mesh,
151  const std::vector<int>& nodeTypes,
152  const UInt elementId);
153 
157  static void AddElementToList(const Mesh& mesh,
158  const std::vector<UInt>& frontList,
159  std::vector<UInt>& frontListCopy,
160  const UInt elementId);
161 
163  static void RedirectNodesOfConnectedElements(Mesh2D& mesh,
164  const UInt elementId,
165  const UInt nodeId,
166  const std::vector<UInt>& indirectlyConnected);
167 
169  [[nodiscard]] static bool RemoveUnwantedBoundaryNodes(Mesh2D& mesh,
170  const std::vector<int>& nodeTypes,
171  const Polygons& polygon,
172  const std::vector<UInt>& indirectlyConnected);
173 
175  [[nodiscard]] static bool DeleteElement(Mesh2D& mesh,
176  std::vector<int>& nodeTypes,
177  const Polygons& polygon,
178  const UInt elementId,
179  const std::vector<UInt>& directlyConnected,
180  const std::vector<UInt>& indirectlyConnected,
181  const std::vector<std::array<int, 2>>& kne);
182 
186  [[nodiscard]] static bool CleanUpEdge(Mesh2D& mesh, const UInt edgeId);
187 
189  [[nodiscard]] static bool DoDeRefinement(Mesh2D& mesh, const Polygons& polygon);
190 
194  static std::vector<int> ComputeNodeTypes(const Mesh2D& mesh, const Polygons& polygon);
195  };
196 
197 } // 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.
Definition: CasulliDeRefinement.hpp:40