[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [17661] branches/soc-2008-mxcurioni: Added changes to support Python' s native iterator protocol in Stroke and StrokeVertexIterator.

Tamito Kajiyama rd6t-kjym at asahi-net.or.jp
Mon Dec 1 12:14:34 CET 2008


Revision: 17661
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=17661
Author:   kjym3
Date:     2008-12-01 12:14:33 +0100 (Mon, 01 Dec 2008)

Log Message:
-----------
Added changes to support Python's native iterator protocol in Stroke and StrokeVertexIterator.

freestyle_init.py
* Added a generic getName() method that allows subclasses to omit the method to return their class names.

BPy_Convert.cpp
BPy_Convert.h
* Added to BPy_StrokeVertexIterator_from_StrokeVertexIterator() a second argument to specify the direction (reversed or not) of the iterator to be created.

BPy_Stroke.cpp
* Added support for Python's native iterator protocol.
* Added code to parse the optional argument of strokeVerticesBegin().

BPy_StrokeVertexIterator.cpp
BPy_StrokeVertexIterator.h
* Added support for Python's native iterator protocol.

Stroke.cpp
* Fixed a null pointer reference.

Stroke.h
* Added new method Stroke::strokeVerticeAt(i) that returns the i-th StrokeVertex of the Stroke.

Modified Paths:
--------------
    branches/soc-2008-mxcurioni/release/scripts/freestyle/style_modules/freestyle_init.py
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.h
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Interface1D/BPy_Stroke.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Iterator/BPy_StrokeVertexIterator.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Iterator/BPy_StrokeVertexIterator.h
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/stroke/Stroke.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/stroke/Stroke.h

Modified: branches/soc-2008-mxcurioni/release/scripts/freestyle/style_modules/freestyle_init.py
===================================================================
--- branches/soc-2008-mxcurioni/release/scripts/freestyle/style_modules/freestyle_init.py	2008-12-01 10:49:06 UTC (rev 17660)
+++ branches/soc-2008-mxcurioni/release/scripts/freestyle/style_modules/freestyle_init.py	2008-12-01 11:14:33 UTC (rev 17661)
@@ -47,7 +47,8 @@
 	pass
 
 class StrokeShader(Blender.Freestyle.StrokeShader):
-	pass
+	def getName(self):
+                return self.__class__.__name__
 
 class UnaryFunction0D(Blender.Freestyle.UnaryFunction0D):
 	pass

Modified: branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.cpp
===================================================================
--- branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.cpp	2008-12-01 10:49:06 UTC (rev 17660)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.cpp	2008-12-01 11:14:33 UTC (rev 17661)
@@ -252,10 +252,11 @@
 	return py_cp_it;
 }
 
-PyObject * BPy_StrokeVertexIterator_from_StrokeVertexIterator( StrokeInternal::StrokeVertexIterator& sv_it) {
+PyObject * BPy_StrokeVertexIterator_from_StrokeVertexIterator( StrokeInternal::StrokeVertexIterator& sv_it, int reversed) {
 	PyObject *py_sv_it = StrokeVertexIterator_Type.tp_new( &StrokeVertexIterator_Type, 0, 0 );
 	((BPy_StrokeVertexIterator *) py_sv_it)->sv_it = new StrokeInternal::StrokeVertexIterator( sv_it );
 	((BPy_StrokeVertexIterator *) py_sv_it)->py_it.it = ((BPy_StrokeVertexIterator *) py_sv_it)->sv_it;
+	((BPy_StrokeVertexIterator *) py_sv_it)->reversed = reversed;
 
 	return py_sv_it;
 }

Modified: branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.h
===================================================================
--- branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.h	2008-12-01 10:49:06 UTC (rev 17660)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.h	2008-12-01 11:14:33 UTC (rev 17661)
@@ -93,7 +93,7 @@
 PyObject * BPy_AdjacencyIterator_from_AdjacencyIterator( AdjacencyIterator& a_it );
 PyObject * BPy_Interface0DIterator_from_Interface0DIterator( Interface0DIterator& if0D_it );
 PyObject * BPy_CurvePointIterator_from_CurvePointIterator( CurveInternal::CurvePointIterator& cp_it );
-PyObject * BPy_StrokeVertexIterator_from_StrokeVertexIterator( StrokeInternal::StrokeVertexIterator& sv_it);
+PyObject * BPy_StrokeVertexIterator_from_StrokeVertexIterator( StrokeInternal::StrokeVertexIterator& sv_it, int reversed);
 PyObject * BPy_SVertexIterator_from_SVertexIterator( ViewEdgeInternal::SVertexIterator& sv_it );
 PyObject * BPy_orientedViewEdgeIterator_from_orientedViewEdgeIterator( ViewVertexInternal::orientedViewEdgeIterator& ove_it );
 PyObject * BPy_ViewEdgeIterator_from_ViewEdgeIterator( ViewEdgeInternal::ViewEdgeIterator& ve_it );

Modified: branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Interface1D/BPy_Stroke.cpp
===================================================================
--- branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Interface1D/BPy_Stroke.cpp	2008-12-01 10:49:06 UTC (rev 17660)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Interface1D/BPy_Stroke.cpp	2008-12-01 11:14:33 UTC (rev 17661)
@@ -15,7 +15,12 @@
 
 /*---------------  Python API function prototypes for Stroke instance  -----------*/
 static int Stroke___init__(BPy_Stroke *self, PyObject *args, PyObject *kwds);
+static PyObject * Stroke___iter__(PyObject *self);
 
+static Py_ssize_t Stroke_length( BPy_Stroke *self );
+static PyObject * Stroke_item( BPy_Stroke *self, Py_ssize_t i );
+static PyObject * Stroke___getitem__( BPy_Stroke *self, PyObject *item );
+
 static PyObject * Stroke_ComputeSampling( BPy_Stroke *self, PyObject *args );
 static PyObject * Stroke_Resample( BPy_Stroke *self, PyObject *args );
 static PyObject * Stroke_InsertVertex( BPy_Stroke *self, PyObject *args );
@@ -38,6 +43,7 @@
 
 /*----------------------Stroke instance definitions ----------------------------*/
 static PyMethodDef BPy_Stroke_methods[] = {	
+	{"__getitem__", ( PyCFunction ) Stroke___getitem__, METH_O, "(int i) Returns the i-th StrokeVertex constituting the Stroke."},
 	{"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."},
 		{"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."},
 	{"RemoveVertex", ( PyCFunction ) Stroke_RemoveVertex, METH_VARARGS, "(StrokeVertex sv) Removes the stroke vertex sv from the stroke. The length and curvilinear abscissa are updated consequently."},
@@ -63,6 +69,19 @@
 
 /*-----------------------BPy_Stroke type definition ------------------------------*/
 
+static PySequenceMethods Stroke_as_sequence = {
+	(lenfunc)Stroke_length,		/* sq_length */
+	NULL,						/* sq_concat */
+	NULL,						/* sq_repeat */
+	(ssizeargfunc)Stroke_item,	/* sq_item */
+	NULL,						/* sq_slice */
+	NULL,						/* sq_ass_item */
+	NULL,						/* sq_ass_slice */
+	NULL,						/* sq_contains */
+	NULL,						/* sq_inplace_concat */
+	NULL,						/* sq_inplace_repeat */
+};
+
 PyTypeObject Stroke_Type = {
 	PyObject_HEAD_INIT( NULL ) 
 	0,							/* ob_size */
@@ -81,7 +100,7 @@
 	/* Method suites for standard classes */
 
 	NULL,                       /* PyNumberMethods *tp_as_number; */
-	NULL,                       /* PySequenceMethods *tp_as_sequence; */
+	&Stroke_as_sequence,        /* PySequenceMethods *tp_as_sequence; */
 	NULL,                       /* PyMappingMethods *tp_as_mapping; */
 
 	/* More standard operations (here for binary compatibility) */
@@ -115,7 +134,7 @@
 
   /*** Added in release 2.2 ***/
 	/*   Iterators */
-	NULL,                       /* getiterfunc tp_iter; */
+	Stroke___iter__,            /* getiterfunc tp_iter; */
 	NULL,                       /* iternextfunc tp_iternext; */
 
   /*** Attribute descriptor and subclassing stuff ***/
@@ -153,22 +172,28 @@
 
 // Stroke ()
 // template<class InputVertexIterator> Stroke (InputVertexIterator iBegin, InputVertexIterator iEnd)
+//
+// pb: - need to be able to switch representation: InputVertexIterator <=> position
+//     - is it even used ? not even in SWIG version
 
 int Stroke___init__(BPy_Stroke *self, PyObject *args, PyObject *kwds)
 {
-	PyObject *obj1 = 0, *obj2 = 0;
+	PyObject *obj1 = NULL, *obj2 = NULL;
 
-    if (! PyArg_ParseTuple(args, "|OO", &obj1) )
+	if (! PyArg_ParseTuple(args, "|OO", &obj1, &obj2) )
         return -1;
 
 	if( !obj1 && !obj2 ){
 		self->s = new Stroke();
-
-	// template<class InputVertexIterator> Stroke (InputVertexIterator iBegin, InputVertexIterator iEnd)
-	//
-	// pb: - need to be able to switch representation: InputVertexIterator <=> position
-	//     - is it even used ? not even in SWIG version
+	} else if ( obj1 && !obj2 ) {
+		if (! BPy_Stroke_Check(obj1) ) {
+			PyErr_SetString(PyExc_TypeError, "not a Stroke object");
+			return -1;
+		}
+		self->s = new Stroke(*( ((BPy_Stroke *)obj1)->s ));
 	} else {
+		PyErr_SetString(PyExc_NotImplementedError,
+			"Stroke(InputVertexIterator iBegin, InputVertexIterator iEnd) not implemented");
 		return -1;
 	}
 
@@ -177,6 +202,42 @@
 	return 0;
 }
 
+PyObject * Stroke___iter__( PyObject *self ) {
+	StrokeInternal::StrokeVertexIterator sv_it( ((BPy_Stroke *)self)->s->strokeVerticesBegin() );
+	return BPy_StrokeVertexIterator_from_StrokeVertexIterator( sv_it, 0 );
+}
+
+Py_ssize_t Stroke_length( BPy_Stroke *self ) {
+	return self->s->strokeVerticesSize();
+}
+
+PyObject * Stroke_item( BPy_Stroke *self, Py_ssize_t i ) {
+	if (i < 0 || i >= (Py_ssize_t)self->s->strokeVerticesSize()) {
+		PyErr_SetString(PyExc_IndexError, "subscript index out of range");
+		return NULL;
+	}
+	return BPy_StrokeVertex_from_StrokeVertex_ptr( self->s->strokeVerticeAt(i) );
+}
+
+PyObject * Stroke___getitem__( BPy_Stroke *self, PyObject *item ) {
+	long i;
+
+	if (PyInt_Check(item)) {
+		i = PyInt_AS_LONG(item);
+	} else if (PyLong_Check(item)) {
+		i = PyLong_AsLong(item);
+		if (i == -1 && PyErr_Occurred())
+			return NULL;
+	} else {
+		PyErr_SetString(PyExc_TypeError, "subscript indices must be integers");
+		return NULL;
+	}
+	if (i < 0) {
+		i += self->s->strokeVerticesSize();
+	}
+	return Stroke_item(self, i);
+}
+
 PyObject * Stroke_ComputeSampling( BPy_Stroke *self, PyObject *args ) {	
 	int i;
 
@@ -314,13 +375,19 @@
 }
 
 PyObject * Stroke_strokeVerticesBegin( BPy_Stroke *self , PyObject *args) {
-	StrokeInternal::StrokeVertexIterator sv_it( self->s->strokeVerticesBegin() );
-	return BPy_StrokeVertexIterator_from_StrokeVertexIterator( sv_it );
+	float f = 0;
+
+	if(!( PyArg_ParseTuple(args, "|f", &f)  )){
+		cout << "ERROR: Stroke_pointsBegin" << endl;
+		Py_RETURN_NONE;
+	}
+	StrokeInternal::StrokeVertexIterator sv_it( self->s->strokeVerticesBegin(f) );
+	return BPy_StrokeVertexIterator_from_StrokeVertexIterator( sv_it, 0 );
 }
 
 PyObject * Stroke_strokeVerticesEnd( BPy_Stroke *self ) {
 	StrokeInternal::StrokeVertexIterator sv_it( self->s->strokeVerticesEnd() );
-	return BPy_StrokeVertexIterator_from_StrokeVertexIterator( sv_it );
+	return BPy_StrokeVertexIterator_from_StrokeVertexIterator( sv_it, 1 );
 }
 
 PyObject * Stroke_strokeVerticesSize( BPy_Stroke *self ) {

Modified: branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Iterator/BPy_StrokeVertexIterator.cpp
===================================================================
--- branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Iterator/BPy_StrokeVertexIterator.cpp	2008-12-01 10:49:06 UTC (rev 17660)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Iterator/BPy_StrokeVertexIterator.cpp	2008-12-01 11:14:33 UTC (rev 17661)
@@ -11,6 +11,7 @@
 

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list