diff options
author | Hans Goudey <h.goudey@me.com> | 2022-03-25 17:57:10 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2022-03-25 17:57:10 +0300 |
commit | 378022c7973db54f1ad8cf335a9359c3bf27ddb5 (patch) | |
tree | a69090518a1f7c0ca7661b370dedd05ee37f6b52 /source/blender/blenlib/BLI_math_base.hh | |
parent | 1243cb803e7d096d27e2b5dcdfa05bf0367e248c (diff) |
BLI: Adjust interpolation to support integers, other tweaks
In order to allow interpolation of integers with a float, add a separate
template parameter for the factor and multiplication types.
Also move some helper constexpr variables to the "base" header
(reversing the dependency to "base" -> "vector").
This also adds a distance function for scalar types, which is
helpful to allow sharing code between vectors and basic types.
Differential Revision: https://developer.blender.org/D14446
Diffstat (limited to 'source/blender/blenlib/BLI_math_base.hh')
-rw-r--r-- | source/blender/blenlib/BLI_math_base.hh | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/source/blender/blenlib/BLI_math_base.hh b/source/blender/blenlib/BLI_math_base.hh index 6a988eda8a9..83f414f853a 100644 --- a/source/blender/blenlib/BLI_math_base.hh +++ b/source/blender/blenlib/BLI_math_base.hh @@ -12,7 +12,6 @@ #include <type_traits> #include "BLI_math_base_safe.h" -#include "BLI_math_vec_types.hh" #include "BLI_utildefines.h" #ifdef WITH_GMP @@ -21,6 +20,15 @@ namespace blender::math { +template<typename T> +inline constexpr bool is_math_float_type = (std::is_floating_point_v<T> +#ifdef WITH_GMP + || std::is_same_v<T, mpq_class> +#endif +); + +template<typename T> inline constexpr bool is_math_integral_type = std::is_integral_v<T>; + template<typename T> inline bool is_zero(const T &a) { return a == T(0); @@ -84,19 +92,23 @@ template<typename T, BLI_ENABLE_IF((is_math_float_type<T>))> inline T ceil(const return std::ceil(a); } +template<typename T> inline T distance(const T &a, const T &b) +{ + return std::abs(a - b); +} + template<typename T, BLI_ENABLE_IF((is_math_float_type<T>))> inline T fract(const T &a) { return a - std::floor(a); } -template<typename T, BLI_ENABLE_IF((is_math_float_type<T>))> -inline T interpolate(const T &a, const T &b, const T &t) +template<typename T, typename FactorT, BLI_ENABLE_IF((is_math_float_type<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((is_math_float_type<T>))> -inline T midpoint(const T &a, const T &b) +template<typename T> inline T midpoint(const T &a, const T &b) { return (a + b) * T(0.5); } |