From 17555efed7583c12459d4f3044dfdd457489ab39 Mon Sep 17 00:00:00 2001 From: Tamito Kajiyama Date: Mon, 1 Dec 2008 11:14:33 +0000 Subject: 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. --- .../freestyle/style_modules/freestyle_init.py | 3 +- .../freestyle/intern/python/BPy_Convert.cpp | 3 +- .../blender/freestyle/intern/python/BPy_Convert.h | 2 +- .../intern/python/Interface1D/BPy_Stroke.cpp | 91 +++++++++++++++++++--- .../python/Iterator/BPy_StrokeVertexIterator.cpp | 22 +++++- .../python/Iterator/BPy_StrokeVertexIterator.h | 1 + source/blender/freestyle/intern/stroke/Stroke.cpp | 3 + source/blender/freestyle/intern/stroke/Stroke.h | 3 + 8 files changed, 111 insertions(+), 17 deletions(-) diff --git a/release/scripts/freestyle/style_modules/freestyle_init.py b/release/scripts/freestyle/style_modules/freestyle_init.py index 504aad25b87..a586f887a97 100644 --- a/release/scripts/freestyle/style_modules/freestyle_init.py +++ b/release/scripts/freestyle/style_modules/freestyle_init.py @@ -47,7 +47,8 @@ class StrokeAttribute(Blender.Freestyle.StrokeAttribute): pass class StrokeShader(Blender.Freestyle.StrokeShader): - pass + def getName(self): + return self.__class__.__name__ class UnaryFunction0D(Blender.Freestyle.UnaryFunction0D): pass diff --git a/source/blender/freestyle/intern/python/BPy_Convert.cpp b/source/blender/freestyle/intern/python/BPy_Convert.cpp index 6154dc67d95..b44626a9930 100644 --- a/source/blender/freestyle/intern/python/BPy_Convert.cpp +++ b/source/blender/freestyle/intern/python/BPy_Convert.cpp @@ -252,10 +252,11 @@ PyObject * BPy_CurvePointIterator_from_CurvePointIterator( CurveInternal::CurveP 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; } diff --git a/source/blender/freestyle/intern/python/BPy_Convert.h b/source/blender/freestyle/intern/python/BPy_Convert.h index 4ddfbe38d54..80cb0600d6e 100644 --- a/source/blender/freestyle/intern/python/BPy_Convert.h +++ b/source/blender/freestyle/intern/python/BPy_Convert.h @@ -93,7 +93,7 @@ PyObject * BPy_ViewShape_from_ViewShape( ViewShape& vs ); 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 ); diff --git a/source/blender/freestyle/intern/python/Interface1D/BPy_Stroke.cpp b/source/blender/freestyle/intern/python/Interface1D/BPy_Stroke.cpp index af4e882a672..d9f46774929 100644 --- a/source/blender/freestyle/intern/python/Interface1D/BPy_Stroke.cpp +++ b/source/blender/freestyle/intern/python/Interface1D/BPy_Stroke.cpp @@ -15,6 +15,11 @@ extern "C" { /*--------------- 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 ); @@ -38,6 +43,7 @@ static PyObject * Stroke_pointsEnd( BPy_Stroke *self , PyObject *args); /*----------------------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 @@ static PyMethodDef BPy_Stroke_methods[] = { /*-----------------------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 @@ PyTypeObject Stroke_Type = { /* 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 @@ PyTypeObject Stroke_Type = { /*** 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 @@ PyTypeObject Stroke_Type = { // Stroke () // template 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 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 @@ int Stroke___init__(BPy_Stroke *self, PyObject *args, PyObject *kwds) 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_setTips( BPy_Stroke *self , PyObject *args) { } 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 ) { diff --git a/source/blender/freestyle/intern/python/Iterator/BPy_StrokeVertexIterator.cpp b/source/blender/freestyle/intern/python/Iterator/BPy_StrokeVertexIterator.cpp index 1994cb9ce6d..14356970c93 100644 --- a/source/blender/freestyle/intern/python/Iterator/BPy_StrokeVertexIterator.cpp +++ b/source/blender/freestyle/intern/python/Iterator/BPy_StrokeVertexIterator.cpp @@ -11,6 +11,7 @@ extern "C" { /*--------------- Python API function prototypes for StrokeVertexIterator instance -----------*/ static int StrokeVertexIterator___init__(BPy_StrokeVertexIterator *self, PyObject *args); +static PyObject * StrokeVertexIterator_iternext( PyObject *obj ); static PyObject * StrokeVertexIterator_t( BPy_StrokeVertexIterator *self ); static PyObject * StrokeVertexIterator_u( BPy_StrokeVertexIterator *self ); static PyObject * StrokeVertexIterator_castToInterface0DIterator( BPy_StrokeVertexIterator *self ); @@ -81,8 +82,8 @@ PyTypeObject StrokeVertexIterator_Type = { /*** Added in release 2.2 ***/ /* Iterators */ - NULL, /* getiterfunc tp_iter; */ - NULL, /* iternextfunc tp_iternext; */ + PyObject_SelfIter, /* getiterfunc tp_iter; */ + (iternextfunc)StrokeVertexIterator_iternext, /* iternextfunc tp_iternext; */ /*** Attribute descriptor and subclassing stuff ***/ BPy_StrokeVertexIterator_methods, /* struct PyMethodDef *tp_methods; */ @@ -139,6 +140,23 @@ int StrokeVertexIterator___init__(BPy_StrokeVertexIterator *self, PyObject *args return 0; } +PyObject * StrokeVertexIterator_iternext( PyObject *obj ) { + BPy_StrokeVertexIterator *self = (BPy_StrokeVertexIterator *)obj; + StrokeVertex *sv; + if (self->reversed) { + if (self->sv_it->isBegin()) + return NULL; + self->sv_it->decrement(); + sv = self->sv_it->operator->(); + } else { + if (self->sv_it->isEnd()) + return NULL; + sv = self->sv_it->operator->(); + self->sv_it->increment(); + } + return BPy_StrokeVertex_from_StrokeVertex_ptr( sv ); +} + PyObject * StrokeVertexIterator_t( BPy_StrokeVertexIterator *self ) { return PyFloat_FromDouble( self->sv_it->t() ); } diff --git a/source/blender/freestyle/intern/python/Iterator/BPy_StrokeVertexIterator.h b/source/blender/freestyle/intern/python/Iterator/BPy_StrokeVertexIterator.h index 9e95fc0a401..0c4b6e4bb6c 100644 --- a/source/blender/freestyle/intern/python/Iterator/BPy_StrokeVertexIterator.h +++ b/source/blender/freestyle/intern/python/Iterator/BPy_StrokeVertexIterator.h @@ -21,6 +21,7 @@ extern PyTypeObject StrokeVertexIterator_Type; typedef struct { BPy_Iterator py_it; StrokeInternal::StrokeVertexIterator *sv_it; + int reversed; } BPy_StrokeVertexIterator; /////////////////////////////////////////////////////////////////////////////////////////// diff --git a/source/blender/freestyle/intern/stroke/Stroke.cpp b/source/blender/freestyle/intern/stroke/Stroke.cpp index dbb49bab45d..01a71f2d4eb 100755 --- a/source/blender/freestyle/intern/stroke/Stroke.cpp +++ b/source/blender/freestyle/intern/stroke/Stroke.cpp @@ -416,7 +416,10 @@ Stroke::Stroke(const Stroke& iBrother) _mediumType = iBrother._mediumType; _textureId = iBrother._textureId; _tips = iBrother._tips; + if(iBrother._rep) _rep = new StrokeRep(*(iBrother._rep)); + else + _rep = 0; } diff --git a/source/blender/freestyle/intern/stroke/Stroke.h b/source/blender/freestyle/intern/stroke/Stroke.h index 3b3a6a5ab2d..a6a025e44a0 100755 --- a/source/blender/freestyle/intern/stroke/Stroke.h +++ b/source/blender/freestyle/intern/stroke/Stroke.h @@ -548,6 +548,9 @@ public: StrokeInternal::StrokeVertexIterator strokeVerticesEnd(); /*! Returns the number of StrokeVertex constituing the Stroke. */ inline unsigned int strokeVerticesSize() const {return _Vertices.size();} + + /*! Returns the i-th StrokeVertex constituting the Stroke. */ + inline StrokeVertex* strokeVerticeAt(unsigned int i) {return _Vertices.at(i);} // Iterator access (Interface1D) /*! Returns an Interface0DIterator pointing on the first StrokeVertex of the -- cgit v1.2.3