[Bf-blender-cvs] [2600806c2ef] master: Fix: BLI math clamp doesn't work
Hans Goudey
noreply at git.blender.org
Wed Mar 2 23:09:27 CET 2022
Commit: 2600806c2ef1b626e3d82f14a7facfcc7f9f0992
Author: Hans Goudey
Date: Wed Mar 2 17:09:17 2022 -0500
Branches: master
https://developer.blender.org/rB2600806c2ef1b626e3d82f14a7facfcc7f9f0992
Fix: BLI math clamp doesn't work
Return type was wrong, output of std::clamp wasn't used.
===================================================================
M source/blender/blenlib/BLI_math_vector.hh
M source/blender/blenlib/tests/BLI_math_vector_test.cc
===================================================================
diff --git a/source/blender/blenlib/BLI_math_vector.hh b/source/blender/blenlib/BLI_math_vector.hh
index 7c848eeb145..b1a3242ae52 100644
--- a/source/blender/blenlib/BLI_math_vector.hh
+++ b/source/blender/blenlib/BLI_math_vector.hh
@@ -79,13 +79,13 @@ inline vec_base<T, Size> max(const vec_base<T, Size> &a, const vec_base<T, Size>
}
template<typename T, int Size>
-inline T clamp(const vec_base<T, Size> &a,
- const vec_base<T, Size> &min,
- const vec_base<T, Size> &max)
+inline vec_base<T, Size> clamp(const vec_base<T, Size> &a,
+ const vec_base<T, Size> &min,
+ const vec_base<T, Size> &max)
{
vec_base<T, Size> result = a;
for (int i = 0; i < Size; i++) {
- std::clamp(result[i], min[i], max[i]);
+ result[i] = std::clamp(result[i], min[i], max[i]);
}
return result;
}
@@ -95,7 +95,7 @@ inline vec_base<T, Size> clamp(const vec_base<T, Size> &a, const T &min, const T
{
vec_base<T, Size> result = a;
for (int i = 0; i < Size; i++) {
- std::clamp(result[i], min, max);
+ result[i] = std::clamp(result[i], min, max);
}
return result;
}
diff --git a/source/blender/blenlib/tests/BLI_math_vector_test.cc b/source/blender/blenlib/tests/BLI_math_vector_test.cc
index 43e329bd778..35a111f04db 100644
--- a/source/blender/blenlib/tests/BLI_math_vector_test.cc
+++ b/source/blender/blenlib/tests/BLI_math_vector_test.cc
@@ -4,6 +4,10 @@
#include "BLI_math.h"
+#include "BLI_math_vector.hh"
+
+namespace blender::tests {
+
TEST(math_vector, ClampVecWithFloats)
{
const float min = 0.0f;
@@ -63,3 +67,22 @@ TEST(math_vector, test_invert_v3_safe)
EXPECT_FLOAT_EQ(inverted_unsafe[1], v3_without_zeroes[1]);
EXPECT_FLOAT_EQ(inverted_unsafe[2], v3_without_zeroes[2]);
}
+
+TEST(math_vector, Clamp)
+{
+ const int3 value(0, 100, -100);
+ const int3 min(5, 40, -95);
+ const int3 max(7, 45, 5);
+
+ const int3 result = math::clamp(value, min, max);
+ EXPECT_EQ(result.x, 5);
+ EXPECT_EQ(result.y, 45);
+ EXPECT_EQ(result.z, -95);
+
+ const int3 result_2 = math::clamp(value, -50, 50);
+ EXPECT_EQ(result_2.x, 0);
+ EXPECT_EQ(result_2.y, 50);
+ EXPECT_EQ(result_2.z, -50);
+}
+
+} // namespace blender::tests
\ No newline at end of file
More information about the Bf-blender-cvs
mailing list