Blender  V3.3
BPy_UnaryFunction1DFloat.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
8 
9 #include "../BPy_Convert.h"
10 #include "../BPy_IntegrationType.h"
11 #include "../BPy_Interface1D.h"
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 using namespace Freestyle;
18 
20 
21 //-------------------MODULE INITIALIZATION--------------------------------
22 
24 {
25  if (module == nullptr) {
26  return -1;
27  }
28 
29  if (PyType_Ready(&UnaryFunction1DFloat_Type) < 0) {
30  return -1;
31  }
32  Py_INCREF(&UnaryFunction1DFloat_Type);
33  PyModule_AddObject(module, "UnaryFunction1DFloat", (PyObject *)&UnaryFunction1DFloat_Type);
34 
35  return 0;
36 }
37 
38 //------------------------INSTANCE METHODS ----------------------------------
39 
41  "Class hierarchy: :class:`UnaryFunction1D` > :class:`UnaryFunction1DFloat`\n"
42  "\n"
43  "Base class for unary functions (functors) that work on\n"
44  ":class:`Interface1D` and return a float value.\n"
45  "\n"
46  ".. method:: __init__()\n"
47  " __init__(integration_type)\n"
48  "\n"
49  " Builds a unary 1D function using the default constructor\n"
50  " or the integration method given as an argument.\n"
51  "\n"
52  " :arg integration_type: An integration method.\n"
53  " :type integration_type: :class:`IntegrationType`\n";
54 
56  PyObject *args,
57  PyObject *kwds)
58 {
59  static const char *kwlist[] = {"integration", nullptr};
60  PyObject *obj = nullptr;
61 
62  if (!PyArg_ParseTupleAndKeywords(
63  args, kwds, "|O!", (char **)kwlist, &IntegrationType_Type, &obj)) {
64  return -1;
65  }
66 
67  if (!obj) {
68  self->uf1D_float = new UnaryFunction1D<float>();
69  }
70  else {
72  }
73 
74  self->uf1D_float->py_uf1D = (PyObject *)self;
75 
76  return 0;
77 }
78 
80 {
81  delete self->uf1D_float;
82  UnaryFunction1D_Type.tp_dealloc((PyObject *)self);
83 }
84 
86 {
87  return PyUnicode_FromFormat("type: %s - address: %p", Py_TYPE(self)->tp_name, self->uf1D_float);
88 }
89 
91  PyObject *args,
92  PyObject *kwds)
93 {
94  static const char *kwlist[] = {"inter", nullptr};
95  PyObject *obj = nullptr;
96 
97  if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!", (char **)kwlist, &Interface1D_Type, &obj)) {
98  return nullptr;
99  }
100 
101  if (typeid(*(self->uf1D_float)) == typeid(UnaryFunction1D<float>)) {
102  PyErr_SetString(PyExc_TypeError, "__call__ method not properly overridden");
103  return nullptr;
104  }
105  if (self->uf1D_float->operator()(*(((BPy_Interface1D *)obj)->if1D)) < 0) {
106  if (!PyErr_Occurred()) {
107  string class_name(Py_TYPE(self)->tp_name);
108  PyErr_SetString(PyExc_RuntimeError, (class_name + " __call__ method failed").c_str());
109  }
110  return nullptr;
111  }
112  return PyFloat_FromDouble(self->uf1D_float->result);
113 }
114 
115 /*----------------------UnaryFunction1DFloat get/setters ----------------------------*/
116 
117 PyDoc_STRVAR(integration_type_doc,
118  "The integration method.\n"
119  "\n"
120  ":type: :class:`IntegrationType`");
121 
122 static PyObject *integration_type_get(BPy_UnaryFunction1DFloat *self, void *UNUSED(closure))
123 {
124  return BPy_IntegrationType_from_IntegrationType(self->uf1D_float->getIntegrationType());
125 }
126 
128  PyObject *value,
129  void *UNUSED(closure))
130 {
131  if (!BPy_IntegrationType_Check(value)) {
132  PyErr_SetString(PyExc_TypeError, "value must be an IntegrationType");
133  return -1;
134  }
135  self->uf1D_float->setIntegrationType(IntegrationType_from_BPy_IntegrationType(value));
136  return 0;
137 }
138 
139 static PyGetSetDef BPy_UnaryFunction1DFloat_getseters[] = {
140  {"integration_type",
141  (getter)integration_type_get,
142  (setter)integration_type_set,
143  integration_type_doc,
144  nullptr},
145  {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
146 };
147 
148 /*-----------------------BPy_UnaryFunction1DFloat type definition ------------------------------*/
149 
151  PyVarObject_HEAD_INIT(nullptr, 0) "UnaryFunction1DFloat", /* tp_name */
152  sizeof(BPy_UnaryFunction1DFloat), /* tp_basicsize */
153  0, /* tp_itemsize */
154  (destructor)UnaryFunction1DFloat___dealloc__, /* tp_dealloc */
155  0, /* tp_vectorcall_offset */
156  nullptr, /* tp_getattr */
157  nullptr, /* tp_setattr */
158  nullptr, /* tp_reserved */
159  (reprfunc)UnaryFunction1DFloat___repr__, /* tp_repr */
160  nullptr, /* tp_as_number */
161  nullptr, /* tp_as_sequence */
162  nullptr, /* tp_as_mapping */
163  nullptr, /* tp_hash */
164  (ternaryfunc)UnaryFunction1DFloat___call__, /* tp_call */
165  nullptr, /* tp_str */
166  nullptr, /* tp_getattro */
167  nullptr, /* tp_setattro */
168  nullptr, /* tp_as_buffer */
169  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
170  UnaryFunction1DFloat___doc__, /* tp_doc */
171  nullptr, /* tp_traverse */
172  nullptr, /* tp_clear */
173  nullptr, /* tp_richcompare */
174  0, /* tp_weaklistoffset */
175  nullptr, /* tp_iter */
176  nullptr, /* tp_iternext */
177  nullptr, /* tp_methods */
178  nullptr, /* tp_members */
179  BPy_UnaryFunction1DFloat_getseters, /* tp_getset */
180  &UnaryFunction1D_Type, /* tp_base */
181  nullptr, /* tp_dict */
182  nullptr, /* tp_descr_get */
183  nullptr, /* tp_descr_set */
184  0, /* tp_dictoffset */
185  (initproc)UnaryFunction1DFloat___init__, /* tp_init */
186  nullptr, /* tp_alloc */
187  nullptr, /* tp_new */
188 };
189 
191 
192 #ifdef __cplusplus
193 }
194 #endif
#define UNUSED(x)
PyObject * BPy_IntegrationType_from_IntegrationType(IntegrationType i)
IntegrationType IntegrationType_from_BPy_IntegrationType(PyObject *obj)
PyTypeObject IntegrationType_Type
#define BPy_IntegrationType_Check(v)
PyTypeObject Interface1D_Type
PyDoc_STRVAR(integration_type_doc, "The integration method.\n" "\n" ":type: :class:`IntegrationType`")
PyTypeObject UnaryFunction1DFloat_Type
static int UnaryFunction1DFloat___init__(BPy_UnaryFunction1DFloat *self, PyObject *args, PyObject *kwds)
static int integration_type_set(BPy_UnaryFunction1DFloat *self, PyObject *value, void *UNUSED(closure))
static PyObject * integration_type_get(BPy_UnaryFunction1DFloat *self, void *UNUSED(closure))
int UnaryFunction1DFloat_Init(PyObject *module)
static PyObject * UnaryFunction1DFloat___repr__(BPy_UnaryFunction1DFloat *self)
static PyGetSetDef BPy_UnaryFunction1DFloat_getseters[]
static void UnaryFunction1DFloat___dealloc__(BPy_UnaryFunction1DFloat *self)
static char UnaryFunction1DFloat___doc__[]
static PyObject * UnaryFunction1DFloat___call__(BPy_UnaryFunction1DFloat *self, PyObject *args, PyObject *kwds)
PyTypeObject UnaryFunction1D_Type
PyObject * self
Definition: bpy_driver.c:165
inherits from class Rep
Definition: AppCanvas.cpp:18
static struct PyModuleDef module
Definition: python.cpp:972