voronoi_robust_fpt.hpp 14 KB

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