From 9f9fc8e40574437f7f4ad29bba61b59aff4c6bf4 Mon Sep 17 00:00:00 2001 From: geay Date: Tue, 29 Apr 2014 11:44:00 +0200 Subject: [PATCH] A useful static method for structured meshes. --- src/MEDCoupling/MEDCouplingStructuredMesh.cxx | 34 +++++++++++++++++-- src/MEDCoupling/MEDCouplingStructuredMesh.hxx | 1 + src/MEDCoupling_Swig/MEDCouplingBasicsTest.py | 3 ++ src/MEDCoupling_Swig/MEDCouplingCommon.i | 24 +++++++++++++ 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/src/MEDCoupling/MEDCouplingStructuredMesh.cxx b/src/MEDCoupling/MEDCouplingStructuredMesh.cxx index 5c5e4be8c..b5a8fb2ca 100644 --- a/src/MEDCoupling/MEDCouplingStructuredMesh.cxx +++ b/src/MEDCoupling/MEDCouplingStructuredMesh.cxx @@ -643,6 +643,34 @@ DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivityOfSubLevelMesh } } +/*! + * This method retrieves the number of entities (it can be cells or nodes) given a range in compact standard format + * used in methods like BuildExplicitIdsFrom,IsPartStructured. + * + * \sa BuildExplicitIdsFrom,IsPartStructured + */ +int MEDCouplingStructuredMesh::DeduceNumberOfGivenRangeInCompactFrmt(const std::vector< std::pair >& partCompactFormat) +{ + int ret(1); + bool isFetched(false); + std::size_t ii(0); + for(std::vector< std::pair >::const_iterator it=partCompactFormat.begin();it!=partCompactFormat.end();it++,ii++) + { + int a((*it).first),b((*it).second); + if(a<0 || b<0 || b-a<0) + { + std::ostringstream oss; oss << "MEDCouplingStructuredMesh::DeduceNumberOfGivenRangeInCompactFrmt : invalid input at dimension " << ii << " !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + if(b-a>0) + { + isFetched=true; + ret*=(b-a); + } + } + return isFetched?ret:0; +} + DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity1D(const int *nodeStBg) { int nbOfCells(*nodeStBg-1); @@ -866,7 +894,7 @@ std::vector MEDCouplingStructuredMesh::getCellGridStructure() const * This method states if given part ids [ \a startIds, \a stopIds) and a structure \a st returns if it can be considered as a structured dataset. * If true is returned \a partCompactFormat will contain the information to build the corresponding part. * - * \sa MEDCouplingStructuredMesh::BuildExplicitIdsFrom + * \sa MEDCouplingStructuredMesh::BuildExplicitIdsFrom, MEDCouplingStructuredMesh::DeduceNumberOfGivenRangeInCompactFrmt */ bool MEDCouplingStructuredMesh::IsPartStructured(const int *startIds, const int *stopIds, const std::vector& st, std::vector< std::pair >& partCompactFormat) { @@ -955,10 +983,10 @@ bool MEDCouplingStructuredMesh::IsPartStructured(const int *startIds, const int /*! * This method builds the explicit entity array from the structure in \a st and the range in \a partCompactFormat. - *If the range contains invalid values regarding sructure an exception will be thrown. + * If the range contains invalid values regarding sructure an exception will be thrown. * * \return DataArrayInt * - a new object. - * \sa MEDCouplingStructuredMesh::IsPartStructured + * \sa MEDCouplingStructuredMesh::IsPartStructured, MEDCouplingStructuredMesh::DeduceNumberOfGivenRangeInCompactFrmt */ DataArrayInt *MEDCouplingStructuredMesh::BuildExplicitIdsFrom(const std::vector& st, const std::vector< std::pair >& partCompactFormat) { diff --git a/src/MEDCoupling/MEDCouplingStructuredMesh.hxx b/src/MEDCoupling/MEDCouplingStructuredMesh.hxx index 0729d59e1..cd4295399 100644 --- a/src/MEDCoupling/MEDCouplingStructuredMesh.hxx +++ b/src/MEDCoupling/MEDCouplingStructuredMesh.hxx @@ -74,6 +74,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT static DataArrayInt *BuildExplicitIdsFrom(const std::vector& st, const std::vector< std::pair >& partCompactFormat); MEDCOUPLING_EXPORT static DataArrayInt *Build1GTNodalConnectivity(const int *nodeStBg, const int *nodeStEnd); MEDCOUPLING_EXPORT static DataArrayInt *Build1GTNodalConnectivityOfSubLevelMesh(const int *nodeStBg, const int *nodeStEnd); + MEDCOUPLING_EXPORT static int DeduceNumberOfGivenRangeInCompactFrmt(const std::vector< std::pair >& partCompactFormat); private: static int GetNumberOfCellsOfSubLevelMesh(const std::vector& cgs, int mdim); static void GetReverseNodalConnectivity1(const std::vector& ngs, DataArrayInt *revNodal, DataArrayInt *revNodalIndx); diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index fbc3370fe..b68019b0c 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -13524,6 +13524,9 @@ class MEDCouplingBasicsTest(unittest.TestCase): d20=DataArrayInt([1,2,3,4,11,12,13,14,21,22,23,24]) a,b=MEDCouplingStructuredMesh.IsPartStructured(d20,st) self.assertTrue(a) ; self.assertEqual(b,[(1,5),(0,3)]) + self.assertEqual(12,MEDCouplingStructuredMesh.DeduceNumberOfGivenRangeInCompactFrmt(b)) + self.assertEqual(8,MEDCouplingStructuredMesh.DeduceNumberOfGivenRangeInCompactFrmt([(1,5),(1,3),(2,2)])) + self.assertEqual(0,MEDCouplingStructuredMesh.DeduceNumberOfGivenRangeInCompactFrmt([(5,5),(3,3),(2,2)])) d20=DataArrayInt([1,2,3,4,12,11,13,14,21,22,23,24]) a,b=MEDCouplingStructuredMesh.IsPartStructured(d20,st) self.assertTrue(not a) diff --git a/src/MEDCoupling_Swig/MEDCouplingCommon.i b/src/MEDCoupling_Swig/MEDCouplingCommon.i index 3efdfc62f..c01b16184 100644 --- a/src/MEDCoupling_Swig/MEDCouplingCommon.i +++ b/src/MEDCoupling_Swig/MEDCouplingCommon.i @@ -2810,6 +2810,30 @@ namespace ParaMEDMEM return MEDCouplingStructuredMesh::BuildExplicitIdsFrom(tmp5,inp); } + static int DeduceNumberOfGivenRangeInCompactFrmt(PyObject *part) throw(INTERP_KERNEL::Exception) + { + int tmpp1=-1,tmpp2=-1; + std::vector tmp=fillArrayWithPyListInt2(part,tmpp1,tmpp2); + std::vector< std::pair > inp; + if(tmpp2==2) + { + inp.resize(tmpp1); + for(int i=0;i