soc-2008-mxcurioni: made considerable changes to support cross-language polymorphism...
[blender-staging.git] / source / blender / freestyle / intern / python / BPy_Interface1D.cpp
1 #include "BPy_Interface1D.h"
2
3 #include "BPy_Convert.h"
4 #include "Interface1D/BPy_FrsCurve.h"
5 #include "Interface1D/Curve/BPy_Chain.h"
6 #include "Interface1D/BPy_FEdge.h"
7 #include "Interface1D/FEdge/BPy_FEdgeSharp.h"
8 #include "Interface1D/FEdge/BPy_FEdgeSmooth.h"
9 #include "Interface1D/BPy_Stroke.h"
10 #include "Interface1D/BPy_ViewEdge.h"
11
12 #include "BPy_MediumType.h"
13
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17
18 ///////////////////////////////////////////////////////////////////////////////////////////
19
20 /*---------------  Python API function prototypes for Interface1D instance  -----------*/
21 static int Interface1D___init__(BPy_Interface1D *self, PyObject *args, PyObject *kwds);
22 static void Interface1D___dealloc__(BPy_Interface1D *self);
23 static PyObject * Interface1D___repr__(BPy_Interface1D *self);
24
25 static PyObject *Interface1D_getExactTypeName( BPy_Interface1D *self );
26 static PyObject *Interface1D_getVertices( BPy_Interface1D *self );
27 static PyObject *Interface1D_getPoints( BPy_Interface1D *self );
28 static PyObject *Interface1D_getLength2D( BPy_Interface1D *self );
29 static PyObject *Interface1D_getId( BPy_Interface1D *self );
30 static PyObject *Interface1D_getNature( BPy_Interface1D *self );
31 static PyObject *Interface1D_getTimeStamp( BPy_Interface1D *self );
32 static PyObject *Interface1D_setTimeStamp( BPy_Interface1D *self, PyObject *args);
33 static PyObject * Interface1D_verticesBegin( BPy_Interface1D *self );
34 static PyObject * Interface1D_verticesEnd( BPy_Interface1D *self );
35 static PyObject * Interface1D_pointsBegin( BPy_Interface1D *self, PyObject *args );
36 static PyObject * Interface1D_pointsEnd( BPy_Interface1D *self, PyObject *args );
37
38 /*----------------------Interface1D instance definitions ----------------------------*/
39 static PyMethodDef BPy_Interface1D_methods[] = {
40         {"getExactTypeName", ( PyCFunction ) Interface1D_getExactTypeName, METH_NOARGS, "( )Returns the string of the name of the interface."},
41         {"getVertices", ( PyCFunction ) Interface1D_getVertices, METH_NOARGS, "Returns the vertices"},
42         {"getPoints", ( PyCFunction ) Interface1D_getPoints, METH_NOARGS, "Returns the points. The difference with getVertices() is that here we can iterate over points of the 1D element at any given sampling. At each call, a virtual point is created."},
43         {"getLength2D", ( PyCFunction ) Interface1D_getLength2D, METH_NOARGS, "Returns the 2D length of the 1D element"},
44         {"getId", ( PyCFunction ) Interface1D_getId, METH_NOARGS, "Returns the Id of the 1D element"},
45         {"getNature", ( PyCFunction ) Interface1D_getNature, METH_NOARGS, "Returns the nature of the 1D element"},
46         {"getTimeStamp", ( PyCFunction ) Interface1D_getTimeStamp, METH_NOARGS, "Returns the time stamp of the 1D element. Mainly used for selection"},
47         {"setTimeStamp", ( PyCFunction ) Interface1D_setTimeStamp, METH_VARARGS, "Sets the time stamp for the 1D element"},
48         {"verticesBegin", ( PyCFunction ) Interface1D_verticesBegin, METH_NOARGS, ""},
49         {"verticesEnd", ( PyCFunction ) Interface1D_verticesEnd, METH_NOARGS, ""},
50         {"pointsBegin", ( PyCFunction ) Interface1D_pointsBegin, METH_VARARGS, ""},
51         {"pointsEnd", ( PyCFunction ) Interface1D_pointsEnd, METH_VARARGS, ""},
52
53         {NULL, NULL, 0, NULL}
54 };
55
56 /*-----------------------BPy_Interface1D type definition ------------------------------*/
57
58 PyTypeObject Interface1D_Type = {
59         PyObject_HEAD_INIT( NULL ) 
60         0,                                                      /* ob_size */
61         "Interface1D",                          /* tp_name */
62         sizeof( BPy_Interface1D ),      /* tp_basicsize */
63         0,                                                      /* tp_itemsize */
64         
65         /* methods */
66         (destructor)Interface1D___dealloc__,    /* tp_dealloc */
67         NULL,                                                   /* printfunc tp_print; */
68         NULL,                                                   /* getattrfunc tp_getattr; */
69         NULL,                                                   /* setattrfunc tp_setattr; */
70         NULL,                                                                           /* tp_compare */
71         (reprfunc)Interface1D___repr__,                                 /* tp_repr */
72
73         /* Method suites for standard classes */
74
75         NULL,                       /* PyNumberMethods *tp_as_number; */
76         NULL,                       /* PySequenceMethods *tp_as_sequence; */
77         NULL,                       /* PyMappingMethods *tp_as_mapping; */
78
79         /* More standard operations (here for binary compatibility) */
80
81         NULL,                                           /* hashfunc tp_hash; */
82         NULL,                       /* ternaryfunc tp_call; */
83         NULL,                       /* reprfunc tp_str; */
84         NULL,                       /* getattrofunc tp_getattro; */
85         NULL,                       /* setattrofunc tp_setattro; */
86
87         /* Functions to access object as input/output buffer */
88         NULL,                       /* PyBufferProcs *tp_as_buffer; */
89
90   /*** Flags to define presence of optional/expanded features ***/
91         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,               /* long tp_flags; */
92
93         NULL,                       /*  char *tp_doc;  Documentation string */
94   /*** Assigned meaning in release 2.0 ***/
95         /* call function for all accessible objects */
96         NULL,                       /* traverseproc tp_traverse; */
97
98         /* delete references to contained objects */
99         NULL,                       /* inquiry tp_clear; */
100
101   /***  Assigned meaning in release 2.1 ***/
102   /*** rich comparisons ***/
103         NULL,                       /* richcmpfunc tp_richcompare; */
104
105   /***  weak reference enabler ***/
106         0,                          /* long tp_weaklistoffset; */
107
108   /*** Added in release 2.2 ***/
109         /*   Iterators */
110         NULL,                       /* getiterfunc tp_iter; */
111         NULL,                       /* iternextfunc tp_iternext; */
112
113   /*** Attribute descriptor and subclassing stuff ***/
114         BPy_Interface1D_methods,        /* struct PyMethodDef *tp_methods; */
115         NULL,                           /* struct PyMemberDef *tp_members; */
116         NULL,                                           /* struct PyGetSetDef *tp_getset; */
117         NULL,                                                   /* struct _typeobject *tp_base; */
118         NULL,                                                   /* PyObject *tp_dict; */
119         NULL,                                                   /* descrgetfunc tp_descr_get; */
120         NULL,                                                   /* descrsetfunc tp_descr_set; */
121         0,                              /* long tp_dictoffset; */
122         (initproc)Interface1D___init__,                         /* initproc tp_init; */
123         NULL,                                                   /* allocfunc tp_alloc; */
124         PyType_GenericNew,              /* newfunc tp_new; */
125         
126         /*  Low-level free-memory routine */
127         NULL,                       /* freefunc tp_free;  */
128         
129         /* For PyObject_IS_GC */
130         NULL,                       /* inquiry tp_is_gc;  */
131         NULL,                       /* PyObject *tp_bases; */
132         
133         /* method resolution order */
134         NULL,                       /* PyObject *tp_mro;  */
135         NULL,                       /* PyObject *tp_cache; */
136         NULL,                       /* PyObject *tp_subclasses; */
137         NULL,                       /* PyObject *tp_weaklist; */
138         NULL
139 };
140
141 //-------------------MODULE INITIALIZATION--------------------------------
142 PyMODINIT_FUNC Interface1D_Init( PyObject *module )
143 {
144         PyObject *tmp;
145         
146         if( module == NULL )
147                 return;
148
149         if( PyType_Ready( &Interface1D_Type ) < 0 )
150                 return;
151         Py_INCREF( &Interface1D_Type );
152         PyModule_AddObject(module, "Interface1D", (PyObject *)&Interface1D_Type);
153         
154         if( PyType_Ready( &FrsCurve_Type ) < 0 )
155                 return;
156         Py_INCREF( &FrsCurve_Type );
157         PyModule_AddObject(module, "FrsCurve", (PyObject *)&FrsCurve_Type);
158
159         if( PyType_Ready( &Chain_Type ) < 0 )
160                 return;
161         Py_INCREF( &Chain_Type );
162         PyModule_AddObject(module, "Chain", (PyObject *)&Chain_Type);
163         
164         if( PyType_Ready( &FEdge_Type ) < 0 )
165                 return;
166         Py_INCREF( &FEdge_Type );
167         PyModule_AddObject(module, "FEdge", (PyObject *)&FEdge_Type);
168
169         if( PyType_Ready( &FEdgeSharp_Type ) < 0 )
170                 return;
171         Py_INCREF( &FEdgeSharp_Type );
172         PyModule_AddObject(module, "FEdgeSharp", (PyObject *)&FEdgeSharp_Type);
173         
174         if( PyType_Ready( &FEdgeSmooth_Type ) < 0 )
175                 return;
176         Py_INCREF( &FEdgeSmooth_Type );
177         PyModule_AddObject(module, "FEdgeSmooth", (PyObject *)&FEdgeSmooth_Type);
178
179         if( PyType_Ready( &Stroke_Type ) < 0 )
180                 return;
181         Py_INCREF( &Stroke_Type );
182         PyModule_AddObject(module, "Stroke", (PyObject *)&Stroke_Type);
183
184         tmp = BPy_MediumType_from_MediumType( Stroke::DRY_MEDIUM ); PyDict_SetItemString( Stroke_Type.tp_dict, "DRY_MEDIUM", tmp); Py_DECREF(tmp);
185         tmp = BPy_MediumType_from_MediumType( Stroke::HUMID_MEDIUM ); PyDict_SetItemString( Stroke_Type.tp_dict, "HUMID_MEDIUM", tmp); Py_DECREF(tmp);
186         tmp = BPy_MediumType_from_MediumType( Stroke::OPAQUE_MEDIUM ); PyDict_SetItemString( Stroke_Type.tp_dict, "OPAQUE_MEDIUM", tmp); Py_DECREF(tmp);
187
188         if( PyType_Ready( &ViewEdge_Type ) < 0 )
189                 return;
190         Py_INCREF( &ViewEdge_Type );
191         PyModule_AddObject(module, "ViewEdge", (PyObject *)&ViewEdge_Type);
192
193         
194 }
195
196 //------------------------INSTANCE METHODS ----------------------------------
197
198 int Interface1D___init__(BPy_Interface1D *self, PyObject *args, PyObject *kwds)
199 {
200         self->if1D = new Interface1D();
201         self->if1D->py_if1D = (PyObject *) self;
202         return 0;
203 }
204
205 void Interface1D___dealloc__(BPy_Interface1D* self)
206 {
207         delete self->if1D;
208     self->ob_type->tp_free((PyObject*)self);
209 }
210
211 PyObject * Interface1D___repr__(BPy_Interface1D* self)
212 {
213     return PyString_FromFormat("type: %s - address: %p", self->if1D->getExactTypeName().c_str(), self->if1D );
214 }
215
216 PyObject *Interface1D_getExactTypeName( BPy_Interface1D *self ) {
217         return PyString_FromString( self->if1D->getExactTypeName().c_str() );   
218 }
219
220 PyObject *Interface1D_getVertices( BPy_Interface1D *self ) {
221         return PyList_New(0);
222 }
223
224 PyObject *Interface1D_getPoints( BPy_Interface1D *self ) {
225         return PyList_New(0);
226 }
227
228 PyObject *Interface1D_getLength2D( BPy_Interface1D *self ) {
229         return PyFloat_FromDouble( (double) self->if1D->getLength2D() );
230 }
231
232 PyObject *Interface1D_getId( BPy_Interface1D *self ) {
233         Id id( self->if1D->getId() );
234         return BPy_Id_from_Id( id );
235 }
236
237 PyObject *Interface1D_getNature( BPy_Interface1D *self ) {
238         return BPy_Nature_from_Nature( self->if1D->getNature() );
239 }
240
241 PyObject *Interface1D_getTimeStamp( BPy_Interface1D *self ) {
242         return PyInt_FromLong( self->if1D->getTimeStamp() );
243 }
244
245 PyObject *Interface1D_setTimeStamp( BPy_Interface1D *self, PyObject *args) {
246         int timestamp = 0 ;
247
248         if( !PyArg_ParseTuple(args, (char *)"i", &timestamp) ) {
249                 cout << "ERROR: Interface1D_setTimeStamp" << endl;
250                 Py_RETURN_NONE;
251         }
252         
253         self->if1D->setTimeStamp( timestamp );
254
255         Py_RETURN_NONE;
256 }
257
258 PyObject * Interface1D_verticesBegin( BPy_Interface1D *self ) {
259         Interface0DIterator if0D_it( self->if1D->verticesBegin() );
260         return BPy_Interface0DIterator_from_Interface0DIterator( if0D_it );
261 }
262
263 PyObject * Interface1D_verticesEnd( BPy_Interface1D *self ) {
264         Interface0DIterator if0D_it( self->if1D->verticesEnd() );
265         return BPy_Interface0DIterator_from_Interface0DIterator( if0D_it );
266 }
267
268
269 PyObject * Interface1D_pointsBegin( BPy_Interface1D *self, PyObject *args ) {
270         float f = 0;
271
272         if(!( PyArg_ParseTuple(args, "|f", &f)  )) {
273                 cout << "ERROR: Interface1D_pointsBegin" << endl;
274                 Py_RETURN_NONE;
275         }
276         
277         Interface0DIterator if0D_it( self->if1D->pointsBegin(f) );
278         return BPy_Interface0DIterator_from_Interface0DIterator( if0D_it );
279 }
280
281 PyObject * Interface1D_pointsEnd( BPy_Interface1D *self, PyObject *args ) {
282         float f = 0;
283
284         if(!( PyArg_ParseTuple(args, "|f", &f)  )) {
285                 cout << "ERROR: Interface1D_pointsEnd" << endl;
286                 Py_RETURN_NONE;
287         }
288         
289         Interface0DIterator if0D_it( self->if1D->pointsEnd(f) );
290         return BPy_Interface0DIterator_from_Interface0DIterator( if0D_it );
291 }
292
293 ///////////////////////////////////////////////////////////////////////////////////////////
294
295 #ifdef __cplusplus
296 }
297 #endif
298
299