]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
Implementation of MEDCouplingUMesh::giveCellsWithType
authorageay <ageay>
Fri, 15 Jun 2012 15:22:42 +0000 (15:22 +0000)
committerageay <ageay>
Fri, 15 Jun 2012 15:22:42 +0000 (15:22 +0000)
src/MEDCoupling/MEDCouplingUMesh.cxx
src/MEDCoupling/MEDCouplingUMesh.hxx
src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx
src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx
src/MEDCoupling_Swig/MEDCoupling.i
src/MEDCoupling_Swig/MEDCouplingBasicsTest.py

index ce5d394fc81aefb2209bdca94dfed3c91b48cb79..8dc28fdd531a2248d8f915f0845605135b158522 100644 (file)
@@ -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<int> 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<nbCells;i++)
+    {
+      if((INTERP_KERNEL::NormalizedCellType)pt[ptI[i]]==type)
+        v.push_back(i);
+    }
+  MEDCouplingAutoRefCountObjectPtr<DataArrayInt> 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'.
  */
index d3019d2ad7274dcd5a5e56adf2e45e2f3acdc949..dc805356a4b433310cbbe929e622a7e01aeff807 100644 (file)
@@ -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<int>& conn) const;
     MEDCOUPLING_EXPORT DataArrayInt *getCellIdsFullyIncludedInNodeIds(const int *partBg, const int *partEnd) const;
index 2b2824c5af8de38de2a5c97de373fb847247f90f..7e52bc1bfc8fac8065bfb4cae4434c8de016473a 100644 (file)
@@ -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<MEDCouplingUMesh *>(m->buildPartOfMySelf(part0,part0+3));
+  MEDCouplingUMesh *m2=static_cast<MEDCouplingUMesh *>(m->buildPartOfMySelf(part1,part1+2));
+  std::vector<const MEDCouplingUMesh *> 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<DataArrayInt *> 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();
+}
index ad4de0c8a20b93469345e83db5f72ec8df0246cd..d7a2b8ca4243b19e3a4df100dc63746789094835 100644 (file)
@@ -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();
   };
 }
 
index 69b26c45282bb50f9a8f37c808c751ca18311a47..33403f8e900cf85f529e01f8b504ef75d6c4d5db 100644 (file)
@@ -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
index 156275851dc3036438cb26bd92ac1f5913c1b796..b0e4bd756507e111d9191dbe90f255baff7167c6 100644 (file)
@@ -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