meridian_inverse.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Boost.Geometry
  2. // Copyright (c) 2017-2018 Oracle and/or its affiliates.
  3. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_GEOMETRY_FORMULAS_MERIDIAN_INVERSE_HPP
  8. #define BOOST_GEOMETRY_FORMULAS_MERIDIAN_INVERSE_HPP
  9. #include <boost/math/constants/constants.hpp>
  10. #include <boost/geometry/core/radius.hpp>
  11. #include <boost/geometry/util/condition.hpp>
  12. #include <boost/geometry/util/math.hpp>
  13. #include <boost/geometry/util/normalize_spheroidal_coordinates.hpp>
  14. #include <boost/geometry/formulas/flattening.hpp>
  15. #include <boost/geometry/formulas/meridian_segment.hpp>
  16. namespace boost { namespace geometry { namespace formula
  17. {
  18. /*!
  19. \brief Compute the arc length of an ellipse.
  20. */
  21. template <typename CT, unsigned int Order = 1>
  22. class meridian_inverse
  23. {
  24. public :
  25. struct result
  26. {
  27. result()
  28. : distance(0)
  29. , meridian(false)
  30. {}
  31. CT distance;
  32. bool meridian;
  33. };
  34. template <typename T>
  35. static bool meridian_not_crossing_pole(T lat1, T lat2, CT diff)
  36. {
  37. CT half_pi = math::pi<CT>()/CT(2);
  38. return math::equals(diff, CT(0)) ||
  39. (math::equals(lat2, half_pi) && math::equals(lat1, -half_pi));
  40. }
  41. static bool meridian_crossing_pole(CT diff)
  42. {
  43. return math::equals(math::abs(diff), math::pi<CT>());
  44. }
  45. template <typename T, typename Spheroid>
  46. static CT meridian_not_crossing_pole_dist(T lat1, T lat2, Spheroid const& spheroid)
  47. {
  48. return math::abs(apply(lat2, spheroid) - apply(lat1, spheroid));
  49. }
  50. template <typename T, typename Spheroid>
  51. static CT meridian_crossing_pole_dist(T lat1, T lat2, Spheroid const& spheroid)
  52. {
  53. CT c0 = 0;
  54. CT half_pi = math::pi<CT>()/CT(2);
  55. CT lat_sign = 1;
  56. if (lat1+lat2 < c0)
  57. {
  58. lat_sign = CT(-1);
  59. }
  60. return math::abs(lat_sign * CT(2) * apply(half_pi, spheroid)
  61. - apply(lat1, spheroid) - apply(lat2, spheroid));
  62. }
  63. template <typename T, typename Spheroid>
  64. static result apply(T lon1, T lat1, T lon2, T lat2, Spheroid const& spheroid)
  65. {
  66. result res;
  67. CT diff = geometry::math::longitude_distance_signed<geometry::radian>(lon1, lon2);
  68. if (lat1 > lat2)
  69. {
  70. std::swap(lat1, lat2);
  71. }
  72. if ( meridian_not_crossing_pole(lat1, lat2, diff) )
  73. {
  74. res.distance = meridian_not_crossing_pole_dist(lat1, lat2, spheroid);
  75. res.meridian = true;
  76. }
  77. else if ( meridian_crossing_pole(diff) )
  78. {
  79. res.distance = meridian_crossing_pole_dist(lat1, lat2, spheroid);
  80. res.meridian = true;
  81. }
  82. return res;
  83. }
  84. // Distance computation on meridians using series approximations
  85. // to elliptic integrals. Formula to compute distance from lattitude 0 to lat
  86. // https://en.wikipedia.org/wiki/Meridian_arc
  87. // latitudes are assumed to be in radians and in [-pi/2,pi/2]
  88. template <typename T, typename Spheroid>
  89. static CT apply(T lat, Spheroid const& spheroid)
  90. {
  91. CT const a = get_radius<0>(spheroid);
  92. CT const f = formula::flattening<CT>(spheroid);
  93. CT n = f / (CT(2) - f);
  94. CT M = a/(1+n);
  95. CT C0 = 1;
  96. if (Order == 0)
  97. {
  98. return M * C0 * lat;
  99. }
  100. CT C2 = -1.5 * n;
  101. if (Order == 1)
  102. {
  103. return M * (C0 * lat + C2 * sin(2*lat));
  104. }
  105. CT n2 = n * n;
  106. C0 += .25 * n2;
  107. CT C4 = 0.9375 * n2;
  108. if (Order == 2)
  109. {
  110. return M * (C0 * lat + C2 * sin(2*lat) + C4 * sin(4*lat));
  111. }
  112. CT n3 = n2 * n;
  113. C2 += 0.1875 * n3;
  114. CT C6 = -0.729166667 * n3;
  115. if (Order == 3)
  116. {
  117. return M * (C0 * lat + C2 * sin(2*lat) + C4 * sin(4*lat)
  118. + C6 * sin(6*lat));
  119. }
  120. CT n4 = n2 * n2;
  121. C4 -= 0.234375 * n4;
  122. CT C8 = 0.615234375 * n4;
  123. if (Order == 4)
  124. {
  125. return M * (C0 * lat + C2 * sin(2*lat) + C4 * sin(4*lat)
  126. + C6 * sin(6*lat) + C8 * sin(8*lat));
  127. }
  128. CT n5 = n4 * n;
  129. C6 += 0.227864583 * n5;
  130. CT C10 = -0.54140625 * n5;
  131. // Order 5 or higher
  132. return M * (C0 * lat + C2 * sin(2*lat) + C4 * sin(4*lat)
  133. + C6 * sin(6*lat) + C8 * sin(8*lat) + C10 * sin(10*lat));
  134. }
  135. };
  136. }}} // namespace boost::geometry::formula
  137. #endif // BOOST_GEOMETRY_FORMULAS_MERIDIAN_INVERSE_HPP