Blender V5.3
BPy_ChainingIterator.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
8
10
11#include "../BPy_Convert.h"
15
16using namespace Freestyle;
17
19
20//------------------------INSTANCE METHODS ----------------------------------
21
23 /* Wrap. */
24 ChainingIterator_doc,
25 "Class hierarchy: :class:`Iterator` > :class:`ViewEdgeIterator` > :class:`ChainingIterator`\n"
26 "\n"
27 "Base class for chaining iterators. This class is designed to be\n"
28 "overloaded in order to describe chaining rules. It makes the\n"
29 "description of chaining rules easier. The two main methods that need\n"
30 "to overloaded are traverse() and init(). traverse() tells which\n"
31 ":class:`ViewEdge` to follow, among the adjacent ones. If you specify\n"
32 "restriction rules (such as \"Chain only ViewEdges of the selection\"),\n"
33 "they will be included in the adjacency iterator (i.e, the adjacent\n"
34 "iterator will only stop on \"valid\" edges).\n"
35 "\n"
36 ".. method:: __init__(*args)\n"
37 "\n"
38 " Accepted call signatures:\n"
39 "\n"
40 " - ``__init__(restrict_to_selection=True, restrict_to_unvisited=True, begin=None, "
41 "orientation=True)``\n"
42 " - ``__init__(brother)``\n"
43 "\n"
44 " Builds a Chaining Iterator from the first ViewEdge used for\n"
45 " iteration and its orientation or by using the copy constructor.\n"
46 "\n"
47 " :param restrict_to_selection: Indicates whether to force the chaining\n"
48 " to stay within the set of selected ViewEdges or not.\n"
49 " :type restrict_to_selection: bool\n"
50 " :param restrict_to_unvisited: Indicates whether a ViewEdge that has\n"
51 " already been chained must be ignored ot not.\n"
52 " :type restrict_to_unvisited: bool\n"
53 " :param begin: The ViewEdge from which to start the chain.\n"
54 " :type begin: :class:`ViewEdge` | None\n"
55 " :param orientation: The direction to follow to explore the graph. If\n"
56 " true, the direction indicated by the first ViewEdge is used.\n"
57 " :type orientation: bool\n"
58 " :param brother: \n"
59 " :type brother: ChainingIterator\n");
60static int check_begin(PyObject *obj, void *v)
61{
62 if (obj != nullptr && obj != Py_None && !BPy_ViewEdge_Check(obj)) {
63 return 0;
64 }
65 *((PyObject **)v) = obj;
66 return 1;
67}
68
69static int ChainingIterator___init__(BPy_ChainingIterator *self, PyObject *args, PyObject *kwds)
70{
71 static const char *kwlist_1[] = {"brother", nullptr};
72 static const char *kwlist_2[] = {
73 "restrict_to_selection", "restrict_to_unvisited", "begin", "orientation", nullptr};
74 PyObject *obj1 = nullptr, *obj2 = nullptr, *obj3 = nullptr, *obj4 = nullptr;
75
76 if (PyArg_ParseTupleAndKeywords(args,
77 kwds,
78 "O!" /* `brother` */
79 ":__init__",
80 (char **)kwlist_1,
82 &obj1))
83 {
84 self->c_it = new ChainingIterator(*(((BPy_ChainingIterator *)obj1)->c_it));
85 }
86 else if ((void)PyErr_Clear(),
87 (void)(obj1 = obj2 = obj3 = obj4 = nullptr),
88 PyArg_ParseTupleAndKeywords(args,
89 kwds,
90 "|" /* Optional arguments. */
91 "O!" /* `restrict_to_selection` */
92 "O!" /* `restrict_to_unvisited` */
93 "O&" /* `begin` */
94 "O!" /* `orientation` */
95 ":__init__",
96 (char **)kwlist_2,
97 &PyBool_Type,
98 &obj1,
99 &PyBool_Type,
100 &obj2,
102 &obj3,
103 &PyBool_Type,
104 &obj4))
105 {
106 bool restrict_to_selection = (!obj1) ? true : bool_from_PyBool(obj1);
107 bool restrict_to_unvisited = (!obj2) ? true : bool_from_PyBool(obj2);
108 ViewEdge *begin = (!obj3 || obj3 == Py_None) ? nullptr : ((BPy_ViewEdge *)obj3)->ve;
109 bool orientation = (!obj4) ? true : bool_from_PyBool(obj4);
110 self->c_it = new ChainingIterator(
111 restrict_to_selection, restrict_to_unvisited, begin, orientation);
112 }
113 else {
114 PyErr_SetString(PyExc_TypeError, "invalid argument(s)");
115 return -1;
116 }
117 self->py_ve_it.ve_it = self->c_it;
118 self->py_ve_it.py_it.it = self->c_it;
119
120 self->c_it->py_c_it = (PyObject *)self;
121
122 return 0;
123}
124
126 /* Wrap. */
127 ChainingIterator_init_doc,
128 ".. method:: init()\n"
129 "\n"
130 " Initializes the iterator context. This method is called each\n"
131 " time a new chain is started. It can be used to reset some\n"
132 " history information that you might want to keep.\n");
134{
135 if (typeid(*(self->c_it)) == typeid(ChainingIterator)) {
136 PyErr_SetString(PyExc_TypeError, "init() method not properly overridden");
137 return nullptr;
138 }
139 self->c_it->init();
140 Py_RETURN_NONE;
141}
142
144 /* Wrap. */
145 ChainingIterator_traverse_doc,
146 ".. method:: traverse(it)\n"
147 "\n"
148 " This method iterates over the potential next ViewEdges and returns\n"
149 " the one that will be followed next. Returns the next ViewEdge to\n"
150 " follow or None when the end of the chain is reached.\n"
151 "\n"
152 " :param it: The iterator over the ViewEdges adjacent to the end vertex\n"
153 " of the current ViewEdge. The adjacency iterator reflects the\n"
154 " restriction rules by only iterating over the valid ViewEdges.\n"
155 " :type it: :class:`AdjacencyIterator`\n"
156 " :return: Returns the next ViewEdge to follow, or None if chaining ends.\n"
157 " :rtype: :class:`ViewEdge` | None\n");
159 PyObject *args,
160 PyObject *kwds)
161{
162 static const char *kwlist[] = {"it", nullptr};
163 PyObject *py_a_it;
164
165 if (typeid(*(self->c_it)) == typeid(ChainingIterator)) {
166 PyErr_SetString(PyExc_TypeError, "traverse() method not properly overridden");
167 return nullptr;
168 }
169 if (!PyArg_ParseTupleAndKeywords(args,
170 kwds,
171 "O!" /* `it` */
172 ":traverse",
173 (char **)kwlist,
175 &py_a_it))
176 {
177 return nullptr;
178 }
179 if (((BPy_AdjacencyIterator *)py_a_it)->a_it) {
180 self->c_it->traverse(*(((BPy_AdjacencyIterator *)py_a_it)->a_it));
181 }
182 Py_RETURN_NONE;
183}
184
185#ifdef __GNUC__
186# ifdef __clang__
187# pragma clang diagnostic push
188# pragma clang diagnostic ignored "-Wcast-function-type"
189# else
190# pragma GCC diagnostic push
191# pragma GCC diagnostic ignored "-Wcast-function-type"
192# endif
193#endif
194
195static PyMethodDef BPy_ChainingIterator_methods[] = {
196 {"init", (PyCFunction)ChainingIterator_init, METH_NOARGS, ChainingIterator_init_doc},
197 {"traverse",
198 (PyCFunction)ChainingIterator_traverse,
199 METH_VARARGS | METH_KEYWORDS,
200 ChainingIterator_traverse_doc},
201 {nullptr, nullptr, 0, nullptr},
202};
203
204#ifdef __GNUC__
205# ifdef __clang__
206# pragma clang diagnostic pop
207# else
208# pragma GCC diagnostic pop
209# endif
210#endif
211
212/*----------------------ChainingIterator get/setters ----------------------------*/
213
215 /* Wrap. */
216 ChainingIterator_object_doc,
217 "The ViewEdge object currently pointed by this iterator.\n"
218 "\n"
219 ":type: :class:`ViewEdge`\n");
220static PyObject *ChainingIterator_object_get(BPy_ChainingIterator *self, void * /*closure*/)
221{
222 if (self->c_it->isEnd()) {
223 PyErr_SetString(PyExc_RuntimeError, "iteration has stopped");
224 return nullptr;
225 }
226 ViewEdge *ve = self->c_it->operator*();
227 if (ve) {
228 return BPy_ViewEdge_from_ViewEdge(*ve);
229 }
230
231 Py_RETURN_NONE;
232}
233
235 /* Wrap. */
236 ChainingIterator_next_vertex_doc,
237 "The ViewVertex that is the next crossing.\n"
238 "\n"
239 ":type: :class:`ViewVertex`\n");
240static PyObject *ChainingIterator_next_vertex_get(BPy_ChainingIterator *self, void * /*closure*/)
241{
242 ViewVertex *v = self->c_it->getVertex();
243 if (v) {
245 }
246
247 Py_RETURN_NONE;
248}
249
251 /* Wrap. */
252 ChainingIterator_is_incrementing_doc,
253 "True if the current iteration is an incrementation.\n"
254 "\n"
255 ":type: bool\n");
257 void * /*closure*/)
258{
259 return PyBool_from_bool(self->c_it->isIncrementing());
260}
261
262static PyGetSetDef BPy_ChainingIterator_getseters[] = {
263 {"object",
265 (setter) nullptr,
266 ChainingIterator_object_doc,
267 nullptr},
268 {"next_vertex",
270 (setter) nullptr,
271 ChainingIterator_next_vertex_doc,
272 nullptr},
273 {"is_incrementing",
275 (setter) nullptr,
276 ChainingIterator_is_incrementing_doc,
277 nullptr},
278 {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
279};
280
281/*-----------------------BPy_ChainingIterator type definition ------------------------------*/
282
283PyTypeObject ChainingIterator_Type = {
284 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
285 /*tp_name*/ "ChainingIterator",
286 /*tp_basicsize*/ sizeof(BPy_ChainingIterator),
287 /*tp_itemsize*/ 0,
288 /*tp_dealloc*/ nullptr,
289 /*tp_vectorcall_offset*/ 0,
290 /*tp_getattr*/ nullptr,
291 /*tp_setattr*/ nullptr,
292 /*tp_as_async*/ nullptr,
293 /*tp_repr*/ nullptr,
294 /*tp_as_number*/ nullptr,
295 /*tp_as_sequence*/ nullptr,
296 /*tp_as_mapping*/ nullptr,
297 /*tp_hash*/ nullptr,
298 /*tp_call*/ nullptr,
299 /*tp_str*/ nullptr,
300 /*tp_getattro*/ nullptr,
301 /*tp_setattro*/ nullptr,
302 /*tp_as_buffer*/ nullptr,
303 /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
304 /*tp_doc*/ ChainingIterator_doc,
305 /*tp_traverse*/ nullptr,
306 /*tp_clear*/ nullptr,
307 /*tp_richcompare*/ nullptr,
308 /*tp_weaklistoffset*/ 0,
309 /*tp_iter*/ nullptr,
310 /*tp_iternext*/ nullptr,
311 /*tp_methods*/ BPy_ChainingIterator_methods,
312 /*tp_members*/ nullptr,
313 /*tp_getset*/ BPy_ChainingIterator_getseters,
314 /*tp_base*/ &ViewEdgeIterator_Type,
315 /*tp_dict*/ nullptr,
316 /*tp_descr_get*/ nullptr,
317 /*tp_descr_set*/ nullptr,
318 /*tp_dictoffset*/ 0,
319 /*tp_init*/ (initproc)ChainingIterator___init__,
320 /*tp_alloc*/ nullptr,
321 /*tp_new*/ nullptr,
322};
323
PyTypeObject AdjacencyIterator_Type
static int check_begin(PyObject *obj, void *v)
static int ChainingIterator___init__(BPy_ChainingIterator *self, PyObject *args, PyObject *kwds)
static PyObject * ChainingIterator_init(BPy_ChainingIterator *self)
static PyGetSetDef BPy_ChainingIterator_getseters[]
static PyObject * ChainingIterator_next_vertex_get(BPy_ChainingIterator *self, void *)
static PyObject * ChainingIterator_traverse(BPy_ChainingIterator *self, PyObject *args, PyObject *kwds)
static PyMethodDef BPy_ChainingIterator_methods[]
static PyObject * ChainingIterator_is_incrementing_get(BPy_ChainingIterator *self, void *)
static PyObject * ChainingIterator_object_get(BPy_ChainingIterator *self, void *)
PyTypeObject ChainingIterator_Type
bool bool_from_PyBool(PyObject *b)
PyObject * Any_BPy_ViewVertex_from_ViewVertex(ViewVertex &vv)
PyObject * BPy_ViewEdge_from_ViewEdge(ViewEdge &ve)
PyObject * PyBool_from_bool(bool b)
PyDoc_STRVAR(FalseBP1D___doc__, "Class hierarchy: :class:`freestyle.types.BinaryPredicate1D` > :class:`FalseBP1D`\n" "\n" ".. method:: __call__(inter1, inter2)\n" "\n" " Always returns false.\n" "\n" " :param inter1: The first Interface1D object.\n" " :type inter1: :class:`freestyle.types.Interface1D`\n" " :param inter2: The second Interface1D object.\n" " :type inter2: :class:`freestyle.types.Interface1D`\n" " :return: False.\n" " :rtype: bool\n")
PyTypeObject ViewEdgeIterator_Type
#define BPy_ViewEdge_Check(v)
ChainingIterator(bool iRestrictToSelection=true, bool iRestrictToUnvisited=true, ViewEdge *begin=nullptr, bool orientation=true)
virtual int traverse(const AdjacencyIterator &it)
inherits from class Rep
Definition AppCanvas.cpp:20
Freestyle::ChainingIterator * c_it
BPy_ViewEdgeIterator py_ve_it
PyObject_HEAD Freestyle::Iterator * it
Freestyle::ViewEdgeInternal::ViewEdgeIterator * ve_it