1 // Copyright (C) 2010-2021 CEA/DEN, 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
21 #include "vtkExtractCellType.h"
22 #include "MEDFileFieldRepresentationTree.hxx"
23 #include "MEDFileFieldOverView.hxx"
25 #include "vtkAdjacentVertexIterator.h"
26 #include "vtkAOSDataArrayTemplate.h"
27 #include "vtkIntArray.h"
28 #include "vtkCellData.h"
29 #include "vtkPointData.h"
31 #include "vtkStreamingDemandDrivenPipeline.h"
32 #include "vtkUnstructuredGrid.h"
33 #include "vtkMultiBlockDataSet.h"
35 #include "vtkInformationStringKey.h"
36 #include "vtkAlgorithmOutput.h"
37 #include "vtkObjectFactory.h"
38 #include "vtkMutableDirectedGraph.h"
39 #include "vtkMultiBlockDataSet.h"
40 #include "vtkDataSet.h"
41 #include "vtkInformationVector.h"
42 #include "vtkInformation.h"
43 #include "vtkDataArraySelection.h"
44 #include "vtkTimeStamp.h"
45 #include "vtkInEdgeIterator.h"
46 #include "vtkInformationDataObjectKey.h"
47 #include "vtkExecutive.h"
48 #include "vtkVariantArray.h"
49 #include "vtkStringArray.h"
50 #include "vtkDoubleArray.h"
51 #include "vtkCharArray.h"
52 #include "vtkUnsignedCharArray.h"
53 #include "vtkDataSetAttributes.h"
54 #include "vtkDemandDrivenPipeline.h"
55 #include "vtkDataObjectTreeIterator.h"
56 #include "vtkThreshold.h"
61 vtkStandardNewMacro(vtkExtractCellType)
63 vtkCxxSetObjectMacro(vtkExtractCellType, SIL, vtkMutableDirectedGraph)
68 class ExtractCellTypeStatus
71 ExtractCellTypeStatus():_status(false),_vtkt(-1),_mct(INTERP_KERNEL::NORM_ERROR) { }
72 ExtractCellTypeStatus(int vtkt, INTERP_KERNEL::NormalizedCellType mct);
73 bool isSame(int vtkt, INTERP_KERNEL::NormalizedCellType mct) const { return _vtkt==vtkt && _mct==mct; }
74 bool getStatus() const { return _status; }
75 void setStatus(bool status) const { _status=status; }
76 void cpyStatusFrom(const ExtractCellTypeStatus& other) { _status=other._status; }
77 std::string getKey() const { return _type_str; }
78 const char *getKeyOfEntry() const { return _type_str.c_str(); }
79 int getVTKCellType() const { return _vtkt; }
80 void printMySelf(std::ostream& os) const;
81 bool isSameAs(const ExtractCellTypeStatus& other) const;
82 void feedSIL(vtkMutableDirectedGraph *sil, vtkIdType root, vtkVariantArray *childEdge, std::vector<std::string>& names) const;
86 INTERP_KERNEL::NormalizedCellType _mct;
87 std::string _type_str;
90 class vtkExtractCellType::vtkExtractCellTypeInternal
93 vtkExtractCellTypeInternal():_ref_mtime(0) { }
94 int getNumberOfEntries() const;
95 const char *getKeyOfEntry(int i) const;
96 bool getStatusOfEntryStr(const char *entry) const;
97 void setStatusOfEntryStr(const char *entry, bool status) const;
98 void feedSIL(vtkMutableDirectedGraph *sil) const;
99 std::vector<int> getIdsToKeep() const;
100 void printMySelf(std::ostream& os) const;
101 bool setRefTime(vtkObject *input) const;
103 void loadFrom(const std::map<int,INTERP_KERNEL::NormalizedCellType>& m);
105 const ExtractCellTypeStatus& getEntry(const char *entry) const;
106 bool checkSame(const std::map<int,INTERP_KERNEL::NormalizedCellType>& m) const;
108 std::vector<ExtractCellTypeStatus> _types;
109 mutable unsigned long _ref_mtime;
112 bool vtkExtractCellType::vtkExtractCellTypeInternal::setRefTime(vtkObject *input) const
114 unsigned long mtime(input->GetMTime());
124 std::vector<int> vtkExtractCellType::vtkExtractCellTypeInternal::getIdsToKeep() const
126 std::vector<int> ret;
127 for(std::vector<ExtractCellTypeStatus>::const_iterator it=_types.begin();it!=_types.end();it++)
129 if((*it).getStatus())
130 ret.push_back((*it).getVTKCellType());
135 void vtkExtractCellType::vtkExtractCellTypeInternal::feedSIL(vtkMutableDirectedGraph *sil) const
137 vtkSmartPointer<vtkVariantArray> childEdge(vtkSmartPointer<vtkVariantArray>::New());
138 childEdge->InsertNextValue(0);
139 vtkSmartPointer<vtkVariantArray> crossEdge(vtkSmartPointer<vtkVariantArray>::New());
140 crossEdge->InsertNextValue(1);
141 // CrossEdge is an edge linking hierarchies.
142 vtkUnsignedCharArray* crossEdgesArray=vtkUnsignedCharArray::New();
143 crossEdgesArray->SetName("CrossEdges");
144 sil->GetEdgeData()->AddArray(crossEdgesArray);
145 crossEdgesArray->Delete();
146 std::vector<std::string> names;
147 // Add global fields root
148 vtkIdType root(sil->AddVertex());
149 names.push_back("CellTypesTree");
151 for(std::vector<ExtractCellTypeStatus>::const_iterator it=_types.begin();it!=_types.end();it++)
153 (*it).feedSIL(sil,root,childEdge,names);
155 // This array is used to assign names to nodes.
156 vtkStringArray *namesArray(vtkStringArray::New());
157 namesArray->SetName("Names");
158 namesArray->SetNumberOfTuples(sil->GetNumberOfVertices());
159 sil->GetVertexData()->AddArray(namesArray);
160 namesArray->Delete();
161 std::vector<std::string>::const_iterator iter;
163 for(cc=0, iter=names.begin(); iter!=names.end(); ++iter, ++cc)
164 namesArray->SetValue(cc,(*iter).c_str());
167 void vtkExtractCellType::vtkExtractCellTypeInternal::loadFrom(const std::map<int,INTERP_KERNEL::NormalizedCellType>& m)
172 std::size_t sz(m.size()),ii(0);
174 for(std::map<int,INTERP_KERNEL::NormalizedCellType>::const_iterator it=m.begin();it!=m.end();it++,ii++)
176 ExtractCellTypeStatus elt((*it).first,(*it).second);
181 int vtkExtractCellType::vtkExtractCellTypeInternal::getNumberOfEntries() const
183 return (int) _types.size();
186 const char *vtkExtractCellType::vtkExtractCellTypeInternal::getKeyOfEntry(int i) const
188 return _types[i].getKeyOfEntry();
191 bool vtkExtractCellType::vtkExtractCellTypeInternal::checkSame(const std::map<int,INTERP_KERNEL::NormalizedCellType>& m) const
193 std::size_t sz(m.size());
194 if(sz!=_types.size())
197 std::map<int,INTERP_KERNEL::NormalizedCellType>::const_iterator it(m.begin());
198 for(std::size_t i=0;i<sz && ret;i++,it++)
199 ret=_types[i].isSame((*it).first,(*it).second);
203 const ExtractCellTypeStatus& vtkExtractCellType::vtkExtractCellTypeInternal::getEntry(const char *entry) const
205 std::string entryCpp(entry);
206 for(std::vector<ExtractCellTypeStatus>::const_iterator it0=_types.begin();it0!=_types.end();it0++)
207 if(entryCpp==(*it0).getKey())
209 std::ostringstream oss; oss << "vtkExtractCellTypeInternal::getEntry : no such entry \"" << entry << "\"!";
210 throw INTERP_KERNEL::Exception(oss.str().c_str());
213 bool vtkExtractCellType::vtkExtractCellTypeInternal::getStatusOfEntryStr(const char *entry) const
217 const ExtractCellTypeStatus& elt(getEntry(entry));
218 return elt.getStatus();
220 catch (INTERP_KERNEL::Exception& /*e*/)
222 //std::cerr << vtkDebugMacro"Exception has been thrown in vtkExtractCellType::vtkExtractCellTypeInternal::getStatusOfEntryStr : " << e.what() << std::endl;
227 void vtkExtractCellType::vtkExtractCellTypeInternal::setStatusOfEntryStr(const char *entry, bool status) const
231 const ExtractCellTypeStatus& elt(getEntry(entry));
232 elt.setStatus(status);
234 catch (INTERP_KERNEL::Exception& /*e*/)
236 //std::cerr << "Exception has been thrown in vtkExtractCellType::vtkExtractCellTypeInternal::setStatusOfEntryStr : " << e.what() << std::endl;
240 void vtkExtractCellType::vtkExtractCellTypeInternal::printMySelf(std::ostream& os) const
242 for(std::vector<ExtractCellTypeStatus>::const_iterator it0=_types.begin();it0!=_types.end();it0++)
243 (*it0).printMySelf(os);
246 ExtractCellTypeStatus::ExtractCellTypeStatus(int vtkt, INTERP_KERNEL::NormalizedCellType mct):_status(false),_vtkt(vtkt),_mct(mct)
248 std::string name(INTERP_KERNEL::CellModel::GetCellModel(mct).getRepr());
249 _type_str=name.substr(5);//skip "NORM_"
252 void ExtractCellTypeStatus::printMySelf(std::ostream& os) const
254 os << " -" << _type_str << "(";
259 os << ")" << std::endl;
262 bool ExtractCellTypeStatus::isSameAs(const ExtractCellTypeStatus& other) const
264 return _vtkt==other._vtkt && _mct==other._mct;
267 void ExtractCellTypeStatus::feedSIL(vtkMutableDirectedGraph *sil, vtkIdType root, vtkVariantArray *childEdge, std::vector<std::string>& names) const
269 vtkIdType InfoGeoType(sil->AddChild(root,childEdge));
270 names.push_back(_type_str);
271 /*vtkIdType InfoVTKID(*/sil->AddChild(InfoGeoType,childEdge)/*)*/; // todo: unused
272 std::ostringstream oss; oss << _vtkt;
273 names.push_back(oss.str());
279 vtkExtractCellType::vtkExtractCellType():SIL(NULL),Internal(new vtkExtractCellTypeInternal),InsideOut(0)
283 vtkExtractCellType::~vtkExtractCellType()
287 delete this->Internal;
290 void vtkExtractCellType::SetInsideOut(int val)
292 if(this->InsideOut!=val)
299 int vtkExtractCellType::RequestInformation(vtkInformation * /*request*/, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
303 //std::cerr << "########################################## vtkExtractCellType::RequestInformation ##########################################" << std::endl;
304 vtkInformation *outInfo(outputVector->GetInformationObject(0));
305 vtkInformation *inputInfo(inputVector[0]->GetInformationObject(0));
306 vtkDataSet *input(0);
308 vtkDataObject *inp(inputInfo->Get(vtkDataObject::DATA_OBJECT()));
309 if(vtkDataSet::SafeDownCast(inp))
310 input=vtkDataSet::SafeDownCast(inp);
313 vtkMultiBlockDataSet *inputTmp(vtkMultiBlockDataSet::SafeDownCast(inp));
316 if(inputTmp->GetNumberOfBlocks()!=1)
318 vtkDebugMacro("vtkExtractCellType::RequestInformation : input vtkMultiBlockDataSet must contain exactly 1 block !");
321 vtkDataSet *blk0(vtkDataSet::SafeDownCast(inputTmp->GetBlock(0)));
324 vtkDebugMacro("vtkExtractCellType::RequestInformation : the single block in input vtkMultiBlockDataSet must be a vtkDataSet instance !");
331 vtkDebugMacro("vtkExtractCellType::RequestInformation : supported input are vtkDataSet or vtkMultiBlockDataSet !");
336 if(this->Internal->setRefTime(input))
338 vtkIdType nbOfCells(input->GetNumberOfCells());
339 std::map<int,INTERP_KERNEL::NormalizedCellType> m;
340 for(vtkIdType cellId=0;cellId<nbOfCells;cellId++)
342 int vtkCt(input->GetCellType(cellId));
343 const std::map<int,INTERP_KERNEL::NormalizedCellType>::const_iterator it(m.find(vtkCt));
346 const unsigned char *pos(std::find(MEDCoupling::MEDMeshMultiLev::PARAMEDMEM_2_VTKTYPE,MEDCoupling::MEDMeshMultiLev::PARAMEDMEM_2_VTKTYPE+MEDCoupling::MEDMeshMultiLev::PARAMEDMEM_2_VTKTYPE_LGTH,vtkCt));
347 if(pos==MEDCoupling::MEDMeshMultiLev::PARAMEDMEM_2_VTKTYPE+MEDCoupling::MEDMeshMultiLev::PARAMEDMEM_2_VTKTYPE_LGTH)
349 vtkDebugMacro("vtkExtractCellType::RequestInformation : cell #" << cellId << " has unrecognized type !");
352 m[vtkCt]=(INTERP_KERNEL::NormalizedCellType)std::distance(MEDCoupling::MEDMeshMultiLev::PARAMEDMEM_2_VTKTYPE,pos);
355 this->Internal->loadFrom(m);
358 this->SIL=vtkMutableDirectedGraph::New();
359 this->Internal->feedSIL(this->SIL);
361 outInfo->Set(vtkDataObject::SIL(),this->SIL);
364 catch(INTERP_KERNEL::Exception& e)
366 std::cerr << "Exception has been thrown in vtkExtractCellType::RequestInformation : " << e.what() << std::endl;
372 vtkDataSet *FilterFamilies(vtkDataSet *input, const std::vector<int>& idsToKeep, bool insideOut)
374 const int VTK_DATA_ARRAY_DELETE=vtkAOSDataArrayTemplate<double>::VTK_DATA_ARRAY_DELETE;
375 const char ZE_SELECTION_ARR_NAME[]="@@ZeSelection@@";
376 vtkDataSet *output(input->NewInstance());
377 output->ShallowCopy(input);
378 vtkSmartPointer<vtkThreshold> thres(vtkSmartPointer<vtkThreshold>::New());
379 thres->SetInputData(output);
380 //vtkDataSetAttributes *dscIn(input->GetCellData()),*dscIn2(input->GetPointData()); // todo: unused
381 //vtkDataSetAttributes *dscOut(output->GetCellData()),*dscOut2(output->GetPointData()); // todo: unused
383 double vMin(insideOut==0?1.:0.),vMax(insideOut==0?2.:1.);
384 thres->ThresholdBetween(vMin,vMax);
386 vtkIdType nbOfCells(input->GetNumberOfCells());
387 vtkCharArray *zeSelection(vtkCharArray::New());
388 zeSelection->SetName(ZE_SELECTION_ARR_NAME);
389 zeSelection->SetNumberOfComponents(1);
390 char *pt(new char[nbOfCells]);
391 zeSelection->SetArray(pt,nbOfCells,0,VTK_DATA_ARRAY_DELETE);
392 std::fill(pt,pt+nbOfCells,0);
393 std::vector<bool> pt2(nbOfCells,false);
394 for(std::vector<int>::const_iterator it=idsToKeep.begin();it!=idsToKeep.end();it++)
396 for(vtkIdType ii=0;ii<nbOfCells;ii++)
398 if(input->GetCellType(ii)==*it)
402 for(int ii=0;ii<nbOfCells;ii++)
405 int idx(output->GetCellData()->AddArray(zeSelection));
406 output->GetCellData()->SetActiveAttribute(idx,vtkDataSetAttributes::SCALARS);
407 output->GetCellData()->CopyScalarsOff();
408 zeSelection->Delete();
410 thres->SetInputArrayToProcess(idx,0,0,"vtkDataObject::FIELD_ASSOCIATION_CELLS",ZE_SELECTION_ARR_NAME);
412 vtkUnstructuredGrid *zeComputedOutput(thres->GetOutput());
413 zeComputedOutput->GetCellData()->RemoveArray(idx);
415 zeComputedOutput->Register(0);
416 return zeComputedOutput;
419 int vtkExtractCellType::RequestData(vtkInformation * /*request*/, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
423 //std::cerr << "########################################## vtkExtractCellType::RequestData ##########################################" << std::endl;
424 vtkInformation* inputInfo=inputVector[0]->GetInformationObject(0);
425 vtkDataSet *input(vtkDataSet::SafeDownCast(inputInfo->Get(vtkDataObject::DATA_OBJECT())));
426 //vtkInformation *info(input->GetInformation()); // todo: unused
427 vtkInformation *outInfo(outputVector->GetInformationObject(0));
428 vtkDataSet *output(vtkDataSet::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT())));
429 std::vector<int> idsToKeep(this->Internal->getIdsToKeep());
430 vtkDataSet *tryOnCell(FilterFamilies(input,idsToKeep,this->InsideOut));
431 // first shrink the input
432 output->ShallowCopy(tryOnCell);
435 catch(INTERP_KERNEL::Exception& e)
437 std::cerr << "Exception has been thrown in vtkExtractCellType::RequestData : " << e.what() << std::endl;
443 int vtkExtractCellType::GetSILUpdateStamp()
445 return (int)this->SILTime;
448 void vtkExtractCellType::PrintSelf(ostream& os, vtkIndent indent)
450 this->Superclass::PrintSelf(os, indent);
453 int vtkExtractCellType::GetNumberOfGeoTypesArrays()
455 int ret(this->Internal->getNumberOfEntries());
456 //std::cerr << "vtkExtractCellType::GetNumberOfGeoTypesArrays() -> " << ret << std::endl;
460 const char *vtkExtractCellType::GetGeoTypesArrayName(int index)
462 const char *ret(this->Internal->getKeyOfEntry(index));
463 //std::cerr << "vtkExtractCellType::GetGeoTypesArrayName(" << index << ") -> " << ret << std::endl;
467 int vtkExtractCellType::GetGeoTypesArrayStatus(const char *name)
469 int ret((int)this->Internal->getStatusOfEntryStr(name));
470 //std::cerr << "vtkExtractCellType::GetGeoTypesArrayStatus(" << name << ") -> " << ret << std::endl;
474 void vtkExtractCellType::SetGeoTypesStatus(const char *name, int status)
476 //std::cerr << "vtkExtractCellType::SetGeoTypesStatus(" << name << "," << status << ")" << std::endl;
477 if (GetNumberOfGeoTypesArrays()<1)
479 this->Internal->setStatusOfEntryStr(name,(bool)status);
480 if(std::string(name)==GetGeoTypesArrayName(GetNumberOfGeoTypesArrays()-1))
483 //this->Internal->printMySelf(std::cerr);