Blender V4.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
12#include <Python.h>
13
14#include "BLI_utildefines.h"
15
16#include "bl_math_py_api.hh"
17
18/* -------------------------------------------------------------------- */
23 /* Wrap. */
24 M_bl_math_doc,
25 "Miscellaneous math utilities module");
26
29/* -------------------------------------------------------------------- */
34 /* Wrap. */
35 py_bl_math_clamp_doc,
36 ".. function:: clamp(value, min=0, max=1)\n"
37 "\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"
40 "\n"
41 " :arg value: The value to clamp.\n"
42 " :type value: float\n"
43 " :arg min: The minimum value, defaults to 0.\n"
44 " :type min: float\n"
45 " :arg max: The maximum value, defaults to 1.\n"
46 " :type max: float\n"
47 " :return: The clamped value.\n"
48 " :rtype: float\n");
49static PyObject *py_bl_math_clamp(PyObject * /*self*/, PyObject *args)
50{
51 double x, minv = 0.0, maxv = 1.0;
52
53 if (PyTuple_Size(args) <= 1) {
54 if (!PyArg_ParseTuple(args, "d:clamp", &x)) {
55 return nullptr;
56 }
57 }
58 else {
59 if (!PyArg_ParseTuple(args, "ddd:clamp", &x, &minv, &maxv)) {
60 return nullptr;
61 }
62 }
63
64 CLAMP(x, minv, maxv);
65
66 return PyFloat_FromDouble(x);
67}
68
70 /* Wrap. */
71 py_bl_math_lerp_doc,
72 ".. function:: lerp(from_value, to_value, factor)\n"
73 "\n"
74 " Linearly interpolate between two float values based on factor.\n"
75 "\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"
83 " :rtype: float\n");
84static PyObject *py_bl_math_lerp(PyObject * /*self*/, PyObject *args)
85{
86 double a, b, x;
87 if (!PyArg_ParseTuple(args, "ddd:lerp", &a, &b, &x)) {
88 return nullptr;
89 }
90
91 return PyFloat_FromDouble(a * (1.0 - x) + b * x);
92}
93
95 /* Wrap. */
96 py_bl_math_smoothstep_doc,
97 ".. function:: smoothstep(from_value, to_value, value)\n"
98 "\n"
99 " Performs smooth interpolation between 0 and 1 as value changes between from and "
100 "to values.\n"
101 " Outside the range the function returns the same value as the nearest edge.\n"
102 "\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"
110 " :rtype: float\n");
111static PyObject *py_bl_math_smoothstep(PyObject * /*self*/, PyObject *args)
112{
113 double a, b, x;
114 if (!PyArg_ParseTuple(args, "ddd:smoothstep", &a, &b, &x)) {
115 return nullptr;
116 }
117
118 double t = (x - a) / (b - a);
119
120 CLAMP(t, 0.0, 1.0);
121
122 return PyFloat_FromDouble(t * t * (3.0 - 2.0 * t));
123}
124
127/* -------------------------------------------------------------------- */
131static PyMethodDef M_bl_math_methods[] = {
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},
134 {"smoothstep", (PyCFunction)py_bl_math_smoothstep, METH_VARARGS, py_bl_math_smoothstep_doc},
135 {nullptr, nullptr, 0, nullptr},
136};
137
138static PyModuleDef M_bl_math_module_def = {
139 /*m_base*/ PyModuleDef_HEAD_INIT,
140 /*m_name*/ "bl_math",
141 /*m_doc*/ M_bl_math_doc,
142 /*m_size*/ 0,
143 /*m_methods*/ M_bl_math_methods,
144 /*m_slots*/ nullptr,
145 /*m_traverse*/ nullptr,
146 /*m_clear*/ nullptr,
147 /*m_free*/ nullptr,
148};
149
150PyMODINIT_FUNC BPyInit_bl_math()
151{
152 PyObject *submodule = PyModule_Create(&M_bl_math_module_def);
153 return submodule;
154}
155
#define CLAMP(a, b, c)
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