Blender V5.3
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
10
11#include <Python.h>
12
13#include "BLI_utildefines.hh"
14
15#include "bl_math_py_api.hh"
16
17namespace blender {
18
19/* -------------------------------------------------------------------- */
22
24 /* Wrap. */
25 py_bl_math_clamp_doc,
26 ".. function:: clamp(value, min=0, max=1)\n"
27 "\n"
28 " Clamps the float value between minimum and maximum. To avoid\n"
29 " confusion, any call must use either one or all three arguments.\n"
30 "\n"
31 " :param value: The value to clamp.\n"
32 " :type value: float\n"
33 " :param min: The minimum value, defaults to 0.\n"
34 " :type min: float\n"
35 " :param max: The maximum value, defaults to 1.\n"
36 " :type max: float\n"
37 " :return: The clamped value.\n"
38 " :rtype: float\n");
39static PyObject *py_bl_math_clamp(PyObject * /*self*/, PyObject *args)
40{
41 double x, minv = 0.0, maxv = 1.0;
42
43 if (PyTuple_Size(args) <= 1) {
44 if (!PyArg_ParseTuple(args,
45 "d" /* `value` */
46 ":clamp",
47 &x))
48 {
49 return nullptr;
50 }
51 }
52 else {
53 if (!PyArg_ParseTuple(args,
54 "d" /* `value` */
55 "d" /* `min` */
56 "d" /* `max` */
57 ":clamp",
58 &x,
59 &minv,
60 &maxv))
61 {
62 return nullptr;
63 }
64 }
65
66 CLAMP(x, minv, maxv);
67
68 return PyFloat_FromDouble(x);
69}
70
72 /* Wrap. */
73 py_bl_math_lerp_doc,
74 ".. function:: lerp(from_value, to_value, factor)\n"
75 "\n"
76 " Linearly interpolate between two float values based on factor.\n"
77 "\n"
78 " :param from_value: The value to return when factor is 0.\n"
79 " :type from_value: float\n"
80 " :param to_value: The value to return when factor is 1.\n"
81 " :type to_value: float\n"
82 " :param factor: The interpolation value, normally in [0.0, 1.0].\n"
83 " :type factor: float\n"
84 " :return: The interpolated value.\n"
85 " :rtype: float\n");
86static PyObject *py_bl_math_lerp(PyObject * /*self*/, PyObject *args)
87{
88 double a, b, x;
89 if (!PyArg_ParseTuple(args,
90 "d" /* `from_value` */
91 "d" /* `to_value` */
92 "d" /* `factor` */
93 ":lerp",
94 &a,
95 &b,
96 &x))
97 {
98 return nullptr;
99 }
100
101 return PyFloat_FromDouble(a * (1.0 - x) + b * x);
102}
103
105 /* Wrap. */
106 py_bl_math_smoothstep_doc,
107 ".. function:: smoothstep(from_value, to_value, value)\n"
108 "\n"
109 " Performs smooth interpolation between 0 and 1 as value changes between from and "
110 "to values.\n"
111 " Outside the range the function returns the same value as the nearest edge.\n"
112 "\n"
113 " :param from_value: The edge value where the result is 0.\n"
114 " :type from_value: float\n"
115 " :param to_value: The edge value where the result is 1.\n"
116 " :type to_value: float\n"
117 " :param value: The interpolation value.\n"
118 " :type value: float\n"
119 " :return: The interpolated value in [0.0, 1.0].\n"
120 " :rtype: float\n");
121static PyObject *py_bl_math_smoothstep(PyObject * /*self*/, PyObject *args)
122{
123 double a, b, x;
124 if (!PyArg_ParseTuple(args,
125 "d" /* `from_value` */
126 "d" /* `to_value` */
127 "d" /* `value` */
128 ":smoothstep",
129 &a,
130 &b,
131 &x))
132 {
133 return nullptr;
134 }
135
136 double t = (x - a) / (b - a);
137
138 CLAMP(t, 0.0, 1.0);
139
140 return PyFloat_FromDouble(t * t * (3.0 - 2.0 * t));
141}
142
144
145/* -------------------------------------------------------------------- */
148
149static PyMethodDef M_bl_math_methods[] = {
150 {"clamp", static_cast<PyCFunction>(py_bl_math_clamp), METH_VARARGS, py_bl_math_clamp_doc},
151 {"lerp", static_cast<PyCFunction>(py_bl_math_lerp), METH_VARARGS, py_bl_math_lerp_doc},
152 {"smoothstep",
153 static_cast<PyCFunction>(py_bl_math_smoothstep),
154 METH_VARARGS,
155 py_bl_math_smoothstep_doc},
156 {nullptr, nullptr, 0, nullptr},
157};
158
160 /* Wrap. */
161 M_bl_math_doc,
162 "Miscellaneous math utilities module.");
163static PyModuleDef M_bl_math_module_def = {
164 /*m_base*/ PyModuleDef_HEAD_INIT,
165 /*m_name*/ "bl_math",
166 /*m_doc*/ M_bl_math_doc,
167 /*m_size*/ 0,
168 /*m_methods*/ M_bl_math_methods,
169 /*m_slots*/ nullptr,
170 /*m_traverse*/ nullptr,
171 /*m_clear*/ nullptr,
172 /*m_free*/ nullptr,
173};
174
175PyMODINIT_FUNC BPyInit_bl_math()
176{
177 PyObject *submodule = PyModule_Create(&M_bl_math_module_def);
178 return submodule;
179}
180
182
183} // namespace blender
#define CLAMP(a, b, c)
BMesh Iterators.
static PyModuleDef M_bl_math_module_def
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
static PyObject * py_bl_math_smoothstep(PyObject *, PyObject *args)
PyMODINIT_FUNC BPyInit_bl_math()
static PyObject * py_bl_math_clamp(PyObject *, PyObject *args)
static PyMethodDef M_bl_math_methods[]
static PyObject * py_bl_math_lerp(PyObject *, PyObject *args)