From: ageay Date: Thu, 28 Jun 2012 15:50:33 +0000 (+0000) Subject: Swig improvements on wrapping of input double *. X-Git-Tag: V6_main_FINAL~619 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=7c452b0a9ebadd958511acf17557d54ff33cb1a1;p=tools%2Fmedcoupling.git Swig improvements on wrapping of input double *. --- diff --git a/src/MEDCoupling_Swig/MEDCoupling.i b/src/MEDCoupling_Swig/MEDCoupling.i index 6bbfbe1a7..32568a426 100644 --- a/src/MEDCoupling_Swig/MEDCoupling.i +++ b/src/MEDCoupling_Swig/MEDCoupling.i @@ -435,15 +435,27 @@ namespace ParaMEDMEM int getCellContainingPoint(PyObject *p, double eps) const throw(INTERP_KERNEL::Exception) { - int sz; - INTERP_KERNEL::AutoPtr pos=convertPyToNewDblArr2(p,&sz); + double val; + DataArrayDouble *a; + DataArrayDoubleTuple *aa; + std::vector bb; + int sw; + int spaceDim=self->getSpaceDimension(); + const char msg[]="Python wrap of MEDCouplingUMesh::getCellContainingPoint : "; + const double *pos=convertObjToPossibleCpp5_Safe(p,sw,val,a,aa,bb,msg,1,spaceDim,true); return self->getCellContainingPoint(pos,eps); } PyObject *getCellsContainingPoints(PyObject *p, int nbOfPoints, double eps) const throw(INTERP_KERNEL::Exception) { - int sz; - INTERP_KERNEL::AutoPtr pos=convertPyToNewDblArr2(p,&sz); + double val; + DataArrayDouble *a; + DataArrayDoubleTuple *aa; + std::vector bb; + int sw; + int spaceDim=self->getSpaceDimension(); + const char msg[]="Python wrap of MEDCouplingUMesh::getCellContainingPoint : "; + const double *pos=convertObjToPossibleCpp5_Safe(p,sw,val,a,aa,bb,msg,nbOfPoints,spaceDim,true); std::vector elts,eltsIndex; self->getCellsContainingPoints(pos,nbOfPoints,eps,elts,eltsIndex); MEDCouplingAutoRefCountObjectPtr d0=DataArrayInt::New(); @@ -639,27 +651,42 @@ namespace ParaMEDMEM void translate(PyObject *vector) throw(INTERP_KERNEL::Exception) { - int sz; - INTERP_KERNEL::AutoPtr v=convertPyToNewDblArr2(vector,&sz); - if(sz!=self->getSpaceDimension()) - { - std::ostringstream oss; oss << "Python wrap of MEDCouplingPointSet::translate : the space dimension is " << self->getSpaceDimension() << " and the input array size is " << sz; - oss << " ! The size of the input list or tuple must be equal to " << self->getSpaceDimension() << " !"; - throw INTERP_KERNEL::Exception(oss.str().c_str()); - } - self->translate(v); + double val; + DataArrayDouble *a; + DataArrayDoubleTuple *aa; + std::vector bb; + int sw; + int spaceDim=self->getSpaceDimension(); + const char msg[]="Python wrap of MEDCouplingPointSet::translate : "; + const double *vectorPtr=convertObjToPossibleCpp5_Safe(vector,sw,val,a,aa,bb,msg,1,spaceDim,true); + self->translate(vectorPtr); + } + + void rotate(PyObject *center, double alpha) throw(INTERP_KERNEL::Exception) + { + const char msg[]="Python wrap of MEDCouplingPointSet::rotate : "; + double val; + DataArrayDouble *a; + DataArrayDoubleTuple *aa; + std::vector bb; + int sw; + int spaceDim=self->getSpaceDimension(); + const double *centerPtr=convertObjToPossibleCpp5_Safe(center,sw,val,a,aa,bb,msg,1,spaceDim,true); + self->rotate(centerPtr,0,alpha); } void rotate(PyObject *center, PyObject *vector, double alpha) throw(INTERP_KERNEL::Exception) { - int sz; - INTERP_KERNEL::AutoPtr c=convertPyToNewDblArr2(center,&sz); - if(!c) - return ; - INTERP_KERNEL::AutoPtr v=convertPyToNewDblArr2(vector,&sz); - if(!v) - { return ; } - self->rotate(c,v,alpha); + const char msg[]="Python wrap of MEDCouplingPointSet::rotate : "; + double val; + DataArrayDouble *a; + DataArrayDoubleTuple *aa; + std::vector bb; + int sw; + int spaceDim=self->getSpaceDimension(); + const double *centerPtr=convertObjToPossibleCpp5_Safe(center,sw,val,a,aa,bb,msg,1,spaceDim,true); + const double *vectorPtr=convertObjToPossibleCpp5_Safe(vector,sw,val,a,aa,bb,msg,1,spaceDim,false); + self->rotate(centerPtr,vectorPtr,alpha); } PyObject *getAllGeoTypes() const throw(INTERP_KERNEL::Exception) @@ -1924,10 +1951,10 @@ namespace ParaMEDMEM int sz; INTERP_KERNEL::AutoPtr orig=convertPyToNewDblArr2(origin,&sz); if(!orig || sz!=3) - throw INTERP_KERNEL::Exception("MEDCouplingUMesh::buildSlice3D : in parameter 1 expecting origin of type list of float of size 3 !"); + throw INTERP_KERNEL::Exception("MEDCouplingUMesh::buildSlice3DSurf : in parameter 1 expecting origin of type list of float of size 3 !"); INTERP_KERNEL::AutoPtr vect=convertPyToNewDblArr2(vec,&sz); if(!vec || sz!=3) - throw INTERP_KERNEL::Exception("MEDCouplingUMesh::buildSlice3D : in parameter 2 expecting vector of type list of float of size 3 !"); + throw INTERP_KERNEL::Exception("MEDCouplingUMesh::buildSlice3DSurf : in parameter 2 expecting vector of type list of float of size 3 !"); DataArrayInt *cellIds=0; MEDCouplingUMesh *ret0=self->buildSlice3DSurf(orig,vect,eps,cellIds); PyObject *ret=PyTuple_New(2); diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index dd85deff3..a7c2e583b 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -1550,7 +1550,7 @@ class MEDCouplingBasicsTest(unittest.TestCase): #2D with no help of bounding box. center=[0.2,0.2] MEDCouplingPointSet.Rotate2DAlg(center,0.78539816339744830962,6,pos); - targetMesh.rotate(center,[],0.78539816339744830962); + targetMesh.rotate(center,0.78539816339744830962); t1=None t2=None t1,t2=targetMesh.getCellsContainingPoints(pos,6,1e-12); @@ -5063,7 +5063,7 @@ class MEDCouplingBasicsTest(unittest.TestCase): f.changeSpaceDimension(2); # center=[0.,0.] - f.rotate(center,[],pi/3); + f.rotate(center,None,pi/3); g=c.buildExtrudedMesh(f,0); g.checkCoherency(); expected1=[ 0.4330127018922193, 0.4330127018922193, 0.649519052838329, 1.2990381056766578, 1.299038105676658, 1.948557158514987, 2.1650635094610955, 2.1650635094610964, 3.2475952641916446, 3.031088913245533, 3.0310889132455352, 4.546633369868303 ] diff --git a/src/MEDCoupling_Swig/MEDCouplingTypemaps.i b/src/MEDCoupling_Swig/MEDCouplingTypemaps.i index 20f6de0c3..429f5884e 100644 --- a/src/MEDCoupling_Swig/MEDCouplingTypemaps.i +++ b/src/MEDCoupling_Swig/MEDCouplingTypemaps.i @@ -1130,3 +1130,148 @@ static void convertObjToPossibleCpp5(PyObject *value, int& sw, double& val, Para } throw INTERP_KERNEL::Exception("4 types accepted : integer, double, DataArrayDouble, DataArrayDoubleTuple"); } + +/*! + * if value int -> cpp val sw=1 + * if value double -> cpp val sw=1 + * if value DataArrayDouble -> cpp DataArrayDouble sw=2 + * if value DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3 + * if value list[int,double] -> cpp std::vector sw=4 + * if value tuple[int,double] -> cpp std::vector sw=4 + */ +static const double *convertObjToPossibleCpp5_Safe(PyObject *value, int& sw, double& val, ParaMEDMEM::DataArrayDouble *&d, ParaMEDMEM::DataArrayDoubleTuple *&e, std::vector& f, + const char *msg, int nbTuplesExpected, int nbCompExpected, bool throwIfNullPt) throw(INTERP_KERNEL::Exception) +{ + sw=-1; + if(PyFloat_Check(value)) + { + val=PyFloat_AS_DOUBLE(value); + sw=1; + if(nbTuplesExpected*nbCompExpected!=1) + { + std::ostringstream oss; oss << msg << "dimension expected to be " << nbTuplesExpected*nbCompExpected << " , and your data in input has dimension one (single PyFloat) !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + return &val; + } + if(PyInt_Check(value)) + { + val=(double)PyInt_AS_LONG(value); + sw=1; + if(nbTuplesExpected*nbCompExpected!=1) + { + std::ostringstream oss; oss << msg << "dimension expected to be " << nbTuplesExpected*nbCompExpected << " , and your data in input has dimension one (single PyInt) !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + return &val; + } + if(PyTuple_Check(value)) + { + int size=PyTuple_Size(value); + f.resize(size); + for(int i=0;i(argp); + sw=2; + if(d) + { + if(d->getNumberOfTuples()==nbTuplesExpected) + { + if(d->getNumberOfComponents()==nbCompExpected) + { + return d->getConstPointer(); + } + else + { + std::ostringstream oss; oss << msg << "nb of components expected to be " << nbCompExpected << " , and input has " << d->getNumberOfComponents() << " components !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + } + else + { + std::ostringstream oss; oss << msg << " input DataArrayDouble should have a number of tuples equal to " << nbTuplesExpected << " and there are " << d->getNumberOfTuples() << " tuples !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + } + else + { + if(throwIfNullPt) + { + std::ostringstream oss; oss << msg << " null pointer not accepted!"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + else + return 0; + } + } + status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDoubleTuple,0|0); + if(SWIG_IsOK(status)) + { + e=reinterpret_cast< ParaMEDMEM::DataArrayDoubleTuple * >(argp); + sw=3; + if(e->getNumberOfCompo()==nbCompExpected) + { + if(nbTuplesExpected==1) + return e->getConstPointer(); + else + { + std::ostringstream oss; oss << msg << "nb of tuples expected to be " << nbTuplesExpected << " , and input DataArrayDoubleTuple has always one tuple by contruction !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + } + else + { + std::ostringstream oss; oss << msg << "nb of components expected to be " << nbCompExpected << " , and input DataArrayDoubleTuple has " << e->getNumberOfCompo() << " components !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + } + throw INTERP_KERNEL::Exception("4 types accepted : integer, double, DataArrayDouble, DataArrayDoubleTuple"); +}