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 <vtkTextProperty.h>
43 #include <vtkWindow.h>
44 #include <vtkActor2DCollection.h>
51 //#define HUE_START 0.69
52 //#define HUE_END 0.41
53 //#define SATURATION_START 1.0
54 //#define SATURATION_END 0.4
58 #define SATURATION_START 1.0
59 #define SATURATION_END 1.0
61 HYDROGUI_VTKPrsDisplayer::HYDROGUI_VTKPrsDisplayer( HYDROGUI_Module* theModule )
62 : HYDROGUI_AbstractDisplayer( theModule ), myDriver( NULL )
66 HYDROGUI_VTKPrsDisplayer::~HYDROGUI_VTKPrsDisplayer()
70 void HYDROGUI_VTKPrsDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
71 const int theViewerId )
73 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
76 HYDROGUI_VTKPrs* anObjShape;
77 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
79 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
82 anObjShape = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
85 anObjShape->setIsToUpdate( true );
92 void HYDROGUI_VTKPrsDisplayer::EraseAll( const int theViewerId )
94 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
97 aViewer->EraseAll( true );
98 module()->removeViewVTKPrs( (size_t)aViewer );
102 void HYDROGUI_VTKPrsDisplayer::DeleteScalarBar( const int theViewerId )
104 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
107 if ( myScalarBars.contains( (size_t)aViewer ) )
109 SUIT_ViewManager* aViewMgr = dynamic_cast<SUIT_ViewManager*>( aViewer->getViewManager() );
110 if ( aViewMgr && aViewMgr->getViewsCount() > 0 )
112 SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>( aViewMgr->getActiveView() );
115 vtkScalarBarActor* aScalarBar = myScalarBars[ (size_t)aViewer ];
116 if ( aView->getRenderer()->HasViewProp( aScalarBar ) )
118 aView->getRenderer()->RemoveActor2D( aScalarBar );
122 myScalarBars.remove( (size_t)aViewer );
127 void HYDROGUI_VTKPrsDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
128 const int theViewerId )
130 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
133 HYDROGUI_VTKPrs* aPrs;
134 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
136 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
140 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
143 aViewer->Erase( aPrs, true );
145 if ( anObj->IsRemoved() )
147 module()->removeObjectVTKPrs( (size_t)aViewer, anObj );
153 void HYDROGUI_VTKPrsDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
154 const int theViewerId,
155 const bool theIsForced,
156 const bool theDoFitAll)
158 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
161 // Hide colors legend bar
162 SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(
163 aViewer->getViewManager()->getActiveView() );
164 vtkScalarBarActor* aScalarBar = 0;
167 if ( !myScalarBars.contains( (size_t)aViewer ) )
169 createScalarBar( (size_t)aViewer );
171 aScalarBar = myScalarBars[ (size_t)aViewer ];
173 if ( aView->getRenderer()->HasViewProp( aScalarBar ) )
175 aView->getRenderer()->RemoveActor2D( aScalarBar );
179 // Invalidate global Z range
180 double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
181 SetZRange( (size_t)aViewer, anInvalidRange );
183 int anInvalidZ = HYDROGUI_VTKPrs::InvalidZValue();
184 bool isChanged = false;
185 HYDROGUI_VTKPrs* aPrs;
186 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
188 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
189 if( !anObj.IsNull() )
191 bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
192 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
194 bool anIsInserted = ( aPrs != 0 );
195 if( anIsVisible && ( !aPrs || aPrs->getIsToUpdate() || theIsForced ) )
197 if( HYDROGUI_VTKPrsDriver* aDriver = getDriver( (size_t)aViewer, anObj ) )
199 if( aDriver->Update( anObj, aPrs ) && aPrs && !anIsInserted )
201 module()->setObjectVTKPrs( (size_t)aViewer, anObj, aPrs );
211 // Extend the global Z range if necessary
212 double* aGlobalRange = GetZRange( (size_t)aViewer );
213 double* aRange = aPrs->getInternalZRange();
214 bool anIsUpdate = false;
215 if ( aRange[0] < aGlobalRange[0] || ValuesEquals( aGlobalRange[0], anInvalidZ ) )
217 aGlobalRange[0] = aRange[0];
220 if ( aRange[1] > aGlobalRange[1] || ValuesEquals( aGlobalRange[1], anInvalidZ ) )
222 aGlobalRange[1] = aRange[1];
228 module()->updateVTKZRange( (size_t)aViewer, aGlobalRange );
231 aViewer->Display( aPrs );
236 aViewer->Erase( aPrs );
245 if ( isChanged && aScalarBar )
247 // Show colors legend bar
248 aView->getRenderer()->AddActor2D( aScalarBar );
254 // Repaint is done inside OnFitAll()
255 aView->onAccelAction( SUIT_Accel::ZoomFit );
257 else if ( isChanged )
265 void HYDROGUI_VTKPrsDisplayer::purgeObjects( const int theViewerId )
267 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
270 SALOME_ListIO aListIO;
271 aViewer->GetVisible( aListIO );
273 HYDROGUI_VTKPrs* aPrs;
274 SALOME_ListIteratorOfListIO anIter( aListIO );
275 for( ; anIter.More(); anIter.Next() )
277 Handle(SALOME_InteractiveObject) aPrsObj = anIter.Value();
278 if ( !aPrsObj.IsNull() )
280 Handle(HYDROData_Entity) anOwnerObj =
281 module()->getDataModel()->objectByEntry( aPrsObj->getEntry() );
282 if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
284 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anOwnerObj );
287 aViewer->Erase( aPrs );
289 module()->removeObjectVTKPrs( (size_t)aViewer, anOwnerObj );
296 HYDROGUI_VTKPrsDriver* HYDROGUI_VTKPrsDisplayer::getDriver( const int theViewId, const Handle(HYDROData_Entity)& theObj )
298 HYDROGUI_VTKPrsDriver* aDriver = NULL;
299 ObjectKind aKind = theObj->GetKind();
300 if( theObj->GetKind() == KIND_BATHYMETRY )
304 myDriver = new HYDROGUI_VTKPrsBathymetryDriver( myScalarBars[ theViewId ] );
312 QString HYDROGUI_VTKPrsDisplayer::GetType() const
314 return SVTK_Viewer::Type();
317 void HYDROGUI_VTKPrsDisplayer::SetZRange( const int theViewId, double theRange[] )
319 myScalarBars[ theViewId ]->GetLookupTable()->SetRange( theRange );
322 double* HYDROGUI_VTKPrsDisplayer::GetZRange( const int theViewId ) const
324 return myScalarBars[ theViewId ]->GetLookupTable()->GetRange();
327 void HYDROGUI_VTKPrsDisplayer::createScalarBar( const int theViewId )
329 if ( !myScalarBars.contains( theViewId ) )
331 // The invalid value is used to identify the case when the table range is not initialized yet.
332 double anInvalidValue = HYDROGUI_VTKPrs::InvalidZValue();
333 vtkLookupTable* aTable = vtkLookupTable::New();
334 aTable->SetHueRange( HUE_START, HUE_END );
335 aTable->SetSaturationRange( SATURATION_START, SATURATION_END );
336 aTable->SetTableRange( anInvalidValue, anInvalidValue );
337 aTable->SetValueRange( 1.0, 1.0 );
338 aTable->SetAlphaRange( 1.0, 1.0 );
339 aTable->SetNumberOfColors( NB_COLORS );
341 vtkSmartPointer<vtkScalarBarActor> aScalarBar = vtkScalarBarActor::New();
342 aScalarBar->SetLookupTable( aTable );
343 aScalarBar->SetNumberOfLabels( NB_COLORS * 0.75 );
344 aScalarBar->SetWidth( aScalarBar->GetWidth() / 1.5 );
345 aScalarBar->SetTextureGridWidth( aScalarBar->GetTextureGridWidth() * 4. );
347 myScalarBars.insert( theViewId, aScalarBar );