Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BPy_Operators.cpp « python « intern « freestyle « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 49bddcbf7c7eb9597c9300b424b9a33dd900eb11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#include "BPy_Operators.h"

#include "BPy_BinaryPredicate1D.h"
#include "BPy_UnaryPredicate0D.h"
#include "BPy_UnaryPredicate1D.h"
#include "UnaryFunction0D/BPy_UnaryFunction0DDouble.h"
#include "UnaryFunction1D/BPy_UnaryFunction1DVoid.h"
#include "Iterator/BPy_ViewEdgeIterator.h"
#include "Iterator/BPy_ChainingIterator.h"
#include "BPy_StrokeShader.h"

#ifdef __cplusplus
extern "C" {
#endif

///////////////////////////////////////////////////////////////////////////////////////////

/*---------------  Python API function prototypes for Operators instance  -----------*/
static void Operators___dealloc__(BPy_Operators *self);

static PyObject * Operators_select(BPy_Operators* self, PyObject *args);
static PyObject * Operators_chain(BPy_Operators* self, PyObject *args);
static PyObject * Operators_bidirectionalChain(BPy_Operators* self, PyObject *args);
static PyObject * Operators_sequentialSplit(BPy_Operators* self, PyObject *args);
static PyObject * Operators_recursiveSplit(BPy_Operators* self, PyObject *args);
static PyObject * Operators_sort(BPy_Operators* self, PyObject *args);
static PyObject * Operators_create(BPy_Operators* self, PyObject *args);
static PyObject * Operators_getViewEdgesSize( BPy_Operators* self);
static PyObject * Operators_getChainsSize( BPy_Operators* self);
static PyObject * Operators_getStrokesSize( BPy_Operators* self);

/*----------------------Operators instance definitions ----------------------------*/
static PyMethodDef BPy_Operators_methods[] = {
	{"select", ( PyCFunction ) Operators_select, METH_VARARGS | METH_STATIC, 
	"select operator"},
	{"chain", ( PyCFunction ) Operators_chain, METH_VARARGS | METH_STATIC,
	 "chain operator"},
	{"bidirectionalChain", ( PyCFunction ) Operators_bidirectionalChain, METH_VARARGS | METH_STATIC,
	 "bidirectionalChain operator"},
	{"sequentialSplit", ( PyCFunction ) Operators_sequentialSplit, METH_VARARGS | METH_STATIC,
	 "sequentialSplit operator"},
	{"recursiveSplit", ( PyCFunction ) Operators_recursiveSplit, METH_VARARGS | METH_STATIC, 
	"recursiveSplit operator"},
	{"sort", ( PyCFunction ) Operators_sort, METH_VARARGS | METH_STATIC, 
	"sort operator"},
	{"create", ( PyCFunction ) Operators_create, METH_VARARGS | METH_STATIC, 
	"create operator"},
	{"getViewEdgesSize", ( PyCFunction ) Operators_getViewEdgesSize, METH_NOARGS | METH_STATIC, ""},
	{"getChainsSize", ( PyCFunction ) Operators_getChainsSize, METH_NOARGS | METH_STATIC, ""},
	{"getStrokesSize", ( PyCFunction ) Operators_getStrokesSize, METH_NOARGS | METH_STATIC, ""},
	{NULL, NULL, 0, NULL}
};

/*-----------------------BPy_Operators type definition ------------------------------*/

PyTypeObject Operators_Type = {
	PyObject_HEAD_INIT( NULL ) 
	0,							/* ob_size */
	"Operators",				/* tp_name */
	sizeof( BPy_Operators ),	/* tp_basicsize */
	0,							/* tp_itemsize */
	
	/* methods */
	(destructor)Operators___dealloc__,	/* tp_dealloc */
	NULL,                       				/* printfunc tp_print; */
	NULL,                       				/* getattrfunc tp_getattr; */
	NULL,                       				/* setattrfunc tp_setattr; */
	NULL,										/* tp_compare */
	NULL,					/* tp_repr */

	/* Method suites for standard classes */

	NULL,                       /* PyNumberMethods *tp_as_number; */
	NULL,                       /* PySequenceMethods *tp_as_sequence; */
	NULL,                       /* PyMappingMethods *tp_as_mapping; */

	/* More standard operations (here for binary compatibility) */

	NULL,						/* hashfunc tp_hash; */
	NULL,                       /* ternaryfunc tp_call; */
	NULL,                       /* reprfunc tp_str; */
	NULL,                       /* getattrofunc tp_getattro; */
	NULL,                       /* setattrofunc tp_setattro; */

	/* Functions to access object as input/output buffer */
	NULL,                       /* PyBufferProcs *tp_as_buffer; */

  /*** Flags to define presence of optional/expanded features ***/
	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, 		/* long tp_flags; */

	NULL,                       /*  char *tp_doc;  Documentation string */
  /*** Assigned meaning in release 2.0 ***/
	/* call function for all accessible objects */
	NULL,                       /* traverseproc tp_traverse; */

	/* delete references to contained objects */
	NULL,                       /* inquiry tp_clear; */

  /***  Assigned meaning in release 2.1 ***/
  /*** rich comparisons ***/
	NULL,                       /* richcmpfunc tp_richcompare; */

  /***  weak reference enabler ***/
	0,                          /* long tp_weaklistoffset; */

  /*** Added in release 2.2 ***/
	/*   Operatorss */
	NULL,                       /* getiterfunc tp_iter; */
	NULL,                       /* iternextfunc tp_iternext; */

  /*** Attribute descriptor and subclassing stuff ***/
	BPy_Operators_methods,	/* struct PyMethodDef *tp_methods; */
	NULL,                       	/* struct PyMemberDef *tp_members; */
	NULL,         					/* struct PyGetSetDef *tp_getset; */
	NULL,							/* struct _typeobject *tp_base; */
	NULL,							/* PyObject *tp_dict; */
	NULL,							/* descrgetfunc tp_descr_get; */
	NULL,							/* descrsetfunc tp_descr_set; */
	0,                          	/* long tp_dictoffset; */
	NULL,                       	/* initproc tp_init; */
	NULL,							/* allocfunc tp_alloc; */
	PyType_GenericNew,		/* newfunc tp_new; */
	
	/*  Low-level free-memory routine */
	NULL,                       /* freefunc tp_free;  */
	
	/* For PyObject_IS_GC */
	NULL,                       /* inquiry tp_is_gc;  */
	NULL,                       /* PyObject *tp_bases; */
	
	/* method resolution order */
	NULL,                       /* PyObject *tp_mro;  */
	NULL,                       /* PyObject *tp_cache; */
	NULL,                       /* PyObject *tp_subclasses; */
	NULL,                       /* PyObject *tp_weaklist; */
	NULL
};

//-------------------MODULE INITIALIZATION--------------------------------
PyMODINIT_FUNC Operators_Init( PyObject *module )
{	
	if( module == NULL )
		return;

	if( PyType_Ready( &Operators_Type ) < 0 )
		return;
	Py_INCREF( &Operators_Type );
	PyModule_AddObject(module, "Operators", (PyObject *)&Operators_Type);	
	
}

//------------------------INSTANCE METHODS ----------------------------------

void Operators___dealloc__(BPy_Operators* self)
{
    self->ob_type->tp_free((PyObject*)self);
}

PyObject * Operators_select(BPy_Operators* self, PyObject *args)
{
	PyObject *obj = 0;

	if ( !PyArg_ParseTuple(args, "O!", &UnaryPredicate1D_Type, &obj) )
		return NULL;
	if ( !((BPy_UnaryPredicate1D *) obj)->up1D ) {
		PyErr_SetString(PyExc_TypeError, "Operators.select(): 1st argument: invalid UnaryPredicate1D object");
		return NULL;
	}

	if (Operators::select(*( ((BPy_UnaryPredicate1D *) obj)->up1D )) < 0) {
		if (!PyErr_Occurred())
			PyErr_SetString(PyExc_RuntimeError, "Operators.select() failed");
		return NULL;
	}

	Py_RETURN_NONE;
}

// CHANGE: first parameter is a chaining iterator, not just a view

PyObject * Operators_chain(BPy_Operators* self, PyObject *args)
{
	PyObject *obj1 = 0, *obj2 = 0, *obj3 = 0;

	if ( !PyArg_ParseTuple(args, "O!O!|O!", &ChainingIterator_Type, &obj1,
										    &UnaryPredicate1D_Type, &obj2,
										    &UnaryFunction1DVoid_Type, &obj3) )
		return NULL;
	if ( !((BPy_ChainingIterator *) obj1)->c_it ) {
		PyErr_SetString(PyExc_TypeError, "Operators.chain(): 1st argument: invalid ChainingIterator object");
		return NULL;
	}
	if ( !((BPy_UnaryPredicate1D *) obj2)->up1D ) {
		PyErr_SetString(PyExc_TypeError, "Operators.chain(): 2nd argument: invalid UnaryPredicate1D object");
		return NULL;
	}

	if( !obj3 ) {
		
		if (Operators::chain( 	*( ((BPy_ChainingIterator *) obj1)->c_it ),
								*( ((BPy_UnaryPredicate1D *) obj2)->up1D )  ) < 0) {
			if (!PyErr_Occurred())
				PyErr_SetString(PyExc_RuntimeError, "Operators.chain() failed");
			return NULL;
		}
							
	} else {
		
		if ( !((BPy_UnaryFunction1DVoid *) obj3)->uf1D_void ) {
			PyErr_SetString(PyExc_TypeError, "Operators.chain(): 3rd argument: invalid UnaryFunction1DVoid object");
			return NULL;
		}
		if (Operators::chain( 	*( ((BPy_ChainingIterator *) obj1)->c_it ),
								*( ((BPy_UnaryPredicate1D *) obj2)->up1D ),
								*( ((BPy_UnaryFunction1DVoid *) obj3)->uf1D_void )  ) < 0) {
			if (!PyErr_Occurred())
				PyErr_SetString(PyExc_RuntimeError, "Operators.chain() failed");
			return NULL;
		}
		
	}
	
	Py_RETURN_NONE;
}

PyObject * Operators_bidirectionalChain(BPy_Operators* self, PyObject *args)
{
	PyObject *obj1 = 0, *obj2 = 0;

	if( !PyArg_ParseTuple(args, "O!|O!", &ChainingIterator_Type, &obj1, &UnaryPredicate1D_Type, &obj2) )
		return NULL;
	if ( !((BPy_ChainingIterator *) obj1)->c_it ) {
		PyErr_SetString(PyExc_TypeError, "Operators.bidirectionalChain(): 1st argument: invalid ChainingIterator object");
		return NULL;
	}

	if( !obj2 ) {

		if (Operators::bidirectionalChain( 	*( ((BPy_ChainingIterator *) obj1)->c_it ) ) < 0) {
			if (!PyErr_Occurred())
				PyErr_SetString(PyExc_RuntimeError, "Operators.bidirectionalChain() failed");
			return NULL;
		}
							
	} else {

		if ( !((BPy_UnaryPredicate1D *) obj2)->up1D ) {
			PyErr_SetString(PyExc_TypeError, "Operators.bidirectionalChain(): 2nd argument: invalid UnaryPredicate1D object");
			return NULL;
		}
		if (Operators::bidirectionalChain( 	*( ((BPy_ChainingIterator *) obj1)->c_it ),
											*( ((BPy_UnaryPredicate1D *) obj2)->up1D ) ) < 0) {
			if (!PyErr_Occurred())
				PyErr_SetString(PyExc_RuntimeError, "Operators.bidirectionalChain() failed");
			return NULL;
		}
		
	}
	
	Py_RETURN_NONE;
}

PyObject * Operators_sequentialSplit(BPy_Operators* self, PyObject *args)
{
	PyObject *obj1 = 0, *obj2 = 0;
	float f = 0.0;

	if( !PyArg_ParseTuple(args, "O!|Of", &UnaryPredicate0D_Type, &obj1, &obj2, &f) )
		return NULL;
	if ( !((BPy_UnaryPredicate0D *) obj1)->up0D ) {
		PyErr_SetString(PyExc_TypeError, "Operators.sequentialSplit(): 1st argument: invalid UnaryPredicate0D object");
		return NULL;
	}

	if( obj2 && BPy_UnaryPredicate0D_Check(obj2) ) {
		
		if ( !((BPy_UnaryPredicate0D *) obj2)->up0D ) {
			PyErr_SetString(PyExc_TypeError, "Operators.sequentialSplit(): 2nd argument: invalid UnaryPredicate0D object");
			return NULL;
		}
		if (Operators::sequentialSplit(	*( ((BPy_UnaryPredicate0D *) obj1)->up0D ),
										*( ((BPy_UnaryPredicate0D *) obj2)->up0D ),
										f ) < 0) {
			if (!PyErr_Occurred())
				PyErr_SetString(PyExc_RuntimeError, "Operators.sequentialSplit() failed");
			return NULL;
		}

	} else {
		
		if ( obj2 ) {
			if ( !PyFloat_Check(obj2) ) {
				PyErr_SetString(PyExc_TypeError, "Operators.sequentialSplit(): invalid 2nd argument");
				return NULL;
			}
			f = PyFloat_AsDouble(obj2);
		}
		if (Operators::sequentialSplit( *( ((BPy_UnaryPredicate0D *) obj1)->up0D ), f ) < 0) {
			if (!PyErr_Occurred())
				PyErr_SetString(PyExc_RuntimeError, "Operators.sequentialSplit() failed");
			return NULL;
		}
		
	}
	
	Py_RETURN_NONE;
}

PyObject * Operators_recursiveSplit(BPy_Operators* self, PyObject *args)
{
	PyObject *obj1 = 0, *obj2 = 0, *obj3 = 0;
	float f = 0.0;

	if ( !PyArg_ParseTuple(args, "O!O|Of", &UnaryFunction0DDouble_Type, &obj1, &obj2, &obj3, &f) )
		return NULL;
	if ( !((BPy_UnaryFunction0DDouble *) obj1)->uf0D_double ) {
		PyErr_SetString(PyExc_TypeError, "Operators.recursiveSplit(): 1st argument: invalid UnaryFunction0DDouble object");
		return NULL;
	}
	
	if ( BPy_UnaryPredicate1D_Check(obj2) ) {

		if ( !((BPy_UnaryPredicate1D *) obj2)->up1D ) {
			PyErr_SetString(PyExc_TypeError, "Operators.recursiveSplit(): 2nd argument: invalid UnaryPredicate1D object");
			return NULL;
		}
		if ( obj3 ) {
			if ( !PyFloat_Check(obj3) ) {
				PyErr_SetString(PyExc_TypeError, "Operators.recursiveSplit(): invalid 3rd argument");
				return NULL;
			}
			f = PyFloat_AsDouble(obj3);
		}
		if (Operators::recursiveSplit( 	*( ((BPy_UnaryFunction0DDouble *) obj1)->uf0D_double ),
										*( ((BPy_UnaryPredicate1D *) obj2)->up1D ),
										f ) < 0) {
			if (!PyErr_Occurred())
				PyErr_SetString(PyExc_RuntimeError, "Operators.recursiveSplit() failed");
			return NULL;
		}
	
	} else {

		if ( !BPy_UnaryPredicate0D_Check(obj2) || !((BPy_UnaryPredicate0D *) obj2)->up0D ) {
			PyErr_SetString(PyExc_TypeError, "Operators.recursiveSplit(): invalid 2nd argument");
			return NULL;
		}
		if ( !BPy_UnaryPredicate1D_Check(obj3) || !((BPy_UnaryPredicate1D *) obj3)->up1D ) {
			PyErr_SetString(PyExc_TypeError, "Operators.recursiveSplit(): invalid 3rd argument");
			return NULL;
		}
		if (Operators::recursiveSplit( 	*( ((BPy_UnaryFunction0DDouble *) obj1)->uf0D_double ),
										*( ((BPy_UnaryPredicate0D *) obj2)->up0D ),
										*( ((BPy_UnaryPredicate1D *) obj3)->up1D ),
										f ) < 0) {
			if (!PyErr_Occurred())
				PyErr_SetString(PyExc_RuntimeError, "Operators.recursiveSplit() failed");
			return NULL;
		}

	}
	
	Py_RETURN_NONE;
}

PyObject * Operators_sort(BPy_Operators* self, PyObject *args)
{
	PyObject *obj = 0;

	if ( !PyArg_ParseTuple(args, "O!", &BinaryPredicate1D_Type, &obj) )
		return NULL;
	if ( !((BPy_BinaryPredicate1D *) obj)->bp1D ) {
		PyErr_SetString(PyExc_TypeError, "Operators.sort(): 1st argument: invalid BinaryPredicate1D object");
		return NULL;
	}

	if (Operators::sort(*( ((BPy_BinaryPredicate1D *) obj)->bp1D )) < 0) {
		if (!PyErr_Occurred())
			PyErr_SetString(PyExc_RuntimeError, "Operators.sort() failed");
		return NULL;
	}
	Py_RETURN_NONE;
}

PyObject * Operators_create(BPy_Operators* self, PyObject *args)
{
	PyObject *obj1 = 0, *obj2 = 0;

	if ( !PyArg_ParseTuple(args, "O!O!", &UnaryPredicate1D_Type, &obj1, &PyList_Type, &obj2) )
		return NULL;
	if ( !((BPy_UnaryPredicate1D *) obj1)->up1D ) {
		PyErr_SetString(PyExc_TypeError, "Operators.create(): 1st argument: invalid UnaryPredicate1D object");
		return NULL;
	}

	vector<StrokeShader *> shaders;
	for( int i = 0; i < PyList_Size(obj2); i++) {
		PyObject *py_ss = PyList_GetItem(obj2,i);
		
		if ( !BPy_StrokeShader_Check(py_ss) ) {
			PyErr_SetString(PyExc_TypeError, "Operators.create() 2nd argument must be a list of StrokeShader objects");
			return NULL;
		}
		shaders.push_back( ((BPy_StrokeShader *) py_ss)->ss );
	}
	
	if (Operators::create( *( ((BPy_UnaryPredicate1D *) obj1)->up1D ), shaders) < 0) {
		if (!PyErr_Occurred())
			PyErr_SetString(PyExc_RuntimeError, "Operators.create() failed");
		return NULL;
	}

	Py_RETURN_NONE;
}

PyObject * Operators_getViewEdgesSize( BPy_Operators* self) {
	return PyInt_FromLong( Operators::getViewEdgesSize() );
}

PyObject * Operators_getChainsSize( BPy_Operators* self ) {
	return PyInt_FromLong( Operators::getChainsSize() );
}

PyObject * Operators_getStrokesSize( BPy_Operators* self) {
	return PyInt_FromLong( Operators::getStrokesSize() );
}


///////////////////////////////////////////////////////////////////////////////////////////

#ifdef __cplusplus
}
#endif