Loading [MathJax]/jax/input/TeX/config.js
MeshKernel
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages Concepts
Mesh2D.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#include <array>
30#include <ranges>
31#include <utility>
32#include <vector>
33
34#include <MeshKernel/Constants.hpp>
35#include <MeshKernel/Definitions.hpp>
36#include <MeshKernel/Entities.hpp>
37#include <MeshKernel/Mesh.hpp>
38#include <MeshKernel/Polygon.hpp>
39#include <MeshKernel/UndoActions/CompoundUndoAction.hpp>
40#include <MeshKernel/UndoActions/SphericalCoordinatesOffsetAction.hpp>
41#include <MeshKernel/UndoActions/UndoAction.hpp>
42
45namespace meshkernel
46{
47 // Forward declarations
48 class CurvilinearGrid;
49 class Polygons;
50 class GeometryList;
51
57 class Mesh2D final : public Mesh
58 {
59 public:
62
65 {
66 InsideNotIntersected = 0,
67 InsideAndIntersected = 1,
68 FacesWithIncludedCircumcenters = 2
69 };
70
72 enum class Property
73 {
74 Orthogonality = 0,
75 EdgeLength = 1
76 };
77
79 ~Mesh2D() override = default;
80
83
86 explicit Mesh2D(Projection projection);
87
92 Mesh2D(const std::vector<Edge>& edges,
93 const std::vector<Point>& nodes,
94 Projection projection);
95
102 Mesh2D(const std::vector<Edge>& edges,
103 const std::vector<Point>& nodes,
104 const std::vector<std::vector<UInt>>& faceNodes,
105 const std::vector<std::uint8_t>& numFaceNodes,
106 Projection projection);
107
112 Mesh2D(const std::vector<Point>& nodes, const Polygons& polygons, Projection projection);
113
115 void Administrate(CompoundUndoAction* undoAction = nullptr) override;
116
118 void ComputeCircumcentersMassCentersAndFaceAreas(bool computeMassCenters = false);
119
121 void FindFaces();
122
126 void FindFacesGivenFaceNodesMapping(const std::vector<std::vector<UInt>>& faceNodes,
127 const std::vector<std::uint8_t>& numFaceNodes);
128
132 [[nodiscard]] std::unique_ptr<SphericalCoordinatesOffsetAction> OffsetSphericalCoordinates(double minx, double maxx);
133
135 void CommitAction(const SphericalCoordinatesOffsetAction& undoAction);
136
140 void RestoreAction(const SphericalCoordinatesOffsetAction& undoAction);
141
148 std::vector<Point>& polygonNodesCache,
149 std::vector<UInt>& localNodeIndicesCache,
150 std::vector<UInt>& globalEdgeIndicesCache) const;
151
156 [[nodiscard]] Point ComputeFaceCircumenter(std::vector<Point>& polygon,
157 const std::vector<UInt>& edgesNumFaces) const;
158
161 [[nodiscard]] std::vector<Point> GetObtuseTrianglesCenters();
162
166 [[nodiscard]] std::vector<UInt> GetEdgesCrossingSmallFlowEdges(double smallFlowEdgesThreshold);
167
171 [[nodiscard]] std::vector<Point> GetFlowEdgesCenters(const std::vector<UInt>& edges) const;
172
195 [[nodiscard]] std::unique_ptr<meshkernel::UndoAction> DeleteSmallFlowEdges(double smallFlowEdgesThreshold);
196
214 [[nodiscard]] std::unique_ptr<UndoAction> DeleteSmallTrianglesAtBoundaries(double minFractionalAreaTriangles);
215
217 void ComputeNodeNeighbours(std::vector<std::vector<UInt>>& nodesNodes, UInt& maxNumNeighbours) const;
218
221 void ComputeAspectRatios(std::vector<double>& aspectRatios) const;
222
225
227 MeshNodeType GetNodeType(const UInt nodeId) const { return m_nodesTypes[nodeId]; }
228
230 void GetNodeTypes(std::vector<MeshNodeType>& nodeTypes) const { nodeTypes = m_nodesTypes; }
231
233 [[nodiscard]] std::unique_ptr<UndoAction> DeleteDegeneratedTriangles();
234
236 [[nodiscard]] std::unique_ptr<UndoAction> TriangulateFaces();
237
239 [[nodiscard]] std::unique_ptr<UndoAction> TriangulateFaces(const Polygons& polygon);
240
246 void MakeDualFace(const std::span<const Point> edgeCentres,
247 UInt node,
248 double enlargementFactor,
249 std::vector<Point>& dualFace) const;
250
254 [[nodiscard]] std::vector<UInt> SortedFacesAroundNode(UInt node) const;
255
259 [[nodiscard]] std::vector<Point> ComputeBoundaryPolygons(const std::vector<Point>& polygon);
260
266 void WalkBoundaryFromNode(const Polygon& polygon,
267 std::vector<bool>& isVisited,
268 UInt& currentNode,
269 std::vector<Point>& meshBoundaryPolygon) const;
270
273 [[nodiscard]] std::vector<UInt> GetHangingEdges() const;
274
276 [[nodiscard]] std::unique_ptr<UndoAction> DeleteHangingEdges();
277
281 [[nodiscard]] std::vector<UInt> PointFaceIndices(const std::vector<Point>& points);
282
288 [[nodiscard]] std::unique_ptr<UndoAction> DeleteMesh(const Polygons& polygon, DeleteMeshOptions deletionOption, bool invertDeletion);
289
297 [[nodiscard]] std::vector<bool> FilterBasedOnMetric(Location location, Property property, double minValue, double maxValue) const;
298
303 [[nodiscard]] std::tuple<UInt, UInt> IsSegmentCrossingABoundaryEdge(const Point& firstPoint, const Point& secondPoint) const;
304
310 [[nodiscard]] std::vector<int> MaskEdgesOfFacesInPolygon(const Polygons& polygons, bool invertSelection, bool includeIntersected) const;
311
315 [[nodiscard]] std::vector<int> NodeMaskFromEdgeMask(std::vector<int> const& edgeMask) const;
316
321 [[nodiscard]] std::vector<int> NodeMaskFromPolygon(const Polygons& polygons, bool inside) const;
322
326 UInt FindOppositeEdge(const UInt faceId, const UInt edgeId) const;
327
332 UInt NextFace(const UInt faceId, const UInt edgeId) const;
333
339 static std::unique_ptr<Mesh2D> Merge(const Mesh2D& mesh1, const Mesh2D& mesh2);
340
342 static std::unique_ptr<Mesh2D> Merge(const std::span<const Point>& mesh1Nodes,
343 const std::span<const Edge>& mesh1Edges,
344 const std::span<const Point>& mesh2Nodes,
345 const std::span<const Edge>& mesh2Edges,
346 const Projection projection);
347
351 [[nodiscard]] BoundingBox GetBoundingBox() const;
352
356 [[nodiscard]] std::vector<BoundingBox> GetEdgesBoundingBoxes() const;
357
362 void FindFacesConnectedToNode(UInt nodeIndex, std::vector<UInt>& sharedFaces) const;
363
368 void GetConnectingNodes(UInt nodeIndex, std::vector<UInt>& connectedNodes) const;
369
376 void FindNodesSharedByFaces(UInt nodeIndex, const std::vector<UInt>& sharedFaces, std::vector<UInt>& connectedNodes, std::vector<std::vector<UInt>>& faceNodeMapping) const;
377
382 UInt IsStartOrEnd(const UInt edgeId, const UInt nodeId) const;
383
388 UInt IsLeftOrRight(const UInt elementId, const UInt edgeId) const;
389
393 UInt FindCommonFace(const UInt edge1, const UInt edge2) const;
394
395 private:
396 // orthogonalization
397 static constexpr double m_minimumEdgeLength = 1e-4;
398 static constexpr double m_curvilinearToOrthogonalRatio = 0.5;
399 static constexpr double m_minimumCellArea = 1e-12;
400 static constexpr UInt m_maximumNumberOfHangingNodesAlongEdge = 5;
401
403 using HangingNodeIndexArray = std::array<UInt, m_maximumNumberOfHangingNodesAlongEdge>;
404
406 void ComputeAverageAreOfNeighbouringFaces(const UInt faceId, UInt& numNonBoundaryFaces, double& averageOtherFacesArea) const;
407
409 void FindSmallestCornerAngle(const UInt faceId,
410 double& minCosPhiSmallTriangle,
411 UInt& nodeToPreserve,
412 UInt& firstNodeToMerge,
413 UInt& secondNodeToMerge,
414 UInt& thirdEdgeSmallTriangle) const;
415
417 void DeleteSmallTriangle(const UInt nodeToPreserve,
418 const UInt firstNodeToMerge,
419 const UInt secondNodeToMerge,
420 bool& nodesMerged,
421 CompoundUndoAction& undoAction);
422
424 void FindNodesToDelete(const Polygons& polygon,
425 const bool invertDeletion,
426 std::vector<bool>& isNodeInsidePolygon,
427 std::vector<bool>& deleteNode) const;
428
430 void DeletedMeshNodesAndEdges(const std::function<bool(UInt)>& excludedFace,
431 std::vector<bool>& deleteNode,
432 CompoundUndoAction& deleteMeshAction);
433
435 std::vector<int> ComputeNodeMask(const Polygons& polygons) const;
436
438 std::vector<int> ComputeEdgeMask(const std::vector<int>& nodeMask,
439 bool includeIntersected) const;
440
442 void RemoveIntersected(const std::vector<int>& edgeMask,
443 std::vector<int>& secondEdgeMask) const;
444
446 void InvertSelection(const std::vector<int>& edgeMask,
447 std::vector<int>& secondEdgeMask) const;
448
450 std::vector<bool> FindFacesEntirelyInsidePolygon(const std::vector<bool>& isNodeInsidePolygon) const;
451
456 [[nodiscard]] std::unique_ptr<UndoAction> DeleteMeshFaces(const Polygons& polygon, bool invertDeletion);
457
468 void FindFacesRecursive(UInt startNode,
469 UInt node,
470 UInt previousEdge,
471 UInt numClosingEdges,
472 std::vector<UInt>& edges,
473 std::vector<UInt>& nodes,
474 std::vector<UInt>& sortedEdges,
475 std::vector<UInt>& sortedNodes,
476 std::vector<Point>& nodalValues);
477
482 [[nodiscard]] bool HasTriangleNoAcuteAngles(const std::vector<UInt>& faceNodes, const std::vector<Point>& nodes) const;
483
487 bool HasDuplicateNodes(const UInt numClosingEdges, const std::vector<UInt>& node, std::vector<UInt>& sortedNodes) const;
488
492 bool HasDuplicateEdgeFaces(const UInt numClosingEdges, const std::vector<UInt>& edges, std::vector<UInt>& sortedEdgesFaces) const;
493
495 void ResizeAndInitializeFaceVectors();
496
500 void DoAdministrationGivenFaceNodesMapping(const std::vector<std::vector<UInt>>& faceNodes,
501 const std::vector<std::uint8_t>& numFaceNodes);
502
505 void DoAdministration(CompoundUndoAction* undoAction = nullptr);
506
508 void InitialiseBoundaryNodeClassification(std::vector<int>& intNodeType) const;
509
511 MeshNodeType ClassifyNode(const UInt nodeId) const;
512
513 // /// @brief Count the number of valid edges in list
514 // UInt CountNumberOfValidEdges(const std::vector<UInt>& edgesNumFaces, const UInt numNodes) const;
515
517 void ComputeMidPointsAndNormals(const std::vector<Point>& polygon,
518 const std::vector<UInt>& edgesNumFaces,
519 const UInt numNodes,
520 std::array<Point, constants::geometric::maximumNumberOfNodesPerFace>& middlePoints,
521 std::array<Point, constants::geometric::maximumNumberOfNodesPerFace>& normals,
522 UInt& pointCount) const;
523
525 Point ComputeCircumCentre(const Point& centerOfMass,
526 const UInt pointCount,
527 const std::array<Point, constants::geometric::maximumNumberOfNodesPerFace>& middlePoints,
528 const std::array<Point, constants::geometric::maximumNumberOfNodesPerFace>& normals) const;
529
531 void ComputeAverageFlowEdgesLength(std::vector<double>& edgesLength,
532 std::vector<double>& averageFlowEdgesLength) const;
533
535 void ComputeAverageEdgeLength(const std::vector<double>& edgesLength,
536 const std::vector<double>& averageFlowEdgesLength,
537 std::vector<bool>& curvilinearGridIndicator,
538 std::vector<std::array<double, 2>>& averageEdgesLength,
539 std::vector<double>& aspectRatios) const;
540
543 UInt InvalidateEdgesWithNoFace();
544
545 std::vector<MeshNodeType> m_nodesTypes;
546 };
547
548} // namespace meshkernel
A class defining a bounding box.
Definition BoundingBox.hpp:40
A class derived from Mesh, which describes unstructures 2d meshes.
Definition Mesh2D.hpp:58
Mesh2D()
Default constructor.
Mesh2D(const std::vector< Edge > &edges, const std::vector< Point > &nodes, const std::vector< std::vector< UInt > > &faceNodes, const std::vector< std::uint8_t > &numFaceNodes, Projection projection)
Construct a mesh2d from face nodes and num face nodes.
~Mesh2D() override=default
Default destructor.
std::vector< int > NodeMaskFromEdgeMask(std::vector< int > const &edgeMask) const
From the edge mask compute the node mask.
static std::unique_ptr< Mesh2D > Merge(const std::span< const Point > &mesh1Nodes, const std::span< const Edge > &mesh1Edges, const std::span< const Point > &mesh2Nodes, const std::span< const Edge > &mesh2Edges, const Projection projection)
Merges mesh node and edge connectivity into a single mesh.
std::unique_ptr< meshkernel::UndoAction > DeleteSmallFlowEdges(double smallFlowEdgesThreshold)
Deletes small flow edges (removesmallflowlinks, part 1)
void ComputeFaceClosedPolygonWithLocalMappings(UInt faceIndex, std::vector< Point > &polygonNodesCache, std::vector< UInt > &localNodeIndicesCache, std::vector< UInt > &globalEdgeIndicesCache) const
For a face create a closed polygon and fill local mapping caches (get_cellpolygon)
UInt NextFace(const UInt faceId, const UInt edgeId) const
Get the next face adjacent to the edge on the opposite side.
MeshNodeType GetNodeType(const UInt nodeId) const
Get the node type.
Definition Mesh2D.hpp:227
DeleteMeshOptions
Enumerator describing the different options to delete a mesh.
Definition Mesh2D.hpp:65
Point ComputeFaceCircumenter(std::vector< Point > &polygon, const std::vector< UInt > &edgesNumFaces) const
For a closed polygon, compute the circumcenter of a face (getcircumcenter)
std::vector< BoundingBox > GetEdgesBoundingBoxes() const
Get the bounding boxes of the mesh edges.
void FindNodesSharedByFaces(UInt nodeIndex, const std::vector< UInt > &sharedFaces, std::vector< UInt > &connectedNodes, std::vector< std::vector< UInt > > &faceNodeMapping) const
Find all unique nodes.
UInt FindOppositeEdge(const UInt faceId, const UInt edgeId) const
Find edge on the opposite side of the element.
std::vector< UInt > SortedFacesAroundNode(UInt node) const
Sorts the faces around a node, sorted in counter clock wise order.
std::vector< UInt > GetHangingEdges() const
Gets the hanging edges.
Property
Enumerator for different properties on a 2D mesh.
Definition Mesh2D.hpp:73
std::vector< int > MaskEdgesOfFacesInPolygon(const Polygons &polygons, bool invertSelection, bool includeIntersected) const
Masks the edges of all faces entirely included in all polygons.
void MakeDualFace(const std::span< const Point > edgeCentres, UInt node, double enlargementFactor, std::vector< Point > &dualFace) const
Make a dual face around the node, enlarged by a factor.
void FindFaces()
Constructs the face nodes mapping, face mass centers and areas.
void RestoreAction(const SphericalCoordinatesOffsetAction &undoAction)
Undo the coordinate offset action.
void CommitAction(const SphericalCoordinatesOffsetAction &undoAction)
Apply the coordinate offset action.
std::tuple< UInt, UInt > IsSegmentCrossingABoundaryEdge(const Point &firstPoint, const Point &secondPoint) const
Inquire if a segment is crossing a face.
void ComputeAspectRatios(std::vector< double > &aspectRatios) const
Gets the aspect ratios (the ratios edges lengths to flow edges lengths)
UInt FindCommonFace(const UInt edge1, const UInt edge2) const
Find the id of the element that is common to both edges.
std::unique_ptr< UndoAction > DeleteSmallTrianglesAtBoundaries(double minFractionalAreaTriangles)
Deletes small triangles at the boundaries (removesmallflowlinks, part 2)
void Administrate(CompoundUndoAction *undoAction=nullptr) override
Perform complete administration.
std::vector< Point > ComputeBoundaryPolygons(const std::vector< Point > &polygon)
Convert all mesh boundaries to a vector of polygon nodes, including holes (copynetboundstopol)
std::vector< UInt > PointFaceIndices(const std::vector< Point > &points)
For a collection of points, compute the face indices including them.
UInt IsStartOrEnd(const UInt edgeId, const UInt nodeId) const
Determine if the node is at the start or end of the edge.
std::vector< UInt > GetEdgesCrossingSmallFlowEdges(double smallFlowEdgesThreshold)
Gets the edges crossing the small flow edges.
void WalkBoundaryFromNode(const Polygon &polygon, std::vector< bool > &isVisited, UInt &currentNode, std::vector< Point > &meshBoundaryPolygon) const
Constructs a polygon from the meshboundary, by walking through the mesh.
std::unique_ptr< UndoAction > DeleteDegeneratedTriangles()
Deletes coinciding triangles.
Mesh2D(const std::vector< Point > &nodes, const Polygons &polygons, Projection projection)
Create triangular grid from nodes (triangulatesamplestonetwork)
void ComputeCircumcentersMassCentersAndFaceAreas(bool computeMassCenters=false)
Compute face circumcenters.
std::vector< Point > GetFlowEdgesCenters(const std::vector< UInt > &edges) const
Gets the flow edges centers from the crossing edges.
Mesh2D(Projection projection)
Construct a mesh2d using only the projection.
static std::unique_ptr< Mesh2D > Merge(const Mesh2D &mesh1, const Mesh2D &mesh2)
Merges mesh connectivity.
std::unique_ptr< UndoAction > DeleteMesh(const Polygons &polygon, DeleteMeshOptions deletionOption, bool invertDeletion)
Deletes a mesh in a polygon, using several options (delnet)
void GetConnectingNodes(UInt nodeIndex, std::vector< UInt > &connectedNodes) const
Get indices of all nodes that are connected directly to a give node along connected edges.
Mesh2D(const std::vector< Edge > &edges, const std::vector< Point > &nodes, Projection projection)
Construct a mesh2d starting from the edges and nodes.
std::vector< int > NodeMaskFromPolygon(const Polygons &polygons, bool inside) const
Mask all nodes included in all polygons.
UInt IsLeftOrRight(const UInt elementId, const UInt edgeId) const
Determine if the element lies on the left or right side of the edge.
std::vector< Point > GetObtuseTrianglesCenters()
Gets the mass centers of obtuse triangles.
void ComputeNodeNeighbours(std::vector< std::vector< UInt > > &nodesNodes, UInt &maxNumNeighbours) const
Computes node neighbours.
BoundingBox GetBoundingBox() const
Get the mesh bounding box.
std::unique_ptr< UndoAction > TriangulateFaces(const Polygons &polygon)
Transform non-triangular faces inside a polygon to triangular faces.
void ClassifyNodes()
Classifies the nodes (makenetnodescoding)
std::vector< bool > FilterBasedOnMetric(Location location, Property property, double minValue, double maxValue) const
This method generates a mask indicating which locations are within the specified range of the given m...
void FindFacesConnectedToNode(UInt nodeIndex, std::vector< UInt > &sharedFaces) const
Find all faces that have the given node as a vertex.
void GetNodeTypes(std::vector< MeshNodeType > &nodeTypes) const
Get the node type.
Definition Mesh2D.hpp:230
std::unique_ptr< SphericalCoordinatesOffsetAction > OffsetSphericalCoordinates(double minx, double maxx)
Offset the x coordinates if m_projection is spherical.
std::unique_ptr< UndoAction > DeleteHangingEdges()
Deletes the hanging edges.
void FindFacesGivenFaceNodesMapping(const std::vector< std::vector< UInt > > &faceNodes, const std::vector< std::uint8_t > &numFaceNodes)
Find remaining face information given the face nodes mapping.
std::unique_ptr< UndoAction > TriangulateFaces()
Transform non-triangular faces to triangular faces.
A class describing an unstructured mesh. This class contains the shared functionality between 1d or 2...
Definition Mesh.hpp:99
void RestoreAction(const ResetNodeAction &undoAction)
Undo the reset node action.
void CommitAction(const ResetNodeAction &undoAction)
Apply the reset node action.
A struct describing a point in a two-dimensional space.
Definition Point.hpp:41
A closed polygon.
Definition Polygon.hpp:46
A class containing a list of polygonaly enclosed regions.
Definition Polygons.hpp:45
Contains the logic of the C++ static library.
Definition AveragingInterpolation.hpp:37
Projection
Enumerator describing the supported projections.
Definition Definitions.hpp:43
MeshNodeType
Possible unstructured node types.
Definition Definitions.hpp:148
Location
Mesh locations enumeration.
Definition Definitions.hpp:76
std::uint32_t UInt
Integer type used when indexing mesh graph entities.
Definition Definitions.hpp:39