Salome HOME
code update due to array modification
[modules/paravis.git] / src / Plugins / VoroGauss / IO / vtkVoroGauss.cxx
1 // Copyright (C) 2017-2019  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 (EDF R&D)
20
21 #include "vtkVoroGauss.h"
22
23 #include "vtkAdjacentVertexIterator.h"
24 #include "vtkIntArray.h"
25 #include "vtkCellData.h"
26 #include "vtkPointData.h"
27 #include "vtkCellType.h"
28 #include "vtkCell.h"
29 #include "vtkCellArray.h"
30 #include "vtkIdTypeArray.h"
31
32 #include "vtkStreamingDemandDrivenPipeline.h"
33 #include "vtkUnstructuredGrid.h"
34 #include  "vtkMultiBlockDataSet.h"
35
36 #include "vtkInformationStringKey.h"
37 #include "vtkAlgorithmOutput.h"
38 #include "vtkObjectFactory.h"
39 #include "vtkMutableDirectedGraph.h"
40 #include "vtkMultiBlockDataSet.h"
41 #include "vtkDataSet.h"
42 #include "vtkInformationVector.h"
43 #include "vtkInformation.h"
44 #include "vtkDataArraySelection.h"
45 #include "vtkTimeStamp.h"
46 #include "vtkInEdgeIterator.h"
47 #include "vtkInformationDataObjectKey.h"
48 #include "vtkInformationDataObjectMetaDataKey.h"
49 #include "vtkInformationDoubleVectorKey.h"
50 #include "vtkExecutive.h"
51 #include "vtkVariantArray.h"
52 #include "vtkStringArray.h"
53 #include "vtkDoubleArray.h"
54 #include "vtkFloatArray.h"
55 #include "vtkCharArray.h"
56 #include "vtkLongArray.h"
57 #include "vtkUnsignedCharArray.h"
58 #include "vtkDataSetAttributes.h"
59 #include "vtkDemandDrivenPipeline.h"
60 #include "vtkDataObjectTreeIterator.h"
61 #include "vtkWarpScalar.h"
62 #include "vtkQuadratureSchemeDefinition.h"
63 #include "vtkInformationQuadratureSchemeDefinitionVectorKey.h"
64 #include "vtkCompositeDataToUnstructuredGridFilter.h"
65 #include "vtkMultiBlockDataGroupFilter.h"
66
67 #include "MEDCouplingMemArray.hxx"
68 #include "MEDCouplingMemArray.txx"
69 #include "MEDCouplingUMesh.hxx"
70 #include "MEDCouplingFieldDouble.hxx"
71 #include "InterpKernelAutoPtr.hxx"
72 #include "InterpKernelGaussCoords.hxx"
73
74 #include <map>
75 #include <set>
76 #include <deque>
77 #include <sstream>
78
79 using MEDCoupling::DataArray;
80 using MEDCoupling::DataArrayInt32;
81 using MEDCoupling::DataArrayInt64;
82 using MEDCoupling::DataArrayDouble;
83 using MEDCoupling::MEDCouplingMesh;
84 using MEDCoupling::MEDCouplingUMesh;
85 using MEDCoupling::DynamicCastSafe;
86 using MEDCoupling::MEDCouplingFieldDouble;
87 using MEDCoupling::ON_GAUSS_PT;
88 using MEDCoupling::MCAuto;
89
90 vtkStandardNewMacro(vtkVoroGauss);
91 ///////////////////
92
93 std::map<int,int> ComputeMapOfType()
94 {
95   std::map<int,int> ret;
96   int nbOfTypesInMC(sizeof(MEDCOUPLING2VTKTYPETRADUCER)/sizeof( decltype(MEDCOUPLING2VTKTYPETRADUCER[0]) ));
97   for(int i=0;i<nbOfTypesInMC;i++)
98     {
99       auto vtkId(MEDCOUPLING2VTKTYPETRADUCER[i]);
100       if(vtkId!=MEDCOUPLING2VTKTYPETRADUCER_NONE)
101         ret[vtkId]=i;
102     }
103   return ret;
104 }
105
106 std::map<int,int> ComputeRevMapOfType()
107 {
108   std::map<int,int> ret;
109   int nbOfTypesInMC(sizeof(MEDCOUPLING2VTKTYPETRADUCER)/sizeof( decltype(MEDCOUPLING2VTKTYPETRADUCER[0]) ));
110   for(int i=0;i<nbOfTypesInMC;i++)
111     {
112       auto vtkId(MEDCOUPLING2VTKTYPETRADUCER[i]);
113       if(vtkId!=MEDCOUPLING2VTKTYPETRADUCER_NONE)
114         ret[i]=vtkId;
115     }
116   return ret;
117 }
118
119 ///////////////////
120
121 vtkInformationDoubleVectorKey *GetMEDReaderMetaDataIfAny()
122 {
123   static const char ZE_KEY[]="vtkMEDReader::GAUSS_DATA";
124   MEDCoupling::GlobalDict *gd(MEDCoupling::GlobalDict::GetInstance());
125   if(!gd->hasKey(ZE_KEY))
126     return 0;
127   std::string ptSt(gd->value(ZE_KEY));
128   void *pt(0);
129   std::istringstream iss(ptSt); iss >> pt;
130   return reinterpret_cast<vtkInformationDoubleVectorKey *>(pt);
131 }
132
133 bool IsInformationOK(vtkInformation *info, std::vector<double>& data)
134 {
135   vtkInformationDoubleVectorKey *key(GetMEDReaderMetaDataIfAny());
136   if(!key)
137     return false;
138   // Check the information contain meta data key
139   if(!info->Has(key))
140     return false;
141   int lgth(key->Length(info));
142   const double *data2(info->Get(key));
143   data.insert(data.end(),data2,data2+lgth);
144   return true;
145 }
146
147 void ExtractInfo(vtkInformationVector *inputVector, vtkUnstructuredGrid *& usgIn)
148 {
149   vtkInformation *inputInfo(inputVector->GetInformationObject(0));
150   vtkDataSet *input(0);
151   vtkDataSet *input0(vtkDataSet::SafeDownCast(inputInfo->Get(vtkDataObject::DATA_OBJECT())));
152   vtkMultiBlockDataSet *input1(vtkMultiBlockDataSet::SafeDownCast(inputInfo->Get(vtkDataObject::DATA_OBJECT())));
153   if(input0)
154     input=input0;
155   else
156     {
157       if(!input1)
158         throw INTERP_KERNEL::Exception("Input dataSet must be a DataSet or single elt multi block dataset expected !");
159       if(input1->GetNumberOfBlocks()!=1)
160         throw INTERP_KERNEL::Exception("Input dataSet is a multiblock dataset with not exactly one block ! Use MergeBlocks or ExtractBlocks filter before calling this filter !");
161       vtkDataObject *input2(input1->GetBlock(0));
162       if(!input2)
163         throw INTERP_KERNEL::Exception("Input dataSet is a multiblock dataset with exactly one block but this single element is NULL !");
164       vtkDataSet *input2c(vtkDataSet::SafeDownCast(input2));
165       if(!input2c)
166         throw INTERP_KERNEL::Exception("Input dataSet is a multiblock dataset with exactly one block but this single element is not a dataset ! Use MergeBlocks or ExtractBlocks filter before calling this filter !");
167       input=input2c;
168     }
169   if(!input)
170     throw INTERP_KERNEL::Exception("Input data set is NULL !");
171   usgIn=vtkUnstructuredGrid::SafeDownCast(input);
172   if(!usgIn)
173     throw INTERP_KERNEL::Exception("Input data set is not an unstructured mesh ! This filter works only on unstructured meshes !");
174 }
175
176 DataArrayIdType *ConvertVTKArrayToMCArrayInt(vtkDataArray *data)
177 {
178   if(!data)
179     throw INTERP_KERNEL::Exception("ConvertVTKArrayToMCArrayInt : internal error !");
180   int nbTuples(data->GetNumberOfTuples()),nbComp(data->GetNumberOfComponents());
181   std::size_t nbElts(nbTuples*nbComp);
182   MCAuto<DataArrayIdType> ret(DataArrayIdType::New());
183   ret->alloc(nbTuples,nbComp);
184   for(int i=0;i<nbComp;i++)
185     {
186       const char *comp(data->GetComponentName(i));
187       if(comp)
188         ret->setInfoOnComponent(i,comp);
189     }
190   mcIdType *ptOut(ret->getPointer());
191   vtkIntArray *d0(vtkIntArray::SafeDownCast(data));
192   if(d0)
193     {
194       const int *pt(d0->GetPointer(0));
195       std::copy(pt,pt+nbElts,ptOut);
196       return ret.retn();
197     }
198   vtkLongArray *d1(vtkLongArray::SafeDownCast(data));
199   if(d1)
200     {
201       const long *pt(d1->GetPointer(0));
202       std::copy(pt,pt+nbElts,ptOut);
203       return ret.retn();
204     }
205   vtkIdTypeArray *d2(vtkIdTypeArray::SafeDownCast(data));
206   if(d2)
207     {
208       const int *pt(d2->GetPointer(0));
209       std::copy(pt,pt+nbElts,ptOut);
210       return ret.retn();
211     }
212   std::ostringstream oss;
213   oss << "ConvertVTKArrayToMCArrayInt : unrecognized array \"" << typeid(*data).name() << "\" type !";
214   throw INTERP_KERNEL::Exception(oss.str());
215 }
216
217 DataArrayDouble *ConvertVTKArrayToMCArrayDouble(vtkDataArray *data)
218 {
219   if(!data)
220     throw INTERP_KERNEL::Exception("ConvertVTKArrayToMCArrayDouble : internal error !");
221   int nbTuples(data->GetNumberOfTuples()),nbComp(data->GetNumberOfComponents());
222   std::size_t nbElts(nbTuples*nbComp);
223   MCAuto<DataArrayDouble> ret(DataArrayDouble::New());
224   ret->alloc(nbTuples,nbComp);
225   for(int i=0;i<nbComp;i++)
226     {
227       const char *comp(data->GetComponentName(i));
228       if(comp)
229         ret->setInfoOnComponent(i,comp);
230     }
231   double *ptOut(ret->getPointer());
232   vtkFloatArray *d0(vtkFloatArray::SafeDownCast(data));
233   if(d0)
234     {
235       const float *pt(d0->GetPointer(0));
236       for(std::size_t i=0;i<nbElts;i++)
237         ptOut[i]=pt[i];
238       return ret.retn();
239     }
240   vtkDoubleArray *d1(vtkDoubleArray::SafeDownCast(data));
241   if(d1)
242     {
243       const double *pt(d1->GetPointer(0));
244       std::copy(pt,pt+nbElts,ptOut);
245       return ret.retn();
246     }
247   std::ostringstream oss;
248   oss << "ConvertVTKArrayToMCArrayDouble : unrecognized array \"" << typeid(*data).name() << "\" type !";
249   throw INTERP_KERNEL::Exception(oss.str());
250 }
251
252 DataArray *ConvertVTKArrayToMCArray(vtkDataArray *data)
253 {
254   if(!data)
255     throw INTERP_KERNEL::Exception("ConvertVTKArrayToMCArray : internal error !");
256   vtkFloatArray *d0(vtkFloatArray::SafeDownCast(data));
257   vtkDoubleArray *d1(vtkDoubleArray::SafeDownCast(data));
258   if(d0 || d1)
259     return ConvertVTKArrayToMCArrayDouble(data);
260   vtkIntArray *d2(vtkIntArray::SafeDownCast(data));
261   vtkLongArray *d3(vtkLongArray::SafeDownCast(data));
262   if(d2 || d3)
263     return ConvertVTKArrayToMCArrayInt(data);
264   std::ostringstream oss;
265   oss << "ConvertVTKArrayToMCArray : unrecognized array \"" << typeid(*data).name() << "\" type !";
266   throw INTERP_KERNEL::Exception(oss.str());
267 }
268
269 DataArrayDouble *BuildCoordsFrom(vtkPointSet *ds)
270 {
271   if(!ds)
272     throw INTERP_KERNEL::Exception("BuildCoordsFrom : internal error !");
273   vtkPoints *pts(ds->GetPoints());
274   if(!pts)
275     throw INTERP_KERNEL::Exception("BuildCoordsFrom : internal error 2 !");
276   vtkDataArray *data(pts->GetData());
277   if(!data)
278     throw INTERP_KERNEL::Exception("BuildCoordsFrom : internal error 3 !");
279   MCAuto<DataArrayDouble> coords(ConvertVTKArrayToMCArrayDouble(data));
280   return coords.retn();
281 }
282
283 void ConvertFromUnstructuredGrid(vtkUnstructuredGrid *ds, std::vector< MCAuto<MEDCouplingUMesh> >& ms, std::vector< MCAuto<DataArrayIdType> >& ids)
284 {
285   MCAuto<DataArrayDouble> coords(BuildCoordsFrom(ds));
286   vtkIdType nbCells(ds->GetNumberOfCells());
287   vtkCellArray *ca(ds->GetCells());
288   if(!ca)
289     return ;
290   vtkIdType nbEnt(ca->GetNumberOfConnectivityEntries());
291   vtkIdType *caPtr(ca->GetPointer());
292   vtkUnsignedCharArray *ct(ds->GetCellTypesArray());
293   if(!ct)
294     throw INTERP_KERNEL::Exception("ConvertFromUnstructuredGrid : internal error");
295   vtkIdTypeArray *cla(ds->GetCellLocationsArray());
296   const vtkIdType *claPtr(cla->GetPointer(0));
297   if(!cla)
298     throw INTERP_KERNEL::Exception("ConvertFromUnstructuredGrid : internal error 2");
299   const unsigned char *ctPtr(ct->GetPointer(0));
300   std::map<int,int> m(ComputeMapOfType());
301   MCAuto<DataArrayInt> lev(DataArrayInt::New()) ;  lev->alloc(nbCells,1);
302   int *levPtr(lev->getPointer());
303   for(vtkIdType i=0;i<nbCells;i++)
304     {
305       std::map<int,int>::iterator it(m.find(ctPtr[i]));
306       if(it!=m.end())
307         {
308           const INTERP_KERNEL::CellModel& cm(INTERP_KERNEL::CellModel::GetCellModel((INTERP_KERNEL::NormalizedCellType)(*it).second));
309           levPtr[i]=cm.getDimension();
310         }
311       else
312         {
313           std::ostringstream oss; oss << "ConvertFromUnstructuredGrid : at pos #" << i << " unrecognized VTK cell with type =" << ctPtr[i];
314           throw INTERP_KERNEL::Exception(oss.str());
315         }
316     }
317   MCAuto<DataArrayInt> levs(lev->getDifferentValues());
318   vtkIdTypeArray *faces(ds->GetFaces()),*faceLoc(ds->GetFaceLocations());
319   for(const int *curLev=levs->begin();curLev!=levs->end();curLev++)
320     {
321       MCAuto<MEDCouplingUMesh> m0(MEDCouplingUMesh::New("",*curLev));
322       m0->setCoords(coords); m0->allocateCells();
323       MCAuto<DataArrayIdType> cellIdsCurLev(lev->findIdsEqual(*curLev));
324       for(const mcIdType *cellId=cellIdsCurLev->begin();cellId!=cellIdsCurLev->end();cellId++)
325         {
326           std::map<int,int>::iterator it(m.find(ctPtr[*cellId]));
327           vtkIdType offset(claPtr[*cellId]);
328           vtkIdType sz(caPtr[offset]);
329           INTERP_KERNEL::NormalizedCellType ct((INTERP_KERNEL::NormalizedCellType)(*it).second);
330           if(ct!=INTERP_KERNEL::NORM_POLYHED)
331             {
332               std::vector<mcIdType> conn2(sz);
333               for(int kk=0;kk<sz;kk++)
334                 conn2[kk]=caPtr[offset+1+kk];
335               m0->insertNextCell(ct,sz,&conn2[0]);
336             }
337           else
338             {
339               if(!faces || !faceLoc)
340                 throw INTERP_KERNEL::Exception("ConvertFromUnstructuredGrid : faces are expected when there are polyhedra !");
341               const vtkIdType *facPtr(faces->GetPointer(0)),*facLocPtr(faceLoc->GetPointer(0));
342               std::vector<mcIdType> conn;
343               int off0(facLocPtr[*cellId]);
344               int nbOfFaces(facPtr[off0++]);
345               for(int k=0;k<nbOfFaces;k++)
346                 {
347                   int nbOfNodesInFace(facPtr[off0++]);
348                   std::copy(facPtr+off0,facPtr+off0+nbOfNodesInFace,std::back_inserter(conn));
349                   off0+=nbOfNodesInFace;
350                   if(k<nbOfFaces-1)
351                     conn.push_back(-1);
352                 }
353               m0->insertNextCell(ct,ToIdType(conn.size()),&conn[0]);
354             }
355         }
356       ms.push_back(m0); ids.push_back(cellIdsCurLev);
357     }
358 }
359
360 vtkSmartPointer<vtkUnstructuredGrid> ConvertUMeshFromMCToVTK(const MEDCouplingUMesh *mVor)
361 {
362   std::map<int,int> zeMapRev(ComputeRevMapOfType());
363   int nbCells(mVor->getNumberOfCells());
364   vtkSmartPointer<vtkUnstructuredGrid> ret(vtkSmartPointer<vtkUnstructuredGrid>::New());
365   ret->Initialize();
366   ret->Allocate();
367   vtkSmartPointer<vtkPoints> points(vtkSmartPointer<vtkPoints>::New());
368   {
369     const DataArrayDouble *vorCoords(mVor->getCoords());
370     vtkSmartPointer<vtkDoubleArray> da(vtkSmartPointer<vtkDoubleArray>::New());
371     da->SetNumberOfComponents((vtkIdType)vorCoords->getNumberOfComponents());
372     da->SetNumberOfTuples((vtkIdType)vorCoords->getNumberOfTuples());
373     std::copy(vorCoords->begin(),vorCoords->end(),da->GetPointer(0));
374     points->SetData(da);
375   }
376   mVor->checkConsistencyLight();
377   switch(mVor->getMeshDimension())
378     {
379     case 3:
380       {
381         int *cPtr(nullptr),*dPtr(nullptr);
382         unsigned char *aPtr(nullptr);
383         vtkSmartPointer<vtkUnsignedCharArray> cellTypes(vtkSmartPointer<vtkUnsignedCharArray>::New());
384         {
385           cellTypes->SetNumberOfComponents(1);
386           cellTypes->SetNumberOfTuples(nbCells);
387           aPtr=cellTypes->GetPointer(0);
388         }
389         vtkSmartPointer<vtkIdTypeArray> cellLocations(vtkSmartPointer<vtkIdTypeArray>::New());
390         {
391           cellLocations->SetNumberOfComponents(1);
392           cellLocations->SetNumberOfTuples(nbCells);
393           cPtr=cellLocations->GetPointer(0);
394         }
395         vtkSmartPointer<vtkIdTypeArray> cells(vtkSmartPointer<vtkIdTypeArray>::New());
396         {
397           MCAuto<DataArrayIdType> tmp2(mVor->computeEffectiveNbOfNodesPerCell());
398           cells->SetNumberOfComponents(1);
399           cells->SetNumberOfTuples(tmp2->accumulate((std::size_t)0)+nbCells);
400           dPtr=cells->GetPointer(0);
401         }
402         const mcIdType *connPtr(mVor->getNodalConnectivity()->begin()),*connIPtr(mVor->getNodalConnectivityIndex()->begin());
403         int k(0),kk(0);
404         std::vector<vtkIdType> ee,ff;
405         for(int i=0;i<nbCells;i++,connIPtr++)
406           {
407             INTERP_KERNEL::NormalizedCellType ct(static_cast<INTERP_KERNEL::NormalizedCellType>(connPtr[connIPtr[0]]));
408             *aPtr++=(unsigned char)zeMapRev[connPtr[connIPtr[0]]];
409             if(ct!=INTERP_KERNEL::NORM_POLYHED)
410               {
411                 int sz(connIPtr[1]-connIPtr[0]-1);
412                 *dPtr++=sz;
413                 dPtr=std::copy(connPtr+connIPtr[0]+1,connPtr+connIPtr[1],dPtr);
414                 *cPtr++=k; k+=sz+1;
415                 ee.push_back(kk);
416               }
417             else
418               {
419                 std::set<int> s(connPtr+connIPtr[0]+1,connPtr+connIPtr[1]); s.erase(-1);
420                 vtkIdType nbFace((vtkIdType)(std::count(connPtr+connIPtr[0]+1,connPtr+connIPtr[1],-1)+1));
421                 ff.push_back(nbFace);
422                 const mcIdType *work(connPtr+connIPtr[0]+1);
423                 for(int j=0;j<nbFace;j++)
424                   {
425                     const mcIdType *work2=std::find(work,connPtr+connIPtr[1],-1);
426                     ff.push_back((vtkIdType)std::distance(work,work2));
427                     ff.insert(ff.end(),work,work2);
428                     work=work2+1;
429                   }
430                 *dPtr++=(int)s.size();
431                 dPtr=std::copy(s.begin(),s.end(),dPtr);
432                 *cPtr++=k; k+=(int)s.size()+1;
433                 ee.push_back(kk); kk+=connIPtr[1]-connIPtr[0]+1;
434               }
435           }
436         //
437         vtkSmartPointer<vtkIdTypeArray> faceLocations(vtkSmartPointer<vtkIdTypeArray>::New());
438         {
439           faceLocations->SetNumberOfComponents(1);
440           faceLocations->SetNumberOfTuples((vtkIdType)ee.size());
441           std::copy(ee.begin(),ee.end(),faceLocations->GetPointer(0));
442         }
443         vtkSmartPointer<vtkIdTypeArray> faces(vtkSmartPointer<vtkIdTypeArray>::New());
444         {
445           faces->SetNumberOfComponents(1);
446           faces->SetNumberOfTuples((vtkIdType)ff.size());
447           std::copy(ff.begin(),ff.end(),faces->GetPointer(0));
448         }
449         vtkSmartPointer<vtkCellArray> cells2(vtkSmartPointer<vtkCellArray>::New());
450         cells2->SetCells(nbCells,cells);
451         ret->SetCells(cellTypes,cellLocations,cells2,faceLocations,faces);
452         break;
453       }
454     case 2:
455       {
456         vtkSmartPointer<vtkUnsignedCharArray> cellTypes(vtkSmartPointer<vtkUnsignedCharArray>::New());
457         {
458           cellTypes->SetNumberOfComponents(1);
459           cellTypes->SetNumberOfTuples(nbCells);
460           unsigned char *ptr(cellTypes->GetPointer(0));
461           std::fill(ptr,ptr+nbCells,zeMapRev[(int)INTERP_KERNEL::NORM_POLYGON]);
462         }
463         int *cPtr(0),*dPtr(0);
464         vtkSmartPointer<vtkIdTypeArray> cellLocations(vtkSmartPointer<vtkIdTypeArray>::New());
465         {
466           cellLocations->SetNumberOfComponents(1);
467           cellLocations->SetNumberOfTuples(nbCells);
468           cPtr=cellLocations->GetPointer(0);
469         }
470         vtkSmartPointer<vtkIdTypeArray> cells(vtkSmartPointer<vtkIdTypeArray>::New());
471         {
472           cells->SetNumberOfComponents(1);
473           cells->SetNumberOfTuples(mVor->getNodalConnectivity()->getNumberOfTuples());
474           dPtr=cells->GetPointer(0);
475         }
476         const mcIdType *connPtr(mVor->getNodalConnectivity()->begin()),*connIPtr(mVor->getNodalConnectivityIndex()->begin());
477         int k(0);
478         for(int i=0;i<nbCells;i++,connIPtr++)
479           {
480             *dPtr++=connIPtr[1]-connIPtr[0]-1;
481             dPtr=std::copy(connPtr+connIPtr[0]+1,connPtr+connIPtr[1],dPtr);
482             *cPtr++=k; k+=connIPtr[1]-connIPtr[0];
483           }
484         vtkSmartPointer<vtkCellArray> cells2(vtkSmartPointer<vtkCellArray>::New());
485         cells2->SetCells(nbCells,cells);
486         ret->SetCells(cellTypes,cellLocations,cells2);
487         break;
488       }
489     case 1:
490       {
491         vtkSmartPointer<vtkUnsignedCharArray> cellTypes(vtkSmartPointer<vtkUnsignedCharArray>::New());
492         {
493           cellTypes->SetNumberOfComponents(1);
494           cellTypes->SetNumberOfTuples(nbCells);
495           unsigned char *ptr(cellTypes->GetPointer(0));
496           std::fill(ptr,ptr+nbCells,zeMapRev[(int)INTERP_KERNEL::NORM_SEG2]);
497         }
498         int *cPtr(0),*dPtr(0);
499         vtkSmartPointer<vtkIdTypeArray> cellLocations(vtkSmartPointer<vtkIdTypeArray>::New());
500         {
501           cellLocations->SetNumberOfComponents(1);
502           cellLocations->SetNumberOfTuples(nbCells);
503           cPtr=cellLocations->GetPointer(0);
504         }
505         vtkSmartPointer<vtkIdTypeArray> cells(vtkSmartPointer<vtkIdTypeArray>::New());
506         {
507           cells->SetNumberOfComponents(1);
508           cells->SetNumberOfTuples(mVor->getNodalConnectivity()->getNumberOfTuples());
509           dPtr=cells->GetPointer(0);
510         }
511         const mcIdType *connPtr(mVor->getNodalConnectivity()->begin()),*connIPtr(mVor->getNodalConnectivityIndex()->begin());
512         for(int i=0;i<nbCells;i++,connIPtr++)
513           {
514             *dPtr++=2;
515             dPtr=std::copy(connPtr+connIPtr[0]+1,connPtr+connIPtr[1],dPtr);
516             *cPtr++=3*i;
517           }
518         vtkSmartPointer<vtkCellArray> cells2(vtkSmartPointer<vtkCellArray>::New());
519         cells2->SetCells(nbCells,cells);
520         ret->SetCells(cellTypes,cellLocations,cells2);
521         break;
522       }
523     default:
524       throw INTERP_KERNEL::Exception("Not implemented yet !");
525     }
526   ret->SetPoints(points);
527   return ret;
528 }
529
530 class OffsetKeeper
531 {
532 public:
533   OffsetKeeper():_vtk_arr(0) { }
534   void pushBack(vtkDataArray *da) { _da_on.push_back(da); }
535   void setVTKArray(vtkIdTypeArray *arr) { 
536     MCAuto<DataArrayIdType> offmc(ConvertVTKArrayToMCArrayInt(arr));
537     _off_arr=offmc; _vtk_arr=arr; }
538   const std::vector<vtkDataArray *>& getArrayGauss() const { return _da_on; }
539   const DataArrayIdType *getOffsets() const { return _off_arr; }
540   vtkIdTypeArray *getVTKOffsets() const { return _vtk_arr; }
541 private:
542   std::vector<vtkDataArray *> _da_on;
543   MCAuto<DataArrayIdType> _off_arr;
544   vtkIdTypeArray *_vtk_arr;
545 };
546
547 void FillAdvInfoFrom(int vtkCT, const std::vector<double>& GaussAdvData, int nbGaussPt, int nbNodesPerCell, std::vector<double>& refCoo,std::vector<double>& posInRefCoo)
548 {
549   int nbOfCTS((int)GaussAdvData[0]),pos(1);
550   for(int i=0;i<nbOfCTS;i++)
551     {
552       int lgth((int)GaussAdvData[pos]);
553       int curCT((int)GaussAdvData[pos+1]),dim((int)GaussAdvData[pos+2]);
554       if(curCT!=vtkCT)
555         {
556           pos+=lgth+1;
557           continue;
558         }
559       int lgthExp(nbNodesPerCell*dim+nbGaussPt*dim);
560       if(lgth!=lgthExp+2)//+2 for cell type and dimension !
561         {
562           std::ostringstream oss; oss << "FillAdvInfoFrom : Internal error. Unmatch with MEDReader version ? Expect size " << lgthExp << " and have " << lgth << " !";
563           throw INTERP_KERNEL::Exception(oss.str());
564         }
565       refCoo.insert(refCoo.end(),GaussAdvData.begin()+pos+3,GaussAdvData.begin()+pos+3+nbNodesPerCell*dim);
566       posInRefCoo.insert(posInRefCoo.end(),GaussAdvData.begin()+pos+3+nbNodesPerCell*dim,GaussAdvData.begin()+pos+3+nbNodesPerCell*dim+nbGaussPt*dim);
567       //std::copy(refCoo.begin(),refCoo.end(),std::ostream_iterator<double>(std::cerr," ")); std::cerr << std::endl;
568       //std::copy(posInRefCoo.begin(),posInRefCoo.end(),std::ostream_iterator<double>(std::cerr," ")); std::cerr << std::endl;
569       return ;
570     }
571   std::ostringstream oss; oss << "FillAdvInfoFrom : Internal error ! Not found cell type " << vtkCT << " in advanced Gauss info !";
572   throw INTERP_KERNEL::Exception(oss.str());
573 }
574
575 template<class T, class U>
576 vtkSmartPointer<T> ExtractFieldFieldArr(T *elt2, int sizeOfOutArr, int nbOfCellsOfInput, const mcIdType *offsetsPtr, const mcIdType *nbPtsPerCellPtr)
577 {
578   vtkSmartPointer<T> elt3(vtkSmartPointer<T>::New());
579   int nbc(elt2->GetNumberOfComponents());
580   elt3->SetNumberOfComponents(nbc);
581   elt3->SetNumberOfTuples(sizeOfOutArr);
582   for(int i=0;i<nbc;i++)
583     {
584       const char *name(elt2->GetComponentName(i));
585       if(name)
586         elt3->SetComponentName(i,name);
587     }
588   elt3->SetName(elt2->GetName());
589   //
590   U *ptr(elt3->GetPointer(0));
591   const U *srcPtr(elt2->GetPointer(0));
592   for(int i=0;i<nbOfCellsOfInput;i++)
593     ptr=std::copy(srcPtr+nbc*offsetsPtr[i],srcPtr+nbc*(offsetsPtr[i]+nbPtsPerCellPtr[i]),ptr);
594   return elt3;
595 }
596
597 template<class T, class U>
598 vtkSmartPointer<T> ExtractCellFieldArr(T *elt2, int sizeOfOutArr, int nbOfCellsOfInput, const mcIdType *idsPtr, const mcIdType *nbPtsPerCellPtr)
599 {
600   vtkSmartPointer<T> elt3(vtkSmartPointer<T>::New());
601   int nbc(elt2->GetNumberOfComponents());
602   elt3->SetNumberOfComponents(nbc);
603   elt3->SetNumberOfTuples(sizeOfOutArr);
604   for(int i=0;i<nbc;i++)
605     {
606       const char *name(elt2->GetComponentName(i));
607       if(name)
608         elt3->SetComponentName(i,name);
609     }
610   elt3->SetName(elt2->GetName());
611   //
612   U *ptr(elt3->GetPointer(0));
613   const U *srcPtr(elt2->GetPointer(0));
614   for(int i=0;i<nbOfCellsOfInput;i++)
615     for(int j=0;j<nbPtsPerCellPtr[i];j++)
616       ptr=std::copy(srcPtr+nbc*idsPtr[i],srcPtr+nbc*(idsPtr[i]+1),ptr);
617   return elt3;
618 }
619
620 vtkSmartPointer<vtkUnstructuredGrid> Voronize(const MEDCouplingUMesh *m, const DataArrayIdType *ids, vtkIdTypeArray *vtkOff, const DataArrayIdType *offsetsBase, const std::vector<vtkDataArray *>& arrGauss, const std::vector<double>& GaussAdvData, const std::vector<vtkDataArray *>& arrsOnCells)
621 {
622   if(arrGauss.empty())
623     throw INTERP_KERNEL::Exception("Voronize : no Gauss array !");
624   int nbTuples(arrGauss[0]->GetNumberOfTuples());
625   for(std::vector<vtkDataArray *>::const_iterator it=arrGauss.begin();it!=arrGauss.end();it++)
626     {
627       if((*it)->GetNumberOfTuples()!=nbTuples)
628         {
629           std::ostringstream oss; oss << "Mismatch of number of tuples in Gauss arrays for array \"" << (*it)->GetName() << "\"";
630           throw INTERP_KERNEL::Exception(oss.str());
631         }
632     }
633   // Look at vtkOff has in the stomac
634   vtkInformation *info(vtkOff->GetInformation());
635   if(!info)
636     throw INTERP_KERNEL::Exception("info is null ! Internal error ! Looks bad !");
637   vtkInformationQuadratureSchemeDefinitionVectorKey *key(vtkQuadratureSchemeDefinition::DICTIONARY());
638   if(!key->Has(info))
639     throw INTERP_KERNEL::Exception("No quadrature key in info included in offets array ! Internal error ! Looks bad !");
640   int dictSize(key->Size(info));
641   INTERP_KERNEL::AutoPtr<vtkQuadratureSchemeDefinition *> dict(new vtkQuadratureSchemeDefinition *[dictSize]);
642   key->GetRange(info,dict,0,0,dictSize);
643   // Voronoize
644   MCAuto<MEDCouplingFieldDouble> field(MEDCouplingFieldDouble::New(ON_GAUSS_PT));
645   field->setMesh(m);
646   // Gauss Part
647   int nbOfCellsOfInput(m->getNumberOfCells());
648   MCAuto<DataArrayIdType> nbPtsPerCellArr(DataArrayIdType::New()); nbPtsPerCellArr->alloc(nbOfCellsOfInput,1);
649   std::map<int,int> zeMapRev(ComputeRevMapOfType()),zeMap(ComputeMapOfType());
650   std::set<INTERP_KERNEL::NormalizedCellType> agt(m->getAllGeoTypes());
651   for(std::set<INTERP_KERNEL::NormalizedCellType>::const_iterator it=agt.begin();it!=agt.end();it++)
652     {
653       const INTERP_KERNEL::CellModel& cm(INTERP_KERNEL::CellModel::GetCellModel(*it));
654       std::map<int,int>::const_iterator it2(zeMapRev.find((int)*it));
655       if(it2==zeMapRev.end())
656         throw INTERP_KERNEL::Exception("Internal error ! no type conversion available !");
657       vtkQuadratureSchemeDefinition *gaussLoc(dict[(*it2).second]);
658       if(!gaussLoc)
659         {
660           std::ostringstream oss; oss << "For cell type " << cm.getRepr() << " no Gauss info !";
661           throw INTERP_KERNEL::Exception(oss.str());
662         }
663       int np(gaussLoc->GetNumberOfQuadraturePoints()),nbPtsPerCell((int)cm.getNumberOfNodes());
664       const double *sfw(gaussLoc->GetShapeFunctionWeights()),*w(gaussLoc->GetQuadratureWeights());;
665       std::vector<double> refCoo,posInRefCoo,wCpp(w,w+np);
666       FillAdvInfoFrom((*it2).second,GaussAdvData,np,nbPtsPerCell,refCoo,posInRefCoo);
667       field->setGaussLocalizationOnType(*it,refCoo,posInRefCoo,wCpp);
668       MCAuto<DataArrayIdType> ids2(m->giveCellsWithType(*it));
669       nbPtsPerCellArr->setPartOfValuesSimple3(np,ids2->begin(),ids2->end(),0,1,1);
670     }
671   int zeSizeOfOutCellArr(nbPtsPerCellArr->accumulate((std::size_t)0));
672   { MCAuto<DataArrayDouble> fakeArray(DataArrayDouble::New()); fakeArray->alloc(zeSizeOfOutCellArr,1); field->setArray(fakeArray); }
673   field->checkConsistencyLight();
674   MCAuto<MEDCouplingFieldDouble> vor(field->voronoize(1e-12));// The key is here !
675   MEDCouplingUMesh *mVor(dynamic_cast<MEDCouplingUMesh *>(vor->getMesh()));
676   //
677   vtkSmartPointer<vtkUnstructuredGrid> ret(ConvertUMeshFromMCToVTK(mVor));
678   // now fields...
679   MCAuto<DataArrayIdType> myOffsets(offsetsBase->selectByTupleIdSafe(ids->begin(),ids->end()));
680   const mcIdType *myOffsetsPtr(myOffsets->begin()),*nbPtsPerCellArrPtr(nbPtsPerCellArr->begin());
681   for(std::vector<vtkDataArray *>::const_iterator it=arrGauss.begin();it!=arrGauss.end();it++)
682     {
683       vtkDataArray *elt(*it);
684       vtkDoubleArray *elt2(vtkDoubleArray::SafeDownCast(elt));
685       vtkIntArray *elt3(vtkIntArray::SafeDownCast(elt));
686       vtkIdTypeArray *elt4(vtkIdTypeArray::SafeDownCast(elt));
687       if(elt2)
688         {
689           vtkSmartPointer<vtkDoubleArray> arr(ExtractFieldFieldArr<vtkDoubleArray,double>(elt2,zeSizeOfOutCellArr,nbOfCellsOfInput,myOffsetsPtr,nbPtsPerCellArrPtr));
690           ret->GetCellData()->AddArray(arr);
691           continue;
692         }
693       if(elt3)
694         {
695           vtkSmartPointer<vtkIntArray> arr(ExtractFieldFieldArr<vtkIntArray,int>(elt3,zeSizeOfOutCellArr,nbOfCellsOfInput,myOffsetsPtr,nbPtsPerCellArrPtr));
696           ret->GetCellData()->AddArray(arr);
697           continue;
698         }
699       if(elt4)
700         {
701           vtkSmartPointer<vtkIdTypeArray> arr(ExtractFieldFieldArr<vtkIdTypeArray,int>(elt4,zeSizeOfOutCellArr,nbOfCellsOfInput,myOffsetsPtr,nbPtsPerCellArrPtr));
702           ret->GetCellData()->AddArray(arr);
703           continue;
704         }
705     }
706   for(std::vector<vtkDataArray *>::const_iterator it=arrsOnCells.begin();it!=arrsOnCells.end();it++)
707     {
708       vtkDataArray *elt(*it);
709       vtkDoubleArray *elt2(vtkDoubleArray::SafeDownCast(elt));
710       vtkIntArray *elt3(vtkIntArray::SafeDownCast(elt));
711       vtkIdTypeArray *elt4(vtkIdTypeArray::SafeDownCast(elt));
712       if(elt2)
713         {
714           vtkSmartPointer<vtkDoubleArray> arr(ExtractCellFieldArr<vtkDoubleArray,double>(elt2,zeSizeOfOutCellArr,nbOfCellsOfInput,ids->begin(),nbPtsPerCellArrPtr));
715           ret->GetCellData()->AddArray(arr);
716           continue;
717         }
718       if(elt3)
719         {
720           vtkSmartPointer<vtkIntArray> arr(ExtractCellFieldArr<vtkIntArray,int>(elt3,zeSizeOfOutCellArr,nbOfCellsOfInput,ids->begin(),nbPtsPerCellArrPtr));
721           ret->GetCellData()->AddArray(arr);
722           continue;
723         }
724       if(elt4)
725         {
726           vtkSmartPointer<vtkIdTypeArray> arr(ExtractCellFieldArr<vtkIdTypeArray,int>(elt4,zeSizeOfOutCellArr,nbOfCellsOfInput,ids->begin(),nbPtsPerCellArrPtr));
727           ret->GetCellData()->AddArray(arr);
728           continue;
729         }
730     }
731   return ret;
732 }
733
734 vtkSmartPointer<vtkUnstructuredGrid> ComputeVoroGauss(vtkUnstructuredGrid *usgIn, const std::vector<double>& GaussAdvData)
735 {
736   OffsetKeeper zeOffsets;
737   std::string zeArrOffset;
738   std::vector<std::string> cellFieldNamesToDestroy;
739   {
740     int nArrays(usgIn->GetFieldData()->GetNumberOfArrays());
741     for(int i=0;i<nArrays;i++)
742       {
743         vtkDataArray *array(usgIn->GetFieldData()->GetArray(i));
744         if(!array)
745           continue;
746         const char* arrayOffsetName(array->GetInformation()->Get(vtkQuadratureSchemeDefinition::QUADRATURE_OFFSET_ARRAY_NAME()));
747         if(!arrayOffsetName)
748           continue;
749         std::string arrOffsetNameCpp(arrayOffsetName);
750         cellFieldNamesToDestroy.push_back(arrOffsetNameCpp);
751         if(arrOffsetNameCpp.find("ELGA@")==std::string::npos)
752           continue;
753         if(zeArrOffset.empty())
754           zeArrOffset=arrOffsetNameCpp;
755         else
756           if(zeArrOffset!=arrOffsetNameCpp)
757             {
758               throw INTERP_KERNEL::Exception("ComputeVoroGauss : error in QUADRATURE_OFFSET_ARRAY_NAME for Gauss fields array !");
759             }
760         zeOffsets.pushBack(array);
761       }
762     if(zeArrOffset.empty())
763       throw INTERP_KERNEL::Exception("ComputeVoroGauss : no Gauss points fields in DataSet !");
764   }
765   std::vector< MCAuto<MEDCouplingUMesh> > ms;
766   std::vector< MCAuto<DataArrayIdType> > ids;
767   ConvertFromUnstructuredGrid(usgIn,ms,ids);
768   {
769     vtkDataArray *offTmp(usgIn->GetCellData()->GetArray(zeArrOffset.c_str()));
770     if(!offTmp)
771       {
772         std::ostringstream oss; oss << "ComputeVoroGauss : cell field " << zeArrOffset << " not found !";
773         throw INTERP_KERNEL::Exception(oss.str());
774       }
775     vtkIdTypeArray *offsets(vtkIdTypeArray::SafeDownCast(offTmp));
776     if(!offsets)
777       {
778         std::ostringstream oss; oss << "ComputeVoroGauss : cell field " << zeArrOffset << " exists but not with the right type of data !";
779         throw INTERP_KERNEL::Exception(oss.str());
780       }
781     ///
782     zeOffsets.setVTKArray(offsets);
783   }
784   //
785   std::vector<vtkDataArray *> arrsOnCells;
786   {
787     int nArrays(usgIn->GetCellData()->GetNumberOfArrays());
788     for(int i=0;i<nArrays;i++)
789       {
790         vtkDataArray *array(usgIn->GetCellData()->GetArray(i));
791         if(!array)
792           continue;
793         std::string name(array->GetName());
794         if(std::find(cellFieldNamesToDestroy.begin(),cellFieldNamesToDestroy.end(),name)==cellFieldNamesToDestroy.end())
795           {
796             arrsOnCells.push_back(array);
797           }
798       }
799     {
800       vtkDataArray *doNotKeepThis(zeOffsets.getVTKOffsets());
801       std::vector<vtkDataArray *>::iterator it2(std::find(arrsOnCells.begin(),arrsOnCells.end(),doNotKeepThis));
802       if(it2!=arrsOnCells.end())
803         arrsOnCells.erase(it2);
804     }
805   }
806   //
807   std::size_t sz(ms.size());
808   std::vector< vtkSmartPointer<vtkUnstructuredGrid> > res;
809   for(std::size_t i=0;i<sz;i++)
810     {
811       MCAuto<MEDCouplingUMesh> mmc(ms[i]);
812       MCAuto<DataArrayIdType> myIds(ids[i]);
813       vtkSmartPointer<vtkUnstructuredGrid> vor(Voronize(mmc,myIds,zeOffsets.getVTKOffsets(),zeOffsets.getOffsets(),zeOffsets.getArrayGauss(),GaussAdvData,arrsOnCells));
814       res.push_back(vor);
815     }
816   if(res.empty())
817     throw INTERP_KERNEL::Exception("Dataset is empty !");
818   vtkSmartPointer<vtkMultiBlockDataGroupFilter> mb(vtkSmartPointer<vtkMultiBlockDataGroupFilter>::New());
819   vtkSmartPointer<vtkCompositeDataToUnstructuredGridFilter> cd(vtkSmartPointer<vtkCompositeDataToUnstructuredGridFilter>::New());
820   for(std::vector< vtkSmartPointer<vtkUnstructuredGrid> >::const_iterator it=res.begin();it!=res.end();it++)
821     mb->AddInputData(*it);
822   cd->SetInputConnection(mb->GetOutputPort());
823   cd->SetMergePoints(0);
824   cd->Update();
825   vtkSmartPointer<vtkUnstructuredGrid> ret;
826   ret=cd->GetOutput();
827   return ret;
828 }
829
830 ////////////////////
831
832 vtkVoroGauss::vtkVoroGauss()
833 {
834   this->SetNumberOfInputPorts(1);
835   this->SetNumberOfOutputPorts(1);
836 }
837
838 vtkVoroGauss::~vtkVoroGauss()
839 {
840 }
841
842 int vtkVoroGauss::RequestInformation(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
843
844   //std::cerr << "########################################## vtkVoroGauss::RequestInformation ##########################################" << std::endl;
845   try
846     {
847       vtkUnstructuredGrid *usgIn(0);
848       ExtractInfo(inputVector[0],usgIn);
849     }
850   catch(INTERP_KERNEL::Exception& e)
851     {
852       std::ostringstream oss;
853       oss << "Exception has been thrown in vtkVoroGauss::RequestInformation : " << e.what() << std::endl;
854       if(this->HasObserver("ErrorEvent") )
855         this->InvokeEvent("ErrorEvent",const_cast<char *>(oss.str().c_str()));
856       else
857         vtkOutputWindowDisplayErrorText(const_cast<char *>(oss.str().c_str()));
858       vtkObject::BreakOnError();
859       return 0;
860     }
861   return 1;
862 }
863
864 int vtkVoroGauss::RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
865 {
866   //std::cerr << "########################################## vtkVoroGauss::RequestData        ##########################################" << std::endl;
867   try
868     {
869       
870       std::vector<double> GaussAdvData;
871       bool isOK(IsInformationOK(inputVector[0]->GetInformationObject(0),GaussAdvData));
872       if(!isOK)
873         throw INTERP_KERNEL::Exception("Sorry but no advanced gauss info found ! Expect to be called right after a MEDReader containing Gauss Points !");
874       vtkUnstructuredGrid *usgIn(0);
875       ExtractInfo(inputVector[0],usgIn);
876       //
877       vtkSmartPointer<vtkUnstructuredGrid> ret(ComputeVoroGauss(usgIn,GaussAdvData));
878       vtkInformation *inInfo(inputVector[0]->GetInformationObject(0));
879       vtkInformation *outInfo(outputVector->GetInformationObject(0));
880       vtkUnstructuredGrid *output(vtkUnstructuredGrid::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT())));
881       output->ShallowCopy(ret);
882     }
883   catch(INTERP_KERNEL::Exception& e)
884     {
885       std::ostringstream oss;
886       oss << "Exception has been thrown in vtkVoroGauss::RequestData : " << e.what() << std::endl;
887       if(this->HasObserver("ErrorEvent") )
888         this->InvokeEvent("ErrorEvent",const_cast<char *>(oss.str().c_str()));
889       else
890         vtkOutputWindowDisplayErrorText(const_cast<char *>(oss.str().c_str()));
891       vtkObject::BreakOnError();
892       return 0;
893     }
894   return 1;
895 }
896
897 void vtkVoroGauss::PrintSelf(ostream& os, vtkIndent indent)
898 {
899   this->Superclass::PrintSelf(os, indent);
900 }