area_formulas.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. // Boost.Geometry
  2. // Copyright (c) 2023 Adam Wulkiewicz, Lodz, Poland.
  3. // Copyright (c) 2015-2022 Oracle and/or its affiliates.
  4. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_GEOMETRY_FORMULAS_AREA_FORMULAS_HPP
  10. #define BOOST_GEOMETRY_FORMULAS_AREA_FORMULAS_HPP
  11. #include <boost/geometry/core/radian_access.hpp>
  12. #include <boost/geometry/formulas/flattening.hpp>
  13. #include <boost/geometry/formulas/mean_radius.hpp>
  14. #include <boost/geometry/formulas/karney_inverse.hpp>
  15. #include <boost/geometry/util/math.hpp>
  16. #include <boost/math/special_functions/hypot.hpp>
  17. namespace boost { namespace geometry { namespace formula
  18. {
  19. /*!
  20. \brief Formulas for computing spherical and ellipsoidal polygon area.
  21. The current class computes the area of the trapezoid defined by a segment
  22. the two meridians passing by the endpoints and the equator.
  23. \author See
  24. - Danielsen JS, The area under the geodesic. Surv Rev 30(232):
  25. 61–66, 1989
  26. - Charles F.F Karney, Algorithms for geodesics, 2011
  27. https://arxiv.org/pdf/1109.4448.pdf
  28. */
  29. template
  30. <
  31. typename CT,
  32. std::size_t SeriesOrder = 2,
  33. bool ExpandEpsN = true
  34. >
  35. class area_formulas
  36. {
  37. public:
  38. //TODO: move the following to a more general space to be used by other
  39. // classes as well
  40. /*
  41. Evaluate the polynomial in x using Horner's method.
  42. */
  43. template <typename NT, typename IteratorType>
  44. static inline NT horner_evaluate(NT const& x,
  45. IteratorType begin,
  46. IteratorType end)
  47. {
  48. NT result(0);
  49. IteratorType it = end;
  50. do
  51. {
  52. result = result * x + *--it;
  53. }
  54. while (it != begin);
  55. return result;
  56. }
  57. /*
  58. Clenshaw algorithm for summing trigonometric series
  59. https://en.wikipedia.org/wiki/Clenshaw_algorithm
  60. */
  61. template <typename NT, typename IteratorType>
  62. static inline NT clenshaw_sum(NT const& cosx,
  63. IteratorType begin,
  64. IteratorType end)
  65. {
  66. IteratorType it = end;
  67. bool odd = true;
  68. CT b_k, b_k1(0), b_k2(0);
  69. do
  70. {
  71. CT c_k = odd ? *--it : NT(0);
  72. b_k = c_k + NT(2) * cosx * b_k1 - b_k2;
  73. b_k2 = b_k1;
  74. b_k1 = b_k;
  75. odd = !odd;
  76. }
  77. while (it != begin);
  78. return *begin + b_k1 * cosx - b_k2;
  79. }
  80. template<typename T>
  81. static inline void normalize(T& x, T& y)
  82. {
  83. T h = boost::math::hypot(x, y);
  84. x /= h;
  85. y /= h;
  86. }
  87. /*
  88. Generate and evaluate the series expansion of the following integral
  89. I4 = -integrate( (t(ep2) - t(k2*sin(sigma1)^2)) / (ep2 - k2*sin(sigma1)^2)
  90. * sin(sigma1)/2, sigma1, pi/2, sigma )
  91. where
  92. t(x) = sqrt(1+1/x)*asinh(sqrt(x)) + x
  93. valid for ep2 and k2 small. We substitute k2 = 4 * eps / (1 - eps)^2
  94. and ep2 = 4 * n / (1 - n)^2 and expand in eps and n.
  95. The resulting sum of the series is of the form
  96. sum(C4[l] * cos((2*l+1)*sigma), l, 0, maxpow-1) )
  97. The above expansion is performed in Computer Algebra System Maxima.
  98. The C++ code (that yields the function evaluate_coeffs_n below) is generated
  99. by the following Maxima script and is based on script:
  100. http://geographiclib.sourceforge.net/html/geod.mac
  101. // Maxima script begin
  102. taylordepth:5$
  103. ataylor(expr,var,ord):=expand(ratdisrep(taylor(expr,var,0,ord)))$
  104. jtaylor(expr,var1,var2,ord):=block([zz],expand(subst([zz=1],
  105. ratdisrep(taylor(subst([var1=zz*var1,var2=zz*var2],expr),zz,0,ord)))))$
  106. compute(maxpow):=block([int,t,intexp,area, x,ep2,k2],
  107. maxpow:maxpow-1,
  108. t : sqrt(1+1/x) * asinh(sqrt(x)) + x,
  109. int:-(tf(ep2) - tf(k2*sin(sigma)^2)) / (ep2 - k2*sin(sigma)^2)
  110. * sin(sigma)/2,
  111. int:subst([tf(ep2)=subst([x=ep2],t),
  112. tf(k2*sin(sigma)^2)=subst([x=k2*sin(sigma)^2],t)],
  113. int),
  114. int:subst([abs(sin(sigma))=sin(sigma)],int),
  115. int:subst([k2=4*eps/(1-eps)^2,ep2=4*n/(1-n)^2],int),
  116. intexp:jtaylor(int,n,eps,maxpow),
  117. area:trigreduce(integrate(intexp,sigma)),
  118. area:expand(area-subst(sigma=%pi/2,area)),
  119. for i:0 thru maxpow do C4[i]:coeff(area,cos((2*i+1)*sigma)),
  120. if expand(area-sum(C4[i]*cos((2*i+1)*sigma),i,0,maxpow)) # 0
  121. then error("left over terms in I4"),
  122. 'done)$
  123. printcode(maxpow):=
  124. block([tab2:" ",tab3:" "],
  125. print(" switch (SeriesOrder) {"),
  126. for nn:1 thru maxpow do block([c],
  127. print(concat(tab2,"case ",string(nn-1),":")),
  128. c:0,
  129. for m:0 thru nn-1 do block(
  130. [q:jtaylor(subst([n=n],C4[m]),n,eps,nn-1),
  131. linel:1200],
  132. for j:m thru nn-1 do (
  133. print(concat(tab3,"coeffs_n[",c,"] = ",
  134. string(horner(coeff(q,eps,j))),";")),
  135. c:c+1)
  136. ),
  137. print(concat(tab3,"break;"))),
  138. print(" }"),
  139. 'done)$
  140. maxpow:6$
  141. compute(maxpow)$
  142. printcode(maxpow)$
  143. // Maxima script end
  144. In the resulting code we should replace each number x by CT(x)
  145. e.g. using the following scirpt:
  146. sed -e 's/[0-9]\+/CT(&)/g; s/\[CT(/\[/g; s/)\]/\]/g;
  147. s/case\sCT(/case /g; s/):/:/g'
  148. */
  149. static inline void evaluate_coeffs_n(CT const& n, CT coeffs_n[])
  150. {
  151. switch (SeriesOrder) {
  152. case 0:
  153. coeffs_n[0] = CT(2)/CT(3);
  154. break;
  155. case 1:
  156. coeffs_n[0] = (CT(10)-CT(4)*n)/CT(15);
  157. coeffs_n[1] = -CT(1)/CT(5);
  158. coeffs_n[2] = CT(1)/CT(45);
  159. break;
  160. case 2:
  161. coeffs_n[0] = (n*(CT(8)*n-CT(28))+CT(70))/CT(105);
  162. coeffs_n[1] = (CT(16)*n-CT(7))/CT(35);
  163. coeffs_n[2] = -CT(2)/CT(105);
  164. coeffs_n[3] = (CT(7)-CT(16)*n)/CT(315);
  165. coeffs_n[4] = -CT(2)/CT(105);
  166. coeffs_n[5] = CT(4)/CT(525);
  167. break;
  168. case 3:
  169. coeffs_n[0] = (n*(n*(CT(4)*n+CT(24))-CT(84))+CT(210))/CT(315);
  170. coeffs_n[1] = ((CT(48)-CT(32)*n)*n-CT(21))/CT(105);
  171. coeffs_n[2] = (-CT(32)*n-CT(6))/CT(315);
  172. coeffs_n[3] = CT(11)/CT(315);
  173. coeffs_n[4] = (n*(CT(32)*n-CT(48))+CT(21))/CT(945);
  174. coeffs_n[5] = (CT(64)*n-CT(18))/CT(945);
  175. coeffs_n[6] = -CT(1)/CT(105);
  176. coeffs_n[7] = (CT(12)-CT(32)*n)/CT(1575);
  177. coeffs_n[8] = -CT(8)/CT(1575);
  178. coeffs_n[9] = CT(8)/CT(2205);
  179. break;
  180. case 4:
  181. coeffs_n[0] = (n*(n*(n*(CT(16)*n+CT(44))+CT(264))-CT(924))+CT(2310))/CT(3465);
  182. coeffs_n[1] = (n*(n*(CT(48)*n-CT(352))+CT(528))-CT(231))/CT(1155);
  183. coeffs_n[2] = (n*(CT(1088)*n-CT(352))-CT(66))/CT(3465);
  184. coeffs_n[3] = (CT(121)-CT(368)*n)/CT(3465);
  185. coeffs_n[4] = CT(4)/CT(1155);
  186. coeffs_n[5] = (n*((CT(352)-CT(48)*n)*n-CT(528))+CT(231))/CT(10395);
  187. coeffs_n[6] = ((CT(704)-CT(896)*n)*n-CT(198))/CT(10395);
  188. coeffs_n[7] = (CT(80)*n-CT(99))/CT(10395);
  189. coeffs_n[8] = CT(4)/CT(1155);
  190. coeffs_n[9] = (n*(CT(320)*n-CT(352))+CT(132))/CT(17325);
  191. coeffs_n[10] = (CT(384)*n-CT(88))/CT(17325);
  192. coeffs_n[11] = -CT(8)/CT(1925);
  193. coeffs_n[12] = (CT(88)-CT(256)*n)/CT(24255);
  194. coeffs_n[13] = -CT(16)/CT(8085);
  195. coeffs_n[14] = CT(64)/CT(31185);
  196. break;
  197. case 5:
  198. coeffs_n[0] = (n*(n*(n*(n*(CT(100)*n+CT(208))+CT(572))+CT(3432))-CT(12012))+CT(30030))
  199. /CT(45045);
  200. coeffs_n[1] = (n*(n*(n*(CT(64)*n+CT(624))-CT(4576))+CT(6864))-CT(3003))/CT(15015);
  201. coeffs_n[2] = (n*((CT(14144)-CT(10656)*n)*n-CT(4576))-CT(858))/CT(45045);
  202. coeffs_n[3] = ((-CT(224)*n-CT(4784))*n+CT(1573))/CT(45045);
  203. coeffs_n[4] = (CT(1088)*n+CT(156))/CT(45045);
  204. coeffs_n[5] = CT(97)/CT(15015);
  205. coeffs_n[6] = (n*(n*((-CT(64)*n-CT(624))*n+CT(4576))-CT(6864))+CT(3003))/CT(135135);
  206. coeffs_n[7] = (n*(n*(CT(5952)*n-CT(11648))+CT(9152))-CT(2574))/CT(135135);
  207. coeffs_n[8] = (n*(CT(5792)*n+CT(1040))-CT(1287))/CT(135135);
  208. coeffs_n[9] = (CT(468)-CT(2944)*n)/CT(135135);
  209. coeffs_n[10] = CT(1)/CT(9009);
  210. coeffs_n[11] = (n*((CT(4160)-CT(1440)*n)*n-CT(4576))+CT(1716))/CT(225225);
  211. coeffs_n[12] = ((CT(4992)-CT(8448)*n)*n-CT(1144))/CT(225225);
  212. coeffs_n[13] = (CT(1856)*n-CT(936))/CT(225225);
  213. coeffs_n[14] = CT(8)/CT(10725);
  214. coeffs_n[15] = (n*(CT(3584)*n-CT(3328))+CT(1144))/CT(315315);
  215. coeffs_n[16] = (CT(1024)*n-CT(208))/CT(105105);
  216. coeffs_n[17] = -CT(136)/CT(63063);
  217. coeffs_n[18] = (CT(832)-CT(2560)*n)/CT(405405);
  218. coeffs_n[19] = -CT(128)/CT(135135);
  219. coeffs_n[20] = CT(128)/CT(99099);
  220. break;
  221. }
  222. }
  223. /*
  224. Expand in k2 and ep2.
  225. */
  226. static inline void evaluate_coeffs_ep(CT const& ep, CT coeffs_n[])
  227. {
  228. switch (SeriesOrder) {
  229. case 0:
  230. coeffs_n[0] = CT(2)/CT(3);
  231. break;
  232. case 1:
  233. coeffs_n[0] = (CT(10)-ep)/CT(15);
  234. coeffs_n[1] = -CT(1)/CT(20);
  235. coeffs_n[2] = CT(1)/CT(180);
  236. break;
  237. case 2:
  238. coeffs_n[0] = (ep*(CT(4)*ep-CT(7))+CT(70))/CT(105);
  239. coeffs_n[1] = (CT(4)*ep-CT(7))/CT(140);
  240. coeffs_n[2] = CT(1)/CT(42);
  241. coeffs_n[3] = (CT(7)-CT(4)*ep)/CT(1260);
  242. coeffs_n[4] = -CT(1)/CT(252);
  243. coeffs_n[5] = CT(1)/CT(2100);
  244. break;
  245. case 3:
  246. coeffs_n[0] = (ep*((CT(12)-CT(8)*ep)*ep-CT(21))+CT(210))/CT(315);
  247. coeffs_n[1] = ((CT(12)-CT(8)*ep)*ep-CT(21))/CT(420);
  248. coeffs_n[2] = (CT(3)-CT(2)*ep)/CT(126);
  249. coeffs_n[3] = -CT(1)/CT(72);
  250. coeffs_n[4] = (ep*(CT(8)*ep-CT(12))+CT(21))/CT(3780);
  251. coeffs_n[5] = (CT(2)*ep-CT(3))/CT(756);
  252. coeffs_n[6] = CT(1)/CT(360);
  253. coeffs_n[7] = (CT(3)-CT(2)*ep)/CT(6300);
  254. coeffs_n[8] = -CT(1)/CT(1800);
  255. coeffs_n[9] = CT(1)/CT(17640);
  256. break;
  257. case 4:
  258. coeffs_n[0] = (ep*(ep*(ep*(CT(64)*ep-CT(88))+CT(132))-CT(231))+CT(2310))/CT(3465);
  259. coeffs_n[1] = (ep*(ep*(CT(64)*ep-CT(88))+CT(132))-CT(231))/CT(4620);
  260. coeffs_n[2] = (ep*(CT(16)*ep-CT(22))+CT(33))/CT(1386);
  261. coeffs_n[3] = (CT(8)*ep-CT(11))/CT(792);
  262. coeffs_n[4] = CT(1)/CT(110);
  263. coeffs_n[5] = (ep*((CT(88)-CT(64)*ep)*ep-CT(132))+CT(231))/CT(41580);
  264. coeffs_n[6] = ((CT(22)-CT(16)*ep)*ep-CT(33))/CT(8316);
  265. coeffs_n[7] = (CT(11)-CT(8)*ep)/CT(3960);
  266. coeffs_n[8] = -CT(1)/CT(495);
  267. coeffs_n[9] = (ep*(CT(16)*ep-CT(22))+CT(33))/CT(69300);
  268. coeffs_n[10] = (CT(8)*ep-CT(11))/CT(19800);
  269. coeffs_n[11] = CT(1)/CT(1925);
  270. coeffs_n[12] = (CT(11)-CT(8)*ep)/CT(194040);
  271. coeffs_n[13] = -CT(1)/CT(10780);
  272. coeffs_n[14] = CT(1)/CT(124740);
  273. break;
  274. case 5:
  275. coeffs_n[0] = (ep*(ep*(ep*((CT(832)-CT(640)*ep)*ep-CT(1144))+CT(1716))-CT(3003))+CT(30030))/CT(45045);
  276. coeffs_n[1] = (ep*(ep*((CT(832)-CT(640)*ep)*ep-CT(1144))+CT(1716))-CT(3003))/CT(60060);
  277. coeffs_n[2] = (ep*((CT(208)-CT(160)*ep)*ep-CT(286))+CT(429))/CT(18018);
  278. coeffs_n[3] = ((CT(104)-CT(80)*ep)*ep-CT(143))/CT(10296);
  279. coeffs_n[4] = (CT(13)-CT(10)*ep)/CT(1430);
  280. coeffs_n[5] = -CT(1)/CT(156);
  281. coeffs_n[6] = (ep*(ep*(ep*(CT(640)*ep-CT(832))+CT(1144))-CT(1716))+CT(3003))/CT(540540);
  282. coeffs_n[7] = (ep*(ep*(CT(160)*ep-CT(208))+CT(286))-CT(429))/CT(108108);
  283. coeffs_n[8] = (ep*(CT(80)*ep-CT(104))+CT(143))/CT(51480);
  284. coeffs_n[9] = (CT(10)*ep-CT(13))/CT(6435);
  285. coeffs_n[10] = CT(5)/CT(3276);
  286. coeffs_n[11] = (ep*((CT(208)-CT(160)*ep)*ep-CT(286))+CT(429))/CT(900900);
  287. coeffs_n[12] = ((CT(104)-CT(80)*ep)*ep-CT(143))/CT(257400);
  288. coeffs_n[13] = (CT(13)-CT(10)*ep)/CT(25025);
  289. coeffs_n[14] = -CT(1)/CT(2184);
  290. coeffs_n[15] = (ep*(CT(80)*ep-CT(104))+CT(143))/CT(2522520);
  291. coeffs_n[16] = (CT(10)*ep-CT(13))/CT(140140);
  292. coeffs_n[17] = CT(5)/CT(45864);
  293. coeffs_n[18] = (CT(13)-CT(10)*ep)/CT(1621620);
  294. coeffs_n[19] = -CT(1)/CT(58968);
  295. coeffs_n[20] = CT(1)/CT(792792);
  296. break;
  297. }
  298. }
  299. /*
  300. Given the set of coefficients coeffs1[] evaluate on var2 and return
  301. the set of coefficients coeffs2[]
  302. */
  303. template <typename CoeffsType>
  304. static inline void evaluate_coeffs_var2(CT const& var2,
  305. CoeffsType const coeffs1[],
  306. CT coeffs2[])
  307. {
  308. std::size_t begin(0), end(0);
  309. for(std::size_t i = 0; i <= SeriesOrder; i++)
  310. {
  311. end = begin + SeriesOrder + 1 - i;
  312. coeffs2[i] = ((i==0) ? CT(1) : math::pow(var2, int(i)))
  313. * horner_evaluate(var2, coeffs1 + begin, coeffs1 + end);
  314. begin = end;
  315. }
  316. }
  317. static inline CT trapezoidal_formula(CT lat1r, CT lat2r, CT lon21r)
  318. {
  319. CT const c1 = CT(1);
  320. CT const c2 = CT(2);
  321. CT const tan_lat1 = tan(lat1r / c2);
  322. CT const tan_lat2 = tan(lat2r / c2);
  323. return c2 * atan(((tan_lat1 + tan_lat2) / (c1 + tan_lat1 * tan_lat2))* tan(lon21r / c2));
  324. }
  325. /*
  326. Compute the spherical excess of a geodesic (or shperical) segment
  327. */
  328. template
  329. <
  330. bool LongSegment,
  331. typename PointOfSegment
  332. >
  333. static inline CT spherical(PointOfSegment const& p1,
  334. PointOfSegment const& p2)
  335. {
  336. CT const pi = math::pi<CT>();
  337. CT excess;
  338. CT const lon1r = get_as_radian<0>(p1);
  339. CT const lat1r = get_as_radian<1>(p1);
  340. CT const lon2r = get_as_radian<0>(p2);
  341. CT const lat2r = get_as_radian<1>(p2);
  342. CT lon12r = lon2r - lon1r;
  343. math::normalize_longitude<radian, CT>(lon12r);
  344. if (lon12r == pi || lon12r == -pi)
  345. {
  346. return pi;
  347. }
  348. if (BOOST_GEOMETRY_CONDITION(LongSegment) && lat1r != lat2r) // not for segments parallel to equator
  349. {
  350. CT const cbet1 = cos(lat1r);
  351. CT const sbet1 = sin(lat1r);
  352. CT const cbet2 = cos(lat2r);
  353. CT const sbet2 = sin(lat2r);
  354. CT const omg12 = lon2r - lon1r;
  355. CT const comg12 = cos(omg12);
  356. CT const somg12 = sin(omg12);
  357. CT const cbet1_sbet2 = cbet1 * sbet2;
  358. CT const sbet1_cbet2 = sbet1 * cbet2;
  359. CT const alp1 = atan2(cbet1_sbet2 - sbet1_cbet2 * comg12, cbet2 * somg12);
  360. CT const alp2 = atan2(cbet1_sbet2 * comg12 - sbet1_cbet2, cbet1 * somg12);
  361. excess = alp2 - alp1;
  362. } else {
  363. excess = trapezoidal_formula(lat1r, lat2r, lon12r);
  364. }
  365. return excess;
  366. }
  367. struct return_type_ellipsoidal
  368. {
  369. return_type_ellipsoidal()
  370. : spherical_term(0),
  371. ellipsoidal_term(0)
  372. {}
  373. CT spherical_term;
  374. CT ellipsoidal_term;
  375. };
  376. /*
  377. Compute the ellipsoidal correction of a geodesic (or shperical) segment
  378. */
  379. template
  380. <
  381. template <typename, bool, bool, bool, bool, bool> class Inverse,
  382. typename PointOfSegment,
  383. typename SpheroidConst
  384. >
  385. static inline auto ellipsoidal(PointOfSegment const& p1,
  386. PointOfSegment const& p2,
  387. SpheroidConst const& spheroid_const)
  388. {
  389. return_type_ellipsoidal result;
  390. CT const lon1r = get_as_radian<0>(p1);
  391. CT const lat1r = get_as_radian<1>(p1);
  392. CT const lon2r = get_as_radian<0>(p2);
  393. CT const lat2r = get_as_radian<1>(p2);
  394. // Azimuth Approximation
  395. using inverse_type = Inverse<CT, true, true, true, false, false>;
  396. auto i_res = inverse_type::apply(lon1r, lat1r, lon2r, lat2r, spheroid_const.m_spheroid);
  397. CT const alp1 = i_res.azimuth;
  398. CT const alp2 = i_res.reverse_azimuth;
  399. // Constants
  400. CT const c0 = CT(0);
  401. CT const c1 = CT(1);
  402. CT const c2 = CT(2);
  403. CT const pi = math::pi<CT>();
  404. CT const half_pi = pi / c2;
  405. CT const ep = spheroid_const.m_ep;
  406. CT const one_minus_f = c1 - spheroid_const.m_f;
  407. // Basic trigonometric computations
  408. // the compiler could optimize here using sincos function
  409. // TODO: optimization: those quantities are already computed in inverse formula
  410. // at least in some inverse formulas, so do not compute them again here
  411. /*
  412. CT sin_bet1 = sin(lat1r);
  413. CT cos_bet1 = cos(lat1r);
  414. CT sin_bet2 = sin(lat2r);
  415. CT cos_bet2 = cos(lat2r);
  416. sin_bet1 *= one_minus_f;
  417. sin_bet2 *= one_minus_f;
  418. normalize(sin_bet1, cos_bet1);
  419. normalize(sin_bet2, cos_bet2);
  420. */
  421. CT const tan_bet1 = tan(lat1r) * one_minus_f;
  422. CT const tan_bet2 = tan(lat2r) * one_minus_f;
  423. CT const cos_bet1 = cos(atan(tan_bet1));
  424. CT const cos_bet2 = cos(atan(tan_bet2));
  425. CT const sin_bet1 = tan_bet1 * cos_bet1;
  426. CT const sin_bet2 = tan_bet2 * cos_bet2;
  427. CT const sin_alp1 = sin(alp1);
  428. CT const cos_alp1 = cos(alp1);
  429. CT const cos_alp2 = cos(alp2);
  430. CT const sin_alp0 = sin_alp1 * cos_bet1;
  431. // Spherical term computation
  432. CT excess;
  433. CT lon12r = lon2r - lon1r;
  434. math::normalize_longitude<radian, CT>(lon12r);
  435. // Comparing with "==" works with all test cases here, but could potential create numerical issues
  436. if (lon12r == pi || lon12r == -pi)
  437. {
  438. result.spherical_term = pi;
  439. }
  440. else
  441. {
  442. bool const meridian = lon12r == c0
  443. || lat1r == half_pi || lat1r == -half_pi
  444. || lat2r == half_pi || lat2r == -half_pi;
  445. if (!meridian && (i_res.distance)
  446. < mean_radius<CT>(spheroid_const.m_spheroid) / CT(638)) // short segment
  447. {
  448. excess = trapezoidal_formula(lat1r, lat2r, lon12r);
  449. }
  450. else
  451. {
  452. /* in some cases this formula gives more accurate results
  453. CT sin_omg12 = cos_omg1 * sin_omg2 - sin_omg1 * cos_omg2;
  454. normalize(sin_omg12, cos_omg12);
  455. CT cos_omg12p1 = CT(1) + cos_omg12;
  456. CT cos_bet1p1 = CT(1) + cos_bet1;
  457. CT cos_bet2p1 = CT(1) + cos_bet2;
  458. excess = CT(2) * atan2(sin_omg12 * (sin_bet1 * cos_bet2p1 + sin_bet2 * cos_bet1p1),
  459. cos_omg12p1 * (sin_bet1 * sin_bet2 + cos_bet1p1 * cos_bet2p1));
  460. */
  461. excess = alp2 - alp1;
  462. }
  463. result.spherical_term = excess;
  464. }
  465. // Ellipsoidal term computation (uses integral approximation)
  466. CT const cos_alp0 = math::sqrt(c1 - math::sqr(sin_alp0));
  467. //CT const cos_alp0 = hypot(cos_alp1, sin_alp1 * sin_bet1);
  468. CT cos_sig1 = cos_alp1 * cos_bet1;
  469. CT cos_sig2 = cos_alp2 * cos_bet2;
  470. CT sin_sig1 = sin_bet1;
  471. CT sin_sig2 = sin_bet2;
  472. normalize(sin_sig1, cos_sig1);
  473. normalize(sin_sig2, cos_sig2);
  474. CT coeffs[SeriesOrder + 1];
  475. if (ExpandEpsN) // expand by eps and n
  476. {
  477. CT const k2 = math::sqr(ep * cos_alp0);
  478. CT const sqrt_k2_plus_one = math::sqrt(c1 + k2);
  479. CT const eps = (sqrt_k2_plus_one - c1) / (sqrt_k2_plus_one + c1);
  480. // Generate and evaluate the polynomials on eps (i.e. var2 = eps)
  481. // to get the final series coefficients
  482. evaluate_coeffs_var2(eps, spheroid_const.m_coeffs_var, coeffs);
  483. }
  484. else
  485. { // expand by k2 and ep
  486. CT const k2 = math::sqr(ep * cos_alp0);
  487. CT const ep2 = math::sqr(ep);
  488. CT coeffs_var[((SeriesOrder+2)*(SeriesOrder+1))/2];
  489. // Generate and evaluate the polynomials on ep2
  490. evaluate_coeffs_ep(ep2, coeffs_var);
  491. // Generate and evaluate the polynomials on k2 (i.e. var2 = k2)
  492. evaluate_coeffs_var2(k2, coeffs_var, coeffs);
  493. }
  494. // Evaluate the trigonometric sum
  495. constexpr auto series_order_plus_one = SeriesOrder + 1;
  496. CT const I12 = clenshaw_sum(cos_sig2, coeffs, coeffs + series_order_plus_one)
  497. - clenshaw_sum(cos_sig1, coeffs, coeffs + series_order_plus_one);
  498. // The part of the ellipsodal correction that depends on
  499. // point coordinates
  500. result.ellipsoidal_term = cos_alp0 * sin_alp0 * I12;
  501. return result;
  502. }
  503. // Check whenever a segment crosses the prime meridian
  504. // First normalize to [0,360)
  505. template <typename PointOfSegment>
  506. static inline bool crosses_prime_meridian(PointOfSegment const& p1,
  507. PointOfSegment const& p2)
  508. {
  509. CT const pi = geometry::math::pi<CT>();
  510. CT const two_pi = geometry::math::two_pi<CT>();
  511. CT const lon1r = get_as_radian<0>(p1);
  512. CT const lon2r = get_as_radian<0>(p2);
  513. CT lon12 = lon2r - lon1r;
  514. math::normalize_longitude<radian, CT>(lon12);
  515. // Comparing with "==" works with all test cases here, but could potential create numerical issues
  516. if (lon12 == pi || lon12 == -pi)
  517. {
  518. return true;
  519. }
  520. CT const p1_lon = lon1r - ( std::floor( lon1r / two_pi ) * two_pi );
  521. CT const p2_lon = lon2r - ( std::floor( lon2r / two_pi ) * two_pi );
  522. CT const max_lon = (std::max)(p1_lon, p2_lon);
  523. CT const min_lon = (std::min)(p1_lon, p2_lon);
  524. return max_lon > pi && min_lon < pi && max_lon - min_lon > pi;
  525. }
  526. };
  527. }}} // namespace boost::geometry::formula
  528. #endif // BOOST_GEOMETRY_FORMULAS_AREA_FORMULAS_HPP