vincenty_inverse.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Boost.Geometry
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2018 Adam Wulkiewicz, Lodz, Poland.
  4. // This file was modified by Oracle on 2014, 2016, 2017.
  5. // Modifications copyright (c) 2014-2017 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_INVERSE_HPP
  11. #define BOOST_GEOMETRY_FORMULAS_VINCENTY_INVERSE_HPP
  12. #include <boost/math/constants/constants.hpp>
  13. #include <boost/geometry/core/radius.hpp>
  14. #include <boost/geometry/util/condition.hpp>
  15. #include <boost/geometry/util/math.hpp>
  16. #include <boost/geometry/formulas/differential_quantities.hpp>
  17. #include <boost/geometry/formulas/flattening.hpp>
  18. #include <boost/geometry/formulas/result_inverse.hpp>
  19. #ifndef BOOST_GEOMETRY_DETAIL_VINCENTY_MAX_STEPS
  20. #define BOOST_GEOMETRY_DETAIL_VINCENTY_MAX_STEPS 1000
  21. #endif
  22. namespace boost { namespace geometry { namespace formula
  23. {
  24. /*!
  25. \brief The solution of the inverse problem of geodesics on latlong coordinates, after Vincenty, 1975
  26. \author See
  27. - http://www.ngs.noaa.gov/PUBS_LIB/inverse.pdf
  28. - http://www.icsm.gov.au/gda/gda-v_2.4.pdf
  29. \author Adapted from various implementations to get it close to the original document
  30. - http://www.movable-type.co.uk/scripts/LatLongVincenty.html
  31. - http://exogen.case.edu/projects/geopy/source/geopy.distance.html
  32. - http://futureboy.homeip.net/fsp/colorize.fsp?fileName=navigation.frink
  33. */
  34. template <
  35. typename CT,
  36. bool EnableDistance,
  37. bool EnableAzimuth,
  38. bool EnableReverseAzimuth = false,
  39. bool EnableReducedLength = false,
  40. bool EnableGeodesicScale = false
  41. >
  42. struct vincenty_inverse
  43. {
  44. static const bool CalcQuantities = EnableReducedLength || EnableGeodesicScale;
  45. static const bool CalcAzimuths = EnableAzimuth || EnableReverseAzimuth || CalcQuantities;
  46. static const bool CalcFwdAzimuth = EnableAzimuth || CalcQuantities;
  47. static const bool CalcRevAzimuth = EnableReverseAzimuth || CalcQuantities;
  48. public:
  49. typedef result_inverse<CT> result_type;
  50. template <typename T1, typename T2, typename Spheroid>
  51. static inline result_type apply(T1 const& lon1,
  52. T1 const& lat1,
  53. T2 const& lon2,
  54. T2 const& lat2,
  55. Spheroid const& spheroid)
  56. {
  57. result_type result;
  58. if (math::equals(lat1, lat2) && math::equals(lon1, lon2))
  59. {
  60. return result;
  61. }
  62. CT const c0 = 0;
  63. CT const c1 = 1;
  64. CT const c2 = 2;
  65. CT const c3 = 3;
  66. CT const c4 = 4;
  67. CT const c16 = 16;
  68. CT const c_e_12 = CT(1e-12);
  69. CT const pi = geometry::math::pi<CT>();
  70. CT const two_pi = c2 * pi;
  71. // lambda: difference in longitude on an auxiliary sphere
  72. CT L = lon2 - lon1;
  73. CT lambda = L;
  74. if (L < -pi) L += two_pi;
  75. if (L > pi) L -= two_pi;
  76. CT const radius_a = CT(get_radius<0>(spheroid));
  77. CT const radius_b = CT(get_radius<2>(spheroid));
  78. CT const f = formula::flattening<CT>(spheroid);
  79. // U: reduced latitude, defined by tan U = (1-f) tan phi
  80. CT const one_min_f = c1 - f;
  81. CT const tan_U1 = one_min_f * tan(lat1); // above (1)
  82. CT const tan_U2 = one_min_f * tan(lat2); // above (1)
  83. // calculate sin U and cos U using trigonometric identities
  84. CT const temp_den_U1 = math::sqrt(c1 + math::sqr(tan_U1));
  85. CT const temp_den_U2 = math::sqrt(c1 + math::sqr(tan_U2));
  86. // cos = 1 / sqrt(1 + tan^2)
  87. CT const cos_U1 = c1 / temp_den_U1;
  88. CT const cos_U2 = c1 / temp_den_U2;
  89. // sin = tan / sqrt(1 + tan^2)
  90. // sin = tan * cos
  91. CT const sin_U1 = tan_U1 * cos_U1;
  92. CT const sin_U2 = tan_U2 * cos_U2;
  93. // calculate sin U and cos U directly
  94. //CT const U1 = atan(tan_U1);
  95. //CT const U2 = atan(tan_U2);
  96. //cos_U1 = cos(U1);
  97. //cos_U2 = cos(U2);
  98. //sin_U1 = tan_U1 * cos_U1; // sin(U1);
  99. //sin_U2 = tan_U2 * cos_U2; // sin(U2);
  100. CT previous_lambda;
  101. CT sin_lambda;
  102. CT cos_lambda;
  103. CT sin_sigma;
  104. CT sin_alpha;
  105. CT cos2_alpha;
  106. CT cos_2sigma_m;
  107. CT cos2_2sigma_m;
  108. CT sigma;
  109. int counter = 0; // robustness
  110. do
  111. {
  112. previous_lambda = lambda; // (13)
  113. sin_lambda = sin(lambda);
  114. cos_lambda = cos(lambda);
  115. sin_sigma = math::sqrt(math::sqr(cos_U2 * sin_lambda) + math::sqr(cos_U1 * sin_U2 - sin_U1 * cos_U2 * cos_lambda)); // (14)
  116. CT cos_sigma = sin_U1 * sin_U2 + cos_U1 * cos_U2 * cos_lambda; // (15)
  117. sin_alpha = cos_U1 * cos_U2 * sin_lambda / sin_sigma; // (17)
  118. cos2_alpha = c1 - math::sqr(sin_alpha);
  119. cos_2sigma_m = math::equals(cos2_alpha, c0) ? c0 : cos_sigma - c2 * sin_U1 * sin_U2 / cos2_alpha; // (18)
  120. cos2_2sigma_m = math::sqr(cos_2sigma_m);
  121. CT C = f/c16 * cos2_alpha * (c4 + f * (c4 - c3 * cos2_alpha)); // (10)
  122. sigma = atan2(sin_sigma, cos_sigma); // (16)
  123. lambda = L + (c1 - C) * f * sin_alpha *
  124. (sigma + C * sin_sigma * (cos_2sigma_m + C * cos_sigma * (-c1 + c2 * cos2_2sigma_m))); // (11)
  125. ++counter; // robustness
  126. } while ( geometry::math::abs(previous_lambda - lambda) > c_e_12
  127. && geometry::math::abs(lambda) < pi
  128. && counter < BOOST_GEOMETRY_DETAIL_VINCENTY_MAX_STEPS ); // robustness
  129. if ( BOOST_GEOMETRY_CONDITION(EnableDistance) )
  130. {
  131. // Some types cannot divide by doubles
  132. CT const c6 = 6;
  133. CT const c47 = 47;
  134. CT const c74 = 74;
  135. CT const c128 = 128;
  136. CT const c256 = 256;
  137. CT const c175 = 175;
  138. CT const c320 = 320;
  139. CT const c768 = 768;
  140. CT const c1024 = 1024;
  141. CT const c4096 = 4096;
  142. CT const c16384 = 16384;
  143. //CT sqr_u = cos2_alpha * (math::sqr(radius_a) - math::sqr(radius_b)) / math::sqr(radius_b); // above (1)
  144. CT sqr_u = cos2_alpha * ( math::sqr(radius_a / radius_b) - c1 ); // above (1)
  145. CT A = c1 + sqr_u/c16384 * (c4096 + sqr_u * (-c768 + sqr_u * (c320 - c175 * sqr_u))); // (3)
  146. CT B = sqr_u/c1024 * (c256 + sqr_u * ( -c128 + sqr_u * (c74 - c47 * sqr_u))); // (4)
  147. CT const cos_sigma = cos(sigma);
  148. CT const sin2_sigma = math::sqr(sin_sigma);
  149. CT delta_sigma = B * sin_sigma * (cos_2sigma_m + (B/c4) * (cos_sigma* (-c1 + c2 * cos2_2sigma_m)
  150. - (B/c6) * cos_2sigma_m * (-c3 + c4 * sin2_sigma) * (-c3 + c4 * cos2_2sigma_m))); // (6)
  151. result.distance = radius_b * A * (sigma - delta_sigma); // (19)
  152. }
  153. if ( BOOST_GEOMETRY_CONDITION(CalcAzimuths) )
  154. {
  155. if (BOOST_GEOMETRY_CONDITION(CalcFwdAzimuth))
  156. {
  157. result.azimuth = atan2(cos_U2 * sin_lambda, cos_U1 * sin_U2 - sin_U1 * cos_U2 * cos_lambda); // (20)
  158. }
  159. if (BOOST_GEOMETRY_CONDITION(CalcRevAzimuth))
  160. {
  161. result.reverse_azimuth = atan2(cos_U1 * sin_lambda, -sin_U1 * cos_U2 + cos_U1 * sin_U2 * cos_lambda); // (21)
  162. }
  163. }
  164. if (BOOST_GEOMETRY_CONDITION(CalcQuantities))
  165. {
  166. typedef differential_quantities<CT, EnableReducedLength, EnableGeodesicScale, 2> quantities;
  167. quantities::apply(lon1, lat1, lon2, lat2,
  168. result.azimuth, result.reverse_azimuth,
  169. radius_b, f,
  170. result.reduced_length, result.geodesic_scale);
  171. }
  172. return result;
  173. }
  174. };
  175. }}} // namespace boost::geometry::formula
  176. #endif // BOOST_GEOMETRY_FORMULAS_VINCENTY_INVERSE_HPP