]> SALOME platform Git repositories - tools/medcoupling.git/blob - src/MEDCoupling/MEDCouplingIMesh.cxx
Salome HOME
Checks the condensation of IMesh from fine to coarse.
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingIMesh.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #include "MEDCouplingIMesh.hxx"
22 #include "MEDCouplingCMesh.hxx"
23 #include "MEDCouplingMemArray.hxx"
24 #include "MEDCouplingFieldDouble.hxx"
25
26 #include <functional>
27 #include <algorithm>
28 #include <sstream>
29 #include <numeric>
30
31 using namespace ParaMEDMEM;
32
33 MEDCouplingIMesh::MEDCouplingIMesh():_space_dim(-1)
34 {
35   _origin[0]=0.; _origin[1]=0.; _origin[2]=0.;
36   _dxyz[0]=0.; _dxyz[1]=0.; _dxyz[2]=0.;
37   _structure[0]=0; _structure[1]=0; _structure[2]=0;
38 }
39
40 MEDCouplingIMesh::MEDCouplingIMesh(const MEDCouplingIMesh& other, bool deepCopy):MEDCouplingStructuredMesh(other,deepCopy),_space_dim(other._space_dim),_axis_unit(other._axis_unit)
41 {
42   _origin[0]=other._origin[0]; _origin[1]=other._origin[1]; _origin[2]=other._origin[2];
43   _dxyz[0]=other._dxyz[0]; _dxyz[1]=other._dxyz[1]; _dxyz[2]=other._dxyz[2];
44   _structure[0]=other._structure[0]; _structure[1]=other._structure[1]; _structure[2]=other._structure[2];
45 }
46
47 MEDCouplingIMesh::~MEDCouplingIMesh()
48 {
49 }
50
51 MEDCouplingIMesh *MEDCouplingIMesh::New()
52 {
53   return new MEDCouplingIMesh;
54 }
55
56 MEDCouplingIMesh *MEDCouplingIMesh::New(const std::string& meshName, int spaceDim, const int *nodeStrctStart, const int *nodeStrctStop,
57                                         const double *originStart, const double *originStop, const double *dxyzStart, const double *dxyzStop)
58 {
59   MEDCouplingAutoRefCountObjectPtr<MEDCouplingIMesh> ret(new MEDCouplingIMesh);
60   ret->setName(meshName);
61   ret->setSpaceDimension(spaceDim);
62   ret->setNodeStruct(nodeStrctStart,nodeStrctStop);
63   ret->setOrigin(originStart,originStop);
64   ret->setDXYZ(dxyzStart,dxyzStop);
65   return ret.retn();
66 }
67
68 MEDCouplingMesh *MEDCouplingIMesh::deepCpy() const
69 {
70   return clone(true);
71 }
72
73 MEDCouplingIMesh *MEDCouplingIMesh::clone(bool recDeepCpy) const
74 {
75   return new MEDCouplingIMesh(*this,recDeepCpy);
76 }
77
78 void MEDCouplingIMesh::setNodeStruct(const int *nodeStrctStart, const int *nodeStrctStop)
79 {
80   checkSpaceDimension();
81   int sz((int)std::distance(nodeStrctStart,nodeStrctStop));
82   if(sz!=_space_dim)
83     throw INTERP_KERNEL::Exception("MEDCouplingIMesh::setNodeStruct : input vector of node structure has not the right size ! Or change space dimension before calling it !");
84   std::copy(nodeStrctStart,nodeStrctStop,_structure);
85   declareAsNew();
86 }
87
88 std::vector<int> MEDCouplingIMesh::getNodeStruct() const
89 {
90   checkSpaceDimension();
91   return std::vector<int>(_structure,_structure+_space_dim);
92 }
93
94 void MEDCouplingIMesh::setOrigin(const double *originStart, const double *originStop)
95 {
96   checkSpaceDimension();
97   int sz((int)std::distance(originStart,originStop));
98   if(sz!=_space_dim)
99     throw INTERP_KERNEL::Exception("MEDCouplingIMesh::setOrigin : input vector of origin vector has not the right size ! Or change space dimension before calling it !");
100   std::copy(originStart,originStop,_origin);
101   declareAsNew();
102 }
103
104 std::vector<double> MEDCouplingIMesh::getOrigin() const
105 {
106   checkSpaceDimension();
107   return std::vector<double>(_origin,_origin+_space_dim);
108 }
109
110 void MEDCouplingIMesh::setDXYZ(const double *dxyzStart, const double *dxyzStop)
111 {
112   checkSpaceDimension();
113   int sz((int)std::distance(dxyzStart,dxyzStop));
114   if(sz!=_space_dim)
115     throw INTERP_KERNEL::Exception("MEDCouplingIMesh::setDXYZ : input vector of dxyz vector has not the right size ! Or change space dimension before calling it !");
116   std::copy(dxyzStart,dxyzStop,_dxyz);
117   declareAsNew();
118 }
119
120 std::vector<double> MEDCouplingIMesh::getDXYZ() const
121 {
122   checkSpaceDimension();
123   return std::vector<double>(_dxyz,_dxyz+_space_dim);
124 }
125
126 void MEDCouplingIMesh::setAxisUnit(const std::string& unitName)
127 {
128   _axis_unit=unitName;
129   declareAsNew();
130 }
131
132 std::string MEDCouplingIMesh::getAxisUnit() const
133 {
134   return _axis_unit;
135 }
136
137 /*!
138  * This method returns the measure of any cell in \a this.
139  * This specific method of image grid mesh utilizes the fact that any cell in \a this have the same measure.
140  * The value returned by this method is those used to feed the returned field in the MEDCouplingIMesh::getMeasureField.
141  *
142  * \sa getMeasureField
143  */
144 double MEDCouplingIMesh::getMeasureOfAnyCell() const
145 {
146   checkCoherency();
147   int dim(getSpaceDimension());
148   double ret(1.);
149   for(int i=0;i<dim;i++)
150     ret*=fabs(_dxyz[i]);
151   return ret;
152 }
153
154 /*!
155  * This method is allows to convert \a this into MEDCouplingCMesh instance.
156  * This method is the middle level between MEDCouplingIMesh and the most general MEDCouplingUMesh.
157  * This method is useful for MED writers that do not have still the image grid support.
158  *
159  * \sa MEDCouplingMesh::buildUnstructured
160  */
161 MEDCouplingCMesh *MEDCouplingIMesh::convertToCartesian() const
162 {
163   checkCoherency();
164   MEDCouplingAutoRefCountObjectPtr<MEDCouplingCMesh> ret(MEDCouplingCMesh::New());
165   try
166   { ret->copyTinyInfoFrom(this); }
167   catch(INTERP_KERNEL::Exception& ) { }
168   int spaceDim(getSpaceDimension());
169   std::vector<std::string> infos(buildInfoOnComponents());
170   for(int i=0;i<spaceDim;i++)
171     {
172       MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> arr(DataArrayDouble::New()); arr->alloc(_structure[i],1); arr->setInfoOnComponent(0,infos[i]);
173       arr->iota(); arr->applyLin(_dxyz[i],_origin[i]);
174       ret->setCoordsAt(i,arr);
175     }
176   return ret.retn();
177 }
178
179 /*!
180  * This method refines \a this uniformaly along all of its dimensions. In case of success the space covered by \a this will remain
181  * the same before the invocation except that the number of cells will be multiplied by \a factor ^ this->getMeshDimension().
182  * The origin of \a this will be not touched only spacing and node structure will be changed.
183  * This method can be useful for AMR users.
184  */
185 void MEDCouplingIMesh::refineWithFactor(int factor)
186 {
187   if(factor==0)
188     throw INTERP_KERNEL::Exception("MEDCouplingIMesh::refineWithFactor : refinement factor must be != 0 !");
189   checkCoherency();
190   int factAbs(std::abs(factor));
191   double fact2(1./(double)factor);
192   std::transform(_structure,_structure+_space_dim,_structure,std::bind2nd(std::plus<int>(),-1));
193   std::transform(_structure,_structure+_space_dim,_structure,std::bind2nd(std::multiplies<int>(),factAbs));
194   std::transform(_structure,_structure+_space_dim,_structure,std::bind2nd(std::plus<int>(),1));
195   std::transform(_dxyz,_dxyz+_space_dim,_dxyz,std::bind2nd(std::multiplies<double>(),fact2));
196   declareAsNew();
197 }
198
199 /*!
200  * This static method is useful to condense field on cells of a MEDCouplingIMesh instance coming from a refinement ( MEDCouplingIMesh::refineWithFactor for example)
201  * to a coarse MEDCouplingIMesh instance. So this method can be seen as a specialization in P0P0 conservative interpolation non overlaping from fine image mesh
202  * to a coarse image mesh. Only tuples ( deduced from \a fineLocInCoarse ) of \a coarseDA will be modified. Other tuples of \a coarseDA will be let unchanged.
203  *
204  * \param [in,out] coarseDA The DataArrayDouble corresponding to the a cell field of a coarse mesh whose cell structure is defined by \a coarseSt.
205  * \param [in] coarseSt The cell structure of coarse mesh.
206  * \param [in] fineDA The DataArray containing the cell field on uniformly refined mesh
207  * \param [in] fineLocInCoarse The cell localization of refined mesh into the coarse one.
208  */
209 void MEDCouplingIMesh::CondenseFineToCoarse(DataArrayDouble *coarseDA, const std::vector<int>& coarseSt, const DataArrayDouble *fineDA, const std::vector< std::pair<int,int> >& fineLocInCoarse)
210 {
211   if(!coarseDA || !coarseDA->isAllocated() || !fineDA || !fineDA->isAllocated())
212     throw INTERP_KERNEL::Exception("MEDCouplingIMesh::CondenseFineToCoarse : the parameters 1 or 3 are NULL or not allocated !");
213   int meshDim((int)coarseSt.size()),nbOfTuplesInCoarseExp(MEDCouplingStructuredMesh::DeduceNumberOfGivenStructure(coarseSt)),nbOfTuplesInFineExp(MEDCouplingStructuredMesh::DeduceNumberOfGivenRangeInCompactFrmt(fineLocInCoarse));
214   int nbCompo(fineDA->getNumberOfComponents());
215   if(coarseDA->getNumberOfComponents()!=nbCompo)
216     throw INTERP_KERNEL::Exception("MEDCouplingIMesh::CondenseFineToCoarse : the number of components of fine DA and coarse one mismatches !");
217   if(meshDim!=(int)fineLocInCoarse.size())
218     throw INTERP_KERNEL::Exception("MEDCouplingIMesh::CondenseFineToCoarse : the size of fineLocInCoarse (4th param) must be equal to the sier of coarseSt (2nd param) !");
219   if(coarseDA->getNumberOfTuples()!=nbOfTuplesInCoarseExp)
220     {
221       std::ostringstream oss; oss << "MEDCouplingIMesh::CondenseFineToCoarse : Expecting " << nbOfTuplesInCoarseExp << " having " << coarseDA->getNumberOfTuples() << " !";
222       throw INTERP_KERNEL::Exception(oss.str().c_str());
223     }
224   int nbTuplesFine(fineDA->getNumberOfTuples());
225   if(nbTuplesFine%nbOfTuplesInFineExp!=0)
226     throw INTERP_KERNEL::Exception("MEDCouplingIMesh::CondenseFineToCoarse : Invalid nb of tuples in fine DataArray regarding its structure !");
227   int factN(nbTuplesFine/nbOfTuplesInFineExp);
228   int fact(FindIntRoot(factN,meshDim));
229   // to improve use jump-iterator. Factorizes with SwitchOnIdsFrom BuildExplicitIdsFrom
230   double *outPtr(coarseDA->getPointer());
231   const double *inPtr(fineDA->begin());
232   //
233   switch(meshDim)
234   {
235     case 2:
236       {
237         std::vector<int> dims(MEDCouplingStructuredMesh::GetDimensionsFromCompactFrmt(fineLocInCoarse));
238         int kk(fineLocInCoarse[0].first+coarseSt[0]*fineLocInCoarse[1].first);
239         for(int j=0;j<dims[1];j++)
240           {
241             for(int jfact=0;jfact<fact;jfact++)
242               {
243                 for(int i=0;i<dims[0];i++)
244                   {
245                     double *loc(outPtr+(kk+i)*nbCompo);
246                     for(int ifact=0;ifact<fact;ifact++,inPtr+=nbCompo)
247                       {
248                         if(jfact!=0 || ifact!=0)
249                           std::transform(inPtr,inPtr+nbCompo,loc,loc,std::plus<double>());
250                         else
251                           std::copy(inPtr,inPtr+nbCompo,loc);
252                       }
253                   }
254               }
255             kk+=coarseSt[0];
256           }
257         break;
258       }
259     default:
260       throw INTERP_KERNEL::Exception("MEDCouplingIMesh::CondenseFineToCoarse : only dimensions 2 supported !");
261   }
262 }
263
264 void MEDCouplingIMesh::setSpaceDimension(int spaceDim)
265 {
266   if(spaceDim==_space_dim)
267     return ;
268   CheckSpaceDimension(spaceDim);
269   _space_dim=spaceDim;
270   declareAsNew();
271 }
272
273 void MEDCouplingIMesh::updateTime() const
274 {
275 }
276
277 std::size_t MEDCouplingIMesh::getHeapMemorySizeWithoutChildren() const
278 {
279   return MEDCouplingStructuredMesh::getHeapMemorySizeWithoutChildren();
280 }
281
282 std::vector<const BigMemoryObject *> MEDCouplingIMesh::getDirectChildren() const
283 {
284   return std::vector<const BigMemoryObject *>();
285 }
286
287 /*!
288  * This method copyies all tiny strings from other (name and components name).
289  * @throw if other and this have not same mesh type.
290  */
291 void MEDCouplingIMesh::copyTinyStringsFrom(const MEDCouplingMesh *other)
292
293   const MEDCouplingIMesh *otherC=dynamic_cast<const MEDCouplingIMesh *>(other);
294   if(!otherC)
295     throw INTERP_KERNEL::Exception("MEDCouplingIMesh::copyTinyStringsFrom : meshes have not same type !");
296   MEDCouplingStructuredMesh::copyTinyStringsFrom(other);
297   declareAsNew();
298 }
299
300 bool MEDCouplingIMesh::isEqualIfNotWhy(const MEDCouplingMesh *other, double prec, std::string& reason) const
301 {
302   if(!other)
303     throw INTERP_KERNEL::Exception("MEDCouplingIMesh::isEqualIfNotWhy : input other pointer is null !");
304   const MEDCouplingIMesh *otherC(dynamic_cast<const MEDCouplingIMesh *>(other));
305   if(!otherC)
306     {
307       reason="mesh given in input is not castable in MEDCouplingIMesh !";
308       return false;
309     }
310   if(!MEDCouplingStructuredMesh::isEqualIfNotWhy(other,prec,reason))
311     return false;
312   if(!isEqualWithoutConsideringStrInternal(otherC,prec,reason))
313     return false;
314   if(_axis_unit!=otherC->_axis_unit)
315     {
316       reason="The units of axis are not the same !";
317       return false;
318     }
319   return true;
320 }
321
322 bool MEDCouplingIMesh::isEqualWithoutConsideringStr(const MEDCouplingMesh *other, double prec) const
323 {
324   const MEDCouplingIMesh *otherC=dynamic_cast<const MEDCouplingIMesh *>(other);
325   if(!otherC)
326     return false;
327   std::string tmp;
328   return isEqualWithoutConsideringStrInternal(other,prec,tmp);
329 }
330
331 bool MEDCouplingIMesh::isEqualWithoutConsideringStrInternal(const MEDCouplingMesh *other, double prec, std::string& reason) const
332 {
333   const MEDCouplingIMesh *otherC=dynamic_cast<const MEDCouplingIMesh *>(other);
334   if(!otherC)
335     return false;
336   if(_space_dim!=otherC->_space_dim)
337     {
338       std::ostringstream oss;
339       oss << "The spaceDimension of this (" << _space_dim << ") is not equal to those of other (" << otherC->_space_dim << ") !";
340       return false;
341     }
342   checkSpaceDimension();
343   for(int i=0;i<_space_dim;i++)
344     {
345       if(fabs(_origin[i]-otherC->_origin[i])>prec)
346         {
347           std::ostringstream oss;
348           oss << "The origin of this and other differs at " << i << " !";
349           reason=oss.str();
350           return false;
351         }
352     }
353   for(int i=0;i<_space_dim;i++)
354     {
355       if(fabs(_dxyz[i]-otherC->_dxyz[i])>prec)
356         {
357           std::ostringstream oss;
358           oss << "The delta of this and other differs at " << i << " !";
359           reason=oss.str();
360           return false;
361         }
362     }
363   for(int i=0;i<_space_dim;i++)
364     {
365       if(_structure[i]!=otherC->_structure[i])
366         {
367           std::ostringstream oss;
368           oss << "The structure of this and other differs at " << i << " !";
369           reason=oss.str();
370           return false;
371         }
372     }
373   return true;
374 }
375
376 void MEDCouplingIMesh::checkDeepEquivalWith(const MEDCouplingMesh *other, int cellCompPol, double prec,
377                                             DataArrayInt *&cellCor, DataArrayInt *&nodeCor) const
378 {
379   if(!isEqualWithoutConsideringStr(other,prec))
380     throw INTERP_KERNEL::Exception("MEDCouplingIMesh::checkDeepEquivalWith : Meshes are not the same !");
381 }
382
383 /*!
384  * Nothing is done here (except to check that the other is a ParaMEDMEM::MEDCouplingIMesh instance too).
385  * The user intend that the nodes are the same, so by construction of ParaMEDMEM::MEDCouplingIMesh, \a this and \a other are the same !
386  */
387 void MEDCouplingIMesh::checkDeepEquivalOnSameNodesWith(const MEDCouplingMesh *other, int cellCompPol, double prec,
388                                                        DataArrayInt *&cellCor) const
389 {
390   if(!isEqualWithoutConsideringStr(other,prec))
391     throw INTERP_KERNEL::Exception("MEDCouplingIMesh::checkDeepEquivalOnSameNodesWith : Meshes are not the same !");
392 }
393
394 void MEDCouplingIMesh::checkCoherency() const
395 {
396   checkSpaceDimension();
397   for(int i=0;i<_space_dim;i++)
398     if(_structure[i]<1)
399       {
400         std::ostringstream oss; oss << "MEDCouplingIMesh::checkCoherency : On axis " << i << "/" << _space_dim << ", number of nodes is equal to " << _structure[i] << " ! must be >=1 !";
401         throw INTERP_KERNEL::Exception(oss.str().c_str());
402       }
403 }
404
405 void MEDCouplingIMesh::checkCoherency1(double eps) const
406 {
407   checkCoherency();
408 }
409
410 void MEDCouplingIMesh::checkCoherency2(double eps) const
411 {
412   checkCoherency1(eps);
413 }
414
415 void MEDCouplingIMesh::getNodeGridStructure(int *res) const
416 {
417   checkSpaceDimension();
418   std::copy(_structure,_structure+_space_dim,res);
419 }
420
421 std::vector<int> MEDCouplingIMesh::getNodeGridStructure() const
422 {
423   checkSpaceDimension();
424   std::vector<int> ret(_structure,_structure+_space_dim);
425   return ret;
426 }
427
428 MEDCouplingStructuredMesh *MEDCouplingIMesh::buildStructuredSubPart(const std::vector< std::pair<int,int> >& cellPart) const
429 {
430   checkCoherency();
431   int dim(getSpaceDimension());
432   if(dim!=(int)cellPart.size())
433     {
434       std::ostringstream oss; oss << "MEDCouplingIMesh::buildStructuredSubPart : the space dimension is " << dim << " and cell part size is " << cellPart.size() << " !";
435       throw INTERP_KERNEL::Exception(oss.str().c_str());
436     }
437   double retOrigin[3]={0.,0.,0.};
438   int retStruct[3]={0,0,0};
439   MEDCouplingAutoRefCountObjectPtr<MEDCouplingIMesh> ret(dynamic_cast<MEDCouplingIMesh *>(deepCpy()));
440   for(int i=0;i<dim;i++)
441     {
442       int startNode(cellPart[i].first),endNode(cellPart[i].second+1);
443       int myDelta(endNode-startNode);
444       if(startNode<0 || startNode>=_structure[i])
445         {
446           std::ostringstream oss; oss << "MEDCouplingIMesh::buildStructuredSubPart : At dimension #" << i << " the start node id is " << startNode << " it should be in [0," << _structure[i] << ") !";
447           throw INTERP_KERNEL::Exception(oss.str().c_str());
448         }
449       if(myDelta<0 || myDelta>_structure[i])
450         {
451           std::ostringstream oss; oss << "MEDCouplingIMesh::buildStructuredSubPart : Along dimension #" << i << " the number of nodes is " << _structure[i] << ", and you are requesting for " << myDelta << " nodes wide range !" << std::endl;
452           throw INTERP_KERNEL::Exception(oss.str().c_str());
453         }
454       retOrigin[i]=_origin[i]+startNode*_dxyz[i];
455       retStruct[i]=myDelta;
456     }
457   ret->setNodeStruct(retStruct,retStruct+dim);
458   ret->setOrigin(retOrigin,retOrigin+dim);
459   ret->checkCoherency();
460   return ret.retn();
461 }
462
463 /*!
464  * Return the space dimension of \a this.
465  */
466 int MEDCouplingIMesh::getSpaceDimension() const
467 {
468   return _space_dim;
469 }
470
471 void MEDCouplingIMesh::getCoordinatesOfNode(int nodeId, std::vector<double>& coo) const
472 {
473   int tmp[3];
474   int spaceDim(getSpaceDimension());
475   getSplitNodeValues(tmp);
476   int tmp2[3];
477   GetPosFromId(nodeId,spaceDim,tmp,tmp2);
478   for(int j=0;j<spaceDim;j++)
479     coo.push_back(_origin[j]+_dxyz[j]*tmp2[j]);
480 }
481
482 std::string MEDCouplingIMesh::simpleRepr() const
483 {
484   std::ostringstream ret;
485   ret << "Image grid with name : \"" << getName() << "\"\n";
486   ret << "Description of mesh : \"" << getDescription() << "\"\n";
487   int tmpp1,tmpp2;
488   double tt(getTime(tmpp1,tmpp2));
489   int spaceDim(_space_dim);
490   ret << "Time attached to the mesh [unit] : " << tt << " [" << getTimeUnit() << "]\n";
491   ret << "Iteration : " << tmpp1  << " Order : " << tmpp2 << "\n";
492   ret << "Space dimension : " << spaceDim << "\n";
493   if(spaceDim<0 || spaceDim>3)
494     return ret.str();
495   ret << "The nodal structure is : "; std::copy(_structure,_structure+spaceDim,std::ostream_iterator<int>(ret," ")); ret << "\n";
496   ret << "The origin position is [" << _axis_unit << "]: ";
497   std::copy(_origin,_origin+spaceDim,std::ostream_iterator<double>(ret," ")); ret << "\n";
498   ret << "The intervals along axis are : ";
499   std::copy(_dxyz,_dxyz+spaceDim,std::ostream_iterator<double>(ret," ")); ret << "\n";
500   return ret.str();
501 }
502
503 std::string MEDCouplingIMesh::advancedRepr() const
504 {
505   return simpleRepr();
506 }
507
508 void MEDCouplingIMesh::getBoundingBox(double *bbox) const
509 {
510   checkCoherency();
511   int dim(getSpaceDimension());
512   for(int idim=0; idim<dim; idim++)
513     {
514       bbox[2*idim]=_origin[idim];
515       bbox[2*idim+1]=_origin[idim]+_dxyz[idim]*_structure[idim];
516     }
517 }
518
519 /*!
520  * Returns a new MEDCouplingFieldDouble containing volumes of cells constituting \a this
521  * mesh.<br>
522  * For 1D cells, the returned field contains lengths.<br>
523  * For 2D cells, the returned field contains areas.<br>
524  * For 3D cells, the returned field contains volumes.
525  *  \param [in] isAbs - a not used parameter.
526  *  \return MEDCouplingFieldDouble * - a new instance of MEDCouplingFieldDouble on cells
527  *         and one time . The caller is to delete this field using decrRef() as it is no
528  *         more needed.
529  */
530 MEDCouplingFieldDouble *MEDCouplingIMesh::getMeasureField(bool isAbs) const
531 {
532   checkCoherency();
533   std::string name="MeasureOfMesh_";
534   name+=getName();
535   int nbelem(getNumberOfCells());
536   MEDCouplingFieldDouble *field(MEDCouplingFieldDouble::New(ON_CELLS,ONE_TIME));
537   field->setName(name);
538   DataArrayDouble* array(DataArrayDouble::New());
539   array->alloc(nbelem,1);
540   array->fillWithValue(getMeasureOfAnyCell());
541   field->setArray(array) ;
542   array->decrRef();
543   field->setMesh(const_cast<MEDCouplingIMesh *>(this));
544   field->synchronizeTimeWithMesh();
545   return field;
546 }
547
548 /*!
549  * not implemented yet !
550  */
551 MEDCouplingFieldDouble *MEDCouplingIMesh::getMeasureFieldOnNode(bool isAbs) const
552 {
553   throw INTERP_KERNEL::Exception("MEDCouplingIMesh::getMeasureFieldOnNode : not implemented yet !");
554   //return 0;
555 }
556
557 int MEDCouplingIMesh::getCellContainingPoint(const double *pos, double eps) const
558 {
559   int dim(getSpaceDimension()),ret(0),coeff(1);
560   for(int i=0;i<dim;i++)
561     {
562       int nbOfCells(_structure[i]-1);
563       double ref(pos[i]);
564       int tmp((int)((ref-_origin[i])/_dxyz[i]));
565       if(tmp>=0 && tmp<nbOfCells)
566         {
567           ret+=coeff*tmp;
568           coeff*=nbOfCells;
569         }
570       else
571         return -1;
572     }
573   return ret;
574 }
575
576 void MEDCouplingIMesh::rotate(const double *center, const double *vector, double angle)
577 {
578   throw INTERP_KERNEL::Exception("No rotation available on IMesh : Traduce it to unstructured mesh to apply it !");
579 }
580
581 /*!
582  * Translates all nodes of \a this mesh by a given vector. Actually, it adds each
583  * component of the \a vector to all node coordinates of a corresponding axis.
584  *  \param [in] vector - the translation vector whose size must be not less than \a
585  *         this->getSpaceDimension().
586  */
587 void MEDCouplingIMesh::translate(const double *vector)
588 {
589   checkSpaceDimension();
590   int dim(getSpaceDimension());
591   std::transform(_origin,_origin+dim,vector,_origin,std::plus<double>());
592   declareAsNew();
593 }
594
595 /*!
596  * Applies scaling transformation to all nodes of \a this mesh.
597  *  \param [in] point - coordinates of a scaling center. This array is to be of
598  *         size \a this->getSpaceDimension() at least.
599  *  \param [in] factor - a scale factor.
600  */
601 void MEDCouplingIMesh::scale(const double *point, double factor)
602 {
603   checkSpaceDimension();
604   int dim(getSpaceDimension());
605   std::transform(_origin,_origin+dim,point,_origin,std::minus<double>());
606   std::transform(_origin,_origin+dim,_origin,std::bind2nd(std::multiplies<double>(),factor));
607   std::transform(_dxyz,_dxyz+dim,_dxyz,std::bind2nd(std::multiplies<double>(),factor));
608   std::transform(_origin,_origin+dim,point,_origin,std::plus<double>());
609   declareAsNew();
610 }
611
612 MEDCouplingMesh *MEDCouplingIMesh::mergeMyselfWith(const MEDCouplingMesh *other) const
613 {
614   //not implemented yet !
615   return 0;
616 }
617
618 /*!
619  * Returns a new DataArrayDouble holding coordinates of all nodes of \a this mesh.
620  *  \return DataArrayDouble * - a new instance of DataArrayDouble, of size \a
621  *          this->getNumberOfNodes() tuples per \a this->getSpaceDimension()
622  *          components. The caller is to delete this array using decrRef() as it is
623  *          no more needed.
624  */
625 DataArrayDouble *MEDCouplingIMesh::getCoordinatesAndOwner() const
626 {
627   checkCoherency();
628   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret(DataArrayDouble::New());
629   int spaceDim(getSpaceDimension()),nbNodes(getNumberOfNodes());
630   ret->alloc(nbNodes,spaceDim);
631   double *pt(ret->getPointer());
632   ret->setInfoOnComponents(buildInfoOnComponents());
633   int tmp2[3],tmp[3];
634   getSplitNodeValues(tmp);
635   for(int i=0;i<nbNodes;i++)
636     {
637       GetPosFromId(i,spaceDim,tmp,tmp2);
638       for(int j=0;j<spaceDim;j++)
639         pt[i*spaceDim+j]=_dxyz[j]*tmp2[j]+_origin[j];
640     }
641   return ret.retn();
642 }
643
644 /*!
645  * Returns a new DataArrayDouble holding barycenters of all cells. The barycenter is
646  * computed by averaging coordinates of cell nodes.
647  *  \return DataArrayDouble * - a new instance of DataArrayDouble, of size \a
648  *          this->getNumberOfCells() tuples per \a this->getSpaceDimension()
649  *          components. The caller is to delete this array using decrRef() as it is
650  *          no more needed.
651  */
652 DataArrayDouble *MEDCouplingIMesh::getBarycenterAndOwner() const
653 {
654   checkCoherency();
655   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret(DataArrayDouble::New());
656   int spaceDim(getSpaceDimension()),nbCells(getNumberOfCells()),tmp[3],tmp2[3];
657   ret->alloc(nbCells,spaceDim);
658   double *pt(ret->getPointer()),shiftOrigin[3];
659   std::transform(_dxyz,_dxyz+spaceDim,shiftOrigin,std::bind2nd(std::multiplies<double>(),0.5));
660   std::transform(_origin,_origin+spaceDim,shiftOrigin,shiftOrigin,std::plus<double>());
661   getSplitCellValues(tmp);
662   ret->setInfoOnComponents(buildInfoOnComponents());
663   for(int i=0;i<nbCells;i++)
664     {
665       GetPosFromId(i,spaceDim,tmp,tmp2);
666       for(int j=0;j<spaceDim;j++)
667         pt[i*spaceDim+j]=_dxyz[j]*tmp2[j]+shiftOrigin[j];
668     }
669   return ret.retn();
670 }
671
672 DataArrayDouble *MEDCouplingIMesh::computeIsoBarycenterOfNodesPerCell() const
673 {
674   return MEDCouplingIMesh::getBarycenterAndOwner();
675 }
676
677 void MEDCouplingIMesh::renumberCells(const int *old2NewBg, bool check)
678 {
679   throw INTERP_KERNEL::Exception("Functionnality of renumbering cell not available for IMesh !");
680 }
681
682 void MEDCouplingIMesh::getTinySerializationInformation(std::vector<double>& tinyInfoD, std::vector<int>& tinyInfo, std::vector<std::string>& littleStrings) const
683 {
684   int it,order;
685   double time(getTime(it,order));
686   tinyInfo.clear();
687   tinyInfoD.clear();
688   littleStrings.clear();
689   littleStrings.push_back(getName());
690   littleStrings.push_back(getDescription());
691   littleStrings.push_back(getTimeUnit());
692   littleStrings.push_back(getAxisUnit());
693   tinyInfo.push_back(it);
694   tinyInfo.push_back(order);
695   tinyInfo.push_back(_space_dim);
696   tinyInfo.insert(tinyInfo.end(),_structure,_structure+3);
697   tinyInfoD.push_back(time);
698   tinyInfoD.insert(tinyInfoD.end(),_dxyz,_dxyz+3);
699   tinyInfoD.insert(tinyInfoD.end(),_origin,_origin+3);
700 }
701
702 void MEDCouplingIMesh::resizeForUnserialization(const std::vector<int>& tinyInfo, DataArrayInt *a1, DataArrayDouble *a2, std::vector<std::string>& littleStrings) const
703 {
704   a1->alloc(0,1);
705   a2->alloc(0,1);
706 }
707
708 void MEDCouplingIMesh::serialize(DataArrayInt *&a1, DataArrayDouble *&a2) const
709 {
710   a1=DataArrayInt::New();
711   a1->alloc(0,1);
712   a2=DataArrayDouble::New();
713   a2->alloc(0,1);
714 }
715
716 void MEDCouplingIMesh::unserialization(const std::vector<double>& tinyInfoD, const std::vector<int>& tinyInfo, const DataArrayInt *a1, DataArrayDouble *a2,
717                                        const std::vector<std::string>& littleStrings)
718 {
719   setName(littleStrings[0]);
720   setDescription(littleStrings[1]);
721   setTimeUnit(littleStrings[2]);
722   setAxisUnit(littleStrings[3]);
723   setTime(tinyInfoD[0],tinyInfo[0],tinyInfo[1]);
724   _space_dim=tinyInfo[2];
725   _structure[0]=tinyInfo[3]; _structure[1]=tinyInfo[4]; _structure[2]=tinyInfo[5];
726   _dxyz[0]=tinyInfoD[1]; _dxyz[1]=tinyInfoD[2]; _dxyz[2]=tinyInfoD[3];
727   _origin[0]=tinyInfoD[4]; _origin[1]=tinyInfoD[5]; _origin[2]=tinyInfoD[6];
728   declareAsNew();
729 }
730
731 void MEDCouplingIMesh::writeVTKLL(std::ostream& ofs, const std::string& cellData, const std::string& pointData, DataArrayByte *byteData) const
732 {
733   checkCoherency();
734   std::ostringstream extent,origin,spacing;
735   for(int i=0;i<3;i++)
736     {
737       if(i<_space_dim)
738         { extent << "0 " <<  _structure[i]-1 << " "; origin << _origin[i] << " "; spacing << _dxyz[i] << " "; }
739       else
740         { extent << "0 0 "; origin << "0 "; spacing << "0 "; }
741     }
742   ofs << "  <" << getVTKDataSetType() << " WholeExtent=\"" << extent.str() << "\" Origin=\"" << origin.str() << "\" Spacing=\"" << spacing.str() << "\">\n";
743   ofs << "    <Piece Extent=\"" << extent.str() << "\">\n";
744   ofs << "      <PointData>\n" << pointData << std::endl;
745   ofs << "      </PointData>\n";
746   ofs << "      <CellData>\n" << cellData << std::endl;
747   ofs << "      </CellData>\n";
748   ofs << "      <Coordinates>\n";
749   ofs << "      </Coordinates>\n";
750   ofs << "    </Piece>\n";
751   ofs << "  </" << getVTKDataSetType() << ">\n";
752 }
753
754 void MEDCouplingIMesh::reprQuickOverview(std::ostream& stream) const
755 {
756   stream << "MEDCouplingIMesh C++ instance at " << this << ". Name : \"" << getName() << "\". Space dimension : " << _space_dim << ".";
757   if(_space_dim<0 || _space_dim>3)
758     return ;
759   stream << "\n";
760   std::ostringstream stream0,stream1;
761   int nbNodes(1),nbCells(0);
762   bool isPb(false);
763   for(int i=0;i<_space_dim;i++)
764     {
765       char tmp('X'+i);
766       int tmpNodes(_structure[i]);
767       stream1 << "- Axis " << tmp << " : " << tmpNodes << " nodes (orig=" << _origin[i] << ", inter=" << _dxyz[i] << ").";
768       if(i!=_space_dim-1)
769         stream1 << std::endl;
770       if(tmpNodes>=1)
771         nbNodes*=tmpNodes;
772       else
773         isPb=true;
774       if(tmpNodes>=2)
775         nbCells=nbCells==0?tmpNodes-1:nbCells*(tmpNodes-1);
776     }
777   if(!isPb)
778     {
779       stream0 << "Number of cells : " << nbCells << ", Number of nodes : " << nbNodes;
780       stream << stream0.str();
781       if(_space_dim>0)
782         stream << std::endl;
783     }
784   stream << stream1.str();
785 }
786
787 std::string MEDCouplingIMesh::getVTKDataSetType() const
788 {
789   return std::string("ImageData");
790 }
791
792 std::vector<std::string> MEDCouplingIMesh::buildInfoOnComponents() const
793 {
794   checkSpaceDimension();
795   int dim(getSpaceDimension());
796   std::vector<std::string> ret(dim);
797   for(int i=0;i<dim;i++)
798     {
799       std::ostringstream oss;
800       char tmp('X'+i); oss << tmp;
801       ret[i]=DataArray::BuildInfoFromVarAndUnit(oss.str(),_axis_unit);
802     }
803   return ret;
804 }
805
806 void MEDCouplingIMesh::checkSpaceDimension() const
807 {
808   CheckSpaceDimension(_space_dim);
809 }
810
811 void MEDCouplingIMesh::CheckSpaceDimension(int spaceDim)
812 {
813   if(spaceDim<0 || spaceDim>3)
814     throw INTERP_KERNEL::Exception("MEDCouplingIMesh::CheckSpaceDimension : input spaceDim must be in [0,1,2,3] !");
815 }
816
817 int MEDCouplingIMesh::FindIntRoot(int val, int order)
818 {
819   if(order==0)
820     return 1;
821   if(val<0)
822     throw INTERP_KERNEL::Exception("MEDCouplingIMesh::FindIntRoot : input val is < 0 ! Not possible to compute a root !");
823   if(order==1)
824     return val;
825   if(order!=2 && order!=3)
826     throw INTERP_KERNEL::Exception("MEDCouplingIMesh::FindIntRoot : the order available are 0,1,2 or 3 !");
827   double valf((double)val);
828   if(order==2)
829     {
830       double retf(sqrt(valf));
831       int ret((int)retf);
832       if(ret*ret!=val)
833         throw INTERP_KERNEL::Exception("MEDCouplingIMesh::FindIntRoot : the input val is not a perfect square root !");
834       return ret;
835     }
836   else//order==3
837     {
838       double retf(std::pow(val,0.3333333333333333));
839       int ret((int)retf),ret2(ret+1);
840       if(ret*ret*ret!=val || ret2*ret2*ret2!=val)
841         throw INTERP_KERNEL::Exception("MEDCouplingIMesh::FindIntRoot : the input val is not a perfect cublic root !");
842       if(ret*ret*ret==val)
843         return ret;
844       else
845         return ret2;
846     }
847 }