1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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 #include "HYDROGUI_VTKPrsDisplayer.h"
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_VTKPrs.h"
28 #include "HYDROGUI_VTKPrsBathymetryDriver.h"
29 #include "HYDROGUI_Tool.h"
31 #include "HYDROData_Tool.h"
32 #include <SVTK_ViewModel.h>
33 #include <SVTK_ViewWindow.h>
34 #include <SALOME_ListIO.hxx>
35 #include <SALOME_ListIteratorOfListIO.hxx>
36 #include <SALOME_InteractiveObject.hxx>
37 #include <SUIT_ViewManager.h>
38 #include <SUIT_Accel.h>
40 #include <vtkLookupTable.h>
41 #include <vtkRenderer.h>
42 #include <vtkWindow.h>
43 #include <vtkActor2DCollection.h>
50 //#define HUE_START 0.69
51 //#define HUE_END 0.41
52 //#define SATURATION_START 1.0
53 //#define SATURATION_END 0.4
57 #define SATURATION_START 1.0
58 #define SATURATION_END 1.0
60 HYDROGUI_VTKPrsDisplayer::HYDROGUI_VTKPrsDisplayer( HYDROGUI_Module* theModule )
61 : HYDROGUI_AbstractDisplayer( theModule ), myDriver( NULL )
63 // The invalid value is used to identify the case when the table range is not initialized yet.
64 double anInvalidValue = HYDROGUI_VTKPrs::InvalidZValue();
65 vtkLookupTable* aTable = vtkLookupTable::New();
66 aTable->SetHueRange( HUE_START, HUE_END );
67 aTable->SetSaturationRange( SATURATION_START, SATURATION_END );
68 aTable->SetTableRange( anInvalidValue, anInvalidValue );
69 aTable->SetValueRange( 1.0, 1.0 );
70 aTable->SetAlphaRange( 1.0, 1.0 );
71 aTable->SetNumberOfColors( NB_COLORS );
72 myScalarBar->SetLookupTable( aTable );
75 HYDROGUI_VTKPrsDisplayer::~HYDROGUI_VTKPrsDisplayer()
79 void HYDROGUI_VTKPrsDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
80 const int theViewerId )
82 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
85 HYDROGUI_VTKPrs* anObjShape;
86 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
88 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
91 anObjShape = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
94 anObjShape->setIsToUpdate( true );
101 void HYDROGUI_VTKPrsDisplayer::EraseAll( const int theViewerId )
103 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
106 aViewer->EraseAll( true );
107 module()->removeViewVTKPrs( (size_t)aViewer );
111 void HYDROGUI_VTKPrsDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
112 const int theViewerId )
114 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
117 HYDROGUI_VTKPrs* aPrs;
118 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
120 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
124 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
127 aViewer->Erase( aPrs, true );
129 if ( anObj->IsRemoved() )
131 module()->removeObjectVTKPrs( (size_t)aViewer, anObj );
137 void HYDROGUI_VTKPrsDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
138 const int theViewerId,
139 const bool theIsForced,
140 const bool theDoFitAll)
142 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
145 // Invalidate global Z range
146 double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
147 SetZRange( anInvalidRange );
148 // Hide colors legend bar
149 SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(
150 aViewer->getViewManager()->getActiveView() );
153 if ( aView->getRenderer()->HasViewProp( myScalarBar.GetPointer() ) )
155 aView->getRenderer()->RemoveActor2D( myScalarBar.GetPointer() );
160 int anInvalidZ = HYDROGUI_VTKPrs::InvalidZValue();
161 bool isChanged = false;
162 HYDROGUI_VTKPrs* aPrs;
163 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
165 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
166 if( !anObj.IsNull() )
168 bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
169 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
171 bool anIsInserted = ( aPrs != 0 );
172 if( anIsVisible && ( !aPrs || aPrs->getIsToUpdate() || theIsForced ) )
174 if( HYDROGUI_VTKPrsDriver* aDriver = getDriver( anObj ) )
176 if( aDriver->Update( anObj, aPrs ) && aPrs && !anIsInserted )
178 module()->setObjectVTKPrs( (size_t)aViewer, anObj, aPrs );
188 // Extend the global Z range if necessary
189 double* aGlobalRange = GetZRange();
190 double* aRange = aPrs->getInternalZRange();
191 bool anIsUpdate = false;
192 if ( aRange[0] < aGlobalRange[0] || ValuesEquals( aGlobalRange[0], anInvalidZ ) )
194 aGlobalRange[0] = aRange[0];
197 if ( aRange[1] > aGlobalRange[1] || ValuesEquals( aGlobalRange[1], anInvalidZ ) )
199 aGlobalRange[1] = aRange[1];
205 module()->updateVTKZRange( aGlobalRange );
208 aViewer->Display( aPrs );
213 aViewer->Erase( aPrs );
222 // Show colors legend bar
225 aView->getRenderer()->AddActor2D( myScalarBar.GetPointer() );
231 // Repaint is done inside OnFitAll()
232 aViewer->getViewManager()->getActiveView()->onAccelAction( SUIT_Accel::ZoomFit );
234 else if ( isChanged )
241 void HYDROGUI_VTKPrsDisplayer::purgeObjects( const int theViewerId )
243 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
246 SALOME_ListIO aListIO;
247 aViewer->GetVisible( aListIO );
249 HYDROGUI_VTKPrs* aPrs;
250 SALOME_ListIteratorOfListIO anIter( aListIO );
251 for( ; anIter.More(); anIter.Next() )
253 Handle(SALOME_InteractiveObject) aPrsObj = anIter.Value();
254 if ( !aPrsObj.IsNull() )
256 Handle(HYDROData_Entity) anOwnerObj =
257 module()->getDataModel()->objectByEntry( aPrsObj->getEntry() );
258 if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
260 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anOwnerObj );
263 aViewer->Erase( aPrs );
265 module()->removeObjectVTKPrs( (size_t)aViewer, anOwnerObj );
272 HYDROGUI_VTKPrsDriver* HYDROGUI_VTKPrsDisplayer::getDriver( const Handle(HYDROData_Entity)& theObj )
274 HYDROGUI_VTKPrsDriver* aDriver = NULL;
275 ObjectKind aKind = theObj->GetKind();
276 if( theObj->GetKind() == KIND_BATHYMETRY )
280 myDriver = new HYDROGUI_VTKPrsBathymetryDriver( myScalarBar.GetPointer() );
288 QString HYDROGUI_VTKPrsDisplayer::GetType() const
290 return SVTK_Viewer::Type();
293 void HYDROGUI_VTKPrsDisplayer::SetZRange( double theRange[] )
295 myScalarBar->GetLookupTable()->SetRange( theRange );
298 double* HYDROGUI_VTKPrsDisplayer::GetZRange() const
300 return myScalarBar->GetLookupTable()->GetRange();