Download presentation

Presentation is loading. Please wait.

Published byJacob Ross Modified over 3 years ago

1
Numerical Robustness for Geometric Calculations Christer Ericson Sony Computer Entertainment

2
Numerical Robustness for Geometric Calculations Christer Ericson Sony Computer Entertainment EPSILON is NOT !

3
Takeaway An appreciation of the pitfalls inherent in working with floating-point arithmetic. An appreciation of the pitfalls inherent in working with floating-point arithmetic. Tools for addressing the robustness of floating-point based code. Tools for addressing the robustness of floating-point based code. Probably something else too. Probably something else too.

4
Somewhat shameless plug! The contents of this presentation is discussed in further detail in my book: The contents of this presentation is discussed in further detail in my book:

5
THE PROBLEM Floating-point arithmetic

6
Floating-point numbers Real numbers must be approximated Real numbers must be approximated Floating-point numbers Floating-point numbers Fixed-point numbers (integers) Fixed-point numbers (integers) Rational numbers Rational numbers Homogeneous representation Homogeneous representation If we could work in real arithmetic, I wouldnt be having this talk! If we could work in real arithmetic, I wouldnt be having this talk!

7
Floating-point numbers IEEE-754 single precision IEEE-754 single precision 1 bit sign 1 bit sign 8 bit exponent (biased) 8 bit exponent (biased) 23 bits fraction (24 bits mantissa, including hidden bit) 23 bits fraction (24 bits mantissa, including hidden bit) s Exponent (e) Fraction (f) This is a normalized format This is a normalized format

8
Floating-point numbers IEEE-754 representable numbers: IEEE-754 representable numbers: ExponentFractionSignValue 0

9
Floating-point numbers In IEEE-754, domain extended with: In IEEE-754, domain extended with: –Inf, +Inf, NaN –Inf, +Inf, NaN Some examples: Some examples: a/0 = +Inf, if a > 0 a/0 = +Inf, if a > 0 a/0 = –Inf, if a < 0 a/0 = –Inf, if a < 0 0/0 = Inf – Inf = ±Inf · 0 = NaN 0/0 = Inf – Inf = ±Inf · 0 = NaN Known as Infinity Arithmetic (IA) Known as Infinity Arithmetic (IA)

10
Floating-point numbers IA is a potential source of robustness errors! IA is a potential source of robustness errors! +Inf and –Inf compare as normal +Inf and –Inf compare as normal But NaN compares as unordered But NaN compares as unordered NaN != NaN is true NaN != NaN is true All other comparisons involving NaNs are false All other comparisons involving NaNs are false These expressions are not equivalent: These expressions are not equivalent: if (a > b) X(); else Y(); if (a <= b) Y(); else X();

11
Floating-point numbers But IA provides a nice feature too But IA provides a nice feature too Allows not having to test for div-by-zero Allows not having to test for div-by-zero Removes test branch from inner loop Removes test branch from inner loop Useful for SIMD code Useful for SIMD code (Although same approach usually works for non-IEEE CPUs too.) (Although same approach usually works for non-IEEE CPUs too.)

12
Floating-point numbers Irregular number line Irregular number line Spacing increases the farther away from zero a number is located Spacing increases the farther away from zero a number is located Number range for exponent k+1 has twice the spacing of the one for exponent k Number range for exponent k+1 has twice the spacing of the one for exponent k Equally many representable numbers from one exponent to another Equally many representable numbers from one exponent to another 0

13
Floating-point numbers Consequence of irregular spacing: Consequence of irregular spacing: – ( ) = 0 – ( ) = 0 (– ) + 1 = 1 (– ) + 1 = 1 Thus, not associative (in general): Thus, not associative (in general): (a + b) + c != a + (b + c) (a + b) + c != a + (b + c) Source of endless errors! Source of endless errors! 0

14
Floating-point numbers All discrete representations have non- representable points All discrete representations have non- representable points A BC D Q P

15
The floating-point grid In floating-point, behavior changes based on position, due to the irregular spacing! In floating-point, behavior changes based on position, due to the irregular spacing!

16
EXAMPLE Polygon splitting

17
Sutherland-Hodgman clipping algorithm Sutherland-Hodgman clipping algorithm A BB C I D C AD J

18
Polygon splitting Enter floating-point errors! Enter floating-point errors! I P Q P Q IFIF

19
Polygon splitting ABCD split against a plane ABCD split against a plane A B D C A B D J I C

20
Polygon splitting Thick planes to the rescue! Thick planes to the rescue! P Q Desired invariant: OnPlane(I, plane) = true where: I = IntersectionPoint(PQ, plane)

21
Polygon splitting Thick planes also help bound the error Thick planes also help bound the error PQP'Q' PQ e e P'Q'

22
A B D C Polygon splitting ABCD split against a thick plane ABCD split against a thick plane A B D C I

23
Polygon splitting Cracks introduced by inconsistent ordering Cracks introduced by inconsistent ordering A B C D A B C D

24
EXAMPLE (K-d) tree robustness

25
Robustness problems for: Robustness problems for: Insertion of primitives Insertion of primitives Querying (collision detection) Querying (collision detection) Same problems apply to: Same problems apply to: All spatial partitioning schemes! All spatial partitioning schemes! (BSP trees, grids, octrees, quadtrees, …) (BSP trees, grids, octrees, quadtrees, …)

26
(K-d) tree robustness Query robustness Query robustness 12 I P Q IFIF C A B

27
(K-d) tree robustness Insertion robustness Insertion robustness 12 C A B 12 C A B I IFIF

28
(K-d) tree robustness How to achieve robustness? How to achieve robustness? Insert primitives conservatively Insert primitives conservatively Accounting for errors in querying and insertion Accounting for errors in querying and insertion Can then ignore problem for queries Can then ignore problem for queries

29
EXAMPLE Ray-triangle test

30
Common approach: Common approach: Compute intersection point P of ray R with plane of triangle T Compute intersection point P of ray R with plane of triangle T Test if P lies inside boundaries of T Test if P lies inside boundaries of T Alas, this is not robust! Alas, this is not robust!

31
Ray-triangle test A problem configuration A problem configuration R P A B C D

32
Ray-triangle test Intersecting R against one plane Intersecting R against one plane R P

33
Ray-triangle test Intersecting R against the other plane Intersecting R against the other plane R P

34
Ray-triangle test Robust test must share calculations for shared edge AB Robust test must share calculations for shared edge AB Perform test directly in 3D! Perform test directly in 3D! Let ray be Let ray be Then, sign of says whether is left or right of AB Then, sign of says whether is left or right of AB If R left of all edges, R intersects CCW triangle If R left of all edges, R intersects CCW triangle Only then compute P Only then compute P Still errors, but managable Still errors, but managable

35
Ray-triangle test Fat tests are also robust! Fat tests are also robust! P

36
EXAMPLES SUMMARY Achieve robustness through… Achieve robustness through… (Correct) use of tolerances (Correct) use of tolerances Sharing of calculations Sharing of calculations Use of fat primitives Use of fat primitives

37
TOLERANCES

38
Tolerance comparisons Absolute tolerance Absolute tolerance Relative tolerance Relative tolerance Combined tolerance Combined tolerance Integer test Integer test

39
Absolute tolerance Almost never used correctly! Almost never used correctly! What should EPSILON be? What should EPSILON be? Typically arbitrary small number used! OMFG!! Typically arbitrary small number used! OMFG!! if (Abs(x – y) <= EPSILON) … Comparing two floats for equality:

40
Absolute tolerances Delta step to next representable number: DecimalHexNext representable number 10.00x x x42C80000x ,000.00x447A0000x ,000.00x461C4000x ,000.00x47C35000x ,000,000.00x x ,000,000.00x4B189680x + 1.0

41
Absolute tolerances Möller-Trumbore ray-triangle code: #define EPSILON #define DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])... /* if determinant is near zero, ray lies in plane of triangle */ det = DOT(edge1, pvec);... if (det > -EPSILON && det < EPSILON) // Abs(det) < EPSILON return 0; Written using doubles. Written using doubles. Change to float without changing epsilon? Change to float without changing epsilon? DOT({10,10,10},{10,10,10}) breaks test! DOT({10,10,10},{10,10,10}) breaks test!

42
Relative tolerance Comparing two floats for equality: Epsilon scaled by magnitude of inputs Epsilon scaled by magnitude of inputs But consider Abs(x)<1.0, Abs(y)<1.0 But consider Abs(x)<1.0, Abs(y)<1.0 if (Abs(x – y) <= EPSILON * Max(Abs(x), Abs(y)) …

43
Combined tolerance Comparing two floats for equality: Absolute test for Abs(x)1.0, Abs(y)1.0 Absolute test for Abs(x)1.0, Abs(y)1.0 Relative test otherwise! Relative test otherwise! if (Abs(x – y) <= EPSILON * Max(1.0f, Abs(x), Abs(y)) …

44
Integer test bool AlmostEqual2sComplement(float A, float B, int maxUlps) { // Make sure maxUlps is non-negative and small enough that the // default NAN won't compare as equal to anything. assert(maxUlps > 0 && maxUlps < 4 * 1024 * 1024); int aInt = *(int*)&A; // Make aInt lexicographically ordered as a twos-complement int if (aInt < 0) aInt = 0x aInt; // Make bInt lexicographically ordered as a twos-complement int int bInt = *(int*)&B; if (bInt < 0) bInt = 0x bInt; int intDiff = abs(aInt - bInt); if (intDiff <= maxUlps) return true; return false; } Also suggested, an integer-based test:

45
Floating-point numbers Caveat: Intel uses 80-bit format internally Caveat: Intel uses 80-bit format internally Unless told otherwise. Unless told otherwise. Errors dependent on what code generated. Errors dependent on what code generated. Gives different results in debug and release. Gives different results in debug and release.

46
EXACTARITHMETIC (and semi-exact ditto)

47
Exact arithmetic Hey! Integer arithmetic is exact Hey! Integer arithmetic is exact As long as there is no overflow As long as there is no overflow Closed under +, –, and * Closed under +, –, and * Not closed under / but can often remove divisions through cross multiplication Not closed under / but can often remove divisions through cross multiplication

48
Exact arithmetic Example: Does C project onto AB ? Example: Does C project onto AB ? AB C D float t = Dot(AC, AB) / Dot(AB, AB); if (t >= 0.0f && t <= 1.0f)... /* do something */ int tnum = Dot(AC, AB), tdenom = Dot(AB, AB); if (tnum >= 0 && tnum <= tdenom)... /* do something */ Floats: Floats: Integers: Integers:

49
Exact arithmetic Another example: Another example: A B C D

50
Exact arithmetic Tests Tests Boolean, can be evaluated exactly Boolean, can be evaluated exactly Constructions Constructions Non-Boolean, cannot be done exactly Non-Boolean, cannot be done exactly

51
Exact arithmetic Tests, often expressed as determinant predicates. E.g. Tests, often expressed as determinant predicates. E.g. Shewchuk's predicates well-known example Shewchuk's predicates well-known example Evaluates using extended-precision arithmetic (EPA) Evaluates using extended-precision arithmetic (EPA) EPA is expensive to evaluate EPA is expensive to evaluate Limit EPA use through floating-point filter Limit EPA use through floating-point filter Common filter is interval arithmetic Common filter is interval arithmetic

52
Exact arithmetic Interval arithmetic Interval arithmetic x = [1,3] = { x R | 1 x 3 } x = [1,3] = { x R | 1 x 3 } Rules: Rules: [a,b] + [c,d] = [a+c,b+d] [a,b] + [c,d] = [a+c,b+d] [a,b] – [c,d] = [a–d,b–c] [a,b] – [c,d] = [a–d,b–c] [a,b] * [c,d] = [min(ac,ad,bc,bd), max(ac,ad,bc,bd)] [a,b] * [c,d] = [min(ac,ad,bc,bd), max(ac,ad,bc,bd)] [a,b] / [c,d] = [a,b] * [1/d,1/c] for 0 [c,d] [a,b] / [c,d] = [a,b] * [1/d,1/c] for 0 [c,d] E.g. [100,101] + [10,12] = [110,113] E.g. [100,101] + [10,12] = [110,113]

53
Exact arithmetic Interval arithmetic Interval arithmetic Intervals must be rounded up/down to nearest machine-representable number Intervals must be rounded up/down to nearest machine-representable number Is a reliable calculation Is a reliable calculation

54
References Ericson, Christer. Real-Time Collision Detection. Morgan Kaufmann, Ericson, Christer. Real-Time Collision Detection. Morgan Kaufmann, Hoffmann, Christoph. Geometric and Solid Modeling: An Introduction. Morgan Kaufmann, Hoffmann, Christoph. Geometric and Solid Modeling: An Introduction. Morgan Kaufmann, Ratschek, Helmut. Jon Rokne. Geometric Computations with Interval and New Robust Methods. Horwood Publishing, Ratschek, Helmut. Jon Rokne. Geometric Computations with Interval and New Robust Methods. Horwood Publishing, Hoffmann, Christoph. Robustness in Geometric Computations. JCISE 1, 2001, pp Hoffmann, Christoph. Robustness in Geometric Computations. JCISE 1, 2001, pp Santisteve, Francisco. Robust Geometric Computation (RGC), State of the Art. Technical report, Santisteve, Francisco. Robust Geometric Computation (RGC), State of the Art. Technical report, Schirra, Stefan. Robustness and precision issues in geometric computation. Research Report MPI-I , Max Planck Institute for Computer Science, Schirra, Stefan. Robustness and precision issues in geometric computation. Research Report MPI-I , Max Planck Institute for Computer Science, Shewchuk, Jonathan. Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates. Discrete & Computational Geometry 18(3): , October Shewchuk, Jonathan. Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates. Discrete & Computational Geometry 18(3): , October BOOKS PAPERS

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google