vincenty_direct.hpp 6.5 KB

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