[Bf-blender-cvs] [14c9f64def7] master: BLI_math: add clamp_v# and clamp_v#_v#v# utility functions

Tiago Chaves noreply at git.blender.org
Wed Mar 4 01:09:37 CET 2020


Commit: 14c9f64def7439aa4b6b74d57ba9bc2b82177dd6
Author: Tiago Chaves
Date:   Wed Mar 4 10:24:21 2020 +1100
Branches: master
https://developer.blender.org/rB14c9f64def7439aa4b6b74d57ba9bc2b82177dd6

BLI_math: add clamp_v# and clamp_v#_v#v# utility functions

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

M	source/blender/blenlib/BLI_math_vector.h
M	source/blender/blenlib/intern/math_vector.c
A	tests/gtests/blenlib/BLI_math_vector_test.cc
M	tests/gtests/blenlib/CMakeLists.txt

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

diff --git a/source/blender/blenlib/BLI_math_vector.h b/source/blender/blenlib/BLI_math_vector.h
index 19a378269fe..e210bae11d6 100644
--- a/source/blender/blenlib/BLI_math_vector.h
+++ b/source/blender/blenlib/BLI_math_vector.h
@@ -440,6 +440,13 @@ void dist_ensure_v2_v2fl(float v1[2], const float v2[2], const float dist);
 
 void axis_sort_v3(const float axis_values[3], int r_axis_order[3]);
 
+void clamp_v2(float vec[2], const float min, const float max);
+void clamp_v3(float vec[3], const float min, const float max);
+void clamp_v4(float vec[4], const float min, const float max);
+void clamp_v2_v2v2(float vec[2], const float min[2], const float max[2]);
+void clamp_v3_v3v3(float vec[3], const float min[3], const float max[3]);
+void clamp_v4_v4v4(float vec[4], const float min[4], const float max[4]);
+
 /***************************** Array Functions *******************************/
 /* follow fixed length vector function conventions. */
 double dot_vn_vn(const float *array_src_a,
diff --git a/source/blender/blenlib/intern/math_vector.c b/source/blender/blenlib/intern/math_vector.c
index 5919b7e1dd6..d1b36884038 100644
--- a/source/blender/blenlib/intern/math_vector.c
+++ b/source/blender/blenlib/intern/math_vector.c
@@ -1051,6 +1051,48 @@ void axis_sort_v3(const float axis_values[3], int r_axis_order[3])
 #undef SWAP_AXIS
 }
 
+void clamp_v2(float vec[2], const float min, const float max)
+{
+  CLAMP(vec[0], min, max);
+  CLAMP(vec[1], min, max);
+}
+
+void clamp_v3(float vec[3], const float min, const float max)
+{
+  CLAMP(vec[0], min, max);
+  CLAMP(vec[1], min, max);
+  CLAMP(vec[2], min, max);
+}
+
+void clamp_v4(float vec[4], const float min, const float max)
+{
+  CLAMP(vec[0], min, max);
+  CLAMP(vec[1], min, max);
+  CLAMP(vec[2], min, max);
+  CLAMP(vec[3], min, max);
+}
+
+void clamp_v2_v2v2(float vec[2], const float min[2], const float max[2])
+{
+  CLAMP(vec[0], min[0], max[0]);
+  CLAMP(vec[1], min[1], max[1]);
+}
+
+void clamp_v3_v3v3(float vec[3], const float min[3], const float max[3])
+{
+  CLAMP(vec[0], min[0], max[0]);
+  CLAMP(vec[1], min[1], max[1]);
+  CLAMP(vec[2], min[2], max[2]);
+}
+
+void clamp_v4_v4v4(float vec[4], const float min[4], const float max[4])
+{
+  CLAMP(vec[0], min[0], max[0]);
+  CLAMP(vec[1], min[1], max[1]);
+  CLAMP(vec[2], min[2], max[2]);
+  CLAMP(vec[3], min[3], max[3]);
+}
+
 /***************************** Array Functions *******************************/
 
 MINLINE double sqr_db(double f)
diff --git a/tests/gtests/blenlib/BLI_math_vector_test.cc b/tests/gtests/blenlib/BLI_math_vector_test.cc
new file mode 100644
index 00000000000..7e75a521d4c
--- /dev/null
+++ b/tests/gtests/blenlib/BLI_math_vector_test.cc
@@ -0,0 +1,47 @@
+/* Apache License, Version 2.0 */
+
+#include "testing/testing.h"
+
+#include "BLI_math.h"
+
+TEST(math_vector, ClampVecWithFloats)
+{
+  const float min = 0.0f;
+  const float max = 1.0f;
+
+  float a[2] = {-1.0f, -1.0f};
+  clamp_v2(a, min, max);
+  EXPECT_FLOAT_EQ(0.0f, a[0]);
+  EXPECT_FLOAT_EQ(0.0f, a[1]);
+
+  float b[2] = {0.5f, 0.5f};
+  clamp_v2(b, min, max);
+  EXPECT_FLOAT_EQ(0.5f, b[0]);
+  EXPECT_FLOAT_EQ(0.5f, b[1]);
+
+  float c[2] = {2.0f, 2.0f};
+  clamp_v2(c, min, max);
+  EXPECT_FLOAT_EQ(1.0f, c[0]);
+  EXPECT_FLOAT_EQ(1.0f, c[1]);
+}
+
+TEST(math_vector, ClampVecWithVecs)
+{
+  const float min[2] = {0.0f, 2.0f};
+  const float max[2] = {1.0f, 3.0f};
+
+  float a[2] = {-1.0f, -1.0f};
+  clamp_v2_v2v2(a, min, max);
+  EXPECT_FLOAT_EQ(0.0f, a[0]);
+  EXPECT_FLOAT_EQ(2.0f, a[1]);
+
+  float b[2] = {0.5f, 2.5f};
+  clamp_v2_v2v2(b, min, max);
+  EXPECT_FLOAT_EQ(0.5f, b[0]);
+  EXPECT_FLOAT_EQ(2.5f, b[1]);
+
+  float c[2] = {2.0f, 4.0f};
+  clamp_v2_v2v2(c, min, max);
+  EXPECT_FLOAT_EQ(1.0f, c[0]);
+  EXPECT_FLOAT_EQ(3.0f, c[1]);
+}
diff --git a/tests/gtests/blenlib/CMakeLists.txt b/tests/gtests/blenlib/CMakeLists.txt
index 1a104fcb746..119b54fa0d4 100644
--- a/tests/gtests/blenlib/CMakeLists.txt
+++ b/tests/gtests/blenlib/CMakeLists.txt
@@ -58,6 +58,7 @@ BLENDER_TEST(BLI_map "bf_blenlib")
 BLENDER_TEST(BLI_math_base "bf_blenlib")
 BLENDER_TEST(BLI_math_color "bf_blenlib")
 BLENDER_TEST(BLI_math_geom "bf_blenlib")
+BLENDER_TEST(BLI_math_vector "bf_blenlib")
 BLENDER_TEST(BLI_memiter "bf_blenlib")
 BLENDER_TEST(BLI_optional "bf_blenlib")
 BLENDER_TEST(BLI_path_util "${BLI_path_util_extra_libs}")



More information about the Bf-blender-cvs mailing list