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