Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BPy_StrokeVertexIterator.cpp « Iterator « python « intern « freestyle « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: abbaae19b9e105277782da2eb75d6f5d0fe4c612 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* SPDX-License-Identifier: GPL-2.0-or-later */

/** \file
 * \ingroup freestyle
 */

#include "BPy_StrokeVertexIterator.h"

#include "../BPy_Convert.h"
#include "../Interface1D/BPy_Stroke.h"
#include "BPy_Interface0DIterator.h"

#ifdef __cplusplus
extern "C" {
#endif

using namespace Freestyle;

///////////////////////////////////////////////////////////////////////////////////////////

//------------------------INSTANCE METHODS ----------------------------------

PyDoc_STRVAR(StrokeVertexIterator_doc,
             "Class hierarchy: :class:`Iterator` > :class:`StrokeVertexIterator`\n"
             "\n"
             "Class defining an iterator designed to iterate over the\n"
             ":class:`StrokeVertex` of a :class:`Stroke`.  An instance of a\n"
             "StrokeVertexIterator can be obtained from a Stroke by calling\n"
             "iter(), stroke_vertices_begin() or stroke_vertices_begin().  It is iterating\n"
             "over the same vertices as an :class:`Interface0DIterator`.  The difference\n"
             "resides in the object access: an Interface0DIterator only allows\n"
             "access to an Interface0D while one might need to access the\n"
             "specialized StrokeVertex type.  In this case, one should use a\n"
             "StrokeVertexIterator.  To call functions of the UnaryFuntion0D type,\n"
             "a StrokeVertexIterator can be converted to an Interface0DIterator by\n"
             "by calling Interface0DIterator(it).\n"
             "\n"
             ".. method:: __init__()\n"
             "            __init__(brother)\n"
             "\n"
             "   Creates a :class:`StrokeVertexIterator` using either the\n"
             "   default constructor or the copy constructor.\n"
             "\n"
             "   :arg brother: A StrokeVertexIterator object.\n"
             "   :type brother: :class:`StrokeVertexIterator`");

static int StrokeVertexIterator_init(BPy_StrokeVertexIterator *self,
                                     PyObject *args,
                                     PyObject *kwds)
{
  static const char *kwlist_1[] = {"brother", nullptr};
  static const char *kwlist_2[] = {"stroke", nullptr};
  PyObject *brother = nullptr, *stroke = nullptr;

  if (PyArg_ParseTupleAndKeywords(
          args, kwds, "O!", (char **)kwlist_1, &StrokeVertexIterator_Type, &brother)) {
    self->sv_it = new StrokeInternal::StrokeVertexIterator(
        *(((BPy_StrokeVertexIterator *)brother)->sv_it));
    self->reversed = ((BPy_StrokeVertexIterator *)brother)->reversed;
    self->at_start = ((BPy_StrokeVertexIterator *)brother)->at_start;
  }

  else if ((void)PyErr_Clear(),
           PyArg_ParseTupleAndKeywords(
               args, kwds, "|O!", (char **)kwlist_2, &Stroke_Type, &stroke)) {
    if (!stroke) {
      self->sv_it = new StrokeInternal::StrokeVertexIterator();
    }
    else {
      self->sv_it = new StrokeInternal::StrokeVertexIterator(
          ((BPy_Stroke *)stroke)->s->strokeVerticesBegin());
    }
    self->reversed = false;
    self->at_start = true;
  }
  else {
    PyErr_SetString(PyExc_TypeError, "argument 1 must be StrokeVertexIterator or Stroke");
    return -1;
  }
  self->py_it.it = self->sv_it;
  return 0;
}

static PyObject *StrokeVertexIterator_iter(BPy_StrokeVertexIterator *self)
{
  Py_INCREF(self);
  self->at_start = true;
  return (PyObject *)self;
}

static PyObject *StrokeVertexIterator_iternext(BPy_StrokeVertexIterator *self)
{
  /* Because Freestyle iterators for which it.isEnd() holds true have no valid object
   * (they point to the past-the-end element and can't be dereferenced), we have to check
   * iterators for validity.
   * Additionally, the at_start attribute is used to keep Freestyle iterator objects
   * and Python for loops in sync. */

  if (self->reversed) {
    if (self->sv_it->isBegin()) {
      PyErr_SetNone(PyExc_StopIteration);
      return nullptr;
    }
    self->sv_it->decrement();
  }
  else {
    /* If sv_it.isEnd() is true, the iterator can't be incremented. */
    if (self->sv_it->isEnd()) {
      PyErr_SetNone(PyExc_StopIteration);
      return nullptr;
    }
    /* If at the start of the iterator, only return the object
     * and don't increment, to keep for-loops in sync */
    if (self->at_start) {
      self->at_start = false;
    }
    /* If sv_it.atLast() is true, the iterator is currently pointing to the final valid element.
     * Incrementing it further would lead to a state that the iterator can't be dereferenced. */
    else if (self->sv_it->atLast()) {
      PyErr_SetNone(PyExc_StopIteration);
      return nullptr;
    }
    else {
      self->sv_it->increment();
    }
  }
  StrokeVertex *sv = self->sv_it->operator->();
  return BPy_StrokeVertex_from_StrokeVertex(*sv);
}

/*----------------------StrokeVertexIterator methods ----------------------------*/

PyDoc_STRVAR(StrokeVertexIterator_incremented_doc,
             ".. method:: incremented()\n"
             "\n"
             "   Returns a copy of an incremented StrokeVertexIterator.\n"
             "\n"
             "   :return: A StrokeVertexIterator pointing the next StrokeVertex.\n"
             "   :rtype: :class:`StrokeVertexIterator`");

static PyObject *StrokeVertexIterator_incremented(BPy_StrokeVertexIterator *self)
{
  if (self->sv_it->isEnd()) {
    PyErr_SetString(PyExc_RuntimeError, "cannot increment any more");
    return nullptr;
  }
  StrokeInternal::StrokeVertexIterator copy(*self->sv_it);
  copy.increment();
  return BPy_StrokeVertexIterator_from_StrokeVertexIterator(copy, self->reversed);
}

PyDoc_STRVAR(StrokeVertexIterator_decremented_doc,
             ".. method:: decremented()\n"
             "\n"
             "   Returns a copy of a decremented StrokeVertexIterator.\n"
             "\n"
             "   :return: A StrokeVertexIterator pointing the previous StrokeVertex.\n"
             "   :rtype: :class:`StrokeVertexIterator`");

static PyObject *StrokeVertexIterator_decremented(BPy_StrokeVertexIterator *self)
{
  if (self->sv_it->isBegin()) {
    PyErr_SetString(PyExc_RuntimeError, "cannot decrement any more");
    return nullptr;
  }
  StrokeInternal::StrokeVertexIterator copy(*self->sv_it);
  copy.decrement();
  return BPy_StrokeVertexIterator_from_StrokeVertexIterator(copy, self->reversed);
}

PyDoc_STRVAR(StrokeVertexIterator_reversed_doc,
             ".. method:: reversed()\n"
             "\n"
             "   Returns a StrokeVertexIterator that traverses stroke vertices in the\n"
             "   reversed order.\n"
             "\n"
             "   :return: A StrokeVertexIterator traversing stroke vertices backward.\n"
             "   :rtype: :class:`StrokeVertexIterator`");

static PyObject *StrokeVertexIterator_reversed(BPy_StrokeVertexIterator *self)
{
  return BPy_StrokeVertexIterator_from_StrokeVertexIterator(*self->sv_it, !self->reversed);
}

static PyMethodDef BPy_StrokeVertexIterator_methods[] = {
    {"incremented",
     (PyCFunction)StrokeVertexIterator_incremented,
     METH_NOARGS,
     StrokeVertexIterator_incremented_doc},
    {"decremented",
     (PyCFunction)StrokeVertexIterator_decremented,
     METH_NOARGS,
     StrokeVertexIterator_decremented_doc},
    {"reversed",
     (PyCFunction)StrokeVertexIterator_reversed,
     METH_NOARGS,
     StrokeVertexIterator_reversed_doc},
    {nullptr, nullptr, 0, nullptr},
};

/*----------------------StrokeVertexIterator get/setters ----------------------------*/

PyDoc_STRVAR(StrokeVertexIterator_object_doc,
             "The StrokeVertex object currently pointed to by this iterator.\n"
             "\n"
             ":type: :class:`StrokeVertex`");

static PyObject *StrokeVertexIterator_object_get(BPy_StrokeVertexIterator *self,
                                                 void * /*closure*/)
{
  if (self->sv_it->isEnd()) {
    PyErr_SetString(PyExc_RuntimeError, "iteration has stopped");
    return nullptr;
  }
  StrokeVertex *sv = self->sv_it->operator->();
  if (sv) {
    return BPy_StrokeVertex_from_StrokeVertex(*sv);
  }
  Py_RETURN_NONE;
}

PyDoc_STRVAR(StrokeVertexIterator_t_doc,
             "The curvilinear abscissa of the current point.\n"
             "\n"
             ":type: float");

static PyObject *StrokeVertexIterator_t_get(BPy_StrokeVertexIterator *self, void * /*closure*/)
{
  return PyFloat_FromDouble(self->sv_it->t());
}

PyDoc_STRVAR(StrokeVertexIterator_u_doc,
             "The point parameter at the current point in the stroke (0 <= u <= 1).\n"
             "\n"
             ":type: float");

static PyObject *StrokeVertexIterator_u_get(BPy_StrokeVertexIterator *self, void * /*closure*/)
{
  return PyFloat_FromDouble(self->sv_it->u());
}

PyDoc_STRVAR(StrokeVertexIterator_at_last_doc,
             "True if the iterator points to the last valid element.\n"
             "For its counterpart (pointing to the first valid element), use it.is_begin.\n"
             "\n"
             ":type: bool");

static PyObject *StrokeVertexIterator_at_last_get(BPy_StrokeVertexIterator *self)
{
  return PyBool_from_bool(self->sv_it->atLast());
}

static PyGetSetDef BPy_StrokeVertexIterator_getseters[] = {
    {"object",
     (getter)StrokeVertexIterator_object_get,
     (setter) nullptr,
     StrokeVertexIterator_object_doc,
     nullptr},
    {"t",
     (getter)StrokeVertexIterator_t_get,
     (setter) nullptr,
     StrokeVertexIterator_t_doc,
     nullptr},
    {"u",
     (getter)StrokeVertexIterator_u_get,
     (setter) nullptr,
     StrokeVertexIterator_u_doc,
     nullptr},
    {"at_last",
     (getter)StrokeVertexIterator_at_last_get,
     (setter) nullptr,
     StrokeVertexIterator_at_last_doc,
     nullptr},
    {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
};

/*-----------------------BPy_StrokeVertexIterator type definition ------------------------------*/

PyTypeObject StrokeVertexIterator_Type = {
    PyVarObject_HEAD_INIT(nullptr, 0)
    /*tp_name*/ "StrokeVertexIterator",
    /*tp_basicsize*/ sizeof(BPy_StrokeVertexIterator),
    /*tp_itemsize*/ 0,
    /*tp_dealloc*/ nullptr,
    /*tp_vectorcall_offset*/ 0,
    /*tp_getattr*/ nullptr,
    /*tp_setattr*/ nullptr,
    /*tp_as_async*/ nullptr,
    /*tp_repr*/ nullptr,
    /*tp_as_number*/ nullptr,
    /*tp_as_sequence*/ nullptr,
    /*tp_as_mapping*/ nullptr,
    /*tp_hash*/ nullptr,
    /*tp_call*/ nullptr,
    /*tp_str*/ nullptr,
    /*tp_getattro*/ nullptr,
    /*tp_setattro*/ nullptr,
    /*tp_as_buffer*/ nullptr,
    /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
    /*tp_doc*/ StrokeVertexIterator_doc,
    /*tp_traverse*/ nullptr,
    /*tp_clear*/ nullptr,
    /*tp_richcompare*/ nullptr,
    /*tp_weaklistoffset*/ 0,
    /*tp_iter*/ (getiterfunc)StrokeVertexIterator_iter,
    /*tp_iternext*/ (iternextfunc)StrokeVertexIterator_iternext,
    /*tp_methods*/ BPy_StrokeVertexIterator_methods,
    /*tp_members*/ nullptr,
    /*tp_getset*/ BPy_StrokeVertexIterator_getseters,
    /*tp_base*/ &Iterator_Type,
    /*tp_dict*/ nullptr,
    /*tp_descr_get*/ nullptr,
    /*tp_descr_set*/ nullptr,
    /*tp_dictoffset*/ 0,
    /*tp_init*/ (initproc)StrokeVertexIterator_init,
    /*tp_alloc*/ nullptr,
    /*tp_new*/ nullptr,
};

///////////////////////////////////////////////////////////////////////////////////////////

#ifdef __cplusplus
}
#endif