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