1 // Copyright (C) 2010-2020 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 "vtkDataArrayTemplate.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);
67 class ExtractCellTypeStatus
70 ExtractCellTypeStatus():_status(false),_vtkt(-1),_mct(INTERP_KERNEL::NORM_ERROR) { }
71 ExtractCellTypeStatus(int vtkt, INTERP_KERNEL::NormalizedCellType mct);
72 bool isSame(int vtkt, INTERP_KERNEL::NormalizedCellType mct) const { return _vtkt==vtkt && _mct==mct; }
73 bool getStatus() const { return _status; }
74 void setStatus(bool status) const { _status=status; }
75 void cpyStatusFrom(const ExtractCellTypeStatus& other) { _status=other._status; }
76 std::string getKey() const { return _type_str; }
77 const char *getKeyOfEntry() const { return _type_str.c_str(); }
78 int getVTKCellType() const { return _vtkt; }
79 void printMySelf(std::ostream& os) const;
80 bool isSameAs(const ExtractCellTypeStatus& other) const;
81 void feedSIL(vtkMutableDirectedGraph *sil, vtkIdType root, vtkVariantArray *childEdge, std::vector<std::string>& names) const;
85 INTERP_KERNEL::NormalizedCellType _mct;
86 std::string _type_str;
89 class vtkExtractCellType::vtkExtractCellTypeInternal
92 vtkExtractCellTypeInternal():_ref_mtime(0) { }
93 int getNumberOfEntries() const;
94 const char *getKeyOfEntry(int i) const;
95 bool getStatusOfEntryStr(const char *entry) const;
96 void setStatusOfEntryStr(const char *entry, bool status) const;
97 void feedSIL(vtkMutableDirectedGraph *sil) const;
98 std::vector<int> getIdsToKeep() const;
99 void printMySelf(std::ostream& os) const;
100 bool setRefTime(vtkObject *input) const;
102 void loadFrom(const std::map<int,INTERP_KERNEL::NormalizedCellType>& m);
104 const ExtractCellTypeStatus& getEntry(const char *entry) const;
105 bool checkSame(const std::map<int,INTERP_KERNEL::NormalizedCellType>& m) const;
107 std::vector<ExtractCellTypeStatus> _types;
108 mutable unsigned long _ref_mtime;
111 bool vtkExtractCellType::vtkExtractCellTypeInternal::setRefTime(vtkObject *input) const
113 unsigned long mtime(input->GetMTime());
123 std::vector<int> vtkExtractCellType::vtkExtractCellTypeInternal::getIdsToKeep() const
125 std::vector<int> ret;
126 for(std::vector<ExtractCellTypeStatus>::const_iterator it=_types.begin();it!=_types.end();it++)
128 if((*it).getStatus())
129 ret.push_back((*it).getVTKCellType());
134 void vtkExtractCellType::vtkExtractCellTypeInternal::feedSIL(vtkMutableDirectedGraph *sil) const
136 vtkSmartPointer<vtkVariantArray> childEdge(vtkSmartPointer<vtkVariantArray>::New());
137 childEdge->InsertNextValue(0);
138 vtkSmartPointer<vtkVariantArray> crossEdge(vtkSmartPointer<vtkVariantArray>::New());
139 crossEdge->InsertNextValue(1);
140 // CrossEdge is an edge linking hierarchies.
141 vtkUnsignedCharArray* crossEdgesArray=vtkUnsignedCharArray::New();
142 crossEdgesArray->SetName("CrossEdges");
143 sil->GetEdgeData()->AddArray(crossEdgesArray);
144 crossEdgesArray->Delete();
145 std::vector<std::string> names;
146 // Add global fields root
147 vtkIdType root(sil->AddVertex());
148 names.push_back("CellTypesTree");
150 for(std::vector<ExtractCellTypeStatus>::const_iterator it=_types.begin();it!=_types.end();it++)
152 (*it).feedSIL(sil,root,childEdge,names);
154 // This array is used to assign names to nodes.
155 vtkStringArray *namesArray(vtkStringArray::New());
156 namesArray->SetName("Names");
157 namesArray->SetNumberOfTuples(sil->GetNumberOfVertices());
158 sil->GetVertexData()->AddArray(namesArray);
159 namesArray->Delete();
160 std::vector<std::string>::const_iterator iter;
162 for(cc=0, iter=names.begin(); iter!=names.end(); ++iter, ++cc)
163 namesArray->SetValue(cc,(*iter).c_str());
166 void vtkExtractCellType::vtkExtractCellTypeInternal::loadFrom(const std::map<int,INTERP_KERNEL::NormalizedCellType>& m)
171 std::size_t sz(m.size()),ii(0);
173 for(std::map<int,INTERP_KERNEL::NormalizedCellType>::const_iterator it=m.begin();it!=m.end();it++,ii++)
175 ExtractCellTypeStatus elt((*it).first,(*it).second);
180 int vtkExtractCellType::vtkExtractCellTypeInternal::getNumberOfEntries() const
182 return (int) _types.size();
185 const char *vtkExtractCellType::vtkExtractCellTypeInternal::getKeyOfEntry(int i) const
187 return _types[i].getKeyOfEntry();
190 bool vtkExtractCellType::vtkExtractCellTypeInternal::checkSame(const std::map<int,INTERP_KERNEL::NormalizedCellType>& m) const
192 std::size_t sz(m.size());
193 if(sz!=_types.size())
196 std::map<int,INTERP_KERNEL::NormalizedCellType>::const_iterator it(m.begin());
197 for(std::size_t i=0;i<sz && ret;i++,it++)
198 ret=_types[i].isSame((*it).first,(*it).second);
202 const ExtractCellTypeStatus& vtkExtractCellType::vtkExtractCellTypeInternal::getEntry(const char *entry) const
204 std::string entryCpp(entry);
205 for(std::vector<ExtractCellTypeStatus>::const_iterator it0=_types.begin();it0!=_types.end();it0++)
206 if(entryCpp==(*it0).getKey())
208 std::ostringstream oss; oss << "vtkExtractCellTypeInternal::getEntry : no such entry \"" << entry << "\"!";
209 throw INTERP_KERNEL::Exception(oss.str().c_str());
212 bool vtkExtractCellType::vtkExtractCellTypeInternal::getStatusOfEntryStr(const char *entry) const
216 const ExtractCellTypeStatus& elt(getEntry(entry));
217 return elt.getStatus();
219 catch (INTERP_KERNEL::Exception e)
221 //std::cerr << vtkDebugMacro"Exception has been thrown in vtkExtractCellType::vtkExtractCellTypeInternal::getStatusOfEntryStr : " << e.what() << std::endl;
226 void vtkExtractCellType::vtkExtractCellTypeInternal::setStatusOfEntryStr(const char *entry, bool status) const
230 const ExtractCellTypeStatus& elt(getEntry(entry));
231 elt.setStatus(status);
233 catch (INTERP_KERNEL::Exception e)
235 //std::cerr << "Exception has been thrown in vtkExtractCellType::vtkExtractCellTypeInternal::setStatusOfEntryStr : " << e.what() << std::endl;
239 void vtkExtractCellType::vtkExtractCellTypeInternal::printMySelf(std::ostream& os) const
241 for(std::vector<ExtractCellTypeStatus>::const_iterator it0=_types.begin();it0!=_types.end();it0++)
242 (*it0).printMySelf(os);
245 ExtractCellTypeStatus::ExtractCellTypeStatus(int vtkt, INTERP_KERNEL::NormalizedCellType mct):_status(false),_vtkt(vtkt),_mct(mct)
247 std::string name(INTERP_KERNEL::CellModel::GetCellModel(mct).getRepr());
248 _type_str=name.substr(5);//skip "NORM_"
251 void ExtractCellTypeStatus::printMySelf(std::ostream& os) const
253 os << " -" << _type_str << "(";
258 os << ")" << std::endl;
261 bool ExtractCellTypeStatus::isSameAs(const ExtractCellTypeStatus& other) const
263 return _vtkt==other._vtkt && _mct==other._mct;
266 void ExtractCellTypeStatus::feedSIL(vtkMutableDirectedGraph *sil, vtkIdType root, vtkVariantArray *childEdge, std::vector<std::string>& names) const
268 vtkIdType InfoGeoType(sil->AddChild(root,childEdge));
269 names.push_back(_type_str);
270 vtkIdType InfoVTKID(sil->AddChild(InfoGeoType,childEdge));
271 std::ostringstream oss; oss << _vtkt;
272 names.push_back(oss.str());
277 vtkExtractCellType::vtkExtractCellType():SIL(NULL),Internal(new vtkExtractCellTypeInternal),InsideOut(0)
281 vtkExtractCellType::~vtkExtractCellType()
285 delete this->Internal;
288 void vtkExtractCellType::SetInsideOut(int val)
290 if(this->InsideOut!=val)
297 int vtkExtractCellType::RequestInformation(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
301 //std::cerr << "########################################## vtkExtractCellType::RequestInformation ##########################################" << std::endl;
302 vtkInformation *outInfo(outputVector->GetInformationObject(0));
303 vtkInformation *inputInfo(inputVector[0]->GetInformationObject(0));
304 vtkDataSet *input(0);
306 vtkDataObject *inp(inputInfo->Get(vtkDataObject::DATA_OBJECT()));
307 if(vtkDataSet::SafeDownCast(inp))
308 input=vtkDataSet::SafeDownCast(inp);
311 vtkMultiBlockDataSet *inputTmp(vtkMultiBlockDataSet::SafeDownCast(inp));
314 if(inputTmp->GetNumberOfBlocks()!=1)
316 vtkDebugMacro("vtkExtractCellType::RequestInformation : input vtkMultiBlockDataSet must contain exactly 1 block !");
319 vtkDataSet *blk0(vtkDataSet::SafeDownCast(inputTmp->GetBlock(0)));
322 vtkDebugMacro("vtkExtractCellType::RequestInformation : the single block in input vtkMultiBlockDataSet must be a vtkDataSet instance !");
329 vtkDebugMacro("vtkExtractCellType::RequestInformation : supported input are vtkDataSet or vtkMultiBlockDataSet !");
334 if(this->Internal->setRefTime(input))
336 vtkIdType nbOfCells(input->GetNumberOfCells());
337 std::map<int,INTERP_KERNEL::NormalizedCellType> m;
338 for(vtkIdType cellId=0;cellId<nbOfCells;cellId++)
340 int vtkCt(input->GetCellType(cellId));
341 const std::map<int,INTERP_KERNEL::NormalizedCellType>::const_iterator it(m.find(vtkCt));
344 const unsigned char *pos(std::find(MEDCoupling::MEDMeshMultiLev::PARAMEDMEM_2_VTKTYPE,MEDCoupling::MEDMeshMultiLev::PARAMEDMEM_2_VTKTYPE+MEDCoupling::MEDMeshMultiLev::PARAMEDMEM_2_VTKTYPE_LGTH,vtkCt));
345 if(pos==MEDCoupling::MEDMeshMultiLev::PARAMEDMEM_2_VTKTYPE+MEDCoupling::MEDMeshMultiLev::PARAMEDMEM_2_VTKTYPE_LGTH)
347 vtkDebugMacro("vtkExtractCellType::RequestInformation : cell #" << cellId << " has unrecognized type !");
350 m[vtkCt]=(INTERP_KERNEL::NormalizedCellType)std::distance(MEDCoupling::MEDMeshMultiLev::PARAMEDMEM_2_VTKTYPE,pos);
353 this->Internal->loadFrom(m);
356 this->SIL=vtkMutableDirectedGraph::New();
357 this->Internal->feedSIL(this->SIL);
359 outInfo->Set(vtkDataObject::SIL(),this->SIL);
362 catch(INTERP_KERNEL::Exception& e)
364 std::cerr << "Exception has been thrown in vtkExtractCellType::RequestInformation : " << e.what() << std::endl;
370 vtkDataSet *FilterFamilies(vtkDataSet *input, const std::vector<int>& idsToKeep, bool insideOut)
372 const int VTK_DATA_ARRAY_DELETE=vtkDataArrayTemplate<double>::VTK_DATA_ARRAY_DELETE;
373 const char ZE_SELECTION_ARR_NAME[]="@@ZeSelection@@";
374 vtkDataSet *output(input->NewInstance());
375 output->ShallowCopy(input);
376 vtkSmartPointer<vtkThreshold> thres(vtkSmartPointer<vtkThreshold>::New());
377 thres->SetInputData(output);
378 vtkDataSetAttributes *dscIn(input->GetCellData()),*dscIn2(input->GetPointData());
379 vtkDataSetAttributes *dscOut(output->GetCellData()),*dscOut2(output->GetPointData());
381 double vMin(insideOut==0?1.:0.),vMax(insideOut==0?2.:1.);
382 thres->ThresholdBetween(vMin,vMax);
384 vtkIdType nbOfCells(input->GetNumberOfCells());
385 vtkCharArray *zeSelection(vtkCharArray::New());
386 zeSelection->SetName(ZE_SELECTION_ARR_NAME);
387 zeSelection->SetNumberOfComponents(1);
388 char *pt(new char[nbOfCells]);
389 zeSelection->SetArray(pt,nbOfCells,0,VTK_DATA_ARRAY_DELETE);
390 std::fill(pt,pt+nbOfCells,0);
391 std::vector<bool> pt2(nbOfCells,false);
392 for(std::vector<int>::const_iterator it=idsToKeep.begin();it!=idsToKeep.end();it++)
394 for(vtkIdType ii=0;ii<nbOfCells;ii++)
396 if(input->GetCellType(ii)==*it)
400 for(int ii=0;ii<nbOfCells;ii++)
403 int idx(output->GetCellData()->AddArray(zeSelection));
404 output->GetCellData()->SetActiveAttribute(idx,vtkDataSetAttributes::SCALARS);
405 output->GetCellData()->CopyScalarsOff();
406 zeSelection->Delete();
408 thres->SetInputArrayToProcess(idx,0,0,"vtkDataObject::FIELD_ASSOCIATION_CELLS",ZE_SELECTION_ARR_NAME);
410 vtkUnstructuredGrid *zeComputedOutput(thres->GetOutput());
411 zeComputedOutput->GetCellData()->RemoveArray(idx);
413 zeComputedOutput->Register(0);
414 return zeComputedOutput;
417 int vtkExtractCellType::RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
421 //std::cerr << "########################################## vtkExtractCellType::RequestData ##########################################" << std::endl;
422 vtkInformation* inputInfo=inputVector[0]->GetInformationObject(0);
423 vtkDataSet *input(vtkDataSet::SafeDownCast(inputInfo->Get(vtkDataObject::DATA_OBJECT())));
424 vtkInformation *info(input->GetInformation());
425 vtkInformation *outInfo(outputVector->GetInformationObject(0));
426 vtkDataSet *output(vtkDataSet::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT())));
427 std::vector<int> idsToKeep(this->Internal->getIdsToKeep());
428 vtkDataSet *tryOnCell(FilterFamilies(input,idsToKeep,this->InsideOut));
429 // first shrink the input
430 output->ShallowCopy(tryOnCell);
433 catch(INTERP_KERNEL::Exception& e)
435 std::cerr << "Exception has been thrown in vtkExtractCellType::RequestData : " << e.what() << std::endl;
441 int vtkExtractCellType::GetSILUpdateStamp()
443 return (int)this->SILTime;
446 void vtkExtractCellType::PrintSelf(ostream& os, vtkIndent indent)
448 this->Superclass::PrintSelf(os, indent);
451 int vtkExtractCellType::GetNumberOfGeoTypesArrays()
453 int ret(this->Internal->getNumberOfEntries());
454 //std::cerr << "vtkExtractCellType::GetNumberOfGeoTypesArrays() -> " << ret << std::endl;
458 const char *vtkExtractCellType::GetGeoTypesArrayName(int index)
460 const char *ret(this->Internal->getKeyOfEntry(index));
461 //std::cerr << "vtkExtractCellType::GetGeoTypesArrayName(" << index << ") -> " << ret << std::endl;
465 int vtkExtractCellType::GetGeoTypesArrayStatus(const char *name)
467 int ret((int)this->Internal->getStatusOfEntryStr(name));
468 //std::cerr << "vtkExtractCellType::GetGeoTypesArrayStatus(" << name << ") -> " << ret << std::endl;
472 void vtkExtractCellType::SetGeoTypesStatus(const char *name, int status)
474 //std::cerr << "vtkExtractCellType::SetGeoTypesStatus(" << name << "," << status << ")" << std::endl;
475 if (GetNumberOfGeoTypesArrays()<1)
477 this->Internal->setStatusOfEntryStr(name,(bool)status);
478 if(std::string(name)==GetGeoTypesArrayName(GetNumberOfGeoTypesArrays()-1))
481 //this->Internal->printMySelf(std::cerr);