Salome HOME
5c5e4be8c60ca1625489efd1a69aaa7efb7cfa68
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingStructuredMesh.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #include "MEDCouplingStructuredMesh.hxx"
22 #include "MEDCouplingFieldDouble.hxx"
23 #include "MEDCouplingMemArray.hxx"
24 #include "MEDCoupling1GTUMesh.hxx"
25 #include "MEDCouplingUMesh.hxx"
26
27 #include <numeric>
28
29 using namespace ParaMEDMEM;
30
31 MEDCouplingStructuredMesh::MEDCouplingStructuredMesh()
32 {
33 }
34
35 MEDCouplingStructuredMesh::MEDCouplingStructuredMesh(const MEDCouplingStructuredMesh& other, bool deepCopy):MEDCouplingMesh(other)
36 {
37 }
38
39 MEDCouplingStructuredMesh::~MEDCouplingStructuredMesh()
40 {
41 }
42
43 std::size_t MEDCouplingStructuredMesh::getHeapMemorySizeWithoutChildren() const
44 {
45   return MEDCouplingMesh::getHeapMemorySizeWithoutChildren();
46 }
47
48 void MEDCouplingStructuredMesh::copyTinyStringsFrom(const MEDCouplingMesh *other)
49 {
50   MEDCouplingMesh::copyTinyStringsFrom(other);
51 }
52
53 bool MEDCouplingStructuredMesh::isEqualIfNotWhy(const MEDCouplingMesh *other, double prec, std::string& reason) const
54 {
55   return MEDCouplingMesh::isEqualIfNotWhy(other,prec,reason);
56 }
57
58 INTERP_KERNEL::NormalizedCellType MEDCouplingStructuredMesh::getTypeOfCell(int cellId) const
59 {
60   return GetGeoTypeGivenMeshDimension(getMeshDimension());
61 }
62
63 INTERP_KERNEL::NormalizedCellType MEDCouplingStructuredMesh::GetGeoTypeGivenMeshDimension(int meshDim)
64 {
65   switch(meshDim)
66   {
67     case 3:
68       return INTERP_KERNEL::NORM_HEXA8;
69     case 2:
70       return INTERP_KERNEL::NORM_QUAD4;
71     case 1:
72       return INTERP_KERNEL::NORM_SEG2;
73     case 0:
74       return INTERP_KERNEL::NORM_POINT1;
75     default:
76       throw INTERP_KERNEL::Exception("Unexpected dimension for MEDCouplingStructuredMesh::GetGeoTypeGivenMeshDimension !");
77   }
78 }
79
80 std::set<INTERP_KERNEL::NormalizedCellType> MEDCouplingStructuredMesh::getAllGeoTypes() const
81 {
82   std::set<INTERP_KERNEL::NormalizedCellType> ret2;
83   ret2.insert(getTypeOfCell(0));
84   return ret2;
85 }
86
87 int MEDCouplingStructuredMesh::getNumberOfCellsWithType(INTERP_KERNEL::NormalizedCellType type) const
88 {
89   int ret=getNumberOfCells();
90   if(type==getTypeOfCell(0))
91     return ret;
92   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
93   std::ostringstream oss; oss << "MEDCouplingStructuredMesh::getNumberOfCellsWithType : no specified type ! Type available is " << cm.getRepr() << " !";
94   throw INTERP_KERNEL::Exception(oss.str().c_str());
95 }
96
97 DataArrayInt *MEDCouplingStructuredMesh::giveCellsWithType(INTERP_KERNEL::NormalizedCellType type) const
98 {
99   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
100   if(getTypeOfCell(0)==type)
101     {
102       ret->alloc(getNumberOfCells(),1);
103       ret->iota(0);
104     }
105   else
106     ret->alloc(0,1);
107   return ret.retn();
108 }
109
110 DataArrayInt *MEDCouplingStructuredMesh::computeNbOfNodesPerCell() const
111 {
112   int nbCells=getNumberOfCells();
113   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
114   ret->alloc(nbCells,1);
115   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
116   ret->fillWithValue((int)cm.getNumberOfNodes());
117   return ret.retn();
118 }
119
120 DataArrayInt *MEDCouplingStructuredMesh::computeNbOfFacesPerCell() const
121 {
122   int nbCells=getNumberOfCells();
123   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
124   ret->alloc(nbCells,1);
125   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
126   ret->fillWithValue((int)cm.getNumberOfSons());
127   return ret.retn();
128 }
129
130 /*!
131  * This method computes effective number of nodes per cell. That is to say nodes appearing several times in nodal connectivity of a cell,
132  * will be counted only once here whereas it will be counted several times in MEDCouplingMesh::computeNbOfNodesPerCell method.
133  * Here for structured mesh it returns exactly as MEDCouplingStructuredMesh::computeNbOfNodesPerCell does.
134  *
135  * \return DataArrayInt * - new object to be deallocated by the caller.
136  */
137 DataArrayInt *MEDCouplingStructuredMesh::computeEffectiveNbOfNodesPerCell() const
138 {
139   return computeNbOfNodesPerCell();
140 }
141
142 void MEDCouplingStructuredMesh::getNodeIdsOfCell(int cellId, std::vector<int>& conn) const
143 {
144   int meshDim=getMeshDimension();
145   int tmpCell[3],tmpNode[3];
146   getSplitCellValues(tmpCell);
147   getSplitNodeValues(tmpNode);
148   int tmp2[3];
149   GetPosFromId(cellId,meshDim,tmpCell,tmp2);
150   switch(meshDim)
151   {
152     case 1:
153       conn.push_back(tmp2[0]); conn.push_back(tmp2[0]+1);
154       break;
155     case 2:
156       conn.push_back(tmp2[1]*tmpNode[1]+tmp2[0]); conn.push_back(tmp2[1]*tmpNode[1]+tmp2[0]+1);
157       conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]+1); conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]);
158       break;
159     case 3:
160       conn.push_back(tmp2[1]*tmpNode[1]+tmp2[0]+tmp2[2]*tmpNode[2]); conn.push_back(tmp2[1]*tmpNode[1]+tmp2[0]+1+tmp2[2]*tmpNode[2]);
161       conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]+1+tmp2[2]*tmpNode[2]); conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]+tmp2[2]*tmpNode[2]);
162       conn.push_back(tmp2[1]*tmpNode[1]+tmp2[0]+(tmp2[2]+1)*tmpNode[2]); conn.push_back(tmp2[1]*tmpNode[1]+tmp2[0]+1+(tmp2[2]+1)*tmpNode[2]);
163       conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]+1+(tmp2[2]+1)*tmpNode[2]); conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]+(tmp2[2]+1)*tmpNode[2]);
164       break;
165     default:
166       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::getNodeIdsOfCell : big problem spacedim must be in 1,2 or 3 !");
167   };
168 }
169
170 /*!
171  * This method returns the mesh dimension of \a this. It can be different from space dimension in case of a not null dimension contains only one node.
172  */
173 int MEDCouplingStructuredMesh::getMeshDimension() const
174 {
175   std::vector<int> ngs(getNodeGridStructure());
176   int ret(0),pos(0);
177   for(std::vector<int>::const_iterator it=ngs.begin();it!=ngs.end();it++,pos++)
178     {
179       if(*it<=0)
180         {
181           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::getMeshDimension : At pos #" << pos << " number of nodes is " << *it << " ! Must be > 0 !";
182           throw INTERP_KERNEL::Exception(oss.str().c_str());
183         }
184       if(*it>1)
185         ret++;
186     }
187   return ret;
188 }
189
190 /*!
191  * This method returns the space dimension by only considering the node grid structure.
192  * For cartesian mesh the returned value is equal to those returned by getSpaceDimension.
193  * But for curvelinear is could be different !
194  */
195 int MEDCouplingStructuredMesh::getSpaceDimensionOnNodeStruct() const
196 {
197   std::vector<int> nodeStr(getNodeGridStructure());
198   int spd1(0),pos(0);
199   for(std::vector<int>::const_iterator it=nodeStr.begin();it!=nodeStr.end();it++,pos++)
200     {
201       int elt(*it);
202       if(elt<=0)
203         {
204           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::getSpaceDimensionOnNodeStruct : At pos #" << pos << " value of node grid structure is " << *it << " ! must be >=1 !";
205           throw INTERP_KERNEL::Exception(oss.str().c_str());
206         }
207       spd1++;
208     }
209   return spd1;
210 }
211
212 /*!
213  * This method returns the number of cells of unstructured sub level mesh, without building it.
214  */
215 int MEDCouplingStructuredMesh::getNumberOfCellsOfSubLevelMesh() const
216 {
217   std::vector<int> cgs(getCellGridStructure());
218   return GetNumberOfCellsOfSubLevelMesh(cgs,getMeshDimension());
219 }
220
221 /*!
222  * See MEDCouplingUMesh::getDistributionOfTypes for more information
223  */
224 std::vector<int> MEDCouplingStructuredMesh::getDistributionOfTypes() const
225 {
226   //only one type of cell
227   std::vector<int> ret(3);
228   ret[0]=getTypeOfCell(0);
229   ret[1]=getNumberOfCells();
230   ret[2]=-1; //ret[3*k+2]==-1 because it has no sense here
231   return ret;
232 }
233
234 /*!
235  * This method tries to minimize at most the number of deep copy.
236  * So if \a idsPerType is not empty it can be returned directly (without copy, but with ref count incremented) in return.
237  * 
238  * See MEDCouplingUMesh::checkTypeConsistencyAndContig for more information
239  */
240 DataArrayInt *MEDCouplingStructuredMesh::checkTypeConsistencyAndContig(const std::vector<int>& code, const std::vector<const DataArrayInt *>& idsPerType) const
241 {
242   int nbOfCells=getNumberOfCells();
243   if(code.size()!=3)
244     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : invalid input code should be exactly of size 3 !");
245   if(code[0]!=(int)getTypeOfCell(0))
246     {
247       std::ostringstream oss; oss << "MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : Mismatch of geometric type ! Asking for " << code[0] << " whereas the geometric type is \a this is " << getTypeOfCell(0) << " !";
248       throw INTERP_KERNEL::Exception(oss.str().c_str());
249     }
250   if(code[2]==-1)
251     {
252       if(code[1]==nbOfCells)
253         return 0;
254       else
255         {
256           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : mismatch between the number of cells in this (" << nbOfCells << ") and the number of non profile (" << code[1] << ") !";
257           throw INTERP_KERNEL::Exception(oss.str().c_str());
258         }
259     }
260   if(code[2]!=0)
261     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : single geo type mesh ! 0 or -1 is expected at pos #2 of input code !");
262   if(idsPerType.size()!=1)
263     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : input code points to DataArrayInt #0 whereas the size of idsPerType is not equal to 1 !");
264   const DataArrayInt *pfl=idsPerType[0];
265   if(!pfl)
266     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : the input code points to a NULL DataArrayInt at rank 0 !");
267   if(pfl->getNumberOfComponents()!=1)
268     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : input profile should have exactly one component !");
269   pfl->checkAllIdsInRange(0,nbOfCells);
270   pfl->incrRef();
271   return const_cast<DataArrayInt *>(pfl);
272 }
273
274 /*!
275  * This method is the opposite of MEDCouplingUMesh::checkTypeConsistencyAndContig method. Given a list of cells in \a profile it returns a list of sub-profiles sorted by geo type.
276  * The result is put in the array \a idsPerType. In the returned parameter \a code, foreach i \a code[3*i+2] refers (if different from -1) to a location into the \a idsPerType.
277  * This method has 1 input \a profile and 3 outputs \a code \a idsInPflPerType and \a idsPerType.
278  * 
279  * \param [out] code is a vector of size 3*n where n is the number of different geometric type in \a this \b reduced to the profile \a profile. \a code has exactly the same semantic than in MEDCouplingUMesh::checkTypeConsistencyAndContig method.
280  * \param [out] idsInPflPerType is a vector of size of different geometric type in the subpart defined by \a profile of \a this ( equal to \a code.size()/3). For each i,
281  *              \a idsInPflPerType[i] stores the tuple ids in \a profile that correspond to the geometric type code[3*i+0]
282  * \param [out] idsPerType is a vector of size of different sub profiles needed to be defined to represent the profile \a profile for a given geometric type.
283  *              This vector can be empty in case of all geometric type cells are fully covered in ascending in the given input \a profile.
284  * 
285  * \warning for performance reasons no deep copy will be performed, if \a profile can been used as this in output parameters \a idsInPflPerType and \a idsPerType.
286  *
287  * \throw if \a profile has not exactly one component. It throws too, if \a profile contains some values not in [0,getNumberOfCells()) or if \a this is not fully defined
288  *
289  *  \b Example1: <br>
290  *          - Before \a this has 3 cells \a profile contains [0,1,2]
291  *          - After \a code contains [NORM_...,nbCells,-1], \a idsInPflPerType [[0,1,2]] and \a idsPerType is empty <br>
292  * 
293  *  \b Example2: <br>
294  *          - Before \a this has 3 cells \a profile contains [1,2]
295  *          - After \a code contains [NORM_...,nbCells,0], \a idsInPflPerType [[0,1]] and \a idsPerType is [[1,2]] <br>
296
297  */
298 void MEDCouplingStructuredMesh::splitProfilePerType(const DataArrayInt *profile, std::vector<int>& code, std::vector<DataArrayInt *>& idsInPflPerType, std::vector<DataArrayInt *>& idsPerType) const
299 {
300   if(!profile || !profile->isAllocated())
301     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::splitProfilePerType : input profile is NULL or not allocated !");
302   if(profile->getNumberOfComponents()!=1)
303     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::splitProfilePerType : input profile should have exactly one component !");
304   int nbTuples=profile->getNumberOfTuples();
305   int nbOfCells=getNumberOfCells();
306   code.resize(3); idsInPflPerType.resize(1);
307   code[0]=(int)getTypeOfCell(0); code[1]=nbOfCells;
308   idsInPflPerType.resize(1);
309   if(profile->isIdentity() && nbTuples==nbOfCells)
310     {
311       code[2]=-1;
312       idsInPflPerType[0]=0;
313       idsPerType.clear();
314       return ;
315     }
316   code[1]=profile->getNumberOfTuples();
317   code[2]=0;
318   profile->checkAllIdsInRange(0,nbOfCells);
319   idsPerType.resize(1);
320   idsPerType[0]=profile->deepCpy();
321   idsInPflPerType[0]=DataArrayInt::Range(0,nbTuples,1);
322 }
323
324 /*!
325  * Creates a new unstructured mesh (MEDCoupling1SGTUMesh) from \a this structured one.
326  *  \return MEDCouplingUMesh * - a new instance of MEDCouplingUMesh. The caller is to
327  * delete this array using decrRef() as it is no more needed. 
328  *  \throw If \a this->getMeshDimension() is not among [1,2,3].
329  */
330 MEDCoupling1SGTUMesh *MEDCouplingStructuredMesh::build1SGTUnstructured() const
331 {
332   int meshDim(getMeshDimension()),spaceDim(getSpaceDimensionOnNodeStruct());
333   if((meshDim<0 || meshDim>3) || (spaceDim<0 || spaceDim>3))
334     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::build1SGTUnstructured : meshdim and spacedim must be in [1,2,3] !");
335   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> coords(getCoordinatesAndOwner());
336   int ns[3];
337   getNodeGridStructure(ns);
338   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(Build1GTNodalConnectivity(ns,ns+spaceDim));
339   MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> ret(MEDCoupling1SGTUMesh::New(getName(),GetGeoTypeGivenMeshDimension(meshDim)));
340   ret->setNodalConnectivity(conn); ret->setCoords(coords);
341   return ret.retn();
342 }
343
344 /*!
345  * This method returns the unstructured mesh (having single geometric type) of the sub level mesh of \a this.
346  * This method is equivalent to computing MEDCouplingUMesh::buildDescendingConnectivity on the unstructurized \a this mesh.
347  * 
348  * The caller is to delete the returned mesh using decrRef() as it is no more needed. 
349  */
350 MEDCoupling1SGTUMesh *MEDCouplingStructuredMesh::build1SGTSubLevelMesh() const
351 {
352   int meshDim(getMeshDimension());
353   if(meshDim<1 || meshDim>3)
354     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::build1SGTSubLevelMesh : meshdim must be in [2,3] !");
355   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> coords(getCoordinatesAndOwner());
356   int ns[3];
357   getNodeGridStructure(ns);
358   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(Build1GTNodalConnectivityOfSubLevelMesh(ns,ns+meshDim));
359   MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> ret(MEDCoupling1SGTUMesh::New(getName(),GetGeoTypeGivenMeshDimension(meshDim-1)));
360   ret->setNodalConnectivity(conn); ret->setCoords(coords);
361   return ret.retn();
362 }
363
364 /*!
365  * Creates a new unstructured mesh (MEDCouplingUMesh) from \a this structured one.
366  *  \return MEDCouplingUMesh * - a new instance of MEDCouplingUMesh. The caller is to
367  * delete this array using decrRef() as it is no more needed. 
368  *  \throw If \a this->getMeshDimension() is not among [1,2,3].
369  */
370 MEDCouplingUMesh *MEDCouplingStructuredMesh::buildUnstructured() const
371 {
372   MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> ret0(build1SGTUnstructured());
373   return ret0->buildUnstructured();
374 }
375
376 /*!
377  * Creates a new MEDCouplingUMesh containing a part of cells of \a this mesh.
378  * The cells to include to the
379  * result mesh are specified by an array of cell ids.
380  *  \param [in] start - an array of cell ids to include to the result mesh.
381  *  \param [in] end - specifies the end of the array \a start, so that
382  *              the last value of \a start is \a end[ -1 ].
383  *  \return MEDCouplingMesh * - a new instance of MEDCouplingUMesh. The caller is to
384  *         delete this mesh using decrRef() as it is no more needed. 
385  */
386 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPart(const int *start, const int *end) const
387 {
388   MEDCouplingUMesh *um=buildUnstructured();
389   MEDCouplingMesh *ret=um->buildPart(start,end);
390   um->decrRef();
391   return ret;
392 }
393
394 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPartAndReduceNodes(const int *start, const int *end, DataArrayInt*& arr) const
395 {
396   std::vector<int> cgs(getCellGridStructure());
397   std::vector< std::pair<int,int> > cellPartFormat,nodePartFormat;
398   if(IsPartStructured(start,end,cgs,cellPartFormat))
399     {
400       MEDCouplingAutoRefCountObjectPtr<MEDCouplingStructuredMesh> ret(buildStructuredSubPart(cellPartFormat));
401       nodePartFormat=cellPartFormat;
402       for(std::vector< std::pair<int,int> >::iterator it=nodePartFormat.begin();it!=nodePartFormat.end();it++)
403         (*it).second++;
404       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp1(BuildExplicitIdsFrom(getNodeGridStructure(),nodePartFormat));
405       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp2(DataArrayInt::New()); tmp2->alloc(getNumberOfNodes(),1);
406       tmp2->fillWithValue(-1);
407       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp3(DataArrayInt::New()); tmp3->alloc(tmp1->getNumberOfTuples(),1); tmp3->iota(0);
408       tmp2->setPartOfValues3(tmp3,tmp1->begin(),tmp1->end(),0,1,1);
409       arr=tmp2.retn();
410       return ret.retn();
411     }
412   else
413     {
414       MEDCouplingUMesh *um=buildUnstructured();
415       MEDCouplingMesh *ret=um->buildPartAndReduceNodes(start,end,arr);
416       um->decrRef();
417       return ret;
418     }
419 }
420
421 DataArrayInt *MEDCouplingStructuredMesh::simplexize(int policy)
422 {
423   throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::simplexize : not available for Cartesian mesh !");
424 }
425
426 /*!
427  * Returns a new MEDCouplingFieldDouble holding normal vectors to cells of \a this
428  * 2D mesh. The computed vectors have 3 components and are normalized.
429  *  \return MEDCouplingFieldDouble * - a new instance of MEDCouplingFieldDouble on
430  *          cells and one time. The caller is to delete this field using decrRef() as
431  *          it is no more needed.
432  *  \throw If \a this->getMeshDimension() != 2.
433  */
434 MEDCouplingFieldDouble *MEDCouplingStructuredMesh::buildOrthogonalField() const
435 {
436   if(getMeshDimension()!=2)
437     throw INTERP_KERNEL::Exception("Expected a MEDCouplingStructuredMesh with meshDim == 2 !");
438   MEDCouplingFieldDouble *ret=MEDCouplingFieldDouble::New(ON_CELLS,NO_TIME);
439   DataArrayDouble *array=DataArrayDouble::New();
440   int nbOfCells=getNumberOfCells();
441   array->alloc(nbOfCells,3);
442   double *vals=array->getPointer();
443   for(int i=0;i<nbOfCells;i++)
444     { vals[3*i]=0.; vals[3*i+1]=0.; vals[3*i+2]=1.; }
445   ret->setArray(array);
446   array->decrRef();
447   ret->setMesh(this);
448   return ret;
449 }
450
451 void MEDCouplingStructuredMesh::getReverseNodalConnectivity(DataArrayInt *revNodal, DataArrayInt *revNodalIndx) const
452 {
453   std::vector<int> ngs(getNodeGridStructure());
454   int dim(getSpaceDimension());
455   switch(dim)
456   {
457     case 1:
458       return GetReverseNodalConnectivity1(ngs,revNodal,revNodalIndx);
459     case 2:
460       return GetReverseNodalConnectivity2(ngs,revNodal,revNodalIndx);
461     case 3:
462       return GetReverseNodalConnectivity3(ngs,revNodal,revNodalIndx);
463     default:
464       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::getReverseNodalConnectivity : only dimensions 1, 2 and 3 are supported !");
465   }
466 }
467
468 void MEDCouplingStructuredMesh::GetReverseNodalConnectivity1(const std::vector<int>& ngs, DataArrayInt *revNodal, DataArrayInt *revNodalIndx)
469 {
470   int nbNodes(ngs[0]);
471   revNodalIndx->alloc(nbNodes+1,1);
472   if(nbNodes==0)
473     { revNodal->alloc(0,1); revNodalIndx->setIJ(0,0,0); return ; }
474   if(nbNodes==1)
475     { revNodal->alloc(1,1); revNodal->setIJ(0,0,0); revNodalIndx->setIJ(0,0,0); revNodalIndx->setIJ(1,0,1); return ; }
476   revNodal->alloc(2*(nbNodes-1),1);
477   int *rn(revNodal->getPointer()),*rni(revNodalIndx->getPointer());
478   *rni++=0; *rni=1; *rn++=0;
479   for(int i=1;i<nbNodes-1;i++,rni++)
480     {
481       rn[0]=i-1; rn[1]=i;
482       rni[1]=rni[0]+2;
483       rn+=2;
484     }
485   rn[0]=nbNodes-2; rni[1]=rni[0]+1;
486 }
487
488 void MEDCouplingStructuredMesh::GetReverseNodalConnectivity2(const std::vector<int>& ngs, DataArrayInt *revNodal, DataArrayInt *revNodalIndx)
489 {
490   int nbNodesX(ngs[0]),nbNodesY(ngs[1]);
491   int nbNodes(nbNodesX*nbNodesY);
492   if(nbNodesX==0 || nbNodesY==0)
493     { revNodal->alloc(0,1); revNodalIndx->setIJ(0,0,0); return ; }
494   if(nbNodesX==1 || nbNodesY==1)
495     { std::vector<int> ngs2(1); ngs2[0]=std::max(nbNodesX,nbNodesY); return GetReverseNodalConnectivity1(ngs2,revNodal,revNodalIndx); }
496   revNodalIndx->alloc(nbNodes+1,1);
497   int nbCellsX(nbNodesX-1),nbCellsY(nbNodesY-1);
498   revNodal->alloc(4*(nbNodesX-2)*(nbNodesY-2)+2*2*(nbNodesX-2)+2*2*(nbNodesY-2)+4,1);
499   int *rn(revNodal->getPointer()),*rni(revNodalIndx->getPointer());
500   *rni++=0; *rni=1; *rn++=0;
501   for(int i=1;i<nbNodesX-1;i++,rni++,rn+=2)
502     {
503       rn[0]=i-1; rn[1]=i;
504       rni[1]=rni[0]+2;
505     }
506   rni[1]=rni[0]+1; *rn++=nbCellsX-1;
507   rni++;
508   for(int j=1;j<nbNodesY-1;j++)
509     {
510       int off(nbCellsX*(j-1)),off2(nbCellsX*j);
511       rni[1]=rni[0]+2; rn[0]=off; rn[1]=off2;
512       rni++; rn+=2;
513       for(int i=1;i<nbNodesX-1;i++,rni++,rn+=4)
514         {
515           rn[0]=i-1+off; rn[1]=i+off; rn[2]=i-1+off2; rn[3]=i+off2;
516           rni[1]=rni[0]+4;
517         }
518       rni[1]=rni[0]+2; rn[0]=off+nbCellsX-1; rn[1]=off2+nbCellsX-1;
519       rni++; rn+=2;
520     }
521   int off3(nbCellsX*(nbCellsY-1));
522   rni[1]=rni[0]+1;
523   rni++; *rn++=off3;
524   for(int i=1;i<nbNodesX-1;i++,rni++,rn+=2)
525     {
526       rn[0]=i-1+off3; rn[1]=i+off3;
527       rni[1]=rni[0]+2;
528     }
529   rni[1]=rni[0]+1; rn[0]=nbCellsX*nbCellsY-1;
530 }
531
532 void MEDCouplingStructuredMesh::GetReverseNodalConnectivity3(const std::vector<int>& ngs, DataArrayInt *revNodal, DataArrayInt *revNodalIndx)
533 {
534   int nbNodesX(ngs[0]),nbNodesY(ngs[1]),nbNodesZ(ngs[2]);
535   int nbNodes(nbNodesX*nbNodesY*nbNodesZ);
536   if(nbNodesX==0 || nbNodesY==0 || nbNodesZ==0)
537     { revNodal->alloc(0,1); revNodalIndx->setIJ(0,0,0); return ; }
538   if(nbNodesX==1 || nbNodesY==1 || nbNodesZ==1)
539     {
540       std::vector<int> ngs2(2);
541       int pos(0);
542       bool pass(false);
543       for(int i=0;i<3;i++)
544         {
545           if(pass)
546             { ngs2[pos++]=ngs[i]; }
547           else
548             {
549               pass=ngs[i]==1;
550               if(!pass)
551                 { ngs2[pos++]=ngs[i]; }
552             }
553         }
554       return GetReverseNodalConnectivity2(ngs2,revNodal,revNodalIndx);
555     }
556   revNodalIndx->alloc(nbNodes+1,1);
557   int nbCellsX(nbNodesX-1),nbCellsY(nbNodesY-1),nbCellsZ(nbNodesZ-1);
558   revNodal->alloc(8*(nbNodesX-2)*(nbNodesY-2)*(nbNodesZ-2)+4*(2*(nbNodesX-2)*(nbNodesY-2)+2*(nbNodesX-2)*(nbNodesZ-2)+2*(nbNodesY-2)*(nbNodesZ-2))+2*4*(nbNodesX-2)+2*4*(nbNodesY-2)+2*4*(nbNodesZ-2)+8,1);
559   int *rn(revNodal->getPointer()),*rni(revNodalIndx->getPointer());
560   *rni=0;
561   for(int k=0;k<nbNodesZ;k++)
562     {
563       bool factZ(k!=0 && k!=nbNodesZ-1);
564       int offZ0((k-1)*nbCellsX*nbCellsY),offZ1(k*nbCellsX*nbCellsY);
565       for(int j=0;j<nbNodesY;j++)
566         {
567           bool factYZ(factZ && (j!=0 && j!=nbNodesY-1));
568           int off00((j-1)*nbCellsX+offZ0),off01(j*nbCellsX+offZ0),off10((j-1)*nbCellsX+offZ1),off11(j*nbCellsX+offZ1);
569           for(int i=0;i<nbNodesX;i++,rni++)
570             {
571               int fact(factYZ && (i!=0 && i!=nbNodesX-1));
572               if(fact)
573                 {//most of points fall in this part of code
574                   rn[0]=off00+i-1; rn[1]=off00+i; rn[2]=off01+i-1; rn[3]=off01+i;
575                   rn[4]=off10+i-1; rn[5]=off10+i; rn[6]=off11+i-1; rn[7]=off11+i;
576                   rni[1]=rni[0]+8;
577                   rn+=8;
578                 }
579               else
580                 {
581                   int *rnRef(rn);
582                   if(k>=1 && j>=1 && i>=1)
583                     *rn++=off00+i-1;
584                   if(k>=1 && j>=1 && i<nbCellsX)
585                     *rn++=off00+i;
586                   if(k>=1 && j<nbCellsY && i>=1)
587                     *rn++=off01+i-1;
588                   if(k>=1 && j<nbCellsY && i<nbCellsX)
589                     *rn++=off01+i;
590                   //
591                   if(k<nbCellsZ && j>=1 && i>=1)
592                     *rn++=off10+i-1;
593                   if(k<nbCellsZ && j>=1 && i<nbCellsX)
594                     *rn++=off10+i;
595                   if(k<nbCellsZ && j<nbCellsY && i>=1)
596                     *rn++=off11+i-1;
597                   if(k<nbCellsZ && j<nbCellsY && i<nbCellsX)
598                     *rn++=off11+i;
599                   rni[1]=rni[0]+(int)(std::distance(rnRef,rn));
600                 }
601             }
602         }
603     }
604 }
605
606 /*!
607  * \return DataArrayInt * - newly allocated instance of nodal connectivity compatible for MEDCoupling1SGTMesh instance
608  */
609 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity(const int *nodeStBg, const int *nodeStEnd)
610 {
611   int zippedNodeSt[3];
612   int dim(ZipNodeStructure(nodeStBg,nodeStEnd,zippedNodeSt));
613   switch(dim)
614   {
615     case 0:
616       {
617         MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
618         conn->alloc(1,1); conn->setIJ(0,0,0);
619         return conn.retn();
620       }
621     case 1:
622       return Build1GTNodalConnectivity1D(zippedNodeSt);
623     case 2:
624       return Build1GTNodalConnectivity2D(zippedNodeSt);
625     case 3:
626       return Build1GTNodalConnectivity3D(zippedNodeSt);
627     default:
628       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::Build1GTNodalConnectivity : only dimension in [0,1,2,3] supported !");
629   }
630 }
631
632 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivityOfSubLevelMesh(const int *nodeStBg, const int *nodeStEnd)
633 {
634   std::size_t dim(std::distance(nodeStBg,nodeStEnd));
635   switch(dim)
636   {
637     case 3:
638       return Build1GTNodalConnectivityOfSubLevelMesh3D(nodeStBg);
639     case 2:
640       return Build1GTNodalConnectivityOfSubLevelMesh2D(nodeStBg);
641     default:
642       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::Build1GTNodalConnectivityOfSubLevelMesh: only dimension in [2,3] supported !");
643   }
644 }
645
646 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity1D(const int *nodeStBg)
647 {
648   int nbOfCells(*nodeStBg-1);
649   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
650   conn->alloc(2*nbOfCells,1);
651   int *cp=conn->getPointer();
652   for(int i=0;i<nbOfCells;i++)
653     {
654       cp[2*i+0]=i;
655       cp[2*i+1]=i+1;
656     }
657   return conn.retn();
658 }
659
660 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity2D(const int *nodeStBg)
661 {
662   int n1=nodeStBg[0]-1;
663   int n2=nodeStBg[1]-1;
664   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
665   conn->alloc(4*n1*n2,1);
666   int *cp=conn->getPointer();
667   int pos=0;
668   for(int j=0;j<n2;j++)
669     for(int i=0;i<n1;i++,pos++)
670       {
671         cp[4*pos+0]=i+1+j*(n1+1);
672         cp[4*pos+1]=i+j*(n1+1);
673         cp[4*pos+2]=i+(j+1)*(n1+1);
674         cp[4*pos+3]=i+1+(j+1)*(n1+1);
675       }
676   return conn.retn();
677 }
678
679 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity3D(const int *nodeStBg)
680 {
681   int n1=nodeStBg[0]-1;
682   int n2=nodeStBg[1]-1;
683   int n3=nodeStBg[2]-1;
684   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
685   conn->alloc(8*n1*n2*n3,1);
686   int *cp=conn->getPointer();
687   int pos=0;
688   for(int k=0;k<n3;k++)
689     for(int j=0;j<n2;j++)
690       for(int i=0;i<n1;i++,pos++)
691         {
692           int tmp=(n1+1)*(n2+1);
693           cp[8*pos+0]=i+1+j*(n1+1)+k*tmp;
694           cp[8*pos+1]=i+j*(n1+1)+k*tmp;
695           cp[8*pos+2]=i+(j+1)*(n1+1)+k*tmp;
696           cp[8*pos+3]=i+1+(j+1)*(n1+1)+k*tmp;
697           cp[8*pos+4]=i+1+j*(n1+1)+(k+1)*tmp;
698           cp[8*pos+5]=i+j*(n1+1)+(k+1)*tmp;
699           cp[8*pos+6]=i+(j+1)*(n1+1)+(k+1)*tmp;
700           cp[8*pos+7]=i+1+(j+1)*(n1+1)+(k+1)*tmp;
701         }
702   return conn.retn();
703 }
704
705 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivityOfSubLevelMesh3D(const int *nodeStBg)
706 {
707   std::vector<int> ngs(3);
708   int n0(nodeStBg[0]-1),n1(nodeStBg[1]-1),n2(nodeStBg[2]-1); ngs[0]=n0; ngs[1]=n1; ngs[2]=n2;
709   int off0(nodeStBg[0]),off1(nodeStBg[0]*nodeStBg[1]);
710   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
711   conn->alloc(4*GetNumberOfCellsOfSubLevelMesh(ngs,3));
712   int *cp(conn->getPointer());
713   //X
714   for(int i=0;i<nodeStBg[0];i++)
715     for(int j=0;j<n1;j++)
716       for(int k=0;k<n2;k++,cp+=4)
717         { cp[0]=k*off1+j*off0+i; cp[1]=(k+1)*off1+j*off0+i; cp[2]=(k+1)*off1+(j+1)*off0+i; cp[3]=k*off1+(j+1)*off0+i; }
718   //Y
719   for(int j=0;j<nodeStBg[1];j++)
720     for(int i=0;i<n0;i++)
721       for(int k=0;k<n2;k++,cp+=4)
722         { cp[0]=k*off1+j*off0+i; cp[1]=(k+1)*off1+j*off0+i; cp[2]=(k+1)*off1+j*off0+(i+1); cp[3]=k*off1+j*off0+(i+1); }
723   //Z
724   for(int k=0;k<nodeStBg[2];k++)
725     for(int i=0;i<n0;i++)
726       for(int j=0;j<n1;j++,cp+=4)
727         { cp[0]=k*off1+j*off0+i; cp[1]=k*off1+j*off0+(i+1); cp[2]=k*off1+(j+1)*off0+(i+1); cp[3]=k*off1+(j+1)*off0+i; }
728   return conn.retn();
729 }
730
731 /*!
732  * This method computes given the nodal structure defined by [ \a nodeStBg , \a nodeStEnd ) the zipped form.
733  * std::distance( \a nodeStBg, \a nodeStEnd ) is equal to the space dimension. The returned value is equal to
734  * the meshDimension (or the zipped spaceDimension).
735  *
736  * \param [out] zipNodeSt - The zipped node strucutre
737  * \return int - the
738  */
739 int MEDCouplingStructuredMesh::ZipNodeStructure(const int *nodeStBg, const int *nodeStEnd, int zipNodeSt[3])
740 {
741   int spaceDim((int)std::distance(nodeStBg,nodeStEnd));
742   if(spaceDim>3 || spaceDim<1)
743     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::ZipNodeStructure : spaceDim must in [1,2,3] !");
744   zipNodeSt[0]=0; zipNodeSt[1]=0; zipNodeSt[2]=0;
745   int zippedI(0);
746   for(int i=0;i<spaceDim;i++)
747     {
748       int elt(nodeStBg[i]);
749       if(elt<1)
750         {
751           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::ZipNodeStructure : the input nodal structure at pos#" << i << "(" << nodeStBg[i] << ") is invalid !";
752           throw INTERP_KERNEL::Exception(oss.str().c_str());
753         }
754       if(elt>=2)
755         zipNodeSt[zippedI++]=elt;
756     }
757   return zippedI;
758 }
759
760 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivityOfSubLevelMesh2D(const int *nodeStBg)
761 {
762   std::vector<int> ngs(2);
763   int n0(nodeStBg[0]-1),n1(nodeStBg[1]-1); ngs[0]=n0; ngs[1]=n1;
764   int off0(nodeStBg[0]);
765   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
766   conn->alloc(2*GetNumberOfCellsOfSubLevelMesh(ngs,2));
767   int *cp(conn->getPointer());
768   //X
769   for(int i=0;i<nodeStBg[0];i++)
770     for(int j=0;j<n1;j++,cp+=2)
771       { cp[0]=j*off0+i; cp[1]=(j+1)*off0+i; }
772   //Y
773   for(int j=0;j<nodeStBg[1];j++)
774     for(int i=0;i<n0;i++,cp+=2)
775       { cp[0]=j*off0+i; cp[1]=j*off0+(i+1); }
776   return conn.retn();
777 }
778
779 /*!
780  * Returns a cell id by its (i,j,k) index. The cell is located between the i-th and
781  * ( i + 1 )-th nodes along X axis etc.
782  *  \param [in] i - a index of node coordinates array along X axis.
783  *  \param [in] j - a index of node coordinates array along Y axis.
784  *  \param [in] k - a index of node coordinates array along Z axis.
785  *  \return int - a cell id in \a this mesh.
786  */
787 int MEDCouplingStructuredMesh::getCellIdFromPos(int i, int j, int k) const
788 {
789   int tmp[3]={i,j,k};
790   int tmp2[3];
791   int meshDim(getMeshDimension());
792   getSplitCellValues(tmp2);
793   std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
794   return std::accumulate(tmp,tmp+meshDim,0);
795 }
796
797 /*!
798  * Returns a node id by its (i,j,k) index.
799  *  \param [in] i - a index of node coordinates array along X axis.
800  *  \param [in] j - a index of node coordinates array along Y axis.
801  *  \param [in] k - a index of node coordinates array along Z axis.
802  *  \return int - a node id in \a this mesh.
803  */
804 int MEDCouplingStructuredMesh::getNodeIdFromPos(int i, int j, int k) const
805 {
806   int tmp[3]={i,j,k};
807   int tmp2[3];
808   int spaceDim(getSpaceDimension());
809   getSplitNodeValues(tmp2);
810   std::transform(tmp,tmp+spaceDim,tmp2,tmp,std::multiplies<int>());
811   return std::accumulate(tmp,tmp+spaceDim,0);
812 }
813
814
815 int MEDCouplingStructuredMesh::getNumberOfCells() const
816 {
817   std::vector<int> ngs(getNodeGridStructure());
818   int ret(1);
819   bool isCatched(false);
820   std::size_t ii(0);
821   for(std::vector<int>::const_iterator it=ngs.begin();it!=ngs.end();it++,ii++)
822     {
823       int elt(*it);
824       if(elt<=0)
825         {
826           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::getNumberOfCells : at pos #" << ii << " the number of nodes in nodeStructure is " << *it << " ! Must be > 0 !";
827           throw INTERP_KERNEL::Exception(oss.str().c_str());
828         }
829       if(elt>1)
830         {
831           ret*=elt-1;
832           isCatched=true;
833         }
834     }
835   return isCatched?ret:0;
836 }
837
838 int MEDCouplingStructuredMesh::getNumberOfNodes() const
839 {
840   std::vector<int> ngs(getNodeGridStructure());
841   int ret(1);
842   for(std::vector<int>::const_iterator it=ngs.begin();it!=ngs.end();it++)
843     ret*=*it;
844   return ret;
845 }
846
847 void MEDCouplingStructuredMesh::GetPosFromId(int nodeId, int meshDim, const int *split, int *res)
848 {
849   int work=nodeId;
850   for(int i=meshDim-1;i>=0;i--)
851     {
852       int pos=work/split[i];
853       work=work%split[i];
854       res[i]=pos;
855     }
856 }
857
858 std::vector<int> MEDCouplingStructuredMesh::getCellGridStructure() const
859 {
860   std::vector<int> ret(getNodeGridStructure());
861   std::transform(ret.begin(),ret.end(),ret.begin(),std::bind2nd(std::plus<int>(),-1));
862   return ret;
863 }
864
865 /*!
866  * 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.
867  * If true is returned \a partCompactFormat will contain the information to build the corresponding part.
868  *
869  * \sa MEDCouplingStructuredMesh::BuildExplicitIdsFrom
870  */
871 bool MEDCouplingStructuredMesh::IsPartStructured(const int *startIds, const int *stopIds, const std::vector<int>& st, std::vector< std::pair<int,int> >& partCompactFormat)
872 {
873   int dim((int)st.size());
874   partCompactFormat.resize(dim);
875   if(dim<1 || dim>3)
876     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::isPartStructured : input structure must be of dimension in [1,2,3] !");
877   std::vector<int> tmp2(dim),tmp(dim),tmp3(dim),tmp4(dim); tmp2[0]=1;
878   for(int i=1;i<dim;i++)
879     tmp2[i]=tmp2[i-1]*st[i-1];
880   std::size_t sz(std::distance(startIds,stopIds));
881   if(sz==0)
882     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : empty input !");
883   GetPosFromId(*startIds,dim,&tmp2[0],&tmp[0]);
884   partCompactFormat.resize(dim);
885   for(int i=0;i<dim;i++)
886     partCompactFormat[i].first=tmp[i];
887   if(tmp[dim-1]<0 || tmp[dim-1]>=st[dim-1])
888     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : first id in input is not in valid range !");
889   if(sz==1)
890     {
891       for(int i=0;i<dim;i++)
892         partCompactFormat[i].second=tmp[i]+1;
893       return true;
894     }
895   GetPosFromId(startIds[sz-1],dim,&tmp2[0],&tmp3[0]);
896   int szExp(1);
897   for(int i=0;i<dim;i++)
898     {
899       if(tmp3[i]<0 || tmp3[i]>=st[i])
900         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : last id in input is not in valid range !");
901       partCompactFormat[i].second=tmp3[i]+1;
902       tmp4[i]=partCompactFormat[i].second-partCompactFormat[i].first;
903       if(tmp4[i]<=0)
904         return false;
905       szExp*=tmp4[i];
906     }
907   if(szExp!=(int)sz)
908     return false;
909   const int *w(startIds);
910   switch(dim)
911   {
912     case 3:
913       {
914         for(int i=0;i<tmp4[2];i++)
915           {
916             int a=tmp2[2]*(partCompactFormat[2].first+i);
917             for(int j=0;j<tmp4[1];j++)
918               {
919                 int b=tmp2[1]*(partCompactFormat[1].first+j);
920                 for(int k=0;k<tmp4[0];k++,w++)
921                   {
922                     if(partCompactFormat[0].first+k+b+a!=*w)
923                       return false;
924                   }
925               }
926           }
927         return true;
928       }
929     case 2:
930       {
931         for(int j=0;j<tmp4[1];j++)
932           {
933             int b=tmp2[1]*(partCompactFormat[1].first+j);
934             for(int k=0;k<tmp4[0];k++,w++)
935               {
936                 if(partCompactFormat[0].first+k+b!=*w)
937                   return false;
938               }
939           }
940         return true;
941       }
942     case 1:
943       {
944         for(int k=0;k<tmp4[0];k++,w++)
945           {
946             if(partCompactFormat[0].first+k!=*w)
947               return false;
948           }
949         return true;
950       }
951     default:
952       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : internal error !");
953   }
954 }
955
956 /*!
957  * This method builds the explicit entity array from the structure in \a st and the range in \a partCompactFormat.
958  *If the range contains invalid values regarding sructure an exception will be thrown.
959  *
960  * \return DataArrayInt * - a new object.
961  * \sa MEDCouplingStructuredMesh::IsPartStructured
962  */
963 DataArrayInt *MEDCouplingStructuredMesh::BuildExplicitIdsFrom(const std::vector<int>& st, const std::vector< std::pair<int,int> >& partCompactFormat)
964 {
965   if(st.size()!=partCompactFormat.size())
966     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : input arrays must have the same size !");
967   int nbOfItems(1);
968   std::vector<int> dims(st.size());
969   for(std::size_t i=0;i<st.size();i++)
970     {
971       if(partCompactFormat[i].first<0 || partCompactFormat[i].first>st[i])
972         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : invalid input range 1 !");
973       if(partCompactFormat[i].second<0 || partCompactFormat[i].second>st[i])
974         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : invalid input range 2 !");
975       if(partCompactFormat[i].second<=partCompactFormat[i].first)
976         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : invalid input range 3 !");
977       dims[i]=partCompactFormat[i].second-partCompactFormat[i].first;
978       nbOfItems*=dims[i];
979     }
980   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New());
981   ret->alloc(nbOfItems,1);
982   int *pt(ret->getPointer());
983   switch(st.size())
984   {
985     case 3:
986       {
987         for(int i=0;i<dims[2];i++)
988           {
989             int a=(partCompactFormat[2].first+i)*st[0]*st[1];
990             for(int j=0;j<dims[1];j++)
991               {
992                 int b=(partCompactFormat[1].first+j)*st[0];
993                 for(int k=0;k<dims[0];k++,pt++)
994                   *pt=partCompactFormat[0].first+k+b+a;
995               }
996           }
997         break;
998       }
999     case 2:
1000       {
1001         for(int j=0;j<dims[1];j++)
1002           {
1003             int b=(partCompactFormat[1].first+j)*st[0];
1004             for(int k=0;k<dims[0];k++,pt++)
1005               *pt=partCompactFormat[0].first+k+b;
1006           }
1007         break;
1008       }
1009     case 1:
1010       {
1011         for(int k=0;k<dims[0];k++,pt++)
1012           *pt=partCompactFormat[0].first+k;
1013         break;
1014       }
1015     default:
1016       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : Dimension supported are 1,2 or 3 !");
1017   }
1018   return ret.retn();
1019 }
1020
1021 int MEDCouplingStructuredMesh::GetNumberOfCellsOfSubLevelMesh(const std::vector<int>& cgs, int mdim)
1022 {
1023   int ret(0);
1024   for(int i=0;i<mdim;i++)
1025     {
1026       int locRet(1);
1027       for(int j=0;j<mdim;j++)
1028         if(j!=i)
1029           locRet*=cgs[j];
1030         else
1031           locRet*=cgs[j]+1;
1032       ret+=locRet;
1033     }
1034   return ret;
1035 }