Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorHans Goudey <h.goudey@me.com>2022-03-03 01:09:17 +0300
committerHans Goudey <h.goudey@me.com>2022-03-03 01:09:17 +0300
commit2600806c2ef1b626e3d82f14a7facfcc7f9f0992 (patch)
treea9af164290a83246e29f0dffd5217c5304ed21cd /source
parentecba8c1243045e72c92f4575393821fc6bf2665f (diff)
Fix: BLI math clamp doesn't work
Return type was wrong, output of std::clamp wasn't used.
Diffstat (limited to 'source')
-rw-r--r--source/blender/blenlib/BLI_math_vector.hh10
-rw-r--r--source/blender/blenlib/tests/BLI_math_vector_test.cc23
2 files changed, 28 insertions, 5 deletions
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