Made the Freestyle Python API compatible with Python 3.
[blender.git] / source / blender / freestyle / intern / python / Interface0D / CurvePoint / BPy_StrokeVertex.cpp
1 #include "BPy_StrokeVertex.h"
2
3 #include "../../BPy_Convert.h"
4 #include "../../BPy_StrokeAttribute.h"
5 #include "../../Interface0D/BPy_SVertex.h"
6
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10
11 ///////////////////////////////////////////////////////////////////////////////////////////
12
13 /*---------------  Python API function prototypes for StrokeVertex instance  -----------*/
14 static int StrokeVertex___init__(BPy_StrokeVertex *self, PyObject *args, PyObject *kwds);
15
16 static PyObject * StrokeVertex_x( BPy_StrokeVertex *self );
17 static PyObject * StrokeVertex_y( BPy_StrokeVertex *self );
18 static PyObject * StrokeVertex_getPoint( BPy_StrokeVertex *self );
19 static PyObject * StrokeVertex_attribute( BPy_StrokeVertex *self );
20 static PyObject * StrokeVertex_curvilinearAbscissa( BPy_StrokeVertex *self );
21 static PyObject * StrokeVertex_strokeLength( BPy_StrokeVertex *self );
22 static PyObject * StrokeVertex_u( BPy_StrokeVertex *self );
23 static PyObject * StrokeVertex_setX( BPy_StrokeVertex *self , PyObject *args);
24 static PyObject * StrokeVertex_setY( BPy_StrokeVertex *self , PyObject *args);
25 static PyObject * StrokeVertex_setPoint( BPy_StrokeVertex *self , PyObject *args);
26 static PyObject * StrokeVertex_setAttribute( BPy_StrokeVertex *self , PyObject *args);
27 static PyObject * StrokeVertex_setCurvilinearAbscissa( BPy_StrokeVertex *self , PyObject *args);
28 static PyObject * StrokeVertex_setStrokeLength( BPy_StrokeVertex *self , PyObject *args);
29
30 /*----------------------StrokeVertex instance definitions ----------------------------*/
31 static PyMethodDef BPy_StrokeVertex_methods[] = {       
32 //      {"__copy__", ( PyCFunction ) StrokeVertex___copy__, METH_NOARGS, "() Cloning method."},
33         {"x", ( PyCFunction ) StrokeVertex_x, METH_NOARGS, "() Returns the 2D point x coordinate"},
34         {"y", ( PyCFunction ) StrokeVertex_y, METH_NOARGS, "() Returns the 2D point y coordinate"},
35         {"getPoint", ( PyCFunction ) StrokeVertex_getPoint, METH_NOARGS, "() Returns the 2D point coordinates as a Vec2d"},
36         {"attribute", ( PyCFunction ) StrokeVertex_attribute, METH_NOARGS, "() Returns the StrokeAttribute of this StrokeVertex"},
37         {"curvilinearAbscissa", ( PyCFunction ) StrokeVertex_curvilinearAbscissa, METH_NOARGS, "() Returns the curvilinear abscissa "},
38         {"strokeLength", ( PyCFunction ) StrokeVertex_strokeLength, METH_NOARGS, "() Returns the length of the Stroke to which this StrokeVertex belongs"},
39         {"u", ( PyCFunction ) StrokeVertex_u, METH_NOARGS, "() Returns the curvilinear abscissa of this StrokeVertex in the Stroke"},
40         {"setX", ( PyCFunction ) StrokeVertex_setX, METH_VARARGS, "(double r) Sets the 2D x value "},
41         {"setY", ( PyCFunction ) StrokeVertex_setY, METH_VARARGS, "(double r) Sets the 2D y value "},
42         {"setPoint", ( PyCFunction ) StrokeVertex_setPoint, METH_VARARGS, "(double x, double y) / ( [x,y] ) Sets the 2D x and y values"},
43         {"setAttribute", ( PyCFunction ) StrokeVertex_setAttribute, METH_VARARGS, "(StrokeAttribute sa) Sets the attribute."},
44         {"setCurvilinearAbscissa", ( PyCFunction ) StrokeVertex_setCurvilinearAbscissa, METH_VARARGS, "(double r) Sets the curvilinear abscissa of this StrokeVertex in the Stroke"},
45         {"setStrokeLength", ( PyCFunction ) StrokeVertex_setStrokeLength, METH_VARARGS, "(double r) Sets the Stroke's length (it's only a value stored by the Stroke Vertex, it won't change the real Stroke's length.) "},
46         {NULL, NULL, 0, NULL}
47 };
48
49 /*-----------------------BPy_StrokeVertex type definition ------------------------------*/
50
51 PyTypeObject StrokeVertex_Type = {
52         PyObject_HEAD_INIT(NULL)
53         "StrokeVertex",                 /* tp_name */
54         sizeof(BPy_StrokeVertex),       /* tp_basicsize */
55         0,                              /* tp_itemsize */
56         0,                              /* tp_dealloc */
57         0,                              /* tp_print */
58         0,                              /* tp_getattr */
59         0,                              /* tp_setattr */
60         0,                              /* tp_reserved */
61         0,                              /* tp_repr */
62         0,                              /* tp_as_number */
63         0,                              /* tp_as_sequence */
64         0,                              /* tp_as_mapping */
65         0,                              /* tp_hash  */
66         0,                              /* tp_call */
67         0,                              /* tp_str */
68         0,                              /* tp_getattro */
69         0,                              /* tp_setattro */
70         0,                              /* tp_as_buffer */
71         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
72         "StrokeVertex objects",         /* tp_doc */
73         0,                              /* tp_traverse */
74         0,                              /* tp_clear */
75         0,                              /* tp_richcompare */
76         0,                              /* tp_weaklistoffset */
77         0,                              /* tp_iter */
78         0,                              /* tp_iternext */
79         BPy_StrokeVertex_methods,       /* tp_methods */
80         0,                              /* tp_members */
81         0,                              /* tp_getset */
82         &CurvePoint_Type,               /* tp_base */
83         0,                              /* tp_dict */
84         0,                              /* tp_descr_get */
85         0,                              /* tp_descr_set */
86         0,                              /* tp_dictoffset */
87         (initproc)StrokeVertex___init__, /* tp_init */
88         0,                              /* tp_alloc */
89         0,                              /* tp_new */
90 };
91
92 //------------------------INSTANCE METHODS ----------------------------------
93
94 int StrokeVertex___init__(BPy_StrokeVertex *self, PyObject *args, PyObject *kwds)
95 {
96
97         PyObject *obj1 = 0, *obj2 = 0 , *obj3 = 0;
98
99     if (! PyArg_ParseTuple(args, "|OOO!", &obj1, &obj2, &PyFloat_Type, &obj3) )
100         return -1;
101
102         if( !obj1 ){
103                 self->sv = new StrokeVertex();
104                 
105         } else if( !obj2 && BPy_StrokeVertex_Check(obj1) && ((BPy_StrokeVertex *) obj1)->sv ) {
106                 self->sv = new StrokeVertex( *(((BPy_StrokeVertex *) obj1)->sv) );
107
108         } else if( !obj2 && BPy_CurvePoint_Check(obj1) && ((BPy_CurvePoint *) obj1)->cp ) {
109                 self->sv = new StrokeVertex( ((BPy_CurvePoint *) obj1)->cp );
110         
111         } else if( !obj2 && BPy_SVertex_Check(obj1) && ((BPy_SVertex *) obj1)->sv ) {
112                 self->sv = new StrokeVertex( ((BPy_SVertex *) obj1)->sv );
113         
114         } else if( obj3 && BPy_StrokeVertex_Check(obj1) && BPy_StrokeVertex_Check(obj2) ) {
115                 StrokeVertex *sv1 = ((BPy_StrokeVertex *) obj1)->sv;
116                 StrokeVertex *sv2 = ((BPy_StrokeVertex *) obj2)->sv;
117                 if( !sv1 || ( sv1->A() == 0 && sv1->B() == 0 ) ) {
118                         PyErr_SetString(PyExc_TypeError, "argument 1 is an invalid StrokeVertex object");
119                         return -1;
120                 }
121                 if( !sv2 || ( sv2->A() == 0 && sv2->B() == 0 ) ) {
122                         PyErr_SetString(PyExc_TypeError, "argument 2 is an invalid StrokeVertex object");
123                         return -1;
124                 }
125                 self->sv = new StrokeVertex( sv1, sv2, PyFloat_AsDouble( obj3 ) );
126
127         } else {
128                 PyErr_SetString(PyExc_TypeError, "invalid argument(s)");
129                 return -1;
130         }
131
132         self->py_cp.cp = self->sv;
133         self->py_cp.py_if0D.if0D = self->sv;
134         self->py_cp.py_if0D.borrowed = 0;
135
136         return 0;
137 }
138
139 PyObject * StrokeVertex_x( BPy_StrokeVertex *self ) {
140         return PyFloat_FromDouble( self->sv->x() );
141 }
142
143 PyObject * StrokeVertex_y( BPy_StrokeVertex *self ) {
144         return PyFloat_FromDouble( self->sv->y() );
145 }
146
147 PyObject * StrokeVertex_getPoint( BPy_StrokeVertex *self ) {
148         Vec2f v( self->sv->getPoint() );
149         return Vector_from_Vec2f( v );
150 }
151
152 PyObject * StrokeVertex_attribute( BPy_StrokeVertex *self ) {
153         return BPy_StrokeAttribute_from_StrokeAttribute( self->sv->attribute() );
154 }
155
156 PyObject * StrokeVertex_curvilinearAbscissa( BPy_StrokeVertex *self ) {
157         return PyFloat_FromDouble( self->sv->curvilinearAbscissa() );
158 }
159
160 PyObject * StrokeVertex_strokeLength( BPy_StrokeVertex *self ) {
161         return PyFloat_FromDouble( self->sv->strokeLength() );
162 }
163
164 PyObject * StrokeVertex_u( BPy_StrokeVertex *self ) {
165         return PyFloat_FromDouble( self->sv->u() );
166 }
167
168
169 PyObject *StrokeVertex_setX( BPy_StrokeVertex *self , PyObject *args) {
170         double r;
171
172         if(!( PyArg_ParseTuple(args, "d", &r)  ))
173                 return NULL;
174
175         self->sv->setX( r );
176
177         Py_RETURN_NONE;
178 }
179
180 PyObject *StrokeVertex_setY( BPy_StrokeVertex *self , PyObject *args) {
181         double r;
182
183         if(!( PyArg_ParseTuple(args, "d", &r)  ))
184                 return NULL;
185
186         self->sv->setY( r );
187
188         Py_RETURN_NONE;
189 }
190
191
192 PyObject *StrokeVertex_setPoint( BPy_StrokeVertex *self , PyObject *args) {
193         PyObject *obj1 = 0, *obj2 = 0;
194
195         if(!( PyArg_ParseTuple(args, "O|O", &obj1, &obj2) ))
196                 return NULL;
197         
198         if( obj1 && !obj2 ){
199                 Vec2f *v = Vec2f_ptr_from_PyObject(obj1);
200                 if( !v ) {
201                         PyErr_SetString(PyExc_TypeError, "argument 1 must be a 2D vector (either a list of 2 elements or Vector)");
202                         return NULL;
203                 }
204                 self->sv->setPoint( *v );
205                 delete v; 
206         } else if( PyFloat_Check(obj1) && obj2 && PyFloat_Check(obj2) ){
207                 self->sv->setPoint( PyFloat_AsDouble(obj1), PyFloat_AsDouble(obj2) );
208         } else {
209                 PyErr_SetString(PyExc_TypeError, "invalid arguments");
210                 return NULL;
211         }
212
213         Py_RETURN_NONE;
214 }
215
216 PyObject *StrokeVertex_setAttribute( BPy_StrokeVertex *self , PyObject *args) {
217         PyObject *py_sa;
218
219         if(!( PyArg_ParseTuple(args, "O!", &StrokeAttribute_Type, &py_sa) ))
220                 return NULL;
221
222         self->sv->setAttribute(*( ((BPy_StrokeAttribute *) py_sa)->sa ));
223
224         Py_RETURN_NONE;
225 }
226
227 PyObject *StrokeVertex_setCurvilinearAbscissa( BPy_StrokeVertex *self , PyObject *args) {
228         double r;
229
230         if(!( PyArg_ParseTuple(args, "d", &r)  ))
231                 return NULL;
232
233         self->sv->setCurvilinearAbscissa( r );
234
235         Py_RETURN_NONE;
236 }
237
238
239 PyObject *StrokeVertex_setStrokeLength( BPy_StrokeVertex *self , PyObject *args) {
240         double r;
241
242         if(!( PyArg_ParseTuple(args, "d", &r)  ))
243                 return NULL;
244
245         self->sv->setStrokeLength( r );
246
247         Py_RETURN_NONE;
248 }
249
250 // real         operator[] (const int i) const
251 // real &       operator[] (const int i)
252
253 ///////////////////////////////////////////////////////////////////////////////////////////
254
255 #ifdef __cplusplus
256 }
257 #endif