Loading [MathJax]/extensions/tex2jax.js
MeshKernel
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages Concepts
MeshTriangulation.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 <memory>
32#include <span>
33#include <vector>
34
35#include "MeshKernel/Constants.hpp"
36#include "MeshKernel/Definitions.hpp"
37#include "MeshKernel/Entities.hpp"
38#include "MeshKernel/Exceptions.hpp"
39#include "MeshKernel/Operations.hpp"
40#include "MeshKernel/Point.hpp"
41#include "MeshKernel/Utilities/RTreeFactory.hpp"
42
43namespace meshkernel
44{
45
47 template <const UInt Dimension>
49 {
50 public:
52 UInt size() const
53 {
54 return m_size;
55 }
56
58 void push_back(const UInt index)
59 {
60 if (m_size == Dimension - 1)
61 {
62 throw ConstraintError("array already at size limit: {}", Dimension);
63 }
64
65 m_indices[m_size] = index;
66 ++m_size;
67 }
68
70 UInt operator[](const UInt index) const
71 {
72 return m_indices[index];
73 }
74
76 UInt& operator[](const UInt index)
77 {
78 return m_indices[index];
79 }
80
82 std::array<UInt, Dimension>::const_iterator begin() const
83 {
84 return m_indices.begin();
85 }
86
88 std::array<UInt, Dimension>::const_iterator end() const
89 {
90 return m_indices.begin() + m_size;
91 }
92
94 bool contains(const UInt index) const
95 {
96 return std::find(begin(), end(), index) != end();
97 }
98
99 private:
101 std::array<UInt, Dimension> m_indices;
102
104 UInt m_size = 0;
105 };
106
112 {
113 public:
115 MeshTriangulation(const std::span<const Point> nodes,
116 const Projection projection);
117
119 MeshTriangulation(const std::span<const double> xNodes,
120 const std::span<const double> yNodes,
121 const Projection projection);
122
125
127 UInt NumberOfNodes() const;
128
130 UInt NumberOfEdges() const;
131
133 UInt NumberOfFaces() const;
134
136 Point GetNode(const UInt nodeId) const;
137
139 Edge GetEdge(const UInt nodeId) const;
140
142 std::array<Point, 3> GetNodes(const UInt faceId) const;
143
145 std::array<UInt, 3> GetNodeIds(const UInt faceId) const;
146
148 std::array<UInt, 3> GetEdgeIds(const UInt faceId) const;
149
153 const std::array<UInt, 2>& GetFaceIds(const UInt edgeId) const;
154
156 UInt FindNearestFace(const Point& pnt) const;
157
159 bool PointIsInElement(const Point& pnt, const UInt faceId) const;
160
161 private:
162 static constexpr UInt MaximumNumberOfEdgesPerNode = 16;
163
165 void Compute(const std::span<const double>& xNodes,
166 const std::span<const double>& yNodes);
167
168 std::vector<Point> m_nodes;
169 std::vector<UInt> m_faceNodes;
170 std::vector<UInt> m_edgeNodes;
171 std::vector<UInt> m_faceEdges;
172 std::vector<std::array<UInt, 2>> m_edgesFaces;
173 std::vector<std::vector<UInt>> m_nodesEdges;
174
175 std::vector<Point> m_elementCentres;
176
177 UInt m_numEdges{0};
178 UInt m_numFaces{0};
179
180 Projection m_projection = Projection::cartesian;
181 std::unique_ptr<RTreeBase> m_elementCentreRTree;
182 std::unique_ptr<RTreeBase> m_nodeRTree;
183 };
184
185} // namespace meshkernel
186
188{
189 return m_projection;
190}
191
193{
194 return static_cast<UInt>(m_nodes.size());
195}
196
198{
199 return m_numEdges;
200}
201
203{
204 return m_numFaces;
205}
206
208{
209 if (nodeId == constants::missing::uintValue)
210 {
211 throw ConstraintError("Invalid node id");
212 }
213
214 if (nodeId >= NumberOfNodes())
215 {
216 throw ConstraintError("node id out of range: {} >= {}", nodeId, NumberOfNodes());
217 }
218
219 return m_nodes[nodeId];
220}
221
223{
224 if (edgeId == constants::missing::uintValue)
225 {
226 throw ConstraintError("Invalid edge id");
227 }
228
229 if (edgeId >= m_numEdges)
230 {
231 throw ConstraintError("edge id out of range: {} >= {}", edgeId, m_numEdges);
232 }
233
234 return {m_edgeNodes[2 * edgeId], m_edgeNodes[2 * edgeId + 1]};
235}
236
237inline const std::array<meshkernel::UInt, 2>& meshkernel::MeshTriangulation::GetFaceIds(const UInt edgeId) const
238{
239 if (edgeId == constants::missing::uintValue)
240 {
241 throw ConstraintError("Invalid edge id");
242 }
243
244 if (edgeId >= m_numEdges)
245 {
246 throw ConstraintError("edge id out of range: {} >= {}", edgeId, m_numEdges);
247 }
248
249 return m_edgesFaces[edgeId];
250}
A simple bounded stack.
Definition MeshTriangulation.hpp:49
std::array< UInt, Dimension >::const_iterator begin() const
The iterator at the start of the array.
Definition MeshTriangulation.hpp:82
UInt & operator[](const UInt index)
Get the element at the position.
Definition MeshTriangulation.hpp:76
bool contains(const UInt index) const
Does the array contain the element value or not.
Definition MeshTriangulation.hpp:94
UInt size() const
Number of elements in the array.
Definition MeshTriangulation.hpp:52
void push_back(const UInt index)
Add an element to the end of the array.
Definition MeshTriangulation.hpp:58
UInt operator[](const UInt index) const
Get the element at the position.
Definition MeshTriangulation.hpp:70
std::array< UInt, Dimension >::const_iterator end() const
The iterator at the end of the array.
Definition MeshTriangulation.hpp:88
An exception class thrown when an attempt is made that violates a range constraint.
Definition Exceptions.hpp:267
Contains a mesh triangulated from a set of points.
Definition MeshTriangulation.hpp:112
const std::array< UInt, 2 > & GetFaceIds(const UInt edgeId) const
Get the id's of faces either side of the edge.
Definition MeshTriangulation.hpp:237
UInt NumberOfFaces() const
Get the number of faces/elements in the triangulation.
Definition MeshTriangulation.hpp:202
bool PointIsInElement(const Point &pnt, const UInt faceId) const
Determine if the point lies within the element.
std::array< Point, 3 > GetNodes(const UInt faceId) const
Get the node values of the element.
std::array< UInt, 3 > GetEdgeIds(const UInt faceId) const
Get the edge id's of the element.
Point GetNode(const UInt nodeId) const
Get node as the position.
Definition MeshTriangulation.hpp:207
MeshTriangulation(const std::span< const Point > nodes, const Projection projection)
Constructor with array of points.
UInt NumberOfNodes() const
Get the number of nodes in the triangulation.
Definition MeshTriangulation.hpp:192
MeshTriangulation(const std::span< const double > xNodes, const std::span< const double > yNodes, const Projection projection)
Constructor with separate arrays of x- and y-coordinates.
Edge GetEdge(const UInt nodeId) const
Get edge as the position.
Definition MeshTriangulation.hpp:222
Projection GetProjection() const
Get the projection type used in the triangulation.
Definition MeshTriangulation.hpp:187
UInt FindNearestFace(const Point &pnt) const
Find the nearest face to the point.
UInt NumberOfEdges() const
Get the number of edges in the triangulation.
Definition MeshTriangulation.hpp:197
std::array< UInt, 3 > GetNodeIds(const UInt faceId) const
Get the node id's of the element.
A struct describing a point in a two-dimensional space.
Definition Point.hpp:41
Contains the logic of the C++ static library.
Definition AveragingInterpolation.hpp:37
std::pair< UInt, UInt > Edge
Describes an edge with two indices.
Definition Entities.hpp:50
Projection
Enumerator describing the supported projections.
Definition Definitions.hpp:43
std::uint32_t UInt
Integer type used when indexing mesh graph entities.
Definition Definitions.hpp:39