Blender  V2.93
BPy_StrokeVertex.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_StrokeVertex.h"
22 
23 #include "../../BPy_Convert.h"
24 #include "../../BPy_Freestyle.h"
25 #include "../../BPy_StrokeAttribute.h"
26 #include "../../Interface0D/BPy_SVertex.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 using namespace Freestyle;
33 
35 
36 //------------------------INSTANCE METHODS ----------------------------------
37 
39  StrokeVertex_doc,
40  "Class hierarchy: :class:`Interface0D` > :class:`CurvePoint` > :class:`StrokeVertex`\n"
41  "\n"
42  "Class to define a stroke vertex.\n"
43  "\n"
44  ".. method:: __init__()\n"
45  " __init__(brother)\n"
46  " __init__(first_vertex, second_vertex, t3d)\n"
47  " __init__(point)\n"
48  " __init__(svertex)\n"
49  " __init__(svertex, attribute)\n"
50  "\n"
51  " Builds a :class:`StrokeVertex` using the default constructor,\n"
52  " copy constructor, from 2 :class:`StrokeVertex` and an interpolation parameter,\n"
53  " from a CurvePoint, from a SVertex, or a :class:`SVertex`"
54  " and a :class:`StrokeAttribute` object.\n"
55  "\n"
56  " :arg brother: A StrokeVertex object.\n"
57  " :type brother: :class:`StrokeVertex`\n"
58  " :arg first_vertex: The first StrokeVertex.\n"
59  " :type first_vertex: :class:`StrokeVertex`\n"
60  " :arg second_vertex: The second StrokeVertex.\n"
61  " :type second_vertex: :class:`StrokeVertex`\n"
62  " :arg t3d: An interpolation parameter.\n"
63  " :type t3d: float\n"
64  " :arg point: A CurvePoint object.\n"
65  " :type point: :class:`CurvePoint`\n"
66  " :arg svertex: An SVertex object.\n"
67  " :type svertex: :class:`SVertex`\n"
68  " :arg svertex: An SVertex object.\n"
69  " :type svertex: :class:`SVertex`\n"
70  " :arg attribute: A StrokeAttribute object.\n"
71  " :type attribute: :class:`StrokeAttribute`");
72 
73 static int StrokeVertex_init(BPy_StrokeVertex *self, PyObject *args, PyObject *kwds)
74 {
75  static const char *kwlist_1[] = {"brother", nullptr};
76  static const char *kwlist_2[] = {"first_vertex", "second_vertex", "t3d", nullptr};
77  static const char *kwlist_3[] = {"point", nullptr};
78  static const char *kwlist_4[] = {"svertex", "attribute", nullptr};
79  PyObject *obj1 = nullptr, *obj2 = nullptr;
80  float t3d;
81 
82  if (PyArg_ParseTupleAndKeywords(
83  args, kwds, "|O!", (char **)kwlist_1, &StrokeVertex_Type, &obj1)) {
84  if (!obj1) {
85  self->sv = new StrokeVertex();
86  }
87  else {
88  if (!((BPy_StrokeVertex *)obj1)->sv) {
89  PyErr_SetString(PyExc_TypeError, "argument 1 is an invalid StrokeVertex object");
90  return -1;
91  }
92  self->sv = new StrokeVertex(*(((BPy_StrokeVertex *)obj1)->sv));
93  }
94  }
95  else if ((void)PyErr_Clear(),
96  PyArg_ParseTupleAndKeywords(args,
97  kwds,
98  "O!O!f",
99  (char **)kwlist_2,
101  &obj1,
103  &obj2,
104  &t3d)) {
105  StrokeVertex *sv1 = ((BPy_StrokeVertex *)obj1)->sv;
106  StrokeVertex *sv2 = ((BPy_StrokeVertex *)obj2)->sv;
107  if (!sv1 || (sv1->A() == nullptr && sv1->B() == nullptr)) {
108  PyErr_SetString(PyExc_TypeError, "argument 1 is an invalid StrokeVertex object");
109  return -1;
110  }
111  if (!sv2 || (sv2->A() == nullptr && sv2->B() == nullptr)) {
112  PyErr_SetString(PyExc_TypeError, "argument 2 is an invalid StrokeVertex object");
113  return -1;
114  }
115  self->sv = new StrokeVertex(sv1, sv2, t3d);
116  }
117  else if ((void)PyErr_Clear(),
118  PyArg_ParseTupleAndKeywords(
119  args, kwds, "O!", (char **)kwlist_3, &CurvePoint_Type, &obj1)) {
120  CurvePoint *cp = ((BPy_CurvePoint *)obj1)->cp;
121  if (!cp || cp->A() == nullptr || cp->B() == nullptr) {
122  PyErr_SetString(PyExc_TypeError, "argument 1 is an invalid CurvePoint object");
123  return -1;
124  }
125  self->sv = new StrokeVertex(cp);
126  }
127  else if ((void)PyErr_Clear(),
128  (void)(obj2 = nullptr),
129  PyArg_ParseTupleAndKeywords(args,
130  kwds,
131  "O!|O!",
132  (char **)kwlist_4,
133  &SVertex_Type,
134  &obj1,
136  &obj2)) {
137  if (!obj2) {
138  self->sv = new StrokeVertex(((BPy_SVertex *)obj1)->sv);
139  }
140  else {
141  self->sv = new StrokeVertex(((BPy_SVertex *)obj1)->sv, *(((BPy_StrokeAttribute *)obj2)->sa));
142  }
143  }
144  else {
145  PyErr_SetString(PyExc_TypeError, "invalid argument(s)");
146  return -1;
147  }
148  self->py_cp.cp = self->sv;
149  self->py_cp.py_if0D.if0D = self->sv;
150  self->py_cp.py_if0D.borrowed = false;
151  return 0;
152 }
153 
154 // real operator[] (const int i) const
155 // real & operator[] (const int i)
156 
157 /*----------------------mathutils callbacks ----------------------------*/
158 
160 {
161  if (!BPy_StrokeVertex_Check(bmo->cb_user)) {
162  return -1;
163  }
164  return 0;
165 }
166 
167 static int StrokeVertex_mathutils_get(BaseMathObject *bmo, int /*subtype*/)
168 {
169  BPy_StrokeVertex *self = (BPy_StrokeVertex *)bmo->cb_user;
170  bmo->data[0] = (float)self->sv->x();
171  bmo->data[1] = (float)self->sv->y();
172  return 0;
173 }
174 
175 static int StrokeVertex_mathutils_set(BaseMathObject *bmo, int /*subtype*/)
176 {
177  BPy_StrokeVertex *self = (BPy_StrokeVertex *)bmo->cb_user;
178  self->sv->setX((real)bmo->data[0]);
179  self->sv->setY((real)bmo->data[1]);
180  return 0;
181 }
182 
183 static int StrokeVertex_mathutils_get_index(BaseMathObject *bmo, int /*subtype*/, int index)
184 {
185  BPy_StrokeVertex *self = (BPy_StrokeVertex *)bmo->cb_user;
186  switch (index) {
187  case 0:
188  bmo->data[0] = (float)self->sv->x();
189  break;
190  case 1:
191  bmo->data[1] = (float)self->sv->y();
192  break;
193  default:
194  return -1;
195  }
196  return 0;
197 }
198 
199 static int StrokeVertex_mathutils_set_index(BaseMathObject *bmo, int /*subtype*/, int index)
200 {
201  BPy_StrokeVertex *self = (BPy_StrokeVertex *)bmo->cb_user;
202  switch (index) {
203  case 0:
204  self->sv->setX((real)bmo->data[0]);
205  break;
206  case 1:
207  self->sv->setY((real)bmo->data[1]);
208  break;
209  default:
210  return -1;
211  }
212  return 0;
213 }
214 
221 };
222 
223 static unsigned char StrokeVertex_mathutils_cb_index = -1;
224 
226 {
228 }
229 
230 /*----------------------StrokeVertex get/setters ----------------------------*/
231 
232 PyDoc_STRVAR(StrokeVertex_attribute_doc,
233  "StrokeAttribute for this StrokeVertex.\n"
234  "\n"
235  ":type: :class:`StrokeAttribute`");
236 
237 static PyObject *StrokeVertex_attribute_get(BPy_StrokeVertex *self, void *UNUSED(closure))
238 {
239  return BPy_StrokeAttribute_from_StrokeAttribute(self->sv->attribute());
240 }
241 
243  PyObject *value,
244  void *UNUSED(closure))
245 {
246  if (!BPy_StrokeAttribute_Check(value)) {
247  PyErr_SetString(PyExc_TypeError, "value must be a StrokeAttribute object");
248  return -1;
249  }
250  self->sv->setAttribute(*(((BPy_StrokeAttribute *)value)->sa));
251  return 0;
252 }
253 
254 PyDoc_STRVAR(StrokeVertex_curvilinear_abscissa_doc,
255  "Curvilinear abscissa of this StrokeVertex in the Stroke.\n"
256  "\n"
257  ":type: float");
258 
260  void *UNUSED(closure))
261 {
262  return PyFloat_FromDouble(self->sv->curvilinearAbscissa());
263 }
264 
266  PyObject *value,
267  void *UNUSED(closure))
268 {
269  float scalar;
270  if ((scalar = PyFloat_AsDouble(value)) == -1.0f && PyErr_Occurred()) {
271  /* parsed item not a number */
272  PyErr_SetString(PyExc_TypeError, "value must be a number");
273  return -1;
274  }
275  self->sv->setCurvilinearAbscissa(scalar);
276  return 0;
277 }
278 
279 PyDoc_STRVAR(StrokeVertex_point_doc,
280  "2D point coordinates.\n"
281  "\n"
282  ":type: :class:`mathutils.Vector`");
283 
284 static PyObject *StrokeVertex_point_get(BPy_StrokeVertex *self, void *UNUSED(closure))
285 {
286  return Vector_CreatePyObject_cb((PyObject *)self, 2, StrokeVertex_mathutils_cb_index, 0);
287 }
288 
289 static int StrokeVertex_point_set(BPy_StrokeVertex *self, PyObject *value, void *UNUSED(closure))
290 {
291  float v[2];
292  if (mathutils_array_parse(v, 2, 2, value, "value must be a 2-dimensional vector") == -1) {
293  return -1;
294  }
295  self->sv->setX(v[0]);
296  self->sv->setY(v[1]);
297  return 0;
298 }
299 
300 PyDoc_STRVAR(StrokeVertex_stroke_length_doc,
301  "Stroke length (it is only a value retained by the StrokeVertex,\n"
302  "and it won't change the real stroke length).\n"
303  "\n"
304  ":type: float");
305 
306 static PyObject *StrokeVertex_stroke_length_get(BPy_StrokeVertex *self, void *UNUSED(closure))
307 {
308  return PyFloat_FromDouble(self->sv->strokeLength());
309 }
310 
312  PyObject *value,
313  void *UNUSED(closure))
314 {
315  float scalar;
316  if ((scalar = PyFloat_AsDouble(value)) == -1.0f && PyErr_Occurred()) {
317  /* parsed item not a number */
318  PyErr_SetString(PyExc_TypeError, "value must be a number");
319  return -1;
320  }
321  self->sv->setStrokeLength(scalar);
322  return 0;
323 }
324 
325 PyDoc_STRVAR(StrokeVertex_u_doc,
326  "Curvilinear abscissa of this StrokeVertex in the Stroke.\n"
327  "\n"
328  ":type: float");
329 
330 static PyObject *StrokeVertex_u_get(BPy_StrokeVertex *self, void *UNUSED(closure))
331 {
332  return PyFloat_FromDouble(self->sv->u());
333 }
334 
335 static PyGetSetDef BPy_StrokeVertex_getseters[] = {
336  {"attribute",
339  StrokeVertex_attribute_doc,
340  nullptr},
341  {"curvilinear_abscissa",
344  StrokeVertex_curvilinear_abscissa_doc,
345  nullptr},
346  {"point",
347  (getter)StrokeVertex_point_get,
348  (setter)StrokeVertex_point_set,
349  StrokeVertex_point_doc,
350  nullptr},
351  {"stroke_length",
354  StrokeVertex_stroke_length_doc,
355  nullptr},
356  {"u", (getter)StrokeVertex_u_get, (setter) nullptr, StrokeVertex_u_doc, nullptr},
357  {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
358 };
359 
360 /*-----------------------BPy_StrokeVertex type definition ------------------------------*/
361 PyTypeObject StrokeVertex_Type = {
362  PyVarObject_HEAD_INIT(nullptr, 0) "StrokeVertex", /* tp_name */
363  sizeof(BPy_StrokeVertex), /* tp_basicsize */
364  0, /* tp_itemsize */
365  nullptr, /* tp_dealloc */
366  0, /* tp_vectorcall_offset */
367  nullptr, /* tp_getattr */
368  nullptr, /* tp_setattr */
369  nullptr, /* tp_reserved */
370  nullptr, /* tp_repr */
371  nullptr, /* tp_as_number */
372  nullptr, /* tp_as_sequence */
373  nullptr, /* tp_as_mapping */
374  nullptr, /* tp_hash */
375  nullptr, /* tp_call */
376  nullptr, /* tp_str */
377  nullptr, /* tp_getattro */
378  nullptr, /* tp_setattro */
379  nullptr, /* tp_as_buffer */
380  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
381  StrokeVertex_doc, /* tp_doc */
382  nullptr, /* tp_traverse */
383  nullptr, /* tp_clear */
384  nullptr, /* tp_richcompare */
385  0, /* tp_weaklistoffset */
386  nullptr, /* tp_iter */
387  nullptr, /* tp_iternext */
388  nullptr, /* tp_methods */
389  nullptr, /* tp_members */
390  BPy_StrokeVertex_getseters, /* tp_getset */
391  &CurvePoint_Type, /* tp_base */
392  nullptr, /* tp_dict */
393  nullptr, /* tp_descr_get */
394  nullptr, /* tp_descr_set */
395  0, /* tp_dictoffset */
396  (initproc)StrokeVertex_init, /* tp_init */
397  nullptr, /* tp_alloc */
398  nullptr, /* tp_new */
399 };
400 
402 
403 #ifdef __cplusplus
404 }
405 #endif
typedef float(TangentPoint)[2]
#define UNUSED(x)
PyObject * BPy_StrokeAttribute_from_StrokeAttribute(StrokeAttribute &sa)
PyTypeObject CurvePoint_Type
PyTypeObject SVertex_Type
PyTypeObject StrokeAttribute_Type
#define BPy_StrokeAttribute_Check(v)
static int StrokeVertex_point_set(BPy_StrokeVertex *self, PyObject *value, void *UNUSED(closure))
static PyGetSetDef BPy_StrokeVertex_getseters[]
static PyObject * StrokeVertex_attribute_get(BPy_StrokeVertex *self, void *UNUSED(closure))
static Mathutils_Callback StrokeVertex_mathutils_cb
static int StrokeVertex_mathutils_check(BaseMathObject *bmo)
static int StrokeVertex_stroke_length_set(BPy_StrokeVertex *self, PyObject *value, void *UNUSED(closure))
static PyObject * StrokeVertex_u_get(BPy_StrokeVertex *self, void *UNUSED(closure))
static int StrokeVertex_mathutils_get(BaseMathObject *bmo, int)
static int StrokeVertex_mathutils_set_index(BaseMathObject *bmo, int, int index)
static int StrokeVertex_init(BPy_StrokeVertex *self, PyObject *args, PyObject *kwds)
static PyObject * StrokeVertex_curvilinear_abscissa_get(BPy_StrokeVertex *self, void *UNUSED(closure))
static int StrokeVertex_attribute_set(BPy_StrokeVertex *self, PyObject *value, void *UNUSED(closure))
static int StrokeVertex_mathutils_get_index(BaseMathObject *bmo, int, int index)
static int StrokeVertex_mathutils_set(BaseMathObject *bmo, int)
void StrokeVertex_mathutils_register_callback()
static PyObject * StrokeVertex_stroke_length_get(BPy_StrokeVertex *self, void *UNUSED(closure))
static int StrokeVertex_curvilinear_abscissa_set(BPy_StrokeVertex *self, PyObject *value, void *UNUSED(closure))
PyTypeObject StrokeVertex_Type
PyDoc_STRVAR(StrokeVertex_doc, "Class hierarchy: :class:`Interface0D` > :class:`CurvePoint` > :class:`StrokeVertex`\n" "\n" "Class to define a stroke vertex.\n" "\n" ".. method:: __init__()\n" " __init__(brother)\n" " __init__(first_vertex, second_vertex, t3d)\n" " __init__(point)\n" " __init__(svertex)\n" " __init__(svertex, attribute)\n" "\n" " Builds a :class:`StrokeVertex` using the default constructor,\n" " copy constructor, from 2 :class:`StrokeVertex` and an interpolation parameter,\n" " from a CurvePoint, from a SVertex, or a :class:`SVertex`" " and a :class:`StrokeAttribute` object.\n" "\n" " :arg brother: A StrokeVertex object.\n" " :type brother: :class:`StrokeVertex`\n" " :arg first_vertex: The first StrokeVertex.\n" " :type first_vertex: :class:`StrokeVertex`\n" " :arg second_vertex: The second StrokeVertex.\n" " :type second_vertex: :class:`StrokeVertex`\n" " :arg t3d: An interpolation parameter.\n" " :type t3d: float\n" " :arg point: A CurvePoint object.\n" " :type point: :class:`CurvePoint`\n" " :arg svertex: An SVertex object.\n" " :type svertex: :class:`SVertex`\n" " :arg svertex: An SVertex object.\n" " :type svertex: :class:`SVertex`\n" " :arg attribute: A StrokeAttribute object.\n" " :type attribute: :class:`StrokeAttribute`")
static unsigned char StrokeVertex_mathutils_cb_index
static PyObject * StrokeVertex_point_get(BPy_StrokeVertex *self, void *UNUSED(closure))
#define BPy_StrokeVertex_Check(v)
ATTR_WARN_UNUSED_RESULT const BMVert * v
PyObject * self
Definition: bpy_driver.c:185
SVertex * B()
Definition: Curve.h:255
SVertex * A()
Definition: Curve.h:249
uchar Mathutils_RegisterCallback(Mathutils_Callback *cb)
Definition: mathutils.c:584
int mathutils_array_parse(float *array, int array_min, int array_max, PyObject *value, const char *error_prefix)
Definition: mathutils.c:118
PyObject * Vector_CreatePyObject_cb(PyObject *cb_user, int size, uchar cb_type, uchar cb_subtype)
inherits from class Rep
Definition: AppCanvas.cpp:32
double real
Definition: Precision.h:26