]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
Addition of getLocationFromCellId and getLocationFromNodeId methods in MEDCouplingStr...
authorAnthony Geay <anthony.geay@edf.fr>
Fri, 13 Feb 2015 09:20:37 +0000 (10:20 +0100)
committerAnthony Geay <anthony.geay@edf.fr>
Fri, 13 Feb 2015 09:20:37 +0000 (10:20 +0100)
src/MEDCoupling/MEDCouplingStructuredMesh.cxx
src/MEDCoupling/MEDCouplingStructuredMesh.hxx
src/MEDCoupling_Swig/MEDCouplingBasicsTest.py
src/MEDCoupling_Swig/MEDCouplingCommon.i

index 1b7bd952bd70867716daa95f9dabaf00db98e5bd..e36e974a679aaf734f382a8deb94f94a9ab42092 100644 (file)
@@ -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<int> MEDCouplingStructuredMesh::getLocationFromCellId(int cellId) const
+{
+  int meshDim(getMeshDimension());
+  std::vector<int> ret(meshDim);
+  std::vector<int> struc(getCellGridStructure());
+  int nbCells(std::accumulate(struc.begin(),struc.end(),1,std::multiplies<int>()));
+  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<int> 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<int> MEDCouplingStructuredMesh::getLocationFromNodeId(int nodeId) const
+{
+  int spaceDim(getSpaceDimension());
+  std::vector<int> ret(spaceDim);
+  std::vector<int> struc(getNodeGridStructure());
+  int nbNodes(std::accumulate(struc.begin(),struc.end(),1,std::multiplies<int>()));
+  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<int> 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];
index c6b95726087942a5d079873fdacd22ffdb6665d6..192fee676c406aeec56aa37662065b83a523f848 100644 (file)
@@ -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<int> getLocationFromCellId(int cellId) const;
+    MEDCOUPLING_EXPORT std::vector<int> 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<int>& conn) const;
     MEDCOUPLING_EXPORT std::size_t getHeapMemorySizeWithoutChildren() const;
index 5b99aaebee6238b1cd871aa920d31ef318e56877..e1a995470aa5b6b801ab9e373343f9509a00422d 100644 (file)
@@ -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__':
index 3866bfe1b405c2d86062db448ebe905b323a58a4..da1a53dbc9e24cb4b0bef9b4f193a2edeca073c8 100644 (file)
@@ -2914,6 +2914,8 @@ namespace ParaMEDMEM
     virtual std::vector<int> getNodeGridStructure() const throw(INTERP_KERNEL::Exception);
     std::vector<int> getCellGridStructure() const throw(INTERP_KERNEL::Exception);
     MEDCoupling1SGTUMesh *build1SGTUnstructured() const throw(INTERP_KERNEL::Exception);
+    std::vector<int> getLocationFromCellId(int cellId) const throw(INTERP_KERNEL::Exception);
+    std::vector<int> 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<int>& st) throw(INTERP_KERNEL::Exception);