25 "Miscellaneous math utilities module");
36 ".. function:: clamp(value, min=0, max=1)\n"
38 " Clamps the float value between minimum and maximum. To avoid\n"
39 " confusion, any call must use either one or all three arguments.\n"
41 " :arg value: The value to clamp.\n"
42 " :type value: float\n"
43 " :arg min: The minimum value, defaults to 0.\n"
45 " :arg max: The maximum value, defaults to 1.\n"
47 " :return: The clamped value.\n"
51 double x, minv = 0.0, maxv = 1.0;
53 if (PyTuple_Size(args) <= 1) {
54 if (!PyArg_ParseTuple(args,
"d:clamp", &x)) {
59 if (!PyArg_ParseTuple(args,
"ddd:clamp", &x, &minv, &maxv)) {
66 return PyFloat_FromDouble(x);
72 ".. function:: lerp(from_value, to_value, factor)\n"
74 " Linearly interpolate between two float values based on factor.\n"
76 " :arg from_value: The value to return when factor is 0.\n"
77 " :type from_value: float\n"
78 " :arg to_value: The value to return when factor is 1.\n"
79 " :type to_value: float\n"
80 " :arg factor: The interpolation value, normally in [0.0, 1.0].\n"
81 " :type factor: float\n"
82 " :return: The interpolated value.\n"
87 if (!PyArg_ParseTuple(args,
"ddd:lerp", &a, &
b, &x)) {
91 return PyFloat_FromDouble(a * (1.0 - x) +
b * x);
96 py_bl_math_smoothstep_doc,
97 ".. function:: smoothstep(from_value, to_value, value)\n"
99 " Performs smooth interpolation between 0 and 1 as value changes between from and "
101 " Outside the range the function returns the same value as the nearest edge.\n"
103 " :arg from_value: The edge value where the result is 0.\n"
104 " :type from_value: float\n"
105 " :arg to_value: The edge value where the result is 1.\n"
106 " :type to_value: float\n"
107 " :arg factor: The interpolation value.\n"
108 " :type factor: float\n"
109 " :return: The interpolated value in [0.0, 1.0].\n"
114 if (!PyArg_ParseTuple(args,
"ddd:smoothstep", &a, &
b, &x)) {
118 double t = (x - a) / (
b - a);
122 return PyFloat_FromDouble(t * t * (3.0 - 2.0 * t));
132 {
"clamp", (PyCFunction)
py_bl_math_clamp, METH_VARARGS, py_bl_math_clamp_doc},
133 {
"lerp", (PyCFunction)
py_bl_math_lerp, METH_VARARGS, py_bl_math_lerp_doc},
135 {
nullptr,
nullptr, 0,
nullptr},
139 PyModuleDef_HEAD_INIT,
static PyObject * py_bl_math_smoothstep(PyObject *, PyObject *args)
PyMODINIT_FUNC BPyInit_bl_math()
PyDoc_STRVAR(M_bl_math_doc, "Miscellaneous math utilities module")
static PyMethodDef M_bl_math_methods[]
static PyObject * py_bl_math_clamp(PyObject *, PyObject *args)
static PyObject * py_bl_math_lerp(PyObject *, PyObject *args)
static PyModuleDef M_bl_math_module_def
local_group_size(16, 16) .push_constant(Type b