MeshKernel
Polygon.hpp
1 //---- GPL ---------------------------------------------------------------------
2 //
3 // Copyright (C) Stichting Deltares, 2011-2023.
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 <vector>
31 
32 #include "MeshKernel/BoundingBox.hpp"
33 #include "MeshKernel/Definitions.hpp"
34 #include "MeshKernel/Point.hpp"
35 
36 namespace meshkernel
37 {
38 
40  class LandBoundary;
41 
45  class Polygon
46  {
47  public:
49  Polygon() = default;
50 
52  Polygon(const Polygon& copy) = default;
53 
55  Polygon(Polygon&& copy) = default;
56 
58  Polygon(const std::vector<Point>& points,
59  Projection projection);
60 
63  Polygon(std::vector<Point>&& points,
64  Projection projection);
65 
67  void Reset(const std::vector<Point>& points,
68  Projection projection);
69 
71  Polygon& operator=(const Polygon& copy);
72 
74  Polygon& operator=(Polygon&& copy);
75 
77  UInt Size() const;
78 
80  const std::vector<Point>& Nodes() const;
81 
83  Point& Node(const size_t i);
84 
86  const Point& Node(const size_t i) const;
87 
89  const BoundingBox& GetBoundingBox() const;
90 
92  bool Contains(const Point& point) const;
93 
97  void SnapToLandBoundary(const size_t startIndex, const size_t endIndex, const LandBoundary& boundary);
98 
104  std::vector<Point> Refine(UInt startIndex, UInt endIndex, double refinementDistance) const;
105 
108  std::vector<Point> LinearRefine(UInt startIndex, UInt endIndex) const;
109 
111  std::tuple<double, Point, TraversalDirection> FaceAreaAndCenterOfMass() const;
112 
114  static std::tuple<double, Point, TraversalDirection> FaceAreaAndCenterOfMass(const std::vector<Point>& polygon, const Projection projection);
115 
120  static std::tuple<double, Point, TraversalDirection> FaceAreaAndCenterOfMass(const std::vector<Point>& nodes,
121  const std::vector<UInt>& nodeIndices,
122  const Projection projection,
123  bool isClosed);
124 
126  double PerimeterLength() const;
127 
130  std::vector<double> EdgeLengths() const;
131 
133  std::vector<Point> ComputeOffset(double displacement, const bool innerAndOuter) const;
134 
136  Projection GetProjection() const;
137 
138  private:
146  static void RefineSegment(std::vector<meshkernel::Point>& refinedPolygon,
147  const std::vector<meshkernel::Point>::const_iterator& nodeIterator,
148  const double refinementDistance,
149  const meshkernel::Projection projection);
150 
152  static void computeAverageLengths(const std::vector<double>& cumulativeDistances, std::vector<double>& averageDistances);
153 
155  static void smoothCumulativeDistance(const std::vector<double>& averageDistances, std::vector<double>& cumulativeDistances);
156 
158  static void smoothAverageLengths(const std::vector<double>& cumulativeDistances,
159  const double firstDistance,
160  const double lastDistance,
161  std::vector<double>& averageLengths);
162 
164  void GetPolygonNodes(const UInt startIndex,
165  const UInt endIndex,
166  std::vector<Point>& polygonNodes) const;
167 
169  std::vector<double> ComputeCumulativeDistances(const std::vector<meshkernel::Point>& polygonNodes) const;
170 
172  std::tuple<meshkernel::UInt, meshkernel::UInt>
173  FindMinMaxRatioIndex(const std::vector<double>& averageLengths,
174  const std::vector<double>& actualAverageLengths) const;
175 
177  static meshkernel::Point interpolatePointOnPolyline(const std::vector<meshkernel::Point>& points,
178  const std::vector<double>& cumulativeDistances,
179  const double pointDistance);
180 
182  void Initialise();
183 
187  bool ContainsCartesian(const Point& point) const;
188 
190  bool ContainsSphericalAccurate(const Point& point) const;
191 
193  std::vector<Point> m_nodes;
194 
196  Projection m_projection = Projection::cartesian;
197 
199  BoundingBox m_boundingBox;
200  };
201 
202 } // namespace meshkernel
203 
205 {
206  return static_cast<UInt>(m_nodes.size());
207 }
208 
209 inline const std::vector<meshkernel::Point>& meshkernel::Polygon::Nodes() const
210 {
211  return m_nodes;
212 }
213 
215 {
216  return m_nodes[i];
217 }
218 
219 inline const meshkernel::Point& meshkernel::Polygon::Node(const size_t i) const
220 {
221  return m_nodes[i];
222 }
223 
225 {
226  return m_boundingBox;
227 }
228 
230 {
231  return m_projection;
232 }
meshkernel::Projection
Projection
Enumerator describing the supported projections.
Definition: Definitions.hpp:41
meshkernel::BoundingBox
A class defining a bounding box.
Definition: BoundingBox.hpp:39
meshkernel::Polygon::LinearRefine
std::vector< Point > LinearRefine(UInt startIndex, UInt endIndex) const
Refine the polygon.
meshkernel::Polygon::GetProjection
Projection GetProjection() const
Get the projection used.
Definition: Polygon.hpp:229
meshkernel::Point
A struct describing a point in a two-dimensional space.
Definition: Point.hpp:40
meshkernel::Polygon::FaceAreaAndCenterOfMass
std::tuple< double, Point, TraversalDirection > FaceAreaAndCenterOfMass() const
Compute the area of the polygon, its centre of mass and the direction.
meshkernel::Polygon::PerimeterLength
double PerimeterLength() const
Compute the perimiter length of the closed polygon.
meshkernel::Polygon::Contains
bool Contains(const Point &point) const
Determine if the polygon contains the point.
meshkernel::LandBoundary
A class containing the land boundary polylines.
Definition: LandBoundary.hpp:39
meshkernel::Polygon
A closed polygon.
Definition: Polygon.hpp:45
meshkernel::Polygon::ComputeOffset
std::vector< Point > ComputeOffset(double displacement, const bool innerAndOuter) const
Compute the poygon offset.
meshkernel::Polygon::Polygon
Polygon()=default
Default constructor.
meshkernel::Polygon::SnapToLandBoundary
void SnapToLandBoundary(const size_t startIndex, const size_t endIndex, const LandBoundary &boundary)
Snap the section of the polygon defined by start- and end-index to the land boundary.
meshkernel::Polygon::operator=
Polygon & operator=(const Polygon &copy)
Copy assignment operator.
meshkernel::Polygon::Node
Point & Node(const size_t i)
Return single point at position.
Definition: Polygon.hpp:214
meshkernel::Polygon::Refine
std::vector< Point > Refine(UInt startIndex, UInt endIndex, double refinementDistance) const
Refine the polygon.
meshkernel
Contains the logic of the C++ static library.
Definition: AveragingInterpolation.hpp:36
meshkernel::Polygon::Nodes
const std::vector< Point > & Nodes() const
Return vector of nodes of the polygon.
Definition: Polygon.hpp:209
meshkernel::UInt
std::uint32_t UInt
Integer type used when indexing mesh graph entities.
Definition: Definitions.hpp:38
meshkernel::Polygon::GetBoundingBox
const BoundingBox & GetBoundingBox() const
Get the bounding box of the polygon.
Definition: Polygon.hpp:224
meshkernel::Polygon::Reset
void Reset(const std::vector< Point > &points, Projection projection)
Reset the polygon.
meshkernel::Polygon::Size
UInt Size() const
Return the number of points in the polygon.
Definition: Polygon.hpp:204
meshkernel::Polygon::EdgeLengths
std::vector< double > EdgeLengths() const
Computes the edge lengths of the polygon.