Salome HOME
1e2186855f2412843fee22ca0679d26bb8e6fba9
[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 void MEDCouplingStructuredMesh::getSplitCellValues(int *res) const
213 {
214   std::vector<int> strct(getCellGridStructure());
215   std::vector<int> ret(MEDCouplingStructuredMesh::GetSplitVectFromStruct(strct));
216   std::copy(ret.begin(),ret.end(),res);
217 }
218
219 void MEDCouplingStructuredMesh::getSplitNodeValues(int *res) const
220 {
221   std::vector<int> strct(getNodeGridStructure());
222   std::vector<int> ret(MEDCouplingStructuredMesh::GetSplitVectFromStruct(strct));
223   std::copy(ret.begin(),ret.end(),res);
224 }
225
226 /*!
227  * This method returns the number of cells of unstructured sub level mesh, without building it.
228  */
229 int MEDCouplingStructuredMesh::getNumberOfCellsOfSubLevelMesh() const
230 {
231   std::vector<int> cgs(getCellGridStructure());
232   return GetNumberOfCellsOfSubLevelMesh(cgs,getMeshDimension());
233 }
234
235 /*!
236  * See MEDCouplingUMesh::getDistributionOfTypes for more information
237  */
238 std::vector<int> MEDCouplingStructuredMesh::getDistributionOfTypes() const
239 {
240   //only one type of cell
241   std::vector<int> ret(3);
242   ret[0]=getTypeOfCell(0);
243   ret[1]=getNumberOfCells();
244   ret[2]=-1; //ret[3*k+2]==-1 because it has no sense here
245   return ret;
246 }
247
248 /*!
249  * This method tries to minimize at most the number of deep copy.
250  * So if \a idsPerType is not empty it can be returned directly (without copy, but with ref count incremented) in return.
251  * 
252  * See MEDCouplingUMesh::checkTypeConsistencyAndContig for more information
253  */
254 DataArrayInt *MEDCouplingStructuredMesh::checkTypeConsistencyAndContig(const std::vector<int>& code, const std::vector<const DataArrayInt *>& idsPerType) const
255 {
256   int nbOfCells=getNumberOfCells();
257   if(code.size()!=3)
258     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : invalid input code should be exactly of size 3 !");
259   if(code[0]!=(int)getTypeOfCell(0))
260     {
261       std::ostringstream oss; oss << "MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : Mismatch of geometric type ! Asking for " << code[0] << " whereas the geometric type is \a this is " << getTypeOfCell(0) << " !";
262       throw INTERP_KERNEL::Exception(oss.str().c_str());
263     }
264   if(code[2]==-1)
265     {
266       if(code[1]==nbOfCells)
267         return 0;
268       else
269         {
270           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : mismatch between the number of cells in this (" << nbOfCells << ") and the number of non profile (" << code[1] << ") !";
271           throw INTERP_KERNEL::Exception(oss.str().c_str());
272         }
273     }
274   if(code[2]!=0)
275     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : single geo type mesh ! 0 or -1 is expected at pos #2 of input code !");
276   if(idsPerType.size()!=1)
277     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : input code points to DataArrayInt #0 whereas the size of idsPerType is not equal to 1 !");
278   const DataArrayInt *pfl=idsPerType[0];
279   if(!pfl)
280     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : the input code points to a NULL DataArrayInt at rank 0 !");
281   if(pfl->getNumberOfComponents()!=1)
282     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : input profile should have exactly one component !");
283   pfl->checkAllIdsInRange(0,nbOfCells);
284   pfl->incrRef();
285   return const_cast<DataArrayInt *>(pfl);
286 }
287
288 /*!
289  * 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.
290  * 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.
291  * This method has 1 input \a profile and 3 outputs \a code \a idsInPflPerType and \a idsPerType.
292  * 
293  * \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.
294  * \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,
295  *              \a idsInPflPerType[i] stores the tuple ids in \a profile that correspond to the geometric type code[3*i+0]
296  * \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.
297  *              This vector can be empty in case of all geometric type cells are fully covered in ascending in the given input \a profile.
298  * 
299  * \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.
300  *
301  * \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
302  *
303  *  \b Example1: <br>
304  *          - Before \a this has 3 cells \a profile contains [0,1,2]
305  *          - After \a code contains [NORM_...,nbCells,-1], \a idsInPflPerType [[0,1,2]] and \a idsPerType is empty <br>
306  * 
307  *  \b Example2: <br>
308  *          - Before \a this has 3 cells \a profile contains [1,2]
309  *          - After \a code contains [NORM_...,nbCells,0], \a idsInPflPerType [[0,1]] and \a idsPerType is [[1,2]] <br>
310
311  */
312 void MEDCouplingStructuredMesh::splitProfilePerType(const DataArrayInt *profile, std::vector<int>& code, std::vector<DataArrayInt *>& idsInPflPerType, std::vector<DataArrayInt *>& idsPerType) const
313 {
314   if(!profile || !profile->isAllocated())
315     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::splitProfilePerType : input profile is NULL or not allocated !");
316   if(profile->getNumberOfComponents()!=1)
317     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::splitProfilePerType : input profile should have exactly one component !");
318   int nbTuples=profile->getNumberOfTuples();
319   int nbOfCells=getNumberOfCells();
320   code.resize(3); idsInPflPerType.resize(1);
321   code[0]=(int)getTypeOfCell(0); code[1]=nbOfCells;
322   idsInPflPerType.resize(1);
323   if(profile->isIdentity() && nbTuples==nbOfCells)
324     {
325       code[2]=-1;
326       idsInPflPerType[0]=0;
327       idsPerType.clear();
328       return ;
329     }
330   code[1]=profile->getNumberOfTuples();
331   code[2]=0;
332   profile->checkAllIdsInRange(0,nbOfCells);
333   idsPerType.resize(1);
334   idsPerType[0]=profile->deepCpy();
335   idsInPflPerType[0]=DataArrayInt::Range(0,nbTuples,1);
336 }
337
338 /*!
339  * Creates a new unstructured mesh (MEDCoupling1SGTUMesh) from \a this structured one.
340  *  \return MEDCouplingUMesh * - a new instance of MEDCouplingUMesh. The caller is to
341  * delete this array using decrRef() as it is no more needed. 
342  *  \throw If \a this->getMeshDimension() is not among [1,2,3].
343  */
344 MEDCoupling1SGTUMesh *MEDCouplingStructuredMesh::build1SGTUnstructured() const
345 {
346   int meshDim(getMeshDimension()),spaceDim(getSpaceDimensionOnNodeStruct());
347   if((meshDim<0 || meshDim>3) || (spaceDim<0 || spaceDim>3))
348     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::build1SGTUnstructured : meshdim and spacedim must be in [1,2,3] !");
349   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> coords(getCoordinatesAndOwner());
350   int ns[3];
351   getNodeGridStructure(ns);
352   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(Build1GTNodalConnectivity(ns,ns+spaceDim));
353   MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> ret(MEDCoupling1SGTUMesh::New(getName(),GetGeoTypeGivenMeshDimension(meshDim)));
354   ret->setNodalConnectivity(conn); ret->setCoords(coords);
355   try
356     { ret->copyTinyInfoFrom(this); }
357   catch(INTERP_KERNEL::Exception&) { }
358   return ret.retn();
359 }
360
361 /*!
362  * This method returns the unstructured mesh (having single geometric type) of the sub level mesh of \a this.
363  * This method is equivalent to computing MEDCouplingUMesh::buildDescendingConnectivity on the unstructurized \a this mesh.
364  * 
365  * The caller is to delete the returned mesh using decrRef() as it is no more needed. 
366  */
367 MEDCoupling1SGTUMesh *MEDCouplingStructuredMesh::build1SGTSubLevelMesh() const
368 {
369   int meshDim(getMeshDimension());
370   if(meshDim<1 || meshDim>3)
371     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::build1SGTSubLevelMesh : meshdim must be in [2,3] !");
372   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> coords(getCoordinatesAndOwner());
373   int ns[3];
374   getNodeGridStructure(ns);
375   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(Build1GTNodalConnectivityOfSubLevelMesh(ns,ns+meshDim));
376   MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> ret(MEDCoupling1SGTUMesh::New(getName(),GetGeoTypeGivenMeshDimension(meshDim-1)));
377   ret->setNodalConnectivity(conn); ret->setCoords(coords);
378   return ret.retn();
379 }
380
381 /*!
382  * Creates a new unstructured mesh (MEDCouplingUMesh) from \a this structured one.
383  *  \return MEDCouplingUMesh * - a new instance of MEDCouplingUMesh. The caller is to
384  * delete this array using decrRef() as it is no more needed. 
385  *  \throw If \a this->getMeshDimension() is not among [1,2,3].
386  */
387 MEDCouplingUMesh *MEDCouplingStructuredMesh::buildUnstructured() const
388 {
389   MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> ret0(build1SGTUnstructured());
390   return ret0->buildUnstructured();
391 }
392
393 /*!
394  * Creates a new MEDCouplingUMesh containing a part of cells of \a this mesh.
395  * The cells to include to the
396  * result mesh are specified by an array of cell ids.
397  *  \param [in] start - an array of cell ids to include to the result mesh.
398  *  \param [in] end - specifies the end of the array \a start, so that
399  *              the last value of \a start is \a end[ -1 ].
400  *  \return MEDCouplingMesh * - a new instance of MEDCouplingUMesh. The caller is to
401  *         delete this mesh using decrRef() as it is no more needed. 
402  */
403 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPart(const int *start, const int *end) const
404 {
405   MEDCouplingUMesh *um=buildUnstructured();
406   MEDCouplingMesh *ret=um->buildPart(start,end);
407   um->decrRef();
408   return ret;
409 }
410
411 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPartAndReduceNodes(const int *start, const int *end, DataArrayInt*& arr) const
412 {
413   std::vector<int> cgs(getCellGridStructure());
414   std::vector< std::pair<int,int> > cellPartFormat,nodePartFormat;
415   if(IsPartStructured(start,end,cgs,cellPartFormat))
416     {
417       MEDCouplingAutoRefCountObjectPtr<MEDCouplingStructuredMesh> ret(buildStructuredSubPart(cellPartFormat));
418       nodePartFormat=cellPartFormat;
419       for(std::vector< std::pair<int,int> >::iterator it=nodePartFormat.begin();it!=nodePartFormat.end();it++)
420         (*it).second++;
421       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp1(BuildExplicitIdsFrom(getNodeGridStructure(),nodePartFormat));
422       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp2(DataArrayInt::New()); tmp2->alloc(getNumberOfNodes(),1);
423       tmp2->fillWithValue(-1);
424       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp3(DataArrayInt::New()); tmp3->alloc(tmp1->getNumberOfTuples(),1); tmp3->iota(0);
425       tmp2->setPartOfValues3(tmp3,tmp1->begin(),tmp1->end(),0,1,1);
426       arr=tmp2.retn();
427       return ret.retn();
428     }
429   else
430     {
431       MEDCouplingUMesh *um=buildUnstructured();
432       MEDCouplingMesh *ret=um->buildPartAndReduceNodes(start,end,arr);
433       um->decrRef();
434       return ret;
435     }
436 }
437
438 DataArrayInt *MEDCouplingStructuredMesh::simplexize(int policy)
439 {
440   throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::simplexize : not available for Cartesian mesh !");
441 }
442
443 /*!
444  * Returns a new MEDCouplingFieldDouble holding normal vectors to cells of \a this
445  * 2D mesh. The computed vectors have 3 components and are normalized.
446  *  \return MEDCouplingFieldDouble * - a new instance of MEDCouplingFieldDouble on
447  *          cells and one time. The caller is to delete this field using decrRef() as
448  *          it is no more needed.
449  *  \throw If \a this->getMeshDimension() != 2.
450  */
451 MEDCouplingFieldDouble *MEDCouplingStructuredMesh::buildOrthogonalField() const
452 {
453   if(getMeshDimension()!=2)
454     throw INTERP_KERNEL::Exception("Expected a MEDCouplingStructuredMesh with meshDim == 2 !");
455   MEDCouplingFieldDouble *ret=MEDCouplingFieldDouble::New(ON_CELLS,NO_TIME);
456   DataArrayDouble *array=DataArrayDouble::New();
457   int nbOfCells=getNumberOfCells();
458   array->alloc(nbOfCells,3);
459   double *vals=array->getPointer();
460   for(int i=0;i<nbOfCells;i++)
461     { vals[3*i]=0.; vals[3*i+1]=0.; vals[3*i+2]=1.; }
462   ret->setArray(array);
463   array->decrRef();
464   ret->setMesh(this);
465   return ret;
466 }
467
468 void MEDCouplingStructuredMesh::getReverseNodalConnectivity(DataArrayInt *revNodal, DataArrayInt *revNodalIndx) const
469 {
470   std::vector<int> ngs(getNodeGridStructure());
471   int dim(getSpaceDimension());
472   switch(dim)
473   {
474     case 1:
475       return GetReverseNodalConnectivity1(ngs,revNodal,revNodalIndx);
476     case 2:
477       return GetReverseNodalConnectivity2(ngs,revNodal,revNodalIndx);
478     case 3:
479       return GetReverseNodalConnectivity3(ngs,revNodal,revNodalIndx);
480     default:
481       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::getReverseNodalConnectivity : only dimensions 1, 2 and 3 are supported !");
482   }
483 }
484
485 void MEDCouplingStructuredMesh::GetReverseNodalConnectivity1(const std::vector<int>& ngs, DataArrayInt *revNodal, DataArrayInt *revNodalIndx)
486 {
487   int nbNodes(ngs[0]);
488   revNodalIndx->alloc(nbNodes+1,1);
489   if(nbNodes==0)
490     { revNodal->alloc(0,1); revNodalIndx->setIJ(0,0,0); return ; }
491   if(nbNodes==1)
492     { revNodal->alloc(1,1); revNodal->setIJ(0,0,0); revNodalIndx->setIJ(0,0,0); revNodalIndx->setIJ(1,0,1); return ; }
493   revNodal->alloc(2*(nbNodes-1),1);
494   int *rn(revNodal->getPointer()),*rni(revNodalIndx->getPointer());
495   *rni++=0; *rni=1; *rn++=0;
496   for(int i=1;i<nbNodes-1;i++,rni++)
497     {
498       rn[0]=i-1; rn[1]=i;
499       rni[1]=rni[0]+2;
500       rn+=2;
501     }
502   rn[0]=nbNodes-2; rni[1]=rni[0]+1;
503 }
504
505 void MEDCouplingStructuredMesh::GetReverseNodalConnectivity2(const std::vector<int>& ngs, DataArrayInt *revNodal, DataArrayInt *revNodalIndx)
506 {
507   int nbNodesX(ngs[0]),nbNodesY(ngs[1]);
508   int nbNodes(nbNodesX*nbNodesY);
509   if(nbNodesX==0 || nbNodesY==0)
510     { revNodal->alloc(0,1); revNodalIndx->setIJ(0,0,0); return ; }
511   if(nbNodesX==1 || nbNodesY==1)
512     { std::vector<int> ngs2(1); ngs2[0]=std::max(nbNodesX,nbNodesY); return GetReverseNodalConnectivity1(ngs2,revNodal,revNodalIndx); }
513   revNodalIndx->alloc(nbNodes+1,1);
514   int nbCellsX(nbNodesX-1),nbCellsY(nbNodesY-1);
515   revNodal->alloc(4*(nbNodesX-2)*(nbNodesY-2)+2*2*(nbNodesX-2)+2*2*(nbNodesY-2)+4,1);
516   int *rn(revNodal->getPointer()),*rni(revNodalIndx->getPointer());
517   *rni++=0; *rni=1; *rn++=0;
518   for(int i=1;i<nbNodesX-1;i++,rni++,rn+=2)
519     {
520       rn[0]=i-1; rn[1]=i;
521       rni[1]=rni[0]+2;
522     }
523   rni[1]=rni[0]+1; *rn++=nbCellsX-1;
524   rni++;
525   for(int j=1;j<nbNodesY-1;j++)
526     {
527       int off(nbCellsX*(j-1)),off2(nbCellsX*j);
528       rni[1]=rni[0]+2; rn[0]=off; rn[1]=off2;
529       rni++; rn+=2;
530       for(int i=1;i<nbNodesX-1;i++,rni++,rn+=4)
531         {
532           rn[0]=i-1+off; rn[1]=i+off; rn[2]=i-1+off2; rn[3]=i+off2;
533           rni[1]=rni[0]+4;
534         }
535       rni[1]=rni[0]+2; rn[0]=off+nbCellsX-1; rn[1]=off2+nbCellsX-1;
536       rni++; rn+=2;
537     }
538   int off3(nbCellsX*(nbCellsY-1));
539   rni[1]=rni[0]+1;
540   rni++; *rn++=off3;
541   for(int i=1;i<nbNodesX-1;i++,rni++,rn+=2)
542     {
543       rn[0]=i-1+off3; rn[1]=i+off3;
544       rni[1]=rni[0]+2;
545     }
546   rni[1]=rni[0]+1; rn[0]=nbCellsX*nbCellsY-1;
547 }
548
549 void MEDCouplingStructuredMesh::GetReverseNodalConnectivity3(const std::vector<int>& ngs, DataArrayInt *revNodal, DataArrayInt *revNodalIndx)
550 {
551   int nbNodesX(ngs[0]),nbNodesY(ngs[1]),nbNodesZ(ngs[2]);
552   int nbNodes(nbNodesX*nbNodesY*nbNodesZ);
553   if(nbNodesX==0 || nbNodesY==0 || nbNodesZ==0)
554     { revNodal->alloc(0,1); revNodalIndx->setIJ(0,0,0); return ; }
555   if(nbNodesX==1 || nbNodesY==1 || nbNodesZ==1)
556     {
557       std::vector<int> ngs2(2);
558       int pos(0);
559       bool pass(false);
560       for(int i=0;i<3;i++)
561         {
562           if(pass)
563             { ngs2[pos++]=ngs[i]; }
564           else
565             {
566               pass=ngs[i]==1;
567               if(!pass)
568                 { ngs2[pos++]=ngs[i]; }
569             }
570         }
571       return GetReverseNodalConnectivity2(ngs2,revNodal,revNodalIndx);
572     }
573   revNodalIndx->alloc(nbNodes+1,1);
574   int nbCellsX(nbNodesX-1),nbCellsY(nbNodesY-1),nbCellsZ(nbNodesZ-1);
575   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);
576   int *rn(revNodal->getPointer()),*rni(revNodalIndx->getPointer());
577   *rni=0;
578   for(int k=0;k<nbNodesZ;k++)
579     {
580       bool factZ(k!=0 && k!=nbNodesZ-1);
581       int offZ0((k-1)*nbCellsX*nbCellsY),offZ1(k*nbCellsX*nbCellsY);
582       for(int j=0;j<nbNodesY;j++)
583         {
584           bool factYZ(factZ && (j!=0 && j!=nbNodesY-1));
585           int off00((j-1)*nbCellsX+offZ0),off01(j*nbCellsX+offZ0),off10((j-1)*nbCellsX+offZ1),off11(j*nbCellsX+offZ1);
586           for(int i=0;i<nbNodesX;i++,rni++)
587             {
588               int fact(factYZ && (i!=0 && i!=nbNodesX-1));
589               if(fact)
590                 {//most of points fall in this part of code
591                   rn[0]=off00+i-1; rn[1]=off00+i; rn[2]=off01+i-1; rn[3]=off01+i;
592                   rn[4]=off10+i-1; rn[5]=off10+i; rn[6]=off11+i-1; rn[7]=off11+i;
593                   rni[1]=rni[0]+8;
594                   rn+=8;
595                 }
596               else
597                 {
598                   int *rnRef(rn);
599                   if(k>=1 && j>=1 && i>=1)
600                     *rn++=off00+i-1;
601                   if(k>=1 && j>=1 && i<nbCellsX)
602                     *rn++=off00+i;
603                   if(k>=1 && j<nbCellsY && i>=1)
604                     *rn++=off01+i-1;
605                   if(k>=1 && j<nbCellsY && i<nbCellsX)
606                     *rn++=off01+i;
607                   //
608                   if(k<nbCellsZ && j>=1 && i>=1)
609                     *rn++=off10+i-1;
610                   if(k<nbCellsZ && j>=1 && i<nbCellsX)
611                     *rn++=off10+i;
612                   if(k<nbCellsZ && j<nbCellsY && i>=1)
613                     *rn++=off11+i-1;
614                   if(k<nbCellsZ && j<nbCellsY && i<nbCellsX)
615                     *rn++=off11+i;
616                   rni[1]=rni[0]+(int)(std::distance(rnRef,rn));
617                 }
618             }
619         }
620     }
621 }
622
623 /*!
624  * \return DataArrayInt * - newly allocated instance of nodal connectivity compatible for MEDCoupling1SGTMesh instance
625  */
626 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity(const int *nodeStBg, const int *nodeStEnd)
627 {
628   int zippedNodeSt[3];
629   int dim(ZipNodeStructure(nodeStBg,nodeStEnd,zippedNodeSt));
630   switch(dim)
631   {
632     case 0:
633       {
634         MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
635         conn->alloc(1,1); conn->setIJ(0,0,0);
636         return conn.retn();
637       }
638     case 1:
639       return Build1GTNodalConnectivity1D(zippedNodeSt);
640     case 2:
641       return Build1GTNodalConnectivity2D(zippedNodeSt);
642     case 3:
643       return Build1GTNodalConnectivity3D(zippedNodeSt);
644     default:
645       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::Build1GTNodalConnectivity : only dimension in [0,1,2,3] supported !");
646   }
647 }
648
649 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivityOfSubLevelMesh(const int *nodeStBg, const int *nodeStEnd)
650 {
651   std::size_t dim(std::distance(nodeStBg,nodeStEnd));
652   switch(dim)
653   {
654     case 3:
655       return Build1GTNodalConnectivityOfSubLevelMesh3D(nodeStBg);
656     case 2:
657       return Build1GTNodalConnectivityOfSubLevelMesh2D(nodeStBg);
658     default:
659       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::Build1GTNodalConnectivityOfSubLevelMesh: only dimension in [2,3] supported !");
660   }
661 }
662
663 /*!
664  * This method returns the list of ids sorted ascendingly of entities that are in the corner in ghost zone.
665  * The ids are returned in a newly created DataArrayInt having a single component.
666  *
667  * \param [in] st - The structure \b without ghost cells.
668  * \param [in] ghostLev - The size of the ghost zone (>=0)
669  * \return DataArrayInt * - The DataArray containing all the ids the caller is to deallocate.
670  */
671 DataArrayInt *MEDCouplingStructuredMesh::ComputeCornersGhost(const std::vector<int>& st, int ghostLev)
672 {
673   if(ghostLev<0)
674     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::ComputeCornersGhost : ghost lev must be >= 0 !");
675   std::size_t dim(st.size());
676   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New());
677   switch(dim)
678   {
679     case 1:
680       {
681         ret->alloc(2*ghostLev,1);
682         int *ptr(ret->getPointer());
683         for(int i=0;i<ghostLev;i++,ptr++)
684           *ptr=i;
685         int offset(st[0]);
686         if(offset<0)
687           throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::ComputeCornersGhost : element in 1D structure must be >= 0 !");
688         for(int i=0;i<ghostLev;i++,ptr++)
689           *ptr=offset+ghostLev+i;
690         break;
691       }
692     case 2:
693       {
694         int offsetX(st[0]),offsetY(st[1]);
695         if(offsetX<0 || offsetY<0)
696           throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::ComputeCornersGhost : elements in 2D structure must be >= 0 !");
697         ret->alloc(4*ghostLev,1);
698         int *ptr(ret->getPointer());
699         for(int i=0;i<ghostLev;i++)
700           {
701             *ptr++=i*(2*ghostLev+offsetX+1);
702             *ptr++=offsetX+2*ghostLev-1+i*(2*ghostLev+offsetX-1);
703           }
704         for(int i=0;i<ghostLev;i++)
705           {
706             *ptr++=(2*ghostLev+offsetX)*(offsetY+ghostLev)+ghostLev-1+i*(2*ghostLev+offsetX-1);
707             *ptr++=(2*ghostLev+offsetX)*(offsetY+ghostLev)+offsetX+ghostLev+i*(2*ghostLev+offsetX+1);
708           }
709         break;
710       }
711     case 3:
712       {
713         int offsetX(st[0]),offsetY(st[1]),offsetZ(st[2]);
714         if(offsetX<0 || offsetY<0 || offsetZ<0)
715           throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::ComputeCornersGhost : elements in 3D structure must be >= 0 !");
716         ret->alloc(8*ghostLev,1);
717         int *ptr(ret->getPointer());
718         int zeOffsetZ((offsetX+2*ghostLev)*(offsetY+2*ghostLev));
719         for(int i=0;i<ghostLev;i++)
720           {
721             *ptr++=i*(2*ghostLev+offsetX+1)+i*zeOffsetZ;
722             *ptr++=offsetX+2*ghostLev-1+i*(2*ghostLev+offsetX-1)+i*zeOffsetZ;
723             *ptr++=(2*ghostLev+offsetX)*(offsetY+ghostLev)+ghostLev-1+(ghostLev-i-1)*(2*ghostLev+offsetX-1)+i*zeOffsetZ;
724             *ptr++=(2*ghostLev+offsetX)*(offsetY+ghostLev)+offsetX+ghostLev+(ghostLev-i-1)*(2*ghostLev+offsetX+1)+i*zeOffsetZ;
725           }
726         int j(0),zeOffsetZ2(zeOffsetZ*(offsetZ+ghostLev));
727         for(int i=ghostLev-1;i>=0;i--,j++)
728           {
729             *ptr++=i*(2*ghostLev+offsetX+1)+j*zeOffsetZ+zeOffsetZ2;
730             *ptr++=offsetX+2*ghostLev-1+i*(2*ghostLev+offsetX-1)+j*zeOffsetZ+zeOffsetZ2;
731             *ptr++=(2*ghostLev+offsetX)*(offsetY+ghostLev)+ghostLev-1+(ghostLev-i-1)*(2*ghostLev+offsetX-1)+j*zeOffsetZ+zeOffsetZ2;
732             *ptr++=(2*ghostLev+offsetX)*(offsetY+ghostLev)+offsetX+ghostLev+(ghostLev-i-1)*(2*ghostLev+offsetX+1)+j*zeOffsetZ+zeOffsetZ2;
733           }
734         break;
735       }
736     default:
737       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::ComputeCornersGhost : Only dimensions 1, 2 and 3 are supported actually !");
738   }
739   return ret.retn();
740 }
741
742 /*!
743  * This method retrieves the number of entities (it can be cells or nodes) given a range in compact standard format
744  * used in methods like BuildExplicitIdsFrom,IsPartStructured.
745  *
746  * \sa BuildExplicitIdsFrom,IsPartStructured
747  */
748 int MEDCouplingStructuredMesh::DeduceNumberOfGivenRangeInCompactFrmt(const std::vector< std::pair<int,int> >& partCompactFormat)
749 {
750   int ret(1);
751   std::size_t ii(0);
752   for(std::vector< std::pair<int,int> >::const_iterator it=partCompactFormat.begin();it!=partCompactFormat.end();it++,ii++)
753     {
754       int a((*it).first),b((*it).second);
755       if(a<0 || b<0 || b-a<0)
756         {
757           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::DeduceNumberOfGivenRangeInCompactFrmt : invalid input at dimension " << ii << " !";
758           throw INTERP_KERNEL::Exception(oss.str().c_str());
759         }
760       ret*=(b-a);
761     }
762   return ret;
763 }
764
765 int MEDCouplingStructuredMesh::DeduceNumberOfGivenStructure(const std::vector<int>& st)
766 {
767   int ret(1);
768   bool isFetched(false);
769   for(std::size_t i=0;i<st.size();i++)
770     {
771       if(st[i]<0)
772         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::DeduceNumberOfGivenStructure : presence of a negative value in structure !");
773       ret*=st[i];
774       isFetched=true;
775     }
776   return isFetched?ret:0;
777 }
778
779 void MEDCouplingStructuredMesh::FindTheWidestAxisOfGivenRangeInCompactFrmt(const std::vector< std::pair<int,int> >& partCompactFormat, int& axisId, int& sizeOfRange)
780 {
781     int dim((int)partCompactFormat.size());
782     int ret(-1);
783     for(int i=0;i<dim;i++)
784       {
785         int curDelta(partCompactFormat[i].second-partCompactFormat[i].first);
786         if(curDelta<0)
787           {
788             std::ostringstream oss; oss << "MEDCouplingStructuredMesh::FindTheWidestAxisOfGivenRangeInCompactFrmt : at axis #" << i << " the range is invalid (first value < second value) !";
789             throw INTERP_KERNEL::Exception(oss.str().c_str());
790           }
791         if(curDelta>ret)
792           {
793             axisId=i; sizeOfRange=curDelta;
794             ret=curDelta;
795           }
796       }
797 }
798
799 /*!
800  * This method is \b NOT wrapped in python because it has no sense in python (for performance reasons).
801  * This method starts from a structured mesh with structure \a st on which a boolean field \a crit is set.
802  * This method find for such minimalist information of mesh and field which is the part of the mesh, given by the range per axis in output parameter
803  * \a partCompactFormat that contains all the True in \a crit. The returned vector of boolean is the field reduced to that part.
804  * So the number of True is equal in \a st and in returned vector of boolean.
805  *
806  * \param [in] minPatchLgth - minimum length that the patch may have for all directions.
807  * \param [in] st - The structure per axis of the structured mesh considered.
808  * \param [in] crit - The field of boolean (for performance reasons) lying on the mesh defined by \a st.
809  * \param [out] partCompactFormat - The minimal part of \a st containing all the true of \a crit.
810  * \param [out] reducedCrit - The reduction of \a criterion on \a partCompactFormat.
811  * \return - The number of True in \a st (that is equal to those in \a reducedCrit)
812  */
813 int MEDCouplingStructuredMesh::FindMinimalPartOf(int minPatchLgth, const std::vector<int>& st, const std::vector<bool>& crit, std::vector<bool>& reducedCrit, std::vector< std::pair<int,int> >& partCompactFormat)
814 {
815   if(minPatchLgth<0)
816     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::FindMinimalPartOf : the input minPatchLgth has to be >=0 !");
817   if((int)crit.size()!=DeduceNumberOfGivenStructure(st))
818     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::FindMinimalPartOf : size of vector of boolean is invalid regarding the declared structure !");
819   int ret(-1);
820   switch((int)st.size())
821   {
822     case 1:
823       {
824         ret=FindMinimalPartOf1D(st,crit,partCompactFormat);
825         break;
826       }
827     case 2:
828       {
829         ret=FindMinimalPartOf2D(st,crit,partCompactFormat);
830         break;
831       }
832     case 3:
833       {
834         ret=FindMinimalPartOf3D(st,crit,partCompactFormat);
835         break;
836       }
837     default:
838       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::FindMinimalPartOf : only dimension 1, 2 and 3 are supported actually !");
839   }
840   std::vector<int> dims(MEDCouplingStructuredMesh::GetDimensionsFromCompactFrmt(partCompactFormat));
841   int i(0);
842   for(std::vector< std::pair<int,int> >::iterator it=partCompactFormat.begin();it!=partCompactFormat.end();it++,i++)
843     {
844       if(st[i]<minPatchLgth)
845         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::FindMinimalPartOf : the input patch is tinier than the min length constraint !");
846       int start((*it).first),stop((*it).second),middle((start+stop)/2);
847       if(stop-start<minPatchLgth)
848         {
849           (*it).first=middle-minPatchLgth/2;
850           (*it).second=middle+minPatchLgth-minPatchLgth/2;
851           if((*it).first<0)
852             {
853               (*it).second+=-(*it).first;
854               (*it).first=0;
855             }
856           if((*it).second>st[i])
857             {
858               (*it).first-=(*it).second-st[i];
859               (*it).second=st[i];
860             }
861         }
862     }
863   ExtractFieldOfBoolFrom(st,crit,partCompactFormat,reducedCrit);
864   return ret;
865 }
866
867 /*!
868  * This method is \b NOT wrapped in python.
869  * This method considers \a crit input parameter as a matrix having dimensions specified by \a st. This method returns for each axis
870  * the signature, that is to say the number of elems equal to true in \a crit along this axis.
871  */
872 std::vector< std::vector<int> > MEDCouplingStructuredMesh::ComputeSignaturePerAxisOf(const std::vector<int>& st, const std::vector<bool>& crit)
873 {
874   int dim((int)st.size());
875   std::vector< std::vector<int> > ret(dim);
876   switch(dim)
877   {
878     case 1:
879       {
880         int nx(st[0]);
881         ret[0].resize(nx);
882         std::vector<int>& retX(ret[0]);
883         for(int i=0;i<nx;i++)
884           retX[i]=crit[i]?1:0;
885         break;
886       }
887     case 2:
888       {
889         int nx(st[0]),ny(st[1]);
890         ret[0].resize(nx); ret[1].resize(ny);
891         std::vector<int>& retX(ret[0]);
892         for(int i=0;i<nx;i++)
893           {
894             int cnt(0);
895             for(int j=0;j<ny;j++)
896               if(crit[j*nx+i])
897                 cnt++;
898             retX[i]=cnt;
899           }
900         std::vector<int>& retY(ret[1]);
901         for(int j=0;j<ny;j++)
902           {
903             int cnt(0);
904             for(int i=0;i<nx;i++)
905               if(crit[j*nx+i])
906                 cnt++;
907             retY[j]=cnt;
908           }
909         break;
910       }
911     case 3:
912       {
913         int nx(st[0]),ny(st[1]),nz(st[2]);
914         ret[0].resize(nx); ret[1].resize(ny); ret[2].resize(nz);
915         std::vector<int>& retX(ret[0]);
916         for(int i=0;i<nx;i++)
917           {
918             int cnt(0);
919             for(int k=0;k<nz;k++)
920               {
921                 int offz(k*nx*ny+i);
922                 for(int j=0;j<ny;j++)
923                   if(crit[offz+j*nx])
924                     cnt++;
925               }
926             retX[i]=cnt;
927           }
928         std::vector<int>& retY(ret[1]);
929         for(int j=0;j<ny;j++)
930           {
931             int cnt(0),offy(j*nx);
932             for(int k=0;k<nz;k++)
933               {
934                 int offz(k*nx*ny+offy);
935                 for(int i=0;i<nx;i++)
936                   if(crit[offz+i])
937                     cnt++;
938               }
939             retY[j]=cnt;
940           }
941         std::vector<int>& retZ(ret[2]);
942         for(int k=0;k<nz;k++)
943           {
944             int cnt(0),offz(k*nx*ny);
945             for(int j=0;j<ny;j++)
946               {
947                 int offy(offz+j*nx);
948                 for(int i=0;i<nx;i++)
949                   if(crit[offy+i])
950                     cnt++;
951               }
952             retZ[k]=cnt;
953           }
954         break;
955       }
956     default:
957        throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::ComputeSignatureOf : only dimensions 1, 2 and 3 are supported !");
958   }
959   return ret;
960 }
961
962 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity1D(const int *nodeStBg)
963 {
964   int nbOfCells(*nodeStBg-1);
965   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
966   conn->alloc(2*nbOfCells,1);
967   int *cp=conn->getPointer();
968   for(int i=0;i<nbOfCells;i++)
969     {
970       cp[2*i+0]=i;
971       cp[2*i+1]=i+1;
972     }
973   return conn.retn();
974 }
975
976 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity2D(const int *nodeStBg)
977 {
978   int n1=nodeStBg[0]-1;
979   int n2=nodeStBg[1]-1;
980   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
981   conn->alloc(4*n1*n2,1);
982   int *cp=conn->getPointer();
983   int pos=0;
984   for(int j=0;j<n2;j++)
985     for(int i=0;i<n1;i++,pos++)
986       {
987         cp[4*pos+0]=i+1+j*(n1+1);
988         cp[4*pos+1]=i+j*(n1+1);
989         cp[4*pos+2]=i+(j+1)*(n1+1);
990         cp[4*pos+3]=i+1+(j+1)*(n1+1);
991       }
992   return conn.retn();
993 }
994
995 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity3D(const int *nodeStBg)
996 {
997   int n1=nodeStBg[0]-1;
998   int n2=nodeStBg[1]-1;
999   int n3=nodeStBg[2]-1;
1000   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
1001   conn->alloc(8*n1*n2*n3,1);
1002   int *cp=conn->getPointer();
1003   int pos=0;
1004   for(int k=0;k<n3;k++)
1005     for(int j=0;j<n2;j++)
1006       for(int i=0;i<n1;i++,pos++)
1007         {
1008           int tmp=(n1+1)*(n2+1);
1009           cp[8*pos+0]=i+1+j*(n1+1)+k*tmp;
1010           cp[8*pos+1]=i+j*(n1+1)+k*tmp;
1011           cp[8*pos+2]=i+(j+1)*(n1+1)+k*tmp;
1012           cp[8*pos+3]=i+1+(j+1)*(n1+1)+k*tmp;
1013           cp[8*pos+4]=i+1+j*(n1+1)+(k+1)*tmp;
1014           cp[8*pos+5]=i+j*(n1+1)+(k+1)*tmp;
1015           cp[8*pos+6]=i+(j+1)*(n1+1)+(k+1)*tmp;
1016           cp[8*pos+7]=i+1+(j+1)*(n1+1)+(k+1)*tmp;
1017         }
1018   return conn.retn();
1019 }
1020
1021 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivityOfSubLevelMesh3D(const int *nodeStBg)
1022 {
1023   std::vector<int> ngs(3);
1024   int n0(nodeStBg[0]-1),n1(nodeStBg[1]-1),n2(nodeStBg[2]-1); ngs[0]=n0; ngs[1]=n1; ngs[2]=n2;
1025   int off0(nodeStBg[0]),off1(nodeStBg[0]*nodeStBg[1]);
1026   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
1027   conn->alloc(4*GetNumberOfCellsOfSubLevelMesh(ngs,3));
1028   int *cp(conn->getPointer());
1029   //X
1030   for(int i=0;i<nodeStBg[0];i++)
1031     for(int j=0;j<n1;j++)
1032       for(int k=0;k<n2;k++,cp+=4)
1033         { 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; }
1034   //Y
1035   for(int j=0;j<nodeStBg[1];j++)
1036     for(int i=0;i<n0;i++)
1037       for(int k=0;k<n2;k++,cp+=4)
1038         { 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); }
1039   //Z
1040   for(int k=0;k<nodeStBg[2];k++)
1041     for(int i=0;i<n0;i++)
1042       for(int j=0;j<n1;j++,cp+=4)
1043         { 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; }
1044   return conn.retn();
1045 }
1046
1047 /*!
1048  * \sa MEDCouplingStructuredMesh::FindMinimalPartOf
1049  */
1050 int MEDCouplingStructuredMesh::FindMinimalPartOf1D(const std::vector<int>& st, const std::vector<bool>& crit, std::vector< std::pair<int,int> >& partCompactFormat)
1051 {
1052   if(st.size()!=1)
1053     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::FindMinimalPartOf1D : the input size of st must be equal to 1 !");
1054   int nxMin(std::numeric_limits<int>::max()),nxMax(-std::numeric_limits<int>::max());
1055   int nx(st[0]),ret(0);
1056   for(int i=0;i<nx;i++)
1057     {
1058       if(crit[i])
1059         {
1060           nxMin=std::min(nxMin,i); nxMax=std::max(nxMax,i);
1061           ret++;
1062         }
1063     }
1064   if(ret==0)
1065     {
1066       std::size_t sz(st.size());
1067       partCompactFormat.resize(sz);
1068       for(std::size_t i=0;i<sz;i++)
1069         {
1070           partCompactFormat[i].first=st[i]/2;
1071           partCompactFormat[i].second=st[i]/2;
1072         }
1073       return ret;
1074     }
1075   partCompactFormat.resize(1);
1076   partCompactFormat[0].first=nxMin; partCompactFormat[0].second=nxMax+1;
1077   return ret;
1078 }
1079
1080 /*!
1081  * \sa MEDCouplingStructuredMesh::FindMinimalPartOf
1082  */
1083 int MEDCouplingStructuredMesh::FindMinimalPartOf2D(const std::vector<int>& st, const std::vector<bool>& crit, std::vector< std::pair<int,int> >& partCompactFormat)
1084 {
1085   if(st.size()!=2)
1086     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::FindMinimalPartOf2D : the input size of st must be equal to 2 !");
1087   int nxMin(std::numeric_limits<int>::max()),nxMax(-std::numeric_limits<int>::max()),nyMin(std::numeric_limits<int>::max()),nyMax(-std::numeric_limits<int>::max());
1088   int it(0),nx(st[0]),ny(st[1]);
1089   int ret(0);
1090   for(int i=0;i<ny;i++)
1091     for(int j=0;j<nx;j++,it++)
1092       {
1093         if(crit[it])
1094           {
1095             nxMin=std::min(nxMin,j); nxMax=std::max(nxMax,j);
1096             nyMin=std::min(nyMin,i); nyMax=std::max(nyMax,i);
1097             ret++;
1098           }
1099       }
1100   if(ret==0)
1101     {
1102       std::size_t sz(st.size());
1103       partCompactFormat.resize(sz);
1104       for(std::size_t i=0;i<sz;i++)
1105         {
1106           partCompactFormat[i].first=st[i]/2;
1107           partCompactFormat[i].second=st[i]/2;
1108         }
1109       return ret;
1110     }
1111   partCompactFormat.resize(2);
1112   partCompactFormat[0].first=nxMin; partCompactFormat[0].second=nxMax+1;
1113   partCompactFormat[1].first=nyMin; partCompactFormat[1].second=nyMax+1;
1114   return ret;
1115 }
1116
1117 /*!
1118  * \sa MEDCouplingStructuredMesh::FindMinimalPartOf
1119  */
1120 int MEDCouplingStructuredMesh::FindMinimalPartOf3D(const std::vector<int>& st, const std::vector<bool>& crit, std::vector< std::pair<int,int> >& partCompactFormat)
1121 {
1122   if(st.size()!=3)
1123     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::FindMinimalPartOf3D : the input size of st must be equal to 3 !");
1124   int nxMin(std::numeric_limits<int>::max()),nxMax(-std::numeric_limits<int>::max()),nyMin(std::numeric_limits<int>::max()),nyMax(-std::numeric_limits<int>::max()),nzMin(std::numeric_limits<int>::max()),nzMax(-std::numeric_limits<int>::max());
1125   int it(0),nx(st[0]),ny(st[1]),nz(st[2]);
1126   int ret(0);
1127   for(int i=0;i<nz;i++)
1128     for(int j=0;j<ny;j++)
1129       for(int k=0;k<nx;k++,it++)
1130         {
1131           if(crit[it])
1132             {
1133               nxMin=std::min(nxMin,k); nxMax=std::max(nxMax,k);
1134               nyMin=std::min(nyMin,j); nyMax=std::max(nyMax,j);
1135               nzMin=std::min(nzMin,i); nzMax=std::max(nzMax,i);
1136               ret++;
1137             }
1138         }
1139   if(ret==0)
1140     {
1141       std::size_t sz(st.size());
1142       partCompactFormat.resize(sz);
1143       for(std::size_t i=0;i<sz;i++)
1144         {
1145           partCompactFormat[i].first=st[i]/2;
1146           partCompactFormat[i].second=st[i]/2;
1147         }
1148       return ret;
1149     }
1150   partCompactFormat.resize(3);
1151   partCompactFormat[0].first=nxMin; partCompactFormat[0].second=nxMax+1;
1152   partCompactFormat[1].first=nyMin; partCompactFormat[1].second=nyMax+1;
1153   partCompactFormat[2].first=nzMin; partCompactFormat[2].second=nzMax+1;
1154   return ret;
1155 }
1156
1157 /*!
1158  * This method computes given the nodal structure defined by [ \a nodeStBg , \a nodeStEnd ) the zipped form.
1159  * std::distance( \a nodeStBg, \a nodeStEnd ) is equal to the space dimension. The returned value is equal to
1160  * the meshDimension (or the zipped spaceDimension).
1161  *
1162  * \param [out] zipNodeSt - The zipped node strucutre
1163  * \return int - the
1164  */
1165 int MEDCouplingStructuredMesh::ZipNodeStructure(const int *nodeStBg, const int *nodeStEnd, int zipNodeSt[3])
1166 {
1167   int spaceDim((int)std::distance(nodeStBg,nodeStEnd));
1168   if(spaceDim>3 || spaceDim<1)
1169     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::ZipNodeStructure : spaceDim must in [1,2,3] !");
1170   zipNodeSt[0]=0; zipNodeSt[1]=0; zipNodeSt[2]=0;
1171   int zippedI(0);
1172   for(int i=0;i<spaceDim;i++)
1173     {
1174       int elt(nodeStBg[i]);
1175       if(elt<1)
1176         {
1177           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::ZipNodeStructure : the input nodal structure at pos#" << i << "(" << nodeStBg[i] << ") is invalid !";
1178           throw INTERP_KERNEL::Exception(oss.str().c_str());
1179         }
1180       if(elt>=2)
1181         zipNodeSt[zippedI++]=elt;
1182     }
1183   return zippedI;
1184 }
1185
1186 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivityOfSubLevelMesh2D(const int *nodeStBg)
1187 {
1188   std::vector<int> ngs(2);
1189   int n0(nodeStBg[0]-1),n1(nodeStBg[1]-1); ngs[0]=n0; ngs[1]=n1;
1190   int off0(nodeStBg[0]);
1191   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
1192   conn->alloc(2*GetNumberOfCellsOfSubLevelMesh(ngs,2));
1193   int *cp(conn->getPointer());
1194   //X
1195   for(int i=0;i<nodeStBg[0];i++)
1196     for(int j=0;j<n1;j++,cp+=2)
1197       { cp[0]=j*off0+i; cp[1]=(j+1)*off0+i; }
1198   //Y
1199   for(int j=0;j<nodeStBg[1];j++)
1200     for(int i=0;i<n0;i++,cp+=2)
1201       { cp[0]=j*off0+i; cp[1]=j*off0+(i+1); }
1202   return conn.retn();
1203 }
1204
1205 /*!
1206  * Returns a cell id by its (i,j,k) index. The cell is located between the i-th and
1207  * ( i + 1 )-th nodes along X axis etc.
1208  *  \param [in] i - a index of node coordinates array along X axis.
1209  *  \param [in] j - a index of node coordinates array along Y axis.
1210  *  \param [in] k - a index of node coordinates array along Z axis.
1211  *  \return int - a cell id in \a this mesh.
1212  */
1213 int MEDCouplingStructuredMesh::getCellIdFromPos(int i, int j, int k) const
1214 {
1215   int tmp[3]={i,j,k};
1216   int tmp2[3];
1217   int meshDim(getMeshDimension());
1218   getSplitCellValues(tmp2);
1219   std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
1220   return std::accumulate(tmp,tmp+meshDim,0);
1221 }
1222
1223 /*!
1224  * Returns a node id by its (i,j,k) index.
1225  *  \param [in] i - a index of node coordinates array along X axis.
1226  *  \param [in] j - a index of node coordinates array along Y axis.
1227  *  \param [in] k - a index of node coordinates array along Z axis.
1228  *  \return int - a node id in \a this mesh.
1229  */
1230 int MEDCouplingStructuredMesh::getNodeIdFromPos(int i, int j, int k) const
1231 {
1232   int tmp[3]={i,j,k};
1233   int tmp2[3];
1234   int spaceDim(getSpaceDimension());
1235   getSplitNodeValues(tmp2);
1236   std::transform(tmp,tmp+spaceDim,tmp2,tmp,std::multiplies<int>());
1237   return std::accumulate(tmp,tmp+spaceDim,0);
1238 }
1239
1240
1241 int MEDCouplingStructuredMesh::getNumberOfCells() const
1242 {
1243   std::vector<int> ngs(getNodeGridStructure());
1244   int ret(1);
1245   bool isCatched(false);
1246   std::size_t ii(0);
1247   for(std::vector<int>::const_iterator it=ngs.begin();it!=ngs.end();it++,ii++)
1248     {
1249       int elt(*it);
1250       if(elt<=0)
1251         {
1252           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::getNumberOfCells : at pos #" << ii << " the number of nodes in nodeStructure is " << *it << " ! Must be > 0 !";
1253           throw INTERP_KERNEL::Exception(oss.str().c_str());
1254         }
1255       if(elt>1)
1256         {
1257           ret*=elt-1;
1258           isCatched=true;
1259         }
1260     }
1261   return isCatched?ret:0;
1262 }
1263
1264 int MEDCouplingStructuredMesh::getNumberOfNodes() const
1265 {
1266   std::vector<int> ngs(getNodeGridStructure());
1267   int ret(1);
1268   for(std::vector<int>::const_iterator it=ngs.begin();it!=ngs.end();it++)
1269     ret*=*it;
1270   return ret;
1271 }
1272
1273 void MEDCouplingStructuredMesh::GetPosFromId(int nodeId, int meshDim, const int *split, int *res)
1274 {
1275   int work=nodeId;
1276   for(int i=meshDim-1;i>=0;i--)
1277     {
1278       int pos=work/split[i];
1279       work=work%split[i];
1280       res[i]=pos;
1281     }
1282 }
1283
1284 std::vector<int> MEDCouplingStructuredMesh::getCellGridStructure() const
1285 {
1286   std::vector<int> ret(getNodeGridStructure());
1287   std::transform(ret.begin(),ret.end(),ret.begin(),std::bind2nd(std::plus<int>(),-1));
1288   return ret;
1289 }
1290
1291 /*!
1292  * This method returns the squareness of \a this (quadrature). \a this is expected to be with a mesh dimension equal to 2 or 3.
1293  */
1294 double MEDCouplingStructuredMesh::computeSquareness() const
1295 {
1296   std::vector<int> cgs(getCellGridStructure());
1297   if(cgs.empty())
1298     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::computeSquareness : empty mesh !");
1299   std::size_t dim(cgs.size());
1300   if(dim==1)
1301     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::computeSquareness : A segment cannot be square !");
1302   if(dim<4)
1303     {
1304       int minAx(cgs[0]),maxAx(cgs[0]);
1305       for(std::size_t i=1;i<dim;i++)
1306         {
1307           minAx=std::min(minAx,cgs[i]);
1308           maxAx=std::max(maxAx,cgs[i]);
1309         }
1310       return (double)minAx/(double)maxAx;
1311     }
1312   throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::computeSquareness : only dimension 2 and 3 supported !");
1313 }
1314
1315 /*!
1316  * Given a struct \a strct it returns a split vector [1,strct[0],strct[0]*strct[1]...]
1317  * This decomposition allows to quickly find i,j,k given a global id.
1318  */
1319 std::vector<int> MEDCouplingStructuredMesh::GetSplitVectFromStruct(const std::vector<int>& strct)
1320 {
1321   int spaceDim((int)strct.size());
1322   std::vector<int> res(spaceDim);
1323   for(int l=0;l<spaceDim;l++)
1324     {
1325       int val=1;
1326       for(int p=0;p<spaceDim-l-1;p++)
1327         val*=strct[p];
1328       res[spaceDim-l-1]=val;
1329     }
1330   return res;
1331 }
1332
1333 /*!
1334  * 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.
1335  * If true is returned \a partCompactFormat will contain the information to build the corresponding part.
1336  *
1337  * \sa MEDCouplingStructuredMesh::BuildExplicitIdsFrom, MEDCouplingStructuredMesh::DeduceNumberOfGivenRangeInCompactFrmt
1338  */
1339 bool MEDCouplingStructuredMesh::IsPartStructured(const int *startIds, const int *stopIds, const std::vector<int>& st, std::vector< std::pair<int,int> >& partCompactFormat)
1340 {
1341   int dim((int)st.size());
1342   partCompactFormat.resize(dim);
1343   if(dim<1 || dim>3)
1344     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::isPartStructured : input structure must be of dimension in [1,2,3] !");
1345   std::vector<int> tmp2(dim),tmp(dim),tmp3(dim),tmp4(dim); tmp2[0]=1;
1346   for(int i=1;i<dim;i++)
1347     tmp2[i]=tmp2[i-1]*st[i-1];
1348   std::size_t sz(std::distance(startIds,stopIds));
1349   if(sz==0)
1350     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : empty input !");
1351   GetPosFromId(*startIds,dim,&tmp2[0],&tmp[0]);
1352   partCompactFormat.resize(dim);
1353   for(int i=0;i<dim;i++)
1354     partCompactFormat[i].first=tmp[i];
1355   if(tmp[dim-1]<0 || tmp[dim-1]>=st[dim-1])
1356     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : first id in input is not in valid range !");
1357   if(sz==1)
1358     {
1359       for(int i=0;i<dim;i++)
1360         partCompactFormat[i].second=tmp[i]+1;
1361       return true;
1362     }
1363   GetPosFromId(startIds[sz-1],dim,&tmp2[0],&tmp3[0]);
1364   int szExp(1);
1365   for(int i=0;i<dim;i++)
1366     {
1367       if(tmp3[i]<0 || tmp3[i]>=st[i])
1368         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : last id in input is not in valid range !");
1369       partCompactFormat[i].second=tmp3[i]+1;
1370       tmp4[i]=partCompactFormat[i].second-partCompactFormat[i].first;
1371       if(tmp4[i]<=0)
1372         return false;
1373       szExp*=tmp4[i];
1374     }
1375   if(szExp!=(int)sz)
1376     return false;
1377   const int *w(startIds);
1378   switch(dim)
1379   {
1380     case 3:
1381       {
1382         for(int i=0;i<tmp4[2];i++)
1383           {
1384             int a=tmp2[2]*(partCompactFormat[2].first+i);
1385             for(int j=0;j<tmp4[1];j++)
1386               {
1387                 int b=tmp2[1]*(partCompactFormat[1].first+j);
1388                 for(int k=0;k<tmp4[0];k++,w++)
1389                   {
1390                     if(partCompactFormat[0].first+k+b+a!=*w)
1391                       return false;
1392                   }
1393               }
1394           }
1395         return true;
1396       }
1397     case 2:
1398       {
1399         for(int j=0;j<tmp4[1];j++)
1400           {
1401             int b=tmp2[1]*(partCompactFormat[1].first+j);
1402             for(int k=0;k<tmp4[0];k++,w++)
1403               {
1404                 if(partCompactFormat[0].first+k+b!=*w)
1405                   return false;
1406               }
1407           }
1408         return true;
1409       }
1410     case 1:
1411       {
1412         for(int k=0;k<tmp4[0];k++,w++)
1413           {
1414             if(partCompactFormat[0].first+k!=*w)
1415               return false;
1416           }
1417         return true;
1418       }
1419     default:
1420       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : internal error !");
1421   }
1422 }
1423
1424 /*!
1425  * This method takes in input a compact format [[Xmax,Xmin),[Ymin,Ymax)] and returns the corresponding dimensions for each axis that is to say
1426  * [Xmax-Xmin,Ymax-Ymin].
1427  *
1428  * \throw if an axis range is so that max<min
1429  * \sa GetCompactFrmtFromDimensions
1430  */
1431 std::vector<int> MEDCouplingStructuredMesh::GetDimensionsFromCompactFrmt(const std::vector< std::pair<int,int> >& partCompactFormat)
1432 {
1433   std::vector<int> ret(partCompactFormat.size());
1434   for(std::size_t i=0;i<partCompactFormat.size();i++)
1435     {
1436       if(partCompactFormat[i].first>partCompactFormat[i].second)
1437         {
1438           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::GetDimensionsFromCompactFrmt : For axis #" << i << " end is before start !";
1439           throw INTERP_KERNEL::Exception(oss.str().c_str());
1440         }
1441       ret[i]=partCompactFormat[i].second-partCompactFormat[i].first;
1442     }
1443   return ret;
1444 }
1445
1446 /*!
1447  * This method takes in input a vector giving the number of entity per axis and returns for each axis a range starting from [0,0...]
1448  *
1449  * \throw if there is an axis in \a dims that is < 0.
1450  * \sa GetDimensionsFromCompactFrmt, ChangeReferenceFromGlobalOfCompactFrmt, ChangeReferenceToGlobalOfCompactFrmt
1451  */
1452 std::vector< std::pair<int,int> > MEDCouplingStructuredMesh::GetCompactFrmtFromDimensions(const std::vector<int>& dims)
1453 {
1454   std::size_t sz(dims.size());
1455   std::vector< std::pair<int,int> > ret(sz);
1456   for(std::size_t i=0;i<sz;i++)
1457     {
1458       if(dims[i]<0)
1459         {
1460           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::GetDimensionsFromCompactFrmt : For axis #" << i << " dimension < 0 !";
1461           throw INTERP_KERNEL::Exception(oss.str().c_str());
1462         }
1463       ret[i].first=0;
1464       ret[i].second=dims[i];
1465     }
1466   return ret;
1467 }
1468
1469 /*!
1470  * This method returns the intersection zone of two ranges (in compact format) \a r1 and \a r2.
1471  * This method will throw exception if on one axis the intersection is empty.
1472  */
1473 std::vector< std::pair<int,int> > MEDCouplingStructuredMesh::IntersectRanges(const std::vector< std::pair<int,int> >& r1, const std::vector< std::pair<int,int> >& r2)
1474 {
1475   std::size_t sz(r1.size());
1476   if(sz!=r2.size())
1477     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IntersectRanges : the two ranges must have the same dimension !");
1478   std::vector< std::pair<int,int> > ret(sz);
1479   for(std::size_t i=0;i<sz;i++)
1480     {
1481       if(r1[i].first>r1[i].second)
1482         {
1483           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::IntersectRanges : On axis " << i << " of range r1, end is before start !";
1484           throw INTERP_KERNEL::Exception(oss.str().c_str());
1485         }
1486       if(r2[i].first>r2[i].second)
1487         {
1488           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::IntersectRanges : On axis " << i << " of range r2, end is before start !";
1489           throw INTERP_KERNEL::Exception(oss.str().c_str());
1490         }
1491       ret[i].first=std::max(r1[i].first,r2[i].first);
1492       ret[i].second=std::min(r1[i].second,r2[i].second);
1493       if(ret[i].first>ret[i].second)
1494         {
1495           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::IntersectRanges : On axis " << i << " the intersection of r1 and r2 is empty !";
1496           throw INTERP_KERNEL::Exception(oss.str().c_str());
1497         }
1498     }
1499   return ret;
1500 }
1501
1502 /*!
1503  * This method is close to BuildExplicitIdsFrom except that instead of returning a DataArrayInt instance containing explicit ids it
1504  * enable elems in the vector of booleans (for performance reasons). As it is method for performance, this method is \b not
1505  * available in python.
1506  *
1507  * \param [in] st The entity structure.
1508  * \param [in] partCompactFormat The compact subpart to be enabled.
1509  * \param [in,out] vectToSwitchOn Vector which fetched items are enabled.
1510  *
1511  * \sa MEDCouplingStructuredMesh::BuildExplicitIdsFrom, ExtractFieldOfBoolFrom
1512  */
1513 void MEDCouplingStructuredMesh::SwitchOnIdsFrom(const std::vector<int>& st, const std::vector< std::pair<int,int> >& partCompactFormat, std::vector<bool>& vectToSwitchOn)
1514 {
1515   if(st.size()!=partCompactFormat.size())
1516     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::SwitchOnIdsFrom : input arrays must have the same size !");
1517   if((int)vectToSwitchOn.size()!=DeduceNumberOfGivenStructure(st))
1518     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::SwitchOnIdsFrom : invalid size of input vector of boolean regarding the structure !");
1519   std::vector<int> dims(GetDimensionsFromCompactFrmt(partCompactFormat));
1520   switch(st.size())
1521   {
1522     case 3:
1523       {
1524         for(int i=0;i<dims[2];i++)
1525           {
1526             int a=(partCompactFormat[2].first+i)*st[0]*st[1];
1527             for(int j=0;j<dims[1];j++)
1528               {
1529                 int b=(partCompactFormat[1].first+j)*st[0];
1530                 for(int k=0;k<dims[0];k++)
1531                   vectToSwitchOn[partCompactFormat[0].first+k+b+a]=true;
1532               }
1533           }
1534         break;
1535       }
1536     case 2:
1537       {
1538         for(int j=0;j<dims[1];j++)
1539           {
1540             int b=(partCompactFormat[1].first+j)*st[0];
1541             for(int k=0;k<dims[0];k++)
1542               vectToSwitchOn[partCompactFormat[0].first+k+b]=true;
1543           }
1544         break;
1545       }
1546     case 1:
1547       {
1548         for(int k=0;k<dims[0];k++)
1549           vectToSwitchOn[partCompactFormat[0].first+k]=true;
1550         break;
1551       }
1552     default:
1553       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::SwitchOnIdsFrom : Dimension supported are 1,2 or 3 !");
1554   }
1555 }
1556
1557 /*!
1558  * Obviously this method is \b NOT wrapped in python.
1559  * This method is close to SwitchOnIdsFrom except that here, a sub field \a fieldOut is built starting from the input field \a fieldOfBool having the structure \a st.
1560  * The extraction is defined by \a partCompactFormat.
1561  *
1562  * \param [in] st The entity structure.
1563  * \param [in] fieldOfBool field of booleans having the size equal to \c MEDCouplingStructuredMesh::DeduceNumberOfGivenStructure(st).
1564  * \param [in] partCompactFormat The compact subpart to be enabled.
1565  * \param [out] fieldOut the result of the extraction.
1566  *
1567  * \sa MEDCouplingStructuredMesh::BuildExplicitIdsFrom, SwitchOnIdsFrom, ExtractFieldOfDoubleFrom
1568  */
1569 void MEDCouplingStructuredMesh::ExtractFieldOfBoolFrom(const std::vector<int>& st, const std::vector<bool>& fieldOfBool, const std::vector< std::pair<int,int> >& partCompactFormat, std::vector<bool>& fieldOut)
1570 {
1571   if(st.size()!=partCompactFormat.size())
1572     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::ExtractFieldOfBoolFrom : input arrays must have the same size !");
1573   if((int)fieldOfBool.size()!=DeduceNumberOfGivenStructure(st))
1574     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::ExtractFieldOfBoolFrom : invalid size of input field of boolean regarding the structure !");
1575   std::vector<int> dims(GetDimensionsFromCompactFrmt(partCompactFormat));
1576   int nbOfTuplesOfOutField(DeduceNumberOfGivenStructure(dims));
1577   fieldOut.resize(nbOfTuplesOfOutField);
1578   int it(0);
1579   switch(st.size())
1580   {
1581     case 3:
1582       {
1583         for(int i=0;i<dims[2];i++)
1584           {
1585             int a=(partCompactFormat[2].first+i)*st[0]*st[1];
1586             for(int j=0;j<dims[1];j++)
1587               {
1588                 int b=(partCompactFormat[1].first+j)*st[0];
1589                 for(int k=0;k<dims[0];k++)
1590                   fieldOut[it++]=fieldOfBool[partCompactFormat[0].first+k+b+a];
1591               }
1592           }
1593         break;
1594       }
1595     case 2:
1596       {
1597         for(int j=0;j<dims[1];j++)
1598           {
1599             int b=(partCompactFormat[1].first+j)*st[0];
1600             for(int k=0;k<dims[0];k++)
1601               fieldOut[it++]=fieldOfBool[partCompactFormat[0].first+k+b];
1602           }
1603         break;
1604       }
1605     case 1:
1606       {
1607         for(int k=0;k<dims[0];k++)
1608           fieldOut[it++]=fieldOfBool[partCompactFormat[0].first+k];
1609         break;
1610       }
1611     default:
1612       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::ExtractFieldOfBoolFrom : Dimension supported are 1,2 or 3 !");
1613   }
1614 }
1615
1616 /*!
1617  * This method is close to SwitchOnIdsFrom except that here, a sub field \a fieldOut is built starting from the input field \a fieldOfDbl having the structure \a st.
1618  * The extraction is defined by \a partCompactFormat.
1619  *
1620  * \param [in] st The entity structure.
1621  * \param [in] fieldOfDbl field of doubles having a number of tuples equal to \c MEDCouplingStructuredMesh::DeduceNumberOfGivenStructure(st).
1622  * \param [in] partCompactFormat The compact subpart to be enabled.
1623  * \return DataArrayDouble * -the result of the extraction.
1624  *
1625  * \sa MEDCouplingStructuredMesh::BuildExplicitIdsFrom, SwitchOnIdsFrom, ExtractFieldOfBoolFrom
1626  */
1627 DataArrayDouble *MEDCouplingStructuredMesh::ExtractFieldOfDoubleFrom(const std::vector<int>& st, const DataArrayDouble *fieldOfDbl, const std::vector< std::pair<int,int> >& partCompactFormat)
1628 {
1629   if(!fieldOfDbl || !fieldOfDbl->isAllocated())
1630     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::ExtractFieldOfDoubleFrom : input array of double is NULL or not allocated!");
1631   if(st.size()!=partCompactFormat.size())
1632     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::ExtractFieldOfDoubleFrom : input arrays must have the same size !");
1633   if(fieldOfDbl->getNumberOfTuples()!=DeduceNumberOfGivenStructure(st))
1634     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::ExtractFieldOfDoubleFrom : invalid size of input array of double regarding the structure !");
1635   std::vector<int> dims(GetDimensionsFromCompactFrmt(partCompactFormat));
1636   int nbOfTuplesOfOutField(DeduceNumberOfGivenStructure(dims)),nbComp(fieldOfDbl->getNumberOfComponents());
1637   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret(DataArrayDouble::New()); ret->alloc(nbOfTuplesOfOutField,nbComp);
1638   ret->copyStringInfoFrom(*fieldOfDbl);
1639   double *ptRet(ret->getPointer());
1640   const double *fieldOfDblPtr(fieldOfDbl->begin());
1641   switch(st.size())
1642   {
1643     case 3:
1644       {
1645         for(int i=0;i<dims[2];i++)
1646           {
1647             int a=(partCompactFormat[2].first+i)*st[0]*st[1];
1648             for(int j=0;j<dims[1];j++)
1649               {
1650                 int b=(partCompactFormat[1].first+j)*st[0];
1651                 for(int k=0;k<dims[0];k++)
1652                   ptRet=std::copy(fieldOfDblPtr+(partCompactFormat[0].first+k+b+a)*nbComp,fieldOfDblPtr+(partCompactFormat[0].first+k+b+a+1)*nbComp,ptRet);
1653               }
1654           }
1655         break;
1656       }
1657     case 2:
1658       {
1659         for(int j=0;j<dims[1];j++)
1660           {
1661             int b=(partCompactFormat[1].first+j)*st[0];
1662             for(int k=0;k<dims[0];k++)
1663               ptRet=std::copy(fieldOfDblPtr+(partCompactFormat[0].first+k+b)*nbComp,fieldOfDblPtr+(partCompactFormat[0].first+k+b+1)*nbComp,ptRet);
1664           }
1665         break;
1666       }
1667     case 1:
1668       {
1669         for(int k=0;k<dims[0];k++)
1670           ptRet=std::copy(fieldOfDblPtr+(partCompactFormat[0].first+k)*nbComp,fieldOfDblPtr+(partCompactFormat[0].first+k+1)*nbComp,ptRet);
1671         break;
1672       }
1673     default:
1674       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::ExtractFieldOfDoubleFrom : Dimension supported are 1,2 or 3 !");
1675   }
1676   return ret.retn();
1677 }
1678
1679 /*!
1680  * This method changes the reference of a part of structured mesh \a partOfBigInAbs define in absolute reference to a new reference \a bigInAbs.
1681  * So this method only performs a translation by doing \a partOfBigRelativeToBig = \a partOfBigInAbs - \a bigInAbs
1682  * This method also checks (if \a check=true) that \a partOfBigInAbs is included in \a bigInAbs.
1683  * This method is useful to extract a part from a field lying on a big mesh.
1684  *
1685  * \sa ChangeReferenceToGlobalOfCompactFrmt, BuildExplicitIdsFrom, SwitchOnIdsFrom, ExtractFieldOfBoolFrom, ExtractFieldOfDoubleFrom
1686  */
1687 void MEDCouplingStructuredMesh::ChangeReferenceFromGlobalOfCompactFrmt(const std::vector< std::pair<int,int> >& bigInAbs, const std::vector< std::pair<int,int> >& partOfBigInAbs, std::vector< std::pair<int,int> >& partOfBigRelativeToBig, bool check)
1688 {
1689   std::size_t dim(bigInAbs.size());
1690   if(dim!=partOfBigInAbs.size())
1691     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::ChangeReferenceFromGlobalOfCompactFrmt : The size of parts (dimension) must be the same !");
1692   partOfBigRelativeToBig.resize(dim);
1693   for(std::size_t i=0;i<dim;i++)
1694     {
1695       if(check)
1696         {
1697           if(bigInAbs[i].first>bigInAbs[i].second)
1698             {
1699               std::ostringstream oss; oss << "MEDCouplingStructuredMesh::ChangeReferenceFromGlobalOfCompactFrmt : Error at axis #" << i << " the input big part invalid, end before start !";
1700               throw INTERP_KERNEL::Exception(oss.str().c_str());
1701             }
1702           if(partOfBigInAbs[i].first<bigInAbs[i].first || partOfBigInAbs[i].first>=bigInAbs[i].second)
1703             {
1704               std::ostringstream oss; oss << "MEDCouplingStructuredMesh::ChangeReferenceFromGlobalOfCompactFrmt : Error at axis #" << i << " the part is not included in the big one (start) !";
1705               throw INTERP_KERNEL::Exception(oss.str().c_str());
1706             }
1707         }
1708       partOfBigRelativeToBig[i].first=partOfBigInAbs[i].first-bigInAbs[i].first;
1709       if(check)
1710         {
1711           if(partOfBigInAbs[i].second<partOfBigInAbs[i].first || partOfBigInAbs[i].second>bigInAbs[i].second)
1712             {
1713               std::ostringstream oss; oss << "MEDCouplingStructuredMesh::ChangeReferenceFromGlobalOfCompactFrmt : Error at axis #" << i << " the part is not included in the big one (end) !";
1714               throw INTERP_KERNEL::Exception(oss.str().c_str());
1715             }
1716         }
1717       partOfBigRelativeToBig[i].second=partOfBigInAbs[i].second-bigInAbs[i].first;
1718     }
1719 }
1720
1721 /*
1722  * This method is performs the opposite reference modification than explained in ChangeReferenceFromGlobalOfCompactFrmt.
1723  *
1724  * \sa ChangeReferenceFromGlobalOfCompactFrmt
1725  */
1726 void MEDCouplingStructuredMesh::ChangeReferenceToGlobalOfCompactFrmt(const std::vector< std::pair<int,int> >& bigInAbs, const std::vector< std::pair<int,int> >& partOfBigRelativeToBig, std::vector< std::pair<int,int> >& partOfBigInAbs, bool check)
1727 {
1728   std::size_t dim(bigInAbs.size());
1729   if(dim!=partOfBigRelativeToBig.size())
1730     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::ChangeReferenceToGlobalOfCompactFrmt : The size of parts (dimension) must be the same !");
1731   partOfBigInAbs.resize(dim);
1732   for(std::size_t i=0;i<dim;i++)
1733     {
1734       if(check)
1735         {
1736           if(bigInAbs[i].first>bigInAbs[i].second)
1737             {
1738               std::ostringstream oss; oss << "MEDCouplingStructuredMesh::ChangeReferenceToGlobalOfCompactFrmt : Error at axis #" << i << " the input big part invalid, end before start !";
1739               throw INTERP_KERNEL::Exception(oss.str().c_str());
1740             }
1741           if(partOfBigRelativeToBig[i].first<0 || partOfBigRelativeToBig[i].first>=bigInAbs[i].second-bigInAbs[i].first)
1742             {
1743               std::ostringstream oss; oss << "MEDCouplingStructuredMesh::ChangeReferenceToGlobalOfCompactFrmt : Error at axis #" << i << " the start of part is not in the big one !";
1744               throw INTERP_KERNEL::Exception(oss.str().c_str());
1745             }
1746         }
1747       partOfBigInAbs[i].first=partOfBigRelativeToBig[i].first+bigInAbs[i].first;
1748       if(check)
1749         {
1750           if(partOfBigRelativeToBig[i].second<partOfBigRelativeToBig[i].first || partOfBigRelativeToBig[i].second>bigInAbs[i].second-bigInAbs[i].first)
1751             {
1752               std::ostringstream oss; oss << "MEDCouplingStructuredMesh::ChangeReferenceToGlobalOfCompactFrmt : Error at axis #" << i << " the end of part is not in the big one !";
1753               throw INTERP_KERNEL::Exception(oss.str().c_str());
1754             }
1755         }
1756       partOfBigInAbs[i].second=partOfBigRelativeToBig[i].second+bigInAbs[i].first;
1757     }
1758 }
1759
1760 /*!
1761  * This method performs a translation (defined by \a translation) of \a part and returns the result of translated part.
1762  *
1763  * \sa FindTranslationFrom
1764  */
1765 std::vector< std::pair<int,int> > MEDCouplingStructuredMesh::TranslateCompactFrmt(const std::vector< std::pair<int,int> >& part, const std::vector<int>& translation)
1766 {
1767   std::size_t sz(part.size());
1768   if(translation.size()!=sz)
1769     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::TranslateCompactFrmt : the sizes are not equal !");
1770   std::vector< std::pair<int,int> > ret(sz);
1771   for(std::size_t i=0;i<sz;i++)
1772     {
1773       ret[i].first=part[i].first+translation[i];
1774       ret[i].second=part[i].second+translation[i];
1775     }
1776   return ret;
1777 }
1778
1779 /*!
1780  * \sa TranslateCompactFrmt
1781  */
1782 std::vector<int> MEDCouplingStructuredMesh::FindTranslationFrom(const std::vector< std::pair<int,int> >& startingFrom, const std::vector< std::pair<int,int> >& goingTo)
1783 {
1784   std::size_t sz(startingFrom.size());
1785   if(goingTo.size()!=sz)
1786     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::FindTranslationFrom : the sizes are not equal !");
1787   std::vector< int > ret(sz);
1788   for(std::size_t i=0;i<sz;i++)
1789     {
1790       ret[i]=goingTo[i].first-startingFrom[i].first;
1791     }
1792   return ret;
1793 }
1794
1795 /*!
1796  * This method builds the explicit entity array from the structure in \a st and the range in \a partCompactFormat.
1797  * If the range contains invalid values regarding sructure an exception will be thrown.
1798  *
1799  * \return DataArrayInt * - a new object.
1800  * \sa MEDCouplingStructuredMesh::IsPartStructured, MEDCouplingStructuredMesh::DeduceNumberOfGivenRangeInCompactFrmt, SwitchOnIdsFrom, ExtractFieldOfBoolFrom, ExtractFieldOfDoubleFrom, MultiplyPartOf
1801  */
1802 DataArrayInt *MEDCouplingStructuredMesh::BuildExplicitIdsFrom(const std::vector<int>& st, const std::vector< std::pair<int,int> >& partCompactFormat)
1803 {
1804   if(st.size()!=partCompactFormat.size())
1805     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : input arrays must have the same size !");
1806   int nbOfItems(1);
1807   std::vector<int> dims(st.size());
1808   for(std::size_t i=0;i<st.size();i++)
1809     {
1810       if(partCompactFormat[i].first<0 || partCompactFormat[i].first>st[i])
1811         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : invalid input range 1 !");
1812       if(partCompactFormat[i].second<0 || partCompactFormat[i].second>st[i])
1813         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : invalid input range 2 !");
1814       if(partCompactFormat[i].second<partCompactFormat[i].first)
1815         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : invalid input range 3 !");
1816       dims[i]=partCompactFormat[i].second-partCompactFormat[i].first;
1817       nbOfItems*=dims[i];
1818     }
1819   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New());
1820   ret->alloc(nbOfItems,1);
1821   int *pt(ret->getPointer());
1822   switch(st.size())
1823   {
1824     case 3:
1825       {
1826         for(int i=0;i<dims[2];i++)
1827           {
1828             int a=(partCompactFormat[2].first+i)*st[0]*st[1];
1829             for(int j=0;j<dims[1];j++)
1830               {
1831                 int b=(partCompactFormat[1].first+j)*st[0];
1832                 for(int k=0;k<dims[0];k++,pt++)
1833                   *pt=partCompactFormat[0].first+k+b+a;
1834               }
1835           }
1836         break;
1837       }
1838     case 2:
1839       {
1840         for(int j=0;j<dims[1];j++)
1841           {
1842             int b=(partCompactFormat[1].first+j)*st[0];
1843             for(int k=0;k<dims[0];k++,pt++)
1844               *pt=partCompactFormat[0].first+k+b;
1845           }
1846         break;
1847       }
1848     case 1:
1849       {
1850         for(int k=0;k<dims[0];k++,pt++)
1851           *pt=partCompactFormat[0].first+k;
1852         break;
1853       }
1854     default:
1855       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : Dimension supported are 1,2 or 3 !");
1856   }
1857   return ret.retn();
1858 }
1859
1860 /*!
1861  * This method multiplies by \a factor values in tuples located by \a part in \a da.
1862  *
1863  * \param [in] st - the structure of grid ( \b without considering ghost cells).
1864  * \param [in] part - the part in the structure ( \b without considering ghost cells) contained in grid whose structure is defined by \a st.
1865  * \param [in] factor - the factor, the tuples in \a da will be multiply by.
1866  * \param [in,out] da - The DataArray in wich only tuples specified by \a part will be modified.
1867  *
1868  * \sa BuildExplicitIdsFrom
1869  */
1870 void MEDCouplingStructuredMesh::MultiplyPartOf(const std::vector<int>& st, const std::vector< std::pair<int,int> >& part, double factor, DataArrayDouble *da)
1871 {
1872   if(!da || !da->isAllocated())
1873     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::MultiplyPartOf : DataArrayDouble instance must be not NULL and allocated !");
1874   if(st.size()!=part.size())
1875     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::MultiplyPartOf : input arrays must have the same size !");
1876   std::vector<int> dims(st.size());
1877   for(std::size_t i=0;i<st.size();i++)
1878     {
1879       if(part[i].first<0 || part[i].first>st[i])
1880         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::MultiplyPartOf : invalid input range 1 !");
1881       if(part[i].second<0 || part[i].second>st[i])
1882         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::MultiplyPartOf : invalid input range 2 !");
1883       if(part[i].second<part[i].first)
1884         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::MultiplyPartOf : invalid input range 3 !");
1885       dims[i]=part[i].second-part[i].first;
1886     }
1887   int nbOfTuplesExp(MEDCouplingStructuredMesh::DeduceNumberOfGivenStructure(st)),nbCompo(da->getNumberOfComponents());
1888   if(da->getNumberOfTuples()!=nbOfTuplesExp)
1889     {
1890       std::ostringstream oss; oss << "MEDCouplingStructuredMesh::MultiplyPartOf : invalid nb of tuples ! Expected " << nbOfTuplesExp << " having " << da->getNumberOfTuples() << " !";
1891       throw INTERP_KERNEL::Exception(oss.str().c_str());
1892     }
1893   double *pt(da->getPointer());
1894   switch(st.size())
1895   {
1896     case 3:
1897       {
1898         for(int i=0;i<dims[2];i++)
1899           {
1900             int a=(part[2].first+i)*st[0]*st[1];
1901             for(int j=0;j<dims[1];j++)
1902               {
1903                 int b=(part[1].first+j)*st[0];
1904                 for(int k=0;k<dims[0];k++)
1905                   {
1906                     int offset(part[0].first+k+b+a);
1907                     std::transform(pt+nbCompo*offset,pt+nbCompo*(offset+1),pt+nbCompo*offset,std::bind2nd(std::multiplies<double>(),factor));
1908                   }
1909               }
1910           }
1911         break;
1912       }
1913     case 2:
1914       {
1915         for(int j=0;j<dims[1];j++)
1916           {
1917             int b=(part[1].first+j)*st[0];
1918             for(int k=0;k<dims[0];k++)
1919               {
1920                 int offset(part[0].first+k+b);
1921                 std::transform(pt+nbCompo*offset,pt+nbCompo*(offset+1),pt+nbCompo*offset,std::bind2nd(std::multiplies<double>(),factor));
1922               }
1923           }
1924         break;
1925       }
1926     case 1:
1927       {
1928         for(int k=0;k<dims[0];k++)
1929           {
1930             int offset(part[0].first+k);
1931             std::transform(pt+nbCompo*offset,pt+nbCompo*(offset+1),pt+nbCompo*offset,std::bind2nd(std::multiplies<double>(),factor));
1932           }
1933         break;
1934       }
1935     default:
1936       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::MultiplyPartOf : Dimension supported are 1,2 or 3 !");
1937   }
1938 }
1939
1940 /*!
1941  * This method multiplies by \a factor values in tuples located by \a part in \a da.
1942  *
1943  * \param [in] st - the structure of grid ( \b without considering ghost cells).
1944  * \param [in] part - the part in the structure ( \b without considering ghost cells) contained in grid whose structure is defined by \a st.
1945  * \param [in] ghostSize - \a ghostSize must be >= 0.
1946  * \param [in] factor - the factor, the tuples in \a da will be multiply by.
1947  * \param [in,out] da - The DataArray in wich only tuples specified by \a part will be modified.
1948  *
1949  * \sa MultiplyPartOf, PutInGhostFormat
1950  */
1951 void MEDCouplingStructuredMesh::MultiplyPartOfByGhost(const std::vector<int>& st, const std::vector< std::pair<int,int> >& part, int ghostSize, double factor, DataArrayDouble *da)
1952 {
1953   std::vector<int> stWG;
1954   std::vector< std::pair<int,int> > partWG;
1955   PutInGhostFormat(ghostSize,st,part,stWG,partWG);
1956   MultiplyPartOf(stWG,partWG,factor,da);
1957 }
1958
1959 /*!
1960  * This method multiplies by \a factor values in tuples located by \a part in \a da.
1961  *
1962  * \param [in] st - the structure of grid ( \b without considering ghost cells).
1963  * \param [in] part - the part in the structure ( \b without considering ghost cells) contained in grid whose structure is defined by \a st.
1964  * \param [in] ghostSize - \a ghostSize must be >= 0.
1965  * \param [out] stWithGhost - the structure considering ghost cells.
1966  * \param [out] partWithGhost - the part considering the ghost cells.
1967  *
1968  * \sa MultiplyPartOf, PutInGhostFormat
1969  */
1970 void MEDCouplingStructuredMesh::PutInGhostFormat(int ghostSize, const std::vector<int>& st, const std::vector< std::pair<int,int> >& part, std::vector<int>& stWithGhost, std::vector< std::pair<int,int> >&partWithGhost)
1971 {
1972   if(ghostSize<0)
1973     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::PutInGhostFormat : ghost size must be >= 0 !");
1974   std::size_t dim(part.size());
1975   if(st.size()!=dim)
1976     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::PutInGhostFormat : the dimension of input vectors must be the same !");
1977   for(std::size_t i=0;i<dim;i++)
1978     if(part[i].first<0 || part[i].first>part[i].second || part[i].second>st[i])
1979       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::PutInGhostFormat : the specified part is invalid ! The begin must be >= 0 and <= end ! The end must be <= to the size at considered dimension !");
1980   stWithGhost.resize(st.size());
1981   std::transform(st.begin(),st.end(),stWithGhost.begin(),std::bind2nd(std::plus<int>(),2*ghostSize));
1982   partWithGhost=part;
1983   ApplyGhostOnCompactFrmt(partWithGhost,ghostSize);
1984 }
1985
1986 /*!
1987  * \param [in,out] partBeforeFact - the part of a image mesh in compact format that will be put in ghost reference.
1988  * \param [in] ghostSize - the ghost size of zone for all axis.
1989  */
1990 void MEDCouplingStructuredMesh::ApplyGhostOnCompactFrmt(std::vector< std::pair<int,int> >& partBeforeFact, int ghostSize)
1991 {
1992   if(ghostSize<0)
1993     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::ApplyGhostOnCompactFrmt : ghost size must be >= 0 !");
1994   std::size_t sz(partBeforeFact.size());
1995   for(std::size_t i=0;i<sz;i++)
1996     {
1997       partBeforeFact[i].first+=ghostSize;
1998       partBeforeFact[i].second+=ghostSize;
1999     }
2000 }
2001
2002 int MEDCouplingStructuredMesh::GetNumberOfCellsOfSubLevelMesh(const std::vector<int>& cgs, int mdim)
2003 {
2004   int ret(0);
2005   for(int i=0;i<mdim;i++)
2006     {
2007       int locRet(1);
2008       for(int j=0;j<mdim;j++)
2009         if(j!=i)
2010           locRet*=cgs[j];
2011         else
2012           locRet*=cgs[j]+1;
2013       ret+=locRet;
2014     }
2015   return ret;
2016 }