thomas_direct.hpp 9.0 KB

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