diff options
author | Sergey Sharybin <sergey.vfx@gmail.com> | 2018-02-14 13:21:27 +0300 |
---|---|---|
committer | Sergey Sharybin <sergey.vfx@gmail.com> | 2018-02-14 13:21:27 +0300 |
commit | c09e4ae08a4d97beaf919e6434eca4956a47d401 (patch) | |
tree | 4b0bc20e3910cf7b860f6752bee38074466e9ac0 | |
parent | a966852362bbeffab9e8b297da2d75bf53e69051 (diff) |
Math utils: Add clamping functions
-rw-r--r-- | source/blender/blenlib/BLI_math_base.h | 4 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_base_inline.c | 20 |
2 files changed, 24 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_math_base.h b/source/blender/blenlib/BLI_math_base.h index 377b9325717..3b24cae018d 100644 --- a/source/blender/blenlib/BLI_math_base.h +++ b/source/blender/blenlib/BLI_math_base.h @@ -132,6 +132,10 @@ MINLINE int max_iiii(int a, int b, int c, int d); MINLINE size_t min_zz(size_t a, size_t b); MINLINE size_t max_zz(size_t a, size_t b); +MINLINE int clamp_i(int value, int min, int max); +MINLINE float clamp_f(float value, float min, float max); +MINLINE size_t clamp_z(size_t value, size_t min, size_t max); + MINLINE int compare_ff(float a, float b, const float max_diff); MINLINE int compare_ff_relative(float a, float b, const float max_diff, const int max_ulps); diff --git a/source/blender/blenlib/intern/math_base_inline.c b/source/blender/blenlib/intern/math_base_inline.c index 2f5b0f420b1..144198f2c06 100644 --- a/source/blender/blenlib/intern/math_base_inline.c +++ b/source/blender/blenlib/intern/math_base_inline.c @@ -324,6 +324,26 @@ MINLINE size_t max_zz(size_t a, size_t b) return (b < a) ? a : b; } +MINLINE int clamp_i(int value, int min, int max) +{ + return min_ii(max_ii(value, min), max); +} + +MINLINE float clamp_f(float value, float min, float max) +{ + if (value > max) { + return max; + } else if (value < min) { + return min; + } + return value; +} + +MINLINE size_t clamp_z(size_t value, size_t min, size_t max) +{ + return min_zz(max_zz(value, min), max); +} + /** * Almost-equal for IEEE floats, using absolute difference method. * |