Salome HOME
aa1da46645afdbbf1f3d8fffbffc11751b40fed1
[modules/visu.git] / src / PIPELINE / VISU_ScalarBarCtrl.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  VISU OBJECT : interactive object for VISU entities implementation
24 // File:    VISU_ScalarBarCtrl.cxx
25 // Author:  Peter KURNEV
26 // Module : VISU
27 //
28 #include "VISU_ScalarBarCtrl.hxx"
29 #include "VISU_LookupTable.hxx"
30 #include "VISU_ScalarBarActor.hxx"
31
32 #include <vtkObjectFactory.h>
33 #include <vtkActor2D.h> 
34 #include <vtkCoordinate.h>
35 #include <vtkRenderer.h>
36 #include <vtkScalarsToColors.h>
37 #include <vtkTextProperty.h>
38 #include <vtkType.h>
39
40 #include <string.h>
41
42
43 //----------------------------------------------------------------------------
44 vtkStandardNewMacro(VISU_ScalarBarCtrl);
45
46 //----------------------------------------------------------------------------
47 VISU_ScalarBarCtrl
48 ::VISU_ScalarBarCtrl():
49   myGlobalRangeIsDefined(false)
50 {
51   myDistance=0.02;
52   myPosition[0]=0.15;
53   myPosition[1]=0.01;
54   myBicolor=false;
55   myMarked=false;
56   myMarkedValue=99.;
57   //
58   SetMode(eSimple);
59
60   // Initilize global scalar bar
61   myGlobalLookupTable = VISU_LookupTable::New();
62   myGlobalLookupTable->SetHueRange(0.667,0.0);
63
64   myGlobalScalarBar = VISU_ScalarBarActor::New();
65   myGlobalScalarBar->SetLookupTable(myGlobalLookupTable);
66   myGlobalLookupTable->Delete();
67
68   // Initilize local scalar bar
69   myLocalLookupTable = VISU_LookupTable::New();
70   myLocalLookupTable->SetHueRange(0.667,0.0);
71
72   myLocalScalarBar = VISU_ScalarBarActor::New();
73   myLocalScalarBar->SetLookupTable(myLocalLookupTable);
74   myLocalLookupTable->Delete();
75
76   myBlack[0] = myBlack[1] = myBlack[2] = 0;
77   myGrey[0] = myGrey[1] = myGrey[2] = 192;
78   //
79   myCtrlVisibility = 1;
80   SetVisibility(1);
81 }
82
83
84 //----------------------------------------------------------------------------
85 VISU_ScalarBarCtrl
86 ::~VISU_ScalarBarCtrl()
87 {
88   myGlobalScalarBar->Delete();
89 }
90
91
92 //----------------------------------------------------------------------------
93 void
94 VISU_ScalarBarCtrl
95 ::SetMode(VISU_ScalarBarCtrl::EMode theMode)
96 {
97   myMode = theMode;
98 }
99
100 VISU_ScalarBarCtrl::EMode
101 VISU_ScalarBarCtrl
102 ::GetMode() const
103 {
104   return myMode;
105 }
106
107
108 //----------------------------------------------------------------------------
109 void
110 VISU_ScalarBarCtrl
111 ::SetVisibility(int theVisibility)
112 {
113   myGlobalScalarBar->SetVisibility(false);
114   myLocalScalarBar->SetVisibility(false);
115   int aVisibility = (myCtrlVisibility && theVisibility);
116   if(aVisibility){
117     if(myMode != eSimple)
118       myGlobalScalarBar->SetVisibility(aVisibility);
119     myLocalScalarBar->SetVisibility(aVisibility);
120   }
121 }
122
123 int
124 VISU_ScalarBarCtrl
125 ::GetVisibility() const
126 {
127   return myGlobalScalarBar->GetVisibility() || myLocalScalarBar->GetVisibility();
128 }
129
130 void
131 VISU_ScalarBarCtrl
132 ::SetCtrlVisibility(int theVisibility)
133 {
134   myCtrlVisibility = theVisibility;
135 }
136
137 int
138 VISU_ScalarBarCtrl
139 ::GetCtrlVisibility() const
140 {
141   return myCtrlVisibility;
142 }
143
144
145 //----------------------------------------------------------------------------
146 void
147 VISU_ScalarBarCtrl
148 ::SetRangeLocal(vtkFloatingPointType *theRange)
149 {
150   myLocalLookupTable->SetTableRange(theRange);
151 }
152
153 void
154 VISU_ScalarBarCtrl
155 ::SetRangeLocal(vtkFloatingPointType theMin,
156                 vtkFloatingPointType theMax)
157 {
158   myLocalLookupTable->SetTableRange(theMin,theMax);
159 }
160
161
162 //----------------------------------------------------------------------------
163 void
164 VISU_ScalarBarCtrl
165 ::SetRangeGlobal(vtkFloatingPointType *theRange)
166 {
167   myGlobalLookupTable->SetTableRange(theRange);
168 }
169
170 void
171 VISU_ScalarBarCtrl
172 ::SetRangeGlobal(vtkFloatingPointType theMin,
173                  vtkFloatingPointType theMax)
174 {
175   myGlobalLookupTable->SetTableRange(theMin,theMax);
176 }
177
178 void
179 VISU_ScalarBarCtrl
180 ::SetGlobalRangeIsDefined(bool theIsDefined)
181 {
182   myGlobalRangeIsDefined = theIsDefined;
183 }
184
185
186 //----------------------------------------------------------------------------
187 VISU_ScalarBarActor* 
188 VISU_ScalarBarCtrl
189 ::GetLocalBar() 
190 {
191   return myLocalScalarBar;
192 }
193
194 VISU_ScalarBarActor* 
195 VISU_ScalarBarCtrl
196 ::GetGlobalBar() 
197 {
198   return myGlobalScalarBar;
199 }
200
201
202 //----------------------------------------------------------------------------
203 VISU_LookupTable* 
204 VISU_ScalarBarCtrl
205 ::GetLocalTable() 
206 {
207   return myLocalLookupTable;
208 }
209
210 VISU_LookupTable* 
211 VISU_ScalarBarCtrl
212 ::GetGlobalTable() 
213 {
214   return myGlobalLookupTable;
215 }
216
217
218 //----------------------------------------------------------------------------
219 void
220 VISU_ScalarBarCtrl
221 ::AddToRender(vtkRenderer* theRenderer)
222 {
223   theRenderer->AddActor2D(myGlobalScalarBar);
224   theRenderer->AddActor2D(myLocalScalarBar);
225   Update();
226 }
227
228 void
229 VISU_ScalarBarCtrl
230 ::RemoveFromRender(vtkRenderer* theRenderer)
231 {
232   theRenderer->RemoveActor2D(myGlobalScalarBar);
233   theRenderer->RemoveActor2D(myLocalScalarBar);
234 }
235
236
237 //----------------------------------------------------------------------------
238 void
239 VISU_ScalarBarCtrl
240 ::SetWidth(vtkFloatingPointType theWidth)
241 {
242   myGlobalScalarBar->SetWidth(theWidth);
243   myLocalScalarBar->SetWidth(theWidth);
244 }
245
246 vtkFloatingPointType
247 VISU_ScalarBarCtrl
248 ::GetWidth() const
249 {
250   return myGlobalScalarBar->GetWidth();
251 }
252
253
254 //----------------------------------------------------------------------------
255 void
256 VISU_ScalarBarCtrl
257 ::SetHeight(vtkFloatingPointType theHeight)
258 {
259   myGlobalScalarBar->SetHeight(theHeight);
260   myLocalScalarBar->SetHeight(theHeight);
261 }
262
263
264 vtkFloatingPointType
265 VISU_ScalarBarCtrl
266 ::GetHeight() const
267 {
268   return myGlobalScalarBar->GetHeight();
269 }
270
271
272 //----------------------------------------------------------------------------
273 void
274 VISU_ScalarBarCtrl
275 ::SetPosition(const vtkFloatingPointType* thePosition)
276 {
277   myPosition[0] = thePosition[0];
278   myPosition[1] = thePosition[1];
279 }
280
281 const vtkFloatingPointType* 
282 VISU_ScalarBarCtrl::GetPosition() const
283 {
284   return myPosition;
285 }
286
287
288 //----------------------------------------------------------------------------
289 void
290 VISU_ScalarBarCtrl
291 ::SetSpacing(const vtkFloatingPointType theSpacing)
292 {
293   myDistance = theSpacing;
294 }
295
296 vtkFloatingPointType
297 VISU_ScalarBarCtrl
298 ::GetSpacing() const
299 {
300   return myDistance;
301 }
302
303
304 //----------------------------------------------------------------------------
305 void
306 VISU_ScalarBarCtrl
307 ::SetBicolor(const bool theBicolor)
308 {
309   myBicolor = theBicolor;
310 }
311
312 bool
313 VISU_ScalarBarCtrl
314 ::GetBicolor() const
315 {
316   return myBicolor;
317 }
318
319
320 //----------------------------------------------------------------------------
321 void
322 VISU_ScalarBarCtrl
323 ::SetMarkValue(const vtkFloatingPointType theValue) 
324 {
325   myMarkedValue = theValue;
326 }
327
328 vtkFloatingPointType
329 VISU_ScalarBarCtrl
330 ::GetMarkValue() const
331 {
332   return myMarkedValue;
333 }
334
335
336 //----------------------------------------------------------------------------
337 void
338 VISU_ScalarBarCtrl
339 ::SetIsMarked(const bool theFlag) 
340 {
341   myMarked = theFlag;
342 }
343
344 bool
345 VISU_ScalarBarCtrl
346 ::GetIsMarked() const
347 {
348   return myMarked;
349 }
350
351
352 //----------------------------------------------------------------------------
353 void
354 VISU_ScalarBarCtrl
355 ::Update()
356 {
357   SetVisibility(GetVisibility());
358   //
359   PrepareTables();
360   //
361   if(myBicolor)
362     UpdateForBicolor();
363   else
364     UpdateForColor();
365   //
366   UpdateMarkValue();
367 }
368
369
370 //----------------------------------------------------------------------------
371 void
372 VISU_ScalarBarCtrl
373 ::UpdateMarkValue()
374 {
375   if(myMarked){
376     if(myMode == eGlobal){
377       myGlobalLookupTable->MarkValueByColor( myMarkedValue, myBlack );
378     }else{
379       myLocalLookupTable->MarkValueByColor( myMarkedValue, myBlack );
380     }
381   }
382   if(myGlobalRangeIsDefined){
383     vtkFloatingPointType aLocalRange[2];
384     myLocalLookupTable->GetTableRange(aLocalRange);
385     myGlobalLookupTable->MarkValueByColor( aLocalRange[0], myBlack );
386     myGlobalLookupTable->MarkValueByColor( aLocalRange[1], myBlack );
387   }
388 }
389
390
391 //----------------------------------------------------------------------------
392 void
393 VISU_ScalarBarCtrl
394 ::PrepareTables(VISU_ScalarBarActor* theScalarBarActor,
395                 VISU_LookupTable *theLookupTable,
396                 vtkIdType theId)
397 {
398   vtkCoordinate * aCoordinate = theScalarBarActor->GetPositionCoordinate();
399   aCoordinate->SetCoordinateSystemToNormalizedViewport();
400   if(theScalarBarActor->GetOrientation() == VTK_ORIENT_VERTICAL){
401     vtkFloatingPointType aWidth = theScalarBarActor->GetWidth();
402     aCoordinate->SetValue(myPosition[0]+theId*(aWidth+myDistance), myPosition[1]);
403   }else{ 
404     vtkFloatingPointType aHeight = theScalarBarActor->GetHeight();
405     aCoordinate->SetValue(myPosition[0], myPosition[1]+theId*(aHeight+myDistance));
406   }
407   // Initialize Lookup Tables and Scalar Bars 
408   theLookupTable->Modified();
409   theLookupTable->Build();
410 }
411
412
413 void
414 VISU_ScalarBarCtrl
415 ::PrepareTables()
416 {
417   if(myMode != eSimple){
418     PrepareTables(myGlobalScalarBar,myGlobalLookupTable,0);
419     PrepareTables(myLocalScalarBar,myLocalLookupTable,1);
420   }else{
421     PrepareTables(myLocalScalarBar,myLocalLookupTable,0);
422   }
423 }
424
425
426 //----------------------------------------------------------------------------
427 void
428 VISU_ScalarBarCtrl
429 ::UpdateForColor()
430 {
431   if(myMode == eGlobal){ 
432     myLocalLookupTable->FillByColor( myGrey );
433   }else if(myMode == eLocal){
434     myGlobalLookupTable->FillByColor( myGrey );
435   }
436 }
437
438
439 //----------------------------------------------------------------------------
440 void
441 VISU_ScalarBarCtrl
442 ::UpdateForBicolor()
443 {
444   myLocalLookupTable->Modified();
445   myLocalLookupTable->Build();
446
447   if(myMode == eSimple){
448     myLocalLookupTable->MakeBiColor();
449     return;
450   }
451
452   if(myMode == eGlobal){
453     myGlobalLookupTable->MakeBiColor();
454     myLocalLookupTable->FillByColor( myGrey );
455   }else if(myMode == eLocal){
456     myLocalLookupTable->MakeBiColor();
457     myGlobalLookupTable->FillByColor( myGrey );
458   }
459 }