1 // Boost.Polygon library detail/voronoi_robust_fpt.hpp header file
3 // Copyright Andrii Sydorchuk 2010-2012.
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // See http://www.boost.org for updates, documentation, and revision history.
10 #ifndef BOOST_POLYGON_DETAIL_VORONOI_ROBUST_FPT
11 #define BOOST_POLYGON_DETAIL_VORONOI_ROBUST_FPT
16 // Geometry predicates with floating-point variables usually require
17 // high-precision predicates to retrieve the correct result.
18 // Epsilon robust predicates give the result within some epsilon relative
19 // error, but are a lot faster than high-precision predicates.
20 // To make algorithm robust and efficient epsilon robust predicates are
21 // used at the first step. In case of the undefined result high-precision
22 // arithmetic is used to produce required robustness. This approach
23 // requires exact computation of epsilon intervals within which epsilon
24 // robust predicates have undefined value.
25 // There are two ways to measure an error of floating-point calculations:
26 // relative error and ULPs (units in the last place).
27 // Let EPS be machine epsilon, then next inequalities have place:
28 // 1 EPS <= 1 ULP <= 2 EPS (1), 0.5 ULP <= 1 EPS <= 1 ULP (2).
29 // ULPs are good for measuring rounding errors and comparing values.
30 // Relative errors are good for computation of general relative
31 // error of formulas or expressions. So to calculate epsilon
32 // interval within which epsilon robust predicates have undefined result
33 // next schema is used:
34 // 1) Compute rounding errors of initial variables using ULPs;
35 // 2) Transform ULPs to epsilons using upper bound of the (1);
36 // 3) Compute relative error of the formula using epsilon arithmetic;
37 // 4) Transform epsilon to ULPs using upper bound of the (2);
38 // In case two values are inside undefined ULP range use high-precision
39 // arithmetic to produce the correct result, else output the result.
40 // Look at almost_equal function to see how two floating-point variables
41 // are checked to fit in the ULP range.
42 // If A has relative error of r(A) and B has relative error of r(B) then:
43 // 1) r(A + B) <= max(r(A), r(B)), for A * B >= 0;
44 // 2) r(A - B) <= B*r(A)+A*r(B)/(A-B), for A * B >= 0;
45 // 2) r(A * B) <= r(A) + r(B);
46 // 3) r(A / B) <= r(A) + r(B);
47 // In addition rounding error should be added, that is always equal to
48 // 0.5 ULP or at most 1 epsilon. As you might see from the above formulas
49 // subtraction relative error may be extremely large, that's why
50 // epsilon robust comparator class is used to store floating point values
51 // and compute subtraction as the final step of the evaluation.
52 // For further information about relative errors and ULPs try this link:
53 // http://docs.sun.com/source/806-3568/ncg_goldberg.html
60 T get_sqrt(const T& that) {
61 return (std::sqrt)(that);
65 bool is_pos(const T& that) {
70 bool is_neg(const T& that) {
75 bool is_zero(const T& that) {
79 template <typename _fpt>
82 typedef _fpt floating_point_type;
83 typedef _fpt relative_error_type;
85 // Rounding error is at most 1 EPS.
90 robust_fpt() : fpv_(0.0), re_(0.0) {}
91 explicit robust_fpt(floating_point_type fpv) :
92 fpv_(fpv), re_(0.0) {}
93 robust_fpt(floating_point_type fpv, relative_error_type error) :
94 fpv_(fpv), re_(error) {}
96 floating_point_type fpv() const { return fpv_; }
97 relative_error_type re() const { return re_; }
98 relative_error_type ulp() const { return re_; }
100 robust_fpt& operator=(const robust_fpt& that) {
101 this->fpv_ = that.fpv_;
102 this->re_ = that.re_;
106 bool has_pos_value() const {
110 bool has_neg_value() const {
114 bool has_zero_value() const {
115 return is_zero(fpv_);
118 robust_fpt operator-() const {
119 return robust_fpt(-fpv_, re_);
122 robust_fpt& operator+=(const robust_fpt& that) {
123 floating_point_type fpv = this->fpv_ + that.fpv_;
124 if ((!is_neg(this->fpv_) && !is_neg(that.fpv_)) ||
125 (!is_pos(this->fpv_) && !is_pos(that.fpv_))) {
126 this->re_ = (std::max)(this->re_, that.re_) + ROUNDING_ERROR;
128 floating_point_type temp =
129 (this->fpv_ * this->re_ - that.fpv_ * that.re_) / fpv;
132 this->re_ = temp + ROUNDING_ERROR;
138 robust_fpt& operator-=(const robust_fpt& that) {
139 floating_point_type fpv = this->fpv_ - that.fpv_;
140 if ((!is_neg(this->fpv_) && !is_pos(that.fpv_)) ||
141 (!is_pos(this->fpv_) && !is_neg(that.fpv_))) {
142 this->re_ = (std::max)(this->re_, that.re_) + ROUNDING_ERROR;
144 floating_point_type temp =
145 (this->fpv_ * this->re_ + that.fpv_ * that.re_) / fpv;
148 this->re_ = temp + ROUNDING_ERROR;
154 robust_fpt& operator*=(const robust_fpt& that) {
155 this->re_ += that.re_ + ROUNDING_ERROR;
156 this->fpv_ *= that.fpv_;
160 robust_fpt& operator/=(const robust_fpt& that) {
161 this->re_ += that.re_ + ROUNDING_ERROR;
162 this->fpv_ /= that.fpv_;
166 robust_fpt operator+(const robust_fpt& that) const {
167 floating_point_type fpv = this->fpv_ + that.fpv_;
168 relative_error_type re;
169 if ((!is_neg(this->fpv_) && !is_neg(that.fpv_)) ||
170 (!is_pos(this->fpv_) && !is_pos(that.fpv_))) {
171 re = (std::max)(this->re_, that.re_) + ROUNDING_ERROR;
173 floating_point_type temp =
174 (this->fpv_ * this->re_ - that.fpv_ * that.re_) / fpv;
177 re = temp + ROUNDING_ERROR;
179 return robust_fpt(fpv, re);
182 robust_fpt operator-(const robust_fpt& that) const {
183 floating_point_type fpv = this->fpv_ - that.fpv_;
184 relative_error_type re;
185 if ((!is_neg(this->fpv_) && !is_pos(that.fpv_)) ||
186 (!is_pos(this->fpv_) && !is_neg(that.fpv_))) {
187 re = (std::max)(this->re_, that.re_) + ROUNDING_ERROR;
189 floating_point_type temp =
190 (this->fpv_ * this->re_ + that.fpv_ * that.re_) / fpv;
193 re = temp + ROUNDING_ERROR;
195 return robust_fpt(fpv, re);
198 robust_fpt operator*(const robust_fpt& that) const {
199 floating_point_type fpv = this->fpv_ * that.fpv_;
200 relative_error_type re = this->re_ + that.re_ + ROUNDING_ERROR;
201 return robust_fpt(fpv, re);
204 robust_fpt operator/(const robust_fpt& that) const {
205 floating_point_type fpv = this->fpv_ / that.fpv_;
206 relative_error_type re = this->re_ + that.re_ + ROUNDING_ERROR;
207 return robust_fpt(fpv, re);
210 robust_fpt sqrt() const {
211 return robust_fpt(get_sqrt(fpv_),
212 re_ * static_cast<relative_error_type>(0.5) +
217 floating_point_type fpv_;
218 relative_error_type re_;
221 template <typename T>
222 robust_fpt<T> get_sqrt(const robust_fpt<T>& that) {
226 template <typename T>
227 bool is_pos(const robust_fpt<T>& that) {
228 return that.has_pos_value();
231 template <typename T>
232 bool is_neg(const robust_fpt<T>& that) {
233 return that.has_neg_value();
236 template <typename T>
237 bool is_zero(const robust_fpt<T>& that) {
238 return that.has_zero_value();
241 // robust_dif consists of two not negative values: value1 and value2.
242 // The resulting expression is equal to the value1 - value2.
243 // Subtraction of a positive value is equivalent to the addition to value2
244 // and subtraction of a negative value is equivalent to the addition to
245 // value1. The structure implicitly avoids difference computation.
246 template <typename T>
253 explicit robust_dif(const T& value) :
254 positive_sum_((value > 0)?value:0),
255 negative_sum_((value < 0)?-value:0) {}
257 robust_dif(const T& pos, const T& neg) :
259 negative_sum_(neg) {}
262 return positive_sum_ - negative_sum_;
266 return positive_sum_;
270 return negative_sum_;
273 robust_dif<T> operator-() const {
274 return robust_dif(negative_sum_, positive_sum_);
277 robust_dif<T>& operator+=(const T& val) {
279 positive_sum_ += val;
281 negative_sum_ -= val;
285 robust_dif<T>& operator+=(const robust_dif<T>& that) {
286 positive_sum_ += that.positive_sum_;
287 negative_sum_ += that.negative_sum_;
291 robust_dif<T>& operator-=(const T& val) {
293 negative_sum_ += val;
295 positive_sum_ -= val;
299 robust_dif<T>& operator-=(const robust_dif<T>& that) {
300 positive_sum_ += that.negative_sum_;
301 negative_sum_ += that.positive_sum_;
305 robust_dif<T>& operator*=(const T& val) {
307 positive_sum_ *= val;
308 negative_sum_ *= val;
310 positive_sum_ *= -val;
311 negative_sum_ *= -val;
317 robust_dif<T>& operator*=(const robust_dif<T>& that) {
318 T positive_sum = this->positive_sum_ * that.positive_sum_ +
319 this->negative_sum_ * that.negative_sum_;
320 T negative_sum = this->positive_sum_ * that.negative_sum_ +
321 this->negative_sum_ * that.positive_sum_;
322 positive_sum_ = positive_sum;
323 negative_sum_ = negative_sum;
327 robust_dif<T>& operator/=(const T& val) {
329 positive_sum_ /= val;
330 negative_sum_ /= val;
332 positive_sum_ /= -val;
333 negative_sum_ /= -val;
341 (std::swap)(positive_sum_, negative_sum_);
349 robust_dif<T> operator+(const robust_dif<T>& lhs,
350 const robust_dif<T>& rhs) {
351 return robust_dif<T>(lhs.pos() + rhs.pos(), lhs.neg() + rhs.neg());
355 robust_dif<T> operator+(const robust_dif<T>& lhs, const T& rhs) {
357 return robust_dif<T>(lhs.pos() + rhs, lhs.neg());
359 return robust_dif<T>(lhs.pos(), lhs.neg() - rhs);
364 robust_dif<T> operator+(const T& lhs, const robust_dif<T>& rhs) {
366 return robust_dif<T>(lhs + rhs.pos(), rhs.neg());
368 return robust_dif<T>(rhs.pos(), rhs.neg() - lhs);
373 robust_dif<T> operator-(const robust_dif<T>& lhs,
374 const robust_dif<T>& rhs) {
375 return robust_dif<T>(lhs.pos() + rhs.neg(), lhs.neg() + rhs.pos());
379 robust_dif<T> operator-(const robust_dif<T>& lhs, const T& rhs) {
381 return robust_dif<T>(lhs.pos(), lhs.neg() + rhs);
383 return robust_dif<T>(lhs.pos() - rhs, lhs.neg());
388 robust_dif<T> operator-(const T& lhs, const robust_dif<T>& rhs) {
390 return robust_dif<T>(lhs + rhs.neg(), rhs.pos());
392 return robust_dif<T>(rhs.neg(), rhs.pos() - lhs);
397 robust_dif<T> operator*(const robust_dif<T>& lhs,
398 const robust_dif<T>& rhs) {
399 T res_pos = lhs.pos() * rhs.pos() + lhs.neg() * rhs.neg();
400 T res_neg = lhs.pos() * rhs.neg() + lhs.neg() * rhs.pos();
401 return robust_dif<T>(res_pos, res_neg);
405 robust_dif<T> operator*(const robust_dif<T>& lhs, const T& val) {
407 return robust_dif<T>(lhs.pos() * val, lhs.neg() * val);
409 return robust_dif<T>(-lhs.neg() * val, -lhs.pos() * val);
414 robust_dif<T> operator*(const T& val, const robust_dif<T>& rhs) {
416 return robust_dif<T>(val * rhs.pos(), val * rhs.neg());
418 return robust_dif<T>(-val * rhs.neg(), -val * rhs.pos());
423 robust_dif<T> operator/(const robust_dif<T>& lhs, const T& val) {
425 return robust_dif<T>(lhs.pos() / val, lhs.neg() / val);
427 return robust_dif<T>(-lhs.neg() / val, -lhs.pos() / val);
431 // Used to compute expressions that operate with sqrts with predefined
432 // relative error. Evaluates expressions of the next type:
433 // sum(i = 1 .. n)(A[i] * sqrt(B[i])), 1 <= n <= 4.
434 template <typename _int, typename _fpt, typename _converter>
435 class robust_sqrt_expr {
437 enum MAX_RELATIVE_ERROR {
438 MAX_RELATIVE_ERROR_EVAL1 = 4,
439 MAX_RELATIVE_ERROR_EVAL2 = 7,
440 MAX_RELATIVE_ERROR_EVAL3 = 16,
441 MAX_RELATIVE_ERROR_EVAL4 = 25
444 // Evaluates expression (re = 4 EPS):
445 // A[0] * sqrt(B[0]).
446 _fpt eval1(_int* A, _int* B) {
447 _fpt a = convert(A[0]);
448 _fpt b = convert(B[0]);
449 return a * get_sqrt(b);
452 // Evaluates expression (re = 7 EPS):
453 // A[0] * sqrt(B[0]) + A[1] * sqrt(B[1]).
454 _fpt eval2(_int* A, _int* B) {
455 _fpt a = eval1(A, B);
456 _fpt b = eval1(A + 1, B + 1);
457 if ((!is_neg(a) && !is_neg(b)) ||
458 (!is_pos(a) && !is_pos(b)))
460 return convert(A[0] * A[0] * B[0] - A[1] * A[1] * B[1]) / (a - b);
463 // Evaluates expression (re = 16 EPS):
464 // A[0] * sqrt(B[0]) + A[1] * sqrt(B[1]) + A[2] * sqrt(B[2]).
465 _fpt eval3(_int* A, _int* B) {
466 _fpt a = eval2(A, B);
467 _fpt b = eval1(A + 2, B + 2);
468 if ((!is_neg(a) && !is_neg(b)) ||
469 (!is_pos(a) && !is_pos(b)))
471 tA[3] = A[0] * A[0] * B[0] + A[1] * A[1] * B[1] - A[2] * A[2] * B[2];
473 tA[4] = A[0] * A[1] * 2;
475 return eval2(tA + 3, tB + 3) / (a - b);
479 // Evaluates expression (re = 25 EPS):
480 // A[0] * sqrt(B[0]) + A[1] * sqrt(B[1]) +
481 // A[2] * sqrt(B[2]) + A[3] * sqrt(B[3]).
482 _fpt eval4(_int* A, _int* B) {
483 _fpt a = eval2(A, B);
484 _fpt b = eval2(A + 2, B + 2);
485 if ((!is_neg(a) && !is_neg(b)) ||
486 (!is_pos(a) && !is_pos(b)))
488 tA[0] = A[0] * A[0] * B[0] + A[1] * A[1] * B[1] -
489 A[2] * A[2] * B[2] - A[3] * A[3] * B[3];
491 tA[1] = A[0] * A[1] * 2;
493 tA[2] = A[2] * A[3] * -2;
495 return eval3(tA, tB) / (a - b);
507 #endif // BOOST_POLYGON_DETAIL_VORONOI_ROBUST_FPT