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