soc-2008-mxcurioni: Added first batch of Iterators: AdjacencyIterator, Interface0DIt...
[blender-staging.git] / source / blender / freestyle / intern / python / Interface1D / BPy_Stroke.cpp
1 #include "BPy_Stroke.h"
2
3 #include "../BPy_Convert.h"
4 #include "../Interface0D/BPy_SVertex.h"
5 #include "../../stroke/StrokeIterators.h"
6
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10
11 ///////////////////////////////////////////////////////////////////////////////////////////
12
13 /*---------------  Python API function prototypes for Stroke instance  -----------*/
14 static int Stroke___init__(BPy_Stroke *self, PyObject *args, PyObject *kwds);
15
16 static PyObject * Stroke_ComputeSampling( BPy_Stroke *self, PyObject *args );
17 static PyObject * Stroke_Resample( BPy_Stroke *self, PyObject *args );
18 //static PyObject * Stroke_InsertVertex( BPy_Stroke *self, PyObject *args );
19 static PyObject * Stroke_RemoveVertex( BPy_Stroke *self, PyObject *args );
20 static PyObject * Stroke_getMediumType( BPy_Stroke *self );
21 static PyObject * Stroke_getTextureId( BPy_Stroke *self );
22 static PyObject * Stroke_hasTips( BPy_Stroke *self );
23 static PyObject * Stroke_setId( BPy_Stroke *self , PyObject *args);
24 static PyObject * Stroke_setLength( BPy_Stroke *self , PyObject *args);
25 static PyObject * Stroke_setMediumType( BPy_Stroke *self , PyObject *args);
26 static PyObject * Stroke_setTextureId( BPy_Stroke *self , PyObject *args);
27 static PyObject * Stroke_setTips( BPy_Stroke *self , PyObject *args);
28 static PyObject * Stroke_strokeVerticesSize( BPy_Stroke *self );
29 static PyObject * Stroke_getStrokeVertices( BPy_Stroke *self );
30
31 /*----------------------Stroke instance definitions ----------------------------*/
32 static PyMethodDef BPy_Stroke_methods[] = {     
33         {"ComputeSampling", ( PyCFunction ) Stroke_ComputeSampling, METH_VARARGS, "(int nVertices) Compute the sampling needed to get nVertices vertices. If the specified number of vertices is less than the actual number of vertices, the actual sampling value is returned."},
34                 {"Resample", ( PyCFunction ) Stroke_Resample, METH_VARARGS, "(float f | int n) Resampling method. If the argument is a float, Resamples the curve with a given sampling; if this sampling is < to the actual sampling value, no resampling is done. If the argument is an integer, Resamples the curve so that it eventually has n. That means it is going to add n-vertices_size, if vertices_size is the number of points we already have. Is vertices_size >= n, no resampling is done."},
35         {"RemoveVertex", ( PyCFunction ) Stroke_RemoveVertex, METH_VARARGS, "(StrokeVertex sv) Removes the stroke vertex sv from the stroke. The length and curvilinear abscissa are updated consequently."},
36         {"getMediumType", ( PyCFunction ) Stroke_getMediumType, METH_NOARGS, "() Returns the MediumType used for this Stroke."},
37         {"getTextureId", ( PyCFunction ) Stroke_getTextureId, METH_NOARGS, "() Returns the id of the texture used to simulate th marks system for this Stroke."},
38         {"hasTips", ( PyCFunction ) Stroke_hasTips, METH_NOARGS, "() Returns true if this Stroke uses a texture with tips, false otherwise."},
39         {"setId", ( PyCFunction ) Stroke_setId, METH_VARARGS, "(Id id) Sets the Id of the Stroke."},
40         {"setLength", ( PyCFunction ) Stroke_setLength, METH_VARARGS, "(float l) Sets the 2D length of the Stroke."},
41         {"setMediumType", ( PyCFunction ) Stroke_setMediumType, METH_VARARGS, "(MediumType mt) Sets the medium type that must be used for this Stroke."},
42         {"setTextureId", ( PyCFunction ) Stroke_setTextureId, METH_VARARGS, "(unsigned int id) Sets the texture id to be used to simulate the marks system for this Stroke."},
43         {"setTips", ( PyCFunction ) Stroke_setTips, METH_VARARGS, "(bool b) Sets the flag telling whether this stroke is using a texture with tips or not."},
44         {"strokeVerticesSize", ( PyCFunction ) Stroke_strokeVerticesSize, METH_NOARGS, "() Returns the number of StrokeVertex constituing the Stroke."},
45         {"getStrokeVertices", ( PyCFunction ) Stroke_getStrokeVertices, METH_NOARGS, "() Returns the stroke vertices. The difference with vertices() is that here we can iterate over points of the 1D element at a any given sampling. Indeed, for each iteration, a virtual point is created."},
46         //{"InsertVertex", ( PyCFunction ) Stroke_InsertVertex, METH_NOARGS, "(StrokeVertex sv, int i) Inserts the stroke vertex iVertex in the stroke before i. The length, curvilinear abscissa are updated consequently."},
47         {NULL, NULL, 0, NULL}
48 };
49
50 /*-----------------------BPy_Stroke type definition ------------------------------*/
51
52 PyTypeObject Stroke_Type = {
53         PyObject_HEAD_INIT( NULL ) 
54         0,                                                      /* ob_size */
55         "Stroke",                               /* tp_name */
56         sizeof( BPy_Stroke ),   /* tp_basicsize */
57         0,                                                      /* tp_itemsize */
58         
59         /* methods */
60         NULL,   /* tp_dealloc */
61         NULL,                                                   /* printfunc tp_print; */
62         NULL,                                                   /* getattrfunc tp_getattr; */
63         NULL,                                                   /* setattrfunc tp_setattr; */
64         NULL,                                                                           /* tp_compare */
65         NULL,                                   /* tp_repr */
66
67         /* Method suites for standard classes */
68
69         NULL,                       /* PyNumberMethods *tp_as_number; */
70         NULL,                       /* PySequenceMethods *tp_as_sequence; */
71         NULL,                       /* PyMappingMethods *tp_as_mapping; */
72
73         /* More standard operations (here for binary compatibility) */
74
75         NULL,                                           /* hashfunc tp_hash; */
76         NULL,                       /* ternaryfunc tp_call; */
77         NULL,                       /* reprfunc tp_str; */
78         NULL,                       /* getattrofunc tp_getattro; */
79         NULL,                       /* setattrofunc tp_setattro; */
80
81         /* Functions to access object as input/output buffer */
82         NULL,                       /* PyBufferProcs *tp_as_buffer; */
83
84   /*** Flags to define presence of optional/expanded features ***/
85         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,               /* long tp_flags; */
86
87         NULL,                       /*  char *tp_doc;  Documentation string */
88   /*** Assigned meaning in release 2.0 ***/
89         /* call function for all accessible objects */
90         NULL,                       /* traverseproc tp_traverse; */
91
92         /* delete references to contained objects */
93         NULL,                       /* inquiry tp_clear; */
94
95   /***  Assigned meaning in release 2.1 ***/
96   /*** rich comparisons ***/
97         NULL,                       /* richcmpfunc tp_richcompare; */
98
99   /***  weak reference enabler ***/
100         0,                          /* long tp_weaklistoffset; */
101
102   /*** Added in release 2.2 ***/
103         /*   Iterators */
104         NULL,                       /* getiterfunc tp_iter; */
105         NULL,                       /* iternextfunc tp_iternext; */
106
107   /*** Attribute descriptor and subclassing stuff ***/
108         BPy_Stroke_methods,     /* struct PyMethodDef *tp_methods; */
109         NULL,                           /* struct PyMemberDef *tp_members; */
110         NULL,                                           /* struct PyGetSetDef *tp_getset; */
111         &Interface1D_Type,                              /* struct _typeobject *tp_base; */
112         NULL,                                                   /* PyObject *tp_dict; */
113         NULL,                                                   /* descrgetfunc tp_descr_get; */
114         NULL,                                                   /* descrsetfunc tp_descr_set; */
115         0,                              /* long tp_dictoffset; */
116         (initproc)Stroke___init__,                              /* initproc tp_init; */
117         NULL,                                                   /* allocfunc tp_alloc; */
118         NULL,           /* newfunc tp_new; */
119         
120         /*  Low-level free-memory routine */
121         NULL,                       /* freefunc tp_free;  */
122         
123         /* For PyObject_IS_GC */
124         NULL,                       /* inquiry tp_is_gc;  */
125         NULL,                       /* PyObject *tp_bases; */
126         
127         /* method resolution order */
128         NULL,                       /* PyObject *tp_mro;  */
129         NULL,                       /* PyObject *tp_cache; */
130         NULL,                       /* PyObject *tp_subclasses; */
131         NULL,                       /* PyObject *tp_weaklist; */
132         NULL
133 };
134
135 //-------------------MODULE INITIALIZATION--------------------------------
136
137
138 //------------------------INSTANCE METHODS ----------------------------------
139
140 // Stroke ()
141 // template<class InputVertexIterator> Stroke (InputVertexIterator iBegin, InputVertexIterator iEnd)
142
143 int Stroke___init__(BPy_Stroke *self, PyObject *args, PyObject *kwds)
144 {
145         PyObject *obj1 = 0, *obj2 = 0;
146
147     if (! PyArg_ParseTuple(args, "|OO", &obj1) )
148         return -1;
149
150         if( !obj1 && !obj2 ){
151                 self->s = new Stroke();
152
153         // template<class InputVertexIterator> Stroke (InputVertexIterator iBegin, InputVertexIterator iEnd)
154         //
155         // pb: - need to be able to switch representation: InputVertexIterator <=> position
156         //     - is it even used ? not even in SWIG version
157         } else {
158                 return -1;
159         }
160
161         self->py_if1D.if1D = self->s;
162
163         return 0;
164 }
165
166 PyObject * Stroke_ComputeSampling( BPy_Stroke *self, PyObject *args ) { 
167         int i;
168
169         if(!( PyArg_ParseTuple(args, "i", &i)  )) {
170                 cout << "ERROR: Stroke_ComputeSampling" << endl;
171                 Py_RETURN_NONE;
172         }
173
174         return PyFloat_FromDouble( self->s->ComputeSampling( i ) );
175 }
176
177 PyObject * Stroke_Resample( BPy_Stroke *self, PyObject *args ) {        
178         PyObject *obj;
179
180         if(!( PyArg_ParseTuple(args, "O", &obj) )) {
181                 cout << "ERROR: Stroke_Resample" << endl;
182                 Py_RETURN_NONE;
183         }
184
185         if( PyInt_Check(obj) )
186                 self->s->Resample( (int) PyInt_AsLong(obj) );
187         else if( PyFloat_Check(obj) )
188                 self->s->Resample( (float) PyFloat_AsDouble(obj) );
189                 
190         Py_RETURN_NONE;
191 }
192
193
194 PyObject * Stroke_RemoveVertex( BPy_Stroke *self, PyObject *args ) {    
195         PyObject *py_sv;
196
197         if(!( PyArg_ParseTuple(args, "O", &py_sv) )) {
198                 cout << "ERROR: Stroke_RemoveVertex" << endl;
199                 Py_RETURN_NONE;
200         }
201
202         if( BPy_StrokeVertex_Check(py_sv) && ((BPy_StrokeVertex *) py_sv)->sv )
203                 self->s->RemoveVertex( ((BPy_StrokeVertex *) py_sv)->sv );
204                 
205         Py_RETURN_NONE;
206 }
207
208 // pb: 'iterator <=> list' correspondence
209 // void         InsertVertex (StrokeVertex *iVertex, StrokeInternal::StrokeVertexIterator next)
210
211 PyObject * Stroke_getMediumType( BPy_Stroke *self ) {   
212         return BPy_MediumType_from_MediumType( self->s->getMediumType() );
213 }
214
215 PyObject * Stroke_getTextureId( BPy_Stroke *self ) {    
216         return PyInt_FromLong( self->s->getTextureId() );
217 }
218
219 PyObject * Stroke_hasTips( BPy_Stroke *self ) { 
220         return PyBool_from_bool( self->s->hasTips() );
221 }
222
223
224 PyObject *Stroke_setId( BPy_Stroke *self , PyObject *args) {
225         PyObject *py_id;
226
227         if(!( PyArg_ParseTuple(args, "O", &py_id) && BPy_Id_Check(py_id)  )) {
228                 cout << "ERROR: Stroke_setId" << endl;
229                 Py_RETURN_NONE;
230         }
231
232         if( ((BPy_Id *) py_id)->id )
233                 self->s->setId(*( ((BPy_Id *) py_id)->id ));
234
235         Py_RETURN_NONE;
236 }
237
238 PyObject *Stroke_setLength( BPy_Stroke *self , PyObject *args) {
239         float f;
240
241         if(!( PyArg_ParseTuple(args, "f", &f) )) {
242                 cout << "ERROR: Stroke_setLength" << endl;
243                 Py_RETURN_NONE;
244         }
245
246         self->s->setLength( f );
247
248         Py_RETURN_NONE;
249 }
250
251 PyObject *Stroke_setMediumType( BPy_Stroke *self , PyObject *args) {
252         PyObject *py_mt;
253
254         if(!( PyArg_ParseTuple(args, "O", &py_mt) && BPy_MediumType_Check(py_mt)  )) {
255                 cout << "ERROR: Stroke_setMediumType" << endl;
256                 Py_RETURN_NONE;
257         }
258
259         self->s->setMediumType( static_cast<Stroke::MediumType>(PyInt_AsLong(py_mt)) );
260
261         Py_RETURN_NONE;
262 }
263
264 PyObject *Stroke_setTextureId( BPy_Stroke *self , PyObject *args) {
265         unsigned int i;
266
267         if(!( PyArg_ParseTuple(args, "I", &i) )) {
268                 cout << "ERROR: Stroke_setTextureId" << endl;
269                 Py_RETURN_NONE;
270         }
271
272         self->s->setTextureId( i );
273
274         Py_RETURN_NONE;
275 }
276
277 PyObject *Stroke_setTips( BPy_Stroke *self , PyObject *args) {
278         PyObject *py_b;
279
280         if(!( PyArg_ParseTuple(args, "O", &py_b) && PyBool_Check(py_b)  )) {
281                 cout << "ERROR: Stroke_setTips" << endl;
282                 Py_RETURN_NONE;
283         }
284
285         self->s->setTips( bool_from_PyBool(py_b) );
286
287         Py_RETURN_NONE;
288 }
289
290
291 PyObject * Stroke_strokeVerticesSize( BPy_Stroke *self ) {      
292         return PyInt_FromLong( self->s->strokeVerticesSize() );
293 }
294
295
296 PyObject *Stroke_getStrokeVertices( BPy_Stroke *self ) {
297         PyObject *py_stroke_vertices = PyList_New(NULL);
298                 
299         for( StrokeInternal::StrokeVertexIterator it = self->s->strokeVerticesBegin();
300                 it != self->s->strokeVerticesEnd();
301                 it++ ) {
302                 PyList_Append( py_stroke_vertices, BPy_StrokeVertex_from_StrokeVertex( *it ) );
303         }
304         
305         return py_stroke_vertices;
306 }
307
308
309
310 ///////////////////////////////////////////////////////////////////////////////////////////
311
312 #ifdef __cplusplus
313 }
314 #endif