cleanup: style
[blender-staging.git] / source / blender / freestyle / intern / python / Iterator / BPy_AdjacencyIterator.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file source/blender/freestyle/intern/python/Iterator/BPy_AdjacencyIterator.cpp
22  *  \ingroup freestyle
23  */
24
25 #include "BPy_AdjacencyIterator.h"
26
27 #include "../BPy_Convert.h"
28 #include "../Interface0D/BPy_ViewVertex.h"
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 ///////////////////////////////////////////////////////////////////////////////////////////
35
36 //------------------------INSTANCE METHODS ----------------------------------
37
38 PyDoc_STRVAR(AdjacencyIterator_doc,
39 "Class hierarchy: :class:`Iterator` > :class:`AdjacencyIterator`\n"
40 "\n"
41 "Class for representing adjacency iterators used in the chaining\n"
42 "process.  An AdjacencyIterator is created in the increment() and\n"
43 "decrement() methods of a :class:`ChainingIterator` and passed to the\n"
44 "traverse() method of the ChainingIterator.\n"
45 "\n"
46 ".. method:: __init__()\n"
47 "\n"
48 "   Default constructor.\n"
49 "\n"
50 ".. method:: __init__(brother)\n"
51 "\n"
52 "   Copy constructor.\n"
53 "\n"
54 "   :arg brother: An AdjacencyIterator object.\n"
55 "   :type brother: :class:`AdjacencyIterator`\n"
56 "\n"
57 ".. method:: __init__(vertex, restrict_to_selection=True, restrict_to_unvisited=True)\n"
58 "\n"
59 "   Builds a AdjacencyIterator object.\n"
60 "\n"
61 "   :arg vertex: The vertex which is the next crossing.\n"
62 "   :type vertex: :class:`ViewVertex`\n"
63 "   :arg restrict_to_selection: Indicates whether to force the chaining\n"
64 "      to stay within the set of selected ViewEdges or not.\n"
65 "   :type restrict_to_selection: bool\n"
66 "   :arg restrict_to_unvisited: Indicates whether a ViewEdge that has\n"
67 "      already been chained must be ignored ot not.\n"
68 "   :type restrict_to_unvisited: bool");
69
70 static int AdjacencyIterator_init(BPy_AdjacencyIterator *self, PyObject *args, PyObject *kwds)
71 {
72         static const char *kwlist_1[] = {"brother", NULL};
73         static const char *kwlist_2[] = {"vertex", "restrict_to_selection", "restrict_to_unvisited", NULL};
74         PyObject *obj1 = 0, *obj2 = 0, *obj3 = 0;
75
76         if (PyArg_ParseTupleAndKeywords(args, kwds, "|O!", (char **)kwlist_1, &AdjacencyIterator_Type, &obj1)) {
77                 if (!obj1) {
78                         self->a_it = new AdjacencyIterator();
79                         self->at_start = true;
80                 }
81                 else {
82                         self->a_it = new AdjacencyIterator(*(((BPy_AdjacencyIterator *)obj1)->a_it));
83                         self->at_start = ((BPy_AdjacencyIterator *)obj1)->at_start;
84                 }
85         }
86         else if (PyErr_Clear(), (obj2 = obj3 = 0),
87                  PyArg_ParseTupleAndKeywords(args, kwds, "O!|O!O!", (char **)kwlist_2,
88                                              &ViewVertex_Type, &obj1, &PyBool_Type, &obj2, &PyBool_Type, &obj3))
89         {
90                 bool restrictToSelection = (!obj2) ? true : bool_from_PyBool(obj2);
91                 bool restrictToUnvisited = (!obj3) ? true : bool_from_PyBool(obj3);
92                 self->a_it = new AdjacencyIterator(((BPy_ViewVertex *)obj1)->vv, restrictToSelection, restrictToUnvisited);
93                 self->at_start = ((BPy_AdjacencyIterator *)obj1)->at_start;
94         }
95         else {
96                 PyErr_SetString(PyExc_TypeError, "invalid argument(s)");
97                 return -1;
98         }
99         self->py_it.it = self->a_it;
100         return 0;
101 }
102
103 static PyObject *AdjacencyIterator_iter(BPy_AdjacencyIterator *self)
104 {
105         Py_INCREF(self);
106         self->at_start = true;
107         return (PyObject *) self;
108 }
109
110 static PyObject *AdjacencyIterator_iternext(BPy_AdjacencyIterator *self)
111 {
112         if (self->a_it->isEnd()) {
113                 PyErr_SetNone(PyExc_StopIteration);
114                 return NULL;
115         }
116         if (self->at_start)
117                 self->at_start = false;
118         else {
119                 self->a_it->increment();
120                 if (self->a_it->isEnd()) {
121                         PyErr_SetNone(PyExc_StopIteration);
122                         return NULL;
123                 }
124         }
125         ViewEdge *ve = self->a_it->operator->();
126         return BPy_ViewEdge_from_ViewEdge(*ve);
127 }
128
129 /*----------------------AdjacencyIterator get/setters ----------------------------*/
130
131 PyDoc_STRVAR(AdjacencyIterator_object_doc,
132 "The ViewEdge object currently pointed to by this iterator.\n"
133 "\n"
134 ":type: :class:`ViewEdge`");
135
136 static PyObject *AdjacencyIterator_object_get(BPy_AdjacencyIterator *self, void *UNUSED(closure))
137 {
138         if (self->a_it->isEnd()) {
139                 PyErr_SetString(PyExc_RuntimeError, "iteration has stopped");
140                 return NULL;
141         }
142         ViewEdge *ve = self->a_it->operator*();
143         if (ve)
144                 return BPy_ViewEdge_from_ViewEdge(*ve);
145         Py_RETURN_NONE;
146 }
147
148 PyDoc_STRVAR(AdjacencyIterator_is_incoming_doc,
149 "True if the current ViewEdge is coming towards the iteration vertex, and\n"
150 "False otherwise.\n"
151 "\n"
152 ":type: bool");
153
154 static PyObject *AdjacencyIterator_is_incoming_get(BPy_AdjacencyIterator *self, void *UNUSED(closure))
155 {
156         if (self->a_it->isEnd()) {
157                 PyErr_SetString(PyExc_RuntimeError, "iteration has stopped");
158                 return NULL;
159         }
160         return PyBool_from_bool(self->a_it->isIncoming());
161 }
162
163 static PyGetSetDef BPy_AdjacencyIterator_getseters[] = {
164         {(char *)"is_incoming", (getter)AdjacencyIterator_is_incoming_get, (setter)NULL,
165                                 (char *)AdjacencyIterator_is_incoming_doc, NULL},
166         {(char *)"object", (getter)AdjacencyIterator_object_get, (setter)NULL, (char *)AdjacencyIterator_object_doc, NULL},
167         {NULL, NULL, NULL, NULL, NULL}  /* Sentinel */
168 };
169
170 /*-----------------------BPy_AdjacencyIterator type definition ------------------------------*/
171
172 PyTypeObject AdjacencyIterator_Type = {
173         PyVarObject_HEAD_INIT(NULL, 0)
174         "AdjacencyIterator",            /* tp_name */
175         sizeof(BPy_AdjacencyIterator),  /* tp_basicsize */
176         0,                              /* tp_itemsize */
177         0,                              /* tp_dealloc */
178         0,                              /* tp_print */
179         0,                              /* tp_getattr */
180         0,                              /* tp_setattr */
181         0,                              /* tp_reserved */
182         0,                              /* tp_repr */
183         0,                              /* tp_as_number */
184         0,                              /* tp_as_sequence */
185         0,                              /* tp_as_mapping */
186         0,                              /* tp_hash  */
187         0,                              /* tp_call */
188         0,                              /* tp_str */
189         0,                              /* tp_getattro */
190         0,                              /* tp_setattro */
191         0,                              /* tp_as_buffer */
192         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
193         AdjacencyIterator_doc,          /* tp_doc */
194         0,                              /* tp_traverse */
195         0,                              /* tp_clear */
196         0,                              /* tp_richcompare */
197         0,                              /* tp_weaklistoffset */
198         (getiterfunc)AdjacencyIterator_iter, /* tp_iter */
199         (iternextfunc)AdjacencyIterator_iternext, /* tp_iternext */
200         0,                              /* tp_methods */
201         0,                              /* tp_members */
202         BPy_AdjacencyIterator_getseters, /* tp_getset */
203         &Iterator_Type,                 /* tp_base */
204         0,                              /* tp_dict */
205         0,                              /* tp_descr_get */
206         0,                              /* tp_descr_set */
207         0,                              /* tp_dictoffset */
208         (initproc)AdjacencyIterator_init, /* tp_init */
209         0,                              /* tp_alloc */
210         0,                              /* tp_new */
211 };
212
213 ///////////////////////////////////////////////////////////////////////////////////////////
214
215 #ifdef __cplusplus
216 }
217 #endif