Blender  V3.3
BPy_CurvePoint.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include "BPy_CurvePoint.h"
8 
9 #include "../BPy_Convert.h"
10 #include "../Interface0D/BPy_SVertex.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 using namespace Freestyle;
17 
19 
20 /*----------------------CurvePoint methods----------------------------*/
21 
22 PyDoc_STRVAR(CurvePoint_doc,
23  "Class hierarchy: :class:`Interface0D` > :class:`CurvePoint`\n"
24  "\n"
25  "Class to represent a point of a curve. A CurvePoint can be any point\n"
26  "of a 1D curve (it doesn't have to be a vertex of the curve). Any\n"
27  ":class:`Interface1D` is built upon ViewEdges, themselves built upon\n"
28  "FEdges. Therefore, a curve is basically a polyline made of a list of\n"
29  ":class:`SVertex` objects. Thus, a CurvePoint is built by linearly\n"
30  "interpolating two :class:`SVertex` instances. CurvePoint can be used\n"
31  "as virtual points while querying 0D information along a curve at a\n"
32  "given resolution.\n"
33  "\n"
34  ".. method:: __init__()\n"
35  " __init__(brother)\n"
36  " __init__(first_vertex, second_vertex, t2d)\n"
37  " __init__(first_point, second_point, t2d)\n"
38  "\n"
39  " Builds a CurvePoint using the default constructor, copy constructor,\n"
40  " or one of the overloaded constructors. The over loaded constructors\n"
41  " can either take two :class:`SVertex` or two :class:`CurvePoint`\n"
42  " objects and an interpolation parameter\n"
43  "\n"
44  " :arg brother: A CurvePoint object.\n"
45  " :type brother: :class:`CurvePoint`\n"
46  " :arg first_vertex: The first SVertex.\n"
47  " :type first_vertex: :class:`SVertex`\n"
48  " :arg second_vertex: The second SVertex.\n"
49  " :type second_vertex: :class:`SVertex`\n"
50  " :arg first_point: The first CurvePoint.\n"
51  " :type first_point: :class:`CurvePoint`\n"
52  " :arg second_point: The second CurvePoint.\n"
53  " :type second_point: :class:`CurvePoint`\n"
54  " :arg t2d: A 2D interpolation parameter used to linearly interpolate\n"
55  " first_vertex and second_vertex or first_point and second_point.\n"
56  " :type t2d: float\n");
57 
58 static int CurvePoint_init(BPy_CurvePoint *self, PyObject *args, PyObject *kwds)
59 {
60  static const char *kwlist_1[] = {"brother", nullptr};
61  static const char *kwlist_2[] = {"first_vertex", "second_vertex", "t2d", nullptr};
62  static const char *kwlist_3[] = {"first_point", "second_point", "t2d", nullptr};
63  PyObject *obj1 = nullptr, *obj2 = nullptr;
64  float t2d;
65 
66  if (PyArg_ParseTupleAndKeywords(args, kwds, "|O!", (char **)kwlist_1, &CurvePoint_Type, &obj1)) {
67  if (!obj1) {
68  self->cp = new CurvePoint();
69  }
70  else {
71  self->cp = new CurvePoint(*(((BPy_CurvePoint *)obj1)->cp));
72  }
73  }
74  else if ((void)PyErr_Clear(),
75  PyArg_ParseTupleAndKeywords(args,
76  kwds,
77  "O!O!f",
78  (char **)kwlist_2,
79  &SVertex_Type,
80  &obj1,
81  &SVertex_Type,
82  &obj2,
83  &t2d)) {
84  self->cp = new CurvePoint(((BPy_SVertex *)obj1)->sv, ((BPy_SVertex *)obj2)->sv, t2d);
85  }
86  else if ((void)PyErr_Clear(),
87  PyArg_ParseTupleAndKeywords(args,
88  kwds,
89  "O!O!f",
90  (char **)kwlist_3,
92  &obj1,
94  &obj2,
95  &t2d)) {
96  CurvePoint *cp1 = ((BPy_CurvePoint *)obj1)->cp;
97  CurvePoint *cp2 = ((BPy_CurvePoint *)obj2)->cp;
98  if (!cp1 || cp1->A() == nullptr || cp1->B() == nullptr) {
99  PyErr_SetString(PyExc_TypeError, "argument 1 is an invalid CurvePoint object");
100  return -1;
101  }
102  if (!cp2 || cp2->A() == nullptr || cp2->B() == nullptr) {
103  PyErr_SetString(PyExc_TypeError, "argument 2 is an invalid CurvePoint object");
104  return -1;
105  }
106  self->cp = new CurvePoint(cp1, cp2, t2d);
107  }
108  else {
109  PyErr_SetString(PyExc_TypeError, "invalid argument(s)");
110  return -1;
111  }
112  self->py_if0D.if0D = self->cp;
113  self->py_if0D.borrowed = false;
114  return 0;
115 }
116 
118 
119 /*----------------------CurvePoint get/setters ----------------------------*/
120 
121 PyDoc_STRVAR(CurvePoint_first_svertex_doc,
122  "The first SVertex upon which the CurvePoint is built.\n"
123  "\n"
124  ":type: :class:`SVertex`");
125 
126 static PyObject *CurvePoint_first_svertex_get(BPy_CurvePoint *self, void *UNUSED(closure))
127 {
128  SVertex *A = self->cp->A();
129  if (A) {
130  return BPy_SVertex_from_SVertex(*A);
131  }
132  Py_RETURN_NONE;
133 }
134 
136  PyObject *value,
137  void *UNUSED(closure))
138 {
139  if (!BPy_SVertex_Check(value)) {
140  PyErr_SetString(PyExc_TypeError, "value must be an SVertex");
141  return -1;
142  }
143  self->cp->setA(((BPy_SVertex *)value)->sv);
144  return 0;
145 }
146 
147 PyDoc_STRVAR(CurvePoint_second_svertex_doc,
148  "The second SVertex upon which the CurvePoint is built.\n"
149  "\n"
150  ":type: :class:`SVertex`");
151 
152 static PyObject *CurvePoint_second_svertex_get(BPy_CurvePoint *self, void *UNUSED(closure))
153 {
154  SVertex *B = self->cp->B();
155  if (B) {
156  return BPy_SVertex_from_SVertex(*B);
157  }
158  Py_RETURN_NONE;
159 }
160 
162  PyObject *value,
163  void *UNUSED(closure))
164 {
165  if (!BPy_SVertex_Check(value)) {
166  PyErr_SetString(PyExc_TypeError, "value must be an SVertex");
167  return -1;
168  }
169  self->cp->setB(((BPy_SVertex *)value)->sv);
170  return 0;
171 }
172 
173 PyDoc_STRVAR(CurvePoint_fedge_doc,
174  "Gets the FEdge for the two SVertices that given CurvePoints consists out of.\n"
175  "A shortcut for CurvePoint.first_svertex.get_fedge(CurvePoint.second_svertex).\n"
176  "\n"
177  ":type: :class:`FEdge`");
178 
179 static PyObject *CurvePoint_fedge_get(BPy_CurvePoint *self, void *UNUSED(closure))
180 {
181  SVertex *A = self->cp->A();
182  Interface0D *B = (Interface0D *)self->cp->B();
183  // B can be NULL under certain circumstances
184  if (B) {
185  return Any_BPy_Interface1D_from_Interface1D(*(A->getFEdge(*B)));
186  }
187  Py_RETURN_NONE;
188 }
189 
190 PyDoc_STRVAR(CurvePoint_t2d_doc,
191  "The 2D interpolation parameter.\n"
192  "\n"
193  ":type: float");
194 
195 static PyObject *CurvePoint_t2d_get(BPy_CurvePoint *self, void *UNUSED(closure))
196 {
197  return PyFloat_FromDouble(self->cp->t2d());
198 }
199 
200 static int CurvePoint_t2d_set(BPy_CurvePoint *self, PyObject *value, void *UNUSED(closure))
201 {
202  float scalar;
203  if ((scalar = PyFloat_AsDouble(value)) == -1.0f && PyErr_Occurred()) {
204  PyErr_SetString(PyExc_TypeError, "value must be a number");
205  return -1;
206  }
207  self->cp->setT2d(scalar);
208  return 0;
209 }
210 
211 static PyGetSetDef BPy_CurvePoint_getseters[] = {
212  {"first_svertex",
215  CurvePoint_first_svertex_doc,
216  nullptr},
217  {"second_svertex",
220  CurvePoint_second_svertex_doc,
221  nullptr},
222  {"fedge", (getter)CurvePoint_fedge_get, nullptr, CurvePoint_fedge_doc, nullptr},
223  {"t2d", (getter)CurvePoint_t2d_get, (setter)CurvePoint_t2d_set, CurvePoint_t2d_doc, nullptr},
224  {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
225 };
226 
227 /*-----------------------BPy_CurvePoint type definition ------------------------------*/
228 PyTypeObject CurvePoint_Type = {
229  PyVarObject_HEAD_INIT(nullptr, 0) "CurvePoint", /* tp_name */
230  sizeof(BPy_CurvePoint), /* tp_basicsize */
231  0, /* tp_itemsize */
232  nullptr, /* tp_dealloc */
233  0, /* tp_vectorcall_offset */
234  nullptr, /* tp_getattr */
235  nullptr, /* tp_setattr */
236  nullptr, /* tp_reserved */
237  nullptr, /* tp_repr */
238  nullptr, /* tp_as_number */
239  nullptr, /* tp_as_sequence */
240  nullptr, /* tp_as_mapping */
241  nullptr, /* tp_hash */
242  nullptr, /* tp_call */
243  nullptr, /* tp_str */
244  nullptr, /* tp_getattro */
245  nullptr, /* tp_setattro */
246  nullptr, /* tp_as_buffer */
247  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
248  CurvePoint_doc, /* tp_doc */
249  nullptr, /* tp_traverse */
250  nullptr, /* tp_clear */
251  nullptr, /* tp_richcompare */
252  0, /* tp_weaklistoffset */
253  nullptr, /* tp_iter */
254  nullptr, /* tp_iternext */
255  nullptr, /* tp_methods */
256  nullptr, /* tp_members */
257  BPy_CurvePoint_getseters, /* tp_getset */
258  &Interface0D_Type, /* tp_base */
259  nullptr, /* tp_dict */
260  nullptr, /* tp_descr_get */
261  nullptr, /* tp_descr_set */
262  0, /* tp_dictoffset */
263  (initproc)CurvePoint_init, /* tp_init */
264  nullptr, /* tp_alloc */
265  nullptr, /* tp_new */
266 };
267 
269 
270 #ifdef __cplusplus
271 }
272 #endif
#define UNUSED(x)
PyObject * BPy_SVertex_from_SVertex(SVertex &sv)
PyObject * Any_BPy_Interface1D_from_Interface1D(Interface1D &if1D)
static PyObject * CurvePoint_second_svertex_get(BPy_CurvePoint *self, void *UNUSED(closure))
static int CurvePoint_init(BPy_CurvePoint *self, PyObject *args, PyObject *kwds)
PyTypeObject CurvePoint_Type
static int CurvePoint_first_svertex_set(BPy_CurvePoint *self, PyObject *value, void *UNUSED(closure))
PyDoc_STRVAR(CurvePoint_doc, "Class hierarchy: :class:`Interface0D` > :class:`CurvePoint`\n" "\n" "Class to represent a point of a curve. A CurvePoint can be any point\n" "of a 1D curve (it doesn't have to be a vertex of the curve). Any\n" ":class:`Interface1D` is built upon ViewEdges, themselves built upon\n" "FEdges. Therefore, a curve is basically a polyline made of a list of\n" ":class:`SVertex` objects. Thus, a CurvePoint is built by linearly\n" "interpolating two :class:`SVertex` instances. CurvePoint can be used\n" "as virtual points while querying 0D information along a curve at a\n" "given resolution.\n" "\n" ".. method:: __init__()\n" " __init__(brother)\n" " __init__(first_vertex, second_vertex, t2d)\n" " __init__(first_point, second_point, t2d)\n" "\n" " Builds a CurvePoint using the default constructor, copy constructor,\n" " or one of the overloaded constructors. The over loaded constructors\n" " can either take two :class:`SVertex` or two :class:`CurvePoint`\n" " objects and an interpolation parameter\n" "\n" " :arg brother: A CurvePoint object.\n" " :type brother: :class:`CurvePoint`\n" " :arg first_vertex: The first SVertex.\n" " :type first_vertex: :class:`SVertex`\n" " :arg second_vertex: The second SVertex.\n" " :type second_vertex: :class:`SVertex`\n" " :arg first_point: The first CurvePoint.\n" " :type first_point: :class:`CurvePoint`\n" " :arg second_point: The second CurvePoint.\n" " :type second_point: :class:`CurvePoint`\n" " :arg t2d: A 2D interpolation parameter used to linearly interpolate\n" " first_vertex and second_vertex or first_point and second_point.\n" " :type t2d: float\n")
static int CurvePoint_t2d_set(BPy_CurvePoint *self, PyObject *value, void *UNUSED(closure))
static PyObject * CurvePoint_first_svertex_get(BPy_CurvePoint *self, void *UNUSED(closure))
static PyGetSetDef BPy_CurvePoint_getseters[]
static int CurvePoint_second_svertex_set(BPy_CurvePoint *self, PyObject *value, void *UNUSED(closure))
static PyObject * CurvePoint_fedge_get(BPy_CurvePoint *self, void *UNUSED(closure))
static PyObject * CurvePoint_t2d_get(BPy_CurvePoint *self, void *UNUSED(closure))
PyTypeObject Interface0D_Type
PyTypeObject SVertex_Type
#define BPy_SVertex_Check(v)
Definition: BPy_SVertex.h:21
#define A
PyObject * self
Definition: bpy_driver.c:165
SVertex * B()
Definition: Curve.h:241
SVertex * A()
Definition: Curve.h:235
#define B
inherits from class Rep
Definition: AppCanvas.cpp:18