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