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