area_formulas.hpp 21 KB

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