]> SALOME platform Git repositories - tools/medcoupling.git/blob - src/MEDCoupling/MEDCouplingStructuredMesh.cxx
Salome HOME
+ Field::cellToNode implementation
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingStructuredMesh.cxx
1 // Copyright (C) 2007-2013  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.
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     default:
74       throw INTERP_KERNEL::Exception("Unexpected dimension for MEDCouplingStructuredMesh::GetGeoTypeGivenMeshDimension !");
75     }
76 }
77
78 std::set<INTERP_KERNEL::NormalizedCellType> MEDCouplingStructuredMesh::getAllGeoTypes() const
79 {
80   std::set<INTERP_KERNEL::NormalizedCellType> ret2;
81   ret2.insert(getTypeOfCell(0));
82   return ret2;
83 }
84
85 int MEDCouplingStructuredMesh::getNumberOfCellsWithType(INTERP_KERNEL::NormalizedCellType type) const
86 {
87   int ret=getNumberOfCells();
88   if(type==getTypeOfCell(0))
89     return ret;
90   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
91   std::ostringstream oss; oss << "MEDCouplingStructuredMesh::getNumberOfCellsWithType : no specified type ! Type available is " << cm.getRepr() << " !";
92   throw INTERP_KERNEL::Exception(oss.str().c_str());
93 }
94
95 DataArrayInt *MEDCouplingStructuredMesh::giveCellsWithType(INTERP_KERNEL::NormalizedCellType type) const
96 {
97   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
98   if(getTypeOfCell(0)==type)
99     {
100       ret->alloc(getNumberOfCells(),1);
101       ret->iota(0);
102     }
103   else
104     ret->alloc(0,1);
105   return ret.retn();
106 }
107
108 DataArrayInt *MEDCouplingStructuredMesh::computeNbOfNodesPerCell() const
109 {
110   int nbCells=getNumberOfCells();
111   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
112   ret->alloc(nbCells,1);
113   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
114   ret->fillWithValue((int)cm.getNumberOfNodes());
115   return ret.retn();
116 }
117
118 DataArrayInt *MEDCouplingStructuredMesh::computeNbOfFacesPerCell() const
119 {
120   int nbCells=getNumberOfCells();
121   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
122   ret->alloc(nbCells,1);
123   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
124   ret->fillWithValue((int)cm.getNumberOfSons());
125   return ret.retn();
126 }
127
128 /*!
129  * This method computes effective number of nodes per cell. That is to say nodes appearing several times in nodal connectivity of a cell,
130  * will be counted only once here whereas it will be counted several times in MEDCouplingMesh::computeNbOfNodesPerCell method.
131  * Here for structured mesh it returns exactly as MEDCouplingStructuredMesh::computeNbOfNodesPerCell does.
132  *
133  * \return DataArrayInt * - new object to be deallocated by the caller.
134  */
135 DataArrayInt *MEDCouplingStructuredMesh::computeEffectiveNbOfNodesPerCell() const
136 {
137   return computeNbOfNodesPerCell();
138 }
139
140 void MEDCouplingStructuredMesh::getNodeIdsOfCell(int cellId, std::vector<int>& conn) const
141 {
142   int meshDim=getMeshDimension();
143   int tmpCell[3],tmpNode[3];
144   getSplitCellValues(tmpCell);
145   getSplitNodeValues(tmpNode);
146   int tmp2[3];
147   GetPosFromId(cellId,meshDim,tmpCell,tmp2);
148   switch(meshDim)
149     {
150     case 1:
151       conn.push_back(tmp2[0]); conn.push_back(tmp2[0]+1);
152       break;
153     case 2:
154       conn.push_back(tmp2[1]*tmpNode[1]+tmp2[0]); conn.push_back(tmp2[1]*tmpNode[1]+tmp2[0]+1);
155       conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]+1); conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]);
156       break;
157     case 3:
158       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]);
159       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]);
160       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]);
161       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]);
162       break;
163     default:
164       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::getNodeIdsOfCell : big problem spacedim must be in 1,2 or 3 !");
165     };
166 }
167
168 /*!
169  * See MEDCouplingUMesh::getDistributionOfTypes for more information
170  */
171 std::vector<int> MEDCouplingStructuredMesh::getDistributionOfTypes() const
172 {
173   //only one type of cell
174   std::vector<int> ret(3);
175   ret[0]=getTypeOfCell(0);
176   ret[1]=getNumberOfCells();
177   ret[2]=-1; //ret[3*k+2]==-1 because it has no sense here
178   return ret;
179 }
180
181 /*!
182  * This method tries to minimize at most the number of deep copy.
183  * So if \a idsPerType is not empty it can be returned directly (without copy, but with ref count incremented) in return.
184  * 
185  * See MEDCouplingUMesh::checkTypeConsistencyAndContig for more information
186  */
187 DataArrayInt *MEDCouplingStructuredMesh::checkTypeConsistencyAndContig(const std::vector<int>& code, const std::vector<const DataArrayInt *>& idsPerType) const
188 {
189   int nbOfCells=getNumberOfCells();
190   if(code.size()!=3)
191     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : invalid input code should be exactly of size 3 !");
192   if(code[0]!=(int)getTypeOfCell(0))
193     {
194       std::ostringstream oss; oss << "MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : Mismatch of geometric type ! Asking for " << code[0] << " whereas the geometric type is \a this is " << getTypeOfCell(0) << " !";
195       throw INTERP_KERNEL::Exception(oss.str().c_str());
196     }
197   if(code[2]==-1)
198     {
199       if(code[1]==nbOfCells)
200         return 0;
201       else
202         {
203           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : mismatch between the number of cells in this (" << nbOfCells << ") and the number of non profile (" << code[1] << ") !";
204           throw INTERP_KERNEL::Exception(oss.str().c_str());
205         }
206     }
207   if(code[2]!=0)
208     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : single geo type mesh ! 0 or -1 is expected at pos #2 of input code !");
209   if(idsPerType.size()!=1)
210     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : input code points to DataArrayInt #0 whereas the size of idsPerType is not equal to 1 !");
211   const DataArrayInt *pfl=idsPerType[0];
212   if(!pfl)
213     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : the input code points to a NULL DataArrayInt at rank 0 !");
214   if(pfl->getNumberOfComponents()!=1)
215     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : input profile should have exactly one component !");
216   pfl->checkAllIdsInRange(0,nbOfCells);
217   pfl->incrRef();
218   return const_cast<DataArrayInt *>(pfl);
219 }
220
221 /*!
222  * 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.
223  * 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.
224  * This method has 1 input \a profile and 3 outputs \a code \a idsInPflPerType and \a idsPerType.
225  * 
226  * \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.
227  * \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,
228  *              \a idsInPflPerType[i] stores the tuple ids in \a profile that correspond to the geometric type code[3*i+0]
229  * \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.
230  *              This vector can be empty in case of all geometric type cells are fully covered in ascending in the given input \a profile.
231  * 
232  * \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.
233  *
234  * \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
235  *
236  *  \b Example1: <br>
237  *          - Before \a this has 3 cells \a profile contains [0,1,2]
238  *          - After \a code contains [NORM_...,nbCells,-1], \a idsInPflPerType [[0,1,2]] and \a idsPerType is empty <br>
239  * 
240  *  \b Example2: <br>
241  *          - Before \a this has 3 cells \a profile contains [1,2]
242  *          - After \a code contains [NORM_...,nbCells,0], \a idsInPflPerType [[0,1]] and \a idsPerType is [[1,2]] <br>
243
244  */
245 void MEDCouplingStructuredMesh::splitProfilePerType(const DataArrayInt *profile, std::vector<int>& code, std::vector<DataArrayInt *>& idsInPflPerType, std::vector<DataArrayInt *>& idsPerType) const
246 {
247   if(!profile || !profile->isAllocated())
248     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::splitProfilePerType : input profile is NULL or not allocated !");
249   if(profile->getNumberOfComponents()!=1)
250     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::splitProfilePerType : input profile should have exactly one component !");
251   int nbTuples=profile->getNumberOfTuples();
252   int nbOfCells=getNumberOfCells();
253   code.resize(3); idsInPflPerType.resize(1);
254   code[0]=(int)getTypeOfCell(0); code[1]=nbOfCells;
255   idsInPflPerType.resize(1);
256   if(profile->isIdentity() && nbTuples==nbOfCells)
257     {
258       code[2]=-1;
259       idsInPflPerType[0]=0;
260       idsPerType.clear();
261       return ;
262     }
263   code[1]=profile->getNumberOfTuples();
264   code[2]=0;
265   profile->checkAllIdsInRange(0,nbOfCells);
266   idsPerType.resize(1);
267   idsPerType[0]=profile->deepCpy();
268   idsInPflPerType[0]=DataArrayInt::Range(0,nbTuples,1);
269 }
270
271 /*!
272  * Creates a new unstructured mesh (MEDCoupling1SGTUMesh) from \a this structured one.
273  *  \return MEDCouplingUMesh * - a new instance of MEDCouplingUMesh. The caller is to
274  * delete this array using decrRef() as it is no more needed. 
275  *  \throw If \a this->getMeshDimension() is not among [1,2,3].
276  */
277 MEDCoupling1SGTUMesh *MEDCouplingStructuredMesh::build1SGTUnstructured() const
278 {
279   int meshDim=getMeshDimension(); 
280   if(meshDim<0 || meshDim>3)
281     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::build1SGTUnstructured : meshdim must be in [1,2,3] !");
282   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> coords(getCoordinatesAndOwner());
283   int ns[3];
284   getNodeGridStructure(ns);
285   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(Build1GTNodalConnectivity(ns,ns+meshDim));
286   MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> ret(MEDCoupling1SGTUMesh::New(getName().c_str(),GetGeoTypeGivenMeshDimension(meshDim)));
287   ret->setNodalConnectivity(conn); ret->setCoords(coords);
288   return ret.retn();
289 }
290
291 /*!
292  * Creates a new unstructured mesh (MEDCouplingUMesh) from \a this structured one.
293  *  \return MEDCouplingUMesh * - a new instance of MEDCouplingUMesh. The caller is to
294  * delete this array using decrRef() as it is no more needed. 
295  *  \throw If \a this->getMeshDimension() is not among [1,2,3].
296  */
297 MEDCouplingUMesh *MEDCouplingStructuredMesh::buildUnstructured() const
298 {
299   MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> ret0(build1SGTUnstructured());
300   return ret0->buildUnstructured();
301 }
302
303 /*!
304  * Creates a new MEDCouplingUMesh containing a part of cells of \a this mesh.
305  * The cells to include to the
306  * result mesh are specified by an array of cell ids.
307  *  \param [in] start - an array of cell ids to include to the result mesh.
308  *  \param [in] end - specifies the end of the array \a start, so that
309  *              the last value of \a start is \a end[ -1 ].
310  *  \return MEDCouplingMesh * - a new instance of MEDCouplingUMesh. The caller is to
311  *         delete this mesh using decrRef() as it is no more needed. 
312  */
313 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPart(const int *start, const int *end) const
314 {
315   MEDCouplingUMesh *um=buildUnstructured();
316   MEDCouplingMesh *ret=um->buildPart(start,end);
317   um->decrRef();
318   return ret;
319 }
320
321 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPartAndReduceNodes(const int *start, const int *end, DataArrayInt*& arr) const
322 {
323   std::vector<int> cgs(getCellGridStructure());
324   std::vector< std::pair<int,int> > cellPartFormat,nodePartFormat;
325   if(IsPartStructured(start,end,cgs,cellPartFormat))
326     {
327       MEDCouplingAutoRefCountObjectPtr<MEDCouplingStructuredMesh> ret(buildStructuredSubPart(cellPartFormat));
328       nodePartFormat=cellPartFormat;
329       for(std::vector< std::pair<int,int> >::iterator it=nodePartFormat.begin();it!=nodePartFormat.end();it++)
330         (*it).second++;
331       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp1(BuildExplicitIdsFrom(getNodeGridStructure(),nodePartFormat));
332       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp2(DataArrayInt::New()); tmp2->alloc(getNumberOfNodes(),1);
333       tmp2->fillWithValue(-1);
334       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp3(DataArrayInt::New()); tmp3->alloc(tmp1->getNumberOfTuples(),1); tmp3->iota(0);
335       tmp2->setPartOfValues3(tmp3,tmp1->begin(),tmp1->end(),0,1,1);
336       arr=tmp2.retn();
337       return ret.retn();
338     }
339   else
340     {
341       MEDCouplingUMesh *um=buildUnstructured();
342       MEDCouplingMesh *ret=um->buildPartAndReduceNodes(start,end,arr);
343       um->decrRef();
344       return ret;
345     }
346 }
347
348 DataArrayInt *MEDCouplingStructuredMesh::simplexize(int policy)
349 {
350   throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::simplexize : not available for Cartesian mesh !");
351 }
352
353 /*!
354  * Returns a new MEDCouplingFieldDouble holding normal vectors to cells of \a this
355  * 2D mesh. The computed vectors have 3 components and are normalized.
356  *  \return MEDCouplingFieldDouble * - a new instance of MEDCouplingFieldDouble on
357  *          cells and one time. The caller is to delete this field using decrRef() as
358  *          it is no more needed.
359  *  \throw If \a this->getMeshDimension() != 2.
360  */
361 MEDCouplingFieldDouble *MEDCouplingStructuredMesh::buildOrthogonalField() const
362 {
363   if(getMeshDimension()!=2)
364     throw INTERP_KERNEL::Exception("Expected a MEDCouplingStructuredMesh with meshDim == 2 !");
365   MEDCouplingFieldDouble *ret=MEDCouplingFieldDouble::New(ON_CELLS,NO_TIME);
366   DataArrayDouble *array=DataArrayDouble::New();
367   int nbOfCells=getNumberOfCells();
368   array->alloc(nbOfCells,3);
369   double *vals=array->getPointer();
370   for(int i=0;i<nbOfCells;i++)
371     { vals[3*i]=0.; vals[3*i+1]=0.; vals[3*i+2]=1.; }
372   ret->setArray(array);
373   array->decrRef();
374   ret->setMesh(this);
375   return ret;
376 }
377
378 void MEDCouplingStructuredMesh::getReverseNodalConnectivity(DataArrayInt *revNodal, DataArrayInt *revNodalIndx) const
379 {
380   std::vector<int> ngs(getNodeGridStructure());
381   int dim(getMeshDimension());
382   switch(dim)
383   {
384     case 1:
385       return GetReverseNodalConnectivity1(ngs,revNodal,revNodalIndx);
386     case 2:
387       return GetReverseNodalConnectivity2(ngs,revNodal,revNodalIndx);
388     case 3:
389       return GetReverseNodalConnectivity3(ngs,revNodal,revNodalIndx);
390     default:
391       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::getReverseNodalConnectivity : only dimensions 1, 2 and 3 are supported !");
392   }
393 }
394
395 void MEDCouplingStructuredMesh::GetReverseNodalConnectivity1(const std::vector<int>& ngs, DataArrayInt *revNodal, DataArrayInt *revNodalIndx)
396 {
397   int nbNodes(ngs[0]);
398   revNodalIndx->alloc(nbNodes+1,1);
399   if(nbNodes==0)
400     { revNodal->alloc(0,1); revNodalIndx->setIJ(0,0,0); return ; }
401   if(nbNodes==1)
402     { revNodal->alloc(0,1); revNodalIndx->setIJ(0,0,0); revNodalIndx->setIJ(1,0,0); return ; }
403   revNodal->alloc(2*(nbNodes-1),1);
404   int *rn(revNodal->getPointer()),*rni(revNodalIndx->getPointer());
405   *rni++=0; *rni=1; *rn++=0;
406   for(int i=1;i<nbNodes-1;i++,rni++)
407     {
408       rn[0]=i-1; rn[1]=i;
409       rni[1]=rni[0]+2;
410       rn+=2;
411     }
412   rn[0]=nbNodes-2; rni[1]=rni[0]+1;
413 }
414
415 void MEDCouplingStructuredMesh::GetReverseNodalConnectivity2(const std::vector<int>& ngs, DataArrayInt *revNodal, DataArrayInt *revNodalIndx)
416 {
417   int nbNodesX(ngs[0]),nbNodesY(ngs[1]);
418   int nbNodes(nbNodesX*nbNodesY);
419   if(nbNodesX==0 || nbNodesY==0)
420     { revNodal->alloc(0,1); revNodalIndx->setIJ(0,0,0); return ; }
421   if(nbNodesX==1 || nbNodesY==1)
422     { std::vector<int> ngs2(1); ngs2[0]=std::max(nbNodesX,nbNodesY); return GetReverseNodalConnectivity1(ngs2,revNodal,revNodalIndx); }
423   revNodalIndx->alloc(nbNodes+1,1);
424   int nbCellsX(nbNodesX-1),nbCellsY(nbNodesY-1);
425   revNodal->alloc(4*(nbNodesX-2)*(nbNodesY-2)+2*2*(nbNodesX-2)+2*2*(nbNodesY-2)+4,1);
426   int *rn(revNodal->getPointer()),*rni(revNodalIndx->getPointer());
427   *rni++=0; *rni=1; *rn++=0;
428   for(int i=1;i<nbNodesX-1;i++,rni++,rn+=2)
429     {
430       rn[0]=i-1; rn[1]=i;
431       rni[1]=rni[0]+2;
432     }
433   rni[1]=rni[0]+1; *rn++=nbCellsX-1;
434   rni++;
435   for(int j=1;j<nbNodesY-1;j++)
436     {
437       int off(nbCellsX*(j-1)),off2(nbCellsX*j);
438       rni[1]=rni[0]+2; rn[0]=off; rn[1]=off2;
439       rni++; rn+=2;
440       for(int i=1;i<nbNodesX-1;i++,rni++,rn+=4)
441         {
442           rn[0]=i-1+off; rn[1]=i+off; rn[2]=i-1+off2; rn[3]=i+off2;
443           rni[1]=rni[0]+4;
444         }
445       rni[1]=rni[0]+2; rn[0]=off+nbCellsX-1; rn[1]=off2+nbCellsX-1;
446       rni++; rn+=2;
447     }
448   int off3(nbCellsX*(nbCellsY-1));
449   rni[1]=rni[0]+1;
450   rni++; *rn++=off3;
451   for(int i=1;i<nbNodesX-1;i++,rni++,rn+=2)
452     {
453       rn[0]=i-1+off3; rn[1]=i+off3;
454       rni[1]=rni[0]+2;
455     }
456   rni[1]=rni[0]+1; rn[0]=nbCellsX*nbCellsY-1;
457 }
458
459 void MEDCouplingStructuredMesh::GetReverseNodalConnectivity3(const std::vector<int>& ngs, DataArrayInt *revNodal, DataArrayInt *revNodalIndx)
460 {
461   int nbNodesX(ngs[0]),nbNodesY(ngs[1]),nbNodesZ(ngs[2]);
462   int nbNodes(nbNodesX*nbNodesY*nbNodesZ);
463   if(nbNodesX==0 || nbNodesY==0 || nbNodesZ==0)
464     { revNodal->alloc(0,1); revNodalIndx->setIJ(0,0,0); return ; }
465   if(nbNodesX==1 || nbNodesY==1 || nbNodesZ==1)
466     {
467       std::vector<int> ngs2(2);
468       int pos(0);
469       bool pass(false);
470       for(int i=0;i<3;i++)
471         {
472           if(pass)
473             { ngs2[pos++]=ngs[i]; }
474           else
475             {
476               pass=ngs[i]==1;
477               if(!pass)
478                 { ngs2[pos++]=ngs[i]; }
479             }
480         }
481       return GetReverseNodalConnectivity2(ngs2,revNodal,revNodalIndx);
482     }
483   revNodalIndx->alloc(nbNodes+1,1);
484   int nbCellsX(nbNodesX-1),nbCellsY(nbNodesY-1),nbCellsZ(nbNodesZ-1);
485   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);
486   int *rn(revNodal->getPointer()),*rni(revNodalIndx->getPointer());
487   *rni=0;
488   for(int k=0;k<nbNodesZ;k++)
489     {
490       bool factZ(k!=0 && k!=nbNodesZ-1);
491       int offZ0((k-1)*nbCellsX*nbCellsY),offZ1(k*nbCellsX*nbCellsY);
492       for(int j=0;j<nbNodesY;j++)
493         {
494           bool factYZ(factZ && (j!=0 && j!=nbNodesY-1));
495           int off00((j-1)*nbCellsX+offZ0),off01(j*nbCellsX+offZ0),off10((j-1)*nbCellsX+offZ1),off11(j*nbCellsX+offZ1);
496           for(int i=0;i<nbNodesX;i++,rni++)
497             {
498               int fact(factYZ && (i!=0 && i!=nbNodesX-1));
499               if(fact)
500                 {//most of points fall in this part of code
501                   rn[0]=off00+i-1; rn[1]=off00+i; rn[2]=off01+i-1; rn[3]=off01+i;
502                   rn[4]=off10+i-1; rn[5]=off10+i; rn[6]=off11+i-1; rn[7]=off11+i;
503                   rni[1]=rni[0]+8;
504                   rn+=8;
505                 }
506               else
507                 {
508                   int *rnRef(rn);
509                   if(k>=1 && j>=1 && i>=1)
510                     *rn++=off00+i-1;
511                   if(k>=1 && j>=1 && i<nbCellsX)
512                     *rn++=off00+i;
513                   if(k>=1 && j<nbCellsY && i>=1)
514                     *rn++=off01+i-1;
515                   if(k>=1 && j<nbCellsY && i<nbCellsX)
516                     *rn++=off01+i;
517                   //
518                   if(k<nbCellsZ && j>=1 && i>=1)
519                     *rn++=off10+i-1;
520                   if(k<nbCellsZ && j>=1 && i<nbCellsX)
521                     *rn++=off10+i;
522                   if(k<nbCellsZ && j<nbCellsY && i>=1)
523                     *rn++=off11+i-1;
524                   if(k<nbCellsZ && j<nbCellsY && i<nbCellsX)
525                     *rn++=off11+i;
526                   rni[1]=rni[0]+(int)(std::distance(rnRef,rn));
527                 }
528             }
529         }
530     }
531 }
532
533 /*!
534  * \return DataArrayInt * - newly allocated instance of nodal connectivity compatible for MEDCoupling1SGTMesh instance
535  */
536 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity(const int *nodeStBg, const int *nodeStEnd)
537 {
538   std::size_t dim=std::distance(nodeStBg,nodeStEnd);
539   switch(dim)
540     {
541     case 1:
542       return Build1GTNodalConnectivity1D(nodeStBg);
543     case 2:
544       return Build1GTNodalConnectivity2D(nodeStBg);
545     case 3:
546       return Build1GTNodalConnectivity3D(nodeStBg);
547     default:
548       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::Build1GTNodalConnectivity : only dimension in [1,2,3] supported !");
549     }
550 }
551
552 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity1D(const int *nodeStBg)
553 {
554   int nbOfCells(*nodeStBg-1);
555   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
556   conn->alloc(2*nbOfCells,1);
557   int *cp=conn->getPointer();
558   for(int i=0;i<nbOfCells;i++)
559     {
560       cp[2*i+0]=i;
561       cp[2*i+1]=i+1;
562     }
563   return conn.retn();
564 }
565
566 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity2D(const int *nodeStBg)
567 {
568   int n1=nodeStBg[0]-1;
569   int n2=nodeStBg[1]-1;
570   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
571   conn->alloc(4*n1*n2,1);
572   int *cp=conn->getPointer();
573   int pos=0;
574   for(int j=0;j<n2;j++)
575     for(int i=0;i<n1;i++,pos++)
576       {
577         cp[4*pos+0]=i+1+j*(n1+1);
578         cp[4*pos+1]=i+j*(n1+1);
579         cp[4*pos+2]=i+(j+1)*(n1+1);
580         cp[4*pos+3]=i+1+(j+1)*(n1+1);
581     }
582   return conn.retn();
583 }
584
585 DataArrayInt *MEDCouplingStructuredMesh::Build1GTNodalConnectivity3D(const int *nodeStBg)
586 {
587   int n1=nodeStBg[0]-1;
588   int n2=nodeStBg[1]-1;
589   int n3=nodeStBg[2]-1;
590   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(DataArrayInt::New());
591   conn->alloc(8*n1*n2*n3,1);
592   int *cp=conn->getPointer();
593   int pos=0;
594   for(int k=0;k<n3;k++)
595     for(int j=0;j<n2;j++)
596       for(int i=0;i<n1;i++,pos++)
597         {
598           int tmp=(n1+1)*(n2+1);
599           cp[8*pos+0]=i+1+j*(n1+1)+k*tmp;
600           cp[8*pos+1]=i+j*(n1+1)+k*tmp;
601           cp[8*pos+2]=i+(j+1)*(n1+1)+k*tmp;
602           cp[8*pos+3]=i+1+(j+1)*(n1+1)+k*tmp;
603           cp[8*pos+4]=i+1+j*(n1+1)+(k+1)*tmp;
604           cp[8*pos+5]=i+j*(n1+1)+(k+1)*tmp;
605           cp[8*pos+6]=i+(j+1)*(n1+1)+(k+1)*tmp;
606           cp[8*pos+7]=i+1+(j+1)*(n1+1)+(k+1)*tmp;
607         }
608   return conn.retn();
609 }
610
611 /*!
612  * Returns a cell id by its (i,j,k) index. The cell is located between the i-th and
613  * ( i + 1 )-th nodes along X axis etc.
614  *  \param [in] i - a index of node coordinates array along X axis.
615  *  \param [in] j - a index of node coordinates array along Y axis.
616  *  \param [in] k - a index of node coordinates array along Z axis.
617  *  \return int - a cell id in \a this mesh.
618  */
619 int MEDCouplingStructuredMesh::getCellIdFromPos(int i, int j, int k) const
620 {
621   int tmp[3]={i,j,k};
622   int tmp2[3];
623   int meshDim=getMeshDimension();
624   getSplitCellValues(tmp2);
625   std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
626   return std::accumulate(tmp,tmp+meshDim,0);
627 }
628
629 /*!
630  * Returns a node id by its (i,j,k) index.
631  *  \param [in] i - a index of node coordinates array along X axis.
632  *  \param [in] j - a index of node coordinates array along Y axis.
633  *  \param [in] k - a index of node coordinates array along Z axis.
634  *  \return int - a node id in \a this mesh.
635  */
636 int MEDCouplingStructuredMesh::getNodeIdFromPos(int i, int j, int k) const
637 {
638   int tmp[3]={i,j,k};
639   int tmp2[3];
640   int meshDim=getMeshDimension();
641   getSplitNodeValues(tmp2);
642   std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
643   return std::accumulate(tmp,tmp+meshDim,0);
644 }
645
646 void MEDCouplingStructuredMesh::GetPosFromId(int nodeId, int meshDim, const int *split, int *res)
647 {
648   int work=nodeId;
649   for(int i=meshDim-1;i>=0;i--)
650     {
651       int pos=work/split[i];
652       work=work%split[i];
653       res[i]=pos;
654     }
655 }
656
657 std::vector<int> MEDCouplingStructuredMesh::getCellGridStructure() const
658 {
659   std::vector<int> ret(getNodeGridStructure());
660   std::transform(ret.begin(),ret.end(),ret.begin(),std::bind2nd(std::plus<int>(),-1));
661   return ret;
662 }
663
664 /*!
665  * 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.
666  * If true is returned \a partCompactFormat will contain the information to build the corresponding part.
667  *
668  * \sa MEDCouplingStructuredMesh::BuildExplicitIdsFrom
669  */
670 bool MEDCouplingStructuredMesh::IsPartStructured(const int *startIds, const int *stopIds, const std::vector<int>& st, std::vector< std::pair<int,int> >& partCompactFormat)
671 {
672   int dim((int)st.size());
673   partCompactFormat.resize(dim);
674   if(dim<1 || dim>3)
675     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::isPartStructured : input structure must be of dimension in [1,2,3] !");
676   std::vector<int> tmp2(dim),tmp(dim),tmp3(dim),tmp4(dim); tmp2[0]=1;
677   for(int i=1;i<dim;i++)
678     tmp2[i]=tmp2[i-1]*st[i-1];
679   std::size_t sz(std::distance(startIds,stopIds));
680   if(sz==0)
681     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : empty input !");
682   GetPosFromId(*startIds,dim,&tmp2[0],&tmp[0]);
683   partCompactFormat.resize(dim);
684   for(int i=0;i<dim;i++)
685     partCompactFormat[i].first=tmp[i];
686   if(tmp[dim-1]<0 || tmp[dim-1]>=st[dim-1])
687     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : first id in input is not in valid range !");
688   if(sz==1)
689     {
690       for(int i=0;i<dim;i++)
691         partCompactFormat[i].second=tmp[i]+1;
692       return true;
693     }
694   GetPosFromId(startIds[sz-1],dim,&tmp2[0],&tmp3[0]);
695   int szExp(1);
696   for(int i=0;i<dim;i++)
697     {
698       if(tmp3[i]<0 || tmp3[i]>=st[i])
699         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : last id in input is not in valid range !");
700       partCompactFormat[i].second=tmp3[i]+1;
701       tmp4[i]=partCompactFormat[i].second-partCompactFormat[i].first;
702       if(tmp4[i]<=0)
703         return false;
704       szExp*=tmp4[i];
705     }
706   if(szExp!=(int)sz)
707     return false;
708   const int *w(startIds);
709   switch(dim)
710     {
711     case 3:
712       {
713         for(int i=0;i<tmp4[2];i++)
714           {
715             int a=tmp2[2]*(partCompactFormat[2].first+i);
716             for(int j=0;j<tmp4[1];j++)
717               {
718                 int b=tmp2[1]*(partCompactFormat[1].first+j);
719                 for(int k=0;k<tmp4[0];k++,w++)
720                   {
721                     if(partCompactFormat[0].first+k+b+a!=*w)
722                       return false;
723                   }
724               }
725           }
726         return true;
727       }
728     case 2:
729       {
730         for(int j=0;j<tmp4[1];j++)
731           {
732             int b=tmp2[1]*(partCompactFormat[1].first+j);
733             for(int k=0;k<tmp4[0];k++,w++)
734               {
735                 if(partCompactFormat[0].first+k+b!=*w)
736                   return false;
737               }
738           }
739         return true;
740       }
741     case 1:
742       {
743         for(int k=0;k<tmp4[0];k++,w++)
744           {
745             if(partCompactFormat[0].first+k!=*w)
746               return false;
747           }
748         return true;
749       }
750     default:
751       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : internal error !");
752     }
753 }
754
755 /*!
756  * This method builds the explicit entity array from the structure in \a st and the range in \a partCompactFormat.
757  *If the range contains invalid values regarding sructure an exception will be thrown.
758  *
759  * \return DataArrayInt * - a new object.
760  * \sa MEDCouplingStructuredMesh::IsPartStructured
761  */
762 DataArrayInt *MEDCouplingStructuredMesh::BuildExplicitIdsFrom(const std::vector<int>& st, const std::vector< std::pair<int,int> >& partCompactFormat)
763 {
764   if(st.size()!=partCompactFormat.size())
765     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : input arrays must have the same size !");
766   int nbOfItems(1);
767   std::vector<int> dims(st.size());
768   for(std::size_t i=0;i<st.size();i++)
769     {
770       if(partCompactFormat[i].first<0 || partCompactFormat[i].first>st[i])
771         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : invalid input range 1 !");
772       if(partCompactFormat[i].second<0 || partCompactFormat[i].second>st[i])
773         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : invalid input range 2 !");
774       if(partCompactFormat[i].second<=partCompactFormat[i].first)
775         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : invalid input range 3 !");
776       dims[i]=partCompactFormat[i].second-partCompactFormat[i].first;
777       nbOfItems*=dims[i];
778     }
779   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New());
780   ret->alloc(nbOfItems,1);
781   int *pt(ret->getPointer());
782   switch(st.size())
783     {
784     case 3:
785       {
786         for(int i=0;i<dims[2];i++)
787           {
788             int a=(partCompactFormat[2].first+i)*st[0]*st[1];
789             for(int j=0;j<dims[1];j++)
790               {
791                 int b=(partCompactFormat[1].first+j)*st[0];
792                 for(int k=0;k<dims[0];k++,pt++)
793                   *pt=partCompactFormat[0].first+k+b+a;
794               }
795           }
796         break;
797       }
798     case 2:
799       {
800         for(int j=0;j<dims[1];j++)
801           {
802             int b=(partCompactFormat[1].first+j)*st[0];
803             for(int k=0;k<dims[0];k++,pt++)
804               *pt=partCompactFormat[0].first+k+b;
805           }
806         break;
807       }
808     case 1:
809       {
810         for(int k=0;k<dims[0];k++,pt++)
811           *pt=partCompactFormat[0].first+k;
812         break;
813       }
814     default:
815       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : Dimension supported are 1,2 or 3 !");
816     }
817   return ret.retn();
818 }