Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenkernel/BKE_attribute_math.hh
| Show First 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | |||||
| * This is typically used to interpolate values within a triangle. | * This is typically used to interpolate values within a triangle. | ||||
| * \{ */ | * \{ */ | ||||
| template<typename T> T mix3(const float3 &weights, const T &v0, const T &v1, const T &v2); | template<typename T> T mix3(const float3 &weights, const T &v0, const T &v1, const T &v2); | ||||
| template<> | template<> | ||||
| inline int8_t mix3(const float3 &weights, const int8_t &v0, const int8_t &v1, const int8_t &v2) | 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) | template<> inline bool mix3(const float3 &weights, const bool &v0, const bool &v1, const bool &v2) | ||||
| { | { | ||||
| return (weights.x * v0 + weights.y * v1 + weights.z * v2) >= 0.5f; | return (weights.x * v0 + weights.y * v1 + weights.z * v2) >= 0.5f; | ||||
| } | } | ||||
| template<> inline int mix3(const float3 &weights, const int &v0, const int &v1, const int &v2) | 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<> | template<> | ||||
| inline float mix3(const float3 &weights, const float &v0, const float &v1, const float &v2) | inline float mix3(const float3 &weights, const float &v0, const float &v1, const float &v2) | ||||
| { | { | ||||
| return weights.x * v0 + weights.y * v1 + weights.z * v2; | return weights.x * v0 + weights.y * v1 + weights.z * v2; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | |||||
| template<> inline bool mix2(const float factor, const bool &a, const bool &b) | template<> inline bool mix2(const float factor, const bool &a, const bool &b) | ||||
| { | { | ||||
| return ((1.0f - factor) * a + factor * b) >= 0.5f; | return ((1.0f - factor) * a + factor * b) >= 0.5f; | ||||
| } | } | ||||
| template<> inline int8_t mix2(const float factor, const int8_t &a, const int8_t &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) | 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) | template<> inline float mix2(const float factor, const float &a, const float &b) | ||||
| { | { | ||||
| return (1.0f - factor) * a + factor * b; | return (1.0f - factor) * a + factor * b; | ||||
| } | } | ||||
| template<> inline float2 mix2(const float factor, const float2 &a, const float2 &b) | template<> inline float2 mix2(const float factor, const float2 &a, const float2 &b) | ||||
| ▲ Show 20 Lines • Show All 203 Lines • ▼ Show 20 Lines | template<> struct DefaultMixerStruct<ColorGeometry4f> { | ||||
| using type = ColorGeometry4fMixer; | using type = ColorGeometry4fMixer; | ||||
| }; | }; | ||||
| template<> struct DefaultMixerStruct<ColorGeometry4b> { | template<> struct DefaultMixerStruct<ColorGeometry4b> { | ||||
| using type = ColorGeometry4bMixer; | using type = ColorGeometry4bMixer; | ||||
| }; | }; | ||||
| template<> struct DefaultMixerStruct<int> { | template<> struct DefaultMixerStruct<int> { | ||||
| static int double_to_int(const double &value) | 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 | /* 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. */ | * uses double instead of float so that it is accurate for all 32 bit integers. */ | ||||
| using type = SimpleMixerWithAccumulationType<int, double, double_to_int>; | using type = SimpleMixerWithAccumulationType<int, double, double_to_int>; | ||||
| }; | }; | ||||
| template<> struct DefaultMixerStruct<bool> { | template<> struct DefaultMixerStruct<bool> { | ||||
| static bool float_to_bool(const float &value) | static bool float_to_bool(const float &value) | ||||
| { | { | ||||
| return value >= 0.5f; | return value >= 0.5f; | ||||
| } | } | ||||
| /* Store interpolated booleans in a float temporary. | /* Store interpolated booleans in a float temporary. | ||||
| * Otherwise information provided by weights is easily rounded away. */ | * Otherwise information provided by weights is easily rounded away. */ | ||||
| using type = SimpleMixerWithAccumulationType<bool, float, float_to_bool>; | using type = SimpleMixerWithAccumulationType<bool, float, float_to_bool>; | ||||
| }; | }; | ||||
| template<> struct DefaultMixerStruct<int8_t> { | template<> struct DefaultMixerStruct<int8_t> { | ||||
| static int8_t float_to_int8_t(const float &value) | 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. */ | /* Store interpolated 8 bit integers in a float temporarily to increase accuracy. */ | ||||
| using type = SimpleMixerWithAccumulationType<int8_t, float, float_to_int8_t>; | using type = SimpleMixerWithAccumulationType<int8_t, float, float_to_int8_t>; | ||||
| }; | }; | ||||
| template<typename T> struct DefaultPropatationMixerStruct { | template<typename T> struct DefaultPropatationMixerStruct { | ||||
| /* Use void by default. This can be checked for in `if constexpr` statements. */ | /* Use void by default. This can be checked for in `if constexpr` statements. */ | ||||
| using type = typename DefaultMixerStruct<T>::type; | using type = typename DefaultMixerStruct<T>::type; | ||||
| Show All 21 Lines | |||||