atlas
Polygon.h
Go to the documentation of this file.
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 
15 
16 #pragma once
17 
18 #include <iosfwd>
19 #include <memory>
20 #include <set>
21 #include <utility>
22 #include <vector>
23 
24 #include "atlas/library/config.h"
25 #include "atlas/util/Config.h"
26 #include "atlas/util/Object.h"
27 #include "atlas/util/Point.h"
28 #include "atlas/util/VectorOfAbstract.h"
29 
30 #include "atlas/projection/Projection.h" // for ExplicitPartitionPolygon
31 
32 namespace eckit {
33 class PathName;
34 }
35 
36 namespace atlas {
37 class Field;
38 class RectangularDomain;
39 class Projection;
40 } // namespace atlas
41 
42 namespace atlas {
43 namespace util {
44 
45 //------------------------------------------------------------------------------------------------------
46 
48 class Polygon : public std::vector<idx_t> {
49 public:
50  // -- Types
51 
52  struct edge_t : std::pair<idx_t, idx_t> {
53  edge_t( idx_t A, idx_t B ) : std::pair<idx_t, idx_t>( A, B ) {}
54 
55  edge_t reverse() const { return edge_t( std::pair<idx_t, idx_t>::second, std::pair<idx_t, idx_t>::first ); }
56 
57  struct LessThan {
58  bool operator()( const edge_t& e1, const edge_t& e2 ) const {
59  // order ascending by 'first'
60  return ( e1.first < e2.first ? true : e1.first > e2.first ? false : e1.second < e2.second );
61  }
62  };
63  };
64 
65  using edge_set_t = std::set<edge_t, typename edge_t::LessThan>;
66  using container_t = std::vector<idx_t>;
67 
68  // -- Constructors
69 
70  Polygon();
71  Polygon( const edge_set_t& );
72 
73  // -- Operators
74 
75  operator bool() const;
76 
77  Polygon& operator+=( const Polygon& );
78 
79  // -- Methods
80 
81  void print( std::ostream& ) const;
82 
83  // -- Friends
84 
85  friend std::ostream& operator<<( std::ostream& s, const Polygon& p ) {
86  p.print( s );
87  return s;
88  }
89 
90 protected:
91  void setup( const edge_set_t& );
92 };
93 
94 //------------------------------------------------------------------------------------------------------
95 
96 class PartitionPolygons;
97 
99 public:
100  using Polygon::Polygon;
101  using PointsXY = std::vector<Point2>;
102  using PointsLonLat = std::vector<Point2>;
103 
105  virtual const RectangularDomain& inscribedDomain() const;
106 
108  virtual idx_t halo() const { return 0; }
109 
111  virtual size_t footprint() const { return 0; }
112 
114  virtual void outputPythonScript( const eckit::PathName&, const eckit::Configuration& = util::NoConfig() ) const {}
115 
117  virtual PointsXY xy() const = 0;
118 
120  virtual PointsLonLat lonlat() const = 0;
121 
122  virtual void allGather( PartitionPolygons& ) const = 0;
123 };
124 
125 //------------------------------------------------------------------------------------------------------
126 
128 public:
129  explicit ExplicitPartitionPolygon( PointsXY&& points ) :
130  ExplicitPartitionPolygon( std::move( points ), RectangularDomain() ) {}
131 
132  explicit ExplicitPartitionPolygon( PointsXY&& points, const RectangularDomain& inscribed ) :
133  points_( std::move( points ) ), inscribed_( inscribed ) {
134  setup( compute_edges( points_.size() ) );
135  }
136 
137  PointsXY xy() const override { return points_; }
138  PointsLonLat lonlat() const override { return points_; }
139 
140  void allGather( util::PartitionPolygons& ) const override;
141 
142  const RectangularDomain& inscribedDomain() const override { return inscribed_; }
143 
144 private:
145  static util::Polygon::edge_set_t compute_edges( idx_t points_size );
146 
147 
148 private:
149  std::vector<Point2> points_;
150  RectangularDomain inscribed_;
151 }; // namespace util
152 
153 //------------------------------------------------------------------------------------------------------
154 
155 class PartitionPolygons : public VectorOfAbstract<PartitionPolygon> {};
156 
157 //------------------------------------------------------------------------------------------------------
158 
160 public:
162  // -- Constructors
163 
164  PolygonCoordinates( const Polygon&, const atlas::Field& coordinates, bool removeAlignedPoints );
165 
166  template <typename PointContainer>
167  PolygonCoordinates( const PointContainer& points );
168 
169  template <typename PointContainer>
170  PolygonCoordinates( const PointContainer& points, bool removeAlignedPoints );
171 
172  // -- Destructor
173 
174  virtual ~PolygonCoordinates();
175 
176  // -- Methods
177 
181  virtual bool contains( const Point2& P ) const = 0;
182 
183  const Point2& coordinatesMax() const;
184  const Point2& coordinatesMin() const;
185  const Point2& centroid() const;
186 
187  idx_t size() const { return coordinates_.size(); }
188 
189  void print( std::ostream& ) const;
190 
191 protected:
192  // -- Members
193 
194  Point2 coordinatesMin_;
195  Point2 coordinatesMax_;
196  Point2 centroid_;
197  std::vector<Point2> coordinates_;
198 };
199 
200 //------------------------------------------------------------------------------------------------------
201 
202 } // namespace util
203 } // namespace atlas
A Field contains an Array, Metadata, and a reference to a FunctionSpace.
Definition: Field.h:59
Definition: Polygon.h:159
virtual void outputPythonScript(const eckit::PathName &, const eckit::Configuration &=util::NoConfig()) const
Output a python script that plots the partition.
Definition: Polygon.h:114
Definition: Polygon.h:155
This file contains classes and functions working on points.
Definition: Polygon.h:52
Definition: Polygon.h:127
Polygon.
Definition: Polygon.h:48
PointsLonLat lonlat() const override
All (lon,lat) coordinates defining a polygon. Last point should match first.
Definition: Polygon.h:138
PointsXY xy() const override
All (x,y) coordinates defining a polygon. Last point should match first.
Definition: Polygon.h:137
Definition: Polygon.h:98
Definition: Object.h:18
Definition: Domain.h:19
Definition: VectorOfAbstract.h:52
Contains all atlas classes and methods.
Definition: atlas-grids.cc:33
virtual idx_t halo() const
Return value of halo.
Definition: Polygon.h:108
Definition: Config.h:148
long idx_t
Integer type for indices in connectivity tables.
Definition: config.h:42
const RectangularDomain & inscribedDomain() const override
Return inscribed rectangular domain (not rotated)
Definition: Polygon.h:142
virtual size_t footprint() const
Return the memory footprint of the Polygon.
Definition: Polygon.h:111
Definition: Domain.h:98