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