1 // Copyright (C) 2014-2016 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Roman NIKOLAEV
21 #include "vtkArrayRenamerFilter.h"
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>
44 class vtkArrayRenamerFilter::vtkInternals
47 //Vector which contains information about array's components : origin_component_name <-> new_name
48 typedef std::vector< std::pair<int, std::string> > ComponentsInfo;
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
61 typedef std::map<std::string, ArrayInfo> ArraysType; // Map : origin_aray_name <-> ArrayInfo struct
66 //------------------------------------------------------------------------------
67 vtkStandardNewMacro(vtkArrayRenamerFilter);
68 //--------------------------------------------------------------------------------------------------
69 vtkArrayRenamerFilter::vtkArrayRenamerFilter()
71 this->Internals = new vtkInternals();
74 //--------------------------------------------------------------------------------------------------
75 vtkArrayRenamerFilter::~vtkArrayRenamerFilter()
77 delete this->Internals;
82 //--------------------------------------------------------------------------------------------------
83 int vtkArrayRenamerFilter::RequestData( vtkInformation* theRequest,
84 vtkInformationVector** theInputVector,
85 vtkInformationVector* theOutputVector )
88 //std::cout<<"vtkArrayRenamerFilter::RequestData !!! "<<std::endl;
90 // Get the information
91 vtkInformation *anInputInfo = theInputVector[0]->GetInformationObject( 0 );
92 vtkInformation *anOutputInfo = theOutputVector->GetInformationObject( 0 );
94 vtkDataSet* anInput = vtkDataSet::GetData(theInputVector[0], 0);
95 vtkDataSet* anOutput = vtkDataSet::GetData(theOutputVector, 0);
96 anOutput->DeepCopy(anInput);
97 vtkFieldData* data = 0;
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();
104 array = anOutput->GetCellData()->GetArray( it->first.c_str() );
105 data = anOutput->GetCellData();
108 if( array && !it->second.NewName.empty() ) {
110 if ( it->second.CopyArray ) {
111 vtkDataArray* new_array = array->NewInstance();
112 new_array->DeepCopy(array);
113 data->AddArray(new_array);
116 array->SetName(it->second.NewName.c_str());
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() );
127 return Superclass::RequestData( theRequest, theInputVector, theOutputVector );
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;
134 vtkInternals::ArraysType::iterator it = this->Internals->Arrays.find(arrayname);
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 ) {
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() ) );
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 )
159 if ( vect_it != it->second.ComponentVector.end() ) {
160 vect_it->second = newcompname;
162 it->second.ComponentVector.push_back( std::pair<int,std::string>( compid,newcompname ) );
168 void vtkArrayRenamerFilter::SetArrayInfo( const char* originarrayname, const char* newarrayname, bool copy ) {
170 //std::cout<<"vtkArrayRenamerFilter::SetArrayInfo : "<<originarrayname<<" "<<"new name :"<<newarrayname<<" , copy = "<<copy<<std::endl;
172 vtkInternals::ArraysType::iterator it = this->Internals->Arrays.find(originarrayname);
174 if ( it == this->Internals->Arrays.end() )
175 this->Internals->Arrays.insert ( std::pair<std::string, vtkInternals::ArrayInfo>( originarrayname, vtkInternals::ArrayInfo() ) );
177 this->Internals->Arrays[originarrayname].NewName = newarrayname;
178 this->Internals->Arrays[originarrayname].CopyArray = copy;
182 void vtkArrayRenamerFilter::ClearArrayInfo() {
183 //std::cout<<"vtkArrayRenamerFilter::ClearArrayInfo"<<std::endl;
184 this->Internals->Arrays.clear();
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();