Salome HOME
Merge branch 'master' of salome:modules/med
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingStructuredMesh.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #include "MEDCouplingStructuredMesh.hxx"
22 #include "MEDCouplingFieldDouble.hxx"
23 #include "MEDCouplingMemArray.hxx"
24 #include "MEDCoupling1GTUMesh.hxx"
25 #include "MEDCouplingUMesh.hxx"
26
27 #include <numeric>
28
29 using namespace ParaMEDMEM;
30
31 MEDCouplingStructuredMesh::MEDCouplingStructuredMesh()
32 {
33 }
34
35 MEDCouplingStructuredMesh::MEDCouplingStructuredMesh(const MEDCouplingStructuredMesh& other, bool deepCopy):MEDCouplingMesh(other)
36 {
37 }
38
39 MEDCouplingStructuredMesh::~MEDCouplingStructuredMesh()
40 {
41 }
42
43 std::size_t MEDCouplingStructuredMesh::getHeapMemorySizeWithoutChildren() const
44 {
45   return MEDCouplingMesh::getHeapMemorySizeWithoutChildren();
46 }
47
48 void MEDCouplingStructuredMesh::copyTinyStringsFrom(const MEDCouplingMesh *other)
49 {
50   MEDCouplingMesh::copyTinyStringsFrom(other);
51 }
52
53 bool MEDCouplingStructuredMesh::isEqualIfNotWhy(const MEDCouplingMesh *other, double prec, std::string& reason) const
54 {
55   return MEDCouplingMesh::isEqualIfNotWhy(other,prec,reason);
56 }
57
58 INTERP_KERNEL::NormalizedCellType MEDCouplingStructuredMesh::getTypeOfCell(int cellId) const
59 {
60   return GetGeoTypeGivenMeshDimension(getMeshDimension());
61 }
62
63 INTERP_KERNEL::NormalizedCellType MEDCouplingStructuredMesh::GetGeoTypeGivenMeshDimension(int meshDim)
64 {
65   switch(meshDim)
66   {
67     case 3:
68       return INTERP_KERNEL::NORM_HEXA8;
69     case 2:
70       return INTERP_KERNEL::NORM_QUAD4;
71     case 1:
72       return INTERP_KERNEL::NORM_SEG2;
73     case 0:
74       return INTERP_KERNEL::NORM_POINT1;
75     default:
76       throw INTERP_KERNEL::Exception("Unexpected dimension for MEDCouplingStructuredMesh::GetGeoTypeGivenMeshDimension !");
77   }
78 }
79
80 std::set<INTERP_KERNEL::NormalizedCellType> MEDCouplingStructuredMesh::getAllGeoTypes() const
81 {
82   std::set<INTERP_KERNEL::NormalizedCellType> ret2;
83   ret2.insert(getTypeOfCell(0));
84   return ret2;
85 }
86
87 int MEDCouplingStructuredMesh::getNumberOfCellsWithType(INTERP_KERNEL::NormalizedCellType type) const
88 {
89   int ret=getNumberOfCells();
90   if(type==getTypeOfCell(0))
91     return ret;
92   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
93   std::ostringstream oss; oss << "MEDCouplingStructuredMesh::getNumberOfCellsWithType : no specified type ! Type available is " << cm.getRepr() << " !";
94   throw INTERP_KERNEL::Exception(oss.str().c_str());
95 }
96
97 DataArrayInt *MEDCouplingStructuredMesh::giveCellsWithType(INTERP_KERNEL::NormalizedCellType type) const
98 {
99   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
100   if(getTypeOfCell(0)==type)
101     {
102       ret->alloc(getNumberOfCells(),1);
103       ret->iota(0);
104     }
105   else
106     ret->alloc(0,1);
107   return ret.retn();
108 }
109
110 DataArrayInt *MEDCouplingStructuredMesh::computeNbOfNodesPerCell() const
111 {
112   int nbCells=getNumberOfCells();
113   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
114   ret->alloc(nbCells,1);
115   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
116   ret->fillWithValue((int)cm.getNumberOfNodes());
117   return ret.retn();
118 }
119
120 DataArrayInt *MEDCouplingStructuredMesh::computeNbOfFacesPerCell() const
121 {
122   int nbCells=getNumberOfCells();
123   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
124   ret->alloc(nbCells,1);
125   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
126   ret->fillWithValue((int)cm.getNumberOfSons());
127   return ret.retn();
128 }
129
130 /*!
131  * This method computes effective number of nodes per cell. That is to say nodes appearing several times in nodal connectivity of a cell,
132  * will be counted only once here whereas it will be counted several times in MEDCouplingMesh::computeNbOfNodesPerCell method.
133  * Here for structured mesh it returns exactly as MEDCouplingStructuredMesh::computeNbOfNodesPerCell does.
134  *
135  * \return DataArrayInt * - new object to be deallocated by the caller.
136  */
137 DataArrayInt *MEDCouplingStructuredMesh::computeEffectiveNbOfNodesPerCell() const
138 {
139   return computeNbOfNodesPerCell();
140 }
141
142 void MEDCouplingStructuredMesh::getNodeIdsOfCell(int cellId, std::vector<int>& conn) const
143 {
144   int meshDim=getMeshDimension();
145   int tmpCell[3],tmpNode[3];
146   getSplitCellValues(tmpCell);
147   getSplitNodeValues(tmpNode);
148   int tmp2[3];
149   GetPosFromId(cellId,meshDim,tmpCell,tmp2);
150   switch(meshDim)
151   {
152     case 1:
153       conn.push_back(tmp2[0]); conn.push_back(tmp2[0]+1);
154       break;
155     case 2:
156       conn.push_back(tmp2[1]*tmpNode[1]+tmp2[0]); conn.push_back(tmp2[1]*tmpNode[1]+tmp2[0]+1);
157       conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]+1); conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]);
158       break;
159     case 3:
160       conn.push_back(tmp2[1]*tmpNode[1]+tmp2[0]+tmp2[2]*tmpNode[2]); conn.push_back(tmp2[1]*tmpNode[1]+tmp2[0]+1+tmp2[2]*tmpNode[2]);
161       conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]+1+tmp2[2]*tmpNode[2]); conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]+tmp2[2]*tmpNode[2]);
162       conn.push_back(tmp2[1]*tmpNode[1]+tmp2[0]+(tmp2[2]+1)*tmpNode[2]); conn.push_back(tmp2[1]*tmpNode[1]+tmp2[0]+1+(tmp2[2]+1)*tmpNode[2]);
163       conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]+1+(tmp2[2]+1)*tmpNode[2]); conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]+(tmp2[2]+1)*tmpNode[2]);
164       break;
165     default:
166       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::getNodeIdsOfCell : big problem spacedim must be in 1,2 or 3 !");
167   };
168 }
169
170 /*!
171  * This method returns the mesh dimension of \a this. It can be different from space dimension in case of a not null dimension contains only one node.
172  */
173 int MEDCouplingStructuredMesh::getMeshDimension() const
174 {
175   std::vector<int> ngs(getNodeGridStructure());
176   int ret(0),pos(0);
177   for(std::vector<int>::const_iterator it=ngs.begin();it!=ngs.end();it++,pos++)
178     {
179       if(*it<=0)
180         {
181           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::getMeshDimension : At pos #" << pos << " number of nodes is " << *it << " ! Must be > 0 !";
182           throw INTERP_KERNEL::Exception(oss.str().c_str());
183         }
184       if(*it>1)
185         ret++;
186     }
187   return ret;
188 }
189
190 /*!
191  * This method returns the space dimension by only considering the node grid structure.
192  * For cartesian mesh the returned value is equal to those returned by getSpaceDimension.
193  * But for curvelinear is could be different !
194  */
195 int MEDCouplingStructuredMesh::getSpaceDimensionOnNodeStruct() const
196 {
197   std::vector<int> nodeStr(getNodeGridStructure());
198   int spd1(0),pos(0);
199   for(std::vector<int>::const_iterator it=nodeStr.begin();it!=nodeStr.end();it++,pos++)
200     {
201       int elt(*it);
202       if(elt<=0)
203         {
204           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::getSpaceDimensionOnNodeStruct : At pos #" << pos << " value of node grid structure is " << *it << " ! must be >=1 !";
205           throw INTERP_KERNEL::Exception(oss.str().c_str());
206         }
207       spd1++;
208     }
209   return spd1;
210 }
211
212 void MEDCouplingStructuredMesh::getSplitCellValues(int *res) const
213 {
214   std::vector<int> strct(getCellGridStructure());
215   std::vector<int> ret(MEDCouplingStructuredMesh::GetSplitVectFromStruct(strct));
216   std::copy(ret.begin(),ret.end(),res);
217 }
218
219 void MEDCouplingStructuredMesh::getSplitNodeValues(int *res) const
220 {
221   std::vector<int> strct(getNodeGridStructure());
222   std::vector<int> ret(MEDCouplingStructuredMesh::GetSplitVectFromStruct(strct));
223   std::copy(ret.begin(),ret.end(),res);
224 }
225
226 /*!
227  * This method returns the number of cells of unstructured sub level mesh, without building it.
228  */
229 int MEDCouplingStructuredMesh::getNumberOfCellsOfSubLevelMesh() const
230 {
231   std::vector<int> cgs(getCellGridStructure());
232   return GetNumberOfCellsOfSubLevelMesh(cgs,getMeshDimension());
233 }
234
235 /*!
236  * See MEDCouplingUMesh::getDistributionOfTypes for more information
237  */
238 std::vector<int> MEDCouplingStructuredMesh::getDistributionOfTypes() const
239 {
240   //only one type of cell
241   std::vector<int> ret(3);
242   ret[0]=getTypeOfCell(0);
243   ret[1]=getNumberOfCells();
244   ret[2]=-1; //ret[3*k+2]==-1 because it has no sense here
245   return ret;
246 }
247
248 /*!
249  * This method tries to minimize at most the number of deep copy.
250  * So if \a idsPerType is not empty it can be returned directly (without copy, but with ref count incremented) in return.
251  * 
252  * See MEDCouplingUMesh::checkTypeConsistencyAndContig for more information
253  */
254 DataArrayInt *MEDCouplingStructuredMesh::checkTypeConsistencyAndContig(const std::vector<int>& code, const std::vector<const DataArrayInt *>& idsPerType) const
255 {
256   int nbOfCells=getNumberOfCells();
257   if(code.size()!=3)
258     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : invalid input code should be exactly of size 3 !");
259   if(code[0]!=(int)getTypeOfCell(0))
260     {
261       std::ostringstream oss; oss << "MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : Mismatch of geometric type ! Asking for " << code[0] << " whereas the geometric type is \a this is " << getTypeOfCell(0) << " !";
262       throw INTERP_KERNEL::Exception(oss.str().c_str());
263     }
264   if(code[2]==-1)
265     {
266       if(code[1]==nbOfCells)
267         return 0;
268       else
269         {
270           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : mismatch between the number of cells in this (" << nbOfCells << ") and the number of non profile (" << code[1] << ") !";
271           throw INTERP_KERNEL::Exception(oss.str().c_str());
272         }
273     }
274   if(code[2]!=0)
275     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : single geo type mesh ! 0 or -1 is expected at pos #2 of input code !");
276   if(idsPerType.size()!=1)
277     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : input code points to DataArrayInt #0 whereas the size of idsPerType is not equal to 1 !");
278   const DataArrayInt *pfl=idsPerType[0];
279   if(!pfl)
280     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : the input code points to a NULL DataArrayInt at rank 0 !");
281   if(pfl->getNumberOfComponents()!=1)
282     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : input profile should have exactly one component !");
283   pfl->checkAllIdsInRange(0,nbOfCells);
284   pfl->incrRef();
285   return const_cast<DataArrayInt *>(pfl);
286 }
287
288 /*!
289  * This method is the opposite of MEDCouplingUMesh::checkTypeConsistencyAndContig method. Given a list of cells in \a profile it returns a list of sub-profiles sorted by geo type.
290  * The result is put in the array \a idsPerType. In the returned parameter \a code, foreach i \a code[3*i+2] refers (if different from -1) to a location into the \a idsPerType.
291  * This method has 1 input \a profile and 3 outputs \a code \a idsInPflPerType and \a idsPerType.
292  * 
293  * \param [out] code is a vector of size 3*n where n is the number of different geometric type in \a this \b reduced to the profile \a profile. \a code has exactly the same semantic than in MEDCouplingUMesh::checkTypeConsistencyAndContig method.
294  * \param [out] idsInPflPerType is a vector of size of different geometric type in the subpart defined by \a profile of \a this ( equal to \a code.size()/3). For each i,
295  *              \a idsInPflPerType[i] stores the tuple ids in \a profile that correspond to the geometric type code[3*i+0]
296  * \param [out] idsPerType is a vector of size of different sub profiles needed to be defined to represent the profile \a profile for a given geometric type.
297  *              This vector can be empty in case of all geometric type cells are fully covered in ascending in the given input \a profile.
298  * 
299  * \warning for performance reasons no deep copy will be performed, if \a profile can been used as this in output parameters \a idsInPflPerType and \a idsPerType.
300  *
301  * \throw if \a profile has not exactly one component. It throws too, if \a profile contains some values not in [0,getNumberOfCells()) or if \a this is not fully defined
302  *
303  *  \b Example1: <br>
304  *          - Before \a this has 3 cells \a profile contains [0,1,2]
305  *          - After \a code contains [NORM_...,nbCells,-1], \a idsInPflPerType [[0,1,2]] and \a idsPerType is empty <br>
306  * 
307  *  \b Example2: <br>
308  *          - Before \a this has 3 cells \a profile contains [1,2]
309  *          - After \a code contains [NORM_...,nbCells,0], \a idsInPflPerType [[0,1]] and \a idsPerType is [[1,2]] <br>
310
311  */
312 void MEDCouplingStructuredMesh::splitProfilePerType(const DataArrayInt *profile, std::vector<int>& code, std::vector<DataArrayInt *>& idsInPflPerType, std::vector<DataArrayInt *>& idsPerType) const
313 {
314   if(!profile || !profile->isAllocated())
315     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::splitProfilePerType : input profile is NULL or not allocated !");
316   if(profile->getNumberOfComponents()!=1)
317     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::splitProfilePerType : input profile should have exactly one component !");
318   int nbTuples=profile->getNumberOfTuples();
319   int nbOfCells=getNumberOfCells();
320   code.resize(3); idsInPflPerType.resize(1);
321   code[0]=(int)getTypeOfCell(0); code[1]=nbOfCells;
322   idsInPflPerType.resize(1);
323   if(profile->isIdentity() && nbTuples==nbOfCells)
324     {
325       code[2]=-1;
326       idsInPflPerType[0]=0;
327       idsPerType.clear();
328       return ;
329     }
330   code[1]=profile->getNumberOfTuples();
331   code[2]=0;
332   profile->checkAllIdsInRange(0,nbOfCells);
333   idsPerType.resize(1);
334   idsPerType[0]=profile->deepCpy();
335   idsInPflPerType[0]=DataArrayInt::Range(0,nbTuples,1);
336 }
337
338 /*!
339  * Creates a new unstructured mesh (MEDCoupling1SGTUMesh) from \a this structured one.
340  *  \return MEDCouplingUMesh * - a new instance of MEDCouplingUMesh. The caller is to
341  * delete this array using decrRef() as it is no more needed. 
342  *  \throw If \a this->getMeshDimension() is not among [1,2,3].
343  */
344 MEDCoupling1SGTUMesh *MEDCouplingStructuredMesh::build1SGTUnstructured() const
345 {
346   int meshDim(getMeshDimension()),spaceDim(getSpaceDimensionOnNodeStruct());
347   if((meshDim<0 || meshDim>3) || (spaceDim<0 || spaceDim>3))
348     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::build1SGTUnstructured : meshdim and spacedim must be in [1,2,3] !");
349   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> coords(getCoordinatesAndOwner());
350   int ns[3];
351   getNodeGridStructure(ns);
352   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(Build1GTNodalConnectivity(ns,ns+spaceDim));
353   MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> ret(MEDCoupling1SGTUMesh::New(getName(),GetGeoTypeGivenMeshDimension(meshDim)));
354   ret->setNodalConnectivity(conn); ret->setCoords(coords);
355   return ret.retn();
356 }
357
358 /*!
359  * This method returns the unstructured mesh (having single geometric type) of the sub level mesh of \a this.
360  * This method is equivalent to computing MEDCouplingUMesh::buildDescendingConnectivity on the unstructurized \a this mesh.
361  * 
362  * The caller is to delete the returned mesh using decrRef() as it is no more needed. 
363  */
364 MEDCoupling1SGTUMesh *MEDCouplingStructuredMesh::build1SGTSubLevelMesh() const
365 {
366   int meshDim(getMeshDimension());
367   if(meshDim<1 || meshDim>3)
368     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::build1SGTSubLevelMesh : meshdim must be in [2,3] !");
369   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> coords(getCoordinatesAndOwner());
370   int ns[3];
371   getNodeGridStructure(ns);
372   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(Build1GTNodalConnectivityOfSubLevelMesh(ns,ns+meshDim));
373   MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> ret(MEDCoupling1SGTUMesh::New(getName(),GetGeoTypeGivenMeshDimension(meshDim-1)));
374   ret->setNodalConnectivity(conn); ret->setCoords(coords);
375   return ret.retn();
376 }
377
378 /*!
379  * Creates a new unstructured mesh (MEDCouplingUMesh) from \a this structured one.
380  *  \return MEDCouplingUMesh * - a new instance of MEDCouplingUMesh. The caller is to
381  * delete this array using decrRef() as it is no more needed. 
382  *  \throw If \a this->getMeshDimension() is not among [1,2,3].
383  */
384 MEDCouplingUMesh *MEDCouplingStructuredMesh::buildUnstructured() const
385 {
386   MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> ret0(build1SGTUnstructured());
387   return ret0->buildUnstructured();
388 }
389
390 /*!
391  * Creates a new MEDCouplingUMesh containing a part of cells of \a this mesh.
392  * The cells to include to the
393  * result mesh are specified by an array of cell ids.
394  *  \param [in] start - an array of cell ids to include to the result mesh.
395  *  \param [in] end - specifies the end of the array \a start, so that
396  *              the last value of \a start is \a end[ -1 ].
397  *  \return MEDCouplingMesh * - a new instance of MEDCouplingUMesh. The caller is to
398  *         delete this mesh using decrRef() as it is no more needed. 
399  */
400 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPart(const int *start, const int *end) const
401 {
402   MEDCouplingUMesh *um=buildUnstructured();
403   MEDCouplingMesh *ret=um->buildPart(start,end);
404   um->decrRef();
405   return ret;
406 }
407
408 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPartAndReduceNodes(const int *start, const int *end, DataArrayInt*& arr) const
409 {
410   std::vector<int> cgs(getCellGridStructure());
411   std::vector< std::pair<int,int> > cellPartFormat,nodePartFormat;
412   if(IsPartStructured(start,end,cgs,cellPartFormat))
413     {
414       MEDCouplingAutoRefCountObjectPtr<MEDCouplingStructuredMesh> ret(buildStructuredSubPart(cellPartFormat));
415       nodePartFormat=cellPartFormat;
416       for(std::vector< std::pair<int,int> >::iterator it=nodePartFormat.begin();it!=nodePartFormat.end();it++)
417         (*it).second++;
418       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp1(BuildExplicitIdsFrom(getNodeGridStructure(),nodePartFormat));
419       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp2(DataArrayInt::New()); tmp2->alloc(getNumberOfNodes(),1);
420       tmp2->fillWithValue(-1);
421       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp3(DataArrayInt::New()); tmp3->alloc(tmp1->getNumberOfTuples(),1); tmp3->iota(0);
422       tmp2->setPartOfValues3(tmp3,tmp1->begin(),tmp1->end(),0,1,1);
423       arr=tmp2.retn();
424       return ret.retn();
425     }
426   else
427     {
428       MEDCouplingUMesh *um=buildUnstructured();
429       MEDCouplingMesh *ret=um->buildPartAndReduceNodes(start,end,arr);
430       um->decrRef();
431       return ret;
432     }
433 }
434
435 DataArrayInt *MEDCouplingStructuredMesh::simplexize(int policy)
436 {
437   throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::simplexize : not available for Cartesian mesh !");
438 }
439
440 /*!
441  * Returns a new MEDCouplingFieldDouble holding normal vectors to cells of \a this
442  * 2D mesh. The computed vectors have 3 components and are normalized.
443  *  \return MEDCouplingFieldDouble * - a new instance of MEDCouplingFieldDouble on
444  *          cells and one time. The caller is to delete this field using decrRef() as
445  *          it is no more needed.
446  *  \throw If \a this->getMeshDimension() != 2.
447  */
448 MEDCouplingFieldDouble *MEDCouplingStructuredMesh::buildOrthogonalField() const
449 {
450   if(getMeshDimension()!=2)
451     throw INTERP_KERNEL::Exception("Expected a MEDCouplingStructuredMesh with meshDim == 2 !");
452   MEDCouplingFieldDouble *ret=MEDCouplingFieldDouble::New(ON_CELLS,NO_TIME);
453   DataArrayDouble *array=DataArrayDouble::New();
454   int nbOfCells=getNumberOfCells();
455   array->alloc(nbOfCells,3);
456   double *vals=array->getPointer();
457   for(int i=0;i<nbOfCells;i++)
458     { vals[3*i]=0.; vals[3*i+1]=0.; vals[3*i+2]=1.; }
459   ret->setArray(array);
460   array->decrRef();
461   ret->setMesh(this);
462   return ret;
463 }
464
465 void MEDCouplingStructuredMesh::getReverseNodalConnectivity(DataArrayInt *revNodal, DataArrayInt *revNodalIndx) const
466 {
467   std::vector<int> ngs(getNodeGridStructure());
468   int dim(getSpaceDimension());
469   switch(dim)
470   {
471     case 1:
472       return GetReverseNodalConnectivity1(ngs,revNodal,revNodalIndx);
473     case 2:
474       return GetReverseNodalConnectivity2(ngs,revNodal,revNodalIndx);
475     case 3:
476       return GetReverseNodalConnectivity3(ngs,revNodal,revNodalIndx);
477     default:
478       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::getReverseNodalConnectivity : only dimensions 1, 2 and 3 are supported !");
479   }
480 }
481
482 void MEDCouplingStructuredMesh::GetReverseNodalConnectivity1(const std::vector<int>& ngs, DataArrayInt *revNodal, DataArrayInt *revNodalIndx)
483 {
484   int nbNodes(ngs[0]);
485   revNodalIndx->alloc(nbNodes+1,1);
486   if(nbNodes==0)
487     { revNodal->alloc(0,1); revNodalIndx->setIJ(0,0,0); return ; }
488   if(nbNodes==1)
489     { revNodal->alloc(1,1); revNodal->setIJ(0,0,0); revNodalIndx->setIJ(0,0,0); revNodalIndx->setIJ(1,0,1); return ; }
490   revNodal->alloc(2*(nbNodes-1),1);
491   int *rn(revNodal->getPointer()),*rni(revNodalIndx->getPointer());
492   *rni++=0; *rni=1; *rn++=0;
493   for(int i=1;i<nbNodes-1;i++,rni++)
494     {
495       rn[0]=i-1; rn[1]=i;
496       rni[1]=rni[0]+2;
497       rn+=2;
498     }
499   rn[0]=nbNodes-2; rni[1]=rni[0]+1;
500 }
501
502 void MEDCouplingStructuredMesh::GetReverseNodalConnectivity2(const std::vector<int>& ngs, DataArrayInt *revNodal, DataArrayInt *revNodalIndx)
503 {
504   int nbNodesX(ngs[0]),nbNodesY(ngs[1]);
505   int nbNodes(nbNodesX*nbNodesY);
506   if(nbNodesX==0 || nbNodesY==0)
507     { revNodal->alloc(0,1); revNodalIndx->setIJ(0,0,0); return ; }
508   if(nbNodesX==1 || nbNodesY==1)
509     { std::vector<int> ngs2(1); ngs2[0]=std::max(nbNodesX,nbNodesY); return GetReverseNodalConnectivity1(ngs2,revNodal,revNodalIndx); }
510   revNodalIndx->alloc(nbNodes+1,1);
511   int nbCellsX(nbNodesX-1),nbCellsY(nbNodesY-1);
512   revNodal->alloc(4*(nbNodesX-2)*(nbNodesY-2)+2*2*(nbNodesX-2)+2*2*(nbNodesY-2)+4,1);
513   int *rn(revNodal->getPointer()),*rni(revNodalIndx->getPointer());
514   *rni++=0; *rni=1; *rn++=0;
515   for(int i=1;i<nbNodesX-1;i++,rni++,rn+=2)
516     {
517       rn[0]=i-1; rn[1]=i;
518       rni[1]=rni[0]+2;
519     }
520   rni[1]=rni[0]+1; *rn++=nbCellsX-1;
521   rni++;
522   for(int j=1;j<nbNodesY-1;j++)
523     {
524       int off(nbCellsX*(j-1)),off2(nbCellsX*j);
525       rni[1]=rni[0]+2; rn[0]=off; rn[1]=off2;
526       rni++; rn+=2;
527       for(int i=1;i<nbNodesX-1;i++,rni++,rn+=4)
528         {
529           rn[0]=i-1+off; rn[1]=i+off; rn[2]=i-1+off2; rn[3]=i+off2;
530           rni[1]=rni[0]+4;
531         }
532       rni[1]=rni[0]+2; rn[0]=off+nbCellsX-1; rn[1]=off2+nbCellsX-1;
533       rni++; rn+=2;
534     }
535   int off3(nbCellsX*(nbCellsY-1));
536   rni[1]=rni[0]+1;
537   rni++; *rn++=off3;
538   for(int i=1;i<nbNodesX-1;i++,rni++,rn+=2)
539     {
540       rn[0]=i-1+off3; rn[1]=i+off3;
541       rni[1]=rni[0]+2;
542     }
543   rni[1]=rni[0]+1; rn[0]=nbCellsX*nbCellsY-1;
544 }
545
546 void MEDCouplingStructuredMesh::GetReverseNodalConnectivity3(const std::vector<int>& ngs, DataArrayInt *revNodal, DataArrayInt *revNodalIndx)
547 {
548   int nbNodesX(ngs[0]),nbNodesY(ngs[1]),nbNodesZ(ngs[2]);
549   int nbNodes(nbNodesX*nbNodesY*nbNodesZ);
550   if(nbNodesX==0 || nbNodesY==0 || nbNodesZ==0)
551     { revNodal->alloc(0,1); revNodalIndx->setIJ(0,0,0); return ; }
552   if(nbNodesX==1 || nbNodesY==1 || nbNodesZ==1)
553     {
554       std::vector<int> ngs2(2);
555       int pos(0);
556       bool pass(false);
557       for(int i=0;i<3;i++)
558         {
559           if(pass)
560             { ngs2[pos++]=ngs[i]; }
561           else
562             {
563               pass=ngs[i]==1;
564               if(!pass)
565                 { ngs2[pos++]=ngs[i]; }
566             }
567         }
568       return GetReverseNodalConnectivity2(ngs2,revNodal,revNodalIndx);
569     }
570   revNodalIndx->alloc(nbNodes+1,1);
571   int nbCellsX(nbNodesX-1),nbCellsY(nbNodesY-1),nbCellsZ(nbNodesZ-1);
572   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);
573   int *rn(revNodal->getPointer()),*rni(revNodalIndx->getPointer());
574   *rni=0;
575   for(int k=0;k<nbNodesZ;k++)
576     {
577       bool factZ(k!=0 && k!=nbNodesZ-1);
578       int offZ0((k-1)*nbCellsX*nbCellsY),offZ1(k*nbCellsX*nbCellsY);
579       for(int j=0;j<nbNodesY;j++)
580         {
581           bool factYZ(factZ && (j!=0 && j!=nbNodesY-1));
582           int off00((j-1)*nbCellsX+offZ0),off01(j*nbCellsX+offZ0),off10((j-1)*nbCellsX+offZ1),off11(j*nbCellsX+offZ1);
583           for(int i=0;i<nbNodesX;i++,rni++)
584             {
585               int fact(factYZ && (i!=0 && i!=nbNodesX-1));
586               if(fact)
587                 {//most of points fall in this part of code
588                   rn[0]=off00+i-1; rn[1]=off00+i; rn[2]=off01+i-1; rn[3]=off01+i;
589                   rn[4]=off10+i-1; rn[5]=off10+i; rn[6]=off11+i-1; rn[7]=off11+i;
590                   rni[1]=rni[0]+8;
591                   rn+=8;
592                 }
593               else
594                 {
595                   int *rnRef(rn);
596                   if(k>=1 && j>=1 && i>=1)
597                     *rn++=off00+i-1;
598                   if(k>=1 && j>=1 && i<nbCellsX)
599                     *rn++=off00+i;
600                   if(k>=1 && j<nbCellsY && i>=1)
601                     *rn++=off01+i-1;
602                   if(k>=1 && j<nbCellsY && i<nbCellsX)
603                     *rn++=off01+i;
604                   //
605                   if(k<nbCellsZ && j>=1 && i>=1)
606                     *rn++=off10+i-1;
607                   if(k<nbCellsZ && j>=1 && i<nbCellsX)
608                     *rn++=off10+i;
609                   if(k<nbCellsZ && j<nbCellsY && i>=1)
610                     *rn++=off11+i-1;
611                   if(k<nbCellsZ && j<nbCellsY && i<nbCellsX)
612                     *rn++=off11+i;
613                   rni[1]=rni[0]+(int)(std::distance(rnRef,rn));
614                 }
615             }
616         }
617     }
618 }
619
620 /*!
621  * \return DataArrayInt * - newly allocated instance of nodal connectivity compatible for MEDCoupling1SGTMesh instance
622  */
623 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity(const int *nodeStBg, const int *nodeStEnd)
624 {
625   int zippedNodeSt[3];
626   int dim(ZipNodeStructure(nodeStBg,nodeStEnd,zippedNodeSt));
627   switch(dim)
628   {
629     case 0:
630       {
631         MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
632         conn->alloc(1,1); conn->setIJ(0,0,0);
633         return conn.retn();
634       }
635     case 1:
636       return Build1GTNodalConnectivity1D(zippedNodeSt);
637     case 2:
638       return Build1GTNodalConnectivity2D(zippedNodeSt);
639     case 3:
640       return Build1GTNodalConnectivity3D(zippedNodeSt);
641     default:
642       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::Build1GTNodalConnectivity : only dimension in [0,1,2,3] supported !");
643   }
644 }
645
646 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivityOfSubLevelMesh(const int *nodeStBg, const int *nodeStEnd)
647 {
648   std::size_t dim(std::distance(nodeStBg,nodeStEnd));
649   switch(dim)
650   {
651     case 3:
652       return Build1GTNodalConnectivityOfSubLevelMesh3D(nodeStBg);
653     case 2:
654       return Build1GTNodalConnectivityOfSubLevelMesh2D(nodeStBg);
655     default:
656       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::Build1GTNodalConnectivityOfSubLevelMesh: only dimension in [2,3] supported !");
657   }
658 }
659
660 /*!
661  * This method retrieves the number of entities (it can be cells or nodes) given a range in compact standard format
662  * used in methods like BuildExplicitIdsFrom,IsPartStructured.
663  *
664  * \sa BuildExplicitIdsFrom,IsPartStructured
665  */
666 int MEDCouplingStructuredMesh::DeduceNumberOfGivenRangeInCompactFrmt(const std::vector< std::pair<int,int> >& partCompactFormat)
667 {
668   int ret(1);
669   bool isFetched(false);
670   std::size_t ii(0);
671   for(std::vector< std::pair<int,int> >::const_iterator it=partCompactFormat.begin();it!=partCompactFormat.end();it++,ii++)
672     {
673       int a((*it).first),b((*it).second);
674       if(a<0 || b<0 || b-a<0)
675         {
676           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::DeduceNumberOfGivenRangeInCompactFrmt : invalid input at dimension " << ii << " !";
677           throw INTERP_KERNEL::Exception(oss.str().c_str());
678         }
679       if(b-a>0)
680         {
681           isFetched=true;
682           ret*=(b-a);
683         }
684     }
685   return isFetched?ret:0;
686 }
687
688 int MEDCouplingStructuredMesh::DeduceNumberOfGivenStructure(const std::vector<int>& st)
689 {
690   int ret(1);
691   bool isFetched(false);
692   for(std::size_t i=0;i<st.size();i++)
693     {
694       if(st[i]<0)
695         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::DeduceNumberOfGivenStructure : presence of a negative value in structure !");
696       ret*=st[i];
697       isFetched=true;
698     }
699   return isFetched?ret:0;
700 }
701
702 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity1D(const int *nodeStBg)
703 {
704   int nbOfCells(*nodeStBg-1);
705   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
706   conn->alloc(2*nbOfCells,1);
707   int *cp=conn->getPointer();
708   for(int i=0;i<nbOfCells;i++)
709     {
710       cp[2*i+0]=i;
711       cp[2*i+1]=i+1;
712     }
713   return conn.retn();
714 }
715
716 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity2D(const int *nodeStBg)
717 {
718   int n1=nodeStBg[0]-1;
719   int n2=nodeStBg[1]-1;
720   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
721   conn->alloc(4*n1*n2,1);
722   int *cp=conn->getPointer();
723   int pos=0;
724   for(int j=0;j<n2;j++)
725     for(int i=0;i<n1;i++,pos++)
726       {
727         cp[4*pos+0]=i+1+j*(n1+1);
728         cp[4*pos+1]=i+j*(n1+1);
729         cp[4*pos+2]=i+(j+1)*(n1+1);
730         cp[4*pos+3]=i+1+(j+1)*(n1+1);
731       }
732   return conn.retn();
733 }
734
735 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity3D(const int *nodeStBg)
736 {
737   int n1=nodeStBg[0]-1;
738   int n2=nodeStBg[1]-1;
739   int n3=nodeStBg[2]-1;
740   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
741   conn->alloc(8*n1*n2*n3,1);
742   int *cp=conn->getPointer();
743   int pos=0;
744   for(int k=0;k<n3;k++)
745     for(int j=0;j<n2;j++)
746       for(int i=0;i<n1;i++,pos++)
747         {
748           int tmp=(n1+1)*(n2+1);
749           cp[8*pos+0]=i+1+j*(n1+1)+k*tmp;
750           cp[8*pos+1]=i+j*(n1+1)+k*tmp;
751           cp[8*pos+2]=i+(j+1)*(n1+1)+k*tmp;
752           cp[8*pos+3]=i+1+(j+1)*(n1+1)+k*tmp;
753           cp[8*pos+4]=i+1+j*(n1+1)+(k+1)*tmp;
754           cp[8*pos+5]=i+j*(n1+1)+(k+1)*tmp;
755           cp[8*pos+6]=i+(j+1)*(n1+1)+(k+1)*tmp;
756           cp[8*pos+7]=i+1+(j+1)*(n1+1)+(k+1)*tmp;
757         }
758   return conn.retn();
759 }
760
761 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivityOfSubLevelMesh3D(const int *nodeStBg)
762 {
763   std::vector<int> ngs(3);
764   int n0(nodeStBg[0]-1),n1(nodeStBg[1]-1),n2(nodeStBg[2]-1); ngs[0]=n0; ngs[1]=n1; ngs[2]=n2;
765   int off0(nodeStBg[0]),off1(nodeStBg[0]*nodeStBg[1]);
766   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
767   conn->alloc(4*GetNumberOfCellsOfSubLevelMesh(ngs,3));
768   int *cp(conn->getPointer());
769   //X
770   for(int i=0;i<nodeStBg[0];i++)
771     for(int j=0;j<n1;j++)
772       for(int k=0;k<n2;k++,cp+=4)
773         { 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; }
774   //Y
775   for(int j=0;j<nodeStBg[1];j++)
776     for(int i=0;i<n0;i++)
777       for(int k=0;k<n2;k++,cp+=4)
778         { 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); }
779   //Z
780   for(int k=0;k<nodeStBg[2];k++)
781     for(int i=0;i<n0;i++)
782       for(int j=0;j<n1;j++,cp+=4)
783         { 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; }
784   return conn.retn();
785 }
786
787 /*!
788  * This method computes given the nodal structure defined by [ \a nodeStBg , \a nodeStEnd ) the zipped form.
789  * std::distance( \a nodeStBg, \a nodeStEnd ) is equal to the space dimension. The returned value is equal to
790  * the meshDimension (or the zipped spaceDimension).
791  *
792  * \param [out] zipNodeSt - The zipped node strucutre
793  * \return int - the
794  */
795 int MEDCouplingStructuredMesh::ZipNodeStructure(const int *nodeStBg, const int *nodeStEnd, int zipNodeSt[3])
796 {
797   int spaceDim((int)std::distance(nodeStBg,nodeStEnd));
798   if(spaceDim>3 || spaceDim<1)
799     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::ZipNodeStructure : spaceDim must in [1,2,3] !");
800   zipNodeSt[0]=0; zipNodeSt[1]=0; zipNodeSt[2]=0;
801   int zippedI(0);
802   for(int i=0;i<spaceDim;i++)
803     {
804       int elt(nodeStBg[i]);
805       if(elt<1)
806         {
807           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::ZipNodeStructure : the input nodal structure at pos#" << i << "(" << nodeStBg[i] << ") is invalid !";
808           throw INTERP_KERNEL::Exception(oss.str().c_str());
809         }
810       if(elt>=2)
811         zipNodeSt[zippedI++]=elt;
812     }
813   return zippedI;
814 }
815
816 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivityOfSubLevelMesh2D(const int *nodeStBg)
817 {
818   std::vector<int> ngs(2);
819   int n0(nodeStBg[0]-1),n1(nodeStBg[1]-1); ngs[0]=n0; ngs[1]=n1;
820   int off0(nodeStBg[0]);
821   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
822   conn->alloc(2*GetNumberOfCellsOfSubLevelMesh(ngs,2));
823   int *cp(conn->getPointer());
824   //X
825   for(int i=0;i<nodeStBg[0];i++)
826     for(int j=0;j<n1;j++,cp+=2)
827       { cp[0]=j*off0+i; cp[1]=(j+1)*off0+i; }
828   //Y
829   for(int j=0;j<nodeStBg[1];j++)
830     for(int i=0;i<n0;i++,cp+=2)
831       { cp[0]=j*off0+i; cp[1]=j*off0+(i+1); }
832   return conn.retn();
833 }
834
835 /*!
836  * Returns a cell id by its (i,j,k) index. The cell is located between the i-th and
837  * ( i + 1 )-th nodes along X axis etc.
838  *  \param [in] i - a index of node coordinates array along X axis.
839  *  \param [in] j - a index of node coordinates array along Y axis.
840  *  \param [in] k - a index of node coordinates array along Z axis.
841  *  \return int - a cell id in \a this mesh.
842  */
843 int MEDCouplingStructuredMesh::getCellIdFromPos(int i, int j, int k) const
844 {
845   int tmp[3]={i,j,k};
846   int tmp2[3];
847   int meshDim(getMeshDimension());
848   getSplitCellValues(tmp2);
849   std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
850   return std::accumulate(tmp,tmp+meshDim,0);
851 }
852
853 /*!
854  * Returns a node id by its (i,j,k) index.
855  *  \param [in] i - a index of node coordinates array along X axis.
856  *  \param [in] j - a index of node coordinates array along Y axis.
857  *  \param [in] k - a index of node coordinates array along Z axis.
858  *  \return int - a node id in \a this mesh.
859  */
860 int MEDCouplingStructuredMesh::getNodeIdFromPos(int i, int j, int k) const
861 {
862   int tmp[3]={i,j,k};
863   int tmp2[3];
864   int spaceDim(getSpaceDimension());
865   getSplitNodeValues(tmp2);
866   std::transform(tmp,tmp+spaceDim,tmp2,tmp,std::multiplies<int>());
867   return std::accumulate(tmp,tmp+spaceDim,0);
868 }
869
870
871 int MEDCouplingStructuredMesh::getNumberOfCells() const
872 {
873   std::vector<int> ngs(getNodeGridStructure());
874   int ret(1);
875   bool isCatched(false);
876   std::size_t ii(0);
877   for(std::vector<int>::const_iterator it=ngs.begin();it!=ngs.end();it++,ii++)
878     {
879       int elt(*it);
880       if(elt<=0)
881         {
882           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::getNumberOfCells : at pos #" << ii << " the number of nodes in nodeStructure is " << *it << " ! Must be > 0 !";
883           throw INTERP_KERNEL::Exception(oss.str().c_str());
884         }
885       if(elt>1)
886         {
887           ret*=elt-1;
888           isCatched=true;
889         }
890     }
891   return isCatched?ret:0;
892 }
893
894 int MEDCouplingStructuredMesh::getNumberOfNodes() const
895 {
896   std::vector<int> ngs(getNodeGridStructure());
897   int ret(1);
898   for(std::vector<int>::const_iterator it=ngs.begin();it!=ngs.end();it++)
899     ret*=*it;
900   return ret;
901 }
902
903 void MEDCouplingStructuredMesh::GetPosFromId(int nodeId, int meshDim, const int *split, int *res)
904 {
905   int work=nodeId;
906   for(int i=meshDim-1;i>=0;i--)
907     {
908       int pos=work/split[i];
909       work=work%split[i];
910       res[i]=pos;
911     }
912 }
913
914 std::vector<int> MEDCouplingStructuredMesh::getCellGridStructure() const
915 {
916   std::vector<int> ret(getNodeGridStructure());
917   std::transform(ret.begin(),ret.end(),ret.begin(),std::bind2nd(std::plus<int>(),-1));
918   return ret;
919 }
920
921 /*!
922  * Given a struct \a strct it returns a split vector [1,strct[0],strct[0]*strct[1]...]
923  * This decomposition allows to quickly find i,j,k given a global id.
924  */
925 std::vector<int> MEDCouplingStructuredMesh::GetSplitVectFromStruct(const std::vector<int>& strct)
926 {
927   int spaceDim((int)strct.size());
928   std::vector<int> res(spaceDim);
929   for(int l=0;l<spaceDim;l++)
930     {
931       int val=1;
932       for(int p=0;p<spaceDim-l-1;p++)
933         val*=strct[p];
934       res[spaceDim-l-1]=val;
935     }
936   return res;
937 }
938
939 /*!
940  * 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.
941  * If true is returned \a partCompactFormat will contain the information to build the corresponding part.
942  *
943  * \sa MEDCouplingStructuredMesh::BuildExplicitIdsFrom, MEDCouplingStructuredMesh::DeduceNumberOfGivenRangeInCompactFrmt
944  */
945 bool MEDCouplingStructuredMesh::IsPartStructured(const int *startIds, const int *stopIds, const std::vector<int>& st, std::vector< std::pair<int,int> >& partCompactFormat)
946 {
947   int dim((int)st.size());
948   partCompactFormat.resize(dim);
949   if(dim<1 || dim>3)
950     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::isPartStructured : input structure must be of dimension in [1,2,3] !");
951   std::vector<int> tmp2(dim),tmp(dim),tmp3(dim),tmp4(dim); tmp2[0]=1;
952   for(int i=1;i<dim;i++)
953     tmp2[i]=tmp2[i-1]*st[i-1];
954   std::size_t sz(std::distance(startIds,stopIds));
955   if(sz==0)
956     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : empty input !");
957   GetPosFromId(*startIds,dim,&tmp2[0],&tmp[0]);
958   partCompactFormat.resize(dim);
959   for(int i=0;i<dim;i++)
960     partCompactFormat[i].first=tmp[i];
961   if(tmp[dim-1]<0 || tmp[dim-1]>=st[dim-1])
962     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : first id in input is not in valid range !");
963   if(sz==1)
964     {
965       for(int i=0;i<dim;i++)
966         partCompactFormat[i].second=tmp[i]+1;
967       return true;
968     }
969   GetPosFromId(startIds[sz-1],dim,&tmp2[0],&tmp3[0]);
970   int szExp(1);
971   for(int i=0;i<dim;i++)
972     {
973       if(tmp3[i]<0 || tmp3[i]>=st[i])
974         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : last id in input is not in valid range !");
975       partCompactFormat[i].second=tmp3[i]+1;
976       tmp4[i]=partCompactFormat[i].second-partCompactFormat[i].first;
977       if(tmp4[i]<=0)
978         return false;
979       szExp*=tmp4[i];
980     }
981   if(szExp!=(int)sz)
982     return false;
983   const int *w(startIds);
984   switch(dim)
985   {
986     case 3:
987       {
988         for(int i=0;i<tmp4[2];i++)
989           {
990             int a=tmp2[2]*(partCompactFormat[2].first+i);
991             for(int j=0;j<tmp4[1];j++)
992               {
993                 int b=tmp2[1]*(partCompactFormat[1].first+j);
994                 for(int k=0;k<tmp4[0];k++,w++)
995                   {
996                     if(partCompactFormat[0].first+k+b+a!=*w)
997                       return false;
998                   }
999               }
1000           }
1001         return true;
1002       }
1003     case 2:
1004       {
1005         for(int j=0;j<tmp4[1];j++)
1006           {
1007             int b=tmp2[1]*(partCompactFormat[1].first+j);
1008             for(int k=0;k<tmp4[0];k++,w++)
1009               {
1010                 if(partCompactFormat[0].first+k+b!=*w)
1011                   return false;
1012               }
1013           }
1014         return true;
1015       }
1016     case 1:
1017       {
1018         for(int k=0;k<tmp4[0];k++,w++)
1019           {
1020             if(partCompactFormat[0].first+k!=*w)
1021               return false;
1022           }
1023         return true;
1024       }
1025     default:
1026       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : internal error !");
1027   }
1028 }
1029
1030 std::vector<int> MEDCouplingStructuredMesh::GetDimensionsFromCompactFrmt(const std::vector< std::pair<int,int> >& partCompactFormat)
1031 {
1032   std::vector<int> ret(partCompactFormat.size());
1033   for(std::size_t i=0;i<partCompactFormat.size();i++)
1034     ret[i]=partCompactFormat[i].second-partCompactFormat[i].first;
1035   return ret;
1036 }
1037
1038 /*!
1039  * This method is close to BuildExplicitIdsFrom except that instead of returning a DataArrayInt instance containing explicit ids it
1040  * enable elems in the vector of booleans (for performance reasons). As it is method for performance, this method is \b not
1041  * available in python.
1042  *
1043  * \param [in] st The entity structure.
1044  * \param [in] partCompactFormat The compact subpart to be enabled.
1045  * \param [in,out] vectToSwitchOn Vector which fetched items are enabled.
1046  *
1047  * \sa MEDCouplingStructuredMesh::BuildExplicitIdsFrom
1048  */
1049 void MEDCouplingStructuredMesh::SwitchOnIdsFrom(const std::vector<int>& st, const std::vector< std::pair<int,int> >& partCompactFormat, std::vector<bool>& vectToSwitchOn)
1050 {
1051   if(st.size()!=partCompactFormat.size())
1052     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::SwitchOnIdsFrom : input arrays must have the same size !");
1053   if((int)vectToSwitchOn.size()!=DeduceNumberOfGivenStructure(st))
1054     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::SwitchOnIdsFrom : invalid size of input vector of boolean regarding the structure !");
1055   std::vector<int> dims(GetDimensionsFromCompactFrmt(partCompactFormat));
1056   switch(st.size())
1057   {
1058     case 3:
1059       {
1060         for(int i=0;i<dims[2];i++)
1061           {
1062             int a=(partCompactFormat[2].first+i)*st[0]*st[1];
1063             for(int j=0;j<dims[1];j++)
1064               {
1065                 int b=(partCompactFormat[1].first+j)*st[0];
1066                 for(int k=0;k<dims[0];k++)
1067                   vectToSwitchOn[partCompactFormat[0].first+k+b+a]=true;
1068               }
1069           }
1070         break;
1071       }
1072     case 2:
1073       {
1074         for(int j=0;j<dims[1];j++)
1075           {
1076             int b=(partCompactFormat[1].first+j)*st[0];
1077             for(int k=0;k<dims[0];k++)
1078               vectToSwitchOn[partCompactFormat[0].first+k+b]=true;
1079           }
1080         break;
1081       }
1082     case 1:
1083       {
1084         for(int k=0;k<dims[0];k++)
1085           vectToSwitchOn[partCompactFormat[0].first+k]=true;
1086         break;
1087       }
1088     default:
1089       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : Dimension supported are 1,2 or 3 !");
1090   }
1091 }
1092
1093 /*!
1094  * This method builds the explicit entity array from the structure in \a st and the range in \a partCompactFormat.
1095  * If the range contains invalid values regarding sructure an exception will be thrown.
1096  *
1097  * \return DataArrayInt * - a new object.
1098  * \sa MEDCouplingStructuredMesh::IsPartStructured, MEDCouplingStructuredMesh::DeduceNumberOfGivenRangeInCompactFrmt, SwitchOnIdsFrom
1099  */
1100 DataArrayInt *MEDCouplingStructuredMesh::BuildExplicitIdsFrom(const std::vector<int>& st, const std::vector< std::pair<int,int> >& partCompactFormat)
1101 {
1102   if(st.size()!=partCompactFormat.size())
1103     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : input arrays must have the same size !");
1104   int nbOfItems(1);
1105   std::vector<int> dims(st.size());
1106   for(std::size_t i=0;i<st.size();i++)
1107     {
1108       if(partCompactFormat[i].first<0 || partCompactFormat[i].first>st[i])
1109         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : invalid input range 1 !");
1110       if(partCompactFormat[i].second<0 || partCompactFormat[i].second>st[i])
1111         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : invalid input range 2 !");
1112       if(partCompactFormat[i].second<=partCompactFormat[i].first)
1113         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : invalid input range 3 !");
1114       dims[i]=partCompactFormat[i].second-partCompactFormat[i].first;
1115       nbOfItems*=dims[i];
1116     }
1117   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New());
1118   ret->alloc(nbOfItems,1);
1119   int *pt(ret->getPointer());
1120   switch(st.size())
1121   {
1122     case 3:
1123       {
1124         for(int i=0;i<dims[2];i++)
1125           {
1126             int a=(partCompactFormat[2].first+i)*st[0]*st[1];
1127             for(int j=0;j<dims[1];j++)
1128               {
1129                 int b=(partCompactFormat[1].first+j)*st[0];
1130                 for(int k=0;k<dims[0];k++,pt++)
1131                   *pt=partCompactFormat[0].first+k+b+a;
1132               }
1133           }
1134         break;
1135       }
1136     case 2:
1137       {
1138         for(int j=0;j<dims[1];j++)
1139           {
1140             int b=(partCompactFormat[1].first+j)*st[0];
1141             for(int k=0;k<dims[0];k++,pt++)
1142               *pt=partCompactFormat[0].first+k+b;
1143           }
1144         break;
1145       }
1146     case 1:
1147       {
1148         for(int k=0;k<dims[0];k++,pt++)
1149           *pt=partCompactFormat[0].first+k;
1150         break;
1151       }
1152     default:
1153       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : Dimension supported are 1,2 or 3 !");
1154   }
1155   return ret.retn();
1156 }
1157
1158 int MEDCouplingStructuredMesh::GetNumberOfCellsOfSubLevelMesh(const std::vector<int>& cgs, int mdim)
1159 {
1160   int ret(0);
1161   for(int i=0;i<mdim;i++)
1162     {
1163       int locRet(1);
1164       for(int j=0;j<mdim;j++)
1165         if(j!=i)
1166           locRet*=cgs[j];
1167         else
1168           locRet*=cgs[j]+1;
1169       ret+=locRet;
1170     }
1171   return ret;
1172 }