Salome HOME
Copyright update 2022
[modules/paravis.git] / src / Plugins / MEDReader / plugin / MEDReaderIO / vtkGroupAsMultiBlock.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 "vtkGroupAsMultiBlock.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
47 class vtkGroupAsMultiBlockInternal : public ExtractGroupInternal
48 {
49 };
50
51 vtkStandardNewMacro(vtkGroupAsMultiBlock)
52
53 vtkGroupAsMultiBlock::vtkGroupAsMultiBlock():Internal(new ExtractGroupInternal)
54 {
55 }
56
57 vtkGroupAsMultiBlock::~vtkGroupAsMultiBlock()
58 {
59   delete this->Internal;
60 }
61
62 int vtkGroupAsMultiBlock::RequestInformation(vtkInformation */*request*/, vtkInformationVector **inputVector, vtkInformationVector */*outputVector*/)
63 {
64   //vtkInformation *outInfo(outputVector->GetInformationObject(0)); // todo: unused
65   vtkInformation *inputInfo(inputVector[0]->GetInformationObject(0));
66   if(!ExtractGroupInternal::IndependantIsInformationOK(vtkMEDReader::META_DATA(),inputInfo))
67   {
68     vtkErrorMacro("No SIL Data available ! The source of this filter must be MEDReader !");
69     return 0;
70   }
71   vtkMutableDirectedGraph *mdg(vtkMutableDirectedGraph::SafeDownCast(inputInfo->Get(vtkMEDReader::META_DATA())));
72   this->Internal->loadFrom(mdg);
73   return 1;
74 }
75
76 int vtkGroupAsMultiBlock::RequestData(vtkInformation *vtkNotUsed(request), vtkInformationVector **inputVector, vtkInformationVector *outputVector)
77 {
78   vtkInformation* inputInfo=inputVector[0]->GetInformationObject(0);
79   vtkMultiBlockDataSet *inputMB(vtkMultiBlockDataSet::SafeDownCast(inputInfo->Get(vtkDataObject::DATA_OBJECT())));
80   if(inputMB->GetNumberOfBlocks()!=1)
81   {
82     vtkErrorMacro("vtkGroupAsMultiBlock::RequestData : input has not the right number of parts ! Expected 1 !");
83     return 0;
84   }
85   vtkDataSet *input(vtkDataSet::SafeDownCast(inputMB->GetBlock(0)));
86   //vtkInformation *info(input->GetInformation()); // todo: unused
87   vtkInformation *outInfo(outputVector->GetInformationObject(0));
88   vtkMultiBlockDataSet *output(vtkMultiBlockDataSet::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT())));
89   if(!output)
90   {
91     vtkErrorMacro("vtkGroupAsMultiBlock::RequestData : something wrong !");
92     return 0; 
93   }
94   //
95   vtkUnstructuredGrid *inputc(vtkUnstructuredGrid::SafeDownCast(input));
96   if(!inputc)
97   {
98     vtkErrorMacro("vtkGroupAsMultiBlock::RequestData : Only implemented for vtkUnstructuredGrid !");
99     return 0; 
100   }
101   //
102   vtkDataArray *famIdsGen(inputc->GetCellData()->GetScalars(MEDFileFieldRepresentationLeavesArrays::FAMILY_ID_CELL_NAME));
103   if(!famIdsGen)
104   {
105     vtkErrorMacro(<< "vtkGroupAsMultiBlock::RequestData : Fail to locate " << MEDFileFieldRepresentationLeavesArrays::FAMILY_ID_CELL_NAME << " array !");
106     return 0;
107   }
108   using vtkMCIdTypeArray = MEDFileVTKTraits<mcIdType>::VtkType;
109   vtkMCIdTypeArray *famIdsArr(vtkMCIdTypeArray::SafeDownCast(famIdsGen));
110   if(!famIdsArr)
111   {
112     vtkErrorMacro(<< "vtkGroupAsMultiBlock::RequestData : cell array " << MEDFileFieldRepresentationLeavesArrays::FAMILY_ID_CELL_NAME << " is exepected to be IdType !");
113     return 0; 
114   }
115   // Let's go !
116   vtkIdType inputNbCell(famIdsArr->GetNumberOfTuples());
117   std::vector< std::pair<std::string,std::vector<int> > > allGroups(this->Internal->getAllGroups());
118   output->SetNumberOfBlocks(allGroups.size());
119   int blockId(0);
120   for(const auto& grp : allGroups)
121   {
122     std::vector<vtkIdType> ids;
123     // iterate on all families on current group grp
124     for(const auto& famId : grp.second)
125     {// for each family of group grp locate cells in input lying on that group
126       vtkIdType pos(0);
127       std::for_each(famIdsArr->GetPointer(0),famIdsArr->GetPointer(inputNbCell),[&pos,&ids,famId](vtkIdType curFamId) { if(curFamId == famId) { ids.push_back(pos); } pos++; });
128     }
129     std::sort(ids.begin(),ids.end());
130     vtkNew<vtkIdTypeArray> idsVTK;
131     idsVTK->SetNumberOfComponents(1); idsVTK->SetNumberOfTuples(ids.size());
132     std::copy(ids.begin(),ids.end(),idsVTK->GetPointer(0));
133     vtkNew<vtkUgSelectCellIds> cellSelector;
134     cellSelector->SetIds(idsVTK);
135     cellSelector->SetInputData(inputc);
136     cellSelector->Update();
137     output->SetBlock(blockId++,cellSelector->GetOutput());
138   }
139   return 1;
140 }