Blender  V2.93
BPy_AdjacencyIterator.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_AdjacencyIterator.h"
22 
23 #include "../BPy_Convert.h"
24 #include "../Interface0D/BPy_ViewVertex.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 using namespace Freestyle;
31 
33 
34 //------------------------INSTANCE METHODS ----------------------------------
35 
37  AdjacencyIterator_doc,
38  "Class hierarchy: :class:`Iterator` > :class:`AdjacencyIterator`\n"
39  "\n"
40  "Class for representing adjacency iterators used in the chaining\n"
41  "process. An AdjacencyIterator is created in the increment() and\n"
42  "decrement() methods of a :class:`ChainingIterator` and passed to the\n"
43  "traverse() method of the ChainingIterator.\n"
44  "\n"
45  ".. method:: __init__()\n"
46  " __init__(brother)\n"
47  " __init__(vertex, restrict_to_selection=True, restrict_to_unvisited=True)\n"
48  "\n"
49  " Builds an :class:`AdjacencyIterator` using the default constructor,\n"
50  " copy constructor or the overloaded constructor.\n"
51  "\n"
52  " :arg brother: An AdjacencyIterator object.\n"
53  " :type brother: :class:`AdjacencyIterator`\n"
54  " :arg vertex: The vertex which is the next crossing.\n"
55  " :type vertex: :class:`ViewVertex`\n"
56  " :arg restrict_to_selection: Indicates whether to force the chaining\n"
57  " to stay within the set of selected ViewEdges or not.\n"
58  " :type restrict_to_selection: bool\n"
59  " :arg restrict_to_unvisited: Indicates whether a ViewEdge that has\n"
60  " already been chained must be ignored ot not.\n"
61  " :type restrict_to_unvisited: bool");
62 
63 static int AdjacencyIterator_init(BPy_AdjacencyIterator *self, PyObject *args, PyObject *kwds)
64 {
65  static const char *kwlist_1[] = {"brother", nullptr};
66  static const char *kwlist_2[] = {
67  "vertex", "restrict_to_selection", "restrict_to_unvisited", nullptr};
68  PyObject *obj1 = nullptr, *obj2 = nullptr, *obj3 = nullptr;
69 
70  if (PyArg_ParseTupleAndKeywords(
71  args, kwds, "|O!", (char **)kwlist_1, &AdjacencyIterator_Type, &obj1)) {
72  if (!obj1) {
73  self->a_it = new AdjacencyIterator();
74  self->at_start = true;
75  }
76  else {
77  self->a_it = new AdjacencyIterator(*(((BPy_AdjacencyIterator *)obj1)->a_it));
78  self->at_start = ((BPy_AdjacencyIterator *)obj1)->at_start;
79  }
80  }
81  else if ((void)PyErr_Clear(),
82  (void)(obj2 = obj3 = nullptr),
83  PyArg_ParseTupleAndKeywords(args,
84  kwds,
85  "O!|O!O!",
86  (char **)kwlist_2,
88  &obj1,
89  &PyBool_Type,
90  &obj2,
91  &PyBool_Type,
92  &obj3)) {
93  bool restrictToSelection = (!obj2) ? true : bool_from_PyBool(obj2);
94  bool restrictToUnvisited = (!obj3) ? true : bool_from_PyBool(obj3);
95  self->a_it = new AdjacencyIterator(
96  ((BPy_ViewVertex *)obj1)->vv, restrictToSelection, restrictToUnvisited);
97  self->at_start = ((BPy_AdjacencyIterator *)obj1)->at_start;
98  }
99  else {
100  PyErr_SetString(PyExc_TypeError, "invalid argument(s)");
101  return -1;
102  }
103  self->py_it.it = self->a_it;
104  return 0;
105 }
106 
108 {
109  Py_INCREF(self);
110  self->at_start = true;
111  return (PyObject *)self;
112 }
113 
115 {
116  if (self->a_it->isEnd()) {
117  PyErr_SetNone(PyExc_StopIteration);
118  return nullptr;
119  }
120  if (self->at_start) {
121  self->at_start = false;
122  }
123  else {
124  self->a_it->increment();
125  if (self->a_it->isEnd()) {
126  PyErr_SetNone(PyExc_StopIteration);
127  return nullptr;
128  }
129  }
130  ViewEdge *ve = self->a_it->operator->();
131  return BPy_ViewEdge_from_ViewEdge(*ve);
132 }
133 
134 /*----------------------AdjacencyIterator get/setters ----------------------------*/
135 
136 PyDoc_STRVAR(AdjacencyIterator_object_doc,
137  "The ViewEdge object currently pointed to by this iterator.\n"
138  "\n"
139  ":type: :class:`ViewEdge`");
140 
141 static PyObject *AdjacencyIterator_object_get(BPy_AdjacencyIterator *self, void *UNUSED(closure))
142 {
143  if (self->a_it->isEnd()) {
144  PyErr_SetString(PyExc_RuntimeError, "iteration has stopped");
145  return nullptr;
146  }
147  ViewEdge *ve = self->a_it->operator*();
148  if (ve) {
149  return BPy_ViewEdge_from_ViewEdge(*ve);
150  }
151  Py_RETURN_NONE;
152 }
153 
154 PyDoc_STRVAR(AdjacencyIterator_is_incoming_doc,
155  "True if the current ViewEdge is coming towards the iteration vertex, and\n"
156  "False otherwise.\n"
157  "\n"
158  ":type: bool");
159 
161  void *UNUSED(closure))
162 {
163  if (self->a_it->isEnd()) {
164  PyErr_SetString(PyExc_RuntimeError, "iteration has stopped");
165  return nullptr;
166  }
167  return PyBool_from_bool(self->a_it->isIncoming());
168 }
169 
170 static PyGetSetDef BPy_AdjacencyIterator_getseters[] = {
171  {"is_incoming",
173  (setter) nullptr,
174  AdjacencyIterator_is_incoming_doc,
175  nullptr},
176  {"object",
178  (setter) nullptr,
179  AdjacencyIterator_object_doc,
180  nullptr},
181  {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
182 };
183 
184 /*-----------------------BPy_AdjacencyIterator type definition ------------------------------*/
185 
186 PyTypeObject AdjacencyIterator_Type = {
187  PyVarObject_HEAD_INIT(nullptr, 0) "AdjacencyIterator", /* tp_name */
188  sizeof(BPy_AdjacencyIterator), /* tp_basicsize */
189  0, /* tp_itemsize */
190  nullptr, /* tp_dealloc */
191  0, /* tp_vectorcall_offset */
192  nullptr, /* tp_getattr */
193  nullptr, /* tp_setattr */
194  nullptr, /* tp_reserved */
195  nullptr, /* tp_repr */
196  nullptr, /* tp_as_number */
197  nullptr, /* tp_as_sequence */
198  nullptr, /* tp_as_mapping */
199  nullptr, /* tp_hash */
200  nullptr, /* tp_call */
201  nullptr, /* tp_str */
202  nullptr, /* tp_getattro */
203  nullptr, /* tp_setattro */
204  nullptr, /* tp_as_buffer */
205  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
206  AdjacencyIterator_doc, /* tp_doc */
207  nullptr, /* tp_traverse */
208  nullptr, /* tp_clear */
209  nullptr, /* tp_richcompare */
210  0, /* tp_weaklistoffset */
211  (getiterfunc)AdjacencyIterator_iter, /* tp_iter */
212  (iternextfunc)AdjacencyIterator_iternext, /* tp_iternext */
213  nullptr, /* tp_methods */
214  nullptr, /* tp_members */
215  BPy_AdjacencyIterator_getseters, /* tp_getset */
216  &Iterator_Type, /* tp_base */
217  nullptr, /* tp_dict */
218  nullptr, /* tp_descr_get */
219  nullptr, /* tp_descr_set */
220  0, /* tp_dictoffset */
221  (initproc)AdjacencyIterator_init, /* tp_init */
222  nullptr, /* tp_alloc */
223  nullptr, /* tp_new */
224 };
225 
227 
228 #ifdef __cplusplus
229 }
230 #endif
#define UNUSED(x)
static PyObject * AdjacencyIterator_iter(BPy_AdjacencyIterator *self)
static PyObject * AdjacencyIterator_is_incoming_get(BPy_AdjacencyIterator *self, void *UNUSED(closure))
static PyObject * AdjacencyIterator_iternext(BPy_AdjacencyIterator *self)
static PyObject * AdjacencyIterator_object_get(BPy_AdjacencyIterator *self, void *UNUSED(closure))
PyTypeObject AdjacencyIterator_Type
static int AdjacencyIterator_init(BPy_AdjacencyIterator *self, PyObject *args, PyObject *kwds)
PyDoc_STRVAR(AdjacencyIterator_doc, "Class hierarchy: :class:`Iterator` > :class:`AdjacencyIterator`\n" "\n" "Class for representing adjacency iterators used in the chaining\n" "process. An AdjacencyIterator is created in the increment() and\n" "decrement() methods of a :class:`ChainingIterator` and passed to the\n" "traverse() method of the ChainingIterator.\n" "\n" ".. method:: __init__()\n" " __init__(brother)\n" " __init__(vertex, restrict_to_selection=True, restrict_to_unvisited=True)\n" "\n" " Builds an :class:`AdjacencyIterator` using the default constructor,\n" " copy constructor or the overloaded constructor.\n" "\n" " :arg brother: An AdjacencyIterator object.\n" " :type brother: :class:`AdjacencyIterator`\n" " :arg vertex: The vertex which is the next crossing.\n" " :type vertex: :class:`ViewVertex`\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")
static PyGetSetDef BPy_AdjacencyIterator_getseters[]
PyObject * PyBool_from_bool(bool b)
Definition: BPy_Convert.cpp:73
bool bool_from_PyBool(PyObject *b)
PyObject * BPy_ViewEdge_from_ViewEdge(ViewEdge &ve)
PyTypeObject Iterator_Type
PyTypeObject ViewVertex_Type
PyObject * self
Definition: bpy_driver.c:185
inherits from class Rep
Definition: AppCanvas.cpp:32