Blender  V2.93
BPy_UnaryFunction1DFloat.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
22 
23 #include "../BPy_Convert.h"
24 #include "../BPy_IntegrationType.h"
25 #include "../BPy_Interface1D.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 using namespace Freestyle;
32 
34 
35 //-------------------MODULE INITIALIZATION--------------------------------
36 
38 {
39  if (module == nullptr) {
40  return -1;
41  }
42 
43  if (PyType_Ready(&UnaryFunction1DFloat_Type) < 0) {
44  return -1;
45  }
46  Py_INCREF(&UnaryFunction1DFloat_Type);
47  PyModule_AddObject(module, "UnaryFunction1DFloat", (PyObject *)&UnaryFunction1DFloat_Type);
48 
49  return 0;
50 }
51 
52 //------------------------INSTANCE METHODS ----------------------------------
53 
55  "Class hierarchy: :class:`UnaryFunction1D` > :class:`UnaryFunction1DFloat`\n"
56  "\n"
57  "Base class for unary functions (functors) that work on\n"
58  ":class:`Interface1D` and return a float value.\n"
59  "\n"
60  ".. method:: __init__()\n"
61  " __init__(integration_type)\n"
62  "\n"
63  " Builds a unary 1D function using the default constructor\n"
64  " or the integration method given as an argument.\n"
65  "\n"
66  " :arg integration_type: An integration method.\n"
67  " :type integration_type: :class:`IntegrationType`\n";
68 
70  PyObject *args,
71  PyObject *kwds)
72 {
73  static const char *kwlist[] = {"integration", nullptr};
74  PyObject *obj = nullptr;
75 
76  if (!PyArg_ParseTupleAndKeywords(
77  args, kwds, "|O!", (char **)kwlist, &IntegrationType_Type, &obj)) {
78  return -1;
79  }
80 
81  if (!obj) {
82  self->uf1D_float = new UnaryFunction1D<float>();
83  }
84  else {
86  }
87 
88  self->uf1D_float->py_uf1D = (PyObject *)self;
89 
90  return 0;
91 }
92 
94 {
95  delete self->uf1D_float;
96  UnaryFunction1D_Type.tp_dealloc((PyObject *)self);
97 }
98 
100 {
101  return PyUnicode_FromFormat("type: %s - address: %p", Py_TYPE(self)->tp_name, self->uf1D_float);
102 }
103 
105  PyObject *args,
106  PyObject *kwds)
107 {
108  static const char *kwlist[] = {"inter", nullptr};
109  PyObject *obj = nullptr;
110 
111  if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!", (char **)kwlist, &Interface1D_Type, &obj)) {
112  return nullptr;
113  }
114 
115  if (typeid(*(self->uf1D_float)) == typeid(UnaryFunction1D<float>)) {
116  PyErr_SetString(PyExc_TypeError, "__call__ method not properly overridden");
117  return nullptr;
118  }
119  if (self->uf1D_float->operator()(*(((BPy_Interface1D *)obj)->if1D)) < 0) {
120  if (!PyErr_Occurred()) {
121  string class_name(Py_TYPE(self)->tp_name);
122  PyErr_SetString(PyExc_RuntimeError, (class_name + " __call__ method failed").c_str());
123  }
124  return nullptr;
125  }
126  return PyFloat_FromDouble(self->uf1D_float->result);
127 }
128 
129 /*----------------------UnaryFunction1DFloat get/setters ----------------------------*/
130 
131 PyDoc_STRVAR(integration_type_doc,
132  "The integration method.\n"
133  "\n"
134  ":type: :class:`IntegrationType`");
135 
136 static PyObject *integration_type_get(BPy_UnaryFunction1DFloat *self, void *UNUSED(closure))
137 {
138  return BPy_IntegrationType_from_IntegrationType(self->uf1D_float->getIntegrationType());
139 }
140 
142  PyObject *value,
143  void *UNUSED(closure))
144 {
145  if (!BPy_IntegrationType_Check(value)) {
146  PyErr_SetString(PyExc_TypeError, "value must be an IntegrationType");
147  return -1;
148  }
149  self->uf1D_float->setIntegrationType(IntegrationType_from_BPy_IntegrationType(value));
150  return 0;
151 }
152 
153 static PyGetSetDef BPy_UnaryFunction1DFloat_getseters[] = {
154  {"integration_type",
155  (getter)integration_type_get,
156  (setter)integration_type_set,
157  integration_type_doc,
158  nullptr},
159  {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
160 };
161 
162 /*-----------------------BPy_UnaryFunction1DFloat type definition ------------------------------*/
163 
165  PyVarObject_HEAD_INIT(nullptr, 0) "UnaryFunction1DFloat", /* tp_name */
166  sizeof(BPy_UnaryFunction1DFloat), /* tp_basicsize */
167  0, /* tp_itemsize */
168  (destructor)UnaryFunction1DFloat___dealloc__, /* tp_dealloc */
169  0, /* tp_vectorcall_offset */
170  nullptr, /* tp_getattr */
171  nullptr, /* tp_setattr */
172  nullptr, /* tp_reserved */
173  (reprfunc)UnaryFunction1DFloat___repr__, /* tp_repr */
174  nullptr, /* tp_as_number */
175  nullptr, /* tp_as_sequence */
176  nullptr, /* tp_as_mapping */
177  nullptr, /* tp_hash */
178  (ternaryfunc)UnaryFunction1DFloat___call__, /* tp_call */
179  nullptr, /* tp_str */
180  nullptr, /* tp_getattro */
181  nullptr, /* tp_setattro */
182  nullptr, /* tp_as_buffer */
183  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
184  UnaryFunction1DFloat___doc__, /* tp_doc */
185  nullptr, /* tp_traverse */
186  nullptr, /* tp_clear */
187  nullptr, /* tp_richcompare */
188  0, /* tp_weaklistoffset */
189  nullptr, /* tp_iter */
190  nullptr, /* tp_iternext */
191  nullptr, /* tp_methods */
192  nullptr, /* tp_members */
193  BPy_UnaryFunction1DFloat_getseters, /* tp_getset */
194  &UnaryFunction1D_Type, /* tp_base */
195  nullptr, /* tp_dict */
196  nullptr, /* tp_descr_get */
197  nullptr, /* tp_descr_set */
198  0, /* tp_dictoffset */
199  (initproc)UnaryFunction1DFloat___init__, /* tp_init */
200  nullptr, /* tp_alloc */
201  nullptr, /* tp_new */
202 };
203 
205 
206 #ifdef __cplusplus
207 }
208 #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
static struct PyModuleDef module
PyObject * self
Definition: bpy_driver.c:185
inherits from class Rep
Definition: AppCanvas.cpp:32