MeshKernel
Loading...
Searching...
No Matches
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/UndoActions/PointArrayUndo.hpp"
35#include <MeshKernel/Constants.hpp>
36#include <MeshKernel/Definitions.hpp>
37#include <MeshKernel/Entities.hpp>
38#include <MeshKernel/Mesh.hpp>
39#include <MeshKernel/Polygon.hpp>
40#include <MeshKernel/UndoActions/CompoundUndoAction.hpp>
41#include <MeshKernel/UndoActions/SphericalCoordinatesOffsetAction.hpp>
42#include <MeshKernel/UndoActions/UndoAction.hpp>
43
46namespace meshkernel
47{
48 // Forward declarations
49 class CurvilinearGrid;
50 class Polygons;
51 class GeometryList;
52
58 class Mesh2D final : public Mesh
59 {
60 public:
63
66 {
67 InsideNotIntersected = 0,
68 InsideAndIntersected = 1,
69 FacesWithIncludedCircumcenters = 2
70 };
71
73 ~Mesh2D() override = default;
74
77
80 explicit Mesh2D(Projection projection);
81
86 Mesh2D(const std::vector<Edge>& edges,
87 const std::vector<Point>& nodes,
88 Projection projection);
89
96 Mesh2D(const std::vector<Edge>& edges,
97 const std::vector<Point>& nodes,
98 const std::vector<std::vector<UInt>>& faceNodes,
99 const std::vector<std::uint8_t>& numFaceNodes,
100 Projection projection);
101
106 Mesh2D(const std::vector<Point>& nodes, const Polygons& polygons, Projection projection);
107
109 void Administrate(CompoundUndoAction* undoAction = nullptr) override;
110
112 void ComputeFaceAreaAndMassCenters(bool computeMassCenters = false);
113
115 void FindFaces();
116
120 void FindFacesGivenFaceNodesMapping(const std::vector<std::vector<UInt>>& faceNodes,
121 const std::vector<std::uint8_t>& numFaceNodes);
122
126 [[nodiscard]] std::unique_ptr<SphericalCoordinatesOffsetAction> OffsetSphericalCoordinates(double minx, double maxx);
127
129 void CommitAction(const SphericalCoordinatesOffsetAction& undoAction);
130
132 void CommitAction(PointArrayUndo& undoAction);
133
137 void RestoreAction(const SphericalCoordinatesOffsetAction& undoAction);
138
140 void RestoreAction(PointArrayUndo& undoAction);
141
148 std::vector<Point>& polygonNodesCache,
149 std::vector<UInt>& localNodeIndicesCache,
150 std::vector<UInt>& globalEdgeIndicesCache) const;
151
154 [[nodiscard]] std::vector<Point> GetObtuseTrianglesCenters();
155
159 [[nodiscard]] std::vector<UInt> GetEdgesCrossingSmallFlowEdges(double smallFlowEdgesThreshold);
160
164 [[nodiscard]] std::vector<Point> GetFlowEdgesCenters(const std::vector<UInt>& edges) const;
165
188 [[nodiscard]] std::unique_ptr<meshkernel::UndoAction> DeleteSmallFlowEdges(double smallFlowEdgesThreshold);
189
207 [[nodiscard]] std::unique_ptr<UndoAction> DeleteSmallTrianglesAtBoundaries(double minFractionalAreaTriangles);
208
210 void ComputeNodeNeighbours(std::vector<std::vector<UInt>>& nodesNodes, UInt& maxNumNeighbours) const;
211
214 void ComputeAspectRatios(std::vector<double>& aspectRatios) const;
215
218
220 MeshNodeType GetNodeType(const UInt nodeId) const { return m_nodesTypes[nodeId]; }
221
223 void GetNodeTypes(std::vector<MeshNodeType>& nodeTypes) const { nodeTypes = m_nodesTypes; }
224
226 [[nodiscard]] std::unique_ptr<UndoAction> DeleteDegeneratedTriangles();
227
229 [[nodiscard]] std::unique_ptr<UndoAction> TriangulateFaces();
230
232 [[nodiscard]] std::unique_ptr<UndoAction> TriangulateFaces(const Polygons& polygon);
233
239 void MakeDualFace(const std::span<const Point> edgeCentres,
240 UInt node,
241 double enlargementFactor,
242 std::vector<Point>& dualFace) const;
243
247 [[nodiscard]] std::vector<UInt> SortedFacesAroundNode(UInt node) const;
248
252 [[nodiscard]] std::vector<Point> ComputeBoundaryPolygons(const std::vector<Point>& polygon);
253
256 std::vector<Point> ComputeInnerBoundaryPolygons() const;
257
260 [[nodiscard]] std::vector<UInt> GetHangingEdges() const;
261
263 [[nodiscard]] std::unique_ptr<UndoAction> DeleteHangingEdges();
264
268 [[nodiscard]] std::vector<UInt> PointFaceIndices(const std::vector<Point>& points);
269
275 [[nodiscard]] std::unique_ptr<UndoAction> DeleteMesh(const Polygons& polygon, DeleteMeshOptions deletionOption, bool invertDeletion);
276
280 [[nodiscard]] std::unique_ptr<UndoAction> DeleteMeshFacesInPolygon(const Polygons& polygon, const bool appendDeletedFaces = true);
281
289 [[nodiscard]] std::vector<bool> FilterBasedOnMetric(Location location, Property property, double minValue, double maxValue) const;
290
295 [[nodiscard]] std::tuple<UInt, UInt> IsSegmentCrossingABoundaryEdge(const Point& firstPoint, const Point& secondPoint) const;
296
302 [[nodiscard]] std::vector<int> MaskEdgesOfFacesInPolygon(const Polygons& polygons, bool invertSelection, bool includeIntersected) const;
303
307 [[nodiscard]] std::vector<int> NodeMaskFromEdgeMask(std::vector<int> const& edgeMask) const;
308
313 [[nodiscard]] std::vector<int> NodeMaskFromPolygon(const Polygons& polygons, bool inside) const;
314
318 UInt FindOppositeEdge(const UInt faceId, const UInt edgeId) const;
319
324 UInt NextFace(const UInt faceId, const UInt edgeId) const;
325
331 static std::unique_ptr<Mesh2D> Merge(const Mesh2D& mesh1, const Mesh2D& mesh2);
332
334 static std::unique_ptr<Mesh2D> Merge(const std::span<const Point>& mesh1Nodes,
335 const std::span<const Edge>& mesh1Edges,
336 const std::span<const Point>& mesh2Nodes,
337 const std::span<const Edge>& mesh2Edges,
338 const Projection projection);
339
343 [[nodiscard]] BoundingBox GetBoundingBox() const;
344
348 [[nodiscard]] std::vector<BoundingBox> GetEdgesBoundingBoxes() const;
349
354 void FindFacesConnectedToNode(UInt nodeIndex, std::vector<UInt>& sharedFaces) const;
355
360 void GetConnectingNodes(UInt nodeIndex, std::vector<UInt>& connectedNodes) const;
361
368 void FindNodesSharedByFaces(UInt nodeIndex, const std::vector<UInt>& sharedFaces, std::vector<UInt>& connectedNodes, std::vector<std::vector<UInt>>& faceNodeMapping) const;
369
374 UInt IsStartOrEnd(const UInt edgeId, const UInt nodeId) const;
375
380 UInt IsLeftOrRight(const UInt elementId, const UInt edgeId) const;
381
385 UInt FindCommonFace(const UInt edge1, const UInt edge2) const;
386
387 private:
388 // orthogonalization
389 static constexpr double m_minimumEdgeLength = 1e-4;
390 static constexpr double m_curvilinearToOrthogonalRatio = 0.5;
391 static constexpr double m_minimumCellArea = 1e-12;
392 static constexpr UInt m_maximumNumberOfHangingNodesAlongEdge = 5;
393
395 using HangingNodeIndexArray = std::array<UInt, m_maximumNumberOfHangingNodesAlongEdge>;
396
398 void ComputeAverageAreOfNeighbouringFaces(const UInt faceId, UInt& numNonBoundaryFaces, double& averageOtherFacesArea) const;
399
401 void FindSmallestCornerAngle(const UInt faceId,
402 double& minCosPhiSmallTriangle,
403 UInt& nodeToPreserve,
404 UInt& firstNodeToMerge,
405 UInt& secondNodeToMerge,
406 UInt& thirdEdgeSmallTriangle) const;
407
409 void DeleteSmallTriangle(const UInt nodeToPreserve,
410 const UInt firstNodeToMerge,
411 const UInt secondNodeToMerge,
412 bool& nodesMerged,
413 CompoundUndoAction& undoAction);
414
416 void FindNodesToDelete(const Polygons& polygon,
417 const bool invertDeletion,
418 std::vector<bool>& isNodeInsidePolygon,
419 std::vector<bool>& deleteNode) const;
420
422 void DeletedMeshNodesAndEdges(const std::function<bool(UInt)>& excludedFace,
423 std::vector<bool>& deleteNode,
424 CompoundUndoAction& deleteMeshAction);
425
427 std::vector<int> ComputeNodeMask(const Polygons& polygons) const;
428
430 std::vector<int> ComputeEdgeMask(const std::vector<int>& nodeMask,
431 bool includeIntersected) const;
432
434 void RemoveIntersected(const std::vector<int>& edgeMask,
435 std::vector<int>& secondEdgeMask) const;
436
438 void InvertSelection(const std::vector<int>& edgeMask,
439 std::vector<int>& secondEdgeMask) const;
440
442 std::vector<bool> FindFacesEntirelyInsidePolygon(const std::vector<bool>& isNodeInsidePolygon) const;
443
445 void WalkBoundaryFromNode(const Polygon& polygon,
446 std::vector<bool>& isVisited,
447 UInt& currentNode,
448 std::vector<Point>& meshBoundaryPolygon) const;
449
453 void WalkMultiBoundaryFromNode(std::vector<bool>& edgeIsVisited,
454 std::vector<bool>& nodeIsVisited,
455 UInt& currentNode,
456 std::vector<Point>& meshBoundaryPolygon,
457 std::vector<UInt>& nodeIds,
458 std::vector<Point>& subSequence) const;
459
461 void OrientatePolygonsAntiClockwise(std::vector<Point>& polygonNodes) const;
462
466 std::vector<Point> RemoveOuterDomainBoundaryPolygon(const std::vector<Point>& polygonNodes) const;
467
472 [[nodiscard]] std::unique_ptr<UndoAction> DeleteMeshFaces(const Polygons& polygon, bool invertDeletion);
473
475 void AppendCellPolygon(const UInt faceId);
476
478 void DeleteMeshHoles(CompoundUndoAction* undoAction);
479
485 std::unique_ptr<UndoAction> UpdateFaceInformation(const std::vector<UInt>& faceIndices, const bool appendDeletedFaces);
486
497 void FindFacesRecursive(UInt startNode,
498 UInt node,
499 UInt previousEdge,
500 UInt numClosingEdges,
501 std::vector<UInt>& edges,
502 std::vector<UInt>& nodes,
503 std::vector<UInt>& sortedEdges,
504 std::vector<UInt>& sortedNodes,
505 std::vector<Point>& nodalValues);
506
511 [[nodiscard]] bool HasTriangleNoAcuteAngles(const std::vector<UInt>& faceNodes, const std::vector<Point>& nodes) const;
512
516 bool HasDuplicateNodes(const UInt numClosingEdges, const std::vector<UInt>& node, std::vector<UInt>& sortedNodes) const;
517
521 bool HasDuplicateEdgeFaces(const UInt numClosingEdges, const std::vector<UInt>& edges, std::vector<UInt>& sortedEdgesFaces) const;
522
524 void ResizeAndInitializeFaceVectors();
525
529 void DoAdministrationGivenFaceNodesMapping(const std::vector<std::vector<UInt>>& faceNodes,
530 const std::vector<std::uint8_t>& numFaceNodes);
531
534 void DoAdministration(CompoundUndoAction* undoAction = nullptr);
535
537 void InitialiseBoundaryNodeClassification(std::vector<int>& intNodeType) const;
538
540 MeshNodeType ClassifyNode(const UInt nodeId) const;
541
543 void ComputeAverageFlowEdgesLength(std::vector<double>& edgesLength,
544 std::vector<double>& averageFlowEdgesLength) const;
545
547 void ComputeAverageEdgeLength(const std::vector<double>& edgesLength,
548 const std::vector<double>& averageFlowEdgesLength,
549 std::vector<bool>& curvilinearGridIndicator,
550 std::vector<std::array<double, 2>>& averageEdgesLength,
551 std::vector<double>& aspectRatios) const;
552
555 UInt InvalidateEdgesWithNoFace();
556
557 std::vector<MeshNodeType> m_nodesTypes;
558 std::vector<Point> m_invalidCellPolygons;
559 };
560
561} // 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:59
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:220
DeleteMeshOptions
Enumerator describing the different options to delete a mesh.
Definition Mesh2D.hpp:66
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.
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 ComputeFaceAreaAndMassCenters(bool computeMassCenters=false)
Compute face mass center.
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)
std::vector< Point > GetFlowEdgesCenters(const std::vector< UInt > &edges) const
Gets the flow edges centers from the crossing edges.
std::vector< Point > ComputeInnerBoundaryPolygons() const
Convert all mesh boundaries to a vector of polygon nodes.
void CommitAction(PointArrayUndo &undoAction)
Set the node values.
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::unique_ptr< UndoAction > DeleteMeshFacesInPolygon(const Polygons &polygon, const bool appendDeletedFaces=true)
Deletes the mesh faces inside a set of polygons.
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)
void RestoreAction(PointArrayUndo &undoAction)
Undo node array.
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:223
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
Property
Enumerator for different properties on a 2D mesh.
Definition Definitions.hpp:158
std::uint32_t UInt
Integer type used when indexing mesh graph entities.
Definition Definitions.hpp:39