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