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