Salome HOME
MEDReader again
[modules/med.git] / src / MEDLoader / MEDFileMeshElt.cxx
index 6321d76bc10ce7b344b9f7d87f7cd0da48b075b1..0725d6890394d0c7a560de9602549be4700e7e8a 100644 (file)
@@ -62,17 +62,17 @@ bool MEDFileUMeshPerType::isExisting(med_idt fid, const char *mName, int dt, int
 
 int MEDFileUMeshPerType::getDim() const
 {
-  const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(_type);
-  return cm.getDimension();
+  return _m->getMeshDimension();
 }
 
 MEDFileUMeshPerType::MEDFileUMeshPerType(med_idt fid, const char *mName, int dt, int it, int mdim, med_geometry_type geoElt, INTERP_KERNEL::NormalizedCellType type,
-                                         med_entity_type entity, MEDFileMeshReadSelector *mrs):_type(type),_entity(entity)
+                                         med_entity_type entity, MEDFileMeshReadSelector *mrs):_entity(entity)
 {
   med_bool changement,transformation;
   int curNbOfElem=MEDmeshnEntity(fid,mName,dt,it,entity,geoElt,MED_CONNECTIVITY,MED_NODAL,
                                  &changement,&transformation);
-  if(type!=INTERP_KERNEL::NORM_POLYGON && type!=INTERP_KERNEL::NORM_POLYHED)
+  const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(type);
+  if(!cm.isDynamic())
     {
       loadFromStaticType(fid,mName,dt,it,mdim,curNbOfElem,geoElt,type,entity,mrs);
       return;
@@ -89,24 +89,15 @@ MEDFileUMeshPerType::MEDFileUMeshPerType(med_idt fid, const char *mName, int dt,
 void MEDFileUMeshPerType::loadFromStaticType(med_idt fid, const char *mName, int dt, int it, int mdim, int curNbOfElem, med_geometry_type geoElt, INTERP_KERNEL::NormalizedCellType type,
                                              med_entity_type entity, MEDFileMeshReadSelector *mrs)
 {
-  _conn=DataArrayInt::New();
-  int nbOfNodesPerCell=(geoElt%100);
-  _conn->alloc((nbOfNodesPerCell+1)*curNbOfElem,1);
-  _conn_index=DataArrayInt::New();
-  _conn_index->alloc(curNbOfElem+1,1);
-  INTERP_KERNEL::AutoPtr<int> connTab=new int[(nbOfNodesPerCell)*curNbOfElem];
-  MEDmeshElementConnectivityRd(fid,mName,dt,it,entity,geoElt,MED_NODAL,MED_FULL_INTERLACE,connTab);
+  _m=MEDCoupling1SGTUMesh::New(mName,type);
+  MEDCoupling1SGTUMesh *mc(dynamic_cast<MEDCoupling1SGTUMesh *>((MEDCoupling1GTUMesh *)_m));
+  MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
+  int nbOfNodesPerCell=mc->getNumberOfNodesPerCell();
+  conn->alloc(nbOfNodesPerCell*curNbOfElem,1);
+  MEDmeshElementConnectivityRd(fid,mName,dt,it,entity,geoElt,MED_NODAL,MED_FULL_INTERLACE,conn->getPointer());
+  std::transform(conn->begin(),conn->end(),conn->getPointer(),std::bind2nd(std::plus<int>(),-1));
+  mc->setNodalConnectivity(conn);
   loadCommonPart(fid,mName,dt,it,mdim,curNbOfElem,geoElt,entity,mrs);
-  int *w1=_conn->getPointer();
-  int *w2=_conn_index->getPointer();
-  *w2++=0;
-  const int *wi=connTab;
-  for(int i=0;i<curNbOfElem;i++,wi+=nbOfNodesPerCell,w2++)
-    {
-      *w1++=(int)type;
-      w1=std::transform(wi,wi+nbOfNodesPerCell,w1,std::bind2nd(std::plus<int>(),-1));
-      *w2=w2[-1]+nbOfNodesPerCell+1;
-    }
 }
 
 void MEDFileUMeshPerType::loadCommonPart(med_idt fid, const char *mName, int dt, int it, int mdim, int curNbOfElem, med_geometry_type geoElt,
@@ -155,24 +146,14 @@ void MEDFileUMeshPerType::loadPolyg(med_idt fid, const char *mName, int dt, int
 {
   med_bool changement,transformation;
   med_int curNbOfElem=MEDmeshnEntity(fid,mName,dt,it,entity,MED_POLYGON,MED_INDEX_NODE,MED_NODAL,&changement,&transformation)-1;
-  _conn_index=DataArrayInt::New();
-  _conn_index->alloc(curNbOfElem+1,1);
-  _conn=DataArrayInt::New();
-  _conn->alloc(arraySize+curNbOfElem,1);
-  INTERP_KERNEL::AutoPtr<int> locConn=new int[arraySize];
-  MEDmeshPolygonRd(fid,mName,dt,it,MED_CELL,MED_NODAL,_conn_index->getPointer(),locConn);
-  int *w1=_conn->getPointer();
-  int *w2=_conn_index->getPointer();
-  const int *wi=locConn;
-  for(int i=0;i<curNbOfElem;i++,w2++)
-    {
-      *w1++=(int)INTERP_KERNEL::NORM_POLYGON;
-      const int *wi2=wi+(w2[1]-w2[0]);
-      w1=std::transform(wi,wi2,w1,std::bind2nd(std::plus<int>(),-1));
-      wi=wi2;
-      *w2=*w2-1+i;
-    }
-  *w2=*w2-1+curNbOfElem;
+  _m=MEDCoupling1DGTUMesh::New(mName,INTERP_KERNEL::NORM_POLYGON);
+  MEDCoupling1DGTUMesh *mc(dynamic_cast<MEDCoupling1DGTUMesh *>((MEDCoupling1GTUMesh *)_m));
+  MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New()),connI(DataArrayInt::New());
+  conn->alloc(arraySize,1); connI->alloc(curNbOfElem+1,1);
+  MEDmeshPolygonRd(fid,mName,dt,it,MED_CELL,MED_NODAL,connI->getPointer(),conn->getPointer());
+  std::transform(conn->begin(),conn->end(),conn->getPointer(),std::bind2nd(std::plus<int>(),-1));
+  std::transform(connI->begin(),connI->end(),connI->getPointer(),std::bind2nd(std::plus<int>(),-1));
+  mc->setNodalConnectivity(conn,connI);
   loadCommonPart(fid,mName,dt,it,mdim,curNbOfElem,MED_POLYGON,entity,mrs);
 }
 
@@ -182,24 +163,25 @@ void MEDFileUMeshPerType::loadPolyh(med_idt fid, const char *mName, int dt, int
   med_bool changement,transformation;
   med_int indexFaceLgth=MEDmeshnEntity(fid,mName,dt,it,MED_CELL,MED_POLYHEDRON,MED_INDEX_NODE,MED_NODAL,&changement,&transformation);
   int curNbOfElem=MEDmeshnEntity(fid,mName,dt,it,MED_CELL,MED_POLYHEDRON,MED_INDEX_FACE,MED_NODAL,&changement,&transformation)-1;
+  _m=MEDCoupling1DGTUMesh::New(mName,INTERP_KERNEL::NORM_POLYHED);
+  MEDCoupling1DGTUMesh *mc(dynamic_cast<MEDCoupling1DGTUMesh *>((MEDCoupling1GTUMesh *)_m));
   INTERP_KERNEL::AutoPtr<int> index=new int[curNbOfElem+1];
   INTERP_KERNEL::AutoPtr<int> indexFace=new int[indexFaceLgth];
   INTERP_KERNEL::AutoPtr<int> locConn=new int[connFaceLgth];
   MEDmeshPolyhedronRd(fid,mName,dt,it,MED_CELL,MED_NODAL,index,indexFace,locConn);
+  MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New()),connI(DataArrayInt::New());
   int arraySize=connFaceLgth;
   for(int i=0;i<curNbOfElem;i++)
     arraySize+=index[i+1]-index[i]-1;
-  _conn=DataArrayInt::New();
-  _conn->alloc(arraySize+curNbOfElem,1);
-  int *wFinalConn=_conn->getPointer();
-  _conn_index=DataArrayInt::New();
-  _conn_index->alloc(curNbOfElem+1,1);
-  int *finalIndex=_conn_index->getPointer();
+  conn=DataArrayInt::New();
+  conn->alloc(arraySize,1);
+  int *wFinalConn=conn->getPointer();
+  connI->alloc(curNbOfElem+1,1);
+  int *finalIndex(connI->getPointer());
   finalIndex[0]=0;
   for(int i=0;i<curNbOfElem;i++)
     {
-      *wFinalConn++=(int)INTERP_KERNEL::NORM_POLYHED;
-      finalIndex[i+1]=finalIndex[i]+index[i+1]-index[i]-1+indexFace[index[i+1]-1]-indexFace[index[i]-1]+1;
+      finalIndex[i+1]=finalIndex[i]+index[i+1]-index[i]-1+indexFace[index[i+1]-1]-indexFace[index[i]-1];
       wFinalConn=std::transform(locConn+indexFace[index[i]-1]-1,locConn+indexFace[index[i]]-1,wFinalConn,std::bind2nd(std::plus<int>(),-1));
       for(int j=index[i];j<index[i+1]-1;j++)
         {
@@ -207,10 +189,11 @@ void MEDFileUMeshPerType::loadPolyh(med_idt fid, const char *mName, int dt, int
           wFinalConn=std::transform(locConn+indexFace[j]-1,locConn+indexFace[j+1]-1,wFinalConn,std::bind2nd(std::plus<int>(),-1));
         }
     }
+  mc->setNodalConnectivity(conn,connI);
   loadCommonPart(fid,mName,dt,it,mdim,curNbOfElem,MED_POLYHEDRON,entity,mrs);
 }
 
-void MEDFileUMeshPerType::write(med_idt fid, const char *mname, int mdim, const MEDCouplingUMesh *m, const DataArrayInt *fam, const DataArrayInt *num, const DataArrayAsciiChar *names)
+void MEDFileUMeshPerType::Write(med_idt fid, const char *mname, int mdim, const MEDCoupling1GTUMesh *m, const DataArrayInt *fam, const DataArrayInt *num, const DataArrayAsciiChar *names)
 {
   int nbOfCells=m->getNumberOfCells();
   if(nbOfCells<1)
@@ -220,46 +203,42 @@ void MEDFileUMeshPerType::write(med_idt fid, const char *mname, int mdim, const
   INTERP_KERNEL::NormalizedCellType ikt=m->getTypeOfCell(0);
   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(ikt);
   med_geometry_type curMedType=typmai3[(int)ikt];
-  const int *conn=m->getNodalConnectivity()->getConstPointer();
-  const int *connI=m->getNodalConnectivityIndex()->getConstPointer();
-  if(ikt!=INTERP_KERNEL::NORM_POLYGON && ikt!=INTERP_KERNEL::NORM_POLYHED)
+  if(!cm.isDynamic())
     {
-      int nbNodesPerCell=cm.getNumberOfNodes();
-      INTERP_KERNEL::AutoPtr<int> tab=new int[nbNodesPerCell*nbOfCells];
-      int *w=tab;
-      for(int i=0;i<nbOfCells;i++)
-        w=std::transform(conn+connI[i]+1,conn+connI[i+1],w,std::bind2nd(std::plus<int>(),1));
-      MEDmeshElementConnectivityWr(fid,mname,dt,it,timm,MED_CELL,curMedType,MED_NODAL,MED_FULL_INTERLACE,nbOfCells,tab);
+      const MEDCoupling1SGTUMesh *m0(dynamic_cast<const MEDCoupling1SGTUMesh *>(m));
+      if(!m0)
+        throw INTERP_KERNEL::Exception("MEDFileUMeshPerType::Write : internal error #1 !");
+      MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr(m0->getNodalConnectivity()->deepCpy());
+      std::transform(arr->begin(),arr->end(),arr->getPointer(),std::bind2nd(std::plus<int>(),1));
+      MEDmeshElementConnectivityWr(fid,mname,dt,it,timm,MED_CELL,curMedType,MED_NODAL,MED_FULL_INTERLACE,nbOfCells,arr->begin());
     }
   else
     {
+      const MEDCoupling1DGTUMesh *m0(dynamic_cast<const MEDCoupling1DGTUMesh *>(m));
+      if(!m0)
+        throw INTERP_KERNEL::Exception("MEDFileUMeshPerType::Write : internal error #2 !");
       if(ikt==INTERP_KERNEL::NORM_POLYGON)
         {
-          INTERP_KERNEL::AutoPtr<int> tab1=new int[nbOfCells+1];
-          INTERP_KERNEL::AutoPtr<int> tab2=new int[m->getMeshLength()];
-          int *wI=tab1; *wI=1;
-          int *w=tab2;
-          for(int i=0;i<nbOfCells;i++,wI++)
-            {
-              wI[1]=wI[0]+connI[i+1]-connI[i]-1;
-              w=std::transform(conn+connI[i]+1,conn+connI[i+1],w,std::bind2nd(std::plus<int>(),1));
-            }
-          MEDmeshPolygonWr(fid,mname,dt,it,timm,MED_CELL,MED_NODAL,nbOfCells+1,tab1,tab2);
+          MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr(m0->getNodalConnectivity()->deepCpy()),arrI(m0->getNodalConnectivityIndex()->deepCpy());
+          std::transform(arr->begin(),arr->end(),arr->getPointer(),std::bind2nd(std::plus<int>(),1));
+          std::transform(arrI->begin(),arrI->end(),arrI->getPointer(),std::bind2nd(std::plus<int>(),1));
+          MEDmeshPolygonWr(fid,mname,dt,it,timm,MED_CELL,MED_NODAL,nbOfCells+1,arrI->begin(),arr->begin());
         }
       else
         {
-          int meshLgth=m->getMeshLength();
+          const int *conn(m0->getNodalConnectivity()->begin()),*connI(m0->getNodalConnectivityIndex()->begin());
+          int meshLgth=m0->getNodalConnectivityLength();
           int nbOfFaces=std::count(conn,conn+meshLgth,-1)+nbOfCells;
           INTERP_KERNEL::AutoPtr<int> tab1=new int[nbOfCells+1];
           int *w1=tab1; *w1=1;
           INTERP_KERNEL::AutoPtr<int> tab2=new int[nbOfFaces+1];
           int *w2=tab2; *w2=1;
-          INTERP_KERNEL::AutoPtr<int> bigtab=new int[meshLgth-nbOfCells];
+          INTERP_KERNEL::AutoPtr<int> bigtab=new int[meshLgth];
           int *bt=bigtab;
           for(int i=0;i<nbOfCells;i++,w1++)
             {
               int nbOfFaces2=0;
-              for(const int *w=conn+connI[i]+1;w!=conn+connI[i+1];w2++)
+              for(const int *w=conn+connI[i];w!=conn+connI[i+1];w2++)
                 {
                   const int *wend=std::find(w,conn+connI[i+1],-1);
                   bt=std::transform(w,wend,bt,std::bind2nd(std::plus<int>(),1));