karney_direct.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Boost.Geometry
  2. // Copyright (c) 2018 Adeel Ahmad, Islamabad, Pakistan.
  3. // Copyright (c) 2023 Adam Wulkiewicz, Lodz, Poland.
  4. // Contributed and/or modified by Adeel Ahmad,
  5. // as part of Google Summer of Code 2018 program.
  6. // This file was modified by Oracle on 2018-2022.
  7. // Modifications copyright (c) 2018-2022 Oracle and/or its affiliates.
  8. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  9. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  10. // Use, modification and distribution is subject to the Boost Software License,
  11. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. // This file is converted from GeographicLib, https://geographiclib.sourceforge.io
  14. // GeographicLib is originally written by Charles Karney.
  15. // Author: Charles Karney (2008-2017)
  16. // Last updated version of GeographicLib: 1.49
  17. // Original copyright notice:
  18. // Copyright (c) Charles Karney (2008-2017) <charles@karney.com> and licensed
  19. // under the MIT/X11 License. For more information, see
  20. // https://geographiclib.sourceforge.io
  21. #ifndef BOOST_GEOMETRY_FORMULAS_KARNEY_DIRECT_HPP
  22. #define BOOST_GEOMETRY_FORMULAS_KARNEY_DIRECT_HPP
  23. #include <boost/math/constants/constants.hpp>
  24. #include <boost/math/special_functions/hypot.hpp>
  25. #include <boost/geometry/formulas/flattening.hpp>
  26. #include <boost/geometry/formulas/result_direct.hpp>
  27. #include <boost/geometry/util/constexpr.hpp>
  28. #include <boost/geometry/util/math.hpp>
  29. #include <boost/geometry/util/normalize_spheroidal_coordinates.hpp>
  30. #include <boost/geometry/util/series_expansion.hpp>
  31. namespace boost { namespace geometry { namespace formula
  32. {
  33. namespace se = series_expansion;
  34. /*!
  35. \brief The solution of the direct problem of geodesics on latlong coordinates,
  36. after Karney (2011).
  37. \author See
  38. - Charles F.F Karney, Algorithms for geodesics, 2011
  39. https://arxiv.org/pdf/1109.4448.pdf
  40. */
  41. template <
  42. typename CT,
  43. bool EnableCoordinates = true,
  44. bool EnableReverseAzimuth = false,
  45. bool EnableReducedLength = false,
  46. bool EnableGeodesicScale = false,
  47. size_t SeriesOrder = 8
  48. >
  49. class karney_direct
  50. {
  51. static const bool CalcQuantities = EnableReducedLength || EnableGeodesicScale;
  52. static const bool CalcCoordinates = EnableCoordinates || CalcQuantities;
  53. static const bool CalcRevAzimuth = EnableReverseAzimuth || CalcCoordinates || CalcQuantities;
  54. public:
  55. typedef result_direct<CT> result_type;
  56. template <typename T, typename Dist, typename Azi, typename Spheroid>
  57. static inline result_type apply(T const& lo1,
  58. T const& la1,
  59. Dist const& distance,
  60. Azi const& azimuth12,
  61. Spheroid const& spheroid)
  62. {
  63. result_type result;
  64. CT lon1 = lo1 * math::r2d<CT>();
  65. CT const lat1 = la1 * math::r2d<CT>();
  66. Azi azi12 = azimuth12 * math::r2d<CT>();
  67. math::normalize_azimuth<degree, Azi>(azi12);
  68. CT const c0 = 0;
  69. CT const c1 = 1;
  70. CT const c2 = 2;
  71. CT const b = CT(get_radius<2>(spheroid));
  72. CT const f = formula::flattening<CT>(spheroid);
  73. CT const one_minus_f = c1 - f;
  74. CT const two_minus_f = c2 - f;
  75. CT const n = f / two_minus_f;
  76. CT const e2 = f * two_minus_f;
  77. CT const ep2 = e2 / math::sqr(one_minus_f);
  78. CT sin_alpha1, cos_alpha1;
  79. math::sin_cos_degrees<CT>(azi12, sin_alpha1, cos_alpha1);
  80. // Find the reduced latitude.
  81. CT sin_beta1, cos_beta1;
  82. math::sin_cos_degrees<CT>(lat1, sin_beta1, cos_beta1);
  83. sin_beta1 *= one_minus_f;
  84. math::normalize_unit_vector<CT>(sin_beta1, cos_beta1);
  85. cos_beta1 = (std::max)(c0, cos_beta1);
  86. // Obtain alpha 0 by solving the spherical triangle.
  87. CT const sin_alpha0 = sin_alpha1 * cos_beta1;
  88. CT const cos_alpha0 = boost::math::hypot(cos_alpha1, sin_alpha1 * sin_beta1);
  89. CT const k2 = math::sqr(cos_alpha0) * ep2;
  90. CT const epsilon = k2 / (c2 * (c1 + math::sqrt(c1 + k2)) + k2);
  91. // Find the coefficients for A1 by computing the
  92. // series expansion using Horner scehme.
  93. CT const expansion_A1 = se::evaluate_A1<SeriesOrder>(epsilon);
  94. // Index zero element of coeffs_C1 is unused.
  95. se::coeffs_C1<SeriesOrder, CT> const coeffs_C1(epsilon);
  96. // Tau is an integration variable.
  97. CT const tau12 = distance / (b * (c1 + expansion_A1));
  98. CT const sin_tau12 = sin(tau12);
  99. CT const cos_tau12 = cos(tau12);
  100. CT sin_sigma1 = sin_beta1;
  101. CT sin_omega1 = sin_alpha0 * sin_beta1;
  102. CT cos_sigma1, cos_omega1;
  103. cos_sigma1 = cos_omega1 = sin_beta1 != c0 || cos_alpha1 != c0 ? cos_beta1 * cos_alpha1 : c1;
  104. math::normalize_unit_vector<CT>(sin_sigma1, cos_sigma1);
  105. CT const B11 = se::sin_cos_series(sin_sigma1, cos_sigma1, coeffs_C1);
  106. CT const sin_B11 = sin(B11);
  107. CT const cos_B11 = cos(B11);
  108. CT const sin_tau1 = sin_sigma1 * cos_B11 + cos_sigma1 * sin_B11;
  109. CT const cos_tau1 = cos_sigma1 * cos_B11 - sin_sigma1 * sin_B11;
  110. // Index zero element of coeffs_C1p is unused.
  111. se::coeffs_C1p<SeriesOrder, CT> const coeffs_C1p(epsilon);
  112. CT const B12 = - se::sin_cos_series(sin_tau1 * cos_tau12 + cos_tau1 * sin_tau12,
  113. cos_tau1 * cos_tau12 - sin_tau1 * sin_tau12,
  114. coeffs_C1p);
  115. CT const sigma12 = tau12 - (B12 - B11);
  116. CT const sin_sigma12 = sin(sigma12);
  117. CT const cos_sigma12 = cos(sigma12);
  118. CT const sin_sigma2 = sin_sigma1 * cos_sigma12 + cos_sigma1 * sin_sigma12;
  119. CT const cos_sigma2 = cos_sigma1 * cos_sigma12 - sin_sigma1 * sin_sigma12;
  120. if BOOST_GEOMETRY_CONSTEXPR (CalcRevAzimuth)
  121. {
  122. CT const sin_alpha2 = sin_alpha0;
  123. CT const cos_alpha2 = cos_alpha0 * cos_sigma2;
  124. result.reverse_azimuth = atan2(sin_alpha2, cos_alpha2);
  125. }
  126. if BOOST_GEOMETRY_CONSTEXPR (CalcCoordinates)
  127. {
  128. // Find the latitude at the second point.
  129. CT const sin_beta2 = cos_alpha0 * sin_sigma2;
  130. CT const cos_beta2 = boost::math::hypot(sin_alpha0, cos_alpha0 * cos_sigma2);
  131. result.lat2 = atan2(sin_beta2, one_minus_f * cos_beta2);
  132. // Find the longitude at the second point.
  133. CT const sin_omega2 = sin_alpha0 * sin_sigma2;
  134. CT const cos_omega2 = cos_sigma2;
  135. CT const omega12 = atan2(sin_omega2 * cos_omega1 - cos_omega2 * sin_omega1,
  136. cos_omega2 * cos_omega1 + sin_omega2 * sin_omega1);
  137. se::coeffs_A3<SeriesOrder, CT> const coeffs_A3(n);
  138. CT const A3 = math::horner_evaluate(epsilon, coeffs_A3.begin(), coeffs_A3.end());
  139. CT const A3c = -f * sin_alpha0 * A3;
  140. se::coeffs_C3<SeriesOrder, CT> const coeffs_C3(n, epsilon);
  141. CT const B31 = se::sin_cos_series(sin_sigma1, cos_sigma1, coeffs_C3);
  142. CT const sin_cos_res = se::sin_cos_series(sin_sigma2, cos_sigma2, coeffs_C3);
  143. CT const lam12 = omega12 + A3c * (sigma12 + (sin_cos_res - B31));
  144. // Convert to degrees to get the longitudinal difference.
  145. CT lon12 = lam12 * math::r2d<CT>();
  146. // Add the longitude at first point to the longitudinal
  147. // difference and normalize the result.
  148. math::normalize_longitude<degree, CT>(lon1);
  149. math::normalize_longitude<degree, CT>(lon12);
  150. result.lon2 = lon1 + lon12;
  151. // For longitudes close to the antimeridian the result can be out
  152. // of range. Therefore normalize.
  153. // In other formulas this has to be done at the end because
  154. // otherwise differential quantities are calculated incorrectly.
  155. // But here it's ok since result.lon2 is not used after this point.
  156. math::normalize_longitude<degree, CT>(result.lon2);
  157. result.lon2 *= math::d2r<CT>();
  158. }
  159. if BOOST_GEOMETRY_CONSTEXPR (CalcQuantities)
  160. {
  161. // Evaluate the coefficients for C2.
  162. // Index zero element of coeffs_C2 is unused.
  163. se::coeffs_C2<SeriesOrder, CT> const coeffs_C2(epsilon);
  164. CT const B21 = se::sin_cos_series(sin_sigma1, cos_sigma1, coeffs_C2);
  165. CT const B22 = se::sin_cos_series(sin_sigma2, cos_sigma2, coeffs_C2);
  166. // Find the coefficients for A2 by computing the
  167. // series expansion using Horner scehme.
  168. CT const expansion_A2 = se::evaluate_A2<SeriesOrder>(epsilon);
  169. CT const AB1 = (c1 + expansion_A1) * (B12 - B11);
  170. CT const AB2 = (c1 + expansion_A2) * (B22 - B21);
  171. CT const J12 = (expansion_A1 - expansion_A2) * sigma12 + (AB1 - AB2);
  172. CT const dn1 = math::sqrt(c1 + ep2 * math::sqr(sin_beta1));
  173. CT const dn2 = math::sqrt(c1 + k2 * math::sqr(sin_sigma2));
  174. // Find the reduced length.
  175. result.reduced_length = b * ((dn2 * (cos_sigma1 * sin_sigma2) -
  176. dn1 * (sin_sigma1 * cos_sigma2)) -
  177. cos_sigma1 * cos_sigma2 * J12);
  178. // Find the geodesic scale.
  179. CT const t = k2 * (sin_sigma2 - sin_sigma1) * (sin_sigma2 + sin_sigma1) / (dn1 + dn2);
  180. result.geodesic_scale = cos_sigma12 + (t * sin_sigma2 - cos_sigma2 * J12) *
  181. sin_sigma1 / dn1;
  182. }
  183. return result;
  184. }
  185. };
  186. }}} // namespace boost::geometry::formula
  187. #endif // BOOST_GEOMETRY_FORMULAS_KARNEY_DIRECT_HPP