Debian Codegraph @

src/atlas/util/Polygon.h

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 /// @file Polygon.h
12 /// @author Pedro Maciel
13 /// @author Willem Deconinck
14 /// @date September 2017
15
16 #pragma once
17
18 #include <iosfwd>
19 #include <memory>
20 #include <set>
21 #include <utility>
22 #include <vector>
23
24 #include "atlas/mdspan.h"
25
26 #include "atlas/library/config.h"
27 #include "atlas/util/Config.h"
28 #include "atlas/util/Object.h"
29 #include "atlas/util/Point.h"
30 #include "atlas/util/VectorOfAbstract.h"
31
32 namespace eckit {
33 class PathName;
34 }
35
36 namespace atlas {
37 namespace util {
38
39 //------------------------------------------------------------------------------------------------------
40
41 /// Polygon
42 class Polygon : public std::vector<idx_t> {
43 public:
44 // -- Types
45
46 struct edge_t : std::pair<idx_t, idx_t> {
47 edge_t(idx_t A, idx_t B): std::pair<idx_t, idx_t>(A, B) {}
48
49 edge_t reverse() const { return edge_t(std::pair<idx_t, idx_t>::second, std::pair<idx_t, idx_t>::first); }
50
51 struct LessThan {
52 bool operator()(const edge_t& e1, const edge_t& e2) const {
53 // order ascending by 'first'
54 return (e1.first < e2.first ? true : e1.first > e2.first ? false : e1.second < e2.second);
55 }
56 };
57 };
58
59 using edge_set_t = std::set<edge_t, typename edge_t::LessThan>;
60 using container_t = std::vector<idx_t>;
61
62 // -- Constructors
63
64 Polygon();
65 Polygon(const edge_set_t&);
66
67 // -- Operators
68
69 operator bool() const;
70
71 Polygon& operator+=(const Polygon&);
72
73 // -- Methods
74
75 void print(std::ostream&) const;
76
77 // -- Friends
78
79 friend std::ostream& operator<<(std::ostream& s, const Polygon& p) {
80 p.print(s);
81 return s;
82 }
83
84 protected:
85 void setup(const edge_set_t&);
86 };
87
88 //------------------------------------------------------------------------------------------------------
89
90 class PartitionPolygons;
91
92 class PartitionPolygon : public Polygon, util::Object {
93 public:
94 using Polygon::Polygon;
95 using PointsXY = std::vector<Point2>;
96 using PointsLonLat = std::vector<Point2>;
97 class RectangleXY {
98 public:
99 RectangleXY() = default;
100 RectangleXY(const std::array<double,2>& x, const std::array<double,2>& y) :
101 xmin_{x[0]}, xmax_{x[1]}, ymin_{y[0]}, ymax_{y[1]} {}
102
103 double xmin() const { return xmin_; }
104 double xmax() const { return xmax_; }
105 double ymin() const { return ymin_; }
106 double ymax() const { return ymax_; }
107 private:
108 double xmin_{std::numeric_limits<double>::max()};
109 double xmax_{std::numeric_limits<double>::max()};
110 double ymin_{std::numeric_limits<double>::max()};
111 double ymax_{std::numeric_limits<double>::max()};
112 };
113
114 /// @brief Return inscribed rectangular domain (not rotated)
115 virtual const RectangleXY& inscribedDomain() const;
116
117 /// @brief Return value of halo
118 virtual idx_t halo() const { return 0; }
119
120 /// @brief Return the memory footprint of the Polygon
121 virtual size_t footprint() const { return 0; }
122
123 /// @brief Output a python script that plots the partition
124 virtual void outputPythonScript(const eckit::PathName&, const eckit::Configuration& = util::NoConfig()) const {}
125
126 /// @brief Output a JSON file with partition polygons
127 virtual std::string json(const eckit::Configuration& = util::NoConfig()) const;
128
129 /// @brief All (x,y) coordinates defining a polygon. Last point should match first.
130 virtual PointsXY xy() const = 0;
131
132 /// @brief All (lon,lat) coordinates defining a polygon. Last point should match first.
133 virtual PointsLonLat lonlat() const = 0;
134
135 virtual void allGather(PartitionPolygons&) const = 0;
136 };
137
138 //------------------------------------------------------------------------------------------------------
139
140 class ExplicitPartitionPolygon : public util::PartitionPolygon {
141 public:
142 explicit ExplicitPartitionPolygon(PointsXY&& points):
143 ExplicitPartitionPolygon(std::move(points), RectangleXY()) {}
144
145 explicit ExplicitPartitionPolygon(PointsXY&& points, const RectangleXY& inscribed):
146 points_(std::move(points)), inscribed_(inscribed) {
147 setup(compute_edges(points_.size()));
148 }
149
150 PointsXY xy() const override { return points_; }
151 PointsLonLat lonlat() const override { return points_; }
152
153 void allGather(util::PartitionPolygons&) const override;
154
155 const RectangleXY& inscribedDomain() const override { return inscribed_; }
156
157 private:
158 static util::Polygon::edge_set_t compute_edges(idx_t points_size);
159
160
161 private:
162 std::vector<Point2> points_;
163 RectangleXY inscribed_;
164 }; // namespace util
165
166 //------------------------------------------------------------------------------------------------------
167
168 class PartitionPolygons : public VectorOfAbstract<PartitionPolygon> {
169 public:
170 std::string json(const eckit::Configuration& = util::NoConfig()) const;
171 };
172
173 //------------------------------------------------------------------------------------------------------
174
175 class PolygonCoordinates {
176 public:
177 using Vector = VectorOfAbstract<PolygonCoordinates>;
178 // -- Constructors
179
180 PolygonCoordinates(const Polygon&, const double x[], const double y[], size_t xstride, size_t ystride, bool removeAlignedPoints);
181
182 template <typename Extents, typename LayoutPolicy, typename AccessorPolicy>
183 PolygonCoordinates(const Polygon& poly, atlas::mdspan<const double, Extents, LayoutPolicy, AccessorPolicy> coordinates, bool removeAlignedPoints):
184 PolygonCoordinates(poly, &coordinates[std::array{0,0}], &coordinates[std::array{0,1}], coordinates.stride(1), coordinates.stride(1), removeAlignedPoints ) {}
185
186 template <typename PointContainer>
187 PolygonCoordinates(const PointContainer& points);
188
189 template <typename PointContainer>
190 PolygonCoordinates(const PointContainer& points, bool removeAlignedPoints);
191
192 // -- Destructor
193
194 virtual ~PolygonCoordinates();
195
196 // -- Methods
197
198 /// @brief Point-in-partition test
199 /// @param[in] P given point
200 /// @return if point is in polygon
201 virtual bool contains(const Point2& P) const = 0;
202
203 const Point2& coordinatesMax() const;
204 const Point2& coordinatesMin() const;
205 const Point2& centroid() const;
206
207 template <typename Index>
208 const Point2& operator[](Index i) const {
209 return coordinates_[i];
210 }
211
212 idx_t size() const { return coordinates_.size(); }
213
214 void print(std::ostream&) const;
215
216 friend std::ostream& operator<<(std::ostream& out, const PolygonCoordinates& pc);
217
218 protected:
219 // -- Members
220
221 Point2 coordinatesMin_;
222 Point2 coordinatesMax_;
223 Point2 centroid_;
224 std::vector<Point2> coordinates_;
225 };
226
227 //------------------------------------------------------------------------------------------------------
228
229 } // namespace util
230 } // namespace atlas