Blender V4.5
BPy_IntegrationType.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2004-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "BPy_Convert.h"
16
17#include "BLI_sys_types.h"
18
19using namespace Freestyle;
20
22
23//------------------------ MODULE FUNCTIONS ----------------------------------
24
26 /* Wrap. */
27 Integrator_integrate_doc,
28 ".. function:: integrate(func, it, it_end, integration_type)\n"
29 "\n"
30 " Returns a single value from a set of values evaluated at each 0D\n"
31 " element of this 1D element.\n"
32 "\n"
33 " :arg func: The UnaryFunction0D used to compute a value at each\n"
34 " Interface0D.\n"
35 " :type func: :class:`UnaryFunction0D`\n"
36 " :arg it: The Interface0DIterator used to iterate over the 0D\n"
37 " elements of this 1D element. The integration will occur over\n"
38 " the 0D elements starting from the one pointed by it.\n"
39 " :type it: :class:`Interface0DIterator`\n"
40 " :arg it_end: The Interface0DIterator pointing the end of the 0D\n"
41 " elements of the 1D element.\n"
42 " :type it_end: :class:`Interface0DIterator`\n"
43 " :arg integration_type: The integration method used to compute a\n"
44 " single value from a set of values.\n"
45 " :type integration_type: :class:`IntegrationType`\n"
46 " :return: The single value obtained for the 1D element. The return\n"
47 " value type is float if func is of the :class:`UnaryFunction0DDouble`\n"
48 " or :class:`UnaryFunction0DFloat` type, and int if func is of the\n"
49 " :class:`UnaryFunction0DUnsigned` type.\n"
50 " :rtype: int | float");
51
52static PyObject *Integrator_integrate(PyObject * /*self*/, PyObject *args, PyObject *kwds)
53{
54 static const char *kwlist[] = {"func", "it", "it_end", "integration_type", nullptr};
55 PyObject *obj1, *obj4 = nullptr;
56 BPy_Interface0DIterator *obj2, *obj3;
57
58 if (!PyArg_ParseTupleAndKeywords(args,
59 kwds,
60 "O!O!O!|O!",
61 (char **)kwlist,
63 &obj1,
65 &obj2,
67 &obj3,
69 &obj4))
70 {
71 return nullptr;
72 }
73
74 Interface0DIterator it(*(obj2->if0D_it)), it_end(*(obj3->if0D_it));
76
78 UnaryFunction0D<double> *fun = ((BPy_UnaryFunction0DDouble *)obj1)->uf0D_double;
79 double res = integrate(*fun, it, it_end, t);
80 return PyFloat_FromDouble(res);
81 }
83 UnaryFunction0D<float> *fun = ((BPy_UnaryFunction0DFloat *)obj1)->uf0D_float;
84 float res = integrate(*fun, it, it_end, t);
85 return PyFloat_FromDouble(res);
86 }
88 UnaryFunction0D<uint> *fun = ((BPy_UnaryFunction0DUnsigned *)obj1)->uf0D_unsigned;
89 uint res = integrate(*fun, it, it_end, t);
90 return PyLong_FromLong(res);
91 }
92
93 string class_name(Py_TYPE(obj1)->tp_name);
94 PyErr_SetString(PyExc_TypeError, ("unsupported function type: " + class_name).c_str());
95 return nullptr;
96}
97
98/*-----------------------Integrator module docstring---------------------------------------*/
99
101 /* Wrap. */
102 module_docstring,
103 "The Blender Freestyle.Integrator submodule\n"
104 "\n");
105
106/*-----------------------Integrator module functions definitions---------------------------*/
107
108#ifdef __GNUC__
109# ifdef __clang__
110# pragma clang diagnostic push
111# pragma clang diagnostic ignored "-Wcast-function-type"
112# else
113# pragma GCC diagnostic push
114# pragma GCC diagnostic ignored "-Wcast-function-type"
115# endif
116#endif
117
118static PyMethodDef module_functions[] = {
119 {"integrate",
120 (PyCFunction)Integrator_integrate,
121 METH_VARARGS | METH_KEYWORDS,
122 Integrator_integrate_doc},
123 {nullptr, nullptr, 0, nullptr},
124};
125
126#ifdef __GNUC__
127# ifdef __clang__
128# pragma clang diagnostic pop
129# else
130# pragma GCC diagnostic pop
131# endif
132#endif
133
134/*-----------------------Integrator module definition--------------------------------------*/
135
136static PyModuleDef module_definition = {
137 /*m_base*/ PyModuleDef_HEAD_INIT,
138 /*m_name*/ "Freestyle.Integrator",
139 /*m_doc*/ module_docstring,
140 /*m_size*/ -1,
141 /*m_methods*/ module_functions,
142 /*m_slots*/ nullptr,
143 /*m_traverse*/ nullptr,
144 /*m_clear*/ nullptr,
145 /*m_free*/ nullptr,
146};
147
148/*-----------------------BPy_IntegrationType type definition ------------------------------*/
149
151 /* Wrap. */
152 IntegrationType_doc,
153 "Class hierarchy: int > :class:`IntegrationType`\n"
154 "\n"
155 "Different integration methods that can be invoked to integrate into a\n"
156 "single value the set of values obtained from each 0D element of an 1D\n"
157 "element:\n"
158 "\n"
159 "* IntegrationType.MEAN: The value computed for the 1D element is the\n"
160 " mean of the values obtained for the 0D elements.\n"
161 "* IntegrationType.MIN: The value computed for the 1D element is the\n"
162 " minimum of the values obtained for the 0D elements.\n"
163 "* IntegrationType.MAX: The value computed for the 1D element is the\n"
164 " maximum of the values obtained for the 0D elements.\n"
165 "* IntegrationType.FIRST: The value computed for the 1D element is the\n"
166 " first of the values obtained for the 0D elements.\n"
167 "* IntegrationType.LAST: The value computed for the 1D element is the\n"
168 " last of the values obtained for the 0D elements.");
169
170PyTypeObject IntegrationType_Type = {
171 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
172 /*tp_name*/ "IntegrationType",
173 /*tp_basicsize*/ sizeof(PyLongObject),
174 /*tp_itemsize*/ 0,
175 /*tp_dealloc*/ nullptr,
176 /*tp_vectorcall_offset*/ 0,
177 /*tp_getattr*/ nullptr,
178 /*tp_setattr*/ nullptr,
179 /*tp_as_async*/ nullptr,
180 /*tp_repr*/ nullptr,
181 /*tp_as_number*/ nullptr,
182 /*tp_as_sequence*/ nullptr,
183 /*tp_as_mapping*/ nullptr,
184 /*tp_hash*/ nullptr,
185 /*tp_call*/ nullptr,
186 /*tp_str*/ nullptr,
187 /*tp_getattro*/ nullptr,
188 /*tp_setattro*/ nullptr,
189 /*tp_as_buffer*/ nullptr,
190 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
191 /*tp_doc*/ IntegrationType_doc,
192 /*tp_traverse*/ nullptr,
193 /*tp_clear*/ nullptr,
194 /*tp_richcompare*/ nullptr,
195 /*tp_weaklistoffset*/ 0,
196 /*tp_iter*/ nullptr,
197 /*tp_iternext*/ nullptr,
198 /*tp_methods*/ nullptr,
199 /*tp_members*/ nullptr,
200 /*tp_getset*/ nullptr,
201 /*tp_base*/ &PyLong_Type,
202 /*tp_dict*/ nullptr,
203 /*tp_descr_get*/ nullptr,
204 /*tp_descr_set*/ nullptr,
205 /*tp_dictoffset*/ 0,
206 /*tp_init*/ nullptr,
207 /*tp_alloc*/ nullptr,
208 /*tp_new*/ nullptr,
209};
210
211/*-----------------------BPy_IntegrationType instance definitions -------------------------*/
212
213//-------------------MODULE INITIALIZATION--------------------------------
215{
216 PyObject *m, *d, *f;
217
218 if (module == nullptr) {
219 return -1;
220 }
221
222 if (PyType_Ready(&IntegrationType_Type) < 0) {
223 return -1;
224 }
225 PyModule_AddObjectRef(module, "IntegrationType", (PyObject *)&IntegrationType_Type);
226
227#define ADD_TYPE_CONST(id) \
228 PyLong_subtype_add_to_dict( \
229 IntegrationType_Type.tp_dict, &IntegrationType_Type, STRINGIFY(id), id)
230
236
237#undef ADD_TYPE_CONST
238
239 m = PyModule_Create(&module_definition);
240 if (m == nullptr) {
241 return -1;
242 }
243 PyModule_AddObjectRef(module, "Integrator", m);
244
245 // from Integrator import *
246 d = PyModule_GetDict(m);
247 for (PyMethodDef *p = module_functions; p->ml_name; p++) {
248 f = PyDict_GetItemString(d, p->ml_name);
249 PyModule_AddObjectRef(module, p->ml_name, f);
250 }
251
252 return 0;
253}
254
unsigned int uint
static PyModuleDef module_definition
static PyMethodDef module_functions[]
IntegrationType IntegrationType_from_BPy_IntegrationType(PyObject *obj)
PyDoc_STRVAR(Integrator_integrate_doc, ".. function:: integrate(func, it, it_end, integration_type)\n" "\n" " Returns a single value from a set of values evaluated at each 0D\n" " element of this 1D element.\n" "\n" " :arg func: The UnaryFunction0D used to compute a value at each\n" " Interface0D.\n" " :type func: :class:`UnaryFunction0D`\n" " :arg it: The Interface0DIterator used to iterate over the 0D\n" " elements of this 1D element. The integration will occur over\n" " the 0D elements starting from the one pointed by it.\n" " :type it: :class:`Interface0DIterator`\n" " :arg it_end: The Interface0DIterator pointing the end of the 0D\n" " elements of the 1D element.\n" " :type it_end: :class:`Interface0DIterator`\n" " :arg integration_type: The integration method used to compute a\n" " single value from a set of values.\n" " :type integration_type: :class:`IntegrationType`\n" " :return: The single value obtained for the 1D element. The return\n" " value type is float if func is of the :class:`UnaryFunction0DDouble`\n" " or :class:`UnaryFunction0DFloat` type, and int if func is of the\n" " :class:`UnaryFunction0DUnsigned` type.\n" " :rtype: int | float")
PyTypeObject IntegrationType_Type
int IntegrationType_Init(PyObject *module)
#define ADD_TYPE_CONST(id)
static PyObject * Integrator_integrate(PyObject *, PyObject *args, PyObject *kwds)
PyTypeObject Interface0DIterator_Type
#define BPy_UnaryFunction0DDouble_Check(v)
#define BPy_UnaryFunction0DFloat_Check(v)
#define BPy_UnaryFunction0DUnsigned_Check(v)
PyTypeObject UnaryFunction0D_Type
#define MIN(_a, _b)
@ FIRST
@ LAST
inherits from class Rep
Definition AppCanvas.cpp:20
T integrate(UnaryFunction0D< T > &fun, Interface0DIterator it, Interface0DIterator it_end, IntegrationType integration_type=MEAN)
Definition Interface1D.h:73
static struct PyModuleDef module
Definition python.cpp:796
Freestyle::Interface0DIterator * if0D_it