]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
*** empty log message ***
authorageay <ageay>
Tue, 23 Aug 2011 10:18:24 +0000 (10:18 +0000)
committerageay <ageay>
Tue, 23 Aug 2011 10:18:24 +0000 (10:18 +0000)
src/MEDCoupling/MEDCouplingUMesh.cxx
src/MEDCoupling/MEDCouplingUMesh.hxx
src/MEDCoupling_Swig/MEDCoupling.i
src/MEDCoupling_Swig/MEDCouplingBasicsTest.py

index 04e737fb155cb852d7e183138fd3cb5c5db2306b..2e65cf02c72d4dc50f15d5569b41a08780cbbe2d 100644 (file)
@@ -263,6 +263,14 @@ void MEDCouplingUMesh::finishInsertingCells()
   updateTime();
 }
 
+/*!
+ * Entry point for iteration over cells of this. Warning the returned cell iterator should be deallocated.
+ */
+MEDCouplingUMeshCellIterator *MEDCouplingUMesh::cellIterator()
+{
+  return new MEDCouplingUMeshCellIterator(this);
+}
+
 std::set<INTERP_KERNEL::NormalizedCellType> MEDCouplingUMesh::getAllGeoTypes() const
 {
   return _types;
@@ -4416,3 +4424,82 @@ void MEDCouplingUMesh::fillInCompact3DMode(int spaceDim, int nbOfNodesInCell, co
   else
     throw INTERP_KERNEL::Exception("MEDCouplingUMesh::fillInCompact3DMode : Invalid spaceDim specified : must be 2 or 3 !");
 }
+
+MEDCouplingUMeshCellIterator::MEDCouplingUMeshCellIterator(MEDCouplingUMesh *mesh):_mesh(mesh),_cell(new MEDCouplingUMeshCell(mesh)),
+                                                                                   _cell_id(-1),_nb_cell(0)
+{
+  if(mesh)
+    {
+      mesh->incrRef();
+      _nb_cell=mesh->getNumberOfCells();
+    }
+}
+
+MEDCouplingUMeshCellIterator::~MEDCouplingUMeshCellIterator()
+{
+  if(_mesh)
+    _mesh->decrRef();
+  delete _cell;
+}
+
+MEDCouplingUMeshCell *MEDCouplingUMeshCellIterator::nextt()
+{
+  _cell_id++;
+  if(_cell_id<_nb_cell)
+    {
+      _cell->next();
+      return _cell;
+    }
+  else
+    return 0;
+}
+
+MEDCouplingUMeshCell::MEDCouplingUMeshCell(MEDCouplingUMesh *mesh):_conn(0),_conn_indx(0),_conn_lgth(NOTICABLE_FIRST_VAL)
+{
+  if(mesh)
+    {
+      _conn=mesh->getNodalConnectivity()->getPointer();
+      _conn_indx=mesh->getNodalConnectivityIndex()->getPointer();
+    }
+}
+
+void MEDCouplingUMeshCell::next()
+{
+  if(_conn_lgth!=NOTICABLE_FIRST_VAL)
+    {
+      _conn+=_conn_lgth;
+      _conn_indx++;
+    }
+  _conn_lgth=_conn_indx[1]-_conn_indx[0];
+}
+
+std::string MEDCouplingUMeshCell::repr() const
+{
+  if(_conn_lgth!=NOTICABLE_FIRST_VAL)
+    {
+      std::ostringstream oss; oss << "Cell Type " << INTERP_KERNEL::CellModel::GetCellModel((INTERP_KERNEL::NormalizedCellType)_conn[0]).getRepr();
+      oss << " : ";
+      std::copy(_conn+1,_conn+_conn_lgth,std::ostream_iterator<int>(oss," "));
+      return oss.str();
+    }
+  else
+    return std::string("MEDCouplingUMeshCell::repr : Invalid pos");
+}
+
+INTERP_KERNEL::NormalizedCellType MEDCouplingUMeshCell::getType() const
+{
+  if(_conn_lgth!=NOTICABLE_FIRST_VAL)
+    return (INTERP_KERNEL::NormalizedCellType)_conn[0];
+  else
+    return INTERP_KERNEL::NORM_ERROR;
+}
+
+const int *MEDCouplingUMeshCell::getAllConn(int& lgth) const
+{
+  lgth=_conn_lgth;
+  if(_conn_lgth!=NOTICABLE_FIRST_VAL)
+    return _conn;
+  else
+    return 0;
+}
+
index f8b30bfbe6411880ed629f4890f02063dd545ba4..098d7f98e10f41d4d5366068a984e8e8caefc5c6 100644 (file)
@@ -28,6 +28,8 @@
 
 namespace ParaMEDMEM
 {
+  class MEDCouplingUMeshCellIterator;
+  
   class MEDCouplingUMesh : public MEDCouplingPointSet
   {
   public:
@@ -51,6 +53,7 @@ namespace ParaMEDMEM
     MEDCOUPLING_EXPORT void allocateCells(int nbOfCells);
     MEDCOUPLING_EXPORT void insertNextCell(INTERP_KERNEL::NormalizedCellType type, int size, const int *nodalConnOfCell) throw(INTERP_KERNEL::Exception);
     MEDCOUPLING_EXPORT void finishInsertingCells();
+    MEDCOUPLING_EXPORT MEDCouplingUMeshCellIterator *cellIterator();
     MEDCOUPLING_EXPORT const std::set<INTERP_KERNEL::NormalizedCellType>& getAllTypes() const { return _types; }
     MEDCOUPLING_EXPORT std::set<INTERP_KERNEL::NormalizedCellType> getAllGeoTypes() const;
     MEDCOUPLING_EXPORT std::set<INTERP_KERNEL::NormalizedCellType> getTypesOfPart(const int *begin, const int *end) const throw(INTERP_KERNEL::Exception);
@@ -206,6 +209,36 @@ namespace ParaMEDMEM
   public:
     static double EPS_FOR_POLYH_ORIENTATION;
   };
+
+  class MEDCouplingUMeshCell;
+
+  class MEDCouplingUMeshCellIterator
+  {
+  public:
+    MEDCouplingUMeshCellIterator(MEDCouplingUMesh *mesh);
+    ~MEDCouplingUMeshCellIterator();
+    MEDCouplingUMeshCell *nextt();
+  private:
+    MEDCouplingUMesh *_mesh;
+    MEDCouplingUMeshCell *_cell;
+    int _cell_id;
+    int _nb_cell;
+  };
+
+  class MEDCouplingUMeshCell
+  {
+  public:
+    MEDCouplingUMeshCell(MEDCouplingUMesh *mesh);
+    void next();
+    std::string repr() const;
+    INTERP_KERNEL::NormalizedCellType getType() const;
+    const int *getAllConn(int& lgth) const;
+  private:
+    int *_conn;
+    int *_conn_indx;
+    int _conn_lgth;
+    static const int NOTICABLE_FIRST_VAL=-7;
+  };
 }
 
 #endif
index 1c3aee83555a9732ce75e06d6d73901ef49988b3..dbf1836d943341d2a08e7320ae275978ba65883e 100644 (file)
@@ -235,6 +235,7 @@ using namespace INTERP_KERNEL;
 %newobject ParaMEDMEM::MEDCouplingUMesh::getNodalConnectivity;
 %newobject ParaMEDMEM::MEDCouplingUMesh::getNodalConnectivityIndex;
 %newobject ParaMEDMEM::MEDCouplingUMesh::clone;
+%newobject ParaMEDMEM::MEDCouplingUMesh::__iter__;
 %newobject ParaMEDMEM::MEDCouplingUMesh::zipConnectivityTraducer;
 %newobject ParaMEDMEM::MEDCouplingUMesh::buildDescendingConnectivity;
 %newobject ParaMEDMEM::MEDCouplingUMesh::buildExtrudedMesh;
@@ -885,6 +886,48 @@ namespace ParaMEDMEM
            }
          }
     };
+
+  class MEDCouplingUMeshCell
+  {
+  public:
+    INTERP_KERNEL::NormalizedCellType getType() const;
+    %extend
+      {
+        std::string __str__() const
+        {
+          return self->repr();
+        }
+
+        PyObject *getAllConn() const
+        {
+          int ret2;
+          const int *r=self->getAllConn(ret2);
+          PyObject *ret=PyTuple_New(ret2);
+          for(int i=0;i<ret2;i++)
+            PyTuple_SetItem(ret,i,PyInt_FromLong(r[i]));
+          return ret;
+        }
+      }
+  };
+
+  class MEDCouplingUMeshCellIterator
+  {
+  public:
+    %extend
+      {
+        PyObject *next()
+        {
+          MEDCouplingUMeshCell *ret=self->nextt();
+          if(ret)
+            return SWIG_NewPointerObj(SWIG_as_voidptr(ret),SWIGTYPE_p_ParaMEDMEM__MEDCouplingUMeshCell,0|0);
+          else
+            {
+              PyErr_SetString(PyExc_StopIteration,"No more data.");
+              return 0;
+            }
+        }
+      }
+  };
   
   class MEDCouplingUMesh : public ParaMEDMEM::MEDCouplingPointSet
   {
@@ -930,6 +973,12 @@ namespace ParaMEDMEM
       {
         return self->simpleRepr();
       }
+      
+      MEDCouplingUMeshCellIterator *__iter__()
+      {
+        return self->cellIterator();
+      }
+
       void insertNextCell(INTERP_KERNEL::NormalizedCellType type, int size, PyObject *li) throw(INTERP_KERNEL::Exception)
       {
         int sz;
index cd37332c03fdeffdd6e9c00254203b8ef24ca12c..eb314eb2eb9a7a17967117e7a6bce196e9601a03 100644 (file)
@@ -6395,7 +6395,18 @@ class MEDCouplingBasicsTest(unittest.TestCase):
             pass
         self.assertEqual([2, 3, 7, 5, 6, 7, 8, 9, 7, 11, 12, 7],da.getValues())
         pass
-    pass
+
+    def testSwigUMeshIterator1(self):
+        m=MEDCouplingDataForTest.build2DTargetMesh_1()
+        li1=[]
+        li2=[]
+        for cell in m:
+            li1+=cell.getAllConn()[1:]
+            li2+=[cell.getType()]
+            pass
+        self.assertEqual(li1,[0, 3, 4, 1, 1, 4, 2, 4, 5, 2, 6, 7, 4, 3, 7, 8, 5, 4])
+        self.assertEqual(li2,[4, 3, 3, 4, 4])
+        pass
 
     def testDAIAggregateMulti1(self):
         a=DataArrayInt.New()