Blender  V3.3
bl_math_py_api.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
10 #include <Python.h>
11 
12 #include "BLI_math.h"
13 #include "BLI_utildefines.h"
14 
15 #include "py_capi_utils.h"
16 
17 #include "bl_math_py_api.h"
18 
19 /* -------------------------------------------------------------------- */
23 PyDoc_STRVAR(M_bl_math_doc, "Miscellaneous math utilities module");
24 
27 /* -------------------------------------------------------------------- */
31 PyDoc_STRVAR(py_bl_math_clamp_doc,
32  ".. function:: clamp(value, min=0, max=1)\n"
33  "\n"
34  " Clamps the float value between minimum and maximum. To avoid\n"
35  " confusion, any call must use either one or all three arguments.\n"
36  "\n"
37  " :arg value: The value to clamp.\n"
38  " :type value: float\n"
39  " :arg min: The minimum value, defaults to 0.\n"
40  " :type min: float\n"
41  " :arg max: The maximum value, defaults to 1.\n"
42  " :type max: float\n"
43  " :return: The clamped value.\n"
44  " :rtype: float\n");
45 static PyObject *py_bl_math_clamp(PyObject *UNUSED(self), PyObject *args)
46 {
47  double x, minv = 0.0, maxv = 1.0;
48 
49  if (PyTuple_Size(args) <= 1) {
50  if (!PyArg_ParseTuple(args, "d:clamp", &x)) {
51  return NULL;
52  }
53  }
54  else {
55  if (!PyArg_ParseTuple(args, "ddd:clamp", &x, &minv, &maxv)) {
56  return NULL;
57  }
58  }
59 
60  CLAMP(x, minv, maxv);
61 
62  return PyFloat_FromDouble(x);
63 }
64 
65 PyDoc_STRVAR(py_bl_math_lerp_doc,
66  ".. function:: lerp(from_value, to_value, factor)\n"
67  "\n"
68  " Linearly interpolate between two float values based on factor.\n"
69  "\n"
70  " :arg from_value: The value to return when factor is 0.\n"
71  " :type from_value: float\n"
72  " :arg to_value: The value to return when factor is 1.\n"
73  " :type to_value: float\n"
74  " :arg factor: The interpolation value, normally in [0.0, 1.0].\n"
75  " :type factor: float\n"
76  " :return: The interpolated value.\n"
77  " :rtype: float\n");
78 static PyObject *py_bl_math_lerp(PyObject *UNUSED(self), PyObject *args)
79 {
80  double a, b, x;
81  if (!PyArg_ParseTuple(args, "ddd:lerp", &a, &b, &x)) {
82  return NULL;
83  }
84 
85  return PyFloat_FromDouble(a * (1.0 - x) + b * x);
86 }
87 
88 PyDoc_STRVAR(py_bl_math_smoothstep_doc,
89  ".. function:: smoothstep(from_value, to_value, value)\n"
90  "\n"
91  " Performs smooth interpolation between 0 and 1 as value changes between from and "
92  "to values.\n"
93  " Outside the range the function returns the same value as the nearest edge.\n"
94  "\n"
95  " :arg from_value: The edge value where the result is 0.\n"
96  " :type from_value: float\n"
97  " :arg to_value: The edge value where the result is 1.\n"
98  " :type to_value: float\n"
99  " :arg factor: The interpolation value.\n"
100  " :type factor: float\n"
101  " :return: The interpolated value in [0.0, 1.0].\n"
102  " :rtype: float\n");
103 static PyObject *py_bl_math_smoothstep(PyObject *UNUSED(self), PyObject *args)
104 {
105  double a, b, x;
106  if (!PyArg_ParseTuple(args, "ddd:smoothstep", &a, &b, &x)) {
107  return NULL;
108  }
109 
110  double t = (x - a) / (b - a);
111 
112  CLAMP(t, 0.0, 1.0);
113 
114  return PyFloat_FromDouble(t * t * (3.0 - 2.0 * t));
115 }
116 
119 /* -------------------------------------------------------------------- */
123 static PyMethodDef M_bl_math_methods[] = {
124  {"clamp", (PyCFunction)py_bl_math_clamp, METH_VARARGS, py_bl_math_clamp_doc},
125  {"lerp", (PyCFunction)py_bl_math_lerp, METH_VARARGS, py_bl_math_lerp_doc},
126  {"smoothstep", (PyCFunction)py_bl_math_smoothstep, METH_VARARGS, py_bl_math_smoothstep_doc},
127  {NULL, NULL, 0, NULL},
128 };
129 
130 static struct PyModuleDef M_bl_math_module_def = {
131  PyModuleDef_HEAD_INIT,
132  "bl_math", /* m_name */
133  M_bl_math_doc, /* m_doc */
134  0, /* m_size */
135  M_bl_math_methods, /* m_methods */
136  NULL, /* m_reload */
137  NULL, /* m_traverse */
138  NULL, /* m_clear */
139  NULL, /* m_free */
140 };
141 
142 PyMODINIT_FUNC BPyInit_bl_math(void)
143 {
144  PyObject *submodule = PyModule_Create(&M_bl_math_module_def);
145  return submodule;
146 }
147 
#define UNUSED(x)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position CLAMP
static struct PyModuleDef M_bl_math_module_def
static PyObject * py_bl_math_smoothstep(PyObject *UNUSED(self), PyObject *args)
static PyObject * py_bl_math_clamp(PyObject *UNUSED(self), PyObject *args)
static PyObject * py_bl_math_lerp(PyObject *UNUSED(self), PyObject *args)
PyDoc_STRVAR(M_bl_math_doc, "Miscellaneous math utilities module")
static PyMethodDef M_bl_math_methods[]
PyMODINIT_FUNC BPyInit_bl_math(void)
static unsigned a[3]
Definition: RandGen.cpp:78
static const pxr::TfToken b("b", pxr::TfToken::Immortal)