From f32d16d5f3a4810fe5477a3c9a145a151596cea1 Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Fri, 13 Feb 2015 10:20:37 +0100 Subject: [PATCH] Addition of getLocationFromCellId and getLocationFromNodeId methods in MEDCouplingStructuredMesh. --- src/MEDCoupling/MEDCouplingStructuredMesh.cxx | 50 +++++++++++- src/MEDCoupling/MEDCouplingStructuredMesh.hxx | 4 +- src/MEDCoupling_Swig/MEDCouplingBasicsTest.py | 78 +++++++++++++++++++ src/MEDCoupling_Swig/MEDCouplingCommon.i | 2 + 4 files changed, 131 insertions(+), 3 deletions(-) diff --git a/src/MEDCoupling/MEDCouplingStructuredMesh.cxx b/src/MEDCoupling/MEDCouplingStructuredMesh.cxx index 1b7bd952b..e36e974a6 100644 --- a/src/MEDCoupling/MEDCouplingStructuredMesh.cxx +++ b/src/MEDCoupling/MEDCouplingStructuredMesh.cxx @@ -1271,9 +1271,55 @@ int MEDCouplingStructuredMesh::getNumberOfNodes() const return ret; } -void MEDCouplingStructuredMesh::GetPosFromId(int nodeId, int meshDim, const int *split, int *res) +/*! + * This method returns for a cell which id is \a cellId the location (locX,locY,locZ) of this cell in \a this. + * + * \param [in] cellId + * \return - A vector of size this->getMeshDimension() + * \throw if \a cellId not in [ 0, this->getNumberOfCells() ) + */ +std::vector MEDCouplingStructuredMesh::getLocationFromCellId(int cellId) const +{ + int meshDim(getMeshDimension()); + std::vector ret(meshDim); + std::vector struc(getCellGridStructure()); + int nbCells(std::accumulate(struc.begin(),struc.end(),1,std::multiplies())); + if(cellId<0 || cellId>=nbCells) + { + std::ostringstream oss; oss << "MEDCouplingStructuredMesh::getLocationFromCellId : Input cell id (" << cellId << ") is invalid ! Should be in [0," << nbCells << ") !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + std::vector spt(GetSplitVectFromStruct(struc)); + GetPosFromId(cellId,meshDim,&spt[0],&ret[0]); + return ret; +} + +/*! + * This method returns for a node which id is \a nodeId the location (locX,locY,locZ) of this node in \a this. + * + * \param [in] nodeId + * \return - A vector of size this->getSpaceDimension() + * \throw if \a cellId not in [ 0, this->getNumberOfNodes() ) + */ +std::vector MEDCouplingStructuredMesh::getLocationFromNodeId(int nodeId) const +{ + int spaceDim(getSpaceDimension()); + std::vector ret(spaceDim); + std::vector struc(getNodeGridStructure()); + int nbNodes(std::accumulate(struc.begin(),struc.end(),1,std::multiplies())); + if(nodeId<0 || nodeId>=nbNodes) + { + std::ostringstream oss; oss << "MEDCouplingStructuredMesh::getLocationFromNodeId : Input node id (" << nodeId << ") is invalid ! Should be in [0," << nbNodes << ") !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + std::vector spt(GetSplitVectFromStruct(struc)); + GetPosFromId(nodeId,spaceDim,&spt[0],&ret[0]); + return ret; +} + +void MEDCouplingStructuredMesh::GetPosFromId(int eltId, int meshDim, const int *split, int *res) { - int work=nodeId; + int work(eltId); for(int i=meshDim-1;i>=0;i--) { int pos=work/split[i]; diff --git a/src/MEDCoupling/MEDCouplingStructuredMesh.hxx b/src/MEDCoupling/MEDCouplingStructuredMesh.hxx index c6b957260..192fee676 100644 --- a/src/MEDCoupling/MEDCouplingStructuredMesh.hxx +++ b/src/MEDCoupling/MEDCouplingStructuredMesh.hxx @@ -38,7 +38,9 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT DataArrayInt *computeNbOfNodesPerCell() const; MEDCOUPLING_EXPORT DataArrayInt *computeNbOfFacesPerCell() const; MEDCOUPLING_EXPORT DataArrayInt *computeEffectiveNbOfNodesPerCell() const; - MEDCOUPLING_EXPORT static void GetPosFromId(int nodeId, int meshDim, const int *split, int *res); + MEDCOUPLING_EXPORT std::vector getLocationFromCellId(int cellId) const; + MEDCOUPLING_EXPORT std::vector getLocationFromNodeId(int nodeId) const; + MEDCOUPLING_EXPORT static void GetPosFromId(int eltId, int meshDim, const int *split, int *res); MEDCOUPLING_EXPORT static INTERP_KERNEL::NormalizedCellType GetGeoTypeGivenMeshDimension(int meshDim); MEDCOUPLING_EXPORT void getNodeIdsOfCell(int cellId, std::vector& conn) const; MEDCOUPLING_EXPORT std::size_t getHeapMemorySizeWithoutChildren() const; diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index 5b99aaebe..e1a995470 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -16287,6 +16287,84 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertTrue(m.getNodalConnectivity().isEqual(DataArrayInt([NORM_TRI3,0,2,1,NORM_QUAD4,3,6,5,4,NORM_POLYGON,7,11,10,9,8,NORM_TRI6,12,14,13,17,16,15,NORM_QUAD8,18,21,20,19,25,24,23,22,NORM_QPOLYG,26,30,29,28,27,35,34,33,32,31]))) self.assertTrue(m.getNodalConnectivityIndex().isEqual(cI)) pass + + def testSwig2StructuredMeshCellLocation1(self): + # 3D + arrX=DataArrayDouble(5) ; arrX.iota() + arrY=DataArrayDouble(4) ; arrY.iota() + arrZ=DataArrayDouble(3) ; arrZ.iota() + m=MEDCouplingCMesh() ; m.setCoords(arrX,arrY,arrZ) + li=[] + liExp3D=[(0,0,0),(1,0,0),(2,0,0),(3,0,0),(0,1,0),(1,1,0),(2,1,0),(3,1,0),(0,2,0),(1,2,0),(2,2,0),(3,2,0),(0,0,1),(1,0,1),(2,0,1),(3,0,1),(0,1,1),(1,1,1),(2,1,1),(3,1,1),(0,2,1),(1,2,1),(2,2,1),(3,2,1)] + self.assertEqual(24,m.getNumberOfCells()) + for i in xrange(m.getNumberOfCells()): + li.append(m.getLocationFromCellId(i)) + pass + self.assertEqual(liExp3D,li) + self.assertRaises(InterpKernelException,m.getLocationFromCellId,24) + self.assertRaises(InterpKernelException,m.getLocationFromCellId,-1) + # 2D + arrX=DataArrayDouble(5) ; arrX.iota() + arrY=DataArrayDouble(4) ; arrY.iota() + m=MEDCouplingCMesh() ; m.setCoords(arrX,arrY) + li=[] + liExp2D=[(0,0),(1,0),(2,0),(3,0),(0,1),(1,1),(2,1),(3,1),(0,2),(1,2),(2,2),(3,2)] + self.assertEqual(12,m.getNumberOfCells()) + for i in xrange(m.getNumberOfCells()): + li.append(m.getLocationFromCellId(i)) + pass + self.assertEqual(liExp2D,li) + self.assertRaises(InterpKernelException,m.getLocationFromCellId,12) + self.assertRaises(InterpKernelException,m.getLocationFromCellId,-1) + # 1D + arrX=DataArrayDouble(5) ; arrX.iota() + m=MEDCouplingCMesh() ; m.setCoords(arrX) + self.assertEqual(4,m.getNumberOfCells()) + for i in xrange(m.getNumberOfCells()): + self.assertEqual((i,),m.getLocationFromCellId(i)) + pass + self.assertRaises(InterpKernelException,m.getLocationFromCellId,4) + self.assertRaises(InterpKernelException,m.getLocationFromCellId,-1) + pass + + def testSwig2StructuredMeshNodeLocation1(self): + # 3D + arrX=DataArrayDouble(5) ; arrX.iota() + arrY=DataArrayDouble(4) ; arrY.iota() + arrZ=DataArrayDouble(3) ; arrZ.iota() + m=MEDCouplingCMesh() ; m.setCoords(arrX,arrY,arrZ) + li=[] + liExp3D=[(0,0,0),(1,0,0),(2,0,0),(3,0,0),(4,0,0),(0,1,0),(1,1,0),(2,1,0),(3,1,0),(4,1,0),(0,2,0),(1,2,0),(2,2,0),(3,2,0),(4,2,0),(0,3,0),(1,3,0),(2,3,0),(3,3,0),(4,3,0),(0,0,1),(1,0,1),(2,0,1),(3,0,1),(4,0,1),(0,1,1),(1,1,1),(2,1,1),(3,1,1),(4,1,1),(0,2,1),(1,2,1),(2,2,1),(3,2,1),(4,2,1),(0,3,1),(1,3,1),(2,3,1),(3,3,1),(4,3,1),(0,0,2),(1,0,2),(2,0,2),(3,0,2),(4,0,2),(0,1,2),(1,1,2),(2,1,2),(3,1,2),(4,1,2),(0,2,2),(1,2,2),(2,2,2),(3,2,2),(4,2,2),(0,3,2),(1,3,2),(2,3,2),(3,3,2),(4,3,2)] + self.assertEqual(60,m.getNumberOfNodes()) + for i in xrange(m.getNumberOfNodes()): + li.append(m.getLocationFromNodeId(i)) + pass + self.assertEqual(liExp3D,li) + self.assertRaises(InterpKernelException,m.getLocationFromNodeId,60) + self.assertRaises(InterpKernelException,m.getLocationFromNodeId,-1) + # 2D + arrX=DataArrayDouble(5) ; arrX.iota() + arrY=DataArrayDouble(4) ; arrY.iota() + m=MEDCouplingCMesh() ; m.setCoords(arrX,arrY) + li=[] + liExp2D=[(0,0),(1,0),(2,0),(3,0),(4,0),(0,1),(1,1),(2,1),(3,1),(4,1),(0,2),(1,2),(2,2),(3,2),(4,2),(0,3),(1,3),(2,3),(3,3),(4,3)] + self.assertEqual(20,m.getNumberOfNodes()) + for i in xrange(m.getNumberOfNodes()): + li.append(m.getLocationFromNodeId(i)) + pass + self.assertEqual(liExp2D,li) + self.assertRaises(InterpKernelException,m.getLocationFromNodeId,20) + self.assertRaises(InterpKernelException,m.getLocationFromNodeId,-1) + # 1D + arrX=DataArrayDouble(5) ; arrX.iota() + m=MEDCouplingCMesh() ; m.setCoords(arrX) + self.assertEqual(5,m.getNumberOfNodes()) + for i in xrange(m.getNumberOfNodes()): + self.assertEqual((i,),m.getLocationFromNodeId(i)) + pass + self.assertRaises(InterpKernelException,m.getLocationFromCellId,5) + self.assertRaises(InterpKernelException,m.getLocationFromCellId,-1) + pass pass if __name__ == '__main__': diff --git a/src/MEDCoupling_Swig/MEDCouplingCommon.i b/src/MEDCoupling_Swig/MEDCouplingCommon.i index 3866bfe1b..da1a53dbc 100644 --- a/src/MEDCoupling_Swig/MEDCouplingCommon.i +++ b/src/MEDCoupling_Swig/MEDCouplingCommon.i @@ -2914,6 +2914,8 @@ namespace ParaMEDMEM virtual std::vector getNodeGridStructure() const throw(INTERP_KERNEL::Exception); std::vector getCellGridStructure() const throw(INTERP_KERNEL::Exception); MEDCoupling1SGTUMesh *build1SGTUnstructured() const throw(INTERP_KERNEL::Exception); + std::vector getLocationFromCellId(int cellId) const throw(INTERP_KERNEL::Exception); + std::vector getLocationFromNodeId(int cellId) const throw(INTERP_KERNEL::Exception); static INTERP_KERNEL::NormalizedCellType GetGeoTypeGivenMeshDimension(int meshDim) throw(INTERP_KERNEL::Exception); MEDCoupling1SGTUMesh *build1SGTSubLevelMesh() const throw(INTERP_KERNEL::Exception); static int DeduceNumberOfGivenStructure(const std::vector& st) throw(INTERP_KERNEL::Exception); -- 2.39.2