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