diff options
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/blenlib/BLI_math_vec_types.hh | 22 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_math_vector.hh | 10 | ||||
-rw-r--r-- | source/blender/blenlib/tests/BLI_math_vec_types_test.cc | 25 |
3 files changed, 44 insertions, 13 deletions
diff --git a/source/blender/blenlib/BLI_math_vec_types.hh b/source/blender/blenlib/BLI_math_vec_types.hh index b6b3d15aefe..389307e331d 100644 --- a/source/blender/blenlib/BLI_math_vec_types.hh +++ b/source/blender/blenlib/BLI_math_vec_types.hh @@ -64,16 +64,6 @@ template<typename T> uint64_t vector_hash(const T &vec) return result; } -template<typename T, int Size> inline bool is_any_zero(const vec_struct_base<T, Size> &a) -{ - for (int i = 0; i < Size; i++) { - if (a[i] == T(0)) { - return true; - } - } - return false; -} - } // namespace math template<typename T, int Size> struct vec_base : public vec_struct_base<T, Size> { @@ -353,7 +343,9 @@ template<typename T, int Size> struct vec_base : public vec_struct_base<T, Size> friend vec_base operator/(const vec_base &a, const vec_base &b) { - BLI_assert(!math::is_any_zero(b)); + for (int i = 0; i < Size; i++) { + BLI_assert(b[i] != T(0)); + } BLI_VEC_OP_IMPL(ret, i, ret[i] = a[i] / b[i]); } @@ -365,7 +357,9 @@ template<typename T, int Size> struct vec_base : public vec_struct_base<T, Size> friend vec_base operator/(T a, const vec_base &b) { - BLI_assert(!math::is_any_zero(b)); + for (int i = 0; i < Size; i++) { + BLI_assert(b[i] != T(0)); + } BLI_VEC_OP_IMPL(ret, i, ret[i] = a / b[i]); } @@ -509,7 +503,9 @@ template<typename T, int Size> struct vec_base : public vec_struct_base<T, Size> BLI_INT_OP(T) friend vec_base operator%(const vec_base &a, const vec_base &b) { - BLI_assert(!math::is_any_zero(b)); + for (int i = 0; i < Size; i++) { + BLI_assert(b[i] != T(0)); + } BLI_VEC_OP_IMPL(ret, i, ret[i] = a[i] % b[i]); } diff --git a/source/blender/blenlib/BLI_math_vector.hh b/source/blender/blenlib/BLI_math_vector.hh index 3bb89bb26b2..7c848eeb145 100644 --- a/source/blender/blenlib/BLI_math_vector.hh +++ b/source/blender/blenlib/BLI_math_vector.hh @@ -39,6 +39,16 @@ template<typename T, int Size> inline bool is_zero(const vec_base<T, Size> &a) return true; } +template<typename T, int Size> inline bool is_any_zero(const vec_base<T, Size> &a) +{ + for (int i = 0; i < Size; i++) { + if (a[i] == T(0)) { + return true; + } + } + return false; +} + template<typename T, int Size> inline vec_base<T, Size> abs(const vec_base<T, Size> &a) { vec_base<T, Size> result; diff --git a/source/blender/blenlib/tests/BLI_math_vec_types_test.cc b/source/blender/blenlib/tests/BLI_math_vec_types_test.cc index 07eb6b29a20..7590d77525b 100644 --- a/source/blender/blenlib/tests/BLI_math_vec_types_test.cc +++ b/source/blender/blenlib/tests/BLI_math_vec_types_test.cc @@ -146,4 +146,29 @@ TEST(math_vec_types, VectorTypeConversion) EXPECT_EQ(d[1], -1.0); } +TEST(math_vec_types, Divide) +{ + float2 a(1.0f, 2.0f); + float2 b(0.5f, 2.0f); + float2 result = a / b; + EXPECT_FLOAT_EQ(result.x, 2.0f); + EXPECT_FLOAT_EQ(result.y, 1.0f); +} + +TEST(math_vec_types, DivideFloatByVector) +{ + float a = 2.0f; + float2 b(0.5f, 2.0f); + float2 result = a / b; + EXPECT_FLOAT_EQ(result.x, 4.0f); + EXPECT_FLOAT_EQ(result.y, 1.0f); +} + +TEST(math_vec_types, DivideFloatByVectorSmall) +{ + float2 result = 2.0f / float2(2.0f); + EXPECT_FLOAT_EQ(result.x, 1.0f); + EXPECT_FLOAT_EQ(result.y, 1.0f); +} + } // namespace blender::tests |