meridian_inverse.hpp 4.8 KB

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