Salome HOME
Copyright update 2022
[modules/paravis.git] / src / Plugins / MEDReader / plugin / MEDReaderIO / vtkGroupsNames.cxx
1 // Copyright (C) 2020-2022  CEA/DEN, 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 "vtkGroupsNames.h"
22 #include "ExtractGroupHelper.h"
23 #include "vtkMEDReader.h"
24 #include "vtkUgSelectCellIds.h"
25 #include "MEDFileFieldRepresentationTree.hxx"
26 #include "vtkLongArray.h"
27 #include "VTKMEDTraits.hxx"
28
29 #include <vtkCellArray.h>
30 #include <vtkCellData.h>
31 #include "vtkMutableDirectedGraph.h"
32 #include "vtkInformationDataObjectMetaDataKey.h"
33 #include <vtkCompositeDataToUnstructuredGridFilter.h>
34 #include <vtkMultiBlockDataGroupFilter.h>
35 #include <vtkDoubleArray.h>
36 #include <vtkInformation.h>
37 #include <vtkInformationVector.h>
38 #include <vtkNew.h>
39 #include <vtkPVGlyphFilter.h>
40 #include <vtkPointData.h>
41 #include <vtkPolyData.h>
42 #include <vtkUnstructuredGrid.h>
43 #include <vtkMultiBlockDataSet.h>
44 #include <vtkCellCenters.h>
45 #include <vtkGlyphSource2D.h>
46 #include "vtkTable.h"
47 #include "vtkStringArray.h"
48
49 class vtkGroupsNamesInternal : public ExtractGroupInternal
50 {
51 };
52
53 vtkStandardNewMacro(vtkGroupsNames)
54
55 vtkGroupsNames::vtkGroupsNames():Internal(new ExtractGroupInternal)
56 {
57 }
58
59 vtkGroupsNames::~vtkGroupsNames()
60 {
61   delete this->Internal;
62 }
63
64 int vtkGroupsNames::RequestInformation(vtkInformation * /*request*/, vtkInformationVector **inputVector, vtkInformationVector */*outputVector*/)
65 {
66   //vtkInformation *outInfo(outputVector->GetInformationObject(0)); // todo: unused
67   vtkInformation *inputInfo(inputVector[0]->GetInformationObject(0));
68   if(!ExtractGroupInternal::IndependantIsInformationOK(vtkMEDReader::META_DATA(),inputInfo))
69   {
70     vtkErrorMacro("No SIL Data available ! The source of this filter must be MEDReader !");
71     return 0;
72   }
73   vtkMutableDirectedGraph *mdg(vtkMutableDirectedGraph::SafeDownCast(inputInfo->Get(vtkMEDReader::META_DATA())));
74   this->Internal->loadFrom(mdg);
75   return 1;
76 }
77
78 int vtkGroupsNames::RequestData(vtkInformation *vtkNotUsed(request), vtkInformationVector **inputVector, vtkInformationVector *outputVector)
79 {
80   vtkInformation* inputInfo=inputVector[0]->GetInformationObject(0);
81   vtkDataSet *input(nullptr);
82   vtkMultiBlockDataSet *inputMB(vtkMultiBlockDataSet::SafeDownCast(inputInfo->Get(vtkDataObject::DATA_OBJECT())));
83   if(inputMB)
84   {
85     if(inputMB->GetNumberOfBlocks()!=1)
86     {
87       vtkErrorMacro("vtkGroupsNames::RequestData : input has not the right number of parts ! Expected 1 !");
88       return 0;
89     }
90     input = vtkDataSet::SafeDownCast(inputMB->GetBlock(0));
91   }
92   else
93   {
94     input = vtkDataSet::SafeDownCast(inputInfo->Get(vtkDataObject::DATA_OBJECT()));
95   }
96   if(!input)
97   {
98     vtkErrorMacro("vtkGroupsNames::RequestData : input is neither a DataSet nor a MultiblockDataSet !");
99     return 0;
100   }
101   //vtkInformation *info(input->GetInformation()); // todo: unused
102   vtkInformation *outInfo(outputVector->GetInformationObject(0));
103   vtkTable *output(vtkTable::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT())));
104   if(!output)
105   {
106     vtkErrorMacro("vtkGroupsNames::RequestData : something wrong !");
107     return 0; 
108   }
109   //
110   vtkUnstructuredGrid *inputc(vtkUnstructuredGrid::SafeDownCast(input));
111   if(!inputc)
112   {
113     vtkErrorMacro("vtkGroupsNames::RequestData : Only implemented for vtkUnstructuredGrid !");
114     return 0; 
115   }
116   //
117   vtkDataArray *famIdsGen(inputc->GetCellData()->GetScalars(MEDFileFieldRepresentationLeavesArrays::FAMILY_ID_CELL_NAME));
118   if(!famIdsGen)
119   {
120     vtkErrorMacro(<< "vtkGroupsNames::RequestData : Fail to locate " << MEDFileFieldRepresentationLeavesArrays::FAMILY_ID_CELL_NAME << " array !");
121     return 0;
122   }
123   using vtkMCIdTypeArray = MEDFileVTKTraits<mcIdType>::VtkType;
124   vtkMCIdTypeArray *famIdsArr(vtkMCIdTypeArray::SafeDownCast(famIdsGen));
125   if(!famIdsArr)
126   {
127     vtkErrorMacro(<< "vtkGroupsNames::RequestData : cell array " << MEDFileFieldRepresentationLeavesArrays::FAMILY_ID_CELL_NAME << " is exepected to be IdType !");
128     return 0; 
129   }
130   // Let's go !
131   std::vector< std::pair<std::string,std::vector<int> > > allGroups(this->Internal->getAllGroups());
132   vtkNew<vtkIntArray> blockIdArr;
133   blockIdArr->SetName("Block ID");
134   blockIdArr->SetNumberOfTuples(allGroups.size());
135   vtkNew<vtkStringArray> groupNameArr;
136   groupNameArr->SetName("Group Name");
137   groupNameArr->SetNumberOfTuples(allGroups.size());
138   int blockId(0);
139   for(const auto& grp : allGroups)
140   {
141     blockIdArr->SetValue(blockId,blockId);
142     groupNameArr->SetValue(blockId,vtkStdString(grp.first));
143     blockId++;
144   }
145   output->AddColumn(blockIdArr);
146   output->AddColumn(groupNameArr);
147   return 1;
148 }