File: test_sphere.cc

package info (click to toggle)
eckit 1.33.1-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 11,216 kB
  • sloc: cpp: 112,634; ansic: 2,826; yacc: 590; lex: 361; python: 289; sh: 162; makefile: 50
file content (329 lines) | stat: -rw-r--r-- 10,142 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
 * (C) Copyright 1996- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation nor
 * does it submit to any jurisdiction.
 */

#include <cmath>
#include <limits>

#include "eckit/geometry/Point2.h"
#include "eckit/geometry/Point3.h"
#include "eckit/geometry/SphereT.h"
#include "eckit/geometry/UnitSphere.h"
#include "eckit/testing/Test.h"

namespace eckit::test {

using namespace geometry;

struct PointLonLat : Point2 {
    PointLonLat(double x, double y) : Point2(x, y) {}
    const double& lon() const { return x_[0]; }
    const double& lat() const { return x_[1]; }
};

struct PointXYZ : Point3 {
    const double& x() const { return x_[0]; }
    const double& y() const { return x_[1]; }
    const double& z() const { return x_[2]; }
};

// set sphere
struct DatumTwoUnits {
    static double radius() { return 2.; }
};

using TwoUnitsSphere = SphereT<DatumTwoUnits>;

const double R = UnitSphere::radius();


// -----------------------------------------------------------------------------
// test unit sphere radius

CASE("test unit sphere radius") {
    EXPECT(UnitSphere::radius() == 1.);
}

// -----------------------------------------------------------------------------
// test unit sphere poles

CASE("test unit sphere north pole") {
    const PointLonLat ll1(0., 90.);
    PointXYZ p;
    UnitSphere::convertSphericalToCartesian(ll1, p);

    EXPECT(p.x() == 0);
    EXPECT(p.y() == 0);
    EXPECT(p.z() == R);
}

CASE("test unit sphere south pole") {
    const PointLonLat ll1(0., -90.);
    PointXYZ p;
    UnitSphere::convertSphericalToCartesian(ll1, p);

    EXPECT(p.x() == 0);
    EXPECT(p.y() == 0);
    EXPECT(p.z() == -R);
}

// -----------------------------------------------------------------------------
// test unit sphere quadrants

CASE("test unit sphere lon 0") {
    const PointLonLat ll1(0., 0.);
    const PointLonLat ll2(-360., 0.);
    PointXYZ p, q;
    UnitSphere::convertSphericalToCartesian(ll1, p);
    UnitSphere::convertSphericalToCartesian(ll2, q);

    EXPECT(p.x() == R);
    EXPECT(p.y() == 0);
    EXPECT(p.z() == 0);

    EXPECT(PointXYZ::equal(p, q));
}

CASE("test unit sphere lon 90") {
    const PointLonLat ll1(90., 0.);
    const PointLonLat ll2(-270., 0.);
    PointXYZ p, q;
    UnitSphere::convertSphericalToCartesian(ll1, p);
    UnitSphere::convertSphericalToCartesian(ll2, q);

    EXPECT(p.x() == 0);
    EXPECT(p.y() == R);
    EXPECT(p.z() == 0);

    EXPECT(PointXYZ::equal(p, q));
}

CASE("test unit sphere lon 180") {
    const PointLonLat ll1(180., 0.);
    const PointLonLat ll2(-180., 0.);
    PointXYZ p, q;
    UnitSphere::convertSphericalToCartesian(ll1, p);
    UnitSphere::convertSphericalToCartesian(ll2, q);

    EXPECT(p.x() == -R);
    EXPECT(p.y() == 0);
    EXPECT(p.z() == 0);

    EXPECT(PointXYZ::equal(p, q));
}

CASE("test unit sphere lon 270") {
    const PointLonLat ll1(270., 0.);
    const PointLonLat ll2(-90., 0.);
    PointXYZ p, q;
    UnitSphere::convertSphericalToCartesian(ll1, p);
    UnitSphere::convertSphericalToCartesian(ll2, q);

    EXPECT(p.x() == 0);
    EXPECT(p.y() == -R);
    EXPECT(p.z() == 0);

    EXPECT(PointXYZ::equal(p, q));
}


// -----------------------------------------------------------------------------
// test unit sphere octants

const double L = R * std::sqrt(2) / 2.;

CASE("test unit sphere lon 45") {
    const PointLonLat ll1(45., 0.);
    const PointLonLat ll2(-315., 0.);
    PointXYZ p, q;
    UnitSphere::convertSphericalToCartesian(ll1, p);
    UnitSphere::convertSphericalToCartesian(ll2, q);

    EXPECT(eckit::types::is_approximately_equal(p.x(), L));
    EXPECT(eckit::types::is_approximately_equal(p.y(), L));
    EXPECT(p.z() == 0);

    EXPECT(PointXYZ::equal(p, q));
}

CASE("test unit sphere lon 135") {
    const PointLonLat ll1(135., 0.);
    const PointLonLat ll2(-225., 0.);
    PointXYZ p, q;
    UnitSphere::convertSphericalToCartesian(ll1, p);
    UnitSphere::convertSphericalToCartesian(ll2, q);

    EXPECT(eckit::types::is_approximately_equal(p.x(), -L));
    EXPECT(eckit::types::is_approximately_equal(p.y(), L));
    EXPECT(p.z() == 0);

    EXPECT(PointXYZ::equal(p, q));
}

CASE("test unit sphere lon 225") {
    const PointLonLat ll1(225., 0.);
    const PointLonLat ll2(-135., 0.);
    PointXYZ p, q;
    UnitSphere::convertSphericalToCartesian(ll1, p);
    UnitSphere::convertSphericalToCartesian(ll2, q);

    EXPECT(eckit::types::is_approximately_equal(p.x(), -L));
    EXPECT(eckit::types::is_approximately_equal(p.y(), -L));
    EXPECT(p.z() == 0);

    EXPECT(PointXYZ::equal(p, q));
}

CASE("test unit sphere lon 315") {
    const PointLonLat ll1(315., 0.);
    const PointLonLat ll2(-45., 0.);
    PointXYZ p, q;
    UnitSphere::convertSphericalToCartesian(ll1, p);
    UnitSphere::convertSphericalToCartesian(ll2, q);

    EXPECT(eckit::types::is_approximately_equal(p.x(), L));
    EXPECT(eckit::types::is_approximately_equal(p.y(), -L));
    EXPECT(p.z() == 0);

    EXPECT(PointXYZ::equal(p, q));
}

// -----------------------------------------------------------------------------
// test unit sphere with non-canonical latitudes outside [-90, 90]

CASE("test unit sphere lat 100") {
    const PointLonLat ll1(0., 100.);
    const PointLonLat ll2(180., 80.);
    PointXYZ p, q;

    // Default behavior throws
    EXPECT_THROWS_AS(UnitSphere::convertSphericalToCartesian(ll1, p), eckit::BadValue);

    UnitSphere::convertSphericalToCartesian(ll1, p, 0., true);
    UnitSphere::convertSphericalToCartesian(ll2, q, 0., true);

    // sin(x) and sin(pi-x) are not bitwise identical
    EXPECT(eckit::types::is_approximately_equal(p.x(), q.x()));
    EXPECT(eckit::types::is_approximately_equal(p.y(), q.y()));
    EXPECT(eckit::types::is_approximately_equal(p.z(), q.z()));
}

CASE("test unit sphere lat 290") {
    const PointLonLat ll1(15., 290.);
    const PointLonLat ll2(15., -70.);
    PointXYZ p, q;

    // Default behavior throws
    EXPECT_THROWS_AS(UnitSphere::convertSphericalToCartesian(ll1, p), eckit::BadValue);

    UnitSphere::convertSphericalToCartesian(ll1, p, 0., true);
    UnitSphere::convertSphericalToCartesian(ll2, q, 0., true);

    // sin(x) and sin(pi-x) are not bitwise identical
    EXPECT(eckit::types::is_approximately_equal(p.x(), q.x()));
    EXPECT(eckit::types::is_approximately_equal(p.y(), q.y()));
    EXPECT(eckit::types::is_approximately_equal(p.z(), q.z()));
}

CASE("test unit sphere lat -120") {
    const PointLonLat ll1(45., -120.);
    const PointLonLat ll2(225., -60.);
    PointXYZ p, q;

    // Default behavior throws
    EXPECT_THROWS_AS(UnitSphere::convertSphericalToCartesian(ll1, p), eckit::BadValue);

    UnitSphere::convertSphericalToCartesian(ll1, p, 0., true);
    UnitSphere::convertSphericalToCartesian(ll2, q, 0., true);

    // sin(x) and sin(pi-x) are not bitwise identical
    EXPECT(eckit::types::is_approximately_equal(p.x(), q.x()));
    EXPECT(eckit::types::is_approximately_equal(p.y(), q.y()));
    EXPECT(eckit::types::is_approximately_equal(p.z(), q.z()));
}

// -----------------------------------------------------------------------------
// test unit sphere distances with non-canonical coordinates

CASE("test unit sphere distances") {
    const PointLonLat P1(-71.6, -33.);  // Valparaíso
    const PointLonLat P2(121.8, 31.4);  // Shanghai

    // Same points with added shifts
    const PointLonLat P1b(288.4, -33.);    // Valparaíso + longitude shift
    const PointLonLat P2b(301.8, 148.6);   // Shanghai + latitude/longitude shift
    const PointLonLat P2c(-58.2, -211.4);  // Shanghai + latitude/longitude shift

    const double d0 = UnitSphere::distance(P1, P2);
    const double d1 = UnitSphere::distance(P1b, P2);
    const double d2 = UnitSphere::distance(P1, P2b);
    const double d3 = UnitSphere::distance(P1, P2c);

    EXPECT(eckit::types::is_approximately_equal(d0, d1));
    EXPECT(eckit::types::is_approximately_equal(d0, d2));
    EXPECT(eckit::types::is_approximately_equal(d0, d3));
}

// -----------------------------------------------------------------------------
// test unit sphere area

CASE("test unit sphere area globe") {
    EXPECT(UnitSphere::area() == 4. * M_PI * R * R);
}

CASE("test unit sphere area hemispheres") {
    const PointLonLat ll1(-180., 90.);
    const PointLonLat ll2(180., 0.);
    const PointLonLat ll3(-180., 0.);
    const PointLonLat ll4(180., -90.);
    const double area_hemisphere_north = UnitSphere::area(ll1, ll2);
    const double area_hemisphere_south = UnitSphere::area(ll3, ll4);

    EXPECT(area_hemisphere_north == 0.5 * UnitSphere::area());
    EXPECT(area_hemisphere_north == area_hemisphere_south);
}

// -----------------------------------------------------------------------------
// test two units sphere

CASE("test two units sphere radius") {
    EXPECT(TwoUnitsSphere::radius() == 2.);
}

CASE("test two units sphere distances") {
    const PointLonLat P1(-71.6, -33.);  // Valparaíso
    const PointLonLat P2(121.8, 31.4);  // Shanghai

    const double d_sphere_1 = UnitSphere::distance(P1, P2);
    const double d_sphere_2 = TwoUnitsSphere::distance(P1, P2);
    EXPECT(2. * d_sphere_1 == d_sphere_2);
}

CASE("test two units sphere areas") {
    const double area_sphere_1 = UnitSphere::area();
    const double area_sphere_2 = TwoUnitsSphere::area();
    EXPECT(4. * area_sphere_1 == area_sphere_2);
}

CASE("test two units sphere sub areas") {
    const PointLonLat P1(-71.6, -33.);  // Valparaíso
    const PointLonLat P2(121.8, 31.4);  // Shanghai

    const double sub_area_sphere_1 = UnitSphere::area(P2, P1);
    const double sub_area_sphere_2 = TwoUnitsSphere::area(P2, P1);
    EXPECT(4. * sub_area_sphere_1 == sub_area_sphere_2);
}

// -----------------------------------------------------------------------------

}  // namespace eckit::test

int main(int argc, char** argv) {
    return eckit::testing::run_tests(argc, argv);
}