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.
This commit is contained in:
2008-12-01 11:14:33 +00:00
parent a407b65c83
commit 17555efed7
8 changed files with 111 additions and 17 deletions

View File

@@ -47,7 +47,8 @@ class StrokeAttribute(Blender.Freestyle.StrokeAttribute):
pass pass
class StrokeShader(Blender.Freestyle.StrokeShader): class StrokeShader(Blender.Freestyle.StrokeShader):
pass def getName(self):
return self.__class__.__name__
class UnaryFunction0D(Blender.Freestyle.UnaryFunction0D): class UnaryFunction0D(Blender.Freestyle.UnaryFunction0D):
pass pass

View File

@@ -252,10 +252,11 @@ PyObject * BPy_CurvePointIterator_from_CurvePointIterator( CurveInternal::CurveP
return py_cp_it; 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 ); 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)->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)->py_it.it = ((BPy_StrokeVertexIterator *) py_sv_it)->sv_it;
((BPy_StrokeVertexIterator *) py_sv_it)->reversed = reversed;
return py_sv_it; return py_sv_it;
} }

View File

@@ -93,7 +93,7 @@ PyObject * BPy_ViewShape_from_ViewShape( ViewShape& vs );
PyObject * BPy_AdjacencyIterator_from_AdjacencyIterator( AdjacencyIterator& a_it ); PyObject * BPy_AdjacencyIterator_from_AdjacencyIterator( AdjacencyIterator& a_it );
PyObject * BPy_Interface0DIterator_from_Interface0DIterator( Interface0DIterator& if0D_it ); PyObject * BPy_Interface0DIterator_from_Interface0DIterator( Interface0DIterator& if0D_it );
PyObject * BPy_CurvePointIterator_from_CurvePointIterator( CurveInternal::CurvePointIterator& cp_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_SVertexIterator_from_SVertexIterator( ViewEdgeInternal::SVertexIterator& sv_it );
PyObject * BPy_orientedViewEdgeIterator_from_orientedViewEdgeIterator( ViewVertexInternal::orientedViewEdgeIterator& ove_it ); PyObject * BPy_orientedViewEdgeIterator_from_orientedViewEdgeIterator( ViewVertexInternal::orientedViewEdgeIterator& ove_it );
PyObject * BPy_ViewEdgeIterator_from_ViewEdgeIterator( ViewEdgeInternal::ViewEdgeIterator& ve_it ); PyObject * BPy_ViewEdgeIterator_from_ViewEdgeIterator( ViewEdgeInternal::ViewEdgeIterator& ve_it );

View File

@@ -15,6 +15,11 @@ extern "C" {
/*--------------- Python API function prototypes for Stroke instance -----------*/ /*--------------- Python API function prototypes for Stroke instance -----------*/
static int Stroke___init__(BPy_Stroke *self, PyObject *args, PyObject *kwds); 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_ComputeSampling( BPy_Stroke *self, PyObject *args );
static PyObject * Stroke_Resample( 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 ----------------------------*/ /*----------------------Stroke instance definitions ----------------------------*/
static PyMethodDef BPy_Stroke_methods[] = { 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."}, {"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."}, {"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."}, {"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 ------------------------------*/ /*-----------------------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 = { PyTypeObject Stroke_Type = {
PyObject_HEAD_INIT( NULL ) PyObject_HEAD_INIT( NULL )
0, /* ob_size */ 0, /* ob_size */
@@ -81,7 +100,7 @@ PyTypeObject Stroke_Type = {
/* Method suites for standard classes */ /* Method suites for standard classes */
NULL, /* PyNumberMethods *tp_as_number; */ NULL, /* PyNumberMethods *tp_as_number; */
NULL, /* PySequenceMethods *tp_as_sequence; */ &Stroke_as_sequence, /* PySequenceMethods *tp_as_sequence; */
NULL, /* PyMappingMethods *tp_as_mapping; */ NULL, /* PyMappingMethods *tp_as_mapping; */
/* More standard operations (here for binary compatibility) */ /* More standard operations (here for binary compatibility) */
@@ -115,7 +134,7 @@ PyTypeObject Stroke_Type = {
/*** Added in release 2.2 ***/ /*** Added in release 2.2 ***/
/* Iterators */ /* Iterators */
NULL, /* getiterfunc tp_iter; */ Stroke___iter__, /* getiterfunc tp_iter; */
NULL, /* iternextfunc tp_iternext; */ NULL, /* iternextfunc tp_iternext; */
/*** Attribute descriptor and subclassing stuff ***/ /*** Attribute descriptor and subclassing stuff ***/
@@ -153,22 +172,28 @@ PyTypeObject Stroke_Type = {
// Stroke () // Stroke ()
// template<class InputVertexIterator> Stroke (InputVertexIterator iBegin, InputVertexIterator iEnd) // 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) 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; return -1;
if( !obj1 && !obj2 ){ if( !obj1 && !obj2 ){
self->s = new Stroke(); self->s = new Stroke();
} else if ( obj1 && !obj2 ) {
// template<class InputVertexIterator> Stroke (InputVertexIterator iBegin, InputVertexIterator iEnd) if (! BPy_Stroke_Check(obj1) ) {
// PyErr_SetString(PyExc_TypeError, "not a Stroke object");
// pb: - need to be able to switch representation: InputVertexIterator <=> position return -1;
// - is it even used ? not even in SWIG version }
self->s = new Stroke(*( ((BPy_Stroke *)obj1)->s ));
} else { } else {
PyErr_SetString(PyExc_NotImplementedError,
"Stroke(InputVertexIterator iBegin, InputVertexIterator iEnd) not implemented");
return -1; return -1;
} }
@@ -177,6 +202,42 @@ int Stroke___init__(BPy_Stroke *self, PyObject *args, PyObject *kwds)
return 0; 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 ) { PyObject * Stroke_ComputeSampling( BPy_Stroke *self, PyObject *args ) {
int i; int i;
@@ -314,13 +375,19 @@ PyObject *Stroke_setTips( BPy_Stroke *self , PyObject *args) {
} }
PyObject * Stroke_strokeVerticesBegin( BPy_Stroke *self , PyObject *args) { PyObject * Stroke_strokeVerticesBegin( BPy_Stroke *self , PyObject *args) {
StrokeInternal::StrokeVertexIterator sv_it( self->s->strokeVerticesBegin() ); float f = 0;
return BPy_StrokeVertexIterator_from_StrokeVertexIterator( sv_it );
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 ) { PyObject * Stroke_strokeVerticesEnd( BPy_Stroke *self ) {
StrokeInternal::StrokeVertexIterator sv_it( self->s->strokeVerticesEnd() ); 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 ) { PyObject * Stroke_strokeVerticesSize( BPy_Stroke *self ) {

View File

@@ -11,6 +11,7 @@ extern "C" {
/*--------------- Python API function prototypes for StrokeVertexIterator instance -----------*/ /*--------------- Python API function prototypes for StrokeVertexIterator instance -----------*/
static int StrokeVertexIterator___init__(BPy_StrokeVertexIterator *self, PyObject *args); 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_t( BPy_StrokeVertexIterator *self );
static PyObject * StrokeVertexIterator_u( BPy_StrokeVertexIterator *self ); static PyObject * StrokeVertexIterator_u( BPy_StrokeVertexIterator *self );
static PyObject * StrokeVertexIterator_castToInterface0DIterator( BPy_StrokeVertexIterator *self ); static PyObject * StrokeVertexIterator_castToInterface0DIterator( BPy_StrokeVertexIterator *self );
@@ -81,8 +82,8 @@ PyTypeObject StrokeVertexIterator_Type = {
/*** Added in release 2.2 ***/ /*** Added in release 2.2 ***/
/* Iterators */ /* Iterators */
NULL, /* getiterfunc tp_iter; */ PyObject_SelfIter, /* getiterfunc tp_iter; */
NULL, /* iternextfunc tp_iternext; */ (iternextfunc)StrokeVertexIterator_iternext, /* iternextfunc tp_iternext; */
/*** Attribute descriptor and subclassing stuff ***/ /*** Attribute descriptor and subclassing stuff ***/
BPy_StrokeVertexIterator_methods, /* struct PyMethodDef *tp_methods; */ BPy_StrokeVertexIterator_methods, /* struct PyMethodDef *tp_methods; */
@@ -139,6 +140,23 @@ int StrokeVertexIterator___init__(BPy_StrokeVertexIterator *self, PyObject *args
return 0; 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 ) { PyObject * StrokeVertexIterator_t( BPy_StrokeVertexIterator *self ) {
return PyFloat_FromDouble( self->sv_it->t() ); return PyFloat_FromDouble( self->sv_it->t() );
} }

View File

@@ -21,6 +21,7 @@ extern PyTypeObject StrokeVertexIterator_Type;
typedef struct { typedef struct {
BPy_Iterator py_it; BPy_Iterator py_it;
StrokeInternal::StrokeVertexIterator *sv_it; StrokeInternal::StrokeVertexIterator *sv_it;
int reversed;
} BPy_StrokeVertexIterator; } BPy_StrokeVertexIterator;
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -416,7 +416,10 @@ Stroke::Stroke(const Stroke& iBrother)
_mediumType = iBrother._mediumType; _mediumType = iBrother._mediumType;
_textureId = iBrother._textureId; _textureId = iBrother._textureId;
_tips = iBrother._tips; _tips = iBrother._tips;
if(iBrother._rep)
_rep = new StrokeRep(*(iBrother._rep)); _rep = new StrokeRep(*(iBrother._rep));
else
_rep = 0;
} }

View File

@@ -549,6 +549,9 @@ public:
/*! Returns the number of StrokeVertex constituing the Stroke. */ /*! Returns the number of StrokeVertex constituing the Stroke. */
inline unsigned int strokeVerticesSize() const {return _Vertices.size();} 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) // Iterator access (Interface1D)
/*! Returns an Interface0DIterator pointing on the first StrokeVertex of the /*! Returns an Interface0DIterator pointing on the first StrokeVertex of the
* Stroke. * Stroke.