Blender  V3.3
BPy_Interface1D.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include "BPy_Interface1D.h"
8 
9 #include "BPy_Convert.h"
10 #include "Interface1D/BPy_FEdge.h"
12 #include "Interface1D/BPy_Stroke.h"
17 
18 #include "BPy_MediumType.h"
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 using namespace Freestyle;
25 
27 
28 //-------------------MODULE INITIALIZATION--------------------------------
29 int Interface1D_Init(PyObject *module)
30 {
31  if (module == nullptr) {
32  return -1;
33  }
34 
35  if (PyType_Ready(&Interface1D_Type) < 0) {
36  return -1;
37  }
38  Py_INCREF(&Interface1D_Type);
39  PyModule_AddObject(module, "Interface1D", (PyObject *)&Interface1D_Type);
40 
41  if (PyType_Ready(&FrsCurve_Type) < 0) {
42  return -1;
43  }
44  Py_INCREF(&FrsCurve_Type);
45  PyModule_AddObject(module, "Curve", (PyObject *)&FrsCurve_Type);
46 
47  if (PyType_Ready(&Chain_Type) < 0) {
48  return -1;
49  }
50  Py_INCREF(&Chain_Type);
51  PyModule_AddObject(module, "Chain", (PyObject *)&Chain_Type);
52 
53  if (PyType_Ready(&FEdge_Type) < 0) {
54  return -1;
55  }
56  Py_INCREF(&FEdge_Type);
57  PyModule_AddObject(module, "FEdge", (PyObject *)&FEdge_Type);
58 
59  if (PyType_Ready(&FEdgeSharp_Type) < 0) {
60  return -1;
61  }
62  Py_INCREF(&FEdgeSharp_Type);
63  PyModule_AddObject(module, "FEdgeSharp", (PyObject *)&FEdgeSharp_Type);
64 
65  if (PyType_Ready(&FEdgeSmooth_Type) < 0) {
66  return -1;
67  }
68  Py_INCREF(&FEdgeSmooth_Type);
69  PyModule_AddObject(module, "FEdgeSmooth", (PyObject *)&FEdgeSmooth_Type);
70 
71  if (PyType_Ready(&Stroke_Type) < 0) {
72  return -1;
73  }
74  Py_INCREF(&Stroke_Type);
75  PyModule_AddObject(module, "Stroke", (PyObject *)&Stroke_Type);
76 
77  PyDict_SetItemString(Stroke_Type.tp_dict, "DRY_MEDIUM", BPy_MediumType_DRY_MEDIUM);
78  PyDict_SetItemString(Stroke_Type.tp_dict, "HUMID_MEDIUM", BPy_MediumType_HUMID_MEDIUM);
79  PyDict_SetItemString(Stroke_Type.tp_dict, "OPAQUE_MEDIUM", BPy_MediumType_OPAQUE_MEDIUM);
80 
81  if (PyType_Ready(&ViewEdge_Type) < 0) {
82  return -1;
83  }
84  Py_INCREF(&ViewEdge_Type);
85  PyModule_AddObject(module, "ViewEdge", (PyObject *)&ViewEdge_Type);
86 
89 
90  return 0;
91 }
92 
93 /*----------------------Interface1D methods ----------------------------*/
94 
95 PyDoc_STRVAR(Interface1D_doc,
96  "Base class for any 1D element.\n"
97  "\n"
98  ".. method:: __init__()\n"
99  "\n"
100  " Default constructor.");
101 
102 static int Interface1D_init(BPy_Interface1D *self, PyObject *args, PyObject *kwds)
103 {
104  static const char *kwlist[] = {nullptr};
105 
106  if (!PyArg_ParseTupleAndKeywords(args, kwds, "", (char **)kwlist)) {
107  return -1;
108  }
109  self->if1D = new Interface1D();
110  self->borrowed = false;
111  return 0;
112 }
113 
115 {
116  if (self->if1D && !self->borrowed) {
117  delete self->if1D;
118  }
119  Py_TYPE(self)->tp_free((PyObject *)self);
120 }
121 
122 static PyObject *Interface1D_repr(BPy_Interface1D *self)
123 {
124  return PyUnicode_FromFormat(
125  "type: %s - address: %p", self->if1D->getExactTypeName().c_str(), self->if1D);
126 }
127 
128 PyDoc_STRVAR(Interface1D_vertices_begin_doc,
129  ".. method:: vertices_begin()\n"
130  "\n"
131  " Returns an iterator over the Interface1D vertices, pointing to the\n"
132  " first vertex.\n"
133  "\n"
134  " :return: An Interface0DIterator pointing to the first vertex.\n"
135  " :rtype: :class:`Interface0DIterator`");
136 
138 {
139  Interface0DIterator if0D_it(self->if1D->verticesBegin());
141 }
142 
143 PyDoc_STRVAR(Interface1D_vertices_end_doc,
144  ".. method:: vertices_end()\n"
145  "\n"
146  " Returns an iterator over the Interface1D vertices, pointing after\n"
147  " the last vertex.\n"
148  "\n"
149  " :return: An Interface0DIterator pointing after the last vertex.\n"
150  " :rtype: :class:`Interface0DIterator`");
151 
153 {
154  Interface0DIterator if0D_it(self->if1D->verticesEnd());
156 }
157 
158 PyDoc_STRVAR(Interface1D_points_begin_doc,
159  ".. method:: points_begin(t=0.0)\n"
160  "\n"
161  " Returns an iterator over the Interface1D points, pointing to the\n"
162  " first point. The difference with vertices_begin() is that here we can\n"
163  " iterate over points of the 1D element at a any given sampling.\n"
164  " Indeed, for each iteration, a virtual point is created.\n"
165  "\n"
166  " :arg t: A sampling with which we want to iterate over points of\n"
167  " this 1D element.\n"
168  " :type t: float\n"
169  " :return: An Interface0DIterator pointing to the first point.\n"
170  " :rtype: :class:`Interface0DIterator`");
171 
172 static PyObject *Interface1D_points_begin(BPy_Interface1D *self, PyObject *args, PyObject *kwds)
173 {
174  static const char *kwlist[] = {"t", nullptr};
175  float f = 0.0f;
176 
177  if (!PyArg_ParseTupleAndKeywords(args, kwds, "|f", (char **)kwlist, &f)) {
178  return nullptr;
179  }
180  Interface0DIterator if0D_it(self->if1D->pointsBegin(f));
182 }
183 
184 PyDoc_STRVAR(Interface1D_points_end_doc,
185  ".. method:: points_end(t=0.0)\n"
186  "\n"
187  " Returns an iterator over the Interface1D points, pointing after the\n"
188  " last point. The difference with vertices_end() is that here we can\n"
189  " iterate over points of the 1D element at a given sampling. Indeed,\n"
190  " for each iteration, a virtual point is created.\n"
191  "\n"
192  " :arg t: A sampling with which we want to iterate over points of\n"
193  " this 1D element.\n"
194  " :type t: float\n"
195  " :return: An Interface0DIterator pointing after the last point.\n"
196  " :rtype: :class:`Interface0DIterator`");
197 
198 static PyObject *Interface1D_points_end(BPy_Interface1D *self, PyObject *args, PyObject *kwds)
199 {
200  static const char *kwlist[] = {"t", nullptr};
201  float f = 0.0f;
202 
203  if (!PyArg_ParseTupleAndKeywords(args, kwds, "|f", (char **)kwlist, &f)) {
204  return nullptr;
205  }
206  Interface0DIterator if0D_it(self->if1D->pointsEnd(f));
208 }
209 
210 static PyMethodDef BPy_Interface1D_methods[] = {
211  {"vertices_begin",
212  (PyCFunction)Interface1D_vertices_begin,
213  METH_NOARGS,
214  Interface1D_vertices_begin_doc},
215  {"vertices_end",
216  (PyCFunction)Interface1D_vertices_end,
217  METH_NOARGS,
218  Interface1D_vertices_end_doc},
219  {"points_begin",
220  (PyCFunction)Interface1D_points_begin,
221  METH_VARARGS | METH_KEYWORDS,
222  Interface1D_points_begin_doc},
223  {"points_end",
224  (PyCFunction)Interface1D_points_end,
225  METH_VARARGS | METH_KEYWORDS,
226  Interface1D_points_end_doc},
227  {nullptr, nullptr, 0, nullptr},
228 };
229 
230 /*----------------------Interface1D get/setters ----------------------------*/
231 
232 PyDoc_STRVAR(Interface1D_name_doc,
233  "The string of the name of the 1D element.\n"
234  "\n"
235  ":type: str");
236 
237 static PyObject *Interface1D_name_get(BPy_Interface1D *self, void *UNUSED(closure))
238 {
239  return PyUnicode_FromString(Py_TYPE(self)->tp_name);
240 }
241 
242 PyDoc_STRVAR(Interface1D_id_doc,
243  "The Id of this Interface1D.\n"
244  "\n"
245  ":type: :class:`Id`");
246 
247 static PyObject *Interface1D_id_get(BPy_Interface1D *self, void *UNUSED(closure))
248 {
249  Id id(self->if1D->getId());
250  if (PyErr_Occurred()) {
251  return nullptr;
252  }
253  return BPy_Id_from_Id(id); // return a copy
254 }
255 
256 PyDoc_STRVAR(Interface1D_nature_doc,
257  "The nature of this Interface1D.\n"
258  "\n"
259  ":type: :class:`Nature`");
260 
261 static PyObject *Interface1D_nature_get(BPy_Interface1D *self, void *UNUSED(closure))
262 {
263  Nature::VertexNature nature = self->if1D->getNature();
264  if (PyErr_Occurred()) {
265  return nullptr;
266  }
267  return BPy_Nature_from_Nature(nature);
268 }
269 
270 PyDoc_STRVAR(Interface1D_length_2d_doc,
271  "The 2D length of this Interface1D.\n"
272  "\n"
273  ":type: float");
274 
275 static PyObject *Interface1D_length_2d_get(BPy_Interface1D *self, void *UNUSED(closure))
276 {
277  real length = self->if1D->getLength2D();
278  if (PyErr_Occurred()) {
279  return nullptr;
280  }
281  return PyFloat_FromDouble((double)length);
282 }
283 
284 PyDoc_STRVAR(Interface1D_time_stamp_doc,
285  "The time stamp of the 1D element, mainly used for selection.\n"
286  "\n"
287  ":type: int");
288 
289 static PyObject *Interface1D_time_stamp_get(BPy_Interface1D *self, void *UNUSED(closure))
290 {
291  return PyLong_FromLong(self->if1D->getTimeStamp());
292 }
293 
295  PyObject *value,
296  void *UNUSED(closure))
297 {
298  int timestamp;
299 
300  if ((timestamp = PyLong_AsLong(value)) == -1 && PyErr_Occurred()) {
301  PyErr_SetString(PyExc_TypeError, "value must be a number");
302  return -1;
303  }
304  self->if1D->setTimeStamp(timestamp);
305  return 0;
306 }
307 
308 static PyGetSetDef BPy_Interface1D_getseters[] = {
309  {"name", (getter)Interface1D_name_get, (setter) nullptr, Interface1D_name_doc, nullptr},
310  {"id", (getter)Interface1D_id_get, (setter) nullptr, Interface1D_id_doc, nullptr},
311  {"nature", (getter)Interface1D_nature_get, (setter) nullptr, Interface1D_nature_doc, nullptr},
312  {"length_2d",
314  (setter) nullptr,
315  Interface1D_length_2d_doc,
316  nullptr},
317  {"time_stamp",
320  Interface1D_time_stamp_doc,
321  nullptr},
322  {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
323 };
324 
325 /*-----------------------BPy_Interface1D type definition ------------------------------*/
326 
327 PyTypeObject Interface1D_Type = {
328  PyVarObject_HEAD_INIT(nullptr, 0) "Interface1D", /* tp_name */
329  sizeof(BPy_Interface1D), /* tp_basicsize */
330  0, /* tp_itemsize */
331  (destructor)Interface1D_dealloc, /* tp_dealloc */
332  0, /* tp_vectorcall_offset */
333  nullptr, /* tp_getattr */
334  nullptr, /* tp_setattr */
335  nullptr, /* tp_reserved */
336  (reprfunc)Interface1D_repr, /* tp_repr */
337  nullptr, /* tp_as_number */
338  nullptr, /* tp_as_sequence */
339  nullptr, /* tp_as_mapping */
340  nullptr, /* tp_hash */
341  nullptr, /* tp_call */
342  nullptr, /* tp_str */
343  nullptr, /* tp_getattro */
344  nullptr, /* tp_setattro */
345  nullptr, /* tp_as_buffer */
346  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
347  Interface1D_doc, /* tp_doc */
348  nullptr, /* tp_traverse */
349  nullptr, /* tp_clear */
350  nullptr, /* tp_richcompare */
351  0, /* tp_weaklistoffset */
352  nullptr, /* tp_iter */
353  nullptr, /* tp_iternext */
354  BPy_Interface1D_methods, /* tp_methods */
355  nullptr, /* tp_members */
356  BPy_Interface1D_getseters, /* tp_getset */
357  nullptr, /* tp_base */
358  nullptr, /* tp_dict */
359  nullptr, /* tp_descr_get */
360  nullptr, /* tp_descr_set */
361  0, /* tp_dictoffset */
362  (initproc)Interface1D_init, /* tp_init */
363  nullptr, /* tp_alloc */
364  PyType_GenericNew, /* tp_new */
365 };
366 
368 
369 #ifdef __cplusplus
370 }
371 #endif
#define UNUSED(x)
PyTypeObject Chain_Type
Definition: BPy_Chain.cpp:136
PyObject * BPy_Interface0DIterator_from_Interface0DIterator(Interface0DIterator &if0D_it, bool reversed)
PyObject * BPy_Id_from_Id(Id &id)
Definition: BPy_Convert.cpp:90
PyObject * BPy_Nature_from_Nature(unsigned short n)
PyTypeObject FEdgeSharp_Type
void FEdgeSharp_mathutils_register_callback()
void FEdgeSmooth_mathutils_register_callback()
PyTypeObject FEdgeSmooth_Type
PyTypeObject FEdge_Type
Definition: BPy_FEdge.cpp:344
PyTypeObject FrsCurve_Type
static void Interface1D_dealloc(BPy_Interface1D *self)
static int Interface1D_time_stamp_set(BPy_Interface1D *self, PyObject *value, void *UNUSED(closure))
static PyObject * Interface1D_time_stamp_get(BPy_Interface1D *self, void *UNUSED(closure))
static PyObject * Interface1D_points_begin(BPy_Interface1D *self, PyObject *args, PyObject *kwds)
PyDoc_STRVAR(Interface1D_doc, "Base class for any 1D element.\n" "\n" ".. method:: __init__()\n" "\n" " Default constructor.")
static PyObject * Interface1D_vertices_end(BPy_Interface1D *self)
PyTypeObject Interface1D_Type
static int Interface1D_init(BPy_Interface1D *self, PyObject *args, PyObject *kwds)
static PyObject * Interface1D_vertices_begin(BPy_Interface1D *self)
static PyObject * Interface1D_repr(BPy_Interface1D *self)
static PyObject * Interface1D_points_end(BPy_Interface1D *self, PyObject *args, PyObject *kwds)
int Interface1D_Init(PyObject *module)
static PyObject * Interface1D_id_get(BPy_Interface1D *self, void *UNUSED(closure))
static PyObject * Interface1D_length_2d_get(BPy_Interface1D *self, void *UNUSED(closure))
static PyGetSetDef BPy_Interface1D_getseters[]
static PyObject * Interface1D_name_get(BPy_Interface1D *self, void *UNUSED(closure))
static PyMethodDef BPy_Interface1D_methods[]
static PyObject * Interface1D_nature_get(BPy_Interface1D *self, void *UNUSED(closure))
#define BPy_MediumType_HUMID_MEDIUM
#define BPy_MediumType_OPAQUE_MEDIUM
#define BPy_MediumType_DRY_MEDIUM
PyTypeObject Stroke_Type
Definition: BPy_Stroke.cpp:487
PyTypeObject ViewEdge_Type
PyObject * self
Definition: bpy_driver.c:165
unsigned short VertexNature
Definition: Nature.h:18
inherits from class Rep
Definition: AppCanvas.cpp:18
double real
Definition: Precision.h:12
T length(const vec_base< T, Size > &a)
static struct PyModuleDef module
Definition: python.cpp:972