Debian Codegraph @

src/atlas/util/Geometry.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 #pragma once
12
13 #include <memory>
14 #include <sstream>
15 #include <string>
16 #include <utility>
17
18 #include "atlas/runtime/Exception.h"
19 #include "atlas/util/Earth.h"
20 #include "atlas/util/Object.h"
21 #include "atlas/util/ObjectHandle.h"
22 #include "atlas/util/Point.h"
23
24 namespace atlas {
25 namespace geometry {
26
27 //------------------------------------------------------------------------------------------------------
28
29 namespace detail {
30
31 // TODO: move greatCircleCourse to eckit::geometry::Sphere
32
33 /// @brief Calculate great-cricle course between points
34 ///
35 /// @details Calculates the direction (clockwise from north) of a great-circle
36 /// arc between lonLat1 and lonLat2. Returns the direction of the arc
37 /// at lonLat1 (first) and lonLat2 (second). Angle is normalised to the
38 /// range of atan2 (usually (-180, 180]). All input and output values
39 /// are in units of degrees.
40 /// @ref https://en.wikipedia.org/wiki/Great-circle_navigation
41 std::pair<double, double> greatCircleCourse(const Point2& lonLat1, const Point2& lonLat2);
42
43 //------------------------------------------------------------------------------------------------------
44
45 class GeometryBase : public util::Object {
46 public:
47 virtual ~GeometryBase() = default;
48 virtual void lonlat2xyz(const Point2&, Point3&) const = 0;
49 virtual void xyz2lonlat(const Point3&, Point2&) const = 0;
50 virtual double distance(const Point2& p1, const Point2& p2) const = 0;
51 virtual double distance(const Point3& p1, const Point3& p2) const = 0;
52 virtual double radius() const = 0;
53 virtual double area() const = 0;
54 virtual std::pair<double, double> greatCircleCourse(const Point2& p1, const Point2& p2) const = 0;
55
56 Point3 xyz(const Point2& lonlat) const {
57 Point3 xyz;
58 lonlat2xyz(lonlat, xyz);
59 return xyz;
60 }
61 Point2 lonlat(const Point3& xyz) const {
62 Point2 lonlat;
63 xyz2lonlat(xyz, lonlat);
64 return lonlat;
65 }
66 };
67
68 //------------------------------------------------------------------------------------------------------
69
70 template <typename SphereT>
71 class GeometrySphereT : public GeometryBase {
72 public:
73 void lonlat2xyz(const Point2& lonlat, Point3& xyz) const override {
74 #if ATLAS_ECKIT_VERSION_AT_LEAST(1, 24, 0)
75 SphereT::convertSphericalToCartesian(lonlat, xyz, 0., true);
76 #else
77 SphereT::convertSphericalToCartesian(lonlat, xyz);
78 #endif
79 }
80 void xyz2lonlat(const Point3& xyz, Point2& lonlat) const override {
81 SphereT::convertCartesianToSpherical(xyz, lonlat);
82 }
83 double distance(const Point2& p1, const Point2& p2) const override { return SphereT::distance(p1, p2); }
84 double distance(const Point3& p1, const Point3& p2) const override { return SphereT::distance(p1, p2); }
85 double radius() const override { return SphereT::radius(); }
86 double area() const override { return SphereT::area(); }
87 std::pair<double, double> greatCircleCourse(const Point2& p1, const Point2& p2) const override {
88 return atlas::geometry::detail::greatCircleCourse(p1, p2);
89 }
90 };
91
92 class GeometrySphere : public GeometryBase {
93 using Sphere = eckit::geometry::Sphere;
94
95 public:
96 GeometrySphere(double radius): radius_(radius) {}
97 void lonlat2xyz(const Point2& lonlat, Point3& xyz) const override;
98 void xyz2lonlat(const Point3& xyz, Point2& lonlat) const override;
99 double distance(const Point2& p1, const Point2& p2) const override { return Sphere::distance(radius_, p1, p2); }
100 double distance(const Point3& p1, const Point3& p2) const override { return Sphere::distance(radius_, p1, p2); }
101 double radius() const override { return radius_; }
102 double area() const override { return Sphere::area(radius_); }
103 std::pair<double, double> greatCircleCourse(const Point2& p1, const Point2& p2) const override {
104 return atlas::geometry::detail::greatCircleCourse(p1, p2);
105 }
106
107 private:
108 double radius_;
109 };
110
111 } // namespace detail
112 } // namespace geometry
113
114 //------------------------------------------------------------------------------------------------------
115
116 class Geometry : public util::ObjectHandle<geometry::detail::GeometryBase> {
117 public:
118 using Handle::Handle;
119
120 Geometry(): Handle(build<geometry::detail::GeometrySphereT<util::Earth>>()) {}
121 Geometry(const std::string& name): Handle(build(name)) {}
122 Geometry(const char* name): Handle(build(name)) {}
123 Geometry(double radius): Handle(build<geometry::detail::GeometrySphere>(radius)) {}
124
125 template <typename SphereT>
126 Geometry(const SphereT&): Handle(build<SphereT>()) {}
127
128 Point3 xyz(const Point2& lonlat) const { return get()->xyz(lonlat); }
129 Point2 lonlat(const Point3& xyz) const { return get()->lonlat(xyz); }
130 void xyz2lonlat(const Point3& xyz, Point2& lonlat) const { get()->xyz2lonlat(xyz, lonlat); }
131 void lonlat2xyz(const Point2& lonlat, Point3& xyz) const { get()->lonlat2xyz(lonlat, xyz); }
132 double distance(const Point2& p1, const Point2& p2) const { return get()->distance(p1, p2); }
133 double distance(const Point3& p1, const Point3& p2) const { return get()->distance(p1, p2); }
134 double radius() const { return get()->radius(); }
135 double area() const { return get()->area(); }
136 std::pair<double, double> greatCircleCourse(const Point2& p1, const Point2& p2) const { return get()->greatCircleCourse(p1, p2); }
137
138 protected:
139 template <typename GeometryT, typename... Args>
140 static Implementation* build(Args... args) {
141 return new GeometryT(args...);
142 }
143
144 static Implementation* build(const std::string& name) {
145 // Factory without self registration
146 if (name == "Earth") {
147 return build<geometry::detail::GeometrySphereT<util::Earth>>();
148 }
149 else if (name == "UnitSphere") {
150 return build<geometry::detail::GeometrySphereT<eckit::geometry::UnitSphere>>();
151 }
152 else {
153 ATLAS_THROW_EXCEPTION("name " << name << " is not a valid key for a Geometry");
154 }
155 }
156 };
157
158 //------------------------------------------------------------------------------------------------------
159
160 namespace geometry {
161 using Earth = Geometry; // Sphere with util::Earth radius by default
162 class UnitSphere : public Geometry {
163 public:
164 UnitSphere(): Geometry(/*radius*/ 1.) {}
165 };
166
167 } // namespace geometry
168
169
170 // ------------------------------------------------------------------
171 // C wrapper interfaces to C++ routines
172
173 extern "C" {
174 Geometry::Implementation* atlas__Geometry__new_name(const char* name);
175 Geometry::Implementation* atlas__Geometry__new_radius(const double radius);
176 void atlas__Geometry__delete(Geometry::Implementation* This);
177 void atlas__Geometry__xyz2lonlat(Geometry::Implementation* This, const double x, const double y, const double z,
178 double& lon, double& lat);
179 void atlas__Geometry__lonlat2xyz(Geometry::Implementation* This, const double lon, const double lat, double& x,
180 double& y, double& z);
181 double atlas__Geometry__distance_lonlat(Geometry::Implementation* This, const double lon1, const double lat1,
182 const double lon2, const double lat2);
183 double atlas__Geometry__distance_xyz(Geometry::Implementation* This, const double x1, const double y1, const double z1,
184 const double x2, const double y2, const double z2);
185 double atlas__Geometry__radius(Geometry::Implementation* This);
186 double atlas__Geometry__area(Geometry::Implementation* This);
187 }
188
189 //------------------------------------------------------------------------------------------------------
190
191 } // namespace atlas