diff options
author | Jacques Lucke <jacques@blender.org> | 2022-05-18 18:00:19 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2022-05-18 18:00:38 +0300 |
commit | 2e70af5cd527e2c1f40e6f3af22adc3337714744 (patch) | |
tree | c5e55df22f2e53d044e541c8d5b1392de30786f5 /source/blender/blenlib | |
parent | 342e12d6d92198bba8355562600a2f97bb45fed5 (diff) |
Fix T97761: incorrect mixing of integers
Sometimes integers are mixed using float weights. In those cases
the mixed result has to be converted from into float again.
Previously, this was done using a simple cast, which was unexpected
because e.g. 14.999 would be cast to 14 instead of 15.
Now, the values are rounded properly.
This can affect existing files unfortunately without a good option
for versioning. Gladly, very few files seem to depend on the details
of the old behavior.
Differential Revision: https://developer.blender.org/D14892
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_math_base.hh | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/source/blender/blenlib/BLI_math_base.hh b/source/blender/blenlib/BLI_math_base.hh index 81f5343056e..034c6968c94 100644 --- a/source/blender/blenlib/BLI_math_base.hh +++ b/source/blender/blenlib/BLI_math_base.hh @@ -108,12 +108,20 @@ template<typename T, 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; + auto result = a * (1 - t) + b * t; + if constexpr (std::is_integral_v<T> && std::is_floating_point_v<FactorT>) { + result = std::round(result); + } + return result; } template<typename T> inline T midpoint(const T &a, const T &b) { - return (a + b) * T(0.5); + auto result = (a + b) * T(0.5); + if constexpr (std::is_integral_v<T>) { + result = std::round(result); + } + return result; } } // namespace blender::math |