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