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