Loading [MathJax]/extensions/tex2jax.js
MeshKernel
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages Concepts
TriangulationWrapper.hpp
1//---- GPL ---------------------------------------------------------------------
2//
3// Copyright (C) Stichting Deltares, 2011-2021.
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 "MeshKernel/Constants.hpp"
31#include "MeshKernel/Entities.hpp"
32#include "MeshKernel/Exceptions.hpp"
33#include "MeshKernel/Point.hpp"
34#include "MeshKernel/PolygonalEnclosure.hpp"
35
36#include <concepts>
37
38namespace meshkernel
39{
40 extern "C"
41 {
45 void Triangulation(int jatri,
46 double const* const xs,
47 double const* const ys,
48 int ns,
49 int* const index,
50 int* const numtri,
51 int* const edgeidx,
52 int* const numedge,
53 int* const triedge,
54 double* const xs3,
55 double* const ys3,
56 int* const ns3,
57 double trisize);
58 }
59
60 class Sample;
61
66 {
74
81 template <std::derived_from<Point> T>
82 void Compute(const std::vector<T>& inputNodes,
83 TriangulationOptions triangulationOption,
84 double averageTriangleArea,
85 UInt estimatedNumberOfTriangles)
86 {
87 if (inputNodes.empty())
88 {
89 throw ConstraintError("The sample is empty.");
90 }
91 std::vector<double> xLocalPolygon(inputNodes.size());
92 std::vector<double> yLocalPolygon(inputNodes.size());
93 for (UInt i = 0; i < inputNodes.size(); ++i)
94 {
95 xLocalPolygon[i] = inputNodes[i].x;
96 yLocalPolygon[i] = inputNodes[i].y;
97 }
98
99 m_numFaces = -1;
100 m_numEdges = 0;
101 m_numNodes = 0;
102
103 int numInputNodes = static_cast<int>(inputNodes.size());
104 auto intTriangulationOption = static_cast<int>(triangulationOption);
105
106 if (estimatedNumberOfTriangles == 0)
107 {
108 estimatedNumberOfTriangles = static_cast<UInt>(inputNodes.size()) * 6 + 10;
109 }
110
111 // If the number of estimated triangles is not sufficient, triangulation must be repeated
112 while (m_numFaces < 0)
113 {
114 m_numFaces = static_cast<int>(estimatedNumberOfTriangles);
115
116 m_faceNodesFlat.resize(estimatedNumberOfTriangles * 3);
117 std::ranges::fill(m_faceNodesFlat, 0);
118
119 m_edgeNodesFlat.resize(estimatedNumberOfTriangles * 2);
120 std::ranges::fill(m_edgeNodesFlat, 0);
121
122 m_faceEdgesFlat.resize(estimatedNumberOfTriangles * 3);
123 std::ranges::fill(m_faceEdgesFlat, 0);
124
125 m_xCoordFlat.resize(estimatedNumberOfTriangles * 3, constants::missing::doubleValue);
126 std::ranges::fill(m_xCoordFlat, 0.0);
127
128 m_yCoordFlat.resize(estimatedNumberOfTriangles * 3, constants::missing::doubleValue);
129 std::ranges::fill(m_yCoordFlat, 0.0);
130
131 Triangulation(intTriangulationOption,
132 xLocalPolygon.data(),
133 yLocalPolygon.data(),
134 numInputNodes,
135 m_faceNodesFlat.data(), // INDEX
136 &m_numFaces,
137 m_edgeNodesFlat.data(), // EDGEINDEX
138 &m_numEdges,
139 m_faceEdgesFlat.data(), // TRIEDGE
140 m_xCoordFlat.data(),
141 m_yCoordFlat.data(),
142 &m_numNodes,
143 averageTriangleArea);
144 if (estimatedNumberOfTriangles > 0)
145 {
146 estimatedNumberOfTriangles = -m_numFaces;
147 }
148 }
149 }
150
152 std::vector<Point> SelectNodes(const PolygonalEnclosure& enclosure) const;
153
156
159 [[nodiscard]] int GetNumEdges() const
160 {
161 return m_numEdges;
162 }
163
166 [[nodiscard]] int GetNumNodes() const
167 {
168 return m_numNodes;
169 }
170
173 [[nodiscard]] int GetNumFaces() const
174 {
175 return m_numFaces;
176 }
177
180 [[nodiscard]] const std::vector<Point>& GetNodes() const
181 {
182 return m_nodes;
183 }
184
188 [[nodiscard]] const std::vector<UInt>& GetFaceNodes(const UInt faceIndex) const
189 {
190 return m_faceNodes[faceIndex];
191 }
192
197 [[nodiscard]] UInt GetFaceNode(const UInt faceIndex, const UInt nodeIndex) const
198 {
199 return m_faceNodes[faceIndex][nodeIndex];
200 }
201
206 [[nodiscard]] UInt GetFaceEdge(const UInt faceIndex, const UInt edgeIndex) const
207 {
208 return m_faceEdges[faceIndex][edgeIndex];
209 }
210
215 [[nodiscard]] UInt GetEdgeNode(const UInt edgeIndex, const UInt nodeIndex) const
216 {
217 return m_edgeNodes[edgeIndex][nodeIndex];
218 }
219
224 [[nodiscard]] UInt GetEdgeFace(const UInt edgeIndex, const UInt faceIndex) const
225 {
226 return m_edgesFaces[edgeIndex][faceIndex];
227 }
228
232 Point GetCoord(const UInt nodeIndex) const
233 {
234 return Point(m_xCoordFlat[nodeIndex], m_yCoordFlat[nodeIndex]);
235 }
236
237 private:
238 std::vector<int> m_faceNodesFlat;
239 std::vector<int> m_edgeNodesFlat;
240 std::vector<int> m_faceEdgesFlat;
241 std::vector<double> m_xCoordFlat;
242 std::vector<double> m_yCoordFlat;
243 int m_numNodes{0};
244 int m_numEdges{0};
245 int m_numFaces{0};
246
247 std::vector<Point> m_nodes;
248 std::vector<std::vector<UInt>> m_faceNodes;
249 std::vector<std::vector<UInt>> m_faceEdges;
250 std::vector<std::vector<UInt>> m_edgeNodes;
251 std::vector<std::vector<UInt>> m_edgesFaces;
252 };
253
254} // namespace meshkernel
An exception class thrown when an attempt is made that violates a range constraint.
Definition Exceptions.hpp:267
A struct describing a point in a two-dimensional space.
Definition Point.hpp:41
A region enclosed by a polygonal permieter.
Definition PolygonalEnclosure.hpp:46
Contains the logic of the C++ static library.
Definition AveragingInterpolation.hpp:37
void Triangulation(int jatri, double const *const xs, double const *const ys, int ns, int *const index, int *const numtri, int *const edgeidx, int *const numedge, int *const triedge, double *const xs3, double *const ys3, int *const ns3, double trisize)
Function of the Triangle library.
std::uint32_t UInt
Integer type used when indexing mesh graph entities.
Definition Definitions.hpp:39
Wrapper around the Triangle library.
Definition TriangulationWrapper.hpp:66
UInt GetEdgeNode(const UInt edgeIndex, const UInt nodeIndex) const
Retrieves the edge node.
Definition TriangulationWrapper.hpp:215
int GetNumNodes() const
Gets the number of triangulated nodes.
Definition TriangulationWrapper.hpp:166
int GetNumFaces() const
Gets the number of triangulated faces.
Definition TriangulationWrapper.hpp:173
const std::vector< UInt > & GetFaceNodes(const UInt faceIndex) const
Gets the nodes of a triangulated face.
Definition TriangulationWrapper.hpp:188
void Compute(const std::vector< T > &inputNodes, TriangulationOptions triangulationOption, double averageTriangleArea, UInt estimatedNumberOfTriangles)
Compute the triangulation.
Definition TriangulationWrapper.hpp:82
UInt GetFaceEdge(const UInt faceIndex, const UInt edgeIndex) const
Retrieves the face edge.
Definition TriangulationWrapper.hpp:206
TriangulationOptions
Enumerator describing all triangulation options.
Definition TriangulationWrapper.hpp:69
@ TriangulatePoints
generate Delaunay triangulation from input nodes
@ TriangulatePointsAndGenerateFaces
generate Delaunay triangulation from input nodes with m_faceEdges and m_edgeNodes
@ GeneratePoints
generate internal nodes in polygon that produce a Delaunay triangulation
void BuildTriangulation()
Build the internal triangulation from the flat triangulation.
UInt GetEdgeFace(const UInt edgeIndex, const UInt faceIndex) const
Retrieves the edge face.
Definition TriangulationWrapper.hpp:224
int GetNumEdges() const
Gets the number of triangulated edges.
Definition TriangulationWrapper.hpp:159
std::vector< Point > SelectNodes(const PolygonalEnclosure &enclosure) const
From the set of computed points, select those that are contained within the enclosure.
UInt GetFaceNode(const UInt faceIndex, const UInt nodeIndex) const
Retrieves the face node.
Definition TriangulationWrapper.hpp:197
Point GetCoord(const UInt nodeIndex) const
Retrieves the (x,y) coordinate of a triangulated node.
Definition TriangulationWrapper.hpp:232
const std::vector< Point > & GetNodes() const
Gets the triangulated nodes.
Definition TriangulationWrapper.hpp:180