Commit: c09e4ae08a4d97beaf919e6434eca4956a47d401
Author: Sergey Sharybin
Date:   Wed Feb 14 11:21:27 2018 +0100
Branches: master
https://developer.blender.org/rBc09e4ae08a4d97beaf919e6434eca4956a47d401

Math utils: Add clamping functions

===================================================================

M       source/blender/blenlib/BLI_math_base.h
M       source/blender/blenlib/intern/math_base_inline.c

===================================================================

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.
  *

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to