vincenty_direct.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Boost.Geometry
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2023 Adam Wulkiewicz, Lodz, Poland.
  4. // This file was modified by Oracle on 2014-2020.
  5. // Modifications copyright (c) 2014-2020 Oracle and/or its affiliates.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_FORMULAS_VINCENTY_DIRECT_HPP
  11. #define BOOST_GEOMETRY_FORMULAS_VINCENTY_DIRECT_HPP
  12. #include <boost/math/constants/constants.hpp>
  13. #include <boost/geometry/core/radius.hpp>
  14. #include <boost/geometry/util/constexpr.hpp>
  15. #include <boost/geometry/util/math.hpp>
  16. #include <boost/geometry/util/normalize_spheroidal_coordinates.hpp>
  17. #include <boost/geometry/formulas/differential_quantities.hpp>
  18. #include <boost/geometry/formulas/flattening.hpp>
  19. #include <boost/geometry/formulas/result_direct.hpp>
  20. #ifndef BOOST_GEOMETRY_DETAIL_VINCENTY_MAX_STEPS
  21. #define BOOST_GEOMETRY_DETAIL_VINCENTY_MAX_STEPS 1000
  22. #endif
  23. namespace boost { namespace geometry { namespace formula
  24. {
  25. /*!
  26. \brief The solution of the direct problem of geodesics on latlong coordinates, after Vincenty, 1975
  27. \author See
  28. - http://www.ngs.noaa.gov/PUBS_LIB/inverse.pdf
  29. - http://www.icsm.gov.au/gda/gdav2.3.pdf
  30. \author Adapted from various implementations to get it close to the original document
  31. - http://www.movable-type.co.uk/scripts/LatLongVincenty.html
  32. - http://exogen.case.edu/projects/geopy/source/geopy.distance.html
  33. - http://futureboy.homeip.net/fsp/colorize.fsp?fileName=navigation.frink
  34. */
  35. template <
  36. typename CT,
  37. bool EnableCoordinates = true,
  38. bool EnableReverseAzimuth = false,
  39. bool EnableReducedLength = false,
  40. bool EnableGeodesicScale = false
  41. >
  42. class vincenty_direct
  43. {
  44. static const bool CalcQuantities = EnableReducedLength || EnableGeodesicScale;
  45. static const bool CalcCoordinates = EnableCoordinates || CalcQuantities;
  46. static const bool CalcRevAzimuth = EnableReverseAzimuth || CalcQuantities;
  47. public:
  48. typedef result_direct<CT> result_type;
  49. template <typename T, typename Dist, typename Azi, typename Spheroid>
  50. static inline result_type apply(T const& lo1,
  51. T const& la1,
  52. Dist const& distance,
  53. Azi const& azimuth12,
  54. Spheroid const& spheroid)
  55. {
  56. result_type result;
  57. CT const lon1 = lo1;
  58. CT const lat1 = la1;
  59. CT const radius_a = CT(get_radius<0>(spheroid));
  60. CT const radius_b = CT(get_radius<2>(spheroid));
  61. CT const flattening = formula::flattening<CT>(spheroid);
  62. CT const sin_azimuth12 = sin(azimuth12);
  63. CT const cos_azimuth12 = cos(azimuth12);
  64. // U: reduced latitude, defined by tan U = (1-f) tan phi
  65. CT const one_min_f = CT(1) - flattening;
  66. CT const tan_U1 = one_min_f * tan(lat1);
  67. CT const sigma1 = atan2(tan_U1, cos_azimuth12); // (1)
  68. // may be calculated from tan using 1 sqrt()
  69. CT const U1 = atan(tan_U1);
  70. CT const sin_U1 = sin(U1);
  71. CT const cos_U1 = cos(U1);
  72. CT const sin_alpha = cos_U1 * sin_azimuth12; // (2)
  73. CT const sin_alpha_sqr = math::sqr(sin_alpha);
  74. CT const cos_alpha_sqr = CT(1) - sin_alpha_sqr;
  75. CT const b_sqr = radius_b * radius_b;
  76. CT const u_sqr = cos_alpha_sqr * (radius_a * radius_a - b_sqr) / b_sqr;
  77. CT const A = CT(1) + (u_sqr/CT(16384)) * (CT(4096) + u_sqr*(CT(-768) + u_sqr*(CT(320) - u_sqr*CT(175)))); // (3)
  78. CT const B = (u_sqr/CT(1024))*(CT(256) + u_sqr*(CT(-128) + u_sqr*(CT(74) - u_sqr*CT(47)))); // (4)
  79. CT s_div_bA = distance / (radius_b * A);
  80. CT sigma = s_div_bA; // (7)
  81. CT previous_sigma;
  82. CT sin_sigma;
  83. CT cos_sigma;
  84. CT cos_2sigma_m;
  85. CT cos_2sigma_m_sqr;
  86. int counter = 0; // robustness
  87. do
  88. {
  89. previous_sigma = sigma;
  90. CT const two_sigma_m = CT(2) * sigma1 + sigma; // (5)
  91. sin_sigma = sin(sigma);
  92. cos_sigma = cos(sigma);
  93. CT const sin_sigma_sqr = math::sqr(sin_sigma);
  94. cos_2sigma_m = cos(two_sigma_m);
  95. cos_2sigma_m_sqr = math::sqr(cos_2sigma_m);
  96. CT const delta_sigma = B * sin_sigma * (cos_2sigma_m
  97. + (B/CT(4)) * ( cos_sigma * (CT(-1) + CT(2)*cos_2sigma_m_sqr)
  98. - (B/CT(6) * cos_2sigma_m * (CT(-3)+CT(4)*sin_sigma_sqr) * (CT(-3)+CT(4)*cos_2sigma_m_sqr)) )); // (6)
  99. sigma = s_div_bA + delta_sigma; // (7)
  100. ++counter; // robustness
  101. } while ( geometry::math::abs(previous_sigma - sigma) > CT(1e-12)
  102. //&& geometry::math::abs(sigma) < pi
  103. && counter < BOOST_GEOMETRY_DETAIL_VINCENTY_MAX_STEPS ); // robustness
  104. if BOOST_GEOMETRY_CONSTEXPR (CalcCoordinates)
  105. {
  106. result.lat2
  107. = atan2( sin_U1 * cos_sigma + cos_U1 * sin_sigma * cos_azimuth12,
  108. one_min_f * math::sqrt(sin_alpha_sqr + math::sqr(sin_U1 * sin_sigma - cos_U1 * cos_sigma * cos_azimuth12))); // (8)
  109. CT const lambda = atan2( sin_sigma * sin_azimuth12,
  110. cos_U1 * cos_sigma - sin_U1 * sin_sigma * cos_azimuth12); // (9)
  111. CT const C = (flattening/CT(16)) * cos_alpha_sqr * ( CT(4) + flattening * ( CT(4) - CT(3) * cos_alpha_sqr ) ); // (10)
  112. CT const L = lambda - (CT(1) - C) * flattening * sin_alpha
  113. * ( sigma + C * sin_sigma * ( cos_2sigma_m + C * cos_sigma * ( CT(-1) + CT(2) * cos_2sigma_m_sqr ) ) ); // (11)
  114. result.lon2 = lon1 + L;
  115. }
  116. if BOOST_GEOMETRY_CONSTEXPR (CalcRevAzimuth)
  117. {
  118. result.reverse_azimuth
  119. = atan2(sin_alpha, -sin_U1 * sin_sigma + cos_U1 * cos_sigma * cos_azimuth12); // (12)
  120. }
  121. if BOOST_GEOMETRY_CONSTEXPR (CalcQuantities)
  122. {
  123. typedef differential_quantities<CT, EnableReducedLength, EnableGeodesicScale, 2> quantities;
  124. quantities::apply(lon1, lat1, result.lon2, result.lat2,
  125. azimuth12, result.reverse_azimuth,
  126. radius_b, flattening,
  127. result.reduced_length, result.geodesic_scale);
  128. }
  129. if BOOST_GEOMETRY_CONSTEXPR (CalcCoordinates)
  130. {
  131. // For longitudes close to the antimeridian the result can be out
  132. // of range. Therefore normalize.
  133. // It has to be done at the end because otherwise differential
  134. // quantities are calculated incorrectly.
  135. math::detail::normalize_angle_cond<radian>(result.lon2);
  136. }
  137. return result;
  138. }
  139. };
  140. }}} // namespace boost::geometry::formula
  141. #endif // BOOST_GEOMETRY_FORMULAS_VINCENTY_DIRECT_HPP