Blender V5.0
bl_math_py_api.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include <Python.h>
13
14#include "BLI_utildefines.h"
15
16#include "bl_math_py_api.hh"
17
18/* -------------------------------------------------------------------- */
21
23 /* Wrap. */
24 py_bl_math_clamp_doc,
25 ".. function:: clamp(value, min=0, max=1)\n"
26 "\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"
29 "\n"
30 " :arg value: The value to clamp.\n"
31 " :type value: float\n"
32 " :arg min: The minimum value, defaults to 0.\n"
33 " :type min: float\n"
34 " :arg max: The maximum value, defaults to 1.\n"
35 " :type max: float\n"
36 " :return: The clamped value.\n"
37 " :rtype: float\n");
38static PyObject *py_bl_math_clamp(PyObject * /*self*/, PyObject *args)
39{
40 double x, minv = 0.0, maxv = 1.0;
41
42 if (PyTuple_Size(args) <= 1) {
43 if (!PyArg_ParseTuple(args, "d:clamp", &x)) {
44 return nullptr;
45 }
46 }
47 else {
48 if (!PyArg_ParseTuple(args, "ddd:clamp", &x, &minv, &maxv)) {
49 return nullptr;
50 }
51 }
52
53 CLAMP(x, minv, maxv);
54
55 return PyFloat_FromDouble(x);
56}
57
59 /* Wrap. */
60 py_bl_math_lerp_doc,
61 ".. function:: lerp(from_value, to_value, factor)\n"
62 "\n"
63 " Linearly interpolate between two float values based on factor.\n"
64 "\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"
72 " :rtype: float\n");
73static PyObject *py_bl_math_lerp(PyObject * /*self*/, PyObject *args)
74{
75 double a, b, x;
76 if (!PyArg_ParseTuple(args, "ddd:lerp", &a, &b, &x)) {
77 return nullptr;
78 }
79
80 return PyFloat_FromDouble(a * (1.0 - x) + b * x);
81}
82
84 /* Wrap. */
85 py_bl_math_smoothstep_doc,
86 ".. function:: smoothstep(from_value, to_value, value)\n"
87 "\n"
88 " Performs smooth interpolation between 0 and 1 as value changes between from and "
89 "to values.\n"
90 " Outside the range the function returns the same value as the nearest edge.\n"
91 "\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"
99 " :rtype: float\n");
100static PyObject *py_bl_math_smoothstep(PyObject * /*self*/, PyObject *args)
101{
102 double a, b, x;
103 if (!PyArg_ParseTuple(args, "ddd:smoothstep", &a, &b, &x)) {
104 return nullptr;
105 }
106
107 double t = (x - a) / (b - a);
108
109 CLAMP(t, 0.0, 1.0);
110
111 return PyFloat_FromDouble(t * t * (3.0 - 2.0 * t));
112}
113
115
116/* -------------------------------------------------------------------- */
119
120static PyMethodDef M_bl_math_methods[] = {
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},
123 {"smoothstep", (PyCFunction)py_bl_math_smoothstep, METH_VARARGS, py_bl_math_smoothstep_doc},
124 {nullptr, nullptr, 0, nullptr},
125};
126
128 /* Wrap. */
129 M_bl_math_doc,
130 "Miscellaneous math utilities module.");
131static PyModuleDef M_bl_math_module_def = {
132 /*m_base*/ PyModuleDef_HEAD_INIT,
133 /*m_name*/ "bl_math",
134 /*m_doc*/ M_bl_math_doc,
135 /*m_size*/ 0,
136 /*m_methods*/ M_bl_math_methods,
137 /*m_slots*/ nullptr,
138 /*m_traverse*/ nullptr,
139 /*m_clear*/ nullptr,
140 /*m_free*/ nullptr,
141};
142
143PyMODINIT_FUNC BPyInit_bl_math()
144{
145 PyObject *submodule = PyModule_Create(&M_bl_math_module_def);
146 return submodule;
147}
148
#define CLAMP(a, b, c)
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