1 // VISU OBJECT : interactive object for VISU entities implementation
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File: VISU_ScalarBarCtrl.cxx
24 // Author: Peter KURNEV
27 #include "VISU_ScalarBarCtrl.hxx"
29 #include <vtkObjectFactory.h>
30 #include <vtkActor2D.h>
31 #include <vtkCoordinate.h>
32 #include <vtkRenderer.h>
33 #include <vtkScalarsToColors.h>
34 #include <vtkLookupTable.h>
35 #include <vtkTextProperty.h>
36 #include <vtkScalarBarActor.h>
37 #include <vtkIdType.h>
42 //----------------------------------------------------------------------------
47 MarkValueByColor(vtkLookupTable* theTable,
48 vtkFloatingPointType theValue,
49 unsigned char* theColor)
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];
60 CopyColor(unsigned char* theTaget, const unsigned char* theSource)
62 theTaget[0] = theSource[0];
63 theTaget[1] = theSource[1];
64 theTaget[2] = theSource[2];
68 FillByColor(vtkLookupTable* theTable,
69 unsigned char* theColor)
71 vtkIdType aNbColors = theTable->GetNumberOfColors();
72 for(int i = 0; i < aNbColors; i++){
73 unsigned char *aTablePtr = theTable->GetPointer(i);
74 CopyColor(aTablePtr,theColor);
79 MakeBiColor(vtkLookupTable* theTable)
81 unsigned char aRedPtr[3] = {255, 0, 0};
82 unsigned char aBluePtr[3] = {0, 0, 255};
84 vtkFloatingPointType aRange[2];
85 theTable->GetTableRange(aRange);
86 vtkIdType aNbColors = theTable->GetNumberOfColors();
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);
94 CopyColor(aTablePtr,aRedPtr);
96 CopyColor(aTablePtr,aBluePtr);
104 //----------------------------------------------------------------------------
105 vtkStandardNewMacro(VISU_ScalarBarCtrl);
107 //----------------------------------------------------------------------------
109 ::VISU_ScalarBarCtrl():
110 myGlobalRangeIsDefined(false)
121 // Initilize global scalar bar
122 myGlobalLookupTable = vtkLookupTable::New();
123 myGlobalLookupTable->SetHueRange(0.667,0.0);
125 myGlobalScalarBar = vtkScalarBarActor::New();
126 myGlobalScalarBar->SetLookupTable(myGlobalLookupTable);
127 myGlobalLookupTable->Delete();
129 // Initilize local scalar bar
130 myLocalLookupTable = vtkLookupTable::New();
131 myLocalLookupTable->SetHueRange(0.667,0.0);
133 myLocalScalarBar = vtkScalarBarActor::New();
134 myLocalScalarBar->SetLookupTable(myLocalLookupTable);
135 myLocalLookupTable->Delete();
137 myBlack[0] = myBlack[1] = myBlack[2] = 0;
138 myGrey[0] = myGrey[1] = myGrey[2] = 192;
140 myCtrlVisibility = 1;
145 //----------------------------------------------------------------------------
147 ::~VISU_ScalarBarCtrl()
149 myGlobalScalarBar->Delete();
153 //----------------------------------------------------------------------------
156 ::SetMode(VISU_ScalarBarCtrl::EMode theMode)
161 VISU_ScalarBarCtrl::EMode
169 //----------------------------------------------------------------------------
172 ::SetVisibility(int theVisibility)
174 myGlobalScalarBar->SetVisibility(false);
175 myLocalScalarBar->SetVisibility(false);
176 int aVisibility = (myCtrlVisibility && theVisibility);
178 if(myMode != eSimple)
179 myGlobalScalarBar->SetVisibility(aVisibility);
180 myLocalScalarBar->SetVisibility(aVisibility);
186 ::GetVisibility() const
188 return myGlobalScalarBar->GetVisibility() || myLocalScalarBar->GetVisibility();
193 ::SetCtrlVisibility(int theVisibility)
195 myCtrlVisibility = theVisibility;
200 ::GetCtrlVisibility() const
202 return myCtrlVisibility;
206 //----------------------------------------------------------------------------
209 ::SetRangeLocal(vtkFloatingPointType *theRange)
211 myLocalLookupTable->SetTableRange(theRange);
216 ::SetRangeLocal(vtkFloatingPointType theMin,
217 vtkFloatingPointType theMax)
219 myLocalLookupTable->SetTableRange(theMin,theMax);
223 //----------------------------------------------------------------------------
226 ::SetRangeGlobal(vtkFloatingPointType *theRange)
228 myGlobalLookupTable->SetTableRange(theRange);
233 ::SetRangeGlobal(vtkFloatingPointType theMin,
234 vtkFloatingPointType theMax)
236 myGlobalLookupTable->SetTableRange(theMin,theMax);
241 ::SetGlobalRangeIsDefined(bool theIsDefined)
243 myGlobalRangeIsDefined = theIsDefined;
247 //----------------------------------------------------------------------------
252 return myLocalScalarBar;
259 return myGlobalScalarBar;
263 //----------------------------------------------------------------------------
268 return myLocalLookupTable;
275 return myGlobalLookupTable;
279 //----------------------------------------------------------------------------
282 ::AddToRender(vtkRenderer* theRenderer)
284 theRenderer->AddActor2D(myGlobalScalarBar);
285 theRenderer->AddActor2D(myLocalScalarBar);
291 ::RemoveFromRender(vtkRenderer* theRenderer)
293 theRenderer->RemoveActor2D(myGlobalScalarBar);
294 theRenderer->RemoveActor2D(myLocalScalarBar);
298 //----------------------------------------------------------------------------
301 ::SetWidth(vtkFloatingPointType theWidth)
303 myGlobalScalarBar->SetWidth(theWidth);
304 myLocalScalarBar->SetWidth(theWidth);
311 return myGlobalScalarBar->GetWidth();
315 //----------------------------------------------------------------------------
318 ::SetHeight(vtkFloatingPointType theHeight)
320 myGlobalScalarBar->SetHeight(theHeight);
321 myLocalScalarBar->SetHeight(theHeight);
329 return myGlobalScalarBar->GetHeight();
333 //----------------------------------------------------------------------------
336 ::SetPosition(const vtkFloatingPointType* thePosition)
338 myPosition[0] = thePosition[0];
339 myPosition[1] = thePosition[1];
342 const vtkFloatingPointType*
343 VISU_ScalarBarCtrl::GetPosition() const
349 //----------------------------------------------------------------------------
352 ::SetSpacing(const vtkFloatingPointType theSpacing)
354 myDistance = theSpacing;
365 //----------------------------------------------------------------------------
368 ::SetBicolor(const bool theBicolor)
370 myBicolor = theBicolor;
381 //----------------------------------------------------------------------------
384 ::SetMarkValue(const vtkFloatingPointType theValue)
386 myMarkedValue = theValue;
391 ::GetMarkValue() const
393 return myMarkedValue;
397 //----------------------------------------------------------------------------
400 ::SetIsMarked(const bool theFlag)
407 ::GetIsMarked() const
413 //----------------------------------------------------------------------------
418 SetVisibility(GetVisibility());
431 //----------------------------------------------------------------------------
437 if(myMode == eGlobal){
438 MarkValueByColor(myGlobalLookupTable, myMarkedValue, myBlack);
440 MarkValueByColor(myLocalLookupTable, myMarkedValue, myBlack);
443 if(myGlobalRangeIsDefined){
444 vtkFloatingPointType aLocalRange[2];
445 myLocalLookupTable->GetTableRange(aLocalRange);
446 MarkValueByColor(myGlobalLookupTable, aLocalRange[0], myBlack);
447 MarkValueByColor(myGlobalLookupTable, aLocalRange[1], myBlack);
452 //----------------------------------------------------------------------------
455 ::PrepareTables(vtkScalarBarActor* theScalarBarActor,
456 vtkLookupTable *theLookupTable,
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]);
465 vtkFloatingPointType aHeight = theScalarBarActor->GetHeight();
466 aCoordinate->SetValue(myPosition[0], myPosition[1]+theId*(aHeight+myDistance));
468 // Initialize Lookup Tables and Scalar Bars
469 theLookupTable->Modified();
470 theLookupTable->Build();
478 if(myMode != eSimple){
479 PrepareTables(myGlobalScalarBar,myGlobalLookupTable,0);
480 PrepareTables(myLocalScalarBar,myLocalLookupTable,1);
482 PrepareTables(myLocalScalarBar,myLocalLookupTable,0);
487 //----------------------------------------------------------------------------
492 if(myMode == eGlobal){
493 FillByColor(myLocalLookupTable,myGrey);
494 }else if(myMode == eLocal){
495 FillByColor(myGlobalLookupTable,myGrey);
500 //----------------------------------------------------------------------------
505 myLocalLookupTable->Modified();
506 myLocalLookupTable->Build();
508 if(myMode == eSimple){
509 MakeBiColor(myLocalLookupTable);
513 if(myMode == eGlobal){
514 MakeBiColor(myGlobalLookupTable);
515 FillByColor(myLocalLookupTable,myGrey);
516 }else if(myMode == eLocal){
517 MakeBiColor(myLocalLookupTable);
518 FillByColor(myGlobalLookupTable,myGrey);