Salome HOME
dc2cf8793494b41f9b1d9ee2f8714ab4d0a34a23
[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& e) { }
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 void MEDCouplingIMesh::setSpaceDimension(int spaceDim)
200 {
201   if(spaceDim==_space_dim)
202     return ;
203   CheckSpaceDimension(spaceDim);
204   _space_dim=spaceDim;
205   declareAsNew();
206 }
207
208 void MEDCouplingIMesh::updateTime() const
209 {
210 }
211
212 std::size_t MEDCouplingIMesh::getHeapMemorySizeWithoutChildren() const
213 {
214   return MEDCouplingStructuredMesh::getHeapMemorySizeWithoutChildren();
215 }
216
217 std::vector<const BigMemoryObject *> MEDCouplingIMesh::getDirectChildren() const
218 {
219   return std::vector<const BigMemoryObject *>();
220 }
221
222 /*!
223  * This method copyies all tiny strings from other (name and components name).
224  * @throw if other and this have not same mesh type.
225  */
226 void MEDCouplingIMesh::copyTinyStringsFrom(const MEDCouplingMesh *other)
227
228   const MEDCouplingIMesh *otherC=dynamic_cast<const MEDCouplingIMesh *>(other);
229   if(!otherC)
230     throw INTERP_KERNEL::Exception("MEDCouplingIMesh::copyTinyStringsFrom : meshes have not same type !");
231   MEDCouplingStructuredMesh::copyTinyStringsFrom(other);
232   declareAsNew();
233 }
234
235 bool MEDCouplingIMesh::isEqualIfNotWhy(const MEDCouplingMesh *other, double prec, std::string& reason) const
236 {
237   if(!other)
238     throw INTERP_KERNEL::Exception("MEDCouplingIMesh::isEqualIfNotWhy : input other pointer is null !");
239   const MEDCouplingIMesh *otherC(dynamic_cast<const MEDCouplingIMesh *>(other));
240   if(!otherC)
241     {
242       reason="mesh given in input is not castable in MEDCouplingIMesh !";
243       return false;
244     }
245   if(!MEDCouplingStructuredMesh::isEqualIfNotWhy(other,prec,reason))
246     return false;
247   if(!isEqualWithoutConsideringStrInternal(otherC,prec,reason))
248     return false;
249   if(_axis_unit!=otherC->_axis_unit)
250     {
251       reason="The units of axis are not the same !";
252       return false;
253     }
254   return true;
255 }
256
257 bool MEDCouplingIMesh::isEqualWithoutConsideringStr(const MEDCouplingMesh *other, double prec) const
258 {
259   const MEDCouplingIMesh *otherC=dynamic_cast<const MEDCouplingIMesh *>(other);
260   if(!otherC)
261     return false;
262   std::string tmp;
263   return isEqualWithoutConsideringStrInternal(other,prec,tmp);
264 }
265
266 bool MEDCouplingIMesh::isEqualWithoutConsideringStrInternal(const MEDCouplingMesh *other, double prec, std::string& reason) const
267 {
268   const MEDCouplingIMesh *otherC=dynamic_cast<const MEDCouplingIMesh *>(other);
269   if(!otherC)
270     return false;
271   if(_space_dim!=otherC->_space_dim)
272     {
273       std::ostringstream oss;
274       oss << "The spaceDimension of this (" << _space_dim << ") is not equal to those of other (" << otherC->_space_dim << ") !";
275       return false;
276     }
277   checkSpaceDimension();
278   for(int i=0;i<_space_dim;i++)
279     {
280       if(fabs(_origin[i]-otherC->_origin[i])>prec)
281         {
282           std::ostringstream oss;
283           oss << "The origin of this and other differs at " << i << " !";
284           reason=oss.str();
285           return false;
286         }
287     }
288   for(int i=0;i<_space_dim;i++)
289     {
290       if(fabs(_dxyz[i]-otherC->_dxyz[i])>prec)
291         {
292           std::ostringstream oss;
293           oss << "The delta of this and other differs at " << i << " !";
294           reason=oss.str();
295           return false;
296         }
297     }
298   for(int i=0;i<_space_dim;i++)
299     {
300       if(_structure[i]!=otherC->_structure[i])
301         {
302           std::ostringstream oss;
303           oss << "The structure of this and other differs at " << i << " !";
304           reason=oss.str();
305           return false;
306         }
307     }
308   return true;
309 }
310
311 void MEDCouplingIMesh::checkDeepEquivalWith(const MEDCouplingMesh *other, int cellCompPol, double prec,
312                                             DataArrayInt *&cellCor, DataArrayInt *&nodeCor) const
313 {
314   if(!isEqualWithoutConsideringStr(other,prec))
315     throw INTERP_KERNEL::Exception("MEDCouplingIMesh::checkDeepEquivalWith : Meshes are not the same !");
316 }
317
318 /*!
319  * Nothing is done here (except to check that the other is a ParaMEDMEM::MEDCouplingIMesh instance too).
320  * The user intend that the nodes are the same, so by construction of ParaMEDMEM::MEDCouplingIMesh, \a this and \a other are the same !
321  */
322 void MEDCouplingIMesh::checkDeepEquivalOnSameNodesWith(const MEDCouplingMesh *other, int cellCompPol, double prec,
323                                                        DataArrayInt *&cellCor) const
324 {
325   if(!isEqualWithoutConsideringStr(other,prec))
326     throw INTERP_KERNEL::Exception("MEDCouplingIMesh::checkDeepEquivalOnSameNodesWith : Meshes are not the same !");
327 }
328
329 void MEDCouplingIMesh::checkCoherency() const
330 {
331   checkSpaceDimension();
332   for(int i=0;i<_space_dim;i++)
333     if(_structure[i]<1)
334       {
335         std::ostringstream oss; oss << "MEDCouplingIMesh::checkCoherency : On axis " << i << "/" << _space_dim << ", number of nodes is equal to " << _structure[i] << " ! must be >=1 !";
336         throw INTERP_KERNEL::Exception(oss.str().c_str());
337       }
338 }
339
340 void MEDCouplingIMesh::checkCoherency1(double eps) const
341 {
342   checkCoherency();
343 }
344
345 void MEDCouplingIMesh::checkCoherency2(double eps) const
346 {
347   checkCoherency1(eps);
348 }
349
350 void MEDCouplingIMesh::getSplitCellValues(int *res) const
351 {
352   int meshDim(getMeshDimension());
353   for(int l=0;l<meshDim;l++)
354     {
355       int val=1;
356       for(int p=0;p<meshDim-l-1;p++)
357         val*=_structure[p]-1;
358       res[meshDim-l-1]=val;
359     }
360 }
361
362 void MEDCouplingIMesh::getSplitNodeValues(int *res) const
363 {
364   int spaceDim(getSpaceDimension());
365   for(int l=0;l<spaceDim;l++)
366     {
367       int val=1;
368       for(int p=0;p<spaceDim-l-1;p++)
369         val*=_structure[p];
370       res[spaceDim-l-1]=val;
371     }
372 }
373
374 void MEDCouplingIMesh::getNodeGridStructure(int *res) const
375 {
376   checkSpaceDimension();
377   std::copy(_structure,_structure+_space_dim,res);
378 }
379
380 std::vector<int> MEDCouplingIMesh::getNodeGridStructure() const
381 {
382   checkSpaceDimension();
383   std::vector<int> ret(_structure,_structure+_space_dim);
384   return ret;
385 }
386
387 MEDCouplingStructuredMesh *MEDCouplingIMesh::buildStructuredSubPart(const std::vector< std::pair<int,int> >& cellPart) const
388 {
389   checkCoherency();
390   int dim(getSpaceDimension());
391   if(dim!=(int)cellPart.size())
392     {
393       std::ostringstream oss; oss << "MEDCouplingIMesh::buildStructuredSubPart : the space dimension is " << dim << " and cell part size is " << cellPart.size() << " !";
394       throw INTERP_KERNEL::Exception(oss.str().c_str());
395     }
396   double retOrigin[3]={0.,0.,0.};
397   int retStruct[3]={0,0,0};
398   MEDCouplingAutoRefCountObjectPtr<MEDCouplingIMesh> ret(dynamic_cast<MEDCouplingIMesh *>(deepCpy()));
399   for(int i=0;i<dim;i++)
400     {
401       int startNode(cellPart[i].first),endNode(cellPart[i].second+1);
402       int myDelta(endNode-startNode);
403       if(startNode<0 || startNode>=_structure[i])
404         {
405           std::ostringstream oss; oss << "MEDCouplingIMesh::buildStructuredSubPart : At dimension #" << i << " the start node id is " << startNode << " it should be in [0," << _structure[i] << ") !";
406           throw INTERP_KERNEL::Exception(oss.str().c_str());
407         }
408       if(myDelta<0 || myDelta>_structure[i])
409         {
410           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;
411           throw INTERP_KERNEL::Exception(oss.str().c_str());
412         }
413       retOrigin[i]=_origin[i]+startNode*_dxyz[i];
414       retStruct[i]=myDelta;
415     }
416   ret->setNodeStruct(retStruct,retStruct+dim);
417   ret->setOrigin(retOrigin,retOrigin+dim);
418   ret->checkCoherency();
419   return ret.retn();
420 }
421
422 /*!
423  * Return the space dimension of \a this.
424  */
425 int MEDCouplingIMesh::getSpaceDimension() const
426 {
427   return _space_dim;
428 }
429
430 void MEDCouplingIMesh::getCoordinatesOfNode(int nodeId, std::vector<double>& coo) const
431 {
432   int tmp[3];
433   int spaceDim(getSpaceDimension());
434   getSplitNodeValues(tmp);
435   int tmp2[3];
436   GetPosFromId(nodeId,spaceDim,tmp,tmp2);
437   for(int j=0;j<spaceDim;j++)
438     coo.push_back(_origin[j]+_dxyz[j]*tmp2[j]);
439 }
440
441 std::string MEDCouplingIMesh::simpleRepr() const
442 {
443   std::ostringstream ret;
444   ret << "Image grid with name : \"" << getName() << "\"\n";
445   ret << "Description of mesh : \"" << getDescription() << "\"\n";
446   int tmpp1,tmpp2;
447   double tt(getTime(tmpp1,tmpp2));
448   int spaceDim(_space_dim);
449   ret << "Time attached to the mesh [unit] : " << tt << " [" << getTimeUnit() << "]\n";
450   ret << "Iteration : " << tmpp1  << " Order : " << tmpp2 << "\n";
451   ret << "Space dimension : " << spaceDim << "\n";
452   if(spaceDim<0 || spaceDim>3)
453     return ret.str();
454   ret << "The nodal structure is : "; std::copy(_structure,_structure+spaceDim,std::ostream_iterator<int>(ret," ")); ret << "\n";
455   ret << "The origin position is [" << _axis_unit << "]: ";
456   std::copy(_origin,_origin+spaceDim,std::ostream_iterator<double>(ret," ")); ret << "\n";
457   ret << "The intervals along axis are : ";
458   std::copy(_dxyz,_dxyz+spaceDim,std::ostream_iterator<double>(ret," ")); ret << "\n";
459   return ret.str();
460 }
461
462 std::string MEDCouplingIMesh::advancedRepr() const
463 {
464   return simpleRepr();
465 }
466
467 void MEDCouplingIMesh::getBoundingBox(double *bbox) const
468 {
469   checkCoherency();
470   int dim(getSpaceDimension());
471   for(int idim=0; idim<dim; idim++)
472     {
473       bbox[2*idim]=_origin[idim];
474       bbox[2*idim+1]=_origin[idim]+_dxyz[idim]*_structure[idim];
475     }
476 }
477
478 /*!
479  * Returns a new MEDCouplingFieldDouble containing volumes of cells constituting \a this
480  * mesh.<br>
481  * For 1D cells, the returned field contains lengths.<br>
482  * For 2D cells, the returned field contains areas.<br>
483  * For 3D cells, the returned field contains volumes.
484  *  \param [in] isAbs - a not used parameter.
485  *  \return MEDCouplingFieldDouble * - a new instance of MEDCouplingFieldDouble on cells
486  *         and one time . The caller is to delete this field using decrRef() as it is no
487  *         more needed.
488  */
489 MEDCouplingFieldDouble *MEDCouplingIMesh::getMeasureField(bool isAbs) const
490 {
491   checkCoherency();
492   std::string name="MeasureOfMesh_";
493   name+=getName();
494   int nbelem(getNumberOfCells());
495   MEDCouplingFieldDouble *field(MEDCouplingFieldDouble::New(ON_CELLS,ONE_TIME));
496   field->setName(name);
497   DataArrayDouble* array(DataArrayDouble::New());
498   array->alloc(nbelem,1);
499   array->fillWithValue(getMeasureOfAnyCell());
500   field->setArray(array) ;
501   array->decrRef();
502   field->setMesh(const_cast<MEDCouplingIMesh *>(this));
503   field->synchronizeTimeWithMesh();
504   return field;
505 }
506
507 /*!
508  * not implemented yet !
509  */
510 MEDCouplingFieldDouble *MEDCouplingIMesh::getMeasureFieldOnNode(bool isAbs) const
511 {
512   throw INTERP_KERNEL::Exception("MEDCouplingIMesh::getMeasureFieldOnNode : not implemented yet !");
513   //return 0;
514 }
515
516 int MEDCouplingIMesh::getCellContainingPoint(const double *pos, double eps) const
517 {
518   int dim(getSpaceDimension()),ret(0),coeff(1);
519   for(int i=0;i<dim;i++)
520     {
521       int nbOfCells(_structure[i]-1);
522       double ref(pos[i]);
523       int tmp((ref-_origin[i])/_dxyz[i]);
524       if(tmp>=0 && tmp<nbOfCells)
525         {
526           ret+=coeff*tmp;
527           coeff*=nbOfCells;
528         }
529       else
530         return -1;
531     }
532   return ret;
533 }
534
535 void MEDCouplingIMesh::rotate(const double *center, const double *vector, double angle)
536 {
537   throw INTERP_KERNEL::Exception("No rotation available on IMesh : Traduce it to unstructured mesh to apply it !");
538 }
539
540 /*!
541  * Translates all nodes of \a this mesh by a given vector. Actually, it adds each
542  * component of the \a vector to all node coordinates of a corresponding axis.
543  *  \param [in] vector - the translation vector whose size must be not less than \a
544  *         this->getSpaceDimension().
545  */
546 void MEDCouplingIMesh::translate(const double *vector)
547 {
548   checkSpaceDimension();
549   int dim(getSpaceDimension());
550   std::transform(_origin,_origin+dim,vector,_origin,std::plus<double>());
551   declareAsNew();
552 }
553
554 /*!
555  * Applies scaling transformation to all nodes of \a this mesh.
556  *  \param [in] point - coordinates of a scaling center. This array is to be of
557  *         size \a this->getSpaceDimension() at least.
558  *  \param [in] factor - a scale factor.
559  */
560 void MEDCouplingIMesh::scale(const double *point, double factor)
561 {
562   checkSpaceDimension();
563   int dim(getSpaceDimension());
564   std::transform(_origin,_origin+dim,point,_origin,std::minus<double>());
565   std::transform(_origin,_origin+dim,_origin,std::bind2nd(std::multiplies<double>(),factor));
566   std::transform(_dxyz,_dxyz+dim,_dxyz,std::bind2nd(std::multiplies<double>(),factor));
567   std::transform(_origin,_origin+dim,point,_origin,std::plus<double>());
568   declareAsNew();
569 }
570
571 MEDCouplingMesh *MEDCouplingIMesh::mergeMyselfWith(const MEDCouplingMesh *other) const
572 {
573   //not implemented yet !
574   return 0;
575 }
576
577 /*!
578  * Returns a new DataArrayDouble holding coordinates of all nodes of \a this mesh.
579  *  \return DataArrayDouble * - a new instance of DataArrayDouble, of size \a
580  *          this->getNumberOfNodes() tuples per \a this->getSpaceDimension()
581  *          components. The caller is to delete this array using decrRef() as it is
582  *          no more needed.
583  */
584 DataArrayDouble *MEDCouplingIMesh::getCoordinatesAndOwner() const
585 {
586   checkCoherency();
587   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret(DataArrayDouble::New());
588   int spaceDim(getSpaceDimension()),nbNodes(getNumberOfNodes());
589   ret->alloc(nbNodes,spaceDim);
590   double *pt(ret->getPointer());
591   ret->setInfoOnComponents(buildInfoOnComponents());
592   int tmp2[3],tmp[3];
593   getSplitNodeValues(tmp);
594   for(int i=0;i<nbNodes;i++)
595     {
596       GetPosFromId(i,spaceDim,tmp,tmp2);
597       for(int j=0;j<spaceDim;j++)
598         pt[i*spaceDim+j]=_dxyz[j]*tmp2[j]+_origin[j];
599     }
600   return ret.retn();
601 }
602
603 /*!
604  * Returns a new DataArrayDouble holding barycenters of all cells. The barycenter is
605  * computed by averaging coordinates of cell nodes.
606  *  \return DataArrayDouble * - a new instance of DataArrayDouble, of size \a
607  *          this->getNumberOfCells() tuples per \a this->getSpaceDimension()
608  *          components. The caller is to delete this array using decrRef() as it is
609  *          no more needed.
610  */
611 DataArrayDouble *MEDCouplingIMesh::getBarycenterAndOwner() const
612 {
613   checkCoherency();
614   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret(DataArrayDouble::New());
615   int spaceDim(getSpaceDimension()),nbCells(getNumberOfCells()),tmp[3],tmp2[3];
616   ret->alloc(nbCells,spaceDim);
617   double *pt(ret->getPointer()),shiftOrigin[3];
618   std::transform(_dxyz,_dxyz+spaceDim,shiftOrigin,std::bind2nd(std::multiplies<double>(),0.5));
619   std::transform(_origin,_origin+spaceDim,shiftOrigin,shiftOrigin,std::plus<double>());
620   getSplitCellValues(tmp);
621   ret->setInfoOnComponents(buildInfoOnComponents());
622   for(int i=0;i<nbCells;i++)
623     {
624       GetPosFromId(i,spaceDim,tmp,tmp2);
625       for(int j=0;j<spaceDim;j++)
626         pt[i*spaceDim+j]=_dxyz[j]*tmp2[j]+shiftOrigin[j];
627     }
628   return ret.retn();
629 }
630
631 DataArrayDouble *MEDCouplingIMesh::computeIsoBarycenterOfNodesPerCell() const
632 {
633   return MEDCouplingIMesh::getBarycenterAndOwner();
634 }
635
636 void MEDCouplingIMesh::renumberCells(const int *old2NewBg, bool check)
637 {
638   throw INTERP_KERNEL::Exception("Functionnality of renumbering cell not available for IMesh !");
639 }
640
641 void MEDCouplingIMesh::getTinySerializationInformation(std::vector<double>& tinyInfoD, std::vector<int>& tinyInfo, std::vector<std::string>& littleStrings) const
642 {
643   int it,order;
644   double time(getTime(it,order));
645   tinyInfo.clear();
646   tinyInfoD.clear();
647   littleStrings.clear();
648   littleStrings.push_back(getName());
649   littleStrings.push_back(getDescription());
650   littleStrings.push_back(getTimeUnit());
651   littleStrings.push_back(getAxisUnit());
652   tinyInfo.push_back(it);
653   tinyInfo.push_back(order);
654   tinyInfo.push_back(_space_dim);
655   tinyInfo.insert(tinyInfo.end(),_structure,_structure+3);
656   tinyInfoD.push_back(time);
657   tinyInfoD.insert(tinyInfoD.end(),_dxyz,_dxyz+3);
658   tinyInfoD.insert(tinyInfoD.end(),_origin,_origin+3);
659 }
660
661 void MEDCouplingIMesh::resizeForUnserialization(const std::vector<int>& tinyInfo, DataArrayInt *a1, DataArrayDouble *a2, std::vector<std::string>& littleStrings) const
662 {
663   a1->alloc(0,1);
664   a2->alloc(0,1);
665 }
666
667 void MEDCouplingIMesh::serialize(DataArrayInt *&a1, DataArrayDouble *&a2) const
668 {
669   a1=DataArrayInt::New();
670   a1->alloc(0,1);
671   a2=DataArrayDouble::New();
672   a2->alloc(0,1);
673 }
674
675 void MEDCouplingIMesh::unserialization(const std::vector<double>& tinyInfoD, const std::vector<int>& tinyInfo, const DataArrayInt *a1, DataArrayDouble *a2,
676                                        const std::vector<std::string>& littleStrings)
677 {
678   setName(littleStrings[0]);
679   setDescription(littleStrings[1]);
680   setTimeUnit(littleStrings[2]);
681   setAxisUnit(littleStrings[3]);
682   setTime(tinyInfoD[0],tinyInfo[0],tinyInfo[1]);
683   _space_dim=tinyInfo[2];
684   _structure[0]=tinyInfo[3]; _structure[1]=tinyInfo[4]; _structure[2]=tinyInfo[5];
685   _dxyz[0]=tinyInfoD[1]; _dxyz[1]=tinyInfoD[2]; _dxyz[2]=tinyInfoD[3];
686   _origin[0]=tinyInfoD[4]; _origin[1]=tinyInfoD[5]; _origin[2]=tinyInfoD[6];
687   declareAsNew();
688 }
689
690 void MEDCouplingIMesh::writeVTKLL(std::ostream& ofs, const std::string& cellData, const std::string& pointData, DataArrayByte *byteData) const
691 {
692   checkCoherency();
693   std::ostringstream extent,origin,spacing;
694   for(int i=0;i<3;i++)
695     {
696       if(i<_space_dim)
697         { extent << "0 " <<  _structure[i]-1 << " "; origin << _origin[i] << " "; spacing << _dxyz[i] << " "; }
698       else
699         { extent << "0 0 "; origin << "0 "; spacing << "0 "; }
700     }
701   ofs << "  <" << getVTKDataSetType() << " WholeExtent=\"" << extent.str() << "\" Origin=\"" << origin.str() << "\" Spacing=\"" << spacing.str() << "\">\n";
702   ofs << "    <Piece Extent=\"" << extent.str() << "\">\n";
703   ofs << "      <PointData>\n" << pointData << std::endl;
704   ofs << "      </PointData>\n";
705   ofs << "      <CellData>\n" << cellData << std::endl;
706   ofs << "      </CellData>\n";
707   ofs << "      <Coordinates>\n";
708   ofs << "      </Coordinates>\n";
709   ofs << "    </Piece>\n";
710   ofs << "  </" << getVTKDataSetType() << ">\n";
711 }
712
713 void MEDCouplingIMesh::reprQuickOverview(std::ostream& stream) const
714 {
715   stream << "MEDCouplingIMesh C++ instance at " << this << ". Name : \"" << getName() << "\". Space dimension : " << _space_dim << ".";
716   if(_space_dim<0 || _space_dim>3)
717     return ;
718   stream << "\n";
719   std::ostringstream stream0,stream1;
720   int nbNodes(1),nbCells(0);
721   bool isPb(false);
722   for(int i=0;i<_space_dim;i++)
723     {
724       char tmp('X'+i);
725       int tmpNodes(_structure[i]);
726       stream1 << "- Axis " << tmp << " : " << tmpNodes << " nodes (orig=" << _origin[i] << ", inter=" << _dxyz[i] << ").";
727       if(i!=_space_dim-1)
728         stream1 << std::endl;
729       if(tmpNodes>=1)
730         nbNodes*=tmpNodes;
731       else
732         isPb=true;
733       if(tmpNodes>=2)
734         nbCells=nbCells==0?tmpNodes-1:nbCells*(tmpNodes-1);
735     }
736   if(!isPb)
737     {
738       stream0 << "Number of cells : " << nbCells << ", Number of nodes : " << nbNodes;
739       stream << stream0.str();
740       if(_space_dim>0)
741         stream << std::endl;
742     }
743   stream << stream1.str();
744 }
745
746 std::string MEDCouplingIMesh::getVTKDataSetType() const
747 {
748   return std::string("ImageData");
749 }
750
751 std::vector<std::string> MEDCouplingIMesh::buildInfoOnComponents() const
752 {
753   checkSpaceDimension();
754   int dim(getSpaceDimension());
755   std::vector<std::string> ret(dim);
756   for(int i=0;i<dim;i++)
757     {
758       std::ostringstream oss;
759       char tmp('X'+i); oss << tmp;
760       ret[i]=DataArray::BuildInfoFromVarAndUnit(oss.str(),_axis_unit);
761     }
762   return ret;
763 }
764
765 void MEDCouplingIMesh::checkSpaceDimension() const
766 {
767   CheckSpaceDimension(_space_dim);
768 }
769
770 void MEDCouplingIMesh::CheckSpaceDimension(int spaceDim)
771 {
772   if(spaceDim<0 || spaceDim>3)
773     throw INTERP_KERNEL::Exception("MEDCouplingIMesh::CheckSpaceDimension : input spaceDim must be in [0,1,2,3] !");
774 }
775