Made the Freestyle Python API compatible with Python 3.
[blender.git] / source / blender / freestyle / intern / python / Interface0D / BPy_SVertex.cpp
1 #include "BPy_SVertex.h"
2
3 #include "../BPy_Convert.h"
4 #include "../BPy_Id.h"
5 #include "../Interface1D/BPy_FEdge.h"
6
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10
11 ///////////////////////////////////////////////////////////////////////////////////////////
12
13 /*---------------  Python API function prototypes for SVertex instance  -----------*/
14 static int SVertex___init__(BPy_SVertex *self, PyObject *args, PyObject *kwds);
15 static PyObject * SVertex___copy__( BPy_SVertex *self );
16 static PyObject * SVertex_normals( BPy_SVertex *self );
17 static PyObject * SVertex_normalsSize( BPy_SVertex *self );
18 static PyObject * SVertex_viewvertex( BPy_SVertex *self );
19 static PyObject * SVertex_setPoint3D( BPy_SVertex *self , PyObject *args);
20 static PyObject * SVertex_setPoint2D( BPy_SVertex *self , PyObject *args);
21 static PyObject * SVertex_AddNormal( BPy_SVertex *self , PyObject *args);
22 static PyObject * SVertex_setId( BPy_SVertex *self , PyObject *args);
23 static PyObject *SVertex_AddFEdge( BPy_SVertex *self , PyObject *args);
24
25 /*----------------------SVertex instance definitions ----------------------------*/
26 static PyMethodDef BPy_SVertex_methods[] = {
27         {"__copy__", ( PyCFunction ) SVertex___copy__, METH_NOARGS, "() Cloning method."},
28         {"normals", ( PyCFunction ) SVertex_normals, METH_NOARGS, "Returns the normals for this Vertex as a list. In a smooth surface, a vertex has exactly one normal. In a sharp surface, a vertex can have any number of normals."},
29         {"normalsSize", ( PyCFunction ) SVertex_normalsSize, METH_NOARGS, "Returns the number of different normals for this vertex." },
30         {"viewvertex", ( PyCFunction ) SVertex_viewvertex, METH_NOARGS, "If this SVertex is also a ViewVertex, this method returns a pointer onto this ViewVertex. 0 is returned otherwise." }, 
31         {"setPoint3D", ( PyCFunction ) SVertex_setPoint3D, METH_VARARGS, "Sets the 3D coordinates of the SVertex." },
32         {"setPoint2D", ( PyCFunction ) SVertex_setPoint2D, METH_VARARGS, "Sets the 3D projected coordinates of the SVertex." },
33         {"AddNormal", ( PyCFunction ) SVertex_AddNormal, METH_VARARGS, "Adds a normal to the Svertex's set of normals. If the same normal is already in the set, nothing changes." },
34         {"setId", ( PyCFunction ) SVertex_setId, METH_VARARGS, "Sets the Id." },
35         {"AddFEdge", ( PyCFunction ) SVertex_AddFEdge, METH_VARARGS, "Add an FEdge to the list of edges emanating from this SVertex." },        
36         {NULL, NULL, 0, NULL}
37 };
38
39 /*-----------------------BPy_SVertex type definition ------------------------------*/
40
41 PyTypeObject SVertex_Type = {
42         PyObject_HEAD_INIT(NULL)
43         "SVertex",                      /* tp_name */
44         sizeof(BPy_SVertex),            /* tp_basicsize */
45         0,                              /* tp_itemsize */
46         0,                              /* tp_dealloc */
47         0,                              /* tp_print */
48         0,                              /* tp_getattr */
49         0,                              /* tp_setattr */
50         0,                              /* tp_reserved */
51         0,                              /* tp_repr */
52         0,                              /* tp_as_number */
53         0,                              /* tp_as_sequence */
54         0,                              /* tp_as_mapping */
55         0,                              /* tp_hash  */
56         0,                              /* tp_call */
57         0,                              /* tp_str */
58         0,                              /* tp_getattro */
59         0,                              /* tp_setattro */
60         0,                              /* tp_as_buffer */
61         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
62         "SVertex objects",              /* tp_doc */
63         0,                              /* tp_traverse */
64         0,                              /* tp_clear */
65         0,                              /* tp_richcompare */
66         0,                              /* tp_weaklistoffset */
67         0,                              /* tp_iter */
68         0,                              /* tp_iternext */
69         BPy_SVertex_methods,            /* tp_methods */
70         0,                              /* tp_members */
71         0,                              /* tp_getset */
72         &Interface0D_Type,              /* tp_base */
73         0,                              /* tp_dict */
74         0,                              /* tp_descr_get */
75         0,                              /* tp_descr_set */
76         0,                              /* tp_dictoffset */
77         (initproc)SVertex___init__,     /* tp_init */
78         0,                              /* tp_alloc */
79         0,                              /* tp_new */
80 };
81
82 //------------------------INSTANCE METHODS ----------------------------------
83
84 int SVertex___init__(BPy_SVertex *self, PyObject *args, PyObject *kwds)
85 {
86         PyObject *py_point = 0;
87         BPy_Id *py_id = 0;
88         
89
90         if (! PyArg_ParseTuple(args, "|OO!", &py_point, &Id_Type, &py_id) )
91         return -1;
92         
93         if( !py_point ) {
94                 self->sv = new SVertex();
95
96         } else if( !py_id && BPy_SVertex_Check(py_point) ) {
97                 self->sv = new SVertex( *(((BPy_SVertex *)py_point)->sv) );
98
99         } else if( py_point && py_id ) {
100                 Vec3r *v = Vec3r_ptr_from_PyObject(py_point);
101                 if( !v ) {
102                         PyErr_SetString(PyExc_TypeError, "argument 1 must be a 3D vector (either a list of 3 elements or Vector)");
103                         return -1;
104                 }
105                 self->sv = new SVertex( *v, *(py_id->id) );
106                 delete v;
107
108         } else {
109                 PyErr_SetString(PyExc_TypeError, "invalid argument(s)");
110                 return -1;
111         }
112
113         self->py_if0D.if0D = self->sv;
114         self->py_if0D.borrowed = 0;
115         
116         return 0;
117 }
118
119 PyObject * SVertex___copy__( BPy_SVertex *self ) {
120         BPy_SVertex *py_svertex;
121         
122         py_svertex = (BPy_SVertex *) SVertex_Type.tp_new( &SVertex_Type, 0, 0 );
123         
124         py_svertex->sv = self->sv->duplicate();
125         py_svertex->py_if0D.if0D = py_svertex->sv;
126         py_svertex->py_if0D.borrowed = 0;
127
128         return (PyObject *) py_svertex;
129 }
130
131
132 PyObject * SVertex_normals( BPy_SVertex *self ) {
133         PyObject *py_normals; 
134         set< Vec3r > normals;
135         
136         py_normals  = PyList_New(NULL);
137         normals = self->sv->normals();
138                 
139         for( set< Vec3r >::iterator set_iterator = normals.begin(); set_iterator != normals.end(); set_iterator++ ) {
140                 Vec3r v( *set_iterator );
141                 PyList_Append( py_normals, Vector_from_Vec3r(v) );
142         }
143         
144         return py_normals;
145 }
146
147 PyObject * SVertex_normalsSize( BPy_SVertex *self ) {
148         return PyLong_FromLong( self->sv->normalsSize() );
149 }
150
151 PyObject * SVertex_viewvertex( BPy_SVertex *self ) {
152         ViewVertex *vv = self->sv->viewvertex();
153         if( vv )
154                 return Any_BPy_ViewVertex_from_ViewVertex( *vv );
155
156         Py_RETURN_NONE;
157 }
158
159 PyObject *SVertex_setPoint3D( BPy_SVertex *self , PyObject *args) {
160         PyObject *py_point;
161
162         if(!( PyArg_ParseTuple(args, "O", &py_point) ))
163                 return NULL;
164         Vec3r *v = Vec3r_ptr_from_PyObject(py_point);
165         if( !v ) {
166                 PyErr_SetString(PyExc_TypeError, "argument 1 must be a 3D vector (either a list of 3 elements or Vector)");
167                 return NULL;
168         }
169         self->sv->setPoint3D( *v );
170         delete v;
171
172         Py_RETURN_NONE;
173 }
174
175 PyObject *SVertex_setPoint2D( BPy_SVertex *self , PyObject *args) {
176         PyObject *py_point;
177
178         if(!( PyArg_ParseTuple(args, "O", &py_point) ))
179                 return NULL;
180         Vec3r *v = Vec3r_ptr_from_PyObject(py_point);
181         if( !v ) {
182                 PyErr_SetString(PyExc_TypeError, "argument 1 must be a 3D vector (either a list of 3 elements or Vector)");
183                 return NULL;
184         }
185         self->sv->setPoint2D( *v );
186         delete v;
187
188         Py_RETURN_NONE;
189 }
190
191 PyObject *SVertex_AddNormal( BPy_SVertex *self , PyObject *args) {
192         PyObject *py_normal;
193
194         if(!( PyArg_ParseTuple(args, "O", &py_normal) ))
195                 return NULL;
196         Vec3r *n = Vec3r_ptr_from_PyObject(py_normal);
197         if( !n ) {
198                 PyErr_SetString(PyExc_TypeError, "argument 1 must be a 3D vector (either a list of 3 elements or Vector)");
199                 return NULL;
200         }
201         self->sv->AddNormal( *n );
202         delete n;
203
204         Py_RETURN_NONE;
205 }
206
207 PyObject *SVertex_setId( BPy_SVertex *self , PyObject *args) {
208         BPy_Id *py_id;
209
210         if( !PyArg_ParseTuple(args, "O!", &Id_Type, &py_id) )
211                 return NULL;
212
213         self->sv->setId( *(py_id->id) );
214
215         Py_RETURN_NONE;
216 }
217
218 PyObject *SVertex_AddFEdge( BPy_SVertex *self , PyObject *args) {
219         PyObject *py_fe;
220
221         if(!( PyArg_ParseTuple(args, "O!", &FEdge_Type, &py_fe) ))
222                 return NULL;
223         
224         self->sv->AddFEdge( ((BPy_FEdge *) py_fe)->fe );
225
226         Py_RETURN_NONE;
227 }
228
229
230 // virtual bool         operator== (const SVertex &iBrother)
231 // ViewVertex *         viewvertex ()
232
233
234 ///////////////////////////////////////////////////////////////////////////////////////////
235
236 #ifdef __cplusplus
237 }
238 #endif
239