1 // Copyright (C) 2014-2022 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>
62 ArraysType; // Map : origin_aray_name <-> ArrayInfo struct
66 //------------------------------------------------------------------------------
67 vtkStandardNewMacro(vtkArrayRenamerFilter)
69 //--------------------------------------------------------------------------------------------------
70 vtkArrayRenamerFilter::vtkArrayRenamerFilter()
72 this->Internals = new vtkInternals();
75 //--------------------------------------------------------------------------------------------------
76 vtkArrayRenamerFilter::~vtkArrayRenamerFilter()
78 delete this->Internals;
81 //--------------------------------------------------------------------------------------------------
82 int vtkArrayRenamerFilter::RequestData(vtkInformation* theRequest,
83 vtkInformationVector** theInputVector, vtkInformationVector* theOutputVector)
85 // std::cout<<"vtkArrayRenamerFilter::RequestData !!! "<<std::endl;
87 // Get the information
88 //vtkInformation* anInputInfo = theInputVector[0]->GetInformationObject(0); // todo: unused
89 //vtkInformation* anOutputInfo = theOutputVector->GetInformationObject(0); // todo: unused
91 vtkDataSet* anInput = vtkDataSet::GetData(theInputVector[0], 0);
92 vtkDataSet* anOutput = vtkDataSet::GetData(theOutputVector, 0);
93 anOutput->DeepCopy(anInput);
94 vtkFieldData* data = 0;
96 vtkInternals::ArraysType::iterator it = this->Internals->Arrays.begin();
97 for (; it != this->Internals->Arrays.end(); it++)
99 vtkDataArray* array = anOutput->GetPointData()->GetArray(it->first.c_str());
100 data = anOutput->GetPointData();
103 array = anOutput->GetCellData()->GetArray(it->first.c_str());
104 data = anOutput->GetCellData();
107 if (array && !it->second.NewName.empty())
110 if (it->second.CopyArray)
112 vtkDataArray* new_array = array->NewInstance();
113 new_array->DeepCopy(array);
114 data->AddArray(new_array);
117 array->SetName(it->second.NewName.c_str());
122 vtkInternals::ComponentsInfo::iterator vect_it = it->second.ComponentVector.begin();
123 for (; vect_it != it->second.ComponentVector.end(); vect_it++)
125 array->SetComponentName(vect_it->first, vect_it->second.c_str());
130 return Superclass::RequestData(theRequest, theInputVector, theOutputVector);
133 void vtkArrayRenamerFilter::SetComponentInfo(
134 const char* arrayname, const int compid, const char* newcompname)
136 // std::cout<<"vtkArrayRenamerFilter::SetComponentArrayInfo : "<<arrayname<<" "<<"id :
137 // "<<compid<<" , newcompname = "<<newcompname<<std::endl;
139 vtkInternals::ArraysType::iterator it = this->Internals->Arrays.find(arrayname);
141 if (it == this->Internals->Arrays.end())
143 vtkInternals::ArraysType::iterator it1 = this->Internals->Arrays.begin();
144 for (; it1 != this->Internals->Arrays.end(); it1++)
146 if (it1->second.NewName.compare(arrayname) == 0)
154 if (it == this->Internals->Arrays.end())
156 std::pair<vtkInternals::ArraysType::iterator, bool> ret;
157 ret = this->Internals->Arrays.insert(
158 std::pair<std::string, vtkInternals::ArrayInfo>(arrayname, vtkInternals::ArrayInfo()));
162 if (it != this->Internals->Arrays.end())
164 vtkInternals::ComponentsInfo::iterator vect_it = it->second.ComponentVector.begin();
165 for (; vect_it != it->second.ComponentVector.end(); vect_it++)
167 if (vect_it->first == compid)
171 if (vect_it != it->second.ComponentVector.end())
173 vect_it->second = newcompname;
177 it->second.ComponentVector.push_back(std::pair<int, std::string>(compid, newcompname));
183 void vtkArrayRenamerFilter::SetArrayInfo(
184 const char* originarrayname, const char* newarrayname, bool copy)
187 // std::cout<<"vtkArrayRenamerFilter::SetArrayInfo : "<<originarrayname<<" "<<"new name
188 // :"<<newarrayname<<" , copy = "<<copy<<std::endl;
190 vtkInternals::ArraysType::iterator it = this->Internals->Arrays.find(originarrayname);
192 if (it == this->Internals->Arrays.end())
193 this->Internals->Arrays.insert(
194 std::pair<std::string, vtkInternals::ArrayInfo>(originarrayname, vtkInternals::ArrayInfo()));
196 this->Internals->Arrays[originarrayname].NewName = newarrayname;
197 this->Internals->Arrays[originarrayname].CopyArray = copy;
201 void vtkArrayRenamerFilter::ClearArrayInfo()
203 // std::cout<<"vtkArrayRenamerFilter::ClearArrayInfo"<<std::endl;
204 this->Internals->Arrays.clear();
208 void vtkArrayRenamerFilter::ClearComponentsInfo()
210 // std::cout<<"vtkArrayRenamerFilter::ClearComponentsInfo"<<std::endl;
211 vtkInternals::ArraysType::iterator it = this->Internals->Arrays.begin();
212 for (; it != this->Internals->Arrays.end(); it++)
214 it->second.ComponentVector.clear();