precise_math.hpp 22 KB

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