2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2020-12-02 13:25:25 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "DNA_node_types.h"
|
|
|
|
|
|
|
|
#include "BLI_math_base_safe.h"
|
|
|
|
#include "BLI_math_rotation.h"
|
2022-02-15 10:27:03 -06:00
|
|
|
#include "BLI_math_vector.hh"
|
2020-12-02 13:25:25 +01:00
|
|
|
#include "BLI_string_ref.hh"
|
|
|
|
|
2022-04-26 17:12:34 +02:00
|
|
|
#include "FN_multi_function_builder.hh"
|
|
|
|
|
2020-12-02 13:25:25 +01:00
|
|
|
namespace blender::nodes {
|
|
|
|
|
|
|
|
struct FloatMathOperationInfo {
|
|
|
|
StringRefNull title_case_name;
|
|
|
|
StringRefNull shader_name;
|
|
|
|
|
|
|
|
FloatMathOperationInfo() = delete;
|
|
|
|
FloatMathOperationInfo(StringRefNull title_case_name, StringRefNull shader_name)
|
|
|
|
: title_case_name(title_case_name), shader_name(shader_name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-07 11:38:08 +11:00
|
|
|
const FloatMathOperationInfo *get_float_math_operation_info(int operation);
|
|
|
|
const FloatMathOperationInfo *get_float3_math_operation_info(int operation);
|
|
|
|
const FloatMathOperationInfo *get_float_compare_operation_info(int operation);
|
2020-12-02 13:25:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This calls the `callback` with two arguments:
|
2022-04-11 11:41:00 +10:00
|
|
|
* 1. The math function that takes a float as input and outputs a new float.
|
|
|
|
* 2. A #FloatMathOperationInfo struct reference.
|
2020-12-02 13:25:25 +01:00
|
|
|
* 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
|
|
|
|
* for every operation. Therefore, if the callback is templated on the math function, it will get
|
|
|
|
* instantiated for every operation separately. This has two benefits:
|
2022-04-11 11:41:00 +10:00
|
|
|
* - The compiler can optimize the callback for every operation separately.
|
|
|
|
* - A static variable declared in the callback will be generated for every operation separately.
|
2020-12-02 13:25:25 +01:00
|
|
|
*
|
|
|
|
* If separate instantiations are not desired, the callback can also take a function pointer with
|
|
|
|
* the following signature as input instead: float (*math_function)(float a).
|
|
|
|
*/
|
|
|
|
template<typename Callback>
|
|
|
|
inline bool try_dispatch_float_math_fl_to_fl(const int operation, Callback &&callback)
|
|
|
|
{
|
|
|
|
const FloatMathOperationInfo *info = get_float_math_operation_info(operation);
|
|
|
|
if (info == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-26 17:12:34 +02:00
|
|
|
static auto exec_preset_fast = fn::CustomMF_presets::AllSpanOrSingle();
|
|
|
|
static auto exec_preset_slow = fn::CustomMF_presets::Materialized();
|
|
|
|
|
2020-12-02 13:25:25 +01:00
|
|
|
/* This is just an utility function to keep the individual cases smaller. */
|
2022-04-26 17:12:34 +02:00
|
|
|
auto dispatch = [&](auto exec_preset, auto math_function) -> bool {
|
|
|
|
callback(exec_preset, math_function, *info);
|
2020-12-02 13:25:25 +01:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (operation) {
|
|
|
|
case NODE_MATH_EXPONENT:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_slow, [](float a) { return expf(a); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_SQRT:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float a) { return safe_sqrtf(a); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_INV_SQRT:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float a) { return safe_inverse_sqrtf(a); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_ABSOLUTE:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float a) { return fabs(a); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_RADIANS:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float a) { return (float)DEG2RAD(a); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_DEGREES:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float a) { return (float)RAD2DEG(a); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_SIGN:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float a) { return compatible_signf(a); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_ROUND:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float a) { return floorf(a + 0.5f); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_FLOOR:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float a) { return floorf(a); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_CEIL:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float a) { return ceilf(a); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_FRACTION:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float a) { return a - floorf(a); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_TRUNC:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float a) { return a >= 0.0f ? floorf(a) : ceilf(a); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_SINE:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_slow, [](float a) { return sinf(a); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_COSINE:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_slow, [](float a) { return cosf(a); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_TANGENT:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_slow, [](float a) { return tanf(a); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_SINH:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_slow, [](float a) { return sinhf(a); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_COSH:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_slow, [](float a) { return coshf(a); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_TANH:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_slow, [](float a) { return tanhf(a); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_ARCSINE:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_slow, [](float a) { return safe_asinf(a); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_ARCCOSINE:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_slow, [](float a) { return safe_acosf(a); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_ARCTANGENT:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_slow, [](float a) { return atanf(a); });
|
2020-12-02 13:25:25 +01:00
|
|
|
}
|
|
|
|
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_fl(const int operation, Callback &&callback)
|
|
|
|
{
|
|
|
|
const FloatMathOperationInfo *info = get_float_math_operation_info(operation);
|
|
|
|
if (info == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-26 17:12:34 +02:00
|
|
|
static auto exec_preset_fast = fn::CustomMF_presets::AllSpanOrSingle();
|
|
|
|
static auto exec_preset_slow = fn::CustomMF_presets::Materialized();
|
|
|
|
|
2020-12-02 13:25:25 +01:00
|
|
|
/* This is just an utility function to keep the individual cases smaller. */
|
2022-04-26 17:12:34 +02:00
|
|
|
auto dispatch = [&](auto exec_preset, auto math_function) -> bool {
|
|
|
|
callback(exec_preset, math_function, *info);
|
2020-12-02 13:25:25 +01:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (operation) {
|
|
|
|
case NODE_MATH_ADD:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float a, float b) { return a + b; });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_SUBTRACT:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float a, float b) { return a - b; });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_MULTIPLY:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float a, float b) { return a * b; });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_DIVIDE:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float a, float b) { return safe_divide(a, b); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_POWER:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_slow, [](float a, float b) { return safe_powf(a, b); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_LOGARITHM:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_slow, [](float a, float b) { return safe_logf(a, b); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_MINIMUM:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float a, float b) { return std::min(a, b); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_MAXIMUM:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float a, float b) { return std::max(a, b); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_LESS_THAN:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float a, float b) { return (float)(a < b); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_GREATER_THAN:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float a, float b) { return (float)(a > b); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_MODULO:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float a, float b) { return safe_modf(a, b); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_SNAP:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast,
|
|
|
|
[](float a, float b) { return floorf(safe_divide(a, b)) * b; });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_ARCTAN2:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_slow, [](float a, float b) { return atan2f(a, b); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_PINGPONG:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float a, float b) { return pingpongf(a, b); });
|
2020-12-02 13:25:25 +01:00
|
|
|
}
|
|
|
|
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_fl_to_fl(const int operation, Callback &&callback)
|
|
|
|
{
|
|
|
|
const FloatMathOperationInfo *info = get_float_math_operation_info(operation);
|
|
|
|
if (info == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is just an utility function to keep the individual cases smaller. */
|
2022-04-26 17:12:34 +02:00
|
|
|
auto dispatch = [&](auto exec_preset, auto math_function) -> bool {
|
|
|
|
callback(exec_preset, math_function, *info);
|
2020-12-02 13:25:25 +01:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (operation) {
|
|
|
|
case NODE_MATH_MULTIPLY_ADD:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(fn::CustomMF_presets::AllSpanOrSingle(),
|
|
|
|
[](float a, float b, float c) { return a * b + c; });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_COMPARE:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(fn::CustomMF_presets::SomeSpanOrSingle<0, 1>(),
|
|
|
|
[](float a, float b, float c) -> float {
|
|
|
|
return ((a == b) || (fabsf(a - b) <= fmaxf(c, FLT_EPSILON))) ? 1.0f : 0.0f;
|
|
|
|
});
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_SMOOTH_MIN:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(fn::CustomMF_presets::SomeSpanOrSingle<0, 1>(),
|
|
|
|
[](float a, float b, float c) { return smoothminf(a, b, c); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_SMOOTH_MAX:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(fn::CustomMF_presets::SomeSpanOrSingle<0, 1>(),
|
|
|
|
[](float a, float b, float c) { return -smoothminf(-a, -b, c); });
|
2020-12-02 13:25:25 +01:00
|
|
|
case NODE_MATH_WRAP:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(fn::CustomMF_presets::SomeSpanOrSingle<0>(),
|
|
|
|
[](float a, float b, float c) { return wrapf(a, b, c); });
|
2020-12-17 12:22:47 -06:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-11 12:06:52 -06:00
|
|
|
/**
|
|
|
|
* 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_fl3_fl3_to_fl3(const NodeVectorMathOperation operation,
|
|
|
|
Callback &&callback)
|
|
|
|
{
|
BLI: Refactor vector types & functions to use templates
This patch implements the vector types (i.e:`float2`) by making heavy
usage of templating. All vector functions are now outside of the vector
classes (inside the `blender::math` namespace) and are not vector size
dependent for the most part.
In the ongoing effort to make shaders less GL centric, we are aiming
to share more code between GLSL and C++ to avoid code duplication.
####Motivations:
- We are aiming to share UBO and SSBO structures between GLSL and C++.
This means we will use many of the existing vector types and others
we currently don't have (uintX, intX). All these variations were
asking for many more code duplication.
- Deduplicate existing code which is duplicated for each vector size.
- We also want to share small functions. Which means that vector
functions should be static and not in the class namespace.
- Reduce friction to use these types in new projects due to their
incompleteness.
- The current state of the `BLI_(float|double|mpq)(2|3|4).hh` is a
bit of a let down. Most clases are incomplete, out of sync with each
others with different codestyles, and some functions that should be
static are not (i.e: `float3::reflect()`).
####Upsides:
- Still support `.x, .y, .z, .w` for readability.
- Compact, readable and easilly extendable.
- All of the vector functions are available for all the vectors types
and can be restricted to certain types. Also template specialization
let us define exception for special class (like mpq).
- With optimization ON, the compiler unroll the loops and performance
is the same.
####Downsides:
- Might impact debugability. Though I would arge that the bugs are
rarelly caused by the vector class itself (since the operations are
quite trivial) but by the type conversions.
- Might impact compile time. I did not saw a significant impact since
the usage is not really widespread.
- Functions needs to be rewritten to support arbitrary vector length.
For instance, one can't call `len_squared_v3v3` in
`math::length_squared()` and call it a day.
- Type cast does not work with the template version of the `math::`
vector functions. Meaning you need to manually cast `float *` and
`(float *)[3]` to `float3` for the function calls.
i.e: `math::distance_squared(float3(nearest.co), positions[i]);`
- Some parts might loose in readability:
`float3::dot(v1.normalized(), v2.normalized())`
becoming
`math::dot(math::normalize(v1), math::normalize(v2))`
But I propose, when appropriate, to use
`using namespace blender::math;` on function local or file scope to
increase readability.
`dot(normalize(v1), normalize(v2))`
####Consideration:
- Include back `.length()` method. It is quite handy and is more C++
oriented.
- I considered the GLM library as a candidate for replacement. It felt
like too much for what we need and would be difficult to extend / modify
to our needs.
- I used Macros to reduce code in operators declaration and potential
copy paste bugs. This could reduce debugability and could be reverted.
- This touches `delaunay_2d.cc` and the intersection code. I would like
to know @howardt opinion on the matter.
- The `noexcept` on the copy constructor of `mpq(2|3)` is being removed.
But according to @JacquesLucke it is not a real problem for now.
I would like to give a huge thanks to @JacquesLucke who helped during this
and pushed me to reduce the duplication further.
Reviewed By: brecht, sergey, JacquesLucke
Differential Revision: https://developer.blender.org/D13791
2022-01-12 12:57:07 +01:00
|
|
|
using namespace blender::math;
|
|
|
|
|
2021-01-11 12:06:52 -06:00
|
|
|
const FloatMathOperationInfo *info = get_float3_math_operation_info(operation);
|
|
|
|
if (info == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-26 17:12:34 +02:00
|
|
|
static auto exec_preset_fast = fn::CustomMF_presets::AllSpanOrSingle();
|
|
|
|
static auto exec_preset_slow = fn::CustomMF_presets::Materialized();
|
|
|
|
|
2021-01-11 12:06:52 -06:00
|
|
|
/* This is just a utility function to keep the individual cases smaller. */
|
2022-04-26 17:12:34 +02:00
|
|
|
auto dispatch = [&](auto exec_preset, auto math_function) -> bool {
|
|
|
|
callback(exec_preset, math_function, *info);
|
2021-01-11 12:06:52 -06:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (operation) {
|
|
|
|
case NODE_VECTOR_MATH_ADD:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float3 a, float3 b) { return a + b; });
|
2021-01-11 12:06:52 -06:00
|
|
|
case NODE_VECTOR_MATH_SUBTRACT:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float3 a, float3 b) { return a - b; });
|
2021-01-11 12:06:52 -06:00
|
|
|
case NODE_VECTOR_MATH_MULTIPLY:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float3 a, float3 b) { return a * b; });
|
2021-01-11 12:06:52 -06:00
|
|
|
case NODE_VECTOR_MATH_DIVIDE:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float3 a, float3 b) { return safe_divide(a, b); });
|
2021-01-11 12:06:52 -06:00
|
|
|
case NODE_VECTOR_MATH_CROSS_PRODUCT:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast,
|
|
|
|
[](float3 a, float3 b) { return cross_high_precision(a, b); });
|
2021-01-11 12:06:52 -06:00
|
|
|
case NODE_VECTOR_MATH_PROJECT:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float3 a, float3 b) { return project(a, b); });
|
2021-01-11 12:06:52 -06:00
|
|
|
case NODE_VECTOR_MATH_REFLECT:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast,
|
|
|
|
[](float3 a, float3 b) { return reflect(a, normalize(b)); });
|
2021-01-11 12:06:52 -06:00
|
|
|
case NODE_VECTOR_MATH_SNAP:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast,
|
|
|
|
[](float3 a, float3 b) { return floor(safe_divide(a, b)) * b; });
|
2021-01-11 12:06:52 -06:00
|
|
|
case NODE_VECTOR_MATH_MODULO:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_slow, [](float3 a, float3 b) { return mod(a, b); });
|
2021-01-11 12:06:52 -06:00
|
|
|
case NODE_VECTOR_MATH_MINIMUM:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float3 a, float3 b) { return min(a, b); });
|
2021-01-11 12:06:52 -06:00
|
|
|
case NODE_VECTOR_MATH_MAXIMUM:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float3 a, float3 b) { return max(a, b); });
|
2021-01-11 12:06:52 -06:00
|
|
|
default:
|
|
|
|
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_fl3_fl3_to_fl(const NodeVectorMathOperation operation,
|
|
|
|
Callback &&callback)
|
|
|
|
{
|
BLI: Refactor vector types & functions to use templates
This patch implements the vector types (i.e:`float2`) by making heavy
usage of templating. All vector functions are now outside of the vector
classes (inside the `blender::math` namespace) and are not vector size
dependent for the most part.
In the ongoing effort to make shaders less GL centric, we are aiming
to share more code between GLSL and C++ to avoid code duplication.
####Motivations:
- We are aiming to share UBO and SSBO structures between GLSL and C++.
This means we will use many of the existing vector types and others
we currently don't have (uintX, intX). All these variations were
asking for many more code duplication.
- Deduplicate existing code which is duplicated for each vector size.
- We also want to share small functions. Which means that vector
functions should be static and not in the class namespace.
- Reduce friction to use these types in new projects due to their
incompleteness.
- The current state of the `BLI_(float|double|mpq)(2|3|4).hh` is a
bit of a let down. Most clases are incomplete, out of sync with each
others with different codestyles, and some functions that should be
static are not (i.e: `float3::reflect()`).
####Upsides:
- Still support `.x, .y, .z, .w` for readability.
- Compact, readable and easilly extendable.
- All of the vector functions are available for all the vectors types
and can be restricted to certain types. Also template specialization
let us define exception for special class (like mpq).
- With optimization ON, the compiler unroll the loops and performance
is the same.
####Downsides:
- Might impact debugability. Though I would arge that the bugs are
rarelly caused by the vector class itself (since the operations are
quite trivial) but by the type conversions.
- Might impact compile time. I did not saw a significant impact since
the usage is not really widespread.
- Functions needs to be rewritten to support arbitrary vector length.
For instance, one can't call `len_squared_v3v3` in
`math::length_squared()` and call it a day.
- Type cast does not work with the template version of the `math::`
vector functions. Meaning you need to manually cast `float *` and
`(float *)[3]` to `float3` for the function calls.
i.e: `math::distance_squared(float3(nearest.co), positions[i]);`
- Some parts might loose in readability:
`float3::dot(v1.normalized(), v2.normalized())`
becoming
`math::dot(math::normalize(v1), math::normalize(v2))`
But I propose, when appropriate, to use
`using namespace blender::math;` on function local or file scope to
increase readability.
`dot(normalize(v1), normalize(v2))`
####Consideration:
- Include back `.length()` method. It is quite handy and is more C++
oriented.
- I considered the GLM library as a candidate for replacement. It felt
like too much for what we need and would be difficult to extend / modify
to our needs.
- I used Macros to reduce code in operators declaration and potential
copy paste bugs. This could reduce debugability and could be reverted.
- This touches `delaunay_2d.cc` and the intersection code. I would like
to know @howardt opinion on the matter.
- The `noexcept` on the copy constructor of `mpq(2|3)` is being removed.
But according to @JacquesLucke it is not a real problem for now.
I would like to give a huge thanks to @JacquesLucke who helped during this
and pushed me to reduce the duplication further.
Reviewed By: brecht, sergey, JacquesLucke
Differential Revision: https://developer.blender.org/D13791
2022-01-12 12:57:07 +01:00
|
|
|
using namespace blender::math;
|
|
|
|
|
2021-01-11 12:06:52 -06:00
|
|
|
const FloatMathOperationInfo *info = get_float3_math_operation_info(operation);
|
|
|
|
if (info == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-26 17:12:34 +02:00
|
|
|
static auto exec_preset_fast = fn::CustomMF_presets::AllSpanOrSingle();
|
|
|
|
|
2021-01-11 12:06:52 -06:00
|
|
|
/* This is just a utility function to keep the individual cases smaller. */
|
2022-04-26 17:12:34 +02:00
|
|
|
auto dispatch = [&](auto exec_preset, auto math_function) -> bool {
|
|
|
|
callback(exec_preset, math_function, *info);
|
2021-01-11 12:06:52 -06:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (operation) {
|
|
|
|
case NODE_VECTOR_MATH_DOT_PRODUCT:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float3 a, float3 b) { return dot(a, b); });
|
2021-01-11 12:06:52 -06:00
|
|
|
case NODE_VECTOR_MATH_DISTANCE:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float3 a, float3 b) { return distance(a, b); });
|
2021-01-11 12:06:52 -06:00
|
|
|
default:
|
|
|
|
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_fl3_fl3_fl3_to_fl3(const NodeVectorMathOperation operation,
|
|
|
|
Callback &&callback)
|
|
|
|
{
|
BLI: Refactor vector types & functions to use templates
This patch implements the vector types (i.e:`float2`) by making heavy
usage of templating. All vector functions are now outside of the vector
classes (inside the `blender::math` namespace) and are not vector size
dependent for the most part.
In the ongoing effort to make shaders less GL centric, we are aiming
to share more code between GLSL and C++ to avoid code duplication.
####Motivations:
- We are aiming to share UBO and SSBO structures between GLSL and C++.
This means we will use many of the existing vector types and others
we currently don't have (uintX, intX). All these variations were
asking for many more code duplication.
- Deduplicate existing code which is duplicated for each vector size.
- We also want to share small functions. Which means that vector
functions should be static and not in the class namespace.
- Reduce friction to use these types in new projects due to their
incompleteness.
- The current state of the `BLI_(float|double|mpq)(2|3|4).hh` is a
bit of a let down. Most clases are incomplete, out of sync with each
others with different codestyles, and some functions that should be
static are not (i.e: `float3::reflect()`).
####Upsides:
- Still support `.x, .y, .z, .w` for readability.
- Compact, readable and easilly extendable.
- All of the vector functions are available for all the vectors types
and can be restricted to certain types. Also template specialization
let us define exception for special class (like mpq).
- With optimization ON, the compiler unroll the loops and performance
is the same.
####Downsides:
- Might impact debugability. Though I would arge that the bugs are
rarelly caused by the vector class itself (since the operations are
quite trivial) but by the type conversions.
- Might impact compile time. I did not saw a significant impact since
the usage is not really widespread.
- Functions needs to be rewritten to support arbitrary vector length.
For instance, one can't call `len_squared_v3v3` in
`math::length_squared()` and call it a day.
- Type cast does not work with the template version of the `math::`
vector functions. Meaning you need to manually cast `float *` and
`(float *)[3]` to `float3` for the function calls.
i.e: `math::distance_squared(float3(nearest.co), positions[i]);`
- Some parts might loose in readability:
`float3::dot(v1.normalized(), v2.normalized())`
becoming
`math::dot(math::normalize(v1), math::normalize(v2))`
But I propose, when appropriate, to use
`using namespace blender::math;` on function local or file scope to
increase readability.
`dot(normalize(v1), normalize(v2))`
####Consideration:
- Include back `.length()` method. It is quite handy and is more C++
oriented.
- I considered the GLM library as a candidate for replacement. It felt
like too much for what we need and would be difficult to extend / modify
to our needs.
- I used Macros to reduce code in operators declaration and potential
copy paste bugs. This could reduce debugability and could be reverted.
- This touches `delaunay_2d.cc` and the intersection code. I would like
to know @howardt opinion on the matter.
- The `noexcept` on the copy constructor of `mpq(2|3)` is being removed.
But according to @JacquesLucke it is not a real problem for now.
I would like to give a huge thanks to @JacquesLucke who helped during this
and pushed me to reduce the duplication further.
Reviewed By: brecht, sergey, JacquesLucke
Differential Revision: https://developer.blender.org/D13791
2022-01-12 12:57:07 +01:00
|
|
|
using namespace blender::math;
|
|
|
|
|
2021-01-11 12:06:52 -06:00
|
|
|
const FloatMathOperationInfo *info = get_float3_math_operation_info(operation);
|
|
|
|
if (info == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-26 17:12:34 +02:00
|
|
|
static auto exec_preset_fast = fn::CustomMF_presets::AllSpanOrSingle();
|
|
|
|
static auto exec_preset_slow = fn::CustomMF_presets::Materialized();
|
|
|
|
|
2021-01-11 12:06:52 -06:00
|
|
|
/* This is just a utility function to keep the individual cases smaller. */
|
2022-04-26 17:12:34 +02:00
|
|
|
auto dispatch = [&](auto exec_preset, auto math_function) -> bool {
|
|
|
|
callback(exec_preset, math_function, *info);
|
2021-01-11 12:06:52 -06:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (operation) {
|
2021-06-04 16:53:50 +01:00
|
|
|
case NODE_VECTOR_MATH_MULTIPLY_ADD:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float3 a, float3 b, float3 c) { return a * b + c; });
|
2021-01-11 12:06:52 -06:00
|
|
|
case NODE_VECTOR_MATH_WRAP:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_slow, [](float3 a, float3 b, float3 c) {
|
2021-01-11 12:06:52 -06:00
|
|
|
return float3(wrapf(a.x, b.x, c.x), wrapf(a.y, b.y, c.y), wrapf(a.z, b.z, c.z));
|
|
|
|
});
|
2021-03-23 09:21:56 +00:00
|
|
|
case NODE_VECTOR_MATH_FACEFORWARD:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast,
|
|
|
|
[](float3 a, float3 b, float3 c) { return faceforward(a, b, c); });
|
2021-03-23 09:21:56 +00:00
|
|
|
default:
|
|
|
|
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_fl3_fl3_fl_to_fl3(const NodeVectorMathOperation operation,
|
|
|
|
Callback &&callback)
|
|
|
|
{
|
BLI: Refactor vector types & functions to use templates
This patch implements the vector types (i.e:`float2`) by making heavy
usage of templating. All vector functions are now outside of the vector
classes (inside the `blender::math` namespace) and are not vector size
dependent for the most part.
In the ongoing effort to make shaders less GL centric, we are aiming
to share more code between GLSL and C++ to avoid code duplication.
####Motivations:
- We are aiming to share UBO and SSBO structures between GLSL and C++.
This means we will use many of the existing vector types and others
we currently don't have (uintX, intX). All these variations were
asking for many more code duplication.
- Deduplicate existing code which is duplicated for each vector size.
- We also want to share small functions. Which means that vector
functions should be static and not in the class namespace.
- Reduce friction to use these types in new projects due to their
incompleteness.
- The current state of the `BLI_(float|double|mpq)(2|3|4).hh` is a
bit of a let down. Most clases are incomplete, out of sync with each
others with different codestyles, and some functions that should be
static are not (i.e: `float3::reflect()`).
####Upsides:
- Still support `.x, .y, .z, .w` for readability.
- Compact, readable and easilly extendable.
- All of the vector functions are available for all the vectors types
and can be restricted to certain types. Also template specialization
let us define exception for special class (like mpq).
- With optimization ON, the compiler unroll the loops and performance
is the same.
####Downsides:
- Might impact debugability. Though I would arge that the bugs are
rarelly caused by the vector class itself (since the operations are
quite trivial) but by the type conversions.
- Might impact compile time. I did not saw a significant impact since
the usage is not really widespread.
- Functions needs to be rewritten to support arbitrary vector length.
For instance, one can't call `len_squared_v3v3` in
`math::length_squared()` and call it a day.
- Type cast does not work with the template version of the `math::`
vector functions. Meaning you need to manually cast `float *` and
`(float *)[3]` to `float3` for the function calls.
i.e: `math::distance_squared(float3(nearest.co), positions[i]);`
- Some parts might loose in readability:
`float3::dot(v1.normalized(), v2.normalized())`
becoming
`math::dot(math::normalize(v1), math::normalize(v2))`
But I propose, when appropriate, to use
`using namespace blender::math;` on function local or file scope to
increase readability.
`dot(normalize(v1), normalize(v2))`
####Consideration:
- Include back `.length()` method. It is quite handy and is more C++
oriented.
- I considered the GLM library as a candidate for replacement. It felt
like too much for what we need and would be difficult to extend / modify
to our needs.
- I used Macros to reduce code in operators declaration and potential
copy paste bugs. This could reduce debugability and could be reverted.
- This touches `delaunay_2d.cc` and the intersection code. I would like
to know @howardt opinion on the matter.
- The `noexcept` on the copy constructor of `mpq(2|3)` is being removed.
But according to @JacquesLucke it is not a real problem for now.
I would like to give a huge thanks to @JacquesLucke who helped during this
and pushed me to reduce the duplication further.
Reviewed By: brecht, sergey, JacquesLucke
Differential Revision: https://developer.blender.org/D13791
2022-01-12 12:57:07 +01:00
|
|
|
using namespace blender::math;
|
|
|
|
|
2021-03-23 09:21:56 +00:00
|
|
|
const FloatMathOperationInfo *info = get_float3_math_operation_info(operation);
|
|
|
|
if (info == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-26 17:12:34 +02:00
|
|
|
static auto exec_preset_slow = fn::CustomMF_presets::Materialized();
|
|
|
|
|
2021-03-23 09:21:56 +00:00
|
|
|
/* This is just a utility function to keep the individual cases smaller. */
|
2022-04-26 17:12:34 +02:00
|
|
|
auto dispatch = [&](auto exec_preset, auto math_function) -> bool {
|
|
|
|
callback(exec_preset, math_function, *info);
|
2021-03-23 09:21:56 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (operation) {
|
|
|
|
case NODE_VECTOR_MATH_REFRACT:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_slow,
|
|
|
|
[](float3 a, float3 b, float c) { return refract(a, normalize(b), c); });
|
2021-01-11 12:06:52 -06:00
|
|
|
default:
|
|
|
|
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_fl3_to_fl(const NodeVectorMathOperation operation,
|
|
|
|
Callback &&callback)
|
|
|
|
{
|
BLI: Refactor vector types & functions to use templates
This patch implements the vector types (i.e:`float2`) by making heavy
usage of templating. All vector functions are now outside of the vector
classes (inside the `blender::math` namespace) and are not vector size
dependent for the most part.
In the ongoing effort to make shaders less GL centric, we are aiming
to share more code between GLSL and C++ to avoid code duplication.
####Motivations:
- We are aiming to share UBO and SSBO structures between GLSL and C++.
This means we will use many of the existing vector types and others
we currently don't have (uintX, intX). All these variations were
asking for many more code duplication.
- Deduplicate existing code which is duplicated for each vector size.
- We also want to share small functions. Which means that vector
functions should be static and not in the class namespace.
- Reduce friction to use these types in new projects due to their
incompleteness.
- The current state of the `BLI_(float|double|mpq)(2|3|4).hh` is a
bit of a let down. Most clases are incomplete, out of sync with each
others with different codestyles, and some functions that should be
static are not (i.e: `float3::reflect()`).
####Upsides:
- Still support `.x, .y, .z, .w` for readability.
- Compact, readable and easilly extendable.
- All of the vector functions are available for all the vectors types
and can be restricted to certain types. Also template specialization
let us define exception for special class (like mpq).
- With optimization ON, the compiler unroll the loops and performance
is the same.
####Downsides:
- Might impact debugability. Though I would arge that the bugs are
rarelly caused by the vector class itself (since the operations are
quite trivial) but by the type conversions.
- Might impact compile time. I did not saw a significant impact since
the usage is not really widespread.
- Functions needs to be rewritten to support arbitrary vector length.
For instance, one can't call `len_squared_v3v3` in
`math::length_squared()` and call it a day.
- Type cast does not work with the template version of the `math::`
vector functions. Meaning you need to manually cast `float *` and
`(float *)[3]` to `float3` for the function calls.
i.e: `math::distance_squared(float3(nearest.co), positions[i]);`
- Some parts might loose in readability:
`float3::dot(v1.normalized(), v2.normalized())`
becoming
`math::dot(math::normalize(v1), math::normalize(v2))`
But I propose, when appropriate, to use
`using namespace blender::math;` on function local or file scope to
increase readability.
`dot(normalize(v1), normalize(v2))`
####Consideration:
- Include back `.length()` method. It is quite handy and is more C++
oriented.
- I considered the GLM library as a candidate for replacement. It felt
like too much for what we need and would be difficult to extend / modify
to our needs.
- I used Macros to reduce code in operators declaration and potential
copy paste bugs. This could reduce debugability and could be reverted.
- This touches `delaunay_2d.cc` and the intersection code. I would like
to know @howardt opinion on the matter.
- The `noexcept` on the copy constructor of `mpq(2|3)` is being removed.
But according to @JacquesLucke it is not a real problem for now.
I would like to give a huge thanks to @JacquesLucke who helped during this
and pushed me to reduce the duplication further.
Reviewed By: brecht, sergey, JacquesLucke
Differential Revision: https://developer.blender.org/D13791
2022-01-12 12:57:07 +01:00
|
|
|
using namespace blender::math;
|
|
|
|
|
2021-01-11 12:06:52 -06:00
|
|
|
const FloatMathOperationInfo *info = get_float3_math_operation_info(operation);
|
|
|
|
if (info == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-26 17:12:34 +02:00
|
|
|
static auto exec_preset_fast = fn::CustomMF_presets::AllSpanOrSingle();
|
|
|
|
|
2021-01-11 12:06:52 -06:00
|
|
|
/* This is just a utility function to keep the individual cases smaller. */
|
2022-04-26 17:12:34 +02:00
|
|
|
auto dispatch = [&](auto exec_preset, auto math_function) -> bool {
|
|
|
|
callback(exec_preset, math_function, *info);
|
2021-01-11 12:06:52 -06:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (operation) {
|
|
|
|
case NODE_VECTOR_MATH_LENGTH:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float3 in) { return length(in); });
|
2021-01-11 12:06:52 -06:00
|
|
|
default:
|
|
|
|
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_fl3_fl_to_fl3(const NodeVectorMathOperation operation,
|
|
|
|
Callback &&callback)
|
|
|
|
{
|
|
|
|
const FloatMathOperationInfo *info = get_float3_math_operation_info(operation);
|
|
|
|
if (info == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-26 17:12:34 +02:00
|
|
|
static auto exec_preset_fast = fn::CustomMF_presets::AllSpanOrSingle();
|
|
|
|
|
2021-01-11 12:06:52 -06:00
|
|
|
/* This is just a utility function to keep the individual cases smaller. */
|
2022-04-26 17:12:34 +02:00
|
|
|
auto dispatch = [&](auto exec_preset, auto math_function) -> bool {
|
|
|
|
callback(exec_preset, math_function, *info);
|
2021-01-11 12:06:52 -06:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (operation) {
|
|
|
|
case NODE_VECTOR_MATH_SCALE:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float3 a, float b) { return a * b; });
|
2021-01-11 12:06:52 -06:00
|
|
|
default:
|
|
|
|
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_fl3_to_fl3(const NodeVectorMathOperation operation,
|
|
|
|
Callback &&callback)
|
|
|
|
{
|
BLI: Refactor vector types & functions to use templates
This patch implements the vector types (i.e:`float2`) by making heavy
usage of templating. All vector functions are now outside of the vector
classes (inside the `blender::math` namespace) and are not vector size
dependent for the most part.
In the ongoing effort to make shaders less GL centric, we are aiming
to share more code between GLSL and C++ to avoid code duplication.
####Motivations:
- We are aiming to share UBO and SSBO structures between GLSL and C++.
This means we will use many of the existing vector types and others
we currently don't have (uintX, intX). All these variations were
asking for many more code duplication.
- Deduplicate existing code which is duplicated for each vector size.
- We also want to share small functions. Which means that vector
functions should be static and not in the class namespace.
- Reduce friction to use these types in new projects due to their
incompleteness.
- The current state of the `BLI_(float|double|mpq)(2|3|4).hh` is a
bit of a let down. Most clases are incomplete, out of sync with each
others with different codestyles, and some functions that should be
static are not (i.e: `float3::reflect()`).
####Upsides:
- Still support `.x, .y, .z, .w` for readability.
- Compact, readable and easilly extendable.
- All of the vector functions are available for all the vectors types
and can be restricted to certain types. Also template specialization
let us define exception for special class (like mpq).
- With optimization ON, the compiler unroll the loops and performance
is the same.
####Downsides:
- Might impact debugability. Though I would arge that the bugs are
rarelly caused by the vector class itself (since the operations are
quite trivial) but by the type conversions.
- Might impact compile time. I did not saw a significant impact since
the usage is not really widespread.
- Functions needs to be rewritten to support arbitrary vector length.
For instance, one can't call `len_squared_v3v3` in
`math::length_squared()` and call it a day.
- Type cast does not work with the template version of the `math::`
vector functions. Meaning you need to manually cast `float *` and
`(float *)[3]` to `float3` for the function calls.
i.e: `math::distance_squared(float3(nearest.co), positions[i]);`
- Some parts might loose in readability:
`float3::dot(v1.normalized(), v2.normalized())`
becoming
`math::dot(math::normalize(v1), math::normalize(v2))`
But I propose, when appropriate, to use
`using namespace blender::math;` on function local or file scope to
increase readability.
`dot(normalize(v1), normalize(v2))`
####Consideration:
- Include back `.length()` method. It is quite handy and is more C++
oriented.
- I considered the GLM library as a candidate for replacement. It felt
like too much for what we need and would be difficult to extend / modify
to our needs.
- I used Macros to reduce code in operators declaration and potential
copy paste bugs. This could reduce debugability and could be reverted.
- This touches `delaunay_2d.cc` and the intersection code. I would like
to know @howardt opinion on the matter.
- The `noexcept` on the copy constructor of `mpq(2|3)` is being removed.
But according to @JacquesLucke it is not a real problem for now.
I would like to give a huge thanks to @JacquesLucke who helped during this
and pushed me to reduce the duplication further.
Reviewed By: brecht, sergey, JacquesLucke
Differential Revision: https://developer.blender.org/D13791
2022-01-12 12:57:07 +01:00
|
|
|
using namespace blender::math;
|
|
|
|
|
2021-01-11 12:06:52 -06:00
|
|
|
const FloatMathOperationInfo *info = get_float3_math_operation_info(operation);
|
|
|
|
if (info == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-26 17:12:34 +02:00
|
|
|
static auto exec_preset_fast = fn::CustomMF_presets::AllSpanOrSingle();
|
|
|
|
static auto exec_preset_slow = fn::CustomMF_presets::Materialized();
|
|
|
|
|
2021-01-11 12:06:52 -06:00
|
|
|
/* This is just a utility function to keep the individual cases smaller. */
|
2022-04-26 17:12:34 +02:00
|
|
|
auto dispatch = [&](auto exec_preset, auto math_function) -> bool {
|
|
|
|
callback(exec_preset, math_function, *info);
|
2021-01-11 12:06:52 -06:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (operation) {
|
|
|
|
case NODE_VECTOR_MATH_NORMALIZE:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast,
|
|
|
|
[](float3 in) { return normalize(in); }); /* Should be safe. */
|
2021-01-11 12:06:52 -06:00
|
|
|
case NODE_VECTOR_MATH_FLOOR:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float3 in) { return floor(in); });
|
2021-01-11 12:06:52 -06:00
|
|
|
case NODE_VECTOR_MATH_CEIL:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float3 in) { return ceil(in); });
|
2021-01-11 12:06:52 -06:00
|
|
|
case NODE_VECTOR_MATH_FRACTION:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float3 in) { return fract(in); });
|
2021-01-11 12:06:52 -06:00
|
|
|
case NODE_VECTOR_MATH_ABSOLUTE:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_fast, [](float3 in) { return abs(in); });
|
2021-01-11 12:06:52 -06:00
|
|
|
case NODE_VECTOR_MATH_SINE:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_slow,
|
|
|
|
[](float3 in) { return float3(sinf(in.x), sinf(in.y), sinf(in.z)); });
|
2021-01-11 12:06:52 -06:00
|
|
|
case NODE_VECTOR_MATH_COSINE:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_slow,
|
|
|
|
[](float3 in) { return float3(cosf(in.x), cosf(in.y), cosf(in.z)); });
|
2021-01-11 12:06:52 -06:00
|
|
|
case NODE_VECTOR_MATH_TANGENT:
|
2022-04-26 17:12:34 +02:00
|
|
|
return dispatch(exec_preset_slow,
|
|
|
|
[](float3 in) { return float3(tanf(in.x), tanf(in.y), tanf(in.z)); });
|
2021-01-11 12:06:52 -06:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-02 13:25:25 +01:00
|
|
|
} // namespace blender::nodes
|