25 ".. function:: clamp(value, min=0, max=1)\n"
27 " Clamps the float value between minimum and maximum. To avoid\n"
28 " confusion, any call must use either one or all three arguments.\n"
30 " :arg value: The value to clamp.\n"
31 " :type value: float\n"
32 " :arg min: The minimum value, defaults to 0.\n"
34 " :arg max: The maximum value, defaults to 1.\n"
36 " :return: The clamped value.\n"
40 double x, minv = 0.0, maxv = 1.0;
42 if (PyTuple_Size(args) <= 1) {
43 if (!PyArg_ParseTuple(args,
"d:clamp", &
x)) {
48 if (!PyArg_ParseTuple(args,
"ddd:clamp", &
x, &minv, &maxv)) {
55 return PyFloat_FromDouble(
x);
61 ".. function:: lerp(from_value, to_value, factor)\n"
63 " Linearly interpolate between two float values based on factor.\n"
65 " :arg from_value: The value to return when factor is 0.\n"
66 " :type from_value: float\n"
67 " :arg to_value: The value to return when factor is 1.\n"
68 " :type to_value: float\n"
69 " :arg factor: The interpolation value, normally in [0.0, 1.0].\n"
70 " :type factor: float\n"
71 " :return: The interpolated value.\n"
76 if (!PyArg_ParseTuple(args,
"ddd:lerp", &a, &
b, &
x)) {
80 return PyFloat_FromDouble(a * (1.0 -
x) +
b *
x);
85 py_bl_math_smoothstep_doc,
86 ".. function:: smoothstep(from_value, to_value, value)\n"
88 " Performs smooth interpolation between 0 and 1 as value changes between from and "
90 " Outside the range the function returns the same value as the nearest edge.\n"
92 " :arg from_value: The edge value where the result is 0.\n"
93 " :type from_value: float\n"
94 " :arg to_value: The edge value where the result is 1.\n"
95 " :type to_value: float\n"
96 " :arg factor: The interpolation value.\n"
97 " :type factor: float\n"
98 " :return: The interpolated value in [0.0, 1.0].\n"
103 if (!PyArg_ParseTuple(args,
"ddd:smoothstep", &a, &
b, &
x)) {
107 double t = (
x - a) / (
b - a);
111 return PyFloat_FromDouble(t * t * (3.0 - 2.0 * t));
121 {
"clamp", (PyCFunction)
py_bl_math_clamp, METH_VARARGS, py_bl_math_clamp_doc},
122 {
"lerp", (PyCFunction)
py_bl_math_lerp, METH_VARARGS, py_bl_math_lerp_doc},
124 {
nullptr,
nullptr, 0,
nullptr},
130 "Miscellaneous math utilities module.");
static PyObject * py_bl_math_smoothstep(PyObject *, PyObject *args)
PyMODINIT_FUNC BPyInit_bl_math()
static PyMethodDef M_bl_math_methods[]
PyDoc_STRVAR(py_bl_math_clamp_doc, ".. function:: clamp(value, min=0, max=1)\n" "\n" " Clamps the float value between minimum and maximum. To avoid\n" " confusion, any call must use either one or all three arguments.\n" "\n" " :arg value: The value to clamp.\n" " :type value: float\n" " :arg min: The minimum value, defaults to 0.\n" " :type min: float\n" " :arg max: The maximum value, defaults to 1.\n" " :type max: float\n" " :return: The clamped value.\n" " :rtype: float\n")
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