diff options
author | Hans Goudey <h.goudey@me.com> | 2022-02-15 01:54:07 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2022-02-15 01:54:07 +0300 |
commit | ce570dd434f8395bb9e1d2746aaab1c93a0d0a27 (patch) | |
tree | bf4de2b0f9f07a2c0e660a47f0b371d509d925ff /source | |
parent | 8a9a7199abbe83f6d2d7ceeec9052b50cd4202a9 (diff) |
BLI: Implement templated math functions for basic types
This is meant to complement the `blender::math` functions recently
added by D13791. It's sometimes desired to template an operation to work
on vector types, but also basic types like `float` and `int`. This patch
adds that ability with a new `BLI_math_base.hh` header. Particularly
useful examples are `midpoint` and `interpolate`, but I'm sure others
will be added in the future.
Implementing the functions separately, rather than as `if constexpr`
branches in the existing header is meant to be more scalable, for
the possibility of even more math types in the future.
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/blenlib/BLI_math_base.hh | 113 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_math_vec_types.hh | 11 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_math_vector.hh | 42 | ||||
-rw-r--r-- | source/blender/blenlib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | source/blender/blenlib/tests/BLI_math_base_test.cc | 23 |
5 files changed, 175 insertions, 15 deletions
diff --git a/source/blender/blenlib/BLI_math_base.hh b/source/blender/blenlib/BLI_math_base.hh new file mode 100644 index 00000000000..9684867afd3 --- /dev/null +++ b/source/blender/blenlib/BLI_math_base.hh @@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later + * Copyright 2022 Blender Foundation. */ + +#pragma once + +/** \file + * \ingroup bli + */ + +#include <algorithm> +#include <cmath> +#include <type_traits> + +#include "BLI_math_base_safe.h" +#include "BLI_math_vector.h" +#include "BLI_utildefines.h" + +#ifdef WITH_GMP +# include "BLI_math_mpq.hh" +#endif + +namespace blender::math { + +#ifdef WITH_GMP +# define BLI_ENABLE_IF_FLT(T) \ + BLI_ENABLE_IF((std::is_floating_point_v<T> || std::is_same_v<T, mpq_class>)) +#else +# define BLI_ENABLE_IF_FLT(T) BLI_ENABLE_IF((std::is_floating_point_v<T>)) +#endif + +#define BLI_ENABLE_IF_INT(T) BLI_ENABLE_IF((std::is_integral_v<T>)) + +template<typename T> inline bool is_zero(const T &a) +{ + return a == T(0); +} + +template<typename T> inline bool is_any_zero(const T &a) +{ + return is_zero(a); +} + +template<typename T> inline T abs(const T &a) +{ + return std::abs(a); +} + +template<typename T> inline T min(const T &a, const T &b) +{ + return std::min(a, b); +} + +template<typename T> inline T max(const T &a, const T &b) +{ + return std::max(a, b); +} + +template<typename T> inline T clamp(const T &a, const T &min, const T &max) +{ + return std::clamp(a, min, max); +} + +template<typename T, BLI_ENABLE_IF_FLT(T)> inline T mod(const T &a, const T &b) +{ + return std::fmod(a, b); +} + +template<typename T, BLI_ENABLE_IF_FLT(T)> inline T safe_mod(const T &a, const T &b) +{ + return (b != 0) ? std::fmod(a, b) : 0; +} + +template<typename T> inline void min_max(const T &vector, T &min_vec, T &max_vec) +{ + min_vec = min(vector, min_vec); + max_vec = max(vector, max_vec); +} + +template<typename T, BLI_ENABLE_IF_FLT(T)> inline T safe_divide(const T &a, const T &b) +{ + return (b != 0) ? a / b : T(0.0f); +} + +template<typename T, BLI_ENABLE_IF_FLT(T)> inline T floor(const T &a) +{ + return std::floor(a); +} + +template<typename T, BLI_ENABLE_IF_FLT(T)> inline T ceil(const T &a) +{ + return std::ceil(a); +} + +template<typename T, BLI_ENABLE_IF_FLT(T)> inline T fract(const T &a) +{ + return a - std::floor(a); +} + +template<typename T, typename FactorT, BLI_ENABLE_IF_FLT(T), BLI_ENABLE_IF_FLT(FactorT)> +inline T interpolate(const T &a, const T &b, const FactorT &t) +{ + return a * (1 - t) + b * t; +} + +template<typename T, BLI_ENABLE_IF_FLT(T)> inline T midpoint(const T &a, const T &b) +{ + return (a + b) * 0.5; +} + +#undef BLI_ENABLE_IF_FLT +#undef BLI_ENABLE_IF_INT + +} // namespace blender::math diff --git a/source/blender/blenlib/BLI_math_vec_types.hh b/source/blender/blenlib/BLI_math_vec_types.hh index 140367b6241..81e59b157ee 100644 --- a/source/blender/blenlib/BLI_math_vec_types.hh +++ b/source/blender/blenlib/BLI_math_vec_types.hh @@ -579,4 +579,15 @@ using double2 = vec_base<double, 2>; using double3 = vec_base<double, 3>; using double4 = vec_base<double, 4>; +template<typename T> constexpr bool is_math_vec_type = false; +template<typename BaseType, int Size> +constexpr bool is_math_vec_type<vec_base<BaseType, Size>> = true; + +static_assert(is_math_vec_type<int2>); +static_assert(is_math_vec_type<uint4>); +static_assert(is_math_vec_type<float2>); +static_assert(is_math_vec_type<vec_base<float, 20>>); +static_assert(!is_math_vec_type<int>); +static_assert(!is_math_vec_type<float>); + } // namespace blender diff --git a/source/blender/blenlib/BLI_math_vector.hh b/source/blender/blenlib/BLI_math_vector.hh index 302ab39a0b9..7c39565cedc 100644 --- a/source/blender/blenlib/BLI_math_vector.hh +++ b/source/blender/blenlib/BLI_math_vector.hh @@ -35,17 +35,25 @@ namespace blender::math { #define bT typename T::base_type +template<typename T> +inline constexpr bool is_float_vector = (std::is_floating_point_v<typename T::base_type> #ifdef WITH_GMP -# define BLI_ENABLE_IF_FLT_VEC(T) \ - BLI_ENABLE_IF((std::is_floating_point_v<typename T::base_type> || \ - std::is_same_v<typename T::base_type, mpq_class>)) -#else -# define BLI_ENABLE_IF_FLT_VEC(T) BLI_ENABLE_IF((std::is_floating_point_v<typename T::base_type>)) + || std::is_same_v<typename T::base_type, mpq_class> #endif +); + +template<typename T> +inline constexpr bool is_integral_vector = std::is_integral_v<typename T::base_type>; + +/* In this file, only implement math functions for the vector types. This allows other + * files to use overloading to implement the same functions with different types. */ +#define BLI_ENABLE_IF_VEC(T) BLI_ENABLE_IF((is_math_vec_type<T>)) + +#define BLI_ENABLE_IF_FLT_VEC(T) BLI_ENABLE_IF_VEC(T), BLI_ENABLE_IF((is_float_vector<T>)) -#define BLI_ENABLE_IF_INT_VEC(T) BLI_ENABLE_IF((std::is_integral_v<typename T::base_type>)) +#define BLI_ENABLE_IF_INT_VEC(T) BLI_ENABLE_IF_VEC(T), BLI_ENABLE_IF((is_integral_vector<T>)) -template<typename T> inline bool is_zero(const T &a) +template<typename T, BLI_ENABLE_IF_VEC(T)> inline bool is_zero(const T &a) { for (int i = 0; i < T::type_length; i++) { if (a[i] != bT(0)) { @@ -55,7 +63,7 @@ template<typename T> inline bool is_zero(const T &a) return true; } -template<typename T> inline bool is_any_zero(const T &a) +template<typename T, BLI_ENABLE_IF_VEC(T)> inline bool is_any_zero(const T &a) { for (int i = 0; i < T::type_length; i++) { if (a[i] == bT(0)) { @@ -65,7 +73,7 @@ template<typename T> inline bool is_any_zero(const T &a) return false; } -template<typename T> inline T abs(const T &a) +template<typename T, BLI_ENABLE_IF_VEC(T)> inline T abs(const T &a) { T result; for (int i = 0; i < T::type_length; i++) { @@ -74,7 +82,7 @@ template<typename T> inline T abs(const T &a) return result; } -template<typename T> inline T min(const T &a, const T &b) +template<typename T, BLI_ENABLE_IF_VEC(T)> inline T min(const T &a, const T &b) { T result; for (int i = 0; i < T::type_length; i++) { @@ -83,7 +91,7 @@ template<typename T> inline T min(const T &a, const T &b) return result; } -template<typename T> inline T max(const T &a, const T &b) +template<typename T, BLI_ENABLE_IF_VEC(T)> inline T max(const T &a, const T &b) { T result; for (int i = 0; i < T::type_length; i++) { @@ -92,7 +100,8 @@ template<typename T> inline T max(const T &a, const T &b) return result; } -template<typename T> inline T clamp(const T &a, const T &min_v, const T &max_v) +template<typename T, BLI_ENABLE_IF_VEC(T)> +inline T clamp(const T &a, const T &min_v, const T &max_v) { T result = a; for (int i = 0; i < T::type_length; i++) { @@ -101,7 +110,8 @@ template<typename T> inline T clamp(const T &a, const T &min_v, const T &max_v) return result; } -template<typename T> inline T clamp(const T &a, const bT &min_v, const bT &max_v) +template<typename T, BLI_ENABLE_IF_VEC(T)> +inline T clamp(const T &a, const bT &min_v, const bT &max_v) { T result = a; for (int i = 0; i < T::type_length; i++) { @@ -151,7 +161,8 @@ template<typename T, BLI_ENABLE_IF_FLT_VEC(T)> inline T safe_mod(const T &a, bT return result; } -template<typename T> inline void min_max(const T &vector, T &min_vec, T &max_vec) +template<typename T, BLI_ENABLE_IF_VEC(T)> +inline void min_max(const T &vector, T &min_vec, T &max_vec) { min_vec = min(vector, min_vec); max_vec = max(vector, max_vec); @@ -207,7 +218,7 @@ template<typename T, BLI_ENABLE_IF_FLT_VEC(T)> inline bT dot(const T &a, const T return result; } -template<typename T> inline bT length_manhattan(const T &a) +template<typename T, BLI_ENABLE_IF_VEC(T)> inline bT length_manhattan(const T &a) { bT result = std::abs(a[0]); for (int i = 1; i < T::type_length; i++) { @@ -364,6 +375,7 @@ template<typename T> struct isect_result { template<typename T, BLI_ENABLE_IF_FLT_VEC(T)> isect_result<T> isect_seg_seg(const T &v1, const T &v2, const T &v3, const T &v4); +#undef BLI_ENABLE_IF_VEC #undef BLI_ENABLE_IF_FLT_VEC #undef BLI_ENABLE_IF_INT_VEC #undef bT diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt index 29015084679..e67d673eb73 100644 --- a/source/blender/blenlib/CMakeLists.txt +++ b/source/blender/blenlib/CMakeLists.txt @@ -220,6 +220,7 @@ set(SRC BLI_map.hh BLI_map_slots.hh BLI_math.h + BLI_math_base.hh BLI_math_base.h BLI_math_base_safe.h BLI_math_bits.h diff --git a/source/blender/blenlib/tests/BLI_math_base_test.cc b/source/blender/blenlib/tests/BLI_math_base_test.cc index 33acefeeac2..34da00cb4bf 100644 --- a/source/blender/blenlib/tests/BLI_math_base_test.cc +++ b/source/blender/blenlib/tests/BLI_math_base_test.cc @@ -4,6 +4,12 @@ #include "BLI_math.h" +#include "BLI_math_vector.hh" + +#include "BLI_math_base.hh" + +namespace blender::tests { + /* In tests below, when we are using -1.0f as max_diff value, we actually turn the function into a * pure-ULP one. */ @@ -131,3 +137,20 @@ TEST(math_base, FloorPowerOf10) EXPECT_NEAR(floor_power_of_10(100.1f), 100.0f, 1e-4f); EXPECT_NEAR(floor_power_of_10(99.9f), 10.0f, 1e-4f); } + +TEST(math_base, MinVectorAndFloat) +{ + EXPECT_EQ(math::min(1.0f, 2.0f), 1.0f); +} + +TEST(math_base, ClampInt) +{ + EXPECT_EQ(math::clamp(111, -50, 101), 101); +} + +TEST(math_base, Midpoint) +{ + EXPECT_NEAR(math::midpoint(100.0f, 200.0f), 150.0f, 1e-4f); +} + +} // namespace blender::tests |