Salome HOME
Fix for Bug IPAL8945
[modules/visu.git] / src / PIPELINE / VISU_ScalarMapPL.cxx
1 //  VISU OBJECT : interactive object for VISU entities implementation
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 // File:    VISU_PipeLine.cxx
25 // Author:  Alexey PETROV
26 // Module : VISU
27
28
29 #include "VISU_ScalarMapPL.hxx"
30 #include "VISU_PipeLineUtils.hxx"
31 #include "SALOME_ExtractGeometry.h"
32
33
34 //============================================================================
35 vtkStandardNewMacro(VISU_ScalarMapPL);
36
37
38 //----------------------------------------------------------------------------
39 VISU_ScalarMapPL
40 ::VISU_ScalarMapPL()
41 {
42   myMapperTable = VISU_LookupTable::New();
43   myMapperTable->SetScale(VTK_SCALE_LINEAR);
44   myMapperTable->SetHueRange(0.667,0.0);
45
46   myBarTable = VISU_LookupTable::New();
47   myBarTable->SetScale(VTK_SCALE_LINEAR);
48   myBarTable->SetHueRange(0.667,0.0);
49
50   myExtractor = VISU_Extractor::New();
51   myExtractor->SetInput(myExtractGeometry->GetOutput());
52
53   myFieldTransform = VISU_FieldTransform::New();
54   myFieldTransform->SetInput(myExtractor->GetOutput());
55
56   myIsShrinkable = true;
57 }
58
59
60 VISU_ScalarMapPL
61 ::~VISU_ScalarMapPL()
62 {
63   myFieldTransform->Delete();
64   myMapperTable->Delete();
65   myBarTable->Delete();
66   myExtractor->Delete();
67 }
68
69
70 //----------------------------------------------------------------------------
71 void
72 VISU_ScalarMapPL
73 ::ShallowCopy(VISU_PipeLine *thePipeLine)
74 {
75   if(VISU_ScalarMapPL *aPipeLine = dynamic_cast<VISU_ScalarMapPL*>(thePipeLine)){
76     SetScalarRange(aPipeLine->GetScalarRange());
77     SetScalarMode(aPipeLine->GetScalarMode());
78     SetNbColors(aPipeLine->GetNbColors());
79     SetScaling(aPipeLine->GetScaling());
80     SetMapScale(aPipeLine->GetMapScale());
81   }
82   Superclass::ShallowCopy(thePipeLine);
83 }
84
85
86 //----------------------------------------------------------------------------
87 int
88 VISU_ScalarMapPL
89 ::GetScalarMode()
90 {
91   return myExtractor->GetScalarMode();
92 }
93
94 void
95 VISU_ScalarMapPL
96 ::SetScalarMode(int theScalarMode)
97 {
98   vtkDataSet *input = GetInput();
99   if (input){
100     if(input->GetPointData()->GetNumberOfArrays()){
101       vtkPointData *inData = input->GetPointData();
102       if(!inData->GetAttribute(vtkDataSetAttributes::VECTORS)) {
103         if (theScalarMode==0){
104           return;
105         }
106       }
107     }
108     else {
109       vtkCellData *inData = input->GetCellData();
110       if(!inData->GetAttribute(vtkDataSetAttributes::VECTORS)){
111         if (theScalarMode==0){
112           return;
113         }
114       }
115     }
116   }
117   //
118   myExtractor->SetScalarMode(theScalarMode);
119   Modified();
120 }
121
122
123 //----------------------------------------------------------------------------
124 int
125 VISU_ScalarMapPL
126 ::GetScaling() 
127 {
128   return myBarTable->GetScale();
129 }
130
131 void
132 VISU_ScalarMapPL
133 ::SetScaling(int theScaling) 
134 {
135   myBarTable->SetScale(theScaling);
136   if(theScaling == VTK_SCALE_LOG10)
137     myFieldTransform->SetScalarTransform(&(VISU_FieldTransform::Log10));
138   else
139     myFieldTransform->SetScalarTransform(&(VISU_FieldTransform::Ident));
140   Modified();
141 }
142
143
144 //----------------------------------------------------------------------------
145 float* 
146 VISU_ScalarMapPL
147 ::GetScalarRange() 
148 {
149   return myFieldTransform->GetScalarRange();
150 }
151
152 void
153 VISU_ScalarMapPL
154 ::SetScalarRange(float theRange[2])
155 {
156   myFieldTransform->SetScalarRange(theRange);
157   myBarTable->SetRange(theRange);
158   Modified();
159 }
160
161 void
162 VISU_ScalarMapPL
163 ::SetScalarMin(float theValue)
164 {
165   float aScalarRange[2] = {theValue, GetScalarRange()[1]};
166   SetScalarRange(aScalarRange);
167 }
168
169 void
170 VISU_ScalarMapPL
171 ::SetScalarMax(float theValue)
172 {
173   float aScalarRange[2] = {GetScalarRange()[0], theValue};
174   SetScalarRange(aScalarRange);
175 }
176
177
178 //----------------------------------------------------------------------------
179 void
180 VISU_ScalarMapPL
181 ::SetNbColors(int theNbColors) 
182 {
183   myMapperTable->SetNumberOfColors(theNbColors);
184   myBarTable->SetNumberOfColors(theNbColors);
185   Modified();
186 }
187
188 int
189 VISU_ScalarMapPL
190 ::GetNbColors() 
191 {
192   return myMapperTable->GetNumberOfColors();
193 }
194
195
196 //----------------------------------------------------------------------------
197 VISU_ScalarMapPL::THook* 
198 VISU_ScalarMapPL
199 ::DoHook()
200 {
201   myMapper->SetColorModeToMapScalars();
202   myMapper->ScalarVisibilityOn();
203   return myFieldTransform->GetUnstructuredGridOutput();
204 }
205
206
207 void
208 VISU_ScalarMapPL
209 ::Init()
210 {
211   SetScalarMode(0);
212   SetSourceRange();
213 }
214
215 void
216 VISU_ScalarMapPL
217 ::Build() 
218 {
219   myMapper->SetInput(DoHook());
220 }
221
222
223 void
224 VISU_ScalarMapPL
225 ::Update() 
226
227   float *aRange = myFieldTransform->GetScalarRange();
228   float aScalarRange[2] = {aRange[0], aRange[1]};
229   if(myBarTable->GetScale() == VTK_SCALE_LOG10)
230     VISU_LookupTable::ComputeLogRange(aRange,aScalarRange);
231   myMapperTable->SetRange(aScalarRange);
232
233   myMapperTable->Build();
234   myBarTable->Build();
235
236   myMapper->SetLookupTable(myMapperTable);
237   myMapper->SetScalarRange(aScalarRange);
238
239   VISU_PipeLine::Update();
240 }
241
242
243 //----------------------------------------------------------------------------
244 VISU_LookupTable *
245 VISU_ScalarMapPL
246 ::GetMapperTable()
247
248   return myMapperTable;
249 }
250
251 VISU_LookupTable*
252 VISU_ScalarMapPL
253 ::GetBarTable()
254 {
255   return myBarTable;
256 }
257
258
259 //----------------------------------------------------------------------------
260 void 
261 VISU_ScalarMapPL
262 ::SetMapScale(float theMapScale)
263 {
264   myMapperTable->SetMapScale(theMapScale);
265   myMapperTable->Build();
266 }
267
268 float
269 VISU_ScalarMapPL::GetMapScale()
270 {
271   return myMapperTable->GetMapScale();
272 }
273
274
275 //----------------------------------------------------------------------------
276 void
277 VISU_ScalarMapPL
278 ::GetSourceRange(float theRange[2])
279 {
280   myExtractor->Update();
281   myExtractor->GetOutput()->GetScalarRange(theRange);
282 }
283
284 void
285 VISU_ScalarMapPL
286 ::SetSourceRange()
287 {
288   float aRange[2];
289   GetSourceRange(aRange);
290   SetScalarRange(aRange);
291 }