Changeset View
Changeset View
Standalone View
Standalone View
source/blender/nodes/NOD_math_functions.hh
| Show All 30 Lines | struct FloatMathOperationInfo { | ||||
| FloatMathOperationInfo() = delete; | FloatMathOperationInfo() = delete; | ||||
| FloatMathOperationInfo(StringRefNull title_case_name, StringRefNull shader_name) | FloatMathOperationInfo(StringRefNull title_case_name, StringRefNull shader_name) | ||||
| : title_case_name(title_case_name), shader_name(shader_name) | : title_case_name(title_case_name), shader_name(shader_name) | ||||
| { | { | ||||
| } | } | ||||
| }; | }; | ||||
| const FloatMathOperationInfo *get_float_math_operation_info(const int operation); | const FloatMathOperationInfo *get_float_math_operation_info(const int operation); | ||||
| const FloatMathOperationInfo *get_float_compare_operation_info(const int operation); | |||||
| /** | /** | ||||
| * This calls the `callback` with two arguments: | * This calls the `callback` with two arguments: | ||||
| * 1. The math function that takes a float as input and outputs a new float. | * 1. The math function that takes a float as input and outputs a new float. | ||||
| * 2. A #FloatMathOperationInfo struct reference. | * 2. A #FloatMathOperationInfo struct reference. | ||||
| * Returns true when the callback has been called, otherwise false. | * Returns true when the callback has been called, otherwise false. | ||||
| * | * | ||||
| * The math function that is passed to the callback is actually a lambda function that is different | * The math function that is passed to the callback is actually a lambda function that is different | ||||
| ▲ Show 20 Lines • Show All 145 Lines • ▼ Show 20 Lines | switch (operation) { | ||||
| case NODE_MATH_SMOOTH_MAX: | case NODE_MATH_SMOOTH_MAX: | ||||
| return dispatch([](float a, float b, float c) { return -smoothminf(-a, -b, -c); }); | return dispatch([](float a, float b, float c) { return -smoothminf(-a, -b, -c); }); | ||||
| case NODE_MATH_WRAP: | case NODE_MATH_WRAP: | ||||
| return dispatch([](float a, float b, float c) { return wrapf(a, b, c); }); | return dispatch([](float a, float b, float c) { return wrapf(a, b, c); }); | ||||
| } | } | ||||
| return false; | return false; | ||||
| } | } | ||||
| /** | |||||
| * This is similar to try_dispatch_float_math_fl_to_fl, just with a different callback signature. | |||||
| */ | |||||
| template<typename Callback> | |||||
| inline bool try_dispatch_float_math_fl_fl_to_bool(const FloatCompareOperation operation, | |||||
| Callback &&callback) | |||||
| { | |||||
| const FloatMathOperationInfo *info = get_float_compare_operation_info(operation); | |||||
| if (info == nullptr) { | |||||
| return false; | |||||
| } | |||||
| /* This is just an utility function to keep the individual cases smaller. */ | |||||
| auto dispatch = [&](auto math_function) -> bool { | |||||
| callback(math_function, *info); | |||||
| return true; | |||||
| }; | |||||
| switch (operation) { | |||||
| case NODE_FLOAT_COMPARE_LESS_THAN: | |||||
| return dispatch([](float a, float b) { return a < b; }); | |||||
| case NODE_FLOAT_COMPARE_LESS_EQUAL: | |||||
| return dispatch([](float a, float b) { return a <= b; }); | |||||
| case NODE_FLOAT_COMPARE_GREATER_THAN: | |||||
| return dispatch([](float a, float b) { return a > b; }); | |||||
| case NODE_FLOAT_COMPARE_GREATER_EQUAL: | |||||
| return dispatch([](float a, float b) { return a >= b; }); | |||||
| default: | |||||
| return false; | |||||
| } | |||||
| return false; | |||||
| } | |||||
| } // namespace blender::nodes | } // namespace blender::nodes | ||||