| 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 |
#include "atlas/util/Geometry.h" |
| 12 |
|
| 13 |
#include <cmath> |
| 14 |
|
| 15 |
#include "atlas/library/config.h" |
| 16 |
#include "atlas/runtime/Exception.h" |
| 17 |
#include "atlas/util/Constants.h" |
| 18 |
|
| 19 |
namespace atlas { |
| 20 |
|
| 21 |
namespace geometry { |
| 22 |
namespace detail { |
| 23 |
void GeometrySphere::lonlat2xyz(const Point2& lonlat, Point3& xyz) const { |
| 24 |
#if ATLAS_ECKIT_VERSION_AT_LEAST(1, 24, 0) |
| 25 |
Sphere::convertSphericalToCartesian(radius_, lonlat, xyz, 0., true); |
| 26 |
#else |
| 27 |
Sphere::convertSphericalToCartesian(radius_, lonlat, xyz); |
| 28 |
#endif |
| 29 |
} |
| 30 |
void GeometrySphere::xyz2lonlat(const Point3& xyz, Point2& lonlat) const { |
| 31 |
Sphere::convertCartesianToSpherical(radius_, xyz, lonlat); |
| 32 |
} |
| 33 |
|
| 34 |
/// @brief Calculate great-cricle course between points |
| 35 |
/// |
| 36 |
/// @details Calculates the direction (clockwise from north) of a great-circle |
| 37 |
/// arc between lonLat1 and lonLat2. Returns the direction of the arc |
| 38 |
/// at lonLat1 (first) and lonLat2 (second). Angle is normalised to the |
| 39 |
/// range of atan2 (usually (-180, 180]). All input and output values |
| 40 |
/// are in units of degrees. |
| 41 |
/// @ref https://en.wikipedia.org/wiki/Great-circle_navigation |
| 42 |
/// |
| 43 |
std::pair<double, double> greatCircleCourse(const Point2& lonLat1, |
| 44 |
const Point2& lonLat2) { |
| 45 |
if (lonLat1 == lonLat2) { |
| 46 |
return std::make_pair(0., 0.); |
| 47 |
} |
| 48 |
|
| 49 |
const auto lambda1 = lonLat1[0] * util::Constants::degreesToRadians(); |
| 50 |
const auto lambda2 = lonLat2[0] * util::Constants::degreesToRadians(); |
| 51 |
const auto phi1 = lonLat1[1] * util::Constants::degreesToRadians(); |
| 52 |
const auto phi2 = lonLat2[1] * util::Constants::degreesToRadians(); |
| 53 |
|
| 54 |
const auto sinLambda12 = std::sin(lambda2 - lambda1); |
| 55 |
const auto cosLambda12 = std::cos(lambda2 - lambda1); |
| 56 |
const auto sinPhi1 = std::sin(phi1); |
| 57 |
const auto sinPhi2 = std::sin(phi2); |
| 58 |
const auto cosPhi1 = std::cos(phi1); |
| 59 |
const auto cosPhi2 = std::cos(phi2); |
| 60 |
|
| 61 |
const auto alpha1 = |
| 62 |
std::atan2(cosPhi2 * sinLambda12, |
| 63 |
cosPhi1 * sinPhi2 - sinPhi1 * cosPhi2 * cosLambda12); |
| 64 |
|
| 65 |
const auto alpha2 = |
| 66 |
std::atan2(cosPhi1 * sinLambda12, |
| 67 |
-cosPhi2 * sinPhi1 + sinPhi2 * cosPhi1 * cosLambda12); |
| 68 |
|
| 69 |
return std::make_pair(alpha1 * util::Constants::radiansToDegrees(), |
| 70 |
alpha2 * util::Constants::radiansToDegrees()); |
| 71 |
}; |
| 72 |
|
| 73 |
} // namespace detail |
| 74 |
} // namespace geometry |
| 75 |
|
| 76 |
extern "C" { |
| 77 |
// ------------------------------------------------------------------ |
| 78 |
// C wrapper interfaces to C++ routines |
| 79 |
Geometry::Implementation* atlas__Geometry__new_name(const char* name) { |
| 80 |
Geometry::Implementation* geometry; |
| 81 |
{ |
| 82 |
Geometry handle{std::string{name}}; |
| 83 |
geometry = handle.get(); |
| 84 |
geometry->attach(); |
| 85 |
} |
| 86 |
geometry->detach(); |
| 87 |
return geometry; |
| 88 |
} |
| 89 |
geometry::detail::GeometryBase* atlas__Geometry__new_radius( |
| 90 |
const double radius) { |
| 91 |
Geometry::Implementation* geometry; |
| 92 |
{ |
| 93 |
Geometry handle{radius}; |
| 94 |
geometry = handle.get(); |
| 95 |
geometry->attach(); |
| 96 |
} |
| 97 |
geometry->detach(); |
| 98 |
return geometry; |
| 99 |
} |
| 100 |
void atlas__Geometry__delete(Geometry::Implementation* This) { |
| 101 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Geometry"); |
| 102 |
delete This; |
| 103 |
} |
| 104 |
void atlas__Geometry__xyz2lonlat(Geometry::Implementation* This, const double x, |
| 105 |
const double y, const double z, double& lon, |
| 106 |
double& lat) { |
| 107 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Geometry"); |
| 108 |
PointLonLat lonlat; |
| 109 |
This->xyz2lonlat(PointXYZ{x, y, z}, lonlat); |
| 110 |
lon = lonlat.lon(); |
| 111 |
lat = lonlat.lat(); |
| 112 |
} |
| 113 |
void atlas__Geometry__lonlat2xyz(Geometry::Implementation* This, |
| 114 |
const double lon, const double lat, double& x, |
| 115 |
double& y, double& z) { |
| 116 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Geometry"); |
| 117 |
PointXYZ xyz; |
| 118 |
This->lonlat2xyz(PointLonLat{lon, lat}, xyz); |
| 119 |
x = xyz.x(); |
| 120 |
y = xyz.y(); |
| 121 |
z = xyz.z(); |
| 122 |
} |
| 123 |
double atlas__Geometry__distance_lonlat(Geometry::Implementation* This, |
| 124 |
const double lon1, const double lat1, |
| 125 |
const double lon2, const double lat2) { |
| 126 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Geometry"); |
| 127 |
return This->distance(PointLonLat{lon1, lat1}, PointLonLat{lon2, lat2}); |
| 128 |
} |
| 129 |
double atlas__Geometry__distance_xyz(Geometry::Implementation* This, |
| 130 |
const double x1, const double y1, |
| 131 |
const double z1, const double x2, |
| 132 |
const double y2, const double z2) { |
| 133 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Geometry"); |
| 134 |
return This->distance(PointXYZ{x1, y1, z1}, PointXYZ{x2, y2, z2}); |
| 135 |
} |
| 136 |
double atlas__Geometry__radius(Geometry::Implementation* This) { |
| 137 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Geometry"); |
| 138 |
return This->radius(); |
| 139 |
} |
| 140 |
double atlas__Geometry__area(Geometry::Implementation* This) { |
| 141 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Geometry"); |
| 142 |
return This->area(); |
| 143 |
} |
| 144 |
} |
| 145 |
// ------------------------------------------------------------------ |
| 146 |
|
| 147 |
} // namespace atlas |