]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
Swig improvements on wrapping of input double *.
authorageay <ageay>
Thu, 28 Jun 2012 15:50:33 +0000 (15:50 +0000)
committerageay <ageay>
Thu, 28 Jun 2012 15:50:33 +0000 (15:50 +0000)
src/MEDCoupling_Swig/MEDCoupling.i
src/MEDCoupling_Swig/MEDCouplingBasicsTest.py
src/MEDCoupling_Swig/MEDCouplingTypemaps.i

index 6bbfbe1a79d6684bd7f263c9908cbd586a2aa344..32568a426453180d746f8d81700152763f2088a6 100644 (file)
@@ -435,15 +435,27 @@ namespace ParaMEDMEM
 
          int getCellContainingPoint(PyObject *p, double eps) const throw(INTERP_KERNEL::Exception)
          {
-           int sz;
-           INTERP_KERNEL::AutoPtr<double> pos=convertPyToNewDblArr2(p,&sz);
+           double val;
+           DataArrayDouble *a;
+           DataArrayDoubleTuple *aa;
+           std::vector<double> 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<double> pos=convertPyToNewDblArr2(p,&sz);
+           double val;
+           DataArrayDouble *a;
+           DataArrayDoubleTuple *aa;
+           std::vector<double> 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<int> elts,eltsIndex;
            self->getCellsContainingPoints(pos,nbOfPoints,eps,elts,eltsIndex);
            MEDCouplingAutoRefCountObjectPtr<DataArrayInt> d0=DataArrayInt::New();
@@ -639,27 +651,42 @@ namespace ParaMEDMEM
 
          void translate(PyObject *vector) throw(INTERP_KERNEL::Exception)
          {
-           int sz;
-           INTERP_KERNEL::AutoPtr<double> 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<double> 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<double> 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<double> c=convertPyToNewDblArr2(center,&sz);
-           if(!c)
-             return ;
-           INTERP_KERNEL::AutoPtr<double> 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<double> 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<double> 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<double> 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);
index dd85deff3cd71efce3d9ae1603a9c090c5662033..a7c2e583bd7b5491d2515bb3187fe2e80b68975c 100644 (file)
@@ -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 ]
index 20f6de0c3636dcc232c15802dcfd512d51e6698b..429f5884e9734f1f188930e74d601a2cae784ded 100644 (file)
@@ -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<double> sw=4
+ * if value tuple[int,double] -> cpp std::vector<double> sw=4
+ */
+static const double *convertObjToPossibleCpp5_Safe(PyObject *value, int& sw, double& val, ParaMEDMEM::DataArrayDouble *&d, ParaMEDMEM::DataArrayDoubleTuple *&e, std::vector<double>& 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<size;i++)
+        {
+          PyObject *o=PyTuple_GetItem(value,i);
+          if(PyFloat_Check(o))
+            f[i]=PyFloat_AS_DOUBLE(o);
+          else if(PyInt_Check(o))
+            f[i]=(double)PyInt_AS_LONG(o);
+          else
+            {
+              std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not double ! only tuples of doubles accepted or integer !";
+              throw INTERP_KERNEL::Exception(oss.str().c_str());
+            }
+        }
+      sw=4;
+      if(nbTuplesExpected*nbCompExpected!=(int)f.size())
+        {
+          std::ostringstream oss; oss << msg << "dimension expected to be " << nbTuplesExpected*nbCompExpected << " , and your data in input has dimension " << f.size() << " !"; 
+          throw INTERP_KERNEL::Exception(oss.str().c_str());
+        }
+      return &f[0];
+    }
+  if(PyList_Check(value))
+    {
+      int size=PyList_Size(value);
+      f.resize(size);
+      for(int i=0;i<size;i++)
+        {
+          PyObject *o=PyList_GetItem(value,i);
+          if(PyFloat_Check(o))
+            f[i]=PyFloat_AS_DOUBLE(o);
+          else if(PyInt_Check(o))
+            f[i]=(double)PyInt_AS_LONG(o);
+          else
+            {
+              std::ostringstream oss; oss << "List as been detected but element #" << i << " is not double ! only lists of doubles accepted or integer !";
+              throw INTERP_KERNEL::Exception(oss.str().c_str());
+            }
+        }
+      sw=4;
+      if(nbTuplesExpected*nbCompExpected!=(int)f.size())
+        {
+          std::ostringstream oss; oss << msg << "dimension expected to be " << nbTuplesExpected*nbCompExpected << " , and your data in input has dimension " << f.size() << " !"; 
+          throw INTERP_KERNEL::Exception(oss.str().c_str());
+        }
+      return &f[0];
+    }
+  void *argp;
+  int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,0|0);
+  if(SWIG_IsOK(status))
+    {  
+      d=reinterpret_cast< ParaMEDMEM::DataArrayDouble * >(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");
+}