MeshKernel
Polygons.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 <vector>
31 
32 #include <MeshKernel/BoundingBox.hpp>
33 #include <MeshKernel/Exceptions.hpp>
34 #include <MeshKernel/Polygon.hpp>
35 #include <MeshKernel/PolygonalEnclosure.hpp>
36 
37 namespace meshkernel
38 {
39 
41  class LandBoundary;
42 
44  class Polygons
45  {
46  public:
48  Polygons() = default;
49 
53  Polygons(const std::vector<Point>& polygon,
54  Projection projection);
55 
57  // This is bollocks
58  // what about nodes for the interior polygons
59  std::tuple<UInt, UInt, UInt> PolygonIndex(UInt startIndex, UInt endIndex) const;
60 
63  const PolygonalEnclosure& Enclosure(const UInt index) const;
64 
67  [[nodiscard]] std::vector<std::vector<Point>> ComputePointsInPolygons() const;
68 
74  [[nodiscard]] std::vector<Point> RefineFirstPolygon(UInt startIndex, UInt endIndex, double refinementDistance) const;
75 
82  [[nodiscard]] std::vector<Point> RefinePolygon(UInt polygonIndex, UInt startIndex, UInt endIndex, double refinementDistance) const;
83 
89  [[nodiscard]] std::vector<Point> LinearRefinePolygon(UInt polygonIndex, UInt startIndex, UInt endIndex) const;
90 
95  [[nodiscard]] Polygons OffsetCopy(double distance, bool innerAndOuter) const;
96 
103  void SnapToLandBoundary(const LandBoundary& landBoundary, UInt startIndex, UInt endIndex);
104 
110  [[nodiscard]] bool IsPointInPolygon(Point const& point, UInt polygonIndex) const;
111 
115  [[nodiscard]] bool IsPointInAnyPolygon(const Point& point) const;
116 
117  // TODO can reduce the result of this function to only a UInt (valid value => found, invalid => not found)
121  [[nodiscard]] std::tuple<bool, UInt> IsPointInPolygons(const Point& point) const;
122 
126  [[nodiscard]] std::vector<bool> PointsInPolygons(const std::vector<Point>& point) const;
127 
130  [[nodiscard]] bool IsEmpty() const;
131 
132  // TODO rename to NumberOfEnclosures or something like that
135  [[nodiscard]] UInt GetNumPolygons() const;
136 
139  [[nodiscard]] UInt GetNumNodes() const;
140 
143  [[nodiscard]] Projection GetProjection() const { return m_projection; }
144 
146  [[nodiscard]] std::vector<Point> GatherAllEnclosureNodes() const;
147 
151  [[nodiscard]] BoundingBox GetBoundingBox(UInt polygonIndex) const;
152 
153  private:
157  double PerimeterClosedPolygon(const std::vector<Point>& polygonNodes) const;
158 
162  std::vector<double> PolygonEdgeLengths(const std::vector<Point>& polygonNodes) const;
163 
164  std::vector<PolygonalEnclosure> m_enclosures;
165  Projection m_projection;
166  std::vector<std::pair<UInt, UInt>> m_outer_polygons_indices;
167  UInt m_numberOfNodes = 0;
168  };
169 } // namespace meshkernel
170 
172 {
173  if (IsEmpty())
174  {
175  throw ConstraintError("Enclosures list is empty.");
176  }
177 
178  if (index >= m_enclosures.size())
179  {
180  throw ConstraintError("Invalid enclosure index: {}, maximum index: {}", index, m_enclosures.size() - 1);
181  }
182 
183  return m_enclosures[index];
184 }
185 
187 {
188  return static_cast<UInt>(m_enclosures.size());
189 }
190 
192 {
193  return m_numberOfNodes;
194 }
meshkernel::Projection
Projection
Enumerator describing the supported projections.
Definition: Definitions.hpp:41
meshkernel::Polygons::GetNumNodes
UInt GetNumNodes() const
Gets the number of polygon nodes.
Definition: Polygons.hpp:191
meshkernel::Polygons::GetNumPolygons
UInt GetNumPolygons() const
Gives the number of polygons.
Definition: Polygons.hpp:186
meshkernel::Polygons::ComputePointsInPolygons
std::vector< std::vector< Point > > ComputePointsInPolygons() const
Creates points inside the polygon using triangulation (the edges size determines how many points will...
meshkernel::Polygons::RefinePolygon
std::vector< Point > RefinePolygon(UInt polygonIndex, UInt startIndex, UInt endIndex, double refinementDistance) const
Refines the polygon edges with additional nodes, from the start to the end index (refinepolygonpart)
meshkernel::Polygons::IsPointInAnyPolygon
bool IsPointInAnyPolygon(const Point &point) const
Checks if a point is included in any of the polygonal enclosures contained.
meshkernel::Polygons::IsPointInPolygon
bool IsPointInPolygon(Point const &point, UInt polygonIndex) const
Checks if a point is included in a given polygon. When the polygon is empty, the point is always incl...
meshkernel::Polygons::IsPointInPolygons
std::tuple< bool, UInt > IsPointInPolygons(const Point &point) const
Checks if a point is included in any of the polygons (dbpinpol_optinside_perpol)
meshkernel::Polygons::Polygons
Polygons()=default
Default constructor.
meshkernel::BoundingBox
A class defining a bounding box.
Definition: BoundingBox.hpp:39
meshkernel::Point
A struct describing a point in a two-dimensional space.
Definition: Point.hpp:40
meshkernel::Polygons::OffsetCopy
Polygons OffsetCopy(double distance, bool innerAndOuter) const
Makes a new polygon from an existing one, by offsetting it by a distance (copypol)
meshkernel::Polygons::PointsInPolygons
std::vector< bool > PointsInPolygons(const std::vector< Point > &point) const
For each point, compute the index of the polygon including it.
meshkernel::LandBoundary
A class containing the land boundary polylines.
Definition: LandBoundary.hpp:39
meshkernel::Polygons::Enclosure
const PolygonalEnclosure & Enclosure(const UInt index) const
Get the polygonal enclosure at the index.
Definition: Polygons.hpp:171
meshkernel::Polygons::GatherAllEnclosureNodes
std::vector< Point > GatherAllEnclosureNodes() const
Gets the nodes of all enclosures.
meshkernel
Contains the logic of the C++ static library.
Definition: AveragingInterpolation.hpp:36
meshkernel::PolygonalEnclosure
A region enclosed by a polygonal permieter.
Definition: PolygonalEnclosure.hpp:45
meshkernel::Polygons::SnapToLandBoundary
void SnapToLandBoundary(const LandBoundary &landBoundary, UInt startIndex, UInt endIndex)
Snap the polygon to the land boundary.
meshkernel::Polygons::LinearRefinePolygon
std::vector< Point > LinearRefinePolygon(UInt polygonIndex, UInt startIndex, UInt endIndex) const
Linear refines the polygon edges with additional nodes, from the start to the end index.
meshkernel::UInt
std::uint32_t UInt
Integer type used when indexing mesh graph entities.
Definition: Definitions.hpp:38
meshkernel::Polygons::PolygonIndex
std::tuple< UInt, UInt, UInt > PolygonIndex(UInt startIndex, UInt endIndex) const
Get index of the polygon, and map the start- and end-index to the start- and end-index of the local p...
meshkernel::Polygons::GetProjection
Projection GetProjection() const
Gets the projection.
Definition: Polygons.hpp:143
meshkernel::Polygons
A class containing a list of polygonaly enclosed regions.
Definition: Polygons.hpp:44
meshkernel::Polygons::RefineFirstPolygon
std::vector< Point > RefineFirstPolygon(UInt startIndex, UInt endIndex, double refinementDistance) const
Refines the polygon edges with additional nodes, from the start to the end index (refinepolygonpart)
meshkernel::Polygons::IsEmpty
bool IsEmpty() const
Checks if the polygon is empty.
meshkernel::Polygons::GetBoundingBox
BoundingBox GetBoundingBox(UInt polygonIndex) const
Gets the bounding box for the polygon index i.
meshkernel::ConstraintError
An exception class thrown when an attempt is made that violates a range constraint.
Definition: Exceptions.hpp:266