diff options
author | Bastien Montagne <montagne29@wanadoo.fr> | 2015-07-10 15:32:35 +0300 |
---|---|---|
committer | Bastien Montagne <montagne29@wanadoo.fr> | 2015-07-10 16:02:43 +0300 |
commit | 7837f0e8332f3726e0322b0c48b0da4d7c2d5813 (patch) | |
tree | 4e0b9d6fb5faa6bacd080484d7ef625566d32699 /source/blender/blenlib/intern/math_vector_inline.c | |
parent | bbcbd2eed9081e0c1127d32e41592a8dc6b5604b (diff) |
BLI_math 'compare' cleanup & enhancements.
This commit:
* Adds a 'compare_ff' function for absolute 'almost equal' comparison of floats.
* Makes 'compare_vxvx' functions use that new 'compare_ff' one.
* Adds a 'compare_ff_relative' function for secured ulp-based relative comparison of floats.
* Adds matching 'compare_vxvx_relative' functions.
* Adds some basic tests for compare_ff_relative.
See https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
Note that we could replace our python/mathutils' EXPP_FloatsAreEqual() by BLI's compare_ff_relative
(using a very small absolute max_diff), but these do not have exact same behavior...
Left a comment there for now, we can do it later if/when we are sure it won't break anything!
Diffstat (limited to 'source/blender/blenlib/intern/math_vector_inline.c')
-rw-r--r-- | source/blender/blenlib/intern/math_vector_inline.c | 55 |
1 files changed, 34 insertions, 21 deletions
diff --git a/source/blender/blenlib/intern/math_vector_inline.c b/source/blender/blenlib/intern/math_vector_inline.c index fb33fed33e4..c21b09748c9 100644 --- a/source/blender/blenlib/intern/math_vector_inline.c +++ b/source/blender/blenlib/intern/math_vector_inline.c @@ -966,23 +966,47 @@ MINLINE bool equals_v4v4(const float v1[4], const float v2[4]) MINLINE bool compare_v2v2(const float v1[2], const float v2[2], const float limit) { - if (fabsf(v1[0] - v2[0]) <= limit) - if (fabsf(v1[1] - v2[1]) <= limit) - return true; - - return false; + return (compare_ff(v1[0], v2[0], limit) && + compare_ff(v1[1], v2[1], limit)); } MINLINE bool compare_v3v3(const float v1[3], const float v2[3], const float limit) { - if (fabsf(v1[0] - v2[0]) <= limit) - if (fabsf(v1[1] - v2[1]) <= limit) - if (fabsf(v1[2] - v2[2]) <= limit) - return true; + return (compare_ff(v1[0], v2[0], limit) && + compare_ff(v1[1], v2[1], limit) && + compare_ff(v1[2], v2[2], limit)); +} + +MINLINE bool compare_v4v4(const float v1[4], const float v2[4], const float limit) +{ + return (compare_ff(v1[0], v2[0], limit) && + compare_ff(v1[1], v2[1], limit) && + compare_ff(v1[2], v2[2], limit) && + compare_ff(v1[3], v2[3], limit)); +} - return false; +MINLINE bool compare_v2v2_relative(const float v1[2], const float v2[2], const float limit, const int max_ulps) +{ + return (compare_ff_relative(v1[0], v2[0], limit, max_ulps) && + compare_ff_relative(v1[1], v2[1], limit, max_ulps)); +} + +MINLINE bool compare_v3v3_relative(const float v1[3], const float v2[3], const float limit, const int max_ulps) +{ + return (compare_ff_relative(v1[0], v2[0], limit, max_ulps) && + compare_ff_relative(v1[1], v2[1], limit, max_ulps) && + compare_ff_relative(v1[2], v2[2], limit, max_ulps)); } +MINLINE bool compare_v4v4_relative(const float v1[4], const float v2[4], const float limit, const int max_ulps) +{ + return (compare_ff_relative(v1[0], v2[0], limit, max_ulps) && + compare_ff_relative(v1[1], v2[1], limit, max_ulps) && + compare_ff_relative(v1[2], v2[2], limit, max_ulps) && + compare_ff_relative(v1[3], v2[3], limit, max_ulps)); +} + + MINLINE bool compare_len_v3v3(const float v1[3], const float v2[3], const float limit) { float x, y, z; @@ -1005,17 +1029,6 @@ MINLINE bool compare_len_squared_v3v3(const float v1[3], const float v2[3], cons return ((x * x + y * y + z * z) <= limit_sq); } -MINLINE bool compare_v4v4(const float v1[4], const float v2[4], const float limit) -{ - if (fabsf(v1[0] - v2[0]) <= limit) - if (fabsf(v1[1] - v2[1]) <= limit) - if (fabsf(v1[2] - v2[2]) <= limit) - if (fabsf(v1[3] - v2[3]) <= limit) - return true; - - return false; -} - /** * <pre> * + l1 |