Salome HOME
Implementation of the '23082: [CEA 1403] Filter to rename fields or components' impro...
[modules/paravis.git] / src / Plugins / ArrayRenamer / vtkArrayRenamerFilter.cxx
1 // Copyright (C) 2014-2015  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 : Roman NIKOLAEV
20
21 #include "vtkArrayRenamerFilter.h"
22
23 #include <vtkCellData.h>
24 #include <vtkDataArray.h>
25 #include <vtkDataObjectTreeIterator.h>
26 #include <vtkDataSet.h>
27 #include <vtkDoubleArray.h>
28 #include <vtkInformation.h>
29 #include <vtkInformationVector.h>
30 #include <vtkMultiBlockDataSet.h>
31 #include <vtkObjectFactory.h>
32 #include <vtkPointData.h>
33 #include <vtkStreamingDemandDrivenPipeline.h>
34 #include <vtkStringArray.h>
35 #include <vtkUnstructuredGrid.h>
36
37 #include <map>
38 #include <string>
39 #include <vector>
40
41 //For debug 
42 //#include <iostream>
43
44 class vtkArrayRenamerFilter::vtkInternals 
45 {
46 public:
47   //Vector which contains information about array's components : origin_component_name <-> new_name
48   typedef std::vector< std::pair<int, std::string> > ComponentsInfo;
49   
50   struct ArrayInfo
51   {
52   std::string   NewName;                               //  New name of the array
53   bool          CopyArray;                             //  Make copy of the array or keep origin array, but change the name
54   ComponentsInfo ComponentVector;                      //  Components of the array
55     ArrayInfo():
56       NewName( "" ),
57       CopyArray( false )
58     {
59     }
60   };
61   typedef std::map<std::string, ArrayInfo> ArraysType;  //  Map : origin_aray_name <-> ArrayInfo struct
62   ArraysType Arrays;
63 };
64
65
66 //------------------------------------------------------------------------------
67 vtkStandardNewMacro(vtkArrayRenamerFilter);
68 //--------------------------------------------------------------------------------------------------
69 vtkArrayRenamerFilter::vtkArrayRenamerFilter()
70 {
71   this->Internals = new vtkInternals();
72 }
73
74 //--------------------------------------------------------------------------------------------------
75 vtkArrayRenamerFilter::~vtkArrayRenamerFilter()
76 {
77   delete this->Internals;
78 }
79
80
81
82 //--------------------------------------------------------------------------------------------------
83 int vtkArrayRenamerFilter::RequestData( vtkInformation*        theRequest,
84                                         vtkInformationVector** theInputVector,
85                                         vtkInformationVector*  theOutputVector )
86 {
87
88   //std::cout<<"vtkArrayRenamerFilter::RequestData  !!! "<<std::endl;
89   
90   // Get the information
91   vtkInformation *anInputInfo  = theInputVector[0]->GetInformationObject( 0 );
92   vtkInformation *anOutputInfo = theOutputVector->GetInformationObject( 0 );
93
94   vtkDataSet* anInput = vtkDataSet::GetData(theInputVector[0], 0);
95   vtkDataSet* anOutput = vtkDataSet::GetData(theOutputVector, 0);
96   anOutput->DeepCopy(anInput);
97   vtkFieldData* data = 0;
98   
99   vtkInternals::ArraysType::iterator it = this->Internals->Arrays.begin();
100   for( ; it !=  this->Internals->Arrays.end(); it ++ ) {
101     vtkDataArray* array = anOutput->GetPointData()->GetArray( it->first.c_str() );
102     data =  anOutput->GetPointData();
103     if( !array ) {
104       array = anOutput->GetCellData()->GetArray( it->first.c_str() );
105       data =  anOutput->GetCellData();
106     }
107     
108     if( array && !it->second.NewName.empty() ) {
109       
110       if ( it->second.CopyArray ) {
111         vtkDataArray* new_array = array->NewInstance();
112         new_array->DeepCopy(array);
113         data->AddArray(new_array);
114         array = new_array;
115       } 
116       array->SetName(it->second.NewName.c_str());
117     }
118     
119     if ( array ) {
120       vtkInternals::ComponentsInfo::iterator vect_it = it->second.ComponentVector.begin();
121       for( ; vect_it != it->second.ComponentVector.end(); vect_it++ ) {
122         array->SetComponentName( vect_it->first, vect_it->second.c_str() );
123       }
124     }
125   }
126   
127   return Superclass::RequestData( theRequest, theInputVector, theOutputVector );
128 }
129
130
131 void vtkArrayRenamerFilter::SetComponentInfo( const char* arrayname, const int compid, const char* newcompname ) {
132   //std::cout<<"vtkArrayRenamerFilter::SetComponentArrayInfo : "<<arrayname<<" "<<"id : "<<compid<<" , newcompname = "<<newcompname<<std::endl;
133
134   vtkInternals::ArraysType::iterator it = this->Internals->Arrays.find(arrayname);
135
136   if ( it == this->Internals->Arrays.end() ) {
137     vtkInternals::ArraysType::iterator it1 = this->Internals->Arrays.begin();
138     for( ; it1 != this->Internals->Arrays.end(); it1 ++ ) {
139       if( it1->second.NewName.compare(arrayname) == 0 ) {
140         it = it1;
141         break;
142       }      
143     }
144   }
145
146   if( it == this->Internals->Arrays.end() ) {
147     std::pair<vtkInternals::ArraysType::iterator,bool> ret;
148     ret = this->Internals->Arrays.insert ( std::pair<std::string, vtkInternals::ArrayInfo>( arrayname, vtkInternals::ArrayInfo() ) );
149     it = ret.first;
150   }
151   
152   if( it != this->Internals->Arrays.end() ) {
153     vtkInternals::ComponentsInfo::iterator vect_it = it->second.ComponentVector.begin();
154     for( ; vect_it != it->second.ComponentVector.end(); vect_it++ ) {
155       if ( vect_it->first ==  compid )
156         break;
157     }
158
159     if ( vect_it != it->second.ComponentVector.end() ) {
160       vect_it->second = newcompname;
161     } else {
162       it->second.ComponentVector.push_back( std::pair<int,std::string>( compid,newcompname ) );
163     }
164   }
165   this->Modified();
166 }
167
168 void vtkArrayRenamerFilter::SetArrayInfo( const char* originarrayname, const char* newarrayname, bool copy ) {
169   
170   //std::cout<<"vtkArrayRenamerFilter::SetArrayInfo : "<<originarrayname<<" "<<"new name :"<<newarrayname<<" , copy = "<<copy<<std::endl;
171   
172   vtkInternals::ArraysType::iterator it = this->Internals->Arrays.find(originarrayname);
173     
174   if ( it == this->Internals->Arrays.end() )
175     this->Internals->Arrays.insert ( std::pair<std::string, vtkInternals::ArrayInfo>( originarrayname, vtkInternals::ArrayInfo() ) );
176
177   this->Internals->Arrays[originarrayname].NewName = newarrayname;
178   this->Internals->Arrays[originarrayname].CopyArray = copy;
179   this->Modified();
180 }
181
182 void vtkArrayRenamerFilter::ClearArrayInfo() {
183   //std::cout<<"vtkArrayRenamerFilter::ClearArrayInfo"<<std::endl;
184   this->Internals->Arrays.clear();
185   this->Modified();
186 }
187
188 void vtkArrayRenamerFilter::ClearComponentsInfo() {
189   //std::cout<<"vtkArrayRenamerFilter::ClearComponentsInfo"<<std::endl;
190   vtkInternals::ArraysType::iterator it = this->Internals->Arrays.begin();
191   for( ; it != this->Internals->Arrays.end(); it ++ ){
192     it->second.ComponentVector.clear();
193   }
194   this->Modified();
195 }