From 5a5e7a37e53d9f8d67f80b1bedeeac4254f8c460 Mon Sep 17 00:00:00 2001 From: ageay Date: Tue, 23 Aug 2011 10:18:24 +0000 Subject: [PATCH] *** empty log message *** --- src/MEDCoupling/MEDCouplingUMesh.cxx | 87 +++++++++++++++++++ src/MEDCoupling/MEDCouplingUMesh.hxx | 33 +++++++ src/MEDCoupling_Swig/MEDCoupling.i | 49 +++++++++++ src/MEDCoupling_Swig/MEDCouplingBasicsTest.py | 13 ++- 4 files changed, 181 insertions(+), 1 deletion(-) diff --git a/src/MEDCoupling/MEDCouplingUMesh.cxx b/src/MEDCoupling/MEDCouplingUMesh.cxx index 04e737fb1..2e65cf02c 100644 --- a/src/MEDCoupling/MEDCouplingUMesh.cxx +++ b/src/MEDCoupling/MEDCouplingUMesh.cxx @@ -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 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(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; +} + diff --git a/src/MEDCoupling/MEDCouplingUMesh.hxx b/src/MEDCoupling/MEDCouplingUMesh.hxx index f8b30bfbe..098d7f98e 100644 --- a/src/MEDCoupling/MEDCouplingUMesh.hxx +++ b/src/MEDCoupling/MEDCouplingUMesh.hxx @@ -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& getAllTypes() const { return _types; } MEDCOUPLING_EXPORT std::set getAllGeoTypes() const; MEDCOUPLING_EXPORT std::set 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 diff --git a/src/MEDCoupling_Swig/MEDCoupling.i b/src/MEDCoupling_Swig/MEDCoupling.i index 1c3aee835..dbf1836d9 100644 --- a/src/MEDCoupling_Swig/MEDCoupling.i +++ b/src/MEDCoupling_Swig/MEDCoupling.i @@ -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;inextt(); + 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; diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index cd37332c0..eb314eb2e 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -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() -- 2.39.2