voronoi_robust_fpt.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. // Boost.Polygon library detail/voronoi_robust_fpt.hpp header file
  2. // Copyright Andrii Sydorchuk 2010-2012.
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org for updates, documentation, and revision history.
  7. #ifndef BOOST_POLYGON_DETAIL_VORONOI_ROBUST_FPT
  8. #define BOOST_POLYGON_DETAIL_VORONOI_ROBUST_FPT
  9. #include <cmath>
  10. // Geometry predicates with floating-point variables usually require
  11. // high-precision predicates to retrieve the correct result.
  12. // Epsilon robust predicates give the result within some epsilon relative
  13. // error, but are a lot faster than high-precision predicates.
  14. // To make algorithm robust and efficient epsilon robust predicates are
  15. // used at the first step. In case of the undefined result high-precision
  16. // arithmetic is used to produce required robustness. This approach
  17. // requires exact computation of epsilon intervals within which epsilon
  18. // robust predicates have undefined value.
  19. // There are two ways to measure an error of floating-point calculations:
  20. // relative error and ULPs (units in the last place).
  21. // Let EPS be machine epsilon, then next inequalities have place:
  22. // 1 EPS <= 1 ULP <= 2 EPS (1), 0.5 ULP <= 1 EPS <= 1 ULP (2).
  23. // ULPs are good for measuring rounding errors and comparing values.
  24. // Relative errors are good for computation of general relative
  25. // error of formulas or expressions. So to calculate epsilon
  26. // interval within which epsilon robust predicates have undefined result
  27. // next schema is used:
  28. // 1) Compute rounding errors of initial variables using ULPs;
  29. // 2) Transform ULPs to epsilons using upper bound of the (1);
  30. // 3) Compute relative error of the formula using epsilon arithmetic;
  31. // 4) Transform epsilon to ULPs using upper bound of the (2);
  32. // In case two values are inside undefined ULP range use high-precision
  33. // arithmetic to produce the correct result, else output the result.
  34. // Look at almost_equal function to see how two floating-point variables
  35. // are checked to fit in the ULP range.
  36. // If A has relative error of r(A) and B has relative error of r(B) then:
  37. // 1) r(A + B) <= max(r(A), r(B)), for A * B >= 0;
  38. // 2) r(A - B) <= B*r(A)+A*r(B)/(A-B), for A * B >= 0;
  39. // 2) r(A * B) <= r(A) + r(B);
  40. // 3) r(A / B) <= r(A) + r(B);
  41. // In addition rounding error should be added, that is always equal to
  42. // 0.5 ULP or at most 1 epsilon. As you might see from the above formulas
  43. // subtraction relative error may be extremely large, that's why
  44. // epsilon robust comparator class is used to store floating point values
  45. // and compute subtraction as the final step of the evaluation.
  46. // For further information about relative errors and ULPs try this link:
  47. // http://docs.sun.com/source/806-3568/ncg_goldberg.html
  48. namespace boost {
  49. namespace polygon {
  50. namespace detail {
  51. template <typename T>
  52. T get_sqrt(const T& that) {
  53. return (std::sqrt)(that);
  54. }
  55. template <typename T>
  56. bool is_pos(const T& that) {
  57. return that > 0;
  58. }
  59. template <typename T>
  60. bool is_neg(const T& that) {
  61. return that < 0;
  62. }
  63. template <typename T>
  64. bool is_zero(const T& that) {
  65. return that == 0;
  66. }
  67. template <typename _fpt>
  68. class robust_fpt {
  69. public:
  70. typedef _fpt floating_point_type;
  71. typedef _fpt relative_error_type;
  72. // Rounding error is at most 1 EPS.
  73. enum {
  74. ROUNDING_ERROR = 1
  75. };
  76. robust_fpt() : fpv_(0.0), re_(0.0) {}
  77. explicit robust_fpt(floating_point_type fpv) :
  78. fpv_(fpv), re_(0.0) {}
  79. robust_fpt(floating_point_type fpv, relative_error_type error) :
  80. fpv_(fpv), re_(error) {}
  81. floating_point_type fpv() const { return fpv_; }
  82. relative_error_type re() const { return re_; }
  83. relative_error_type ulp() const { return re_; }
  84. robust_fpt& operator=(const robust_fpt& that) {
  85. this->fpv_ = that.fpv_;
  86. this->re_ = that.re_;
  87. return *this;
  88. }
  89. bool has_pos_value() const {
  90. return is_pos(fpv_);
  91. }
  92. bool has_neg_value() const {
  93. return is_neg(fpv_);
  94. }
  95. bool has_zero_value() const {
  96. return is_zero(fpv_);
  97. }
  98. robust_fpt operator-() const {
  99. return robust_fpt(-fpv_, re_);
  100. }
  101. robust_fpt& operator+=(const robust_fpt& that) {
  102. floating_point_type fpv = this->fpv_ + that.fpv_;
  103. if ((!is_neg(this->fpv_) && !is_neg(that.fpv_)) ||
  104. (!is_pos(this->fpv_) && !is_pos(that.fpv_))) {
  105. this->re_ = (std::max)(this->re_, that.re_) + ROUNDING_ERROR;
  106. } else {
  107. floating_point_type temp =
  108. (this->fpv_ * this->re_ - that.fpv_ * that.re_) / fpv;
  109. if (is_neg(temp))
  110. temp = -temp;
  111. this->re_ = temp + ROUNDING_ERROR;
  112. }
  113. this->fpv_ = fpv;
  114. return *this;
  115. }
  116. robust_fpt& operator-=(const robust_fpt& that) {
  117. floating_point_type fpv = this->fpv_ - that.fpv_;
  118. if ((!is_neg(this->fpv_) && !is_pos(that.fpv_)) ||
  119. (!is_pos(this->fpv_) && !is_neg(that.fpv_))) {
  120. this->re_ = (std::max)(this->re_, that.re_) + ROUNDING_ERROR;
  121. } else {
  122. floating_point_type temp =
  123. (this->fpv_ * this->re_ + that.fpv_ * that.re_) / fpv;
  124. if (is_neg(temp))
  125. temp = -temp;
  126. this->re_ = temp + ROUNDING_ERROR;
  127. }
  128. this->fpv_ = fpv;
  129. return *this;
  130. }
  131. robust_fpt& operator*=(const robust_fpt& that) {
  132. this->re_ += that.re_ + ROUNDING_ERROR;
  133. this->fpv_ *= that.fpv_;
  134. return *this;
  135. }
  136. robust_fpt& operator/=(const robust_fpt& that) {
  137. this->re_ += that.re_ + ROUNDING_ERROR;
  138. this->fpv_ /= that.fpv_;
  139. return *this;
  140. }
  141. robust_fpt operator+(const robust_fpt& that) const {
  142. floating_point_type fpv = this->fpv_ + that.fpv_;
  143. relative_error_type re;
  144. if ((!is_neg(this->fpv_) && !is_neg(that.fpv_)) ||
  145. (!is_pos(this->fpv_) && !is_pos(that.fpv_))) {
  146. re = (std::max)(this->re_, that.re_) + ROUNDING_ERROR;
  147. } else {
  148. floating_point_type temp =
  149. (this->fpv_ * this->re_ - that.fpv_ * that.re_) / fpv;
  150. if (is_neg(temp))
  151. temp = -temp;
  152. re = temp + ROUNDING_ERROR;
  153. }
  154. return robust_fpt(fpv, re);
  155. }
  156. robust_fpt operator-(const robust_fpt& that) const {
  157. floating_point_type fpv = this->fpv_ - that.fpv_;
  158. relative_error_type re;
  159. if ((!is_neg(this->fpv_) && !is_pos(that.fpv_)) ||
  160. (!is_pos(this->fpv_) && !is_neg(that.fpv_))) {
  161. re = (std::max)(this->re_, that.re_) + ROUNDING_ERROR;
  162. } else {
  163. floating_point_type temp =
  164. (this->fpv_ * this->re_ + that.fpv_ * that.re_) / fpv;
  165. if (is_neg(temp))
  166. temp = -temp;
  167. re = temp + ROUNDING_ERROR;
  168. }
  169. return robust_fpt(fpv, re);
  170. }
  171. robust_fpt operator*(const robust_fpt& that) const {
  172. floating_point_type fpv = this->fpv_ * that.fpv_;
  173. relative_error_type re = this->re_ + that.re_ + ROUNDING_ERROR;
  174. return robust_fpt(fpv, re);
  175. }
  176. robust_fpt operator/(const robust_fpt& that) const {
  177. floating_point_type fpv = this->fpv_ / that.fpv_;
  178. relative_error_type re = this->re_ + that.re_ + ROUNDING_ERROR;
  179. return robust_fpt(fpv, re);
  180. }
  181. robust_fpt sqrt() const {
  182. return robust_fpt(get_sqrt(fpv_),
  183. re_ * static_cast<relative_error_type>(0.5) +
  184. ROUNDING_ERROR);
  185. }
  186. private:
  187. floating_point_type fpv_;
  188. relative_error_type re_;
  189. };
  190. template <typename T>
  191. robust_fpt<T> get_sqrt(const robust_fpt<T>& that) {
  192. return that.sqrt();
  193. }
  194. template <typename T>
  195. bool is_pos(const robust_fpt<T>& that) {
  196. return that.has_pos_value();
  197. }
  198. template <typename T>
  199. bool is_neg(const robust_fpt<T>& that) {
  200. return that.has_neg_value();
  201. }
  202. template <typename T>
  203. bool is_zero(const robust_fpt<T>& that) {
  204. return that.has_zero_value();
  205. }
  206. // robust_dif consists of two not negative values: value1 and value2.
  207. // The resulting expression is equal to the value1 - value2.
  208. // Subtraction of a positive value is equivalent to the addition to value2
  209. // and subtraction of a negative value is equivalent to the addition to
  210. // value1. The structure implicitly avoids difference computation.
  211. template <typename T>
  212. class robust_dif {
  213. public:
  214. robust_dif() :
  215. positive_sum_(0),
  216. negative_sum_(0) {}
  217. explicit robust_dif(const T& value) :
  218. positive_sum_((value > 0)?value:0),
  219. negative_sum_((value < 0)?-value:0) {}
  220. robust_dif(const T& pos, const T& neg) :
  221. positive_sum_(pos),
  222. negative_sum_(neg) {}
  223. T dif() const {
  224. return positive_sum_ - negative_sum_;
  225. }
  226. T pos() const {
  227. return positive_sum_;
  228. }
  229. T neg() const {
  230. return negative_sum_;
  231. }
  232. robust_dif<T> operator-() const {
  233. return robust_dif(negative_sum_, positive_sum_);
  234. }
  235. robust_dif<T>& operator+=(const T& val) {
  236. if (!is_neg(val))
  237. positive_sum_ += val;
  238. else
  239. negative_sum_ -= val;
  240. return *this;
  241. }
  242. robust_dif<T>& operator+=(const robust_dif<T>& that) {
  243. positive_sum_ += that.positive_sum_;
  244. negative_sum_ += that.negative_sum_;
  245. return *this;
  246. }
  247. robust_dif<T>& operator-=(const T& val) {
  248. if (!is_neg(val))
  249. negative_sum_ += val;
  250. else
  251. positive_sum_ -= val;
  252. return *this;
  253. }
  254. robust_dif<T>& operator-=(const robust_dif<T>& that) {
  255. positive_sum_ += that.negative_sum_;
  256. negative_sum_ += that.positive_sum_;
  257. return *this;
  258. }
  259. robust_dif<T>& operator*=(const T& val) {
  260. if (!is_neg(val)) {
  261. positive_sum_ *= val;
  262. negative_sum_ *= val;
  263. } else {
  264. positive_sum_ *= -val;
  265. negative_sum_ *= -val;
  266. swap();
  267. }
  268. return *this;
  269. }
  270. robust_dif<T>& operator*=(const robust_dif<T>& that) {
  271. T positive_sum = this->positive_sum_ * that.positive_sum_ +
  272. this->negative_sum_ * that.negative_sum_;
  273. T negative_sum = this->positive_sum_ * that.negative_sum_ +
  274. this->negative_sum_ * that.positive_sum_;
  275. positive_sum_ = positive_sum;
  276. negative_sum_ = negative_sum;
  277. return *this;
  278. }
  279. robust_dif<T>& operator/=(const T& val) {
  280. if (!is_neg(val)) {
  281. positive_sum_ /= val;
  282. negative_sum_ /= val;
  283. } else {
  284. positive_sum_ /= -val;
  285. negative_sum_ /= -val;
  286. swap();
  287. }
  288. return *this;
  289. }
  290. private:
  291. void swap() {
  292. (std::swap)(positive_sum_, negative_sum_);
  293. }
  294. T positive_sum_;
  295. T negative_sum_;
  296. };
  297. template<typename T>
  298. robust_dif<T> operator+(const robust_dif<T>& lhs,
  299. const robust_dif<T>& rhs) {
  300. return robust_dif<T>(lhs.pos() + rhs.pos(), lhs.neg() + rhs.neg());
  301. }
  302. template<typename T>
  303. robust_dif<T> operator+(const robust_dif<T>& lhs, const T& rhs) {
  304. if (!is_neg(rhs)) {
  305. return robust_dif<T>(lhs.pos() + rhs, lhs.neg());
  306. } else {
  307. return robust_dif<T>(lhs.pos(), lhs.neg() - rhs);
  308. }
  309. }
  310. template<typename T>
  311. robust_dif<T> operator+(const T& lhs, const robust_dif<T>& rhs) {
  312. if (!is_neg(lhs)) {
  313. return robust_dif<T>(lhs + rhs.pos(), rhs.neg());
  314. } else {
  315. return robust_dif<T>(rhs.pos(), rhs.neg() - lhs);
  316. }
  317. }
  318. template<typename T>
  319. robust_dif<T> operator-(const robust_dif<T>& lhs,
  320. const robust_dif<T>& rhs) {
  321. return robust_dif<T>(lhs.pos() + rhs.neg(), lhs.neg() + rhs.pos());
  322. }
  323. template<typename T>
  324. robust_dif<T> operator-(const robust_dif<T>& lhs, const T& rhs) {
  325. if (!is_neg(rhs)) {
  326. return robust_dif<T>(lhs.pos(), lhs.neg() + rhs);
  327. } else {
  328. return robust_dif<T>(lhs.pos() - rhs, lhs.neg());
  329. }
  330. }
  331. template<typename T>
  332. robust_dif<T> operator-(const T& lhs, const robust_dif<T>& rhs) {
  333. if (!is_neg(lhs)) {
  334. return robust_dif<T>(lhs + rhs.neg(), rhs.pos());
  335. } else {
  336. return robust_dif<T>(rhs.neg(), rhs.pos() - lhs);
  337. }
  338. }
  339. template<typename T>
  340. robust_dif<T> operator*(const robust_dif<T>& lhs,
  341. const robust_dif<T>& rhs) {
  342. T res_pos = lhs.pos() * rhs.pos() + lhs.neg() * rhs.neg();
  343. T res_neg = lhs.pos() * rhs.neg() + lhs.neg() * rhs.pos();
  344. return robust_dif<T>(res_pos, res_neg);
  345. }
  346. template<typename T>
  347. robust_dif<T> operator*(const robust_dif<T>& lhs, const T& val) {
  348. if (!is_neg(val)) {
  349. return robust_dif<T>(lhs.pos() * val, lhs.neg() * val);
  350. } else {
  351. return robust_dif<T>(-lhs.neg() * val, -lhs.pos() * val);
  352. }
  353. }
  354. template<typename T>
  355. robust_dif<T> operator*(const T& val, const robust_dif<T>& rhs) {
  356. if (!is_neg(val)) {
  357. return robust_dif<T>(val * rhs.pos(), val * rhs.neg());
  358. } else {
  359. return robust_dif<T>(-val * rhs.neg(), -val * rhs.pos());
  360. }
  361. }
  362. template<typename T>
  363. robust_dif<T> operator/(const robust_dif<T>& lhs, const T& val) {
  364. if (!is_neg(val)) {
  365. return robust_dif<T>(lhs.pos() / val, lhs.neg() / val);
  366. } else {
  367. return robust_dif<T>(-lhs.neg() / val, -lhs.pos() / val);
  368. }
  369. }
  370. // Used to compute expressions that operate with sqrts with predefined
  371. // relative error. Evaluates expressions of the next type:
  372. // sum(i = 1 .. n)(A[i] * sqrt(B[i])), 1 <= n <= 4.
  373. template <typename _int, typename _fpt, typename _converter>
  374. class robust_sqrt_expr {
  375. public:
  376. enum MAX_RELATIVE_ERROR {
  377. MAX_RELATIVE_ERROR_EVAL1 = 4,
  378. MAX_RELATIVE_ERROR_EVAL2 = 7,
  379. MAX_RELATIVE_ERROR_EVAL3 = 16,
  380. MAX_RELATIVE_ERROR_EVAL4 = 25
  381. };
  382. // Evaluates expression (re = 4 EPS):
  383. // A[0] * sqrt(B[0]).
  384. _fpt eval1(_int* A, _int* B) {
  385. _fpt a = convert(A[0]);
  386. _fpt b = convert(B[0]);
  387. return a * get_sqrt(b);
  388. }
  389. // Evaluates expression (re = 7 EPS):
  390. // A[0] * sqrt(B[0]) + A[1] * sqrt(B[1]).
  391. _fpt eval2(_int* A, _int* B) {
  392. _fpt a = eval1(A, B);
  393. _fpt b = eval1(A + 1, B + 1);
  394. if ((!is_neg(a) && !is_neg(b)) ||
  395. (!is_pos(a) && !is_pos(b)))
  396. return a + b;
  397. return convert(A[0] * A[0] * B[0] - A[1] * A[1] * B[1]) / (a - b);
  398. }
  399. // Evaluates expression (re = 16 EPS):
  400. // A[0] * sqrt(B[0]) + A[1] * sqrt(B[1]) + A[2] * sqrt(B[2]).
  401. _fpt eval3(_int* A, _int* B) {
  402. _fpt a = eval2(A, B);
  403. _fpt b = eval1(A + 2, B + 2);
  404. if ((!is_neg(a) && !is_neg(b)) ||
  405. (!is_pos(a) && !is_pos(b)))
  406. return a + b;
  407. tA[3] = A[0] * A[0] * B[0] + A[1] * A[1] * B[1] - A[2] * A[2] * B[2];
  408. tB[3] = 1;
  409. tA[4] = A[0] * A[1] * 2;
  410. tB[4] = B[0] * B[1];
  411. return eval2(tA + 3, tB + 3) / (a - b);
  412. }
  413. // Evaluates expression (re = 25 EPS):
  414. // A[0] * sqrt(B[0]) + A[1] * sqrt(B[1]) +
  415. // A[2] * sqrt(B[2]) + A[3] * sqrt(B[3]).
  416. _fpt eval4(_int* A, _int* B) {
  417. _fpt a = eval2(A, B);
  418. _fpt b = eval2(A + 2, B + 2);
  419. if ((!is_neg(a) && !is_neg(b)) ||
  420. (!is_pos(a) && !is_pos(b)))
  421. return a + b;
  422. tA[0] = A[0] * A[0] * B[0] + A[1] * A[1] * B[1] -
  423. A[2] * A[2] * B[2] - A[3] * A[3] * B[3];
  424. tB[0] = 1;
  425. tA[1] = A[0] * A[1] * 2;
  426. tB[1] = B[0] * B[1];
  427. tA[2] = A[2] * A[3] * -2;
  428. tB[2] = B[2] * B[3];
  429. return eval3(tA, tB) / (a - b);
  430. }
  431. private:
  432. _int tA[5];
  433. _int tB[5];
  434. _converter convert;
  435. };
  436. } // detail
  437. } // polygon
  438. } // boost
  439. #endif // BOOST_POLYGON_DETAIL_VORONOI_ROBUST_FPT