Debian Codegraph @

src/atlas/projection/detail/ProjectionUtilities.h

raw
1 /*
2 * (C) Copyright 2020 UCAR
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 */
7
8 #pragma once
9
10 #include <cmath>
11 #include <functional>
12
13 #include "eckit/geometry/Sphere.h"
14
15 #include "atlas/domain.h"
16 #include "atlas/runtime/Log.h"
17 #include "atlas/util/Constants.h"
18 #include "atlas/util/CoordinateEnums.h"
19 #include "atlas/util/Earth.h"
20 #include "atlas/util/Point.h"
21
22 namespace atlas {
23 namespace projection {
24 namespace detail {
25
26 // -------------------------------------------------------------------------------------------------
27
28 struct ProjectionUtilities {
29 // -----------------------------------------------------------------------------------------------
30
31 enum class CoordinateSystem
32 {
33 RIGHT_HAND,
34 LEFT_HAND,
35 };
36
37 static constexpr bool debug = false; // constexpr so compiler can optimize `if ( debug ) { ... }` out
38
39 // -----------------------------------------------------------------------------------------------
40
41 static void cartesianToSpherical(const double xyz[], double lonlat[], const CoordinateSystem coordinate_system,
42 const double& radius = 0) {
43 using eckit::geometry::Sphere;
44 using util::Constants;
45
46 // Make point objects.
47 const auto pointXYZ = PointXYZ(xyz);
48 auto pointLonLat = PointLonLat();
49
50 // Transform coordinates.
51 auto r = radius != 0. ? radius : PointXYZ::norm(pointXYZ);
52 Sphere::convertCartesianToSpherical(r, pointXYZ, pointLonLat);
53
54 // Copy to array.
55 lonlat[LON] = pointLonLat.lon();
56 lonlat[LAT] = -pointLonLat.lat();
57
58 // Left or right hand system.
59 if (coordinate_system == CoordinateSystem::RIGHT_HAND) {
60 lonlat[LAT] += 90.;
61 }
62 }
63
64 //------------------------------------------------------------------------------------------------
65
66 static void sphericalToCartesian(const double lonlat[], double xyz[], const CoordinateSystem coordinate_system,
67 const double& radius = 0) {
68 using eckit::geometry::Sphere;
69 using util::Constants;
70
71 // Make point objects.
72
73 const auto pointLonLat = PointLonLat(lonlat);
74
75 auto pointXYZ = PointXYZ();
76
77 // Set Radius
78 auto r = radius != 0 ? radius : util::Earth::radius();
79
80 // Transform coordinates.
81 Sphere::convertSphericalToCartesian(r, pointLonLat, pointXYZ, 0.0);
82
83 if (debug) {
84 Log::info() << "sphericalToCartesian:: pointLonLat pointXYZ = " << pointLonLat << " " << pointXYZ
85 << std::endl;
86 }
87
88 // Copy to array.
89 xyz[XX] = pointXYZ.x();
90 xyz[YY] = pointXYZ.y();
91 xyz[ZZ] = pointXYZ.z();
92
93 // Left or right hand system
94 if (coordinate_system != CoordinateSystem::RIGHT_HAND) {
95 xyz[ZZ] *= -1;
96 }
97 }
98
99 //------------------------------------------------------------------------------------------------
100
101 static void rotate3dX(const double angle, double xyz[]) {
102 const double c = std::cos(angle);
103 const double s = std::sin(angle);
104 double xyz_in[3];
105 std::copy(xyz, xyz + 3, xyz_in);
106 xyz[YY] = c * xyz_in[YY] + s * xyz_in[ZZ];
107 xyz[ZZ] = -s * xyz_in[YY] + c * xyz_in[ZZ];
108 };
109
110 //------------------------------------------------------------------------------------------------
111
112 static void rotate3dY(const double angle, double xyz[]) {
113 const double c = std::cos(angle);
114 const double s = std::sin(angle);
115 double xyz_in[3];
116 std::copy(xyz, xyz + 3, xyz_in);
117 xyz[XX] = c * xyz_in[XX] - s * xyz_in[ZZ];
118 xyz[ZZ] = s * xyz_in[XX] + c * xyz_in[ZZ];
119 };
120
121 //------------------------------------------------------------------------------------------------
122
123 static void rotate3dZ(const double angle, double xyz[]) {
124 const double c = std::cos(angle);
125 const double s = std::sin(angle);
126 double xyz_in[3];
127 std::copy(xyz, xyz + 3, xyz_in);
128 xyz[XX] = c * xyz_in[XX] + s * xyz_in[YY];
129 xyz[YY] = -s * xyz_in[XX] + c * xyz_in[YY];
130 };
131
132 //------------------------------------------------------------------------------------------------
133 };
134
135 //--------------------------------------------------------------------------------------------------
136
137 } // namespace detail
138 } // namespace projection
139 } // namespace atlas