Salome HOME
New extract cell type
[modules/paravis.git] / src / Plugins / MEDReader / plugin / MEDReaderIO / vtkExtractCellType.cxx
1 #include "vtkExtractCellType.h"
2
3 #include "vtkCell.h"
4 #include "vtkCellTypes.h"
5 #include "vtkCompositeDataSet.h"
6 #include "vtkCompositeDataIterator.h"
7 #include "vtkDataArraySelection.h"
8 #include "vtkDataSet.h"
9 #include "vtkExtractSelection.h"
10 #include "vtkIdTypeArray.h"
11 #include "vtkInformation.h"
12 #include "vtkObjectFactory.h"
13 #include "vtkSelection.h"
14 #include "vtkSelectionNode.h"
15
16 vtkStandardNewMacro(vtkExtractCellType);
17
18 //----------------------------------------------------------------------------
19 vtkExtractCellType::vtkExtractCellType(){}
20
21 //----------------------------------------------------------------------------
22 vtkExtractCellType::~vtkExtractCellType(){}
23
24 //----------------------------------------------------------------------------
25 void vtkExtractCellType::AppendToGeoTypes(vtkDataSet* dataset)
26 {
27   for(int i = 0; i < dataset->GetNumberOfCells(); i++)
28   {
29     vtkCell* cell = dataset->GetCell(i);
30
31     // TODO use MEDCoupling CellType API
32     const char* cellType = vtkCellTypes::GetClassNameFromTypeId(cell->GetCellType());
33     if (!this->FieldSelection->ArrayExists(cellType))
34     {
35       this->FieldSelection->AddArray(cellType, false);
36     }
37   }
38 }
39
40 //----------------------------------------------------------------------------
41 void vtkExtractCellType::SelectIds(vtkDataSet* dataset, vtkIdTypeArray* selArr)
42 {
43   for(int i = 0; i < dataset->GetNumberOfCells(); i++)
44   {
45     vtkCell* cell = dataset->GetCell(i);
46     
47     // TODO use MEDCoupling CellType API
48     const char* cellType = vtkCellTypes::GetClassNameFromTypeId(cell->GetCellType());
49
50     // Needed as append to geotypes may not have been done in request information
51     // when loading state
52     if (!this->FieldSelection->ArrayExists(cellType))
53     {
54       this->FieldSelection->AddArray(cellType, false);
55     }
56     else if(this->FieldSelection->ArrayIsEnabled(cellType))
57     {
58       selArr->InsertNextValue(i);
59     }
60   }
61 }
62
63 //----------------------------------------------------------------------------
64 int vtkExtractCellType::RequestInformation(vtkInformation* req, vtkInformationVector** inputVector, vtkInformationVector* outputVector)
65 {
66   vtkDataSet* inputDS = vtkDataSet::GetData(inputVector[0]);
67   vtkCompositeDataSet* inputComposite = vtkCompositeDataSet::GetData(inputVector[0]);
68
69   if (inputDS)
70   {
71     this->AppendToGeoTypes(inputDS);
72   }
73   else if(inputComposite)
74   {
75     vtkSmartPointer<vtkCompositeDataIterator> iter;
76     iter.TakeReference(inputComposite->NewIterator());
77     for (iter->InitTraversal(); !iter->IsDoneWithTraversal(); iter->GoToNextItem())
78     {
79       this->AppendToGeoTypes(vtkDataSet::SafeDownCast(inputComposite->GetDataSet(iter)));
80     }
81   }
82
83   return 1;
84 }
85
86 //----------------------------------------------------------------------------
87 int vtkExtractCellType::RequestData(vtkInformation* req, vtkInformationVector** inputVector, vtkInformationVector* outputVector)
88 {
89   // Get the input and output data objects.
90   vtkDataSet* input = vtkDataSet::GetData(inputVector[0]);
91   vtkDataSet* output = vtkDataSet::GetData(outputVector);
92
93   // Create a selection, sel1, of cells with indices 7643-7499-7355-7211
94   vtkNew<vtkIdTypeArray> selArr1;
95
96   vtkDataSet* inputDS = vtkDataSet::GetData(inputVector[0]);
97   vtkCompositeDataSet* inputComposite = vtkCompositeDataSet::GetData(inputVector[0]);
98
99   if (inputDS)
100   {
101     this->SelectIds(inputDS, selArr1);
102   }
103   else if(inputComposite)
104   {
105     vtkSmartPointer<vtkCompositeDataIterator> iter;
106     iter.TakeReference(inputComposite->NewIterator());
107     for (iter->InitTraversal(); !iter->IsDoneWithTraversal(); iter->GoToNextItem())
108     {
109       this->SelectIds(vtkDataSet::SafeDownCast(inputComposite->GetDataSet(iter)), selArr1);
110     }
111   }
112
113   vtkNew<vtkSelectionNode> selNode1;
114   selNode1->SetContentType(vtkSelectionNode::INDICES);
115   selNode1->SetFieldType(vtkSelectionNode::CELL);
116   selNode1->GetProperties()->Set(vtkSelectionNode::COMPOSITE_INDEX(), 1);
117   selNode1->GetProperties()->Set(vtkSelectionNode::INVERSE(), this->InsideOut);
118   selNode1->SetSelectionList(selArr1);
119   vtkNew<vtkSelection> sel1;
120   sel1->AddNode(selNode1);
121
122   this->SetInputData(1, sel1);
123   this->Superclass::RequestData(req, inputVector, outputVector);
124   return 1;
125 }
126
127 //----------------------------------------------------------------------------
128 void vtkExtractCellType::PrintSelf(ostream& os, vtkIndent indent)
129 {
130   this->Superclass::PrintSelf(os, indent);
131 }
132 //------------------------------------------------------------------------------
133 int vtkExtractCellType::GetNumberOfGeoTypesArrays()
134 {
135   return this->FieldSelection->GetNumberOfArrays();
136 }
137
138 //------------------------------------------------------------------------------
139 const char *vtkExtractCellType::GetGeoTypesArrayName(int index)
140 {
141   return this->FieldSelection->GetArrayName(index) + 3;  // String parsing -- remove "vtk"
142 }
143
144 //------------------------------------------------------------------------------
145 int vtkExtractCellType::GetGeoTypesArrayStatus(const char *name)
146 {
147   return this->FieldSelection->ArrayIsEnabled(name);
148 }
149
150 //------------------------------------------------------------------------------
151 void vtkExtractCellType::SetGeoTypesStatus(const char *name, int status)
152 {
153   std::string complete_name = "vtk";
154   complete_name.append(name);
155   if (this->GetGeoTypesArrayStatus(complete_name.c_str()) != status)
156   {
157     if (status)
158     {
159       this->FieldSelection->EnableArray(complete_name.c_str());
160     }
161     else
162     {
163       this->FieldSelection->DisableArray(complete_name.c_str());
164     }
165     this->Modified();
166   }
167 }