Blender  V3.3
BPy_ChainingIterator.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include "BPy_ChainingIterator.h"
8 
9 #include "../BPy_Convert.h"
10 #include "../Interface0D/BPy_ViewVertex.h"
11 #include "../Interface1D/BPy_ViewEdge.h"
12 #include "BPy_AdjacencyIterator.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 using namespace Freestyle;
19 
21 
22 //------------------------INSTANCE METHODS ----------------------------------
23 
25  ChainingIterator_doc,
26  "Class hierarchy: :class:`Iterator` > :class:`ViewEdgeIterator` > :class:`ChainingIterator`\n"
27  "\n"
28  "Base class for chaining iterators. This class is designed to be\n"
29  "overloaded in order to describe chaining rules. It makes the\n"
30  "description of chaining rules easier. The two main methods that need\n"
31  "to overloaded are traverse() and init(). traverse() tells which\n"
32  ":class:`ViewEdge` to follow, among the adjacent ones. If you specify\n"
33  "restriction rules (such as \"Chain only ViewEdges of the selection\"),\n"
34  "they will be included in the adjacency iterator (i.e, the adjacent\n"
35  "iterator will only stop on \"valid\" edges).\n"
36  "\n"
37  ".. method:: __init__(restrict_to_selection=True, restrict_to_unvisited=True,"
38  " begin=None, orientation=True)\n"
39  " __init__(brother)\n"
40  "\n"
41  " Builds a Chaining Iterator from the first ViewEdge used for\n"
42  " iteration and its orientation or by using the copy constructor.\n"
43  "\n"
44  " :arg restrict_to_selection: Indicates whether to force the chaining\n"
45  " to stay within the set of selected ViewEdges or not.\n"
46  " :type restrict_to_selection: bool\n"
47  " :arg restrict_to_unvisited: Indicates whether a ViewEdge that has\n"
48  " already been chained must be ignored ot not.\n"
49  " :type restrict_to_unvisited: bool\n"
50  " :arg begin: The ViewEdge from which to start the chain.\n"
51  " :type begin: :class:`ViewEdge` or None\n"
52  " :arg orientation: The direction to follow to explore the graph. If\n"
53  " true, the direction indicated by the first ViewEdge is used.\n"
54  " :type orientation: bool\n"
55  " :arg brother: \n"
56  " :type brother: ChainingIterator");
57 
58 static int check_begin(PyObject *obj, void *v)
59 {
60  if (obj != nullptr && obj != Py_None && !BPy_ViewEdge_Check(obj)) {
61  return 0;
62  }
63  *((PyObject **)v) = obj;
64  return 1;
65 }
66 
67 static int ChainingIterator___init__(BPy_ChainingIterator *self, PyObject *args, PyObject *kwds)
68 {
69  static const char *kwlist_1[] = {"brother", nullptr};
70  static const char *kwlist_2[] = {
71  "restrict_to_selection", "restrict_to_unvisited", "begin", "orientation", nullptr};
72  PyObject *obj1 = nullptr, *obj2 = nullptr, *obj3 = nullptr, *obj4 = nullptr;
73 
74  if (PyArg_ParseTupleAndKeywords(
75  args, kwds, "O!", (char **)kwlist_1, &ChainingIterator_Type, &obj1)) {
76  self->c_it = new ChainingIterator(*(((BPy_ChainingIterator *)obj1)->c_it));
77  }
78  else if ((void)PyErr_Clear(),
79  (void)(obj1 = obj2 = obj3 = obj4 = nullptr),
80  PyArg_ParseTupleAndKeywords(args,
81  kwds,
82  "|O!O!O&O!",
83  (char **)kwlist_2,
84  &PyBool_Type,
85  &obj1,
86  &PyBool_Type,
87  &obj2,
89  &obj3,
90  &PyBool_Type,
91  &obj4)) {
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 
111 PyDoc_STRVAR(ChainingIterator_init_doc,
112  ".. method:: init()\n"
113  "\n"
114  " Initializes the iterator context. This method is called each\n"
115  " time a new chain is started. It can be used to reset some\n"
116  " history information that you might want to keep.");
117 
119 {
120  if (typeid(*(self->c_it)) == typeid(ChainingIterator)) {
121  PyErr_SetString(PyExc_TypeError, "init() method not properly overridden");
122  return nullptr;
123  }
124  self->c_it->init();
125  Py_RETURN_NONE;
126 }
127 
128 PyDoc_STRVAR(ChainingIterator_traverse_doc,
129  ".. method:: traverse(it)\n"
130  "\n"
131  " This method iterates over the potential next ViewEdges and returns\n"
132  " the one that will be followed next. Returns the next ViewEdge to\n"
133  " follow or None when the end of the chain is reached.\n"
134  "\n"
135  " :arg it: The iterator over the ViewEdges adjacent to the end vertex\n"
136  " of the current ViewEdge. The adjacency iterator reflects the\n"
137  " restriction rules by only iterating over the valid ViewEdges.\n"
138  " :type it: :class:`AdjacencyIterator`\n"
139  " :return: Returns the next ViewEdge to follow, or None if chaining ends.\n"
140  " :rtype: :class:`ViewEdge` or None");
141 
143  PyObject *args,
144  PyObject *kwds)
145 {
146  static const char *kwlist[] = {"it", nullptr};
147  PyObject *py_a_it;
148 
149  if (typeid(*(self->c_it)) == typeid(ChainingIterator)) {
150  PyErr_SetString(PyExc_TypeError, "traverse() method not properly overridden");
151  return nullptr;
152  }
153  if (!PyArg_ParseTupleAndKeywords(
154  args, kwds, "O!", (char **)kwlist, &AdjacencyIterator_Type, &py_a_it)) {
155  return nullptr;
156  }
157  if (((BPy_AdjacencyIterator *)py_a_it)->a_it) {
158  self->c_it->traverse(*(((BPy_AdjacencyIterator *)py_a_it)->a_it));
159  }
160  Py_RETURN_NONE;
161 }
162 
163 static PyMethodDef BPy_ChainingIterator_methods[] = {
164  {"init", (PyCFunction)ChainingIterator_init, METH_NOARGS, ChainingIterator_init_doc},
165  {"traverse",
166  (PyCFunction)ChainingIterator_traverse,
167  METH_VARARGS | METH_KEYWORDS,
168  ChainingIterator_traverse_doc},
169  {nullptr, nullptr, 0, nullptr},
170 };
171 
172 /*----------------------ChainingIterator get/setters ----------------------------*/
173 
174 PyDoc_STRVAR(ChainingIterator_object_doc,
175  "The ViewEdge object currently pointed by this iterator.\n"
176  "\n"
177  ":type: :class:`ViewEdge`");
178 
179 static PyObject *ChainingIterator_object_get(BPy_ChainingIterator *self, void *UNUSED(closure))
180 {
181  if (self->c_it->isEnd()) {
182  PyErr_SetString(PyExc_RuntimeError, "iteration has stopped");
183  return nullptr;
184  }
185  ViewEdge *ve = self->c_it->operator*();
186  if (ve) {
187  return BPy_ViewEdge_from_ViewEdge(*ve);
188  }
189 
190  Py_RETURN_NONE;
191 }
192 
193 PyDoc_STRVAR(ChainingIterator_next_vertex_doc,
194  "The ViewVertex that is the next crossing.\n"
195  "\n"
196  ":type: :class:`ViewVertex`");
197 
199  void *UNUSED(closure))
200 {
201  ViewVertex *v = self->c_it->getVertex();
202  if (v) {
204  }
205 
206  Py_RETURN_NONE;
207 }
208 
209 PyDoc_STRVAR(ChainingIterator_is_incrementing_doc,
210  "True if the current iteration is an incrementation.\n"
211  "\n"
212  ":type: bool");
213 
215  void *UNUSED(closure))
216 {
217  return PyBool_from_bool(self->c_it->isIncrementing());
218 }
219 
220 static PyGetSetDef BPy_ChainingIterator_getseters[] = {
221  {"object",
223  (setter) nullptr,
224  ChainingIterator_object_doc,
225  nullptr},
226  {"next_vertex",
228  (setter) nullptr,
229  ChainingIterator_next_vertex_doc,
230  nullptr},
231  {"is_incrementing",
233  (setter) nullptr,
234  ChainingIterator_is_incrementing_doc,
235  nullptr},
236  {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
237 };
238 
239 /*-----------------------BPy_ChainingIterator type definition ------------------------------*/
240 
241 PyTypeObject ChainingIterator_Type = {
242  PyVarObject_HEAD_INIT(nullptr, 0) "ChainingIterator", /* tp_name */
243  sizeof(BPy_ChainingIterator), /* tp_basicsize */
244  0, /* tp_itemsize */
245  nullptr, /* tp_dealloc */
246  0, /* tp_vectorcall_offset */
247  nullptr, /* tp_getattr */
248  nullptr, /* tp_setattr */
249  nullptr, /* tp_reserved */
250  nullptr, /* tp_repr */
251  nullptr, /* tp_as_number */
252  nullptr, /* tp_as_sequence */
253  nullptr, /* tp_as_mapping */
254  nullptr, /* tp_hash */
255  nullptr, /* tp_call */
256  nullptr, /* tp_str */
257  nullptr, /* tp_getattro */
258  nullptr, /* tp_setattro */
259  nullptr, /* tp_as_buffer */
260  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
261  ChainingIterator_doc, /* tp_doc */
262  nullptr, /* tp_traverse */
263  nullptr, /* tp_clear */
264  nullptr, /* tp_richcompare */
265  0, /* tp_weaklistoffset */
266  nullptr, /* tp_iter */
267  nullptr, /* tp_iternext */
268  BPy_ChainingIterator_methods, /* tp_methods */
269  nullptr, /* tp_members */
270  BPy_ChainingIterator_getseters, /* tp_getset */
271  &ViewEdgeIterator_Type, /* tp_base */
272  nullptr, /* tp_dict */
273  nullptr, /* tp_descr_get */
274  nullptr, /* tp_descr_set */
275  0, /* tp_dictoffset */
276  (initproc)ChainingIterator___init__, /* tp_init */
277  nullptr, /* tp_alloc */
278  nullptr, /* tp_new */
279 };
280 
282 
283 #ifdef __cplusplus
284 }
285 #endif
#define UNUSED(x)
PyTypeObject AdjacencyIterator_Type
static PyObject * ChainingIterator_next_vertex_get(BPy_ChainingIterator *self, void *UNUSED(closure))
static int check_begin(PyObject *obj, void *v)
static PyObject * ChainingIterator_object_get(BPy_ChainingIterator *self, void *UNUSED(closure))
static int ChainingIterator___init__(BPy_ChainingIterator *self, PyObject *args, PyObject *kwds)
static PyGetSetDef BPy_ChainingIterator_getseters[]
static PyMethodDef BPy_ChainingIterator_methods[]
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` or 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")
static PyObject * ChainingIterator_is_incrementing_get(BPy_ChainingIterator *self, void *UNUSED(closure))
static PyObject * ChainingIterator_init(BPy_ChainingIterator *self)
PyTypeObject ChainingIterator_Type
static PyObject * ChainingIterator_traverse(BPy_ChainingIterator *self, PyObject *args, PyObject *kwds)
PyObject * PyBool_from_bool(bool b)
Definition: BPy_Convert.cpp:59
bool bool_from_PyBool(PyObject *b)
PyObject * BPy_ViewEdge_from_ViewEdge(ViewEdge &ve)
PyObject * Any_BPy_ViewVertex_from_ViewVertex(ViewVertex &vv)
PyTypeObject ViewEdgeIterator_Type
#define BPy_ViewEdge_Check(v)
Definition: BPy_ViewEdge.h:21
ATTR_WARN_UNUSED_RESULT const BMVert * v
PyObject * self
Definition: bpy_driver.c:165
inherits from class Rep
Definition: AppCanvas.cpp:18