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