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