Debian Codegraph @

src/atlas/grid/detail/partitioner/EqualRegionsPartitioner.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 // Purpose.
12 // --------
13 // eq_regions provides the code to perform a high level
14 // partitioning of the surface of a sphere into regions of
15 // equal area and small diameter.
16 //
17 // Background.
18 // -----------
19 // This C++ implementation is ported from the MATLAB
20 // "Recursive Zonal Equal Area (EQ) Sphere Partitioning Toolbox" of the
21 // same name developed by Paul Leopardi at the University of New South
22 // Wales.
23 // This version has been coded specifically for the case of partitioning the
24 // surface of a sphere or S^dim (where dim=2) as denoted in the original
25 // code.
26 // Only a subset of the original eq_regions package has been coded to
27 // determin
28 // the high level distribution of regions on a sphere, as the detailed
29 // distribution of grid points to each region is left to implentatios.
30 //
31 // The following copyright notice for the eq_regions package is included
32 // from
33 // the original MatLab release.
34 //
35 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
36 // + Release 1.10 2005-06-26 +
37 // + +
38 // + Copyright (c) 2004, 2005, University of New South Wales +
39 // + +
40 // + Permission is hereby granted, free of charge, to any person obtaining +
41 // + a copy of this software and associated documentation files (the +
42 // + "Software"), to deal in the Software without restriction, including +
43 // + without limitation the rights to use, copy, modify, merge, publish, +
44 // + distribute, sublicense, and/or sell copies of the Software, and to +
45 // + permit persons to whom the Software is furnished to do so, subject to +
46 // + the following conditions: +
47 // + +
48 // + The above copyright notice and this permission notice shall be included
49 // +
50 // + in all copies or substantial portions of the Software. +
51 // + +
52 // + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +
53 // + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +
54 // + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
55 // +
56 // + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +
57 // + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +
58 // + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +
59 // + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +
60 // + +
61 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
62 //
63
64 #pragma once
65
66 #include <vector>
67
68 #include "atlas/grid/detail/partitioner/Partitioner.h"
69
70 namespace atlas {
71 namespace grid {
72 namespace detail {
73 namespace partitioner {
74
75 void eq_caps(int N, std::vector<int>& n_regions, std::vector<double>& s_cap);
76 void eq_regions(int N, double xmin[], double xmax[], double ymin[], double ymax[]);
77
78 class EqualAreaPartitioner;
79 class EqualRegionsPartitioner : public Partitioner {
80 public:
81 EqualRegionsPartitioner();
82
83 EqualRegionsPartitioner(int N);
84 EqualRegionsPartitioner(int N, const eckit::Parametrisation& config);
85 EqualRegionsPartitioner(const eckit::Parametrisation& config);
86
87 void where(int partition, int& band, int& sector) const;
88 int nb_bands() const { return bands_.size(); }
89 int nb_regions(int band) const { return sectors_[band]; }
90
91 using Partitioner::partition;
92 virtual void partition(const Grid&, int part[]) const;
93
94 virtual std::string type() const { return "equal_regions"; }
95
96 public:
97 // Node struct that holds the longitude and latitude in millidegrees
98 // (integers)
99 // This structure is used in sorting algorithms, and uses less memory than
100 // if x and y were in double precision.
101 struct NodeInt {
102 int x, y;
103 int n;
104 bool operator!=(const NodeInt& other) const { return n != other.n; }
105 bool operator==(const NodeInt& other) const { return n == other.n; }
106 void swap(NodeInt& other) {
107 auto _swap = [](int& a, int& b) {
108 int tmp = a;
109 a = b;
110 b = tmp;
111 };
112 _swap(x, other.x);
113 _swap(y, other.y);
114 _swap(n, other.n);
115 }
116 friend void swap(NodeInt& a, NodeInt& b) { a.swap(b); }
117 };
118
119 private:
120 void init();
121 // Doesn't matter if nodes[] is in degrees or radians, as a sorting
122 // algorithm is used internally
123 void partition(int nb_nodes, NodeInt nodes[], int part[]) const;
124
125 friend class EqualAreaPartitioner;
126 // y in radians
127 int band(const double& y) const;
128
129 // x in radians
130 int sector(int band, const double& x) const;
131
132 // x and y in radians
133 int partition(const double& x, const double& y) const;
134
135 private:
136 int N_;
137 std::vector<double> bands_;
138 std::vector<int> sectors_;
139 enum class Coordinates
140 {
141 XY,
142 LONLAT,
143 };
144 Coordinates coordinates_ = Coordinates::XY;
145 };
146
147 } // namespace partitioner
148 } // namespace detail
149 } // namespace grid
150 } // namespace atlas