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