thomas_direct.hpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Boost.Geometry
  2. // Copyright (c) 2016-2018 Oracle and/or its affiliates.
  3. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  4. // Contributed and/or modified by Adam Wulkiewicz, 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_THOMAS_DIRECT_HPP
  9. #define BOOST_GEOMETRY_FORMULAS_THOMAS_DIRECT_HPP
  10. #include <boost/math/constants/constants.hpp>
  11. #include <boost/geometry/core/assert.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. namespace boost { namespace geometry { namespace formula
  19. {
  20. /*!
  21. \brief The solution of the direct problem of geodesics on latlong coordinates,
  22. Forsyth-Andoyer-Lambert type approximation with first/second order terms.
  23. \author See
  24. - Technical Report: PAUL D. THOMAS, MATHEMATICAL MODELS FOR NAVIGATION SYSTEMS, 1965
  25. http://www.dtic.mil/docs/citations/AD0627893
  26. - Technical Report: PAUL D. THOMAS, SPHEROIDAL GEODESICS, REFERENCE SYSTEMS, AND LOCAL GEOMETRY, 1970
  27. http://www.dtic.mil/docs/citations/AD0703541
  28. */
  29. template <
  30. typename CT,
  31. bool SecondOrder = true,
  32. bool EnableCoordinates = true,
  33. bool EnableReverseAzimuth = false,
  34. bool EnableReducedLength = false,
  35. bool EnableGeodesicScale = false
  36. >
  37. class thomas_direct
  38. {
  39. static const bool CalcQuantities = EnableReducedLength || EnableGeodesicScale;
  40. static const bool CalcCoordinates = EnableCoordinates || CalcQuantities;
  41. static const bool CalcRevAzimuth = EnableReverseAzimuth || CalcCoordinates || CalcQuantities;
  42. public:
  43. typedef result_direct<CT> result_type;
  44. template <typename T, typename Dist, typename Azi, typename Spheroid>
  45. static inline result_type apply(T const& lo1,
  46. T const& la1,
  47. Dist const& distance,
  48. Azi const& azimuth12,
  49. Spheroid const& spheroid)
  50. {
  51. result_type result;
  52. CT const lon1 = lo1;
  53. CT const lat1 = la1;
  54. CT const c0 = 0;
  55. CT const c1 = 1;
  56. CT const c2 = 2;
  57. CT const c4 = 4;
  58. CT const a = CT(get_radius<0>(spheroid));
  59. CT const b = CT(get_radius<2>(spheroid));
  60. CT const f = formula::flattening<CT>(spheroid);
  61. CT const one_minus_f = c1 - f;
  62. CT const pi = math::pi<CT>();
  63. CT const pi_half = pi / c2;
  64. BOOST_GEOMETRY_ASSERT(-pi <= azimuth12 && azimuth12 <= pi);
  65. // keep azimuth small - experiments show low accuracy
  66. // if the azimuth is closer to (+-)180 deg.
  67. CT azi12_alt = azimuth12;
  68. CT lat1_alt = lat1;
  69. bool alter_result = vflip_if_south(lat1, azimuth12, lat1_alt, azi12_alt);
  70. CT const theta1 = math::equals(lat1_alt, pi_half) ? lat1_alt :
  71. math::equals(lat1_alt, -pi_half) ? lat1_alt :
  72. atan(one_minus_f * tan(lat1_alt));
  73. CT const sin_theta1 = sin(theta1);
  74. CT const cos_theta1 = cos(theta1);
  75. CT const sin_a12 = sin(azi12_alt);
  76. CT const cos_a12 = cos(azi12_alt);
  77. CT const M = cos_theta1 * sin_a12; // cos_theta0
  78. CT const theta0 = acos(M);
  79. CT const sin_theta0 = sin(theta0);
  80. CT const N = cos_theta1 * cos_a12;
  81. CT const C1 = f * M; // lower-case c1 in the technical report
  82. CT const C2 = f * (c1 - math::sqr(M)) / c4; // lower-case c2 in the technical report
  83. CT D = 0;
  84. CT P = 0;
  85. if ( BOOST_GEOMETRY_CONDITION(SecondOrder) )
  86. {
  87. D = (c1 - C2) * (c1 - C2 - C1 * M);
  88. P = C2 * (c1 + C1 * M / c2) / D;
  89. }
  90. else
  91. {
  92. D = c1 - c2 * C2 - C1 * M;
  93. P = C2 / D;
  94. }
  95. // special case for equator:
  96. // sin_theta0 = 0 <=> lat1 = 0 ^ |azimuth12| = pi/2
  97. // NOTE: in this case it doesn't matter what's the value of cos_sigma1 because
  98. // theta1=0, theta0=0, M=1|-1, C2=0 so X=0 and Y=0 so d_sigma=d
  99. // cos_a12=0 so N=0, therefore
  100. // lat2=0, azi21=pi/2|-pi/2
  101. // d_eta = atan2(sin_d_sigma, cos_d_sigma)
  102. // H = C1 * d_sigma
  103. CT const cos_sigma1 = math::equals(sin_theta0, c0)
  104. ? c1
  105. : normalized1_1(sin_theta1 / sin_theta0);
  106. CT const sigma1 = acos(cos_sigma1);
  107. CT const d = distance / (a * D);
  108. CT const u = 2 * (sigma1 - d);
  109. CT const cos_d = cos(d);
  110. CT const sin_d = sin(d);
  111. CT const cos_u = cos(u);
  112. CT const sin_u = sin(u);
  113. CT const W = c1 - c2 * P * cos_u;
  114. CT const V = cos_u * cos_d - sin_u * sin_d;
  115. CT const Y = c2 * P * V * W * sin_d;
  116. CT X = 0;
  117. CT d_sigma = d - Y;
  118. if ( BOOST_GEOMETRY_CONDITION(SecondOrder) )
  119. {
  120. X = math::sqr(C2) * sin_d * cos_d * (2 * math::sqr(V) - c1);
  121. d_sigma += X;
  122. }
  123. CT const sin_d_sigma = sin(d_sigma);
  124. CT const cos_d_sigma = cos(d_sigma);
  125. if (BOOST_GEOMETRY_CONDITION(CalcRevAzimuth))
  126. {
  127. result.reverse_azimuth = atan2(M, N * cos_d_sigma - sin_theta1 * sin_d_sigma);
  128. if (alter_result)
  129. {
  130. vflip_rev_azi(result.reverse_azimuth, azimuth12);
  131. }
  132. }
  133. if (BOOST_GEOMETRY_CONDITION(CalcCoordinates))
  134. {
  135. CT const S_sigma = c2 * sigma1 - d_sigma;
  136. CT cos_S_sigma = 0;
  137. CT H = C1 * d_sigma;
  138. if ( BOOST_GEOMETRY_CONDITION(SecondOrder) )
  139. {
  140. cos_S_sigma = cos(S_sigma);
  141. H = H * (c1 - C2) - C1 * C2 * sin_d_sigma * cos_S_sigma;
  142. }
  143. CT const d_eta = atan2(sin_d_sigma * sin_a12, cos_theta1 * cos_d_sigma - sin_theta1 * sin_d_sigma * cos_a12);
  144. CT const d_lambda = d_eta - H;
  145. result.lon2 = lon1 + d_lambda;
  146. if (! math::equals(M, c0))
  147. {
  148. CT const sin_a21 = sin(result.reverse_azimuth);
  149. CT const tan_theta2 = (sin_theta1 * cos_d_sigma + N * sin_d_sigma) * sin_a21 / M;
  150. result.lat2 = atan(tan_theta2 / one_minus_f);
  151. }
  152. else
  153. {
  154. CT const sigma2 = S_sigma - sigma1;
  155. //theta2 = asin(cos(sigma2)) <=> sin_theta0 = 1
  156. // NOTE: cos(sigma2) defines the sign of tan_theta2
  157. CT const tan_theta2 = cos(sigma2) / math::abs(sin(sigma2));
  158. result.lat2 = atan(tan_theta2 / one_minus_f);
  159. }
  160. if (alter_result)
  161. {
  162. result.lat2 = -result.lat2;
  163. }
  164. }
  165. if (BOOST_GEOMETRY_CONDITION(CalcQuantities))
  166. {
  167. typedef differential_quantities<CT, EnableReducedLength, EnableGeodesicScale, 2> quantities;
  168. quantities::apply(lon1, lat1, result.lon2, result.lat2,
  169. azimuth12, result.reverse_azimuth,
  170. b, f,
  171. result.reduced_length, result.geodesic_scale);
  172. }
  173. return result;
  174. }
  175. private:
  176. static inline bool vflip_if_south(CT const& lat1, CT const& azi12, CT & lat1_alt, CT & azi12_alt)
  177. {
  178. CT const c2 = 2;
  179. CT const pi = math::pi<CT>();
  180. CT const pi_half = pi / c2;
  181. if (azi12 > pi_half)
  182. {
  183. azi12_alt = pi - azi12;
  184. lat1_alt = -lat1;
  185. return true;
  186. }
  187. else if (azi12 < -pi_half)
  188. {
  189. azi12_alt = -pi - azi12;
  190. lat1_alt = -lat1;
  191. return true;
  192. }
  193. return false;
  194. }
  195. static inline void vflip_rev_azi(CT & rev_azi, CT const& azimuth12)
  196. {
  197. CT const c0 = 0;
  198. CT const pi = math::pi<CT>();
  199. if (rev_azi == c0)
  200. {
  201. rev_azi = azimuth12 >= 0 ? pi : -pi;
  202. }
  203. else if (rev_azi > c0)
  204. {
  205. rev_azi = pi - rev_azi;
  206. }
  207. else
  208. {
  209. rev_azi = -pi - rev_azi;
  210. }
  211. }
  212. static inline CT normalized1_1(CT const& value)
  213. {
  214. CT const c1 = 1;
  215. return value > c1 ? c1 :
  216. value < -c1 ? -c1 :
  217. value;
  218. }
  219. };
  220. }}} // namespace boost::geometry::formula
  221. #endif // BOOST_GEOMETRY_FORMULAS_THOMAS_DIRECT_HPP