From: ageay Date: Wed, 29 Feb 2012 11:56:07 +0000 (+0000) Subject: All C++ methods that return std::vector that can be huge, are wrapped now in DataArra... X-Git-Tag: V6_main_FINAL~811 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=b0093dbaa3ea2e26effb589c56d5e527b0a3a5d4;p=tools%2Fmedcoupling.git All C++ methods that return std::vector that can be huge, are wrapped now in DataArrayInt instance instead of PyList. --- diff --git a/src/MEDCoupling_Swig/MEDCoupling.i b/src/MEDCoupling_Swig/MEDCoupling.i index 366fe721c..63ebd1c16 100644 --- a/src/MEDCoupling_Swig/MEDCoupling.i +++ b/src/MEDCoupling_Swig/MEDCoupling.i @@ -304,6 +304,10 @@ using namespace INTERP_KERNEL; %ignore ParaMEDMEM::DataArrayDoubleIterator::nextt; %ignore ParaMEDMEM::DataArrayDoubleTuple::next; %ignore ParaMEDMEM::DataArrayDoubleTuple::repr; +%ignore ParaMEDMEM::DataArrayDouble::writeVTK; +%ignore ParaMEDMEM::DataArrayInt::writeVTK; +%ignore ParaMEDMEM::DataArrayDouble::SetArrayIn; +%ignore ParaMEDMEM::DataArrayInt::SetArrayIn; %nodefaultctor; @@ -450,7 +454,10 @@ namespace ParaMEDMEM INTERP_KERNEL::AutoPtr pos=convertPyToNewDblArr2(p,&sz); std::vector elts; self->getCellsContainingPoint(pos,eps,elts); - return convertIntArrToPyList2(elts); + DataArrayInt *ret=DataArrayInt::New(); + ret->alloc((int)elts.size(),1); + std::copy(elts.begin(),elts.end(),ret->getPointer()); + return SWIG_NewPointerObj(SWIG_as_voidptr(ret),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 ); } void renumberCells(PyObject *li, bool check) throw(INTERP_KERNEL::Exception) @@ -788,7 +795,10 @@ namespace ParaMEDMEM { std::vector nodes; self->findBoundaryNodes(nodes); - return convertIntArrToPyList2(nodes); + DataArrayInt *ret=DataArrayInt::New(); + ret->alloc((int)nodes.size(),1); + std::copy(nodes.begin(),nodes.end(),ret->getPointer()); + return SWIG_NewPointerObj(SWIG_as_voidptr(ret),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 ); } void renumberNodes(PyObject *li, int newNbOfNodes) throw(INTERP_KERNEL::Exception) { @@ -840,7 +850,10 @@ namespace ParaMEDMEM self->findNodesOnLine(p,v,eps,nodes); delete [] v; delete [] p; - return convertIntArrToPyList2(nodes); + DataArrayInt *ret=DataArrayInt::New(); + ret->alloc((int)nodes.size(),1); + std::copy(nodes.begin(),nodes.end(),ret->getPointer()); + return SWIG_NewPointerObj(SWIG_as_voidptr(ret),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 ); } PyObject *findNodesOnPlane(PyObject *pt, PyObject *vec, double eps) const throw(INTERP_KERNEL::Exception) { @@ -851,7 +864,10 @@ namespace ParaMEDMEM self->findNodesOnPlane(p,v,eps,nodes); delete [] v; delete [] p; - return convertIntArrToPyList2(nodes); + DataArrayInt *ret=DataArrayInt::New(); + ret->alloc((int)nodes.size(),1); + std::copy(nodes.begin(),nodes.end(),ret->getPointer()); + return SWIG_NewPointerObj(SWIG_as_voidptr(ret),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 ); } PyObject *getNodeIdsNearPoint(PyObject *pt, double eps) const throw(INTERP_KERNEL::Exception) { @@ -860,7 +876,10 @@ namespace ParaMEDMEM if(sizegetSpaceDimension()) throw INTERP_KERNEL::Exception("getNodeIdsNearPoint : to tiny array ! must be at least of size SpaceDim !"); std::vector tmp=self->getNodeIdsNearPoint(pos,eps); - return convertIntArrToPyList2(tmp); + DataArrayInt *ret=DataArrayInt::New(); + ret->alloc((int)tmp.size(),1); + std::copy(tmp.begin(),tmp.end(),ret->getPointer()); + return SWIG_NewPointerObj(SWIG_as_voidptr(ret),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 ); } PyObject *getNodeIdsNearPoints(PyObject *pt, int nbOfNodes, double eps) const throw(INTERP_KERNEL::Exception) @@ -892,7 +911,10 @@ namespace ParaMEDMEM double *tmp=convertPyToNewDblArr2(bbox,&size); self->getCellsInBoundingBox(tmp,eps,elems); delete [] tmp; - return convertIntArrToPyList2(elems); + DataArrayInt *ret=DataArrayInt::New(); + ret->alloc((int)elems.size(),1); + std::copy(elems.begin(),elems.end(),ret->getPointer()); + return SWIG_NewPointerObj(SWIG_as_voidptr(ret),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 ); } static void Rotate2DAlg(PyObject *center, double angle, int nbNodes, PyObject *coords) throw(INTERP_KERNEL::Exception) @@ -1121,7 +1143,10 @@ namespace ParaMEDMEM { std::vector cells; self->checkButterflyCells(cells); - return convertIntArrToPyList2(cells); + DataArrayInt *ret=DataArrayInt::New(); + ret->alloc((int)cells.size(),1); + std::copy(cells.begin(),cells.end(),ret->getPointer()); + return SWIG_NewPointerObj(SWIG_as_voidptr(ret),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 ); } PyObject *splitByType() const throw(INTERP_KERNEL::Exception) @@ -1210,7 +1235,10 @@ namespace ParaMEDMEM throw e; } delete [] v; - return convertIntArrToPyList2(cells); + DataArrayInt *ret=DataArrayInt::New(); + ret->alloc((int)cells.size(),1); + std::copy(cells.begin(),cells.end(),ret->getPointer()); + return SWIG_NewPointerObj(SWIG_as_voidptr(ret),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 ); } void orientCorrectly2DCells(PyObject *vec, bool polyOnly) throw(INTERP_KERNEL::Exception) @@ -1233,14 +1261,20 @@ namespace ParaMEDMEM { std::vector cells; self->arePolyhedronsNotCorrectlyOriented(cells); - return convertIntArrToPyList2(cells); + DataArrayInt *ret=DataArrayInt::New(); + ret->alloc((int)cells.size(),1); + std::copy(cells.begin(),cells.end(),ret->getPointer()); + return SWIG_NewPointerObj(SWIG_as_voidptr(ret),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 ); } PyObject *findAndCorrectBadOriented3DExtrudedCells() throw(INTERP_KERNEL::Exception) { std::vector cells; self->findAndCorrectBadOriented3DExtrudedCells(cells); - return convertIntArrToPyList2(cells); + DataArrayInt *ret=DataArrayInt::New(); + ret->alloc((int)cells.size(),1); + std::copy(cells.begin(),cells.end(),ret->getPointer()); + return SWIG_NewPointerObj(SWIG_as_voidptr(ret),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 ); } PyObject *getFastAveragePlaneOfThis() const throw(INTERP_KERNEL::Exception) @@ -1478,8 +1512,38 @@ namespace ParaMEDMEM throw INTERP_KERNEL::Exception("MEDCouplingUMesh::getCellIdsCrossingPlane : in parameter 2 expecting vector of type list of float of size 3 !"); return self->getCellIdsCrossingPlane(orig,vect,eps); } + + void convertToPolyTypes(PyObject *li) throw(INTERP_KERNEL::Exception) + { + int sw; + int pos1; + std::vector pos2; + DataArrayInt *pos3=0; + convertObjToPossibleCpp1(li,sw,pos1,pos2,pos3); + switch(sw) + { + case 1: + { + self->convertToPolyTypes(&pos1,&pos1+1); + return; + } + case 2: + { + if(pos2.empty()) + return; + self->convertToPolyTypes(&pos2[0],&pos2[0]+pos2.size()); + return ; + } + case 3: + { + self->convertToPolyTypes(pos3->begin(),pos3->end()); + return ; + } + default: + throw INTERP_KERNEL::Exception("MEDCouplingUMesh::convertToPolyTypes : unexpected input array type recognized !"); + } + } } - void convertToPolyTypes(const std::vector& cellIdsToConvert) throw(INTERP_KERNEL::Exception); void convertAllToPoly(); void convertExtrudedPolyhedra() throw(INTERP_KERNEL::Exception); void unPolyze() throw(INTERP_KERNEL::Exception); @@ -3127,6 +3191,20 @@ namespace ParaMEDMEM return self->repr(); } + int __len__() const throw(INTERP_KERNEL::Exception) + { + if(self->isAllocated()) + { + if(self->getNumberOfComponents()==1) + return self->getNumberOfTuples(); + throw INTERP_KERNEL::Exception("DataArrayInt::__len__ : len is not defined here because number of components is not equal to 1 !"); + } + else + { + throw INTERP_KERNEL::Exception("DataArrayInt::__len__ : Instance is NOT allocated !"); + } + } + DataArrayIntIterator *__iter__() { return self->iterator(); @@ -4448,7 +4526,10 @@ namespace ParaMEDMEM { std::vector tmp; self->getCellIdsHavingGaussLocalization(locId,tmp); - return convertIntArrToPyList2(tmp); + DataArrayInt *ret=DataArrayInt::New(); + ret->alloc((int)tmp.size(),1); + std::copy(tmp.begin(),tmp.end(),ret->getPointer()); + return SWIG_NewPointerObj(SWIG_as_voidptr(ret),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 ); } } }; diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index 09de918dd..6db802084 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -157,7 +157,7 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertEqual(targetMesh.getNumberOfNodes(),9) self.assertEqual(targetMesh.getMeshDimension(),0) pass - + def testMeshM1D(self): meshM1D=MEDCouplingUMesh.New(); self.assertRaises(InterpKernelException,meshM1D.getMeshDimension); @@ -226,7 +226,7 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertEqual(8,mesh.getNumberOfCells()); self.assertEqual(114,mesh.getNodalConnectivity().getNumberOfTuples()); pass - + def testDescConn2D(self): mesh=MEDCouplingDataForTest.build2DTargetMesh_1(); desc=DataArrayInt.New(); @@ -341,21 +341,21 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertEqual(expected5,list(mesh2.getNodalConnectivityIndex().getValues())); self.assertEqual(expected7,list(mesh2.getNodalConnectivity().getValues())); pass - + def testFindBoundaryNodes(self): mesh=MEDCouplingDataForTest.build3DTargetMesh_1(); boundaryNodes=mesh.findBoundaryNodes(); expected1=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]; - self.assertEqual(expected1,list(boundaryNodes)); + self.assertEqual(expected1,boundaryNodes.getValues()); pass - + def testBoundaryMesh(self): mesh=MEDCouplingDataForTest.build3DTargetMesh_1(); mesh2=mesh.buildBoundaryMesh(False); self.assertEqual(24,mesh2.getNumberOfCells()); self.assertEqual(26,mesh2.getNumberOfNodes()); pass - + def testBuildPartOfMySelf(self): mesh=MEDCouplingDataForTest.build2DTargetMesh_1(); mesh.setName("Toto"); @@ -873,7 +873,7 @@ class MEDCouplingBasicsTest(unittest.TestCase): conn.setIJ(16,0,tmp) cells=sourceMesh.checkButterflyCells(); self.assertEqual(1,len(cells)); - self.assertEqual(3,cells[0]); + self.assertEqual([3],cells.getValues()); tmp=conn.getIJ(15,0) conn.setIJ(15,0,conn.getIJ(16,0)) conn.setIJ(16,0,tmp) @@ -889,7 +889,7 @@ class MEDCouplingBasicsTest(unittest.TestCase): conn.setIJ(16,0,tmp) cells=sourceMesh.checkButterflyCells(); self.assertEqual(1,len(cells)); - self.assertEqual(3,cells[0]); + self.assertEqual([3],cells.getValues()); tmp=conn.getIJ(15,0) conn.setIJ(15,0,conn.getIJ(16,0)) conn.setIJ(16,0,tmp) @@ -1568,13 +1568,13 @@ class MEDCouplingBasicsTest(unittest.TestCase): t1=targetMesh.getCellsContainingPoint(pos2,1e-12) self.assertEqual(2,len(t1)); expectedValues3=[0,1] - self.assertEqual(list(t1),expectedValues3); + self.assertEqual(list(t1.getValues()),expectedValues3); pos3=[0.2,0.2] t1=None t1=targetMesh.getCellsContainingPoint(pos3,1e-12); self.assertEqual(5,len(t1)); expectedValues4=[0,1,2,3,4] - self.assertEqual(list(t1),expectedValues4); + self.assertEqual(list(t1.getValues()),expectedValues4); self.assertEqual(0,targetMesh.getCellContainingPoint(pos3,1e-12)); #3D targetMesh=MEDCouplingDataForTest.build3DTargetMesh_1(); @@ -1585,13 +1585,13 @@ class MEDCouplingBasicsTest(unittest.TestCase): t1=targetMesh.getCellsContainingPoint(pos5,1e-12); self.assertEqual(8,len(t1)); expectedValues5=[0,1,2,3,4,5,6,7] - self.assertEqual(list(t1),expectedValues5); + self.assertEqual(list(t1.getValues()),expectedValues5); pos6=[0., 50., 0.] t1=None t1=targetMesh.getCellsContainingPoint(pos6,1e-12); self.assertEqual(2,len(t1)); expectedValues6=[0,2] - self.assertEqual(list(t1),expectedValues6); + self.assertEqual(list(t1.getValues()),expectedValues6); #3D outside pos7=[-1.0,-1.0,0.] self.assertEqual(-1,targetMesh.getCellContainingPoint(pos7,1e-12)); @@ -1924,9 +1924,7 @@ class MEDCouplingBasicsTest(unittest.TestCase): val=da.getValues(); self.assertEqual(expected,list(val)); # - ddd=DataArrayInt.New() - ddd.setValues(n,len(n),1) - m3dSurf=mesh.buildFacePartOfMySelfNode(ddd,True); + m3dSurf=mesh.buildFacePartOfMySelfNode(n,True); self.assertTrue(isinstance(m3dSurf,MEDCouplingUMesh)) me=MEDCouplingExtrudedMesh.New(mesh,m3dSurf,0); da=me.getMesh3DIds(); @@ -2036,7 +2034,7 @@ class MEDCouplingBasicsTest(unittest.TestCase): f.setGaussLocalizationOnCells(ids4,_refCoo2,_gsCoo2,_wg2); self.assertEqual(3,f.getNbOfGaussLocalization()); tmpIds=f.getCellIdsHavingGaussLocalization(0); - self.assertEqual(ids2,list(tmpIds)); + self.assertEqual(ids2,list(tmpIds.getValues())); self.assertRaises(InterpKernelException,f.checkCoherency);#<- it's always not ok because undelying array not with the good size. array2=f.getArray().substr(0,10); f.setArray(array2); @@ -2096,7 +2094,7 @@ class MEDCouplingBasicsTest(unittest.TestCase): m.getNodalConnectivity().setValues(conn,len(conn),1) res1=m.are2DCellsNotCorrectlyOriented(vec,False); self.assertEqual(1,len(res1)); - self.assertEqual(2,res1[0]); + self.assertEqual(2,res1.getValues()[0]); m.orientCorrectly2DCells(vec,False); res1=m.are2DCellsNotCorrectlyOriented(vec,False); self.assertTrue(len(res1)==0); @@ -4340,7 +4338,7 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertEqual(16,m.getCellContainingPoint(pos1,1e-12)); # elems=m2.getCellsInBoundingBox([3.5,6.,12.2,25.,0.,1.5],1e-7) - self.assertEqual([1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17],elems) + self.assertEqual([1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17],elems.getValues()) # pt=[2.4,12.7,-3.4] m.scale(pt,3.7); @@ -4941,7 +4939,6 @@ class MEDCouplingBasicsTest(unittest.TestCase): pass def testElementaryDAThrowAndSpecialCases(self): - return da=DataArrayInt.New(); self.assertRaises(InterpKernelException, da.checkAllocated); self.assertRaises(InterpKernelException, da.fillWithValue, 1); @@ -5046,7 +5043,6 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertEqual(5,db.GetNumberOfItemGivenBES(0,10,2,"theMessageInThrow")); self.assertEqual(6,db.GetNumberOfItemGivenBES(0,11,2,"theMessageInThrow")); - #std::cout<<"\n!!!!!!!!!\n"< a(0); #input list must be NON EMPTY a=[] self.assertRaises(InterpKernelException, DataArrayDouble_Aggregate, a); self.assertRaises(InterpKernelException, DataArrayDouble_Meld, a); - a.push_back(dbl2); - a.push_back(dbl); #Nb of components mismatch + a=[dbl2,dbl]; #Nb of components mismatch self.assertRaises(InterpKernelException, DataArrayDouble_Aggregate, a); self.assertRaises(InterpKernelException, DataArrayDouble_Dot, dbl2, dbl); @@ -5325,7 +5309,7 @@ class MEDCouplingBasicsTest(unittest.TestCase): mesh.setCoords(tmp2); pts=[0.2,0.2,0.1,0.3,-0.3,0.7] c=mesh.getNodeIdsNearPoint(pts,1e-7); - self.assertEqual([4,9,11],c); + self.assertEqual([4,9,11],c.getValues()); c,cI=mesh.getNodeIdsNearPoints(pts,3,1e-7); self.assertEqual([0,3,3,4],cI.getValues()); self.assertEqual([4,9,11,6],c.getValues()); @@ -8249,7 +8233,7 @@ class MEDCouplingBasicsTest(unittest.TestCase): # v=m.findAndCorrectBadOriented3DExtrudedCells(); self.assertEqual(4,len(v)); - self.assertEqual(v,invalidCells); + self.assertEqual(v.getValues(),invalidCells); self.assertEqual(connExp,m.getNodalConnectivity().getValues()); # pass @@ -8598,13 +8582,13 @@ class MEDCouplingBasicsTest(unittest.TestCase): expected1=[0,3,6] res=mesh.findNodesOnLine(pt,vec,1e-12); self.assertEqual(3,len(res)); - self.assertEqual(expected1,res); + self.assertEqual(expected1,res.getValues()); # mesh.changeSpaceDimension(3); mesh.rotate(pt2,vec2,pi/4.); res=mesh.findNodesOnLine(pt3,vec3,1e-12); self.assertEqual(3,len(res)); - self.assertEqual(expected1,res); + self.assertEqual(expected1,res.getValues()); pass def testIntersect2DMeshesTmp2(self): @@ -9149,7 +9133,7 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertRaises(InterpKernelException,m1Cpy.are2DCellsNotCorrectlyOriented,vec1,False); m1Cpy.changeSpaceDimension(3); res=m1Cpy.are2DCellsNotCorrectlyOriented(vec1,False) - self.assertEqual([0],res); + self.assertEqual([0],res.getValues()); pass pass