Salome HOME
Copyright update 2022
[modules/paravis.git] / src / Plugins / ScaleVector / plugin / ScaleVectorModule / vtkScaleVectorFilter.cxx
1 // Copyright (C) 2010-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
20 #include "vtkScaleVectorFilter.h"
21
22 #include <vtkCellArray.h>
23 #include <vtkDataSet.h>
24 #include <vtkInformation.h>
25 #include <vtkInformationVector.h>
26 #include <vtkObjectFactory.h>
27 #include <vtkPointData.h>
28
29 #include <string>
30
31 using namespace std;
32
33 // vtkCxxRevisionMacro(vtkScaleVectorFilter, "$Revision$");
34 vtkStandardNewMacro(vtkScaleVectorFilter)
35
36 vtkScaleVectorFilter::vtkScaleVectorFilter()
37 {
38   this->ScaleFactor = 0.5;
39 }
40
41 vtkScaleVectorFilter::~vtkScaleVectorFilter()
42 {
43 }
44
45 //----------------------------------------------------------------------------
46 int vtkScaleVectorFilter::FillInputPortInformation(int vtkNotUsed(port), vtkInformation* info)
47 {
48   // now add our info
49   info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
50   return 1;
51 }
52
53 int vtkScaleVectorFilter::RequestData(
54   vtkInformation* vtkNotUsed(request), vtkInformationVector** input, vtkInformationVector* output)
55 {
56   vtkDataSet* dsIn =
57     vtkDataSet::SafeDownCast(input[0]->GetInformationObject(0)->Get(vtkDataObject::DATA_OBJECT()));
58
59   vtkDataSet* dsOut =
60     vtkDataSet::SafeDownCast(output->GetInformationObject(0)->Get(vtkDataObject::DATA_OBJECT()));
61
62   vtkDataArray* array = this->GetInputArrayToProcess(0, input);
63
64   if (dsIn == NULL || array == NULL || dsOut == NULL || array->GetNumberOfComponents() != 3 ||
65     this->GetInputArrayAssociation(0, input) != vtkDataObject::FIELD_ASSOCIATION_POINTS)
66   {
67     vtkDebugMacro("vtkScaleVectorFilter no correctly configured");
68     return 1;
69   }
70
71   dsOut->ShallowCopy(dsIn);
72
73   vtkPointData* inPD = dsIn->GetPointData();
74   vtkPointData* outPD = dsOut->GetPointData();
75
76   outPD->RemoveArray(array->GetName());
77
78   vtkDataArray* outArray = array->NewInstance();
79   outArray->SetName(array->GetName());
80
81   string name = array->GetName();
82   size_t pos1 = name.find_last_of("[");
83   if (pos1 != string::npos)
84   {
85     string name1 = name.substr(0, pos1);
86     size_t noblankpos = name1.find_last_not_of(" ");
87     name1 = name1.substr(0, noblankpos + 1);
88
89     outArray->SetName(name1.c_str());
90
91     for (int arrayId = 0; arrayId < inPD->GetNumberOfArrays(); arrayId++)
92     {
93       vtkDataArray* thearray = inPD->GetArray(arrayId);
94       if (thearray == NULL)
95         continue;
96
97       if (strncmp(name1.c_str(), thearray->GetName(), name1.size()) == 0)
98       {
99         outPD->RemoveArray(thearray->GetName());
100       }
101     }
102   }
103
104   outPD->AddArray(outArray);
105   outArray->Delete();
106
107   outArray->SetNumberOfComponents(array->GetNumberOfComponents());
108   outArray->SetNumberOfTuples(array->GetNumberOfTuples());
109
110   if (array->HasAComponentName())
111   {
112     outArray->CopyComponentNames(array);
113   }
114
115   if (array->HasInformation())
116   {
117     outArray->CopyInformation(array->GetInformation());
118   }
119
120   for (vtkIdType id = 0; id < outArray->GetNumberOfTuples(); id++)
121   {
122     double tuple[3];
123     array->GetTuple(id, tuple);
124     tuple[0] *= this->ScaleFactor;
125     tuple[1] *= this->ScaleFactor;
126     tuple[2] *= this->ScaleFactor;
127     outArray->SetTuple(id, tuple);
128   }
129   // fix the range keys to match the full range, so that the
130   // gui automatically sets the range.
131   double range[2];
132   vtkInformation* info = outArray->GetInformation();
133
134   array->GetRange(range, -1);
135   range[0] = 0;
136   info->Set(vtkDataArray::L2_NORM_RANGE(), range, 2);
137
138   if (!outArray->GetInformation()->Has(vtkDataArray::PER_COMPONENT()))
139   {
140     vtkInformationVector* infoVec = vtkInformationVector::New();
141     info->Set(vtkDataArray::PER_COMPONENT(), infoVec);
142     infoVec->FastDelete();
143   }
144   vtkInformationVector* per_comp = info->Get(vtkDataArray::PER_COMPONENT());
145   per_comp->SetNumberOfInformationObjects(outArray->GetNumberOfComponents());
146   for (int comp = 0; comp < outArray->GetNumberOfComponents(); comp++)
147   {
148     array->GetRange(range, comp);
149     range[0] = fabs(range[0]);
150     range[1] = fabs(range[1]);
151     double r = (range[0] > range[1] ? range[0] : range[1]);
152     range[0] = -r;
153     range[1] = r;
154     per_comp->GetInformationObject(comp)->Set(vtkDataArray::COMPONENT_RANGE(), range, 2);
155   }
156
157   outPD->SetActiveVectors(outArray->GetName());
158
159   return 1;
160 }
161
162 void vtkScaleVectorFilter::PrintSelf(ostream& os, vtkIndent indent)
163 {
164   this->Superclass::PrintSelf(os, indent);
165
166   os << indent << "ScaleFactor : " << this->ScaleFactor << endl;
167 }