precise_math.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2019 Tinko Bartels, Berlin, Germany.
  3. // Contributed and/or modified by Tinko Bartels,
  4. // as part of Google Summer of Code 2019 program.
  5. // This file was modified by Oracle on 2021.
  6. // Modifications copyright (c) 2021, Oracle and/or its affiliates.
  7. // Contributed and/or modified by Vissarion Fisikopoulos, on behalf of Oracle
  8. // Use, modification and distribution is subject to the Boost Software License,
  9. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. #ifndef BOOST_GEOMETRY_EXTENSIONS_TRIANGULATION_STRATEGIES_CARTESIAN_DETAIL_PRECISE_MATH_HPP
  12. #define BOOST_GEOMETRY_EXTENSIONS_TRIANGULATION_STRATEGIES_CARTESIAN_DETAIL_PRECISE_MATH_HPP
  13. #include<numeric>
  14. #include<cmath>
  15. #include<limits>
  16. #include<array>
  17. #include <boost/geometry/core/access.hpp>
  18. // The following code is based on "Adaptive Precision Floating-Point Arithmetic
  19. // and Fast Robust Geometric Predicates" by Richard Shewchuk,
  20. // J. Discrete Comput Geom (1997) 18: 305. https://doi.org/10.1007/PL00009321
  21. namespace boost { namespace geometry
  22. {
  23. namespace detail { namespace precise_math
  24. {
  25. // See Theorem 6, page 6
  26. template
  27. <
  28. typename RealNumber
  29. >
  30. inline std::array<RealNumber, 2> fast_two_sum(RealNumber const a,
  31. RealNumber const b)
  32. {
  33. RealNumber x = a + b;
  34. RealNumber b_virtual = x - a;
  35. return {{x, b - b_virtual}};
  36. }
  37. // See Theorem 7, page 7 - 8
  38. template
  39. <
  40. typename RealNumber
  41. >
  42. inline std::array<RealNumber, 2> two_sum(RealNumber const a,
  43. RealNumber const b)
  44. {
  45. RealNumber x = a + b;
  46. RealNumber b_virtual = x - a;
  47. RealNumber a_virtual = x - b_virtual;
  48. RealNumber b_roundoff = b - b_virtual;
  49. RealNumber a_roundoff = a - a_virtual;
  50. RealNumber y = a_roundoff + b_roundoff;
  51. return {{ x, y }};
  52. }
  53. // See bottom of page 8
  54. template
  55. <
  56. typename RealNumber
  57. >
  58. inline RealNumber two_diff_tail(RealNumber const a,
  59. RealNumber const b,
  60. RealNumber const x)
  61. {
  62. RealNumber b_virtual = a - x;
  63. RealNumber a_virtual = x + b_virtual;
  64. RealNumber b_roundoff = b_virtual - b;
  65. RealNumber a_roundoff = a - a_virtual;
  66. return a_roundoff + b_roundoff;
  67. }
  68. // see bottom of page 8
  69. template
  70. <
  71. typename RealNumber
  72. >
  73. inline std::array<RealNumber, 2> two_diff(RealNumber const a,
  74. RealNumber const b)
  75. {
  76. RealNumber x = a - b;
  77. RealNumber y = two_diff_tail(a, b, x);
  78. return {{ x, y }};
  79. }
  80. // see theorem 18, page 19
  81. template
  82. <
  83. typename RealNumber
  84. >
  85. inline RealNumber two_product_tail(RealNumber const a,
  86. RealNumber const b,
  87. RealNumber const x)
  88. {
  89. return std::fma(a, b, -x);
  90. }
  91. // see theorem 18, page 19
  92. template
  93. <
  94. typename RealNumber
  95. >
  96. inline std::array<RealNumber, 2> two_product(RealNumber const a,
  97. RealNumber const b)
  98. {
  99. RealNumber x = a * b;
  100. RealNumber y = two_product_tail(a, b, x);
  101. return {{ x , y }};
  102. }
  103. // see theorem 12, figure 7, page 11 - 12,
  104. // this is the 2 by 2 case for the corresponding diff-method
  105. // note that this method takes input in descending order of magnitude and
  106. // returns components in ascending order of magnitude
  107. template
  108. <
  109. typename RealNumber
  110. >
  111. inline std::array<RealNumber, 4> two_two_expansion_diff(
  112. std::array<RealNumber, 2> const a,
  113. std::array<RealNumber, 2> const b)
  114. {
  115. std::array<RealNumber, 4> h;
  116. std::array<RealNumber, 2> Qh = two_diff(a[1], b[1]);
  117. h[0] = Qh[1];
  118. Qh = two_sum( a[0], Qh[0] );
  119. RealNumber _j = Qh[0];
  120. Qh = two_diff(Qh[1], b[0]);
  121. h[1] = Qh[1];
  122. Qh = two_sum( _j, Qh[0] );
  123. h[2] = Qh[1];
  124. h[3] = Qh[0];
  125. return h;
  126. }
  127. // see theorem 13, figure 8. This implementation uses zero elimination as
  128. // suggested on page 17, second to last paragraph. Returns the number of
  129. // non-zero components in the result and writes the result to h.
  130. // the merger into a single sequence g is done implicitly
  131. template
  132. <
  133. typename RealNumber,
  134. std::size_t InSize1,
  135. std::size_t InSize2,
  136. std::size_t OutSize
  137. >
  138. inline int fast_expansion_sum_zeroelim(
  139. std::array<RealNumber, InSize1> const& e,
  140. std::array<RealNumber, InSize2> const& f,
  141. std::array<RealNumber, OutSize> & h,
  142. int m = InSize1,
  143. int n = InSize2)
  144. {
  145. std::array<RealNumber, 2> Qh;
  146. int i_e = 0;
  147. int i_f = 0;
  148. int i_h = 0;
  149. if (std::abs(f[0]) > std::abs(e[0]))
  150. {
  151. Qh[0] = e[i_e++];
  152. }
  153. else
  154. {
  155. Qh[0] = f[i_f++];
  156. }
  157. i_h = 0;
  158. if ((i_e < m) && (i_f < n))
  159. {
  160. if (std::abs(f[i_f]) > std::abs(e[i_e]))
  161. {
  162. Qh = fast_two_sum(e[i_e++], Qh[0]);
  163. }
  164. else
  165. {
  166. Qh = fast_two_sum(f[i_f++], Qh[0]);
  167. }
  168. if (Qh[1] != 0.0)
  169. {
  170. h[i_h++] = Qh[1];
  171. }
  172. while ((i_e < m) && (i_f < n))
  173. {
  174. if (std::abs(f[i_f]) > std::abs(e[i_e]))
  175. {
  176. Qh = two_sum(Qh[0], e[i_e++]);
  177. }
  178. else
  179. {
  180. Qh = two_sum(Qh[0], f[i_f++]);
  181. }
  182. if (Qh[1] != 0.0)
  183. {
  184. h[i_h++] = Qh[1];
  185. }
  186. }
  187. }
  188. while (i_e < m)
  189. {
  190. Qh = two_sum(Qh[0], e[i_e++]);
  191. if (Qh[1] != 0.0)
  192. {
  193. h[i_h++] = Qh[1];
  194. }
  195. }
  196. while (i_f < n)
  197. {
  198. Qh = two_sum(Qh[0], f[i_f++]);
  199. if (Qh[1] != 0.0)
  200. {
  201. h[i_h++] = Qh[1];
  202. }
  203. }
  204. if ((Qh[0] != 0.0) || (i_h == 0))
  205. {
  206. h[i_h++] = Qh[0];
  207. }
  208. return i_h;
  209. }
  210. // see theorem 19, figure 13, page 20 - 21. This implementation uses zero
  211. // elimination as suggested on page 17, second to last paragraph. Returns the
  212. // number of non-zero components in the result and writes the result to h.
  213. template
  214. <
  215. typename RealNumber,
  216. std::size_t InSize
  217. >
  218. inline int scale_expansion_zeroelim(
  219. std::array<RealNumber, InSize> const& e,
  220. RealNumber const b,
  221. std::array<RealNumber, 2 * InSize> & h,
  222. int e_non_zeros = InSize)
  223. {
  224. std::array<RealNumber, 2> Qh = two_product(e[0], b);
  225. int i_h = 0;
  226. if (Qh[1] != 0)
  227. {
  228. h[i_h++] = Qh[1];
  229. }
  230. for (int i_e = 1; i_e < e_non_zeros; i_e++)
  231. {
  232. std::array<RealNumber, 2> Tt = two_product(e[i_e], b);
  233. Qh = two_sum(Qh[0], Tt[1]);
  234. if (Qh[1] != 0)
  235. {
  236. h[i_h++] = Qh[1];
  237. }
  238. Qh = fast_two_sum(Tt[0], Qh[0]);
  239. if (Qh[1] != 0)
  240. {
  241. h[i_h++] = Qh[1];
  242. }
  243. }
  244. if ((Qh[0] != 0.0) || (i_h == 0))
  245. {
  246. h[i_h++] = Qh[0];
  247. }
  248. return i_h;
  249. }
  250. template<typename RealNumber>
  251. struct vec2d
  252. {
  253. RealNumber x;
  254. RealNumber y;
  255. };
  256. template
  257. <
  258. typename RealNumber,
  259. std::size_t Robustness
  260. >
  261. inline RealNumber orient2dtail(vec2d<RealNumber> const& p1,
  262. vec2d<RealNumber> const& p2,
  263. vec2d<RealNumber> const& p3,
  264. std::array<RealNumber, 2>& t1,
  265. std::array<RealNumber, 2>& t2,
  266. std::array<RealNumber, 2>& t3,
  267. std::array<RealNumber, 2>& t4,
  268. std::array<RealNumber, 2>& t5_01,
  269. std::array<RealNumber, 2>& t6_01,
  270. RealNumber const& magnitude)
  271. {
  272. t5_01[1] = two_product_tail(t1[0], t2[0], t5_01[0]);
  273. t6_01[1] = two_product_tail(t3[0], t4[0], t6_01[0]);
  274. std::array<RealNumber, 4> tA_03 = two_two_expansion_diff(t5_01, t6_01);
  275. RealNumber det = std::accumulate(tA_03.begin(), tA_03.end(), static_cast<RealNumber>(0));
  276. if (Robustness == 1)
  277. {
  278. return det;
  279. }
  280. // see p.39, mind the different definition of epsilon for error bound
  281. RealNumber B_relative_bound =
  282. (1 + 3 * std::numeric_limits<RealNumber>::epsilon())
  283. * std::numeric_limits<RealNumber>::epsilon();
  284. RealNumber absolute_bound = B_relative_bound * magnitude;
  285. if (std::abs(det) >= absolute_bound)
  286. {
  287. return det; //B estimate
  288. }
  289. t1[1] = two_diff_tail(p1.x, p3.x, t1[0]);
  290. t2[1] = two_diff_tail(p2.y, p3.y, t2[0]);
  291. t3[1] = two_diff_tail(p1.y, p3.y, t3[0]);
  292. t4[1] = two_diff_tail(p2.x, p3.x, t4[0]);
  293. if ((t1[1] == 0) && (t3[1] == 0) && (t2[1] == 0) && (t4[1] == 0))
  294. {
  295. return det; //If all tails are zero, there is noething else to compute
  296. }
  297. RealNumber sub_bound =
  298. (1.5 + 2 * std::numeric_limits<RealNumber>::epsilon())
  299. * std::numeric_limits<RealNumber>::epsilon();
  300. // see p.39, mind the different definition of epsilon for error bound
  301. RealNumber C_relative_bound =
  302. (2.25 + 8 * std::numeric_limits<RealNumber>::epsilon())
  303. * std::numeric_limits<RealNumber>::epsilon()
  304. * std::numeric_limits<RealNumber>::epsilon();
  305. absolute_bound = C_relative_bound * magnitude + sub_bound * std::abs(det);
  306. det += (t1[0] * t2[1] + t2[0] * t1[1]) - (t3[0] * t4[1] + t4[0] * t3[1]);
  307. if (Robustness == 2 || std::abs(det) >= absolute_bound)
  308. {
  309. return det; //C estimate
  310. }
  311. std::array<RealNumber, 8> D_left;
  312. int D_left_nz;
  313. {
  314. std::array<RealNumber, 2> t5_23 = two_product(t1[1], t2[0]);
  315. std::array<RealNumber, 2> t6_23 = two_product(t3[1], t4[0]);
  316. std::array<RealNumber, 4> tA_47 = two_two_expansion_diff(t5_23, t6_23);
  317. D_left_nz = fast_expansion_sum_zeroelim(tA_03, tA_47, D_left);
  318. }
  319. std::array<RealNumber, 8> D_right;
  320. int D_right_nz;
  321. {
  322. std::array<RealNumber, 2> t5_45 = two_product(t1[0], t2[1]);
  323. std::array<RealNumber, 2> t6_45 = two_product(t3[0], t4[1]);
  324. std::array<RealNumber, 4> tA_8_11 = two_two_expansion_diff(t5_45, t6_45);
  325. std::array<RealNumber, 2> t5_67 = two_product(t1[1], t2[1]);
  326. std::array<RealNumber, 2> t6_67 = two_product(t3[1], t4[1]);
  327. std::array<RealNumber, 4> tA_12_15 = two_two_expansion_diff(t5_67, t6_67);
  328. D_right_nz = fast_expansion_sum_zeroelim(tA_8_11, tA_12_15, D_right);
  329. }
  330. std::array<RealNumber, 16> D;
  331. int D_nz = fast_expansion_sum_zeroelim(D_left, D_right, D, D_left_nz, D_right_nz);
  332. // only return component of highest magnitude because we mostly care about the sign.
  333. return(D[D_nz - 1]);
  334. }
  335. // see page 38, Figure 21 for the calculations, notation follows the notation
  336. // in the figure.
  337. template
  338. <
  339. typename RealNumber,
  340. std::size_t Robustness = 3,
  341. typename EpsPolicy
  342. >
  343. inline RealNumber orient2d(vec2d<RealNumber> const& p1,
  344. vec2d<RealNumber> const& p2,
  345. vec2d<RealNumber> const& p3,
  346. EpsPolicy& eps_policy)
  347. {
  348. std::array<RealNumber, 2> t1, t2, t3, t4;
  349. t1[0] = p1.x - p3.x;
  350. t2[0] = p2.y - p3.y;
  351. t3[0] = p1.y - p3.y;
  352. t4[0] = p2.x - p3.x;
  353. eps_policy = EpsPolicy(t1[0], t2[0], t3[0], t4[0]);
  354. std::array<RealNumber, 2> t5_01, t6_01;
  355. t5_01[0] = t1[0] * t2[0];
  356. t6_01[0] = t3[0] * t4[0];
  357. RealNumber det = t5_01[0] - t6_01[0];
  358. if (Robustness == 0)
  359. {
  360. return det;
  361. }
  362. RealNumber const magnitude = std::abs(t5_01[0]) + std::abs(t6_01[0]);
  363. // see p.39, mind the different definition of epsilon for error bound
  364. RealNumber const A_relative_bound =
  365. (1.5 + 4 * std::numeric_limits<RealNumber>::epsilon())
  366. * std::numeric_limits<RealNumber>::epsilon();
  367. RealNumber absolute_bound = A_relative_bound * magnitude;
  368. if ( std::abs(det) >= absolute_bound )
  369. {
  370. return det; //A estimate
  371. }
  372. if ( (t5_01[0] > 0 && t6_01[0] <= 0) || (t5_01[0] < 0 && t6_01[0] >= 0) )
  373. {
  374. //if diagonal and antidiagonal have different sign, the sign of det is
  375. //obvious
  376. return det;
  377. }
  378. return orient2dtail<RealNumber, Robustness>(p1, p2, p3, t1, t2, t3, t4,
  379. t5_01, t6_01, magnitude);
  380. }
  381. // This method adaptively computes increasingly precise approximations of the following
  382. // determinant using Laplace expansion along the last column.
  383. // det A =
  384. // | p1_x - p4_x p1_y - p4_y ( p1_x - p4_x ) ^ 2 + ( p1_y - p4_y ) ^ 2 |
  385. // | p2_x - p4_x p2_y - p4_y ( p2_x - p4_x ) ^ 2 + ( p1_y - p4_y ) ^ 2 |
  386. // | p3_x - p4_x p3_y - p4_y ( p3_x - p4_x ) ^ 2 + ( p3_y - p4_y ) ^ 2 |
  387. // = a_13 * C_13 + a_23 * C_23 + a_33 * C_33
  388. // where a_ij is the i-j-entry and C_ij is the i_j Cofactor
  389. template
  390. <
  391. typename RealNumber,
  392. std::size_t Robustness = 2
  393. >
  394. RealNumber incircle(std::array<RealNumber, 2> const& p1,
  395. std::array<RealNumber, 2> const& p2,
  396. std::array<RealNumber, 2> const& p3,
  397. std::array<RealNumber, 2> const& p4)
  398. {
  399. RealNumber A_11 = p1[0] - p4[0];
  400. RealNumber A_21 = p2[0] - p4[0];
  401. RealNumber A_31 = p3[0] - p4[0];
  402. RealNumber A_12 = p1[1] - p4[1];
  403. RealNumber A_22 = p2[1] - p4[1];
  404. RealNumber A_32 = p3[1] - p4[1];
  405. std::array<RealNumber, 2> A_21_x_A_32,
  406. A_31_x_A_22,
  407. A_31_x_A_12,
  408. A_11_x_A_32,
  409. A_11_x_A_22,
  410. A_21_x_A_12;
  411. A_21_x_A_32[0] = A_21 * A_32;
  412. A_31_x_A_22[0] = A_31 * A_22;
  413. RealNumber A_13 = A_11 * A_11 + A_12 * A_12;
  414. A_31_x_A_12[0] = A_31 * A_12;
  415. A_11_x_A_32[0] = A_11 * A_32;
  416. RealNumber A_23 = A_21 * A_21 + A_22 * A_22;
  417. A_11_x_A_22[0] = A_11 * A_22;
  418. A_21_x_A_12[0] = A_21 * A_12;
  419. RealNumber A_33 = A_31 * A_31 + A_32 * A_32;
  420. RealNumber det = A_13 * (A_21_x_A_32[0] - A_31_x_A_22[0])
  421. + A_23 * (A_31_x_A_12[0] - A_11_x_A_32[0])
  422. + A_33 * (A_11_x_A_22[0] - A_21_x_A_12[0]);
  423. if(Robustness == 0) return det;
  424. RealNumber magnitude =
  425. (std::abs(A_21_x_A_32[0]) + std::abs(A_31_x_A_22[0])) * A_13
  426. + (std::abs(A_31_x_A_12[0]) + std::abs(A_11_x_A_32[0])) * A_23
  427. + (std::abs(A_11_x_A_22[0]) + std::abs(A_21_x_A_12[0])) * A_33;
  428. RealNumber A_relative_bound =
  429. (5 + 24 * std::numeric_limits<RealNumber>::epsilon())
  430. * std::numeric_limits<RealNumber>::epsilon();
  431. RealNumber absolute_bound = A_relative_bound * magnitude;
  432. if (std::abs(det) > absolute_bound)
  433. {
  434. return det;
  435. }
  436. // (p2_x - p4_x) * (p3_y - p4_y)
  437. A_21_x_A_32[1] = two_product_tail(A_21, A_32, A_21_x_A_32[0]);
  438. // (p3_x - p4_x) * (p2_y - p4_y)
  439. A_31_x_A_22[1] = two_product_tail(A_31, A_22, A_31_x_A_22[0]);
  440. // (bx - dx) * (cy - dy) - (cx - dx) * (by - dy)
  441. std::array<RealNumber, 4> C_13 = two_two_expansion_diff(A_21_x_A_32, A_31_x_A_22);
  442. std::array<RealNumber, 8> C_13_x_A11;
  443. // ( (bx - dx) * (cy - dy) - (cx - dx) * (by - dy) ) * ( ax - dx )
  444. int C_13_x_A11_nz = scale_expansion_zeroelim(C_13, A_11, C_13_x_A11);
  445. std::array<RealNumber, 16> C_13_x_A11_sq;
  446. // ( (bx - dx) * (cy - dy) - (cx - dx) * (by - dy) ) * ( ax - dx ) * (ax - dx)
  447. int C_13_x_A11_sq_nz = scale_expansion_zeroelim(C_13_x_A11,
  448. A_11,
  449. C_13_x_A11_sq,
  450. C_13_x_A11_nz);
  451. std::array<RealNumber, 8> C_13_x_A12;
  452. // ( (bx - dx) * (cy - dy) - (cx - dx) * (by - dy) ) * ( ay - dy )
  453. int C_13_x_A12_nz = scale_expansion_zeroelim(C_13, A_12, C_13_x_A12);
  454. std::array<RealNumber, 16> C_13_x_A12_sq;
  455. // ( (bx - dx) * (cy - dy) - (cx - dx) * (by - dy) ) * ( ay - dy ) * ( ay - dy )
  456. int C_13_x_A12_sq_nz = scale_expansion_zeroelim(C_13_x_A12, A_12,
  457. C_13_x_A12_sq,
  458. C_13_x_A12_nz);
  459. std::array<RealNumber, 32> A_13_x_C13;
  460. // ( (bx - dx) * (cy - dy) - (cx - dx) * (by - dy) )
  461. // * ( ( ay - dy ) * ( ay - dy ) + ( ax - dx ) * (ax - dx) )
  462. int A_13_x_C13_nz = fast_expansion_sum_zeroelim(C_13_x_A11_sq,
  463. C_13_x_A12_sq,
  464. A_13_x_C13,
  465. C_13_x_A11_sq_nz,
  466. C_13_x_A12_sq_nz);
  467. // (cx - dx) * (ay - dy)
  468. A_31_x_A_12[1] = two_product_tail(A_31, A_12, A_31_x_A_12[0]);
  469. // (ax - dx) * (cy - dy)
  470. A_11_x_A_32[1] = two_product_tail(A_11, A_32, A_11_x_A_32[0]);
  471. // (cx - dx) * (ay - dy) - (ax - dx) * (cy - dy)
  472. std::array<RealNumber, 4> C_23 = two_two_expansion_diff(A_31_x_A_12,
  473. A_11_x_A_32);
  474. std::array<RealNumber, 8> C_23_x_A_21;
  475. // ( (cx - dx) * (ay - dy) - (ax - dx) * (cy - dy) ) * ( bx - dx )
  476. int C_23_x_A_21_nz = scale_expansion_zeroelim(C_23, A_21, C_23_x_A_21);
  477. std::array<RealNumber, 16> C_23_x_A_21_sq;
  478. // ( (cx - dx) * (ay - dy) - (ax - dx) * (cy - dy) ) * ( bx - dx ) * ( bx - dx )
  479. int C_23_x_A_21_sq_nz = scale_expansion_zeroelim(C_23_x_A_21, A_21,
  480. C_23_x_A_21_sq,
  481. C_23_x_A_21_nz);
  482. std::array<RealNumber, 8> C_23_x_A_22;
  483. // ( (cx - dx) * (ay - dy) - (ax - dx) * (cy - dy) ) * ( by - dy )
  484. int C_23_x_A_22_nz = scale_expansion_zeroelim(C_23, A_22, C_23_x_A_22);
  485. std::array<RealNumber, 16> C_23_x_A_22_sq;
  486. // ( (cx - dx) * (ay - dy) - (ax - dx) * (cy - dy) ) * ( by - dy ) * ( by - dy )
  487. int C_23_x_A_22_sq_nz = scale_expansion_zeroelim(C_23_x_A_22, A_22,
  488. C_23_x_A_22_sq,
  489. C_23_x_A_22_nz);
  490. std::array<RealNumber, 32> A_23_x_C_23;
  491. // ( (cx - dx) * (ay - dy) - (ax - dx) * (cy - dy) )
  492. // * ( ( bx - dx ) * ( bx - dx ) + ( by - dy ) * ( by - dy ) )
  493. int A_23_x_C_23_nz = fast_expansion_sum_zeroelim(C_23_x_A_21_sq,
  494. C_23_x_A_22_sq,
  495. A_23_x_C_23,
  496. C_23_x_A_21_sq_nz,
  497. C_23_x_A_22_sq_nz);
  498. // (ax - dx) * (by - dy)
  499. A_11_x_A_22[1] = two_product_tail(A_11, A_22, A_11_x_A_22[0]);
  500. // (bx - dx) * (ay - dy)
  501. A_21_x_A_12[1] = two_product_tail(A_21, A_12, A_21_x_A_12[0]);
  502. // (ax - dx) * (by - dy) - (bx - dx) * (ay - dy)
  503. std::array<RealNumber, 4> C_33 = two_two_expansion_diff(A_11_x_A_22,
  504. A_21_x_A_12);
  505. std::array<RealNumber, 8> C_33_x_A31;
  506. // ( (ax - dx) * (by - dy) - (bx - dx) * (ay - dy) ) * ( cx - dx )
  507. int C_33_x_A31_nz = scale_expansion_zeroelim(C_33, A_31, C_33_x_A31);
  508. std::array<RealNumber, 16> C_33_x_A31_sq;
  509. // ( (ax - dx) * (by - dy) - (bx - dx) * (ay - dy) ) * ( cx - dx ) * ( cx - dx )
  510. int C_33_x_A31_sq_nz = scale_expansion_zeroelim(C_33_x_A31, A_31,
  511. C_33_x_A31_sq,
  512. C_33_x_A31_nz);
  513. std::array<RealNumber, 8> C_33_x_A_32;
  514. // ( (ax - dx) * (by - dy) - (bx - dx) * (ay - dy) ) * ( cy - dy )
  515. int C_33_x_A_32_nz = scale_expansion_zeroelim(C_33, A_32, C_33_x_A_32);
  516. std::array<RealNumber, 16> C_33_x_A_32_sq;
  517. // ( (ax - dx) * (by - dy) - (bx - dx) * (ay - dy) ) * ( cy - dy ) * ( cy - dy )
  518. int C_33_x_A_32_sq_nz = scale_expansion_zeroelim(C_33_x_A_32, A_32,
  519. C_33_x_A_32_sq,
  520. C_33_x_A_32_nz);
  521. std::array<RealNumber, 32> A_33_x_C_33;
  522. int A_33_x_C_33_nz = fast_expansion_sum_zeroelim(C_33_x_A31_sq,
  523. C_33_x_A_32_sq,
  524. A_33_x_C_33,
  525. C_33_x_A31_sq_nz,
  526. C_33_x_A_32_sq_nz);
  527. std::array<RealNumber, 64> A_13_x_C13_p_A_13_x_C13;
  528. int A_13_x_C13_p_A_13_x_C13_nz = fast_expansion_sum_zeroelim(
  529. A_13_x_C13, A_23_x_C_23,
  530. A_13_x_C13_p_A_13_x_C13,
  531. A_13_x_C13_nz,
  532. A_23_x_C_23_nz);
  533. std::array<RealNumber, 96> det_expansion;
  534. int det_expansion_nz = fast_expansion_sum_zeroelim(
  535. A_13_x_C13_p_A_13_x_C13,
  536. A_33_x_C_33,
  537. det_expansion,
  538. A_13_x_C13_p_A_13_x_C13_nz,
  539. A_33_x_C_33_nz);
  540. det = std::accumulate(det_expansion.begin(),
  541. det_expansion.begin() + det_expansion_nz,
  542. static_cast<RealNumber>(0));
  543. if(Robustness == 1) return det;
  544. RealNumber B_relative_bound =
  545. (2 + 12 * std::numeric_limits<RealNumber>::epsilon())
  546. * std::numeric_limits<RealNumber>::epsilon();
  547. absolute_bound = B_relative_bound * magnitude;
  548. if (std::abs(det) >= absolute_bound)
  549. {
  550. return det;
  551. }
  552. RealNumber A_11tail = two_diff_tail(p1[0], p4[0], A_11);
  553. RealNumber A_12tail = two_diff_tail(p1[1], p4[1], A_12);
  554. RealNumber A_21tail = two_diff_tail(p2[0], p4[0], A_21);
  555. RealNumber A_22tail = two_diff_tail(p2[1], p4[1], A_22);
  556. RealNumber A_31tail = two_diff_tail(p3[0], p4[0], A_31);
  557. RealNumber A_32tail = two_diff_tail(p3[1], p4[1], A_32);
  558. if ((A_11tail == 0) && (A_21tail == 0) && (A_31tail == 0)
  559. && (A_12tail == 0) && (A_22tail == 0) && (A_32tail == 0))
  560. {
  561. return det;
  562. }
  563. // RealNumber sub_bound = (1.5 + 2.0 * std::numeric_limits<RealNumber>::epsilon())
  564. // * std::numeric_limits<RealNumber>::epsilon();
  565. // RealNumber C_relative_bound = (11.0 + 72.0 * std::numeric_limits<RealNumber>::epsilon())
  566. // * std::numeric_limits<RealNumber>::epsilon()
  567. // * std::numeric_limits<RealNumber>::epsilon();
  568. //absolute_bound = C_relative_bound * magnitude + sub_bound * std::abs(det);
  569. det += ((A_11 * A_11 + A_12 * A_12) * ((A_21 * A_32tail + A_32 * A_21tail)
  570. - (A_22 * A_31tail + A_31 * A_22tail))
  571. + 2 * (A_11 * A_11tail + A_12 * A_12tail) * (A_21 * A_32 - A_22 * A_31))
  572. + ((A_21 * A_21 + A_22 * A_22) * ((A_31 * A_12tail + A_12 * A_31tail)
  573. - (A_32 * A_11tail + A_11 * A_32tail))
  574. + 2 * (A_21 * A_21tail + A_22 * A_22tail) * (A_31 * A_12 - A_32 * A_11))
  575. + ((A_31 * A_31 + A_32 * A_32) * ((A_11 * A_22tail + A_22 * A_11tail)
  576. - (A_12 * A_21tail + A_21 * A_12tail))
  577. + 2 * (A_31 * A_31tail + A_32 * A_32tail) * (A_11 * A_22 - A_12 * A_21));
  578. //if (std::abs(det) >= absolute_bound)
  579. //{
  580. return det;
  581. //}
  582. }
  583. }} // namespace detail::precise_math
  584. }} // namespace boost::geometry
  585. #endif // BOOST_GEOMETRY_EXTENSIONS_TRIANGULATION_STRATEGIES_CARTESIAN_DETAIL_PRECISE_MATH_HPP