thomas_direct.hpp 8.5 KB

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