differential_quantities.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // Boost.Geometry
  2. // Copyright (c) 2016-2019 Oracle and/or its affiliates.
  3. // Contributed and/or modified by Adam Wulkiewicz, 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_INVERSE_DIFFERENTIAL_QUANTITIES_HPP
  8. #define BOOST_GEOMETRY_FORMULAS_INVERSE_DIFFERENTIAL_QUANTITIES_HPP
  9. #include <boost/geometry/core/assert.hpp>
  10. #include <boost/geometry/util/condition.hpp>
  11. #include <boost/geometry/util/math.hpp>
  12. namespace boost { namespace geometry { namespace formula
  13. {
  14. /*!
  15. \brief The solution of a part of the inverse problem - differential quantities.
  16. \author See
  17. - Charles F.F Karney, Algorithms for geodesics, 2011
  18. https://arxiv.org/pdf/1109.4448.pdf
  19. */
  20. template <
  21. typename CT,
  22. bool EnableReducedLength,
  23. bool EnableGeodesicScale,
  24. unsigned int Order = 2,
  25. bool ApproxF = true
  26. >
  27. class differential_quantities
  28. {
  29. public:
  30. static inline void apply(CT const& lon1, CT const& lat1,
  31. CT const& lon2, CT const& lat2,
  32. CT const& azimuth, CT const& reverse_azimuth,
  33. CT const& b, CT const& f,
  34. CT & reduced_length, CT & geodesic_scale)
  35. {
  36. CT const dlon = lon2 - lon1;
  37. CT const sin_lat1 = sin(lat1);
  38. CT const cos_lat1 = cos(lat1);
  39. CT const sin_lat2 = sin(lat2);
  40. CT const cos_lat2 = cos(lat2);
  41. apply(dlon, sin_lat1, cos_lat1, sin_lat2, cos_lat2,
  42. azimuth, reverse_azimuth,
  43. b, f,
  44. reduced_length, geodesic_scale);
  45. }
  46. static inline void apply(CT const& dlon,
  47. CT const& sin_lat1, CT const& cos_lat1,
  48. CT const& sin_lat2, CT const& cos_lat2,
  49. CT const& azimuth, CT const& reverse_azimuth,
  50. CT const& b, CT const& f,
  51. CT & reduced_length, CT & geodesic_scale)
  52. {
  53. CT const c0 = 0;
  54. CT const c1 = 1;
  55. CT const one_minus_f = c1 - f;
  56. CT sin_bet1 = one_minus_f * sin_lat1;
  57. CT sin_bet2 = one_minus_f * sin_lat2;
  58. // equator
  59. if (math::equals(sin_bet1, c0) && math::equals(sin_bet2, c0))
  60. {
  61. CT const sig_12 = dlon / one_minus_f;
  62. if (BOOST_GEOMETRY_CONDITION(EnableReducedLength))
  63. {
  64. BOOST_GEOMETRY_ASSERT((-math::pi<CT>() <= azimuth && azimuth <= math::pi<CT>()));
  65. int azi_sign = math::sign(azimuth) >= 0 ? 1 : -1; // for antipodal
  66. CT m12 = azi_sign * sin(sig_12) * b;
  67. reduced_length = m12;
  68. }
  69. if (BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
  70. {
  71. CT M12 = cos(sig_12);
  72. geodesic_scale = M12;
  73. }
  74. }
  75. else
  76. {
  77. CT const c2 = 2;
  78. CT const e2 = f * (c2 - f);
  79. CT const ep2 = e2 / math::sqr(one_minus_f);
  80. CT const sin_alp1 = sin(azimuth);
  81. CT const cos_alp1 = cos(azimuth);
  82. //CT const sin_alp2 = sin(reverse_azimuth);
  83. CT const cos_alp2 = cos(reverse_azimuth);
  84. CT cos_bet1 = cos_lat1;
  85. CT cos_bet2 = cos_lat2;
  86. normalize(sin_bet1, cos_bet1);
  87. normalize(sin_bet2, cos_bet2);
  88. CT sin_sig1 = sin_bet1;
  89. CT cos_sig1 = cos_alp1 * cos_bet1;
  90. CT sin_sig2 = sin_bet2;
  91. CT cos_sig2 = cos_alp2 * cos_bet2;
  92. normalize(sin_sig1, cos_sig1);
  93. normalize(sin_sig2, cos_sig2);
  94. CT const sin_alp0 = sin_alp1 * cos_bet1;
  95. CT const cos_alp0_sqr = c1 - math::sqr(sin_alp0);
  96. CT const J12 = BOOST_GEOMETRY_CONDITION(ApproxF) ?
  97. J12_f(sin_sig1, cos_sig1, sin_sig2, cos_sig2, cos_alp0_sqr, f) :
  98. J12_ep_sqr(sin_sig1, cos_sig1, sin_sig2, cos_sig2, cos_alp0_sqr, ep2) ;
  99. CT const dn1 = math::sqrt(c1 + ep2 * math::sqr(sin_bet1));
  100. CT const dn2 = math::sqrt(c1 + ep2 * math::sqr(sin_bet2));
  101. if (BOOST_GEOMETRY_CONDITION(EnableReducedLength))
  102. {
  103. CT const m12_b = dn2 * (cos_sig1 * sin_sig2)
  104. - dn1 * (sin_sig1 * cos_sig2)
  105. - cos_sig1 * cos_sig2 * J12;
  106. CT const m12 = m12_b * b;
  107. reduced_length = m12;
  108. }
  109. if (BOOST_GEOMETRY_CONDITION(EnableGeodesicScale))
  110. {
  111. CT const cos_sig12 = cos_sig1 * cos_sig2 + sin_sig1 * sin_sig2;
  112. CT const t = ep2 * (cos_bet1 - cos_bet2) * (cos_bet1 + cos_bet2) / (dn1 + dn2);
  113. CT const M12 = cos_sig12 + (t * sin_sig2 - cos_sig2 * J12) * sin_sig1 / dn1;
  114. geodesic_scale = M12;
  115. }
  116. }
  117. }
  118. private:
  119. /*! Approximation of J12, expanded into taylor series in f
  120. Maxima script:
  121. ep2: f * (2-f) / ((1-f)^2);
  122. k2: ca02 * ep2;
  123. assume(f < 1);
  124. assume(sig > 0);
  125. I1(sig):= integrate(sqrt(1 + k2 * sin(s)^2), s, 0, sig);
  126. I2(sig):= integrate(1/sqrt(1 + k2 * sin(s)^2), s, 0, sig);
  127. J(sig):= I1(sig) - I2(sig);
  128. S: taylor(J(sig), f, 0, 3);
  129. S1: factor( 2*integrate(sin(s)^2,s,0,sig)*ca02*f );
  130. S2: factor( ((integrate(-6*ca02^2*sin(s)^4+6*ca02*sin(s)^2,s,0,sig)+integrate(-2*ca02^2*sin(s)^4+6*ca02*sin(s)^2,s,0,sig))*f^2)/4 );
  131. S3: factor( ((integrate(30*ca02^3*sin(s)^6-54*ca02^2*sin(s)^4+24*ca02*sin(s)^2,s,0,sig)+integrate(6*ca02^3*sin(s)^6-18*ca02^2*sin(s)^4+24*ca02*sin(s)^2,s,0,sig))*f^3)/12 );
  132. */
  133. static inline CT J12_f(CT const& sin_sig1, CT const& cos_sig1,
  134. CT const& sin_sig2, CT const& cos_sig2,
  135. CT const& cos_alp0_sqr, CT const& f)
  136. {
  137. if (Order == 0)
  138. {
  139. return 0;
  140. }
  141. CT const c2 = 2;
  142. CT const sig_12 = atan2(cos_sig1 * sin_sig2 - sin_sig1 * cos_sig2,
  143. cos_sig1 * cos_sig2 + sin_sig1 * sin_sig2);
  144. CT const sin_2sig1 = c2 * cos_sig1 * sin_sig1; // sin(2sig1)
  145. CT const sin_2sig2 = c2 * cos_sig2 * sin_sig2; // sin(2sig2)
  146. CT const sin_2sig_12 = sin_2sig2 - sin_2sig1;
  147. CT const L1 = sig_12 - sin_2sig_12 / c2;
  148. if (Order == 1)
  149. {
  150. return cos_alp0_sqr * f * L1;
  151. }
  152. CT const sin_4sig1 = c2 * sin_2sig1 * (math::sqr(cos_sig1) - math::sqr(sin_sig1)); // sin(4sig1)
  153. CT const sin_4sig2 = c2 * sin_2sig2 * (math::sqr(cos_sig2) - math::sqr(sin_sig2)); // sin(4sig2)
  154. CT const sin_4sig_12 = sin_4sig2 - sin_4sig1;
  155. CT const c8 = 8;
  156. CT const c12 = 12;
  157. CT const c16 = 16;
  158. CT const c24 = 24;
  159. CT const L2 = -( cos_alp0_sqr * sin_4sig_12
  160. + (-c8 * cos_alp0_sqr + c12) * sin_2sig_12
  161. + (c12 * cos_alp0_sqr - c24) * sig_12)
  162. / c16;
  163. if (Order == 2)
  164. {
  165. return cos_alp0_sqr * f * (L1 + f * L2);
  166. }
  167. CT const c4 = 4;
  168. CT const c9 = 9;
  169. CT const c48 = 48;
  170. CT const c60 = 60;
  171. CT const c64 = 64;
  172. CT const c96 = 96;
  173. CT const c128 = 128;
  174. CT const c144 = 144;
  175. CT const cos_alp0_quad = math::sqr(cos_alp0_sqr);
  176. CT const sin3_2sig1 = math::sqr(sin_2sig1) * sin_2sig1;
  177. CT const sin3_2sig2 = math::sqr(sin_2sig2) * sin_2sig2;
  178. CT const sin3_2sig_12 = sin3_2sig2 - sin3_2sig1;
  179. CT const A = (c9 * cos_alp0_quad - c12 * cos_alp0_sqr) * sin_4sig_12;
  180. CT const B = c4 * cos_alp0_quad * sin3_2sig_12;
  181. CT const C = (-c48 * cos_alp0_quad + c96 * cos_alp0_sqr - c64) * sin_2sig_12;
  182. CT const D = (c60 * cos_alp0_quad - c144 * cos_alp0_sqr + c128) * sig_12;
  183. CT const L3 = (A + B + C + D) / c64;
  184. // Order 3 and higher
  185. return cos_alp0_sqr * f * (L1 + f * (L2 + f * L3));
  186. }
  187. /*! Approximation of J12, expanded into taylor series in e'^2
  188. Maxima script:
  189. k2: ca02 * ep2;
  190. assume(sig > 0);
  191. I1(sig):= integrate(sqrt(1 + k2 * sin(s)^2), s, 0, sig);
  192. I2(sig):= integrate(1/sqrt(1 + k2 * sin(s)^2), s, 0, sig);
  193. J(sig):= I1(sig) - I2(sig);
  194. S: taylor(J(sig), ep2, 0, 3);
  195. S1: factor( integrate(sin(s)^2,s,0,sig)*ca02*ep2 );
  196. S2: factor( (integrate(sin(s)^4,s,0,sig)*ca02^2*ep2^2)/2 );
  197. S3: factor( (3*integrate(sin(s)^6,s,0,sig)*ca02^3*ep2^3)/8 );
  198. */
  199. static inline CT J12_ep_sqr(CT const& sin_sig1, CT const& cos_sig1,
  200. CT const& sin_sig2, CT const& cos_sig2,
  201. CT const& cos_alp0_sqr, CT const& ep_sqr)
  202. {
  203. if (Order == 0)
  204. {
  205. return 0;
  206. }
  207. CT const c2 = 2;
  208. CT const c4 = 4;
  209. CT const c2a0ep2 = cos_alp0_sqr * ep_sqr;
  210. CT const sig_12 = atan2(cos_sig1 * sin_sig2 - sin_sig1 * cos_sig2,
  211. cos_sig1 * cos_sig2 + sin_sig1 * sin_sig2); // sig2 - sig1
  212. CT const sin_2sig1 = c2 * cos_sig1 * sin_sig1; // sin(2sig1)
  213. CT const sin_2sig2 = c2 * cos_sig2 * sin_sig2; // sin(2sig2)
  214. CT const sin_2sig_12 = sin_2sig2 - sin_2sig1;
  215. CT const L1 = (c2 * sig_12 - sin_2sig_12) / c4;
  216. if (Order == 1)
  217. {
  218. return c2a0ep2 * L1;
  219. }
  220. CT const c8 = 8;
  221. CT const c64 = 64;
  222. CT const sin_4sig1 = c2 * sin_2sig1 * (math::sqr(cos_sig1) - math::sqr(sin_sig1)); // sin(4sig1)
  223. CT const sin_4sig2 = c2 * sin_2sig2 * (math::sqr(cos_sig2) - math::sqr(sin_sig2)); // sin(4sig2)
  224. CT const sin_4sig_12 = sin_4sig2 - sin_4sig1;
  225. CT const L2 = (sin_4sig_12 - c8 * sin_2sig_12 + 12 * sig_12) / c64;
  226. if (Order == 2)
  227. {
  228. return c2a0ep2 * (L1 + c2a0ep2 * L2);
  229. }
  230. CT const sin3_2sig1 = math::sqr(sin_2sig1) * sin_2sig1;
  231. CT const sin3_2sig2 = math::sqr(sin_2sig2) * sin_2sig2;
  232. CT const sin3_2sig_12 = sin3_2sig2 - sin3_2sig1;
  233. CT const c9 = 9;
  234. CT const c48 = 48;
  235. CT const c60 = 60;
  236. CT const c512 = 512;
  237. CT const L3 = (c9 * sin_4sig_12 + c4 * sin3_2sig_12 - c48 * sin_2sig_12 + c60 * sig_12) / c512;
  238. // Order 3 and higher
  239. return c2a0ep2 * (L1 + c2a0ep2 * (L2 + c2a0ep2 * L3));
  240. }
  241. static inline void normalize(CT & x, CT & y)
  242. {
  243. CT const len = math::sqrt(math::sqr(x) + math::sqr(y));
  244. x /= len;
  245. y /= len;
  246. }
  247. };
  248. }}} // namespace boost::geometry::formula
  249. #endif // BOOST_GEOMETRY_FORMULAS_INVERSE_DIFFERENTIAL_QUANTITIES_HPP