]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
Small tools
authorAnthony Geay <anthony.geay@edf.fr>
Thu, 26 Jan 2017 10:30:01 +0000 (11:30 +0100)
committerAnthony Geay <anthony.geay@edf.fr>
Thu, 26 Jan 2017 10:30:01 +0000 (11:30 +0100)
src/MEDCoupling/MEDCouplingMemArray.hxx
src/MEDCoupling/MEDCouplingMemArray.txx
src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py
src/MEDCoupling_Swig/MEDCouplingMemArray.i

index 573b2da9008fcf0dd5ab62bc4571a578c207ff60..aea13c23df3e652f97ecae4291da51ec35758c9f 100644 (file)
@@ -215,7 +215,8 @@ namespace MEDCoupling
   class DataArrayTemplate : public DataArray
   {
   public:
-    static MCAuto< typename Traits<T>::ArrayTypeCh > NewFromStdVector(const typename std::vector<T>& v);
+    MEDCOUPLING_EXPORT static MCAuto< typename Traits<T>::ArrayTypeCh > NewFromStdVector(const typename std::vector<T>& v);
+    MEDCOUPLING_EXPORT std::vector< MCAuto< typename Traits<T>::ArrayTypeCh > > explodeComponents() const;
     //
     std::size_t getHeapMemorySizeWithoutChildren() const;
     //
index dcb942da11fe3cfc54ae5c9b7baf088c22e39085..cbf43aecd6f19f30e74b27f09c7039967bd30ce1 100644 (file)
@@ -490,6 +490,30 @@ namespace MEDCoupling
     return ret;
   }
   
+  template<class T>
+  std::vector< MCAuto< typename Traits<T>::ArrayTypeCh > > DataArrayTemplate<T>::explodeComponents() const
+  {
+    checkAllocated();
+    std::size_t sz(getNumberOfComponents());
+    int nbTuples(getNumberOfTuples());
+    std::string name(getName());
+    std::vector<std::string> compNames(getInfoOnComponents());
+    std::vector< MCAuto< typename Traits<T>::ArrayTypeCh > > ret(sz);
+    const T *thisPt(begin());
+    for(std::size_t i=0;i<sz;i++)
+      {
+        MCAuto< typename Traits<T>::ArrayTypeCh > part(Traits<T>::ArrayTypeCh::New());
+        part->alloc(nbTuples,1);
+        part->setName(name);
+        part->setInfoOnComponent(0,compNames[i]);
+        T *otherPt(part->getPointer());
+        for(int j=0;j<nbTuples;j++)
+          otherPt[j]=thisPt[sz*j+i];
+        ret[i]=part;
+      }
+    return ret;
+  }
+  
   template<class T>
   std::size_t DataArrayTemplate<T>::getHeapMemorySizeWithoutChildren() const
   {
index 5c523f0fdb351a98e32e773a03ce8cbc5242ae87..b6e54dd7241eb8f1371af857f1412b734eb800bc 100644 (file)
@@ -4543,6 +4543,30 @@ class MEDCouplingBasicsTest5(unittest.TestCase):
         d3Exp=DataArrayInt([3,2,1,6,5,4],2,3) ; d3Exp.setInfoOnComponents(["c","b","a"])
         self.assertTrue(d3Exp.isEqual(d2))
         pass
+
+    def testDAExplodeComponents1(self):
+        d=DataArrayDouble([(1,2),(3,4),(5,6)])
+        d.setName("toto")
+        d.setInfoOnComponents(["a","b"])
+        d2=d.explodeComponents()
+        self.assertEqual(len(d2),2)
+        #
+        d3=DataArrayDouble([1,3,5]) ; d3.setName("toto") ; d3.setInfoOnComponents(["a"])
+        self.assertTrue(d3.isEqual(d2[0],1e-14))
+        d4=DataArrayDouble([2,4,6]) ; d4.setName("toto") ; d4.setInfoOnComponents(["b"])
+        self.assertTrue(d4.isEqual(d2[1],1e-14))
+        #
+        d=DataArrayInt([(1,2),(3,4),(5,6)])
+        d.setName("toto")
+        d.setInfoOnComponents(["a","b"])
+        d2=d.explodeComponents()
+        self.assertEqual(len(d2),2)
+        #
+        d3=DataArrayInt([1,3,5]) ; d3.setName("toto") ; d3.setInfoOnComponents(["a"])
+        self.assertTrue(d3.isEqual(d2[0]))
+        d4=DataArrayInt([2,4,6]) ; d4.setName("toto") ; d4.setInfoOnComponents(["b"])
+        self.assertTrue(d4.isEqual(d2[1]))
+        pass
     
     pass
 
index dfb31875e981be48fd67413c64f051f01422e6d2..2396c3cb3527146424b4281332b3ea7a22c8a303 100644 (file)
@@ -1119,6 +1119,16 @@ namespace MEDCoupling
         convertPyToNewIntArr3(li,tmp);
         self->setSelectedComponents(a,tmp);
       }
+
+      PyObject *explodeComponents() const throw(INTERP_KERNEL::Exception)
+      {
+        std::vector< MCAuto<DataArrayDouble> > retCpp(self->explodeComponents());
+        std::size_t sz(retCpp.size());
+        PyObject *res(PyList_New(sz));
+        for(std::size_t i=0;i<sz;i++)
+          PyList_SetItem(res,i,SWIG_NewPointerObj(SWIG_as_voidptr(retCpp[i].retn()),SWIGTYPE_p_MEDCoupling__DataArrayDouble, SWIG_POINTER_OWN | 0 ));
+        return res;
+      }
    
       PyObject *getTuple(int tupleId) throw(INTERP_KERNEL::Exception)
       {
@@ -3244,6 +3254,16 @@ namespace MEDCoupling
         self->setSelectedComponents(a,tmp);
       }
 
+      PyObject *explodeComponents() const throw(INTERP_KERNEL::Exception)
+      {
+        std::vector< MCAuto<DataArrayInt> > retCpp(self->explodeComponents());
+        std::size_t sz(retCpp.size());
+        PyObject *res(PyList_New(sz));
+        for(std::size_t i=0;i<sz;i++)
+          PyList_SetItem(res,i,SWIG_NewPointerObj(SWIG_as_voidptr(retCpp[i].retn()),SWIGTYPE_p_MEDCoupling__DataArrayInt, SWIG_POINTER_OWN | 0 ));
+        return res;
+      }
+
       PyObject *getTuple(int tupleId) throw(INTERP_KERNEL::Exception)
       {
         int sz=self->getNumberOfComponents();