Blender V5.0
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\n");
51static PyObject *Integrator_integrate(PyObject * /*self*/, PyObject *args, PyObject *kwds)
52{
53 static const char *kwlist[] = {"func", "it", "it_end", "integration_type", nullptr};
54 PyObject *obj1, *obj4 = nullptr;
55 BPy_Interface0DIterator *obj2, *obj3;
56
57 if (!PyArg_ParseTupleAndKeywords(args,
58 kwds,
59 "O!O!O!|O!",
60 (char **)kwlist,
62 &obj1,
64 &obj2,
66 &obj3,
68 &obj4))
69 {
70 return nullptr;
71 }
72
73 Interface0DIterator it(*(obj2->if0D_it)), it_end(*(obj3->if0D_it));
75
77 UnaryFunction0D<double> *fun = ((BPy_UnaryFunction0DDouble *)obj1)->uf0D_double;
78 double res = integrate(*fun, it, it_end, t);
79 return PyFloat_FromDouble(res);
80 }
82 UnaryFunction0D<float> *fun = ((BPy_UnaryFunction0DFloat *)obj1)->uf0D_float;
83 float res = integrate(*fun, it, it_end, t);
84 return PyFloat_FromDouble(res);
85 }
87 UnaryFunction0D<uint> *fun = ((BPy_UnaryFunction0DUnsigned *)obj1)->uf0D_unsigned;
88 uint res = integrate(*fun, it, it_end, t);
89 return PyLong_FromLong(res);
90 }
91
92 string class_name(Py_TYPE(obj1)->tp_name);
93 PyErr_SetString(PyExc_TypeError, ("unsupported function type: " + class_name).c_str());
94 return nullptr;
95}
96
97/*-----------------------Integrator module docstring---------------------------------------*/
98
100 /* Wrap. */
101 module_docstring,
102 "The Blender Freestyle.Integrator submodule\n"
103 "\n");
104
105/*-----------------------Integrator module functions definitions---------------------------*/
106
107#ifdef __GNUC__
108# ifdef __clang__
109# pragma clang diagnostic push
110# pragma clang diagnostic ignored "-Wcast-function-type"
111# else
112# pragma GCC diagnostic push
113# pragma GCC diagnostic ignored "-Wcast-function-type"
114# endif
115#endif
116
117static PyMethodDef module_functions[] = {
118 {"integrate",
119 (PyCFunction)Integrator_integrate,
120 METH_VARARGS | METH_KEYWORDS,
121 Integrator_integrate_doc},
122 {nullptr, nullptr, 0, nullptr},
123};
124
125#ifdef __GNUC__
126# ifdef __clang__
127# pragma clang diagnostic pop
128# else
129# pragma GCC diagnostic pop
130# endif
131#endif
132
133/*-----------------------Integrator module definition--------------------------------------*/
134
135static PyModuleDef module_definition = {
136 /*m_base*/ PyModuleDef_HEAD_INIT,
137 /*m_name*/ "Freestyle.Integrator",
138 /*m_doc*/ module_docstring,
139 /*m_size*/ -1,
140 /*m_methods*/ module_functions,
141 /*m_slots*/ nullptr,
142 /*m_traverse*/ nullptr,
143 /*m_clear*/ nullptr,
144 /*m_free*/ nullptr,
145};
146
147/*-----------------------BPy_IntegrationType type definition ------------------------------*/
148
150 /* Wrap. */
151 IntegrationType_doc,
152 "Class hierarchy: int > :class:`IntegrationType`\n"
153 "\n"
154 "Different integration methods that can be invoked to integrate into a\n"
155 "single value the set of values obtained from each 0D element of an 1D\n"
156 "element:\n"
157 "\n"
158 "* IntegrationType.MEAN: The value computed for the 1D element is the\n"
159 " mean of the values obtained for the 0D elements.\n"
160 "* IntegrationType.MIN: The value computed for the 1D element is the\n"
161 " minimum of the values obtained for the 0D elements.\n"
162 "* IntegrationType.MAX: The value computed for the 1D element is the\n"
163 " maximum of the values obtained for the 0D elements.\n"
164 "* IntegrationType.FIRST: The value computed for the 1D element is the\n"
165 " first of the values obtained for the 0D elements.\n"
166 "* IntegrationType.LAST: The value computed for the 1D element is the\n"
167 " last of the values obtained for the 0D elements.\n");
168PyTypeObject IntegrationType_Type = {
169 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
170 /*tp_name*/ "IntegrationType",
171 /*tp_basicsize*/ sizeof(PyLongObject),
172 /*tp_itemsize*/ 0,
173 /*tp_dealloc*/ nullptr,
174 /*tp_vectorcall_offset*/ 0,
175 /*tp_getattr*/ nullptr,
176 /*tp_setattr*/ nullptr,
177 /*tp_as_async*/ nullptr,
178 /*tp_repr*/ nullptr,
179 /*tp_as_number*/ nullptr,
180 /*tp_as_sequence*/ nullptr,
181 /*tp_as_mapping*/ nullptr,
182 /*tp_hash*/ nullptr,
183 /*tp_call*/ nullptr,
184 /*tp_str*/ nullptr,
185 /*tp_getattro*/ nullptr,
186 /*tp_setattro*/ nullptr,
187 /*tp_as_buffer*/ nullptr,
188 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
189 /*tp_doc*/ IntegrationType_doc,
190 /*tp_traverse*/ nullptr,
191 /*tp_clear*/ nullptr,
192 /*tp_richcompare*/ nullptr,
193 /*tp_weaklistoffset*/ 0,
194 /*tp_iter*/ nullptr,
195 /*tp_iternext*/ nullptr,
196 /*tp_methods*/ nullptr,
197 /*tp_members*/ nullptr,
198 /*tp_getset*/ nullptr,
199 /*tp_base*/ &PyLong_Type,
200 /*tp_dict*/ nullptr,
201 /*tp_descr_get*/ nullptr,
202 /*tp_descr_set*/ nullptr,
203 /*tp_dictoffset*/ 0,
204 /*tp_init*/ nullptr,
205 /*tp_alloc*/ nullptr,
206 /*tp_new*/ nullptr,
207};
208
209/*-----------------------BPy_IntegrationType instance definitions -------------------------*/
210
211//-------------------MODULE INITIALIZATION--------------------------------
213{
214 PyObject *m, *d, *f;
215
216 if (module == nullptr) {
217 return -1;
218 }
219
220 if (PyType_Ready(&IntegrationType_Type) < 0) {
221 return -1;
222 }
223 PyModule_AddObjectRef(module, "IntegrationType", (PyObject *)&IntegrationType_Type);
224
225#define ADD_TYPE_CONST(id) \
226 PyLong_subtype_add_to_dict( \
227 IntegrationType_Type.tp_dict, &IntegrationType_Type, STRINGIFY(id), id)
228
234
235#undef ADD_TYPE_CONST
236
237 m = PyModule_Create(&module_definition);
238 if (m == nullptr) {
239 return -1;
240 }
241 PyModule_AddObjectRef(module, "Integrator", m);
242
243 // from Integrator import *
244 d = PyModule_GetDict(m);
245 for (PyMethodDef *p = module_functions; p->ml_name; p++) {
246 f = PyDict_GetItemString(d, p->ml_name);
247 PyModule_AddObjectRef(module, p->ml_name, f);
248 }
249
250 return 0;
251}
252
unsigned int uint
static PyModuleDef module_definition
static PyMethodDef module_functions[]
IntegrationType IntegrationType_from_BPy_IntegrationType(PyObject *obj)
PyTypeObject IntegrationType_Type
int IntegrationType_Init(PyObject *module)
#define ADD_TYPE_CONST(id)
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\n")
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