1 // Copyright (C) 2017-2019 EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (EDF R&D)
21 #include "vtkVoroGauss.h"
23 #include "vtkAdjacentVertexIterator.h"
24 #include "vtkIntArray.h"
25 #include "vtkCellData.h"
26 #include "vtkPointData.h"
27 #include "vtkCellType.h"
29 #include "vtkCellArray.h"
30 #include "vtkIdTypeArray.h"
32 #include "vtkStreamingDemandDrivenPipeline.h"
33 #include "vtkUnstructuredGrid.h"
34 #include "vtkMultiBlockDataSet.h"
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"
67 #include "MEDCouplingMemArray.hxx"
68 #include "MEDCouplingMemArray.txx"
69 #include "MEDCouplingUMesh.hxx"
70 #include "MEDCouplingFieldDouble.hxx"
71 #include "InterpKernelAutoPtr.hxx"
72 #include "InterpKernelGaussCoords.hxx"
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;
90 vtkStandardNewMacro(vtkVoroGauss);
93 std::map<int,int> ComputeMapOfType()
95 std::map<int,int> ret;
96 int nbOfTypesInMC(sizeof(MEDCOUPLING2VTKTYPETRADUCER)/sizeof( decltype(MEDCOUPLING2VTKTYPETRADUCER[0]) ));
97 for(int i=0;i<nbOfTypesInMC;i++)
99 auto vtkId(MEDCOUPLING2VTKTYPETRADUCER[i]);
100 if(vtkId!=MEDCOUPLING2VTKTYPETRADUCER_NONE)
106 std::map<int,int> ComputeRevMapOfType()
108 std::map<int,int> ret;
109 int nbOfTypesInMC(sizeof(MEDCOUPLING2VTKTYPETRADUCER)/sizeof( decltype(MEDCOUPLING2VTKTYPETRADUCER[0]) ));
110 for(int i=0;i<nbOfTypesInMC;i++)
112 auto vtkId(MEDCOUPLING2VTKTYPETRADUCER[i]);
113 if(vtkId!=MEDCOUPLING2VTKTYPETRADUCER_NONE)
121 vtkInformationDoubleVectorKey *GetMEDReaderMetaDataIfAny()
123 static const char ZE_KEY[]="vtkMEDReader::GAUSS_DATA";
124 MEDCoupling::GlobalDict *gd(MEDCoupling::GlobalDict::GetInstance());
125 if(!gd->hasKey(ZE_KEY))
127 std::string ptSt(gd->value(ZE_KEY));
129 std::istringstream iss(ptSt); iss >> pt;
130 return reinterpret_cast<vtkInformationDoubleVectorKey *>(pt);
133 bool IsInformationOK(vtkInformation *info, std::vector<double>& data)
135 vtkInformationDoubleVectorKey *key(GetMEDReaderMetaDataIfAny());
138 // Check the information contain meta data key
141 int lgth(key->Length(info));
142 const double *data2(info->Get(key));
143 data.insert(data.end(),data2,data2+lgth);
147 void ExtractInfo(vtkInformationVector *inputVector, vtkUnstructuredGrid *& usgIn)
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())));
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));
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));
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 !");
170 throw INTERP_KERNEL::Exception("Input data set is NULL !");
171 usgIn=vtkUnstructuredGrid::SafeDownCast(input);
173 throw INTERP_KERNEL::Exception("Input data set is not an unstructured mesh ! This filter works only on unstructured meshes !");
176 DataArrayIdType *ConvertVTKArrayToMCArrayInt(vtkDataArray *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++)
186 const char *comp(data->GetComponentName(i));
188 ret->setInfoOnComponent(i,comp);
190 mcIdType *ptOut(ret->getPointer());
191 vtkIntArray *d0(vtkIntArray::SafeDownCast(data));
194 const int *pt(d0->GetPointer(0));
195 std::copy(pt,pt+nbElts,ptOut);
198 vtkLongArray *d1(vtkLongArray::SafeDownCast(data));
201 const long *pt(d1->GetPointer(0));
202 std::copy(pt,pt+nbElts,ptOut);
205 vtkIdTypeArray *d2(vtkIdTypeArray::SafeDownCast(data));
208 const int *pt(d2->GetPointer(0));
209 std::copy(pt,pt+nbElts,ptOut);
212 std::ostringstream oss;
213 oss << "ConvertVTKArrayToMCArrayInt : unrecognized array \"" << typeid(*data).name() << "\" type !";
214 throw INTERP_KERNEL::Exception(oss.str());
217 DataArrayDouble *ConvertVTKArrayToMCArrayDouble(vtkDataArray *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++)
227 const char *comp(data->GetComponentName(i));
229 ret->setInfoOnComponent(i,comp);
231 double *ptOut(ret->getPointer());
232 vtkFloatArray *d0(vtkFloatArray::SafeDownCast(data));
235 const float *pt(d0->GetPointer(0));
236 for(std::size_t i=0;i<nbElts;i++)
240 vtkDoubleArray *d1(vtkDoubleArray::SafeDownCast(data));
243 const double *pt(d1->GetPointer(0));
244 std::copy(pt,pt+nbElts,ptOut);
247 std::ostringstream oss;
248 oss << "ConvertVTKArrayToMCArrayDouble : unrecognized array \"" << typeid(*data).name() << "\" type !";
249 throw INTERP_KERNEL::Exception(oss.str());
252 DataArray *ConvertVTKArrayToMCArray(vtkDataArray *data)
255 throw INTERP_KERNEL::Exception("ConvertVTKArrayToMCArray : internal error !");
256 vtkFloatArray *d0(vtkFloatArray::SafeDownCast(data));
257 vtkDoubleArray *d1(vtkDoubleArray::SafeDownCast(data));
259 return ConvertVTKArrayToMCArrayDouble(data);
260 vtkIntArray *d2(vtkIntArray::SafeDownCast(data));
261 vtkLongArray *d3(vtkLongArray::SafeDownCast(data));
263 return ConvertVTKArrayToMCArrayInt(data);
264 std::ostringstream oss;
265 oss << "ConvertVTKArrayToMCArray : unrecognized array \"" << typeid(*data).name() << "\" type !";
266 throw INTERP_KERNEL::Exception(oss.str());
269 DataArrayDouble *BuildCoordsFrom(vtkPointSet *ds)
272 throw INTERP_KERNEL::Exception("BuildCoordsFrom : internal error !");
273 vtkPoints *pts(ds->GetPoints());
275 throw INTERP_KERNEL::Exception("BuildCoordsFrom : internal error 2 !");
276 vtkDataArray *data(pts->GetData());
278 throw INTERP_KERNEL::Exception("BuildCoordsFrom : internal error 3 !");
279 MCAuto<DataArrayDouble> coords(ConvertVTKArrayToMCArrayDouble(data));
280 return coords.retn();
283 void ConvertFromUnstructuredGrid(vtkUnstructuredGrid *ds, std::vector< MCAuto<MEDCouplingUMesh> >& ms, std::vector< MCAuto<DataArrayIdType> >& ids)
285 MCAuto<DataArrayDouble> coords(BuildCoordsFrom(ds));
286 vtkIdType nbCells(ds->GetNumberOfCells());
287 vtkCellArray *ca(ds->GetCells());
290 vtkIdType nbEnt(ca->GetNumberOfConnectivityEntries());
291 vtkIdType *caPtr(ca->GetPointer());
292 vtkUnsignedCharArray *ct(ds->GetCellTypesArray());
294 throw INTERP_KERNEL::Exception("ConvertFromUnstructuredGrid : internal error");
295 vtkIdTypeArray *cla(ds->GetCellLocationsArray());
296 const vtkIdType *claPtr(cla->GetPointer(0));
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++)
305 std::map<int,int>::iterator it(m.find(ctPtr[i]));
308 const INTERP_KERNEL::CellModel& cm(INTERP_KERNEL::CellModel::GetCellModel((INTERP_KERNEL::NormalizedCellType)(*it).second));
309 levPtr[i]=cm.getDimension();
313 std::ostringstream oss; oss << "ConvertFromUnstructuredGrid : at pos #" << i << " unrecognized VTK cell with type =" << ctPtr[i];
314 throw INTERP_KERNEL::Exception(oss.str());
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++)
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++)
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)
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]);
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++)
347 int nbOfNodesInFace(facPtr[off0++]);
348 std::copy(facPtr+off0,facPtr+off0+nbOfNodesInFace,std::back_inserter(conn));
349 off0+=nbOfNodesInFace;
353 m0->insertNextCell(ct,ToIdType(conn.size()),&conn[0]);
356 ms.push_back(m0); ids.push_back(cellIdsCurLev);
360 vtkSmartPointer<vtkUnstructuredGrid> ConvertUMeshFromMCToVTK(const MEDCouplingUMesh *mVor)
362 std::map<int,int> zeMapRev(ComputeRevMapOfType());
363 int nbCells(mVor->getNumberOfCells());
364 vtkSmartPointer<vtkUnstructuredGrid> ret(vtkSmartPointer<vtkUnstructuredGrid>::New());
367 vtkSmartPointer<vtkPoints> points(vtkSmartPointer<vtkPoints>::New());
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));
376 mVor->checkConsistencyLight();
377 switch(mVor->getMeshDimension())
381 int *cPtr(nullptr),*dPtr(nullptr);
382 unsigned char *aPtr(nullptr);
383 vtkSmartPointer<vtkUnsignedCharArray> cellTypes(vtkSmartPointer<vtkUnsignedCharArray>::New());
385 cellTypes->SetNumberOfComponents(1);
386 cellTypes->SetNumberOfTuples(nbCells);
387 aPtr=cellTypes->GetPointer(0);
389 vtkSmartPointer<vtkIdTypeArray> cellLocations(vtkSmartPointer<vtkIdTypeArray>::New());
391 cellLocations->SetNumberOfComponents(1);
392 cellLocations->SetNumberOfTuples(nbCells);
393 cPtr=cellLocations->GetPointer(0);
395 vtkSmartPointer<vtkIdTypeArray> cells(vtkSmartPointer<vtkIdTypeArray>::New());
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);
402 const mcIdType *connPtr(mVor->getNodalConnectivity()->begin()),*connIPtr(mVor->getNodalConnectivityIndex()->begin());
404 std::vector<vtkIdType> ee,ff;
405 for(int i=0;i<nbCells;i++,connIPtr++)
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)
411 int sz(connIPtr[1]-connIPtr[0]-1);
413 dPtr=std::copy(connPtr+connIPtr[0]+1,connPtr+connIPtr[1],dPtr);
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++)
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);
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;
437 vtkSmartPointer<vtkIdTypeArray> faceLocations(vtkSmartPointer<vtkIdTypeArray>::New());
439 faceLocations->SetNumberOfComponents(1);
440 faceLocations->SetNumberOfTuples((vtkIdType)ee.size());
441 std::copy(ee.begin(),ee.end(),faceLocations->GetPointer(0));
443 vtkSmartPointer<vtkIdTypeArray> faces(vtkSmartPointer<vtkIdTypeArray>::New());
445 faces->SetNumberOfComponents(1);
446 faces->SetNumberOfTuples((vtkIdType)ff.size());
447 std::copy(ff.begin(),ff.end(),faces->GetPointer(0));
449 vtkSmartPointer<vtkCellArray> cells2(vtkSmartPointer<vtkCellArray>::New());
450 cells2->SetCells(nbCells,cells);
451 ret->SetCells(cellTypes,cellLocations,cells2,faceLocations,faces);
456 vtkSmartPointer<vtkUnsignedCharArray> cellTypes(vtkSmartPointer<vtkUnsignedCharArray>::New());
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]);
463 int *cPtr(0),*dPtr(0);
464 vtkSmartPointer<vtkIdTypeArray> cellLocations(vtkSmartPointer<vtkIdTypeArray>::New());
466 cellLocations->SetNumberOfComponents(1);
467 cellLocations->SetNumberOfTuples(nbCells);
468 cPtr=cellLocations->GetPointer(0);
470 vtkSmartPointer<vtkIdTypeArray> cells(vtkSmartPointer<vtkIdTypeArray>::New());
472 cells->SetNumberOfComponents(1);
473 cells->SetNumberOfTuples(mVor->getNodalConnectivity()->getNumberOfTuples());
474 dPtr=cells->GetPointer(0);
476 const mcIdType *connPtr(mVor->getNodalConnectivity()->begin()),*connIPtr(mVor->getNodalConnectivityIndex()->begin());
478 for(int i=0;i<nbCells;i++,connIPtr++)
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];
484 vtkSmartPointer<vtkCellArray> cells2(vtkSmartPointer<vtkCellArray>::New());
485 cells2->SetCells(nbCells,cells);
486 ret->SetCells(cellTypes,cellLocations,cells2);
491 vtkSmartPointer<vtkUnsignedCharArray> cellTypes(vtkSmartPointer<vtkUnsignedCharArray>::New());
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]);
498 int *cPtr(0),*dPtr(0);
499 vtkSmartPointer<vtkIdTypeArray> cellLocations(vtkSmartPointer<vtkIdTypeArray>::New());
501 cellLocations->SetNumberOfComponents(1);
502 cellLocations->SetNumberOfTuples(nbCells);
503 cPtr=cellLocations->GetPointer(0);
505 vtkSmartPointer<vtkIdTypeArray> cells(vtkSmartPointer<vtkIdTypeArray>::New());
507 cells->SetNumberOfComponents(1);
508 cells->SetNumberOfTuples(mVor->getNodalConnectivity()->getNumberOfTuples());
509 dPtr=cells->GetPointer(0);
511 const mcIdType *connPtr(mVor->getNodalConnectivity()->begin()),*connIPtr(mVor->getNodalConnectivityIndex()->begin());
512 for(int i=0;i<nbCells;i++,connIPtr++)
515 dPtr=std::copy(connPtr+connIPtr[0]+1,connPtr+connIPtr[1],dPtr);
518 vtkSmartPointer<vtkCellArray> cells2(vtkSmartPointer<vtkCellArray>::New());
519 cells2->SetCells(nbCells,cells);
520 ret->SetCells(cellTypes,cellLocations,cells2);
524 throw INTERP_KERNEL::Exception("Not implemented yet !");
526 ret->SetPoints(points);
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; }
542 std::vector<vtkDataArray *> _da_on;
543 MCAuto<DataArrayIdType> _off_arr;
544 vtkIdTypeArray *_vtk_arr;
547 void FillAdvInfoFrom(int vtkCT, const std::vector<double>& GaussAdvData, int nbGaussPt, int nbNodesPerCell, std::vector<double>& refCoo,std::vector<double>& posInRefCoo)
549 int nbOfCTS((int)GaussAdvData[0]),pos(1);
550 for(int i=0;i<nbOfCTS;i++)
552 int lgth((int)GaussAdvData[pos]);
553 int curCT((int)GaussAdvData[pos+1]),dim((int)GaussAdvData[pos+2]);
559 int lgthExp(nbNodesPerCell*dim+nbGaussPt*dim);
560 if(lgth!=lgthExp+2)//+2 for cell type and dimension !
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());
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;
571 std::ostringstream oss; oss << "FillAdvInfoFrom : Internal error ! Not found cell type " << vtkCT << " in advanced Gauss info !";
572 throw INTERP_KERNEL::Exception(oss.str());
575 template<class T, class U>
576 vtkSmartPointer<T> ExtractFieldFieldArr(T *elt2, int sizeOfOutArr, int nbOfCellsOfInput, const mcIdType *offsetsPtr, const mcIdType *nbPtsPerCellPtr)
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++)
584 const char *name(elt2->GetComponentName(i));
586 elt3->SetComponentName(i,name);
588 elt3->SetName(elt2->GetName());
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);
597 template<class T, class U>
598 vtkSmartPointer<T> ExtractCellFieldArr(T *elt2, int sizeOfOutArr, int nbOfCellsOfInput, const mcIdType *idsPtr, const mcIdType *nbPtsPerCellPtr)
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++)
606 const char *name(elt2->GetComponentName(i));
608 elt3->SetComponentName(i,name);
610 elt3->SetName(elt2->GetName());
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);
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)
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++)
627 if((*it)->GetNumberOfTuples()!=nbTuples)
629 std::ostringstream oss; oss << "Mismatch of number of tuples in Gauss arrays for array \"" << (*it)->GetName() << "\"";
630 throw INTERP_KERNEL::Exception(oss.str());
633 // Look at vtkOff has in the stomac
634 vtkInformation *info(vtkOff->GetInformation());
636 throw INTERP_KERNEL::Exception("info is null ! Internal error ! Looks bad !");
637 vtkInformationQuadratureSchemeDefinitionVectorKey *key(vtkQuadratureSchemeDefinition::DICTIONARY());
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);
644 MCAuto<MEDCouplingFieldDouble> field(MEDCouplingFieldDouble::New(ON_GAUSS_PT));
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++)
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]);
660 std::ostringstream oss; oss << "For cell type " << cm.getRepr() << " no Gauss info !";
661 throw INTERP_KERNEL::Exception(oss.str());
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);
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()));
677 vtkSmartPointer<vtkUnstructuredGrid> ret(ConvertUMeshFromMCToVTK(mVor));
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++)
683 vtkDataArray *elt(*it);
684 vtkDoubleArray *elt2(vtkDoubleArray::SafeDownCast(elt));
685 vtkIntArray *elt3(vtkIntArray::SafeDownCast(elt));
686 vtkIdTypeArray *elt4(vtkIdTypeArray::SafeDownCast(elt));
689 vtkSmartPointer<vtkDoubleArray> arr(ExtractFieldFieldArr<vtkDoubleArray,double>(elt2,zeSizeOfOutCellArr,nbOfCellsOfInput,myOffsetsPtr,nbPtsPerCellArrPtr));
690 ret->GetCellData()->AddArray(arr);
695 vtkSmartPointer<vtkIntArray> arr(ExtractFieldFieldArr<vtkIntArray,int>(elt3,zeSizeOfOutCellArr,nbOfCellsOfInput,myOffsetsPtr,nbPtsPerCellArrPtr));
696 ret->GetCellData()->AddArray(arr);
701 vtkSmartPointer<vtkIdTypeArray> arr(ExtractFieldFieldArr<vtkIdTypeArray,int>(elt4,zeSizeOfOutCellArr,nbOfCellsOfInput,myOffsetsPtr,nbPtsPerCellArrPtr));
702 ret->GetCellData()->AddArray(arr);
706 for(std::vector<vtkDataArray *>::const_iterator it=arrsOnCells.begin();it!=arrsOnCells.end();it++)
708 vtkDataArray *elt(*it);
709 vtkDoubleArray *elt2(vtkDoubleArray::SafeDownCast(elt));
710 vtkIntArray *elt3(vtkIntArray::SafeDownCast(elt));
711 vtkIdTypeArray *elt4(vtkIdTypeArray::SafeDownCast(elt));
714 vtkSmartPointer<vtkDoubleArray> arr(ExtractCellFieldArr<vtkDoubleArray,double>(elt2,zeSizeOfOutCellArr,nbOfCellsOfInput,ids->begin(),nbPtsPerCellArrPtr));
715 ret->GetCellData()->AddArray(arr);
720 vtkSmartPointer<vtkIntArray> arr(ExtractCellFieldArr<vtkIntArray,int>(elt3,zeSizeOfOutCellArr,nbOfCellsOfInput,ids->begin(),nbPtsPerCellArrPtr));
721 ret->GetCellData()->AddArray(arr);
726 vtkSmartPointer<vtkIdTypeArray> arr(ExtractCellFieldArr<vtkIdTypeArray,int>(elt4,zeSizeOfOutCellArr,nbOfCellsOfInput,ids->begin(),nbPtsPerCellArrPtr));
727 ret->GetCellData()->AddArray(arr);
734 vtkSmartPointer<vtkUnstructuredGrid> ComputeVoroGauss(vtkUnstructuredGrid *usgIn, const std::vector<double>& GaussAdvData)
736 OffsetKeeper zeOffsets;
737 std::string zeArrOffset;
738 std::vector<std::string> cellFieldNamesToDestroy;
740 int nArrays(usgIn->GetFieldData()->GetNumberOfArrays());
741 for(int i=0;i<nArrays;i++)
743 vtkDataArray *array(usgIn->GetFieldData()->GetArray(i));
746 const char* arrayOffsetName(array->GetInformation()->Get(vtkQuadratureSchemeDefinition::QUADRATURE_OFFSET_ARRAY_NAME()));
749 std::string arrOffsetNameCpp(arrayOffsetName);
750 cellFieldNamesToDestroy.push_back(arrOffsetNameCpp);
751 if(arrOffsetNameCpp.find("ELGA@")==std::string::npos)
753 if(zeArrOffset.empty())
754 zeArrOffset=arrOffsetNameCpp;
756 if(zeArrOffset!=arrOffsetNameCpp)
758 throw INTERP_KERNEL::Exception("ComputeVoroGauss : error in QUADRATURE_OFFSET_ARRAY_NAME for Gauss fields array !");
760 zeOffsets.pushBack(array);
762 if(zeArrOffset.empty())
763 throw INTERP_KERNEL::Exception("ComputeVoroGauss : no Gauss points fields in DataSet !");
765 std::vector< MCAuto<MEDCouplingUMesh> > ms;
766 std::vector< MCAuto<DataArrayIdType> > ids;
767 ConvertFromUnstructuredGrid(usgIn,ms,ids);
769 vtkDataArray *offTmp(usgIn->GetCellData()->GetArray(zeArrOffset.c_str()));
772 std::ostringstream oss; oss << "ComputeVoroGauss : cell field " << zeArrOffset << " not found !";
773 throw INTERP_KERNEL::Exception(oss.str());
775 vtkIdTypeArray *offsets(vtkIdTypeArray::SafeDownCast(offTmp));
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());
782 zeOffsets.setVTKArray(offsets);
785 std::vector<vtkDataArray *> arrsOnCells;
787 int nArrays(usgIn->GetCellData()->GetNumberOfArrays());
788 for(int i=0;i<nArrays;i++)
790 vtkDataArray *array(usgIn->GetCellData()->GetArray(i));
793 std::string name(array->GetName());
794 if(std::find(cellFieldNamesToDestroy.begin(),cellFieldNamesToDestroy.end(),name)==cellFieldNamesToDestroy.end())
796 arrsOnCells.push_back(array);
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);
807 std::size_t sz(ms.size());
808 std::vector< vtkSmartPointer<vtkUnstructuredGrid> > res;
809 for(std::size_t i=0;i<sz;i++)
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));
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);
825 vtkSmartPointer<vtkUnstructuredGrid> ret;
832 vtkVoroGauss::vtkVoroGauss()
834 this->SetNumberOfInputPorts(1);
835 this->SetNumberOfOutputPorts(1);
838 vtkVoroGauss::~vtkVoroGauss()
842 int vtkVoroGauss::RequestInformation(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
844 //std::cerr << "########################################## vtkVoroGauss::RequestInformation ##########################################" << std::endl;
847 vtkUnstructuredGrid *usgIn(0);
848 ExtractInfo(inputVector[0],usgIn);
850 catch(INTERP_KERNEL::Exception& e)
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()));
857 vtkOutputWindowDisplayErrorText(const_cast<char *>(oss.str().c_str()));
858 vtkObject::BreakOnError();
864 int vtkVoroGauss::RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
866 //std::cerr << "########################################## vtkVoroGauss::RequestData ##########################################" << std::endl;
870 std::vector<double> GaussAdvData;
871 bool isOK(IsInformationOK(inputVector[0]->GetInformationObject(0),GaussAdvData));
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);
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);
883 catch(INTERP_KERNEL::Exception& e)
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()));
890 vtkOutputWindowDisplayErrorText(const_cast<char *>(oss.str().c_str()));
891 vtkObject::BreakOnError();
897 void vtkVoroGauss::PrintSelf(ostream& os, vtkIndent indent)
899 this->Superclass::PrintSelf(os, indent);