From: ageay Date: Fri, 15 Jun 2012 15:22:42 +0000 (+0000) Subject: Implementation of MEDCouplingUMesh::giveCellsWithType X-Git-Tag: V6_main_FINAL~643 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=614b1135d08a0029087b4e7de5dba64eab860fef;p=tools%2Fmedcoupling.git Implementation of MEDCouplingUMesh::giveCellsWithType --- diff --git a/src/MEDCoupling/MEDCouplingUMesh.cxx b/src/MEDCoupling/MEDCouplingUMesh.cxx index ce5d394fc..8dc28fdd5 100644 --- a/src/MEDCoupling/MEDCouplingUMesh.cxx +++ b/src/MEDCoupling/MEDCouplingUMesh.cxx @@ -2221,6 +2221,37 @@ INTERP_KERNEL::NormalizedCellType MEDCouplingUMesh::getTypeOfCell(int cellId) co return (INTERP_KERNEL::NormalizedCellType) pt[ptI[cellId]]; } +/*! + * This method returns a newly allocated array containing cell ids (ascendingly sorted) whose geometric type are equal to type. + * This method throws an INTERP_KERNEL::Exception if meshdimension of \b this is not equal to those of \b type. + * The coordinates array is not considered here. + * + * \param [in] type the geometric type + * \return the + */ +DataArrayInt *MEDCouplingUMesh::giveCellsWithType(INTERP_KERNEL::NormalizedCellType type) const throw(INTERP_KERNEL::Exception) +{ + + std::vector v; + checkConnectivityFullyDefined(); + int nbCells=getNumberOfCells(); + int mdim=getMeshDimension(); + const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(type); + if(mdim!=(int)cm.getDimension()) + throw INTERP_KERNEL::Exception("MEDCouplingUMesh::giveCellsWithType : Mismatch between mesh dimension and dimension of the cell !"); + const int *ptI=_nodal_connec_index->getConstPointer(); + const int *pt=_nodal_connec->getConstPointer(); + for(int i=0;i ret=DataArrayInt::New(); ret->alloc((int)v.size(),1); + std::copy(v.begin(),v.end(),ret->getPointer()); + ret->incrRef(); + return ret; +} + /*! * Returns nb of cells having the geometric type 'type'. */ diff --git a/src/MEDCoupling/MEDCouplingUMesh.hxx b/src/MEDCoupling/MEDCouplingUMesh.hxx index d3019d2ad..dc805356a 100644 --- a/src/MEDCoupling/MEDCouplingUMesh.hxx +++ b/src/MEDCoupling/MEDCouplingUMesh.hxx @@ -67,6 +67,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT DataArrayInt *getNodalConnectivity() { return _nodal_connec; } MEDCOUPLING_EXPORT DataArrayInt *getNodalConnectivityIndex() { return _nodal_connec_index; } MEDCOUPLING_EXPORT INTERP_KERNEL::NormalizedCellType getTypeOfCell(int cellId) const; + MEDCOUPLING_EXPORT DataArrayInt *giveCellsWithType(INTERP_KERNEL::NormalizedCellType type) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT int getNumberOfCellsWithType(INTERP_KERNEL::NormalizedCellType type) const; MEDCOUPLING_EXPORT void getNodeIdsOfCell(int cellId, std::vector& conn) const; MEDCOUPLING_EXPORT DataArrayInt *getCellIdsFullyIncludedInNodeIds(const int *partBg, const int *partEnd) const; diff --git a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx index 2b2824c5a..7e52bc1bf 100644 --- a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx +++ b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx @@ -1176,3 +1176,79 @@ void MEDCouplingBasicsTest5::testDataArraySort1() ard2->decrRef(); ard->decrRef(); } + +void MEDCouplingBasicsTest5::testPartitionBySpreadZone1() +{ + MEDCouplingUMesh *m=build2DTargetMesh_1(); + const int part0[3]={2,3,4}; + const int part1[2]={0,1}; + MEDCouplingUMesh *m1=static_cast(m->buildPartOfMySelf(part0,part0+3)); + MEDCouplingUMesh *m2=static_cast(m->buildPartOfMySelf(part1,part1+2)); + std::vector v(3); v[0]=m; v[1]=m1; v[2]=m2; + MEDCouplingUMesh *m4=MEDCouplingUMesh::MergeUMeshes(v); + const int renum[10]={5,2,9,6,4,7,0,1,3,8}; + m4->renumberCells(renum); + // + std::vector v2=m4->partitionBySpreadZone(); + CPPUNIT_ASSERT_EQUAL(3,(int)v2.size()); + const int expected0[3]={0,1,7}; + const int expected1[5]={2,4,5,6,9}; + const int expected2[2]={3,8}; + CPPUNIT_ASSERT_EQUAL(3,v2[0]->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(1,v2[0]->getNumberOfComponents()); + CPPUNIT_ASSERT_EQUAL(5,v2[1]->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(1,v2[1]->getNumberOfComponents()); + CPPUNIT_ASSERT_EQUAL(2,v2[2]->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(1,v2[2]->getNumberOfComponents()); + // + CPPUNIT_ASSERT(std::equal(expected0,expected0+3,v2[0]->getConstPointer())); + CPPUNIT_ASSERT(std::equal(expected1,expected1+5,v2[1]->getConstPointer())); + CPPUNIT_ASSERT(std::equal(expected2,expected2+2,v2[2]->getConstPointer())); + v2[0]->decrRef(); + v2[1]->decrRef(); + v2[2]->decrRef(); + // + MEDCouplingUMesh *m5=m4->buildSpreadZonesWithPoly(); + CPPUNIT_ASSERT_EQUAL(3,m5->getNumberOfCells()); + CPPUNIT_ASSERT(m5->getCoords()==m4->getCoords()); + const int expected3[23]={5,15,16,17,14,11,13,12,5,2,1,0,3,6,7,8,5,5,18,21,22,20,19}; + const int expected4[4]={0,8,17,23}; + CPPUNIT_ASSERT_EQUAL(23,m5->getNodalConnectivity()->getNumberOfTuples()); + CPPUNIT_ASSERT(std::equal(expected3,expected3+23,m5->getNodalConnectivity()->getConstPointer())); + CPPUNIT_ASSERT_EQUAL(4,m5->getNodalConnectivityIndex()->getNumberOfTuples()); + CPPUNIT_ASSERT(std::equal(expected4,expected4+4,m5->getNodalConnectivityIndex()->getConstPointer())); + // + m->decrRef(); + m1->decrRef(); + m2->decrRef(); + m4->decrRef(); + m5->decrRef(); +} + +void MEDCouplingBasicsTest5::testGiveCellsWithType1() +{ + const int expected0[2]={1,2}; + const int expected1[3]={0,3,4}; + MEDCouplingUMesh *m=build2DTargetMesh_1(); + DataArrayInt *da=m->giveCellsWithType(INTERP_KERNEL::NORM_TRI3); + CPPUNIT_ASSERT_EQUAL(2,da->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(1,da->getNumberOfComponents()); + CPPUNIT_ASSERT(std::equal(expected0,expected0+2,da->getConstPointer())); + da->decrRef(); + // + da=m->giveCellsWithType(INTERP_KERNEL::NORM_QUAD4); + CPPUNIT_ASSERT_EQUAL(3,da->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(1,da->getNumberOfComponents()); + CPPUNIT_ASSERT(std::equal(expected1,expected1+3,da->getConstPointer())); + da->decrRef(); + // + da=m->giveCellsWithType(INTERP_KERNEL::NORM_TRI6); + CPPUNIT_ASSERT_EQUAL(0,da->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(1,da->getNumberOfComponents()); + da->decrRef(); + // + CPPUNIT_ASSERT_THROW(m->giveCellsWithType(INTERP_KERNEL::NORM_SEG2),INTERP_KERNEL::Exception); + CPPUNIT_ASSERT_THROW(m->giveCellsWithType(INTERP_KERNEL::NORM_HEXA8),INTERP_KERNEL::Exception); + // + m->decrRef(); +} diff --git a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx index ad4de0c8a..d7a2b8ca4 100644 --- a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx +++ b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx @@ -56,6 +56,8 @@ namespace ParaMEDMEM CPPUNIT_TEST( testZipConnectivityPol1 ); CPPUNIT_TEST( testConvexEnvelop2D1 ); CPPUNIT_TEST( testDataArraySort1 ); + CPPUNIT_TEST( testPartitionBySpreadZone1 ); + CPPUNIT_TEST( testGiveCellsWithType1 ); CPPUNIT_TEST_SUITE_END(); public: void testUMeshTessellate2D1(); @@ -79,6 +81,8 @@ namespace ParaMEDMEM void testZipConnectivityPol1(); void testConvexEnvelop2D1(); void testDataArraySort1(); + void testPartitionBySpreadZone1(); + void testGiveCellsWithType1(); }; } diff --git a/src/MEDCoupling_Swig/MEDCoupling.i b/src/MEDCoupling_Swig/MEDCoupling.i index 69b26c452..33403f8e9 100644 --- a/src/MEDCoupling_Swig/MEDCoupling.i +++ b/src/MEDCoupling_Swig/MEDCoupling.i @@ -248,6 +248,7 @@ using namespace INTERP_KERNEL; %newobject ParaMEDMEM::MEDCouplingUMesh::__iter__; %newobject ParaMEDMEM::MEDCouplingUMesh::__getitem__; %newobject ParaMEDMEM::MEDCouplingUMesh::cellsByType; +%newobject ParaMEDMEM::MEDCouplingUMesh::giveCellsWithType; %newobject ParaMEDMEM::MEDCouplingUMesh::zipConnectivityTraducer; %newobject ParaMEDMEM::MEDCouplingUMesh::buildDescendingConnectivity; %newobject ParaMEDMEM::MEDCouplingUMesh::buildDescendingConnectivity2; @@ -1075,6 +1076,7 @@ namespace ParaMEDMEM int getNumberOfNodesInCell(int cellId) const throw(INTERP_KERNEL::Exception); int getMeshLength() const throw(INTERP_KERNEL::Exception); void computeTypes() throw(INTERP_KERNEL::Exception); + DataArrayInt *giveCellsWithType(INTERP_KERNEL::NormalizedCellType type) const throw(INTERP_KERNEL::Exception); std::string reprConnectivityOfThis() const throw(INTERP_KERNEL::Exception); MEDCouplingUMesh *buildSetInstanceFromThis(int spaceDim) const throw(INTERP_KERNEL::Exception); //tools diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index 156275851..b0e4bd756 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -9816,6 +9816,47 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertAlmostEqual(expected4[i],ard2.getIJ(i,0),12) pass pass + + def testPartitionBySpreadZone1(self): + m=MEDCouplingDataForTest.build2DTargetMesh_1(); + m4=MEDCouplingUMesh.MergeUMeshes([m,m[2:],m[0:2]]); + m4.renumberCells([5,2,9,6,4,7,0,1,3,8]); + # + v2=m4.partitionBySpreadZone(); + self.assertTrue(3,len(v2)); + self.assertTrue(v2[0].isEqual(DataArrayInt.New([0,1,7]))) + self.assertTrue(v2[1].isEqual(DataArrayInt.New([2,4,5,6,9]))) + self.assertTrue(v2[2].isEqual(DataArrayInt.New([3,8]))) + # + m5=m4.buildSpreadZonesWithPoly(); + self.assertEqual(3,m5.getNumberOfCells()); + self.assertTrue(m5.getCoords().getHiddenCppPointer()==m4.getCoords().getHiddenCppPointer()); + self.assertEqual([5,15,16,17,14,11,13,12,5,2,1,0,3,6,7,8,5,5,18,21,22,20,19],m5.getNodalConnectivity().getValues()) + self.assertEqual([0,8,17,23],m5.getNodalConnectivityIndex().getValues()) + # + pass + + def testGiveCellsWithType1(self): + expected0=[1,2] + expected1=[0,3,4] + m=MEDCouplingDataForTest.build2DTargetMesh_1(); + da=m.giveCellsWithType(NORM_TRI3); + self.assertEqual(2,da.getNumberOfTuples()); + self.assertEqual(1,da.getNumberOfComponents()); + self.assertEqual(expected0,da.getValues()) + # + da=m.giveCellsWithType(NORM_QUAD4); + self.assertEqual(3,da.getNumberOfTuples()); + self.assertEqual(1,da.getNumberOfComponents()); + self.assertEqual(expected1,da.getValues()) + # + da=m.giveCellsWithType(NORM_TRI6); + self.assertEqual(0,da.getNumberOfTuples()); + self.assertEqual(1,da.getNumberOfComponents()); + # + self.assertRaises(InterpKernelException,m.giveCellsWithType,NORM_SEG2) + self.assertRaises(InterpKernelException,m.giveCellsWithType,NORM_HEXA8) + pass def setUp(self): pass