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
This commit is contained in:
Jacques Lucke 2022-05-18 17:00:19 +02:00
parent 342e12d6d9
commit 2e70af5cd5
Notes: blender-bot 2023-02-14 02:27:56 +01:00
Referenced by issue #98298, Regression: Attribute capture yields broken results
Referenced by issue #97761, Truncation Error for Integers Capture on Points after Resampling
2 changed files with 16 additions and 8 deletions

View File

@ -56,7 +56,7 @@ template<typename T> T mix3(const float3 &weights, const T &v0, const T &v1, con
template<>
inline int8_t mix3(const float3 &weights, const int8_t &v0, const int8_t &v1, const int8_t &v2)
{
return static_cast<int8_t>(weights.x * v0 + weights.y * v1 + weights.z * v2);
return static_cast<int8_t>(std::round(weights.x * v0 + weights.y * v1 + weights.z * v2));
}
template<> inline bool mix3(const float3 &weights, const bool &v0, const bool &v1, const bool &v2)
@ -66,7 +66,7 @@ template<> inline bool mix3(const float3 &weights, const bool &v0, const bool &v
template<> inline int mix3(const float3 &weights, const int &v0, const int &v1, const int &v2)
{
return static_cast<int>(weights.x * v0 + weights.y * v1 + weights.z * v2);
return static_cast<int>(std::round(weights.x * v0 + weights.y * v1 + weights.z * v2));
}
template<>
@ -131,12 +131,12 @@ template<> inline bool mix2(const float factor, const bool &a, const bool &b)
template<> inline int8_t mix2(const float factor, const int8_t &a, const int8_t &b)
{
return static_cast<int8_t>((1.0f - factor) * a + factor * b);
return static_cast<int8_t>(std::round((1.0f - factor) * a + factor * b));
}
template<> inline int mix2(const float factor, const int &a, const int &b)
{
return static_cast<int>((1.0f - factor) * a + factor * b);
return static_cast<int>(std::round((1.0f - factor) * a + factor * b));
}
template<> inline float mix2(const float factor, const float &a, const float &b)
@ -356,7 +356,7 @@ template<> struct DefaultMixerStruct<ColorGeometry4b> {
template<> struct DefaultMixerStruct<int> {
static int double_to_int(const double &value)
{
return static_cast<int>(value);
return static_cast<int>(std::round(value));
}
/* Store interpolated ints in a double temporarily, so that weights are handled correctly. It
* uses double instead of float so that it is accurate for all 32 bit integers. */
@ -375,7 +375,7 @@ template<> struct DefaultMixerStruct<bool> {
template<> struct DefaultMixerStruct<int8_t> {
static int8_t float_to_int8_t(const float &value)
{
return static_cast<int8_t>(value);
return static_cast<int8_t>(std::round(value));
}
/* Store interpolated 8 bit integers in a float temporarily to increase accuracy. */
using type = SimpleMixerWithAccumulationType<int8_t, float, float_to_int8_t>;

View File

@ -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