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_VTKPrsShapeDriver.h"
30 #include "HYDROGUI_Tool.h"
32 #include "HYDROData_Tool.h"
33 #include <SVTK_ViewModel.h>
34 #include <SVTK_ViewWindow.h>
35 #include <SALOME_ListIO.hxx>
36 //#include <SALOME_ListIteratorOfListIO.hxx>
37 #include <SALOME_InteractiveObject.hxx>
38 #include <SUIT_ViewManager.h>
39 #include <SUIT_Accel.h>
41 #include <vtkLookupTable.h>
42 #include <vtkRenderer.h>
43 #include <vtkTextProperty.h>
44 #include <vtkWindow.h>
45 #include <vtkActor2DCollection.h>
52 //#define HUE_START 0.69
53 //#define HUE_END 0.41
54 //#define SATURATION_START 1.0
55 //#define SATURATION_END 0.4
59 #define SATURATION_START 1.0
60 #define SATURATION_END 1.0
62 HYDROGUI_VTKPrsDisplayer::HYDROGUI_VTKPrsDisplayer( HYDROGUI_Module* theModule )
63 : HYDROGUI_AbstractDisplayer( theModule ), myDriver( NULL ), myShapeDriver( NULL )
67 HYDROGUI_VTKPrsDisplayer::~HYDROGUI_VTKPrsDisplayer()
71 void HYDROGUI_VTKPrsDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
72 const int theViewerId )
74 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
77 HYDROGUI_VTKPrs* anObjShape;
78 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
80 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
83 anObjShape = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
86 anObjShape->setIsToUpdate( true );
93 void HYDROGUI_VTKPrsDisplayer::DisplayAll( const int theViewerId,
94 const bool theIsForced,
95 const bool theDoFitAll )
97 HYDROGUI_AbstractDisplayer::DisplayAll( theViewerId, theIsForced, theDoFitAll );
99 bool isEraseScalarBar = true;
101 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
104 SALOME_ListIO aListIO;
105 aViewer->GetVisible( aListIO );
107 HYDROGUI_VTKPrs* aPrs;
108 SALOME_ListIteratorOfListIO anIter( aListIO );
109 for( ; anIter.More(); anIter.Next() )
111 Handle(SALOME_InteractiveObject) aPrsObj = anIter.Value();
112 if ( !aPrsObj.IsNull() )
114 Handle(HYDROData_Entity) anObj =
115 module()->getDataModel()->objectByEntry( aPrsObj->getEntry() );
116 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
117 if( aPrs->needScalarBar() )
119 isEraseScalarBar = false;
126 if( isEraseScalarBar )
127 EraseScalarBar( theViewerId );
130 void HYDROGUI_VTKPrsDisplayer::EraseAll( const int theViewerId )
132 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
135 aViewer->EraseAll( 0, true );
136 module()->removeViewVTKPrs( (size_t)aViewer );
137 EraseScalarBar( theViewerId );
141 void HYDROGUI_VTKPrsDisplayer::EraseScalarBar( const int theViewerId, const bool theIsDelete )
143 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
146 if ( myScalarBars.contains( (size_t)aViewer ) )
148 SUIT_ViewManager* aViewMgr = dynamic_cast<SUIT_ViewManager*>( aViewer->getViewManager() );
149 if ( aViewMgr && aViewMgr->getViewsCount() > 0 )
151 SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>( aViewMgr->getActiveView() );
154 vtkScalarBarActor* aScalarBar = myScalarBars[ (size_t)aViewer ];
155 if ( aView->getRenderer()->HasViewProp( aScalarBar ) )
157 aView->getRenderer()->RemoveActor2D( aScalarBar );
163 myScalarBars.remove( (size_t)aViewer );
169 void HYDROGUI_VTKPrsDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
170 const int theViewerId )
172 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
175 HYDROGUI_VTKPrs* aPrs;
176 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
178 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
182 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
185 aViewer->Erase( aPrs, true );
187 if ( anObj->IsRemoved() )
189 module()->removeObjectVTKPrs( (size_t)aViewer, anObj );
195 void HYDROGUI_VTKPrsDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
196 const int theViewerId,
197 const bool theIsForced,
198 const bool theDoFitAll)
200 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
203 // Hide colors legend bar
204 SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(
205 aViewer->getViewManager()->getActiveView() );
206 vtkScalarBarActor* aScalarBar = 0;
209 if ( !myScalarBars.contains( (size_t)aViewer ) )
211 createScalarBar( (size_t)aViewer );
213 aScalarBar = myScalarBars[ (size_t)aViewer ];
215 if ( aView->getRenderer()->HasViewProp( aScalarBar ) )
217 aView->getRenderer()->RemoveActor2D( aScalarBar );
221 // Invalidate global Z range
222 double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
223 SetZRange( (size_t)aViewer, anInvalidRange );
225 int anInvalidZ = HYDROGUI_VTKPrs::InvalidZValue();
226 bool isChanged = false;
227 bool isScalarBarNeeded = false;
228 HYDROGUI_VTKPrs* aPrs;
229 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
231 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
232 if( !anObj.IsNull() )
234 bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
235 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
237 bool anIsInserted = ( aPrs != 0 );
238 if( anIsVisible && ( !aPrs || aPrs->getIsToUpdate() || theIsForced ) )
240 // Erase the presentation in the view because of the problem with GEOM_Actor upadting on second SetShape.
243 aViewer->Erase( aPrs, true );
245 // Update the presentation
246 if( HYDROGUI_VTKPrsDriver* aDriver = getDriver( (size_t)aViewer, anObj ) )
248 if( aDriver->Update( anObj, aPrs ) && aPrs && !anIsInserted )
250 module()->setObjectVTKPrs( (size_t)aViewer, anObj, aPrs );
259 if ( aPrs->needScalarBar() )
261 // Extend the global Z range if necessary
262 double* aGlobalRange = GetZRange( (size_t)aViewer );
263 double* aRange = aPrs->getInternalZRange();
264 bool anIsUpdate = false;
265 if ( aRange[0] < aGlobalRange[0] || ValuesEquals( aGlobalRange[0], anInvalidZ ) )
267 aGlobalRange[0] = aRange[0];
270 if ( aRange[1] > aGlobalRange[1] || ValuesEquals( aGlobalRange[1], anInvalidZ ) )
272 aGlobalRange[1] = aRange[1];
278 module()->updateVTKZRange( (size_t)aViewer, aGlobalRange );
282 aViewer->Display( aPrs );
283 isScalarBarNeeded = isScalarBarNeeded || aPrs->needScalarBar();
288 aViewer->Erase( aPrs );
297 if ( isChanged && isScalarBarNeeded && aScalarBar )
299 // Show colors legend bar
300 aView->getRenderer()->AddActor2D( aScalarBar );
306 // Repaint is done inside OnFitAll()
307 aView->onAccelAction( SUIT_Accel::ZoomFit );
309 else if ( isChanged )
311 aView->Repaint( true );
317 void HYDROGUI_VTKPrsDisplayer::purgeObjects( const int theViewerId )
319 bool doEraseScalarBar = false;
321 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
324 SALOME_ListIO aListIO;
325 aViewer->GetVisible( aListIO );
327 HYDROGUI_VTKPrs* aPrs;
328 SALOME_ListIteratorOfListIO anIter( aListIO );
329 for( ; anIter.More(); anIter.Next() )
331 Handle(SALOME_InteractiveObject) aPrsObj = anIter.Value();
332 if ( !aPrsObj.IsNull() )
334 Handle(HYDROData_Entity) anOwnerObj =
335 module()->getDataModel()->objectByEntry( aPrsObj->getEntry() );
336 if ( anOwnerObj.IsNull() )
338 // We found an unknown IO presented in the viewer.
339 // Remove such an orphan presentation from all the views of the viewer.
340 SUIT_ViewManager* aViewMgr = dynamic_cast<SUIT_ViewManager*>( aViewer->getViewManager() );
341 if ( aViewMgr && aViewMgr->getViewsCount() > 0 )
343 QVector<SUIT_ViewWindow*> aViews = aViewMgr->getViews();
344 foreach ( SUIT_ViewWindow* aView, aViews )
346 SVTK_ViewWindow* aVTKView = dynamic_cast<SVTK_ViewWindow*>( aView );
349 aVTKView->Erase( aPrsObj, false );
353 // Remove the object presentation from the module's cache
354 module()->removeObjectVTKPrs( (size_t)aViewer, aPrsObj->getEntry() );
355 doEraseScalarBar = true;
357 else if ( anOwnerObj->IsRemoved() )
359 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anOwnerObj );
362 aViewer->Erase( aPrs );
367 module()->removeObjectVTKPrs( (size_t)aViewer, anOwnerObj );
368 doEraseScalarBar = true;
373 if ( doEraseScalarBar )
375 EraseScalarBar( theViewerId );
380 HYDROGUI_VTKPrsDriver* HYDROGUI_VTKPrsDisplayer::getDriver( const int theViewId, const Handle(HYDROData_Entity)& theObj )
382 HYDROGUI_VTKPrsDriver* aDriver = NULL;
383 ObjectKind aKind = theObj->GetKind();
384 if( theObj->GetKind() == KIND_BATHYMETRY )
388 myDriver = new HYDROGUI_VTKPrsBathymetryDriver( myScalarBars[ theViewId ] );
394 if ( !myShapeDriver )
396 myShapeDriver = new HYDROGUI_VTKPrsShapeDriver( myScalarBars[ theViewId ] );
398 aDriver = myShapeDriver;
404 QString HYDROGUI_VTKPrsDisplayer::GetType() const
406 return SVTK_Viewer::Type();
409 void HYDROGUI_VTKPrsDisplayer::SetZRange( const int theViewId, double theRange[] )
411 myScalarBars[ theViewId ]->GetLookupTable()->SetRange( theRange );
414 double* HYDROGUI_VTKPrsDisplayer::GetZRange( const int theViewId ) const
416 return myScalarBars[ theViewId ]->GetLookupTable()->GetRange();
419 void HYDROGUI_VTKPrsDisplayer::createScalarBar( const int theViewId )
421 if ( !myScalarBars.contains( theViewId ) )
423 // The invalid value is used to identify the case when the table range is not initialized yet.
424 double anInvalidValue = HYDROGUI_VTKPrs::InvalidZValue();
425 vtkLookupTable* aTable = vtkLookupTable::New();
426 aTable->SetHueRange( HUE_START, HUE_END );
427 aTable->SetSaturationRange( SATURATION_START, SATURATION_END );
428 aTable->SetTableRange( anInvalidValue, anInvalidValue );
429 aTable->SetValueRange( 1.0, 1.0 );
430 aTable->SetAlphaRange( 1.0, 1.0 );
431 aTable->SetNumberOfColors( NB_COLORS );
433 vtkSmartPointer<vtkScalarBarActor> aScalarBar = vtkScalarBarActor::New();
434 aScalarBar->SetLookupTable( aTable );
435 aScalarBar->SetNumberOfLabels( NB_COLORS * 0.75 );
436 aScalarBar->SetWidth( aScalarBar->GetWidth() / 1.5 );
437 aScalarBar->SetTextureGridWidth( aScalarBar->GetTextureGridWidth() * 4. );
438 aScalarBar->SetTitle( " " );
439 // The call of SetTitle() with dummy string is a workaround necessary
440 // to avoid the problem with uninitialized variables in VTK scalar bar actor
441 // which leads to incorrect (very big) size of the VTK scalar bar presentation
443 myScalarBars.insert( theViewId, aScalarBar );