thomas_direct.hpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. if ( math::equals(distance, Dist(0)) || distance < Dist(0) )
  54. {
  55. result.lon2 = lon1;
  56. result.lat2 = lat1;
  57. return result;
  58. }
  59. CT const c0 = 0;
  60. CT const c1 = 1;
  61. CT const c2 = 2;
  62. CT const c4 = 4;
  63. CT const a = CT(get_radius<0>(spheroid));
  64. CT const b = CT(get_radius<2>(spheroid));
  65. CT const f = formula::flattening<CT>(spheroid);
  66. CT const one_minus_f = c1 - f;
  67. CT const pi = math::pi<CT>();
  68. CT const pi_half = pi / c2;
  69. // keep azimuth small - experiments show low accuracy
  70. // if the azimuth is closer to (+-)180 deg.
  71. CT azi12_alt = azimuth12;
  72. CT lat1_alt = lat1;
  73. bool alter_result = vflip_if_south(lat1, azimuth12, lat1_alt, azi12_alt);
  74. CT const theta1 = math::equals(lat1_alt, pi_half) ? lat1_alt :
  75. math::equals(lat1_alt, -pi_half) ? lat1_alt :
  76. atan(one_minus_f * tan(lat1_alt));
  77. CT const sin_theta1 = sin(theta1);
  78. CT const cos_theta1 = cos(theta1);
  79. CT const sin_a12 = sin(azi12_alt);
  80. CT const cos_a12 = cos(azi12_alt);
  81. CT const M = cos_theta1 * sin_a12; // cos_theta0
  82. CT const theta0 = acos(M);
  83. CT const sin_theta0 = sin(theta0);
  84. CT const N = cos_theta1 * cos_a12;
  85. CT const C1 = f * M; // lower-case c1 in the technical report
  86. CT const C2 = f * (c1 - math::sqr(M)) / c4; // lower-case c2 in the technical report
  87. CT D = 0;
  88. CT P = 0;
  89. if ( BOOST_GEOMETRY_CONDITION(SecondOrder) )
  90. {
  91. D = (c1 - C2) * (c1 - C2 - C1 * M);
  92. P = C2 * (c1 + C1 * M / c2) / D;
  93. }
  94. else
  95. {
  96. D = c1 - c2 * C2 - C1 * M;
  97. P = C2 / D;
  98. }
  99. // special case for equator:
  100. // sin_theta0 = 0 <=> lat1 = 0 ^ |azimuth12| = pi/2
  101. // NOTE: in this case it doesn't matter what's the value of cos_sigma1 because
  102. // theta1=0, theta0=0, M=1|-1, C2=0 so X=0 and Y=0 so d_sigma=d
  103. // cos_a12=0 so N=0, therefore
  104. // lat2=0, azi21=pi/2|-pi/2
  105. // d_eta = atan2(sin_d_sigma, cos_d_sigma)
  106. // H = C1 * d_sigma
  107. CT const cos_sigma1 = math::equals(sin_theta0, c0)
  108. ? c1
  109. : normalized1_1(sin_theta1 / sin_theta0);
  110. CT const sigma1 = acos(cos_sigma1);
  111. CT const d = distance / (a * D);
  112. CT const u = 2 * (sigma1 - d);
  113. CT const cos_d = cos(d);
  114. CT const sin_d = sin(d);
  115. CT const cos_u = cos(u);
  116. CT const sin_u = sin(u);
  117. CT const W = c1 - c2 * P * cos_u;
  118. CT const V = cos_u * cos_d - sin_u * sin_d;
  119. CT const Y = c2 * P * V * W * sin_d;
  120. CT X = 0;
  121. CT d_sigma = d - Y;
  122. if ( BOOST_GEOMETRY_CONDITION(SecondOrder) )
  123. {
  124. X = math::sqr(C2) * sin_d * cos_d * (2 * math::sqr(V) - c1);
  125. d_sigma += X;
  126. }
  127. CT const sin_d_sigma = sin(d_sigma);
  128. CT const cos_d_sigma = cos(d_sigma);
  129. if (BOOST_GEOMETRY_CONDITION(CalcRevAzimuth))
  130. {
  131. result.reverse_azimuth = atan2(M, N * cos_d_sigma - sin_theta1 * sin_d_sigma);
  132. if (alter_result)
  133. {
  134. vflip_rev_azi(result.reverse_azimuth, azimuth12);
  135. }
  136. }
  137. if (BOOST_GEOMETRY_CONDITION(CalcCoordinates))
  138. {
  139. CT const S_sigma = c2 * sigma1 - d_sigma;
  140. CT cos_S_sigma = 0;
  141. CT H = C1 * d_sigma;
  142. if ( BOOST_GEOMETRY_CONDITION(SecondOrder) )
  143. {
  144. cos_S_sigma = cos(S_sigma);
  145. H = H * (c1 - C2) - C1 * C2 * sin_d_sigma * cos_S_sigma;
  146. }
  147. CT const d_eta = atan2(sin_d_sigma * sin_a12, cos_theta1 * cos_d_sigma - sin_theta1 * sin_d_sigma * cos_a12);
  148. CT const d_lambda = d_eta - H;
  149. result.lon2 = lon1 + d_lambda;
  150. if (! math::equals(M, c0))
  151. {
  152. CT const sin_a21 = sin(result.reverse_azimuth);
  153. CT const tan_theta2 = (sin_theta1 * cos_d_sigma + N * sin_d_sigma) * sin_a21 / M;
  154. result.lat2 = atan(tan_theta2 / one_minus_f);
  155. }
  156. else
  157. {
  158. CT const sigma2 = S_sigma - sigma1;
  159. //theta2 = asin(cos(sigma2)) <=> sin_theta0 = 1
  160. // NOTE: cos(sigma2) defines the sign of tan_theta2
  161. CT const tan_theta2 = cos(sigma2) / math::abs(sin(sigma2));
  162. result.lat2 = atan(tan_theta2 / one_minus_f);
  163. }
  164. if (alter_result)
  165. {
  166. result.lat2 = -result.lat2;
  167. }
  168. }
  169. if (BOOST_GEOMETRY_CONDITION(CalcQuantities))
  170. {
  171. typedef differential_quantities<CT, EnableReducedLength, EnableGeodesicScale, 2> quantities;
  172. quantities::apply(lon1, lat1, result.lon2, result.lat2,
  173. azimuth12, result.reverse_azimuth,
  174. b, f,
  175. result.reduced_length, result.geodesic_scale);
  176. }
  177. return result;
  178. }
  179. private:
  180. static inline bool vflip_if_south(CT const& lat1, CT const& azi12, CT & lat1_alt, CT & azi12_alt)
  181. {
  182. CT const c2 = 2;
  183. CT const pi = math::pi<CT>();
  184. CT const pi_half = pi / c2;
  185. if (azi12 > pi_half)
  186. {
  187. azi12_alt = pi - azi12;
  188. lat1_alt = -lat1;
  189. return true;
  190. }
  191. else if (azi12 < -pi_half)
  192. {
  193. azi12_alt = -pi - azi12;
  194. lat1_alt = -lat1;
  195. return true;
  196. }
  197. return false;
  198. }
  199. static inline void vflip_rev_azi(CT & rev_azi, CT const& azimuth12)
  200. {
  201. CT const c0 = 0;
  202. CT const pi = math::pi<CT>();
  203. if (rev_azi == c0)
  204. {
  205. rev_azi = azimuth12 >= 0 ? pi : -pi;
  206. }
  207. else if (rev_azi > c0)
  208. {
  209. rev_azi = pi - rev_azi;
  210. }
  211. else
  212. {
  213. rev_azi = -pi - rev_azi;
  214. }
  215. }
  216. static inline CT normalized1_1(CT const& value)
  217. {
  218. CT const c1 = 1;
  219. return value > c1 ? c1 :
  220. value < -c1 ? -c1 :
  221. value;
  222. }
  223. };
  224. }}} // namespace boost::geometry::formula
  225. #endif // BOOST_GEOMETRY_FORMULAS_THOMAS_DIRECT_HPP