]> SALOME platform Git repositories - tools/medcoupling.git/blob - src/MEDCoupling/MEDCouplingStructuredMesh.cxx
Salome HOME
MEDCouplingStructuredMesh.IsPartStructured MEDCouplingStructuredMesh.BuildExplicitIdsFrom
[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 "MEDCouplingUMesh.hxx"
25
26 #include <numeric>
27
28 using namespace ParaMEDMEM;
29
30 MEDCouplingStructuredMesh::MEDCouplingStructuredMesh()
31 {
32 }
33
34 MEDCouplingStructuredMesh::MEDCouplingStructuredMesh(const MEDCouplingStructuredMesh& other, bool deepCopy):MEDCouplingMesh(other)
35 {
36 }
37
38 MEDCouplingStructuredMesh::~MEDCouplingStructuredMesh()
39 {
40 }
41
42 std::size_t MEDCouplingStructuredMesh::getHeapMemorySize() const
43 {
44   return MEDCouplingMesh::getHeapMemorySize();
45 }
46
47 void MEDCouplingStructuredMesh::copyTinyStringsFrom(const MEDCouplingMesh *other) throw(INTERP_KERNEL::Exception)
48 {
49   MEDCouplingMesh::copyTinyStringsFrom(other);
50 }
51
52 bool MEDCouplingStructuredMesh::isEqualIfNotWhy(const MEDCouplingMesh *other, double prec, std::string& reason) const throw(INTERP_KERNEL::Exception)
53 {
54   return MEDCouplingMesh::isEqualIfNotWhy(other,prec,reason);
55 }
56
57 INTERP_KERNEL::NormalizedCellType MEDCouplingStructuredMesh::getTypeOfCell(int cellId) const
58 {
59   return GetGeoTypeGivenMeshDimension(getMeshDimension());
60 }
61
62 INTERP_KERNEL::NormalizedCellType MEDCouplingStructuredMesh::GetGeoTypeGivenMeshDimension(int meshDim) throw(INTERP_KERNEL::Exception)
63 {
64   switch(meshDim)
65     {
66     case 3:
67       return INTERP_KERNEL::NORM_HEXA8;
68     case 2:
69       return INTERP_KERNEL::NORM_QUAD4;
70     case 1:
71       return INTERP_KERNEL::NORM_SEG2;
72     default:
73       throw INTERP_KERNEL::Exception("Unexpected dimension for MEDCouplingStructuredMesh::GetGeoTypeGivenMeshDimension !");
74     }
75 }
76
77 std::set<INTERP_KERNEL::NormalizedCellType> MEDCouplingStructuredMesh::getAllGeoTypes() const
78 {
79   std::set<INTERP_KERNEL::NormalizedCellType> ret2;
80   ret2.insert(getTypeOfCell(0));
81   return ret2;
82 }
83
84 int MEDCouplingStructuredMesh::getNumberOfCellsWithType(INTERP_KERNEL::NormalizedCellType type) const
85 {
86   int ret=getNumberOfCells();
87   if(type==getTypeOfCell(0))
88     return ret;
89   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
90   std::ostringstream oss; oss << "MEDCouplingStructuredMesh::getNumberOfCellsWithType : no specified type ! Type available is " << cm.getRepr() << " !";
91   throw INTERP_KERNEL::Exception(oss.str().c_str());
92 }
93
94 DataArrayInt *MEDCouplingStructuredMesh::giveCellsWithType(INTERP_KERNEL::NormalizedCellType type) const throw(INTERP_KERNEL::Exception)
95 {
96   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
97   if(getTypeOfCell(0)==type)
98     {
99       ret->alloc(getNumberOfCells(),1);
100       ret->iota(0);
101     }
102   else
103     ret->alloc(0,1);
104   return ret.retn();
105 }
106
107 DataArrayInt *MEDCouplingStructuredMesh::computeNbOfNodesPerCell() const throw(INTERP_KERNEL::Exception)
108 {
109   int nbCells=getNumberOfCells();
110   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
111   ret->alloc(nbCells,1);
112   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
113   ret->fillWithValue((int)cm.getNumberOfNodes());
114   return ret.retn();
115 }
116
117 DataArrayInt *MEDCouplingStructuredMesh::computeNbOfFacesPerCell() const throw(INTERP_KERNEL::Exception)
118 {
119   int nbCells=getNumberOfCells();
120   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
121   ret->alloc(nbCells,1);
122   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
123   ret->fillWithValue((int)cm.getNumberOfSons());
124   return ret.retn();
125 }
126
127 void MEDCouplingStructuredMesh::getNodeIdsOfCell(int cellId, std::vector<int>& conn) const
128 {
129   int meshDim=getMeshDimension();
130   int tmpCell[3],tmpNode[3];
131   getSplitCellValues(tmpCell);
132   getSplitNodeValues(tmpNode);
133   int tmp2[3];
134   GetPosFromId(cellId,meshDim,tmpCell,tmp2);
135   switch(meshDim)
136     {
137     case 1:
138       conn.push_back(tmp2[0]); conn.push_back(tmp2[0]+1);
139       break;
140     case 2:
141       conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]); conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]+1);
142       conn.push_back((tmp2[1]+1)*(tmpCell[1]+1)+tmp2[0]+1); conn.push_back((tmp2[1]+1)*(tmpCell[1]+1)+tmp2[0]);
143       break;
144     case 3:
145       conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]+tmp2[2]*tmpNode[2]); conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]+1+tmp2[2]*tmpNode[2]);
146       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]);
147       conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]+(tmp2[2]+1)*tmpNode[2]); conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]+1+(tmp2[2]+1)*tmpNode[2]);
148       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]);
149       break;
150     default:
151       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::getNodeIdsOfCell : big problem spacedim must be in 1,2 or 3 !");
152     };
153 }
154
155 /*!
156  * See MEDCouplingUMesh::getDistributionOfTypes for more information
157  */
158 std::vector<int> MEDCouplingStructuredMesh::getDistributionOfTypes() const throw(INTERP_KERNEL::Exception)
159 {
160   //only one type of cell
161   std::vector<int> ret(3);
162   ret[0]=getTypeOfCell(0);
163   ret[1]=getNumberOfCells();
164   ret[2]=-1; //ret[3*k+2]==-1 because it has no sense here
165   return ret;
166 }
167
168 /*!
169  * This method tries to minimize at most the number of deep copy.
170  * So if \a idsPerType is not empty it can be returned directly (without copy, but with ref count incremented) in return.
171  * 
172  * See MEDCouplingUMesh::checkTypeConsistencyAndContig for more information
173  */
174 DataArrayInt *MEDCouplingStructuredMesh::checkTypeConsistencyAndContig(const std::vector<int>& code, const std::vector<const DataArrayInt *>& idsPerType) const throw(INTERP_KERNEL::Exception)
175 {
176   int nbOfCells=getNumberOfCells();
177   if(code.size()!=3)
178     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : invalid input code should be exactly of size 3 !");
179   if(code[0]!=(int)getTypeOfCell(0))
180     {
181       std::ostringstream oss; oss << "MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : Mismatch of geometric type ! Asking for " << code[0] << " whereas the geometric type is \a this is " << getTypeOfCell(0) << " !";
182       throw INTERP_KERNEL::Exception(oss.str().c_str());
183     }
184   if(code[2]==-1)
185     {
186       if(code[1]==nbOfCells)
187         return 0;
188       else
189         {
190           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : mismatch between the number of cells in this (" << nbOfCells << ") and the number of non profile (" << code[1] << ") !";
191           throw INTERP_KERNEL::Exception(oss.str().c_str());
192         }
193     }
194   if(code[2]!=0)
195     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : single geo type mesh ! 0 or -1 is expected at pos #2 of input code !");
196   if(idsPerType.size()!=1)
197     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : input code points to DataArrayInt #0 whereas the size of idsPerType is not equal to 1 !");
198   const DataArrayInt *pfl=idsPerType[0];
199   if(!pfl)
200     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : the input code points to a NULL DataArrayInt at rank 0 !");
201   if(pfl->getNumberOfComponents()!=1)
202     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : input profile should have exactly one component !");
203   pfl->checkAllIdsInRange(0,nbOfCells);
204   pfl->incrRef();
205   return const_cast<DataArrayInt *>(pfl);
206 }
207
208 /*!
209  * 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.
210  * 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.
211  * This method has 1 input \a profile and 3 outputs \a code \a idsInPflPerType and \a idsPerType.
212  * 
213  * \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.
214  * \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,
215  *              \a idsInPflPerType[i] stores the tuple ids in \a profile that correspond to the geometric type code[3*i+0]
216  * \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.
217  *              This vector can be empty in case of all geometric type cells are fully covered in ascending in the given input \a profile.
218  * 
219  * \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.
220  *
221  * \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
222  *
223  *  \b Example1: <br>
224  *          - Before \a this has 3 cells \a profile contains [0,1,2]
225  *          - After \a code contains [NORM_...,nbCells,-1], \a idsInPflPerType [[0,1,2]] and \a idsPerType is empty <br>
226  * 
227  *  \b Example2: <br>
228  *          - Before \a this has 3 cells \a profile contains [1,2]
229  *          - After \a code contains [NORM_...,nbCells,0], \a idsInPflPerType [[0,1]] and \a idsPerType is [[1,2]] <br>
230
231  */
232 void MEDCouplingStructuredMesh::splitProfilePerType(const DataArrayInt *profile, std::vector<int>& code, std::vector<DataArrayInt *>& idsInPflPerType, std::vector<DataArrayInt *>& idsPerType) const throw(INTERP_KERNEL::Exception)
233 {
234   if(!profile)
235     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::splitProfilePerType : input profile is NULL !");
236   if(profile->getNumberOfComponents()!=1)
237     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::splitProfilePerType : input profile should have exactly one component !");
238   int nbTuples=profile->getNumberOfTuples();
239   int nbOfCells=getNumberOfCells();
240   code.resize(3); idsInPflPerType.resize(1);
241   code[0]=(int)getTypeOfCell(0); code[1]=nbOfCells;
242   idsInPflPerType.resize(1);
243   if(profile->isIdentity() && nbTuples==nbOfCells)
244     {
245       code[2]=-1;
246       idsInPflPerType[0]=const_cast<DataArrayInt *>(profile); idsInPflPerType[0]->incrRef();
247       idsPerType.clear(); 
248     }
249   code[2]=0;
250   profile->checkAllIdsInRange(0,nbOfCells);
251   idsPerType.resize(1);
252   idsPerType[0]=const_cast<DataArrayInt *>(profile); idsPerType[0]->incrRef();
253   idsInPflPerType[0]=DataArrayInt::Range(0,nbTuples,1);
254 }
255
256 /*!
257  * Creates a new unstructured mesh (MEDCouplingUMesh) from \a this structured one.
258  *  \return MEDCouplingUMesh * - a new instance of MEDCouplingUMesh. The caller is to
259  * delete this array using decrRef() as it is no more needed. 
260  *  \throw If \a this->getMeshDimension() is not among [1,2,3].
261  */
262 MEDCouplingUMesh *MEDCouplingStructuredMesh::buildUnstructured() const throw(INTERP_KERNEL::Exception)
263 {
264   int meshDim=getMeshDimension();
265   MEDCouplingUMesh *ret=MEDCouplingUMesh::New(getName(),meshDim);
266   DataArrayDouble *coords=getCoordinatesAndOwner();
267   ret->setCoords(coords);
268   coords->decrRef();
269   switch(meshDim)
270     {
271     case 1:
272       fill1DUnstructuredMesh(ret);
273       break;
274     case 2:
275       fill2DUnstructuredMesh(ret);
276       break;
277     case 3:
278       fill3DUnstructuredMesh(ret);
279       break;
280     default:
281       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::buildUnstructured : big problem spacedim must be in 1,2 or 3 !");
282     };
283   return ret;
284 }
285
286 /*!
287  * Creates a new MEDCouplingUMesh containing a part of cells of \a this mesh.
288  * The cells to include to the
289  * result mesh are specified by an array of cell ids.
290  *  \param [in] start - an array of cell ids to include to the result mesh.
291  *  \param [in] end - specifies the end of the array \a start, so that
292  *              the last value of \a start is \a end[ -1 ].
293  *  \return MEDCouplingMesh * - a new instance of MEDCouplingUMesh. The caller is to
294  *         delete this mesh using decrRef() as it is no more needed. 
295  */
296 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPart(const int *start, const int *end) const
297 {
298   MEDCouplingUMesh *um=buildUnstructured();
299   MEDCouplingMesh *ret=um->buildPart(start,end);
300   um->decrRef();
301   return ret;
302 }
303
304 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPartAndReduceNodes(const int *start, const int *end, DataArrayInt*& arr) const
305 {
306   std::vector<int> cgs(getCellGridStructure());
307   std::vector< std::pair<int,int> > cellPartFormat,nodePartFormat;
308   if(IsPartStructured(start,end,cgs,cellPartFormat))
309     {
310       MEDCouplingAutoRefCountObjectPtr<MEDCouplingStructuredMesh> ret(buildStructuredSubPart(cellPartFormat));
311       nodePartFormat=cellPartFormat;
312       for(std::vector< std::pair<int,int> >::iterator it=nodePartFormat.begin();it!=nodePartFormat.end();it++)
313         (*it).second++;
314       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp1(BuildExplicitIdsFrom(getNodeGridStructure(),nodePartFormat));
315       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp2(DataArrayInt::New()); tmp2->alloc(getNumberOfNodes(),1);
316       tmp2->fillWithValue(-1);
317       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp3(DataArrayInt::New()); tmp3->alloc(tmp1->getNumberOfTuples(),1); tmp3->iota(0);
318       tmp2->setPartOfValues3(tmp3,tmp1->begin(),tmp1->end(),0,1,1);
319       arr=tmp2.retn();
320       return ret.retn();
321     }
322   else
323     {
324       MEDCouplingUMesh *um=buildUnstructured();
325       MEDCouplingMesh *ret=um->buildPartAndReduceNodes(start,end,arr);
326       um->decrRef();
327       return ret;
328     }
329 }
330
331 DataArrayInt *MEDCouplingStructuredMesh::simplexize(int policy) throw(INTERP_KERNEL::Exception)
332 {
333   throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::simplexize : not available for Cartesian mesh !");
334 }
335
336 /*!
337  * Returns a new MEDCouplingFieldDouble holding normal vectors to cells of \a this
338  * 2D mesh. The computed vectors have 3 components and are normalized.
339  *  \return MEDCouplingFieldDouble * - a new instance of MEDCouplingFieldDouble on
340  *          cells and one time. The caller is to delete this field using decrRef() as
341  *          it is no more needed.
342  *  \throw If \a this->getMeshDimension() != 2.
343  */
344 MEDCouplingFieldDouble *MEDCouplingStructuredMesh::buildOrthogonalField() const
345 {
346   if(getMeshDimension()!=2)
347     throw INTERP_KERNEL::Exception("Expected a MEDCouplingStructuredMesh with meshDim == 2 !");
348   MEDCouplingFieldDouble *ret=MEDCouplingFieldDouble::New(ON_CELLS,NO_TIME);
349   DataArrayDouble *array=DataArrayDouble::New();
350   int nbOfCells=getNumberOfCells();
351   array->alloc(nbOfCells,3);
352   double *vals=array->getPointer();
353   for(int i=0;i<nbOfCells;i++)
354     { vals[3*i]=0.; vals[3*i+1]=0.; vals[3*i+2]=1.; }
355   ret->setArray(array);
356   array->decrRef();
357   ret->setMesh(this);
358   return ret;
359 }
360
361 void MEDCouplingStructuredMesh::fill1DUnstructuredMesh(MEDCouplingUMesh *m) const
362 {
363   int nbOfCells=-1;
364   getNodeGridStructure(&nbOfCells);
365   nbOfCells--;
366   DataArrayInt *connI=DataArrayInt::New();
367   connI->alloc(nbOfCells+1,1);
368   int *ci=connI->getPointer();
369   DataArrayInt *conn=DataArrayInt::New();
370   conn->alloc(3*nbOfCells,1);
371   ci[0]=0;
372   int *cp=conn->getPointer();
373   for(int i=0;i<nbOfCells;i++)
374     {
375       cp[3*i]=(int)INTERP_KERNEL::NORM_SEG2;
376       cp[3*i+1]=i;
377       cp[3*i+2]=i+1;
378       ci[i+1]=3*(i+1);
379     }
380   m->setConnectivity(conn,connI,true);
381   conn->decrRef();
382   connI->decrRef();
383 }
384
385 void MEDCouplingStructuredMesh::fill2DUnstructuredMesh(MEDCouplingUMesh *m) const
386 {
387   int ns[2];
388   getNodeGridStructure(ns);
389   int n1=ns[0]-1;
390   int n2=ns[1]-1;
391   DataArrayInt *connI=DataArrayInt::New();
392   connI->alloc(n1*n2+1,1);
393   int *ci=connI->getPointer();
394   DataArrayInt *conn=DataArrayInt::New();
395   conn->alloc(5*n1*n2,1);
396   ci[0]=0;
397   int *cp=conn->getPointer();
398   int pos=0;
399   for(int j=0;j<n2;j++)
400     for(int i=0;i<n1;i++,pos++)
401       {
402         cp[5*pos]=(int)INTERP_KERNEL::NORM_QUAD4;
403         cp[5*pos+1]=i+1+j*(n1+1);
404         cp[5*pos+2]=i+j*(n1+1);
405         cp[5*pos+3]=i+(j+1)*(n1+1);
406         cp[5*pos+4]=i+1+(j+1)*(n1+1);
407         ci[pos+1]=5*(pos+1);
408     }
409   m->setConnectivity(conn,connI,true);
410   conn->decrRef();
411   connI->decrRef();
412 }
413
414 void MEDCouplingStructuredMesh::fill3DUnstructuredMesh(MEDCouplingUMesh *m) const
415 {
416   int ns[3];
417   getNodeGridStructure(ns);
418   int n1=ns[0]-1;
419   int n2=ns[1]-1;
420   int n3=ns[2]-1;
421   DataArrayInt *connI=DataArrayInt::New();
422   connI->alloc(n1*n2*n3+1,1);
423   int *ci=connI->getPointer();
424   DataArrayInt *conn=DataArrayInt::New();
425   conn->alloc(9*n1*n2*n3,1);
426   ci[0]=0;
427   int *cp=conn->getPointer();
428   int pos=0;
429   for(int k=0;k<n3;k++)
430     for(int j=0;j<n2;j++)
431       for(int i=0;i<n1;i++,pos++)
432         {
433           cp[9*pos]=(int)INTERP_KERNEL::NORM_HEXA8;
434           int tmp=(n1+1)*(n2+1);
435           cp[9*pos+1]=i+1+j*(n1+1)+k*tmp;
436           cp[9*pos+2]=i+j*(n1+1)+k*tmp;
437           cp[9*pos+3]=i+(j+1)*(n1+1)+k*tmp;
438           cp[9*pos+4]=i+1+(j+1)*(n1+1)+k*tmp;
439           cp[9*pos+5]=i+1+j*(n1+1)+(k+1)*tmp;
440           cp[9*pos+6]=i+j*(n1+1)+(k+1)*tmp;
441           cp[9*pos+7]=i+(j+1)*(n1+1)+(k+1)*tmp;
442           cp[9*pos+8]=i+1+(j+1)*(n1+1)+(k+1)*tmp;
443           ci[pos+1]=9*(pos+1);
444         }
445   m->setConnectivity(conn,connI,true);
446   conn->decrRef();
447   connI->decrRef();
448 }
449
450 /*!
451  * Returns a cell id by its (i,j,k) index. The cell is located between the i-th and
452  * ( i + 1 )-th nodes along X axis etc.
453  *  \param [in] i - a index of node coordinates array along X axis.
454  *  \param [in] j - a index of node coordinates array along Y axis.
455  *  \param [in] k - a index of node coordinates array along Z axis.
456  *  \return int - a cell id in \a this mesh.
457  */
458 int MEDCouplingStructuredMesh::getCellIdFromPos(int i, int j, int k) const
459 {
460   int tmp[3]={i,j,k};
461   int tmp2[3];
462   int meshDim=getMeshDimension();
463   getSplitCellValues(tmp2);
464   std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
465   return std::accumulate(tmp,tmp+meshDim,0);
466 }
467
468 /*!
469  * Returns a node id by its (i,j,k) index.
470  *  \param [in] i - a index of node coordinates array along X axis.
471  *  \param [in] j - a index of node coordinates array along Y axis.
472  *  \param [in] k - a index of node coordinates array along Z axis.
473  *  \return int - a node id in \a this mesh.
474  */
475 int MEDCouplingStructuredMesh::getNodeIdFromPos(int i, int j, int k) const
476 {
477   int tmp[3]={i,j,k};
478   int tmp2[3];
479   int meshDim=getMeshDimension();
480   getSplitNodeValues(tmp2);
481   std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
482   return std::accumulate(tmp,tmp+meshDim,0);
483 }
484
485 void MEDCouplingStructuredMesh::GetPosFromId(int nodeId, int meshDim, const int *split, int *res)
486 {
487   int work=nodeId;
488   for(int i=meshDim-1;i>=0;i--)
489     {
490       int pos=work/split[i];
491       work=work%split[i];
492       res[i]=pos;
493     }
494 }
495
496 std::vector<int> MEDCouplingStructuredMesh::getCellGridStructure() const throw(INTERP_KERNEL::Exception)
497 {
498   std::vector<int> ret(getNodeGridStructure());
499   std::transform(ret.begin(),ret.end(),ret.begin(),std::bind2nd(std::plus<int>(),-1));
500   return ret;
501 }
502
503 /*!
504  * 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.
505  * If true is returned \a partCompactFormat will contain the information to build the corresponding part.
506  *
507  * \sa MEDCouplingStructuredMesh::BuildExplicitIdsFrom
508  */
509 bool MEDCouplingStructuredMesh::IsPartStructured(const int *startIds, const int *stopIds, const std::vector<int>& st, std::vector< std::pair<int,int> >& partCompactFormat) throw(INTERP_KERNEL::Exception)
510 {
511   int dim((int)st.size());
512   partCompactFormat.resize(dim);
513   if(dim<1 || dim>3)
514     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::isPartStructured : input structure must be of dimension in [1,2,3] !");
515   std::vector<int> tmp2(dim),tmp(dim),tmp3(dim),tmp4(dim); tmp2[0]=1;
516   for(int i=1;i<dim;i++)
517     tmp2[i]=tmp2[i-1]*st[i-1];
518   std::size_t sz(std::distance(startIds,stopIds));
519   if(sz==0)
520     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : empty input !");
521   GetPosFromId(*startIds,dim,&tmp2[0],&tmp[0]);
522   partCompactFormat.resize(dim);
523   for(int i=0;i<dim;i++)
524     partCompactFormat[i].first=tmp[i];
525   if(tmp[dim-1]<0 || tmp[dim-1]>=st[dim-1])
526     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : first id in input is not in valid range !");
527   if(sz==1)
528     {
529       for(int i=0;i<dim;i++)
530         partCompactFormat[i].second=tmp[i]+1;
531       return true;
532     }
533   GetPosFromId(startIds[sz-1],dim,&tmp2[0],&tmp3[0]);
534   for(int i=0;i<dim;i++)
535     {
536       if(tmp3[i]<0 || tmp3[i]>=st[i])
537         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : last id in input is not in valid range !");
538       partCompactFormat[i].second=tmp3[i]+1;
539       tmp4[i]=partCompactFormat[i].second-partCompactFormat[i].first;
540       if(tmp4[i]<=0)
541         return false;
542     }
543   const int *w(startIds);
544   switch(dim)
545     {
546     case 3:
547       {
548         for(int i=0;i<tmp4[2];i++)
549           {
550             int a=tmp2[2]*(partCompactFormat[2].first+i);
551             for(int j=0;j<tmp4[1];j++)
552               {
553                 int b=tmp2[1]*(partCompactFormat[1].first+j);
554                 for(int k=0;k<tmp4[0];k++,w++)
555                   {
556                     if(partCompactFormat[0].first+k+b+a!=*w)
557                       return false;
558                   }
559               }
560           }
561         return true;
562       }
563     case 2:
564       {
565         for(int j=0;j<tmp4[1];j++)
566           {
567             int b=tmp2[1]*(partCompactFormat[1].first+j);
568             for(int k=0;k<tmp4[0];k++,w++)
569               {
570                 if(partCompactFormat[0].first+k+b!=*w)
571                   return false;
572               }
573           }
574         return true;
575       }
576     case 1:
577       {
578         for(int k=0;k<tmp4[0];k++,w++)
579           {
580             if(partCompactFormat[0].first+k!=*w)
581               return false;
582           }
583         return true;
584       }
585     default:
586       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::IsPartStructured : internal error !");
587     }
588 }
589
590 /*!
591  * This method builds the explicit entity array from the structure in \a st and the range in \a partCompactFormat.
592  *If the range contains invalid values regarding sructure an exception will be thrown.
593  *
594  * \return DataArrayInt * - a new object.
595  * \sa MEDCouplingStructuredMesh::IsPartStructured
596  */
597 DataArrayInt *MEDCouplingStructuredMesh::BuildExplicitIdsFrom(const std::vector<int>& st, const std::vector< std::pair<int,int> >& partCompactFormat) throw(INTERP_KERNEL::Exception)
598 {
599   if(st.size()!=partCompactFormat.size())
600     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : input arrays must have the same size !");
601   int nbOfItems(1);
602   std::vector<int> dims(st.size());
603   for(std::size_t i=0;i<st.size();i++)
604     {
605       if(partCompactFormat[i].first<0 || partCompactFormat[i].first>st[i])
606         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : invalid input range 1 !");
607       if(partCompactFormat[i].second<0 || partCompactFormat[i].second>st[i])
608         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : invalid input range 2 !");
609       if(partCompactFormat[i].second<=partCompactFormat[i].first)
610         throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : invalid input range 3 !");
611       dims[i]=partCompactFormat[i].second-partCompactFormat[i].first;
612       nbOfItems*=dims[i];
613     }
614   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret(DataArrayInt::New());
615   ret->alloc(nbOfItems,1);
616   int *pt(ret->getPointer());
617   switch(st.size())
618     {
619     case 3:
620       {
621         for(int i=0;i<dims[2];i++)
622           {
623             int a=(partCompactFormat[2].first+i)*st[0]*st[1];
624             for(int j=0;j<dims[1];j++)
625               {
626                 int b=(partCompactFormat[1].first+j)*st[0];
627                 for(int k=0;k<dims[0];k++,pt++)
628                   *pt=partCompactFormat[0].first+k+b+a;
629               }
630           }
631         break;
632       }
633     case 2:
634       {
635         for(int j=0;j<dims[1];j++)
636           {
637             int b=(partCompactFormat[1].first+j)*st[0];
638             for(int k=0;k<dims[0];k++,pt++)
639               *pt=partCompactFormat[0].first+k+b;
640           }
641         break;
642       }
643     case 1:
644       {
645         for(int k=0;k<dims[0];k++,pt++)
646           *pt=partCompactFormat[0].first+k;
647         break;
648       }
649     default:
650       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::BuildExplicitIdsFrom : Dimension supported are 1,2 or 3 !");
651     }
652   return ret.retn();
653 }