1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_VTKPrsDisplayer.h"
21 #include "HYDROGUI_DataModel.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_VTKPrs.h"
24 #include "HYDROGUI_VTKPrsBathymetryDriver.h"
25 #include "HYDROGUI_VTKPrsShapeDriver.h"
26 #include "HYDROGUI_Tool.h"
28 #include "HYDROData_Tool.h"
29 #include <SVTK_ViewModel.h>
30 #include <SVTK_ViewWindow.h>
31 #include <SALOME_ListIO.hxx>
32 //#include <SALOME_ListIteratorOfListIO.hxx>
33 #include <SALOME_InteractiveObject.hxx>
34 #include <SUIT_ViewManager.h>
35 #include <SUIT_Accel.h>
37 #include <vtkLookupTable.h>
38 #include <vtkRenderer.h>
39 #include <vtkTextProperty.h>
40 #include <vtkWindow.h>
41 #include <vtkActor2DCollection.h>
48 //#define HUE_START 0.69
49 //#define HUE_END 0.41
50 //#define SATURATION_START 1.0
51 //#define SATURATION_END 0.4
55 #define SATURATION_START 1.0
56 #define SATURATION_END 1.0
58 HYDROGUI_VTKPrsDisplayer::HYDROGUI_VTKPrsDisplayer( HYDROGUI_Module* theModule )
59 : HYDROGUI_AbstractDisplayer( theModule ), myDriver( NULL ), myShapeDriver( NULL )
63 HYDROGUI_VTKPrsDisplayer::~HYDROGUI_VTKPrsDisplayer()
67 void HYDROGUI_VTKPrsDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
68 const int theViewerId )
70 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
73 HYDROGUI_VTKPrs* anObjShape;
74 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
76 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
79 anObjShape = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
82 anObjShape->setIsToUpdate( true );
89 void HYDROGUI_VTKPrsDisplayer::DisplayAll( const int theViewerId,
90 const bool theIsForced,
91 const bool theDoFitAll )
93 HYDROGUI_AbstractDisplayer::DisplayAll( theViewerId, theIsForced, theDoFitAll );
95 bool isEraseScalarBar = true;
97 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
100 SALOME_ListIO aListIO;
101 aViewer->GetVisible( aListIO );
103 HYDROGUI_VTKPrs* aPrs;
104 SALOME_ListIteratorOfListIO anIter( aListIO );
105 for( ; anIter.More(); anIter.Next() )
107 Handle(SALOME_InteractiveObject) aPrsObj = anIter.Value();
108 if ( !aPrsObj.IsNull() )
110 Handle(HYDROData_Entity) anObj =
111 module()->getDataModel()->objectByEntry( aPrsObj->getEntry() );
112 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
113 if( aPrs->needScalarBar() )
115 isEraseScalarBar = false;
122 if( isEraseScalarBar )
123 EraseScalarBar( theViewerId );
126 void HYDROGUI_VTKPrsDisplayer::EraseAll( const int theViewerId )
128 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
131 aViewer->EraseAll( 0, true );
132 module()->removeViewVTKPrs( (size_t)aViewer );
133 EraseScalarBar( theViewerId );
137 void HYDROGUI_VTKPrsDisplayer::EraseScalarBar( const int theViewerId, const bool theIsDelete )
139 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
142 if ( myScalarBars.contains( (size_t)aViewer ) )
144 SUIT_ViewManager* aViewMgr = dynamic_cast<SUIT_ViewManager*>( aViewer->getViewManager() );
145 if ( aViewMgr && aViewMgr->getViewsCount() > 0 )
147 SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>( aViewMgr->getActiveView() );
150 vtkScalarBarActor* aScalarBar = myScalarBars[ (size_t)aViewer ];
151 if ( aView->getRenderer()->HasViewProp( aScalarBar ) )
153 aView->getRenderer()->RemoveActor2D( aScalarBar );
159 myScalarBars.remove( (size_t)aViewer );
165 void HYDROGUI_VTKPrsDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
166 const int theViewerId )
168 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
171 HYDROGUI_VTKPrs* aPrs;
172 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
174 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
178 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
181 aViewer->Erase( aPrs, true );
183 if ( anObj->IsRemoved() )
185 module()->removeObjectVTKPrs( (size_t)aViewer, anObj );
191 void HYDROGUI_VTKPrsDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
192 const int theViewerId,
193 const bool theIsForced,
194 const bool theDoFitAll)
196 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
199 // Hide colors legend bar
200 SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(
201 aViewer->getViewManager()->getActiveView() );
202 vtkScalarBarActor* aScalarBar = 0;
205 if ( !myScalarBars.contains( (size_t)aViewer ) )
207 createScalarBar( (size_t)aViewer );
209 aScalarBar = myScalarBars[ (size_t)aViewer ];
211 if ( aView->getRenderer()->HasViewProp( aScalarBar ) )
213 aView->getRenderer()->RemoveActor2D( aScalarBar );
217 // Invalidate global Z range
218 double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
219 SetZRange( (size_t)aViewer, anInvalidRange );
221 int anInvalidZ = HYDROGUI_VTKPrs::InvalidZValue();
222 bool isChanged = false;
223 bool isScalarBarNeeded = false;
224 HYDROGUI_VTKPrs* aPrs;
225 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
227 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
228 if( !anObj.IsNull() )
230 bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
231 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
233 bool anIsInserted = ( aPrs != 0 );
234 if( anIsVisible && ( !aPrs || aPrs->getIsToUpdate() || theIsForced ) )
236 // Erase the presentation in the view because of the problem with GEOM_Actor upadting on second SetShape.
239 aViewer->Erase( aPrs, true );
241 // Update the presentation
242 if( HYDROGUI_VTKPrsDriver* aDriver = getDriver( (size_t)aViewer, anObj ) )
244 if( aDriver->Update( anObj, aPrs ) && aPrs && !anIsInserted )
246 module()->setObjectVTKPrs( (size_t)aViewer, anObj, aPrs );
255 if ( aPrs->needScalarBar() )
257 // Extend the global Z range if necessary
258 double* aGlobalRange = GetZRange( (size_t)aViewer );
259 double* aRange = aPrs->getInternalZRange();
260 bool anIsUpdate = false;
261 if ( aRange[0] < aGlobalRange[0] || ValuesEquals( aGlobalRange[0], anInvalidZ ) )
263 aGlobalRange[0] = aRange[0];
266 if ( aRange[1] > aGlobalRange[1] || ValuesEquals( aGlobalRange[1], anInvalidZ ) )
268 aGlobalRange[1] = aRange[1];
274 module()->updateVTKZRange( (size_t)aViewer, aGlobalRange );
278 aViewer->Display( aPrs );
279 isScalarBarNeeded = isScalarBarNeeded || aPrs->needScalarBar();
284 aViewer->Erase( aPrs );
293 if ( isChanged && isScalarBarNeeded && aScalarBar )
295 // Show colors legend bar
296 aView->getRenderer()->AddActor2D( aScalarBar );
302 // Repaint is done inside OnFitAll()
303 aView->onAccelAction( SUIT_Accel::ZoomFit );
305 else if ( isChanged )
307 aView->Repaint( true );
313 void HYDROGUI_VTKPrsDisplayer::purgeObjects( const int theViewerId )
315 bool doEraseScalarBar = false;
317 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
320 SALOME_ListIO aListIO;
321 aViewer->GetVisible( aListIO );
323 HYDROGUI_VTKPrs* aPrs;
324 SALOME_ListIteratorOfListIO anIter( aListIO );
325 for( ; anIter.More(); anIter.Next() )
327 Handle(SALOME_InteractiveObject) aPrsObj = anIter.Value();
328 if ( !aPrsObj.IsNull() )
330 Handle(HYDROData_Entity) anOwnerObj =
331 module()->getDataModel()->objectByEntry( aPrsObj->getEntry() );
332 if ( anOwnerObj.IsNull() )
334 // We found an unknown IO presented in the viewer.
335 // Remove such an orphan presentation from all the views of the viewer.
336 SUIT_ViewManager* aViewMgr = dynamic_cast<SUIT_ViewManager*>( aViewer->getViewManager() );
337 if ( aViewMgr && aViewMgr->getViewsCount() > 0 )
339 QVector<SUIT_ViewWindow*> aViews = aViewMgr->getViews();
340 foreach ( SUIT_ViewWindow* aView, aViews )
342 SVTK_ViewWindow* aVTKView = dynamic_cast<SVTK_ViewWindow*>( aView );
345 aVTKView->Erase( aPrsObj, false );
349 // Remove the object presentation from the module's cache
350 module()->removeObjectVTKPrs( (size_t)aViewer, aPrsObj->getEntry() );
351 doEraseScalarBar = true;
353 else if ( anOwnerObj->IsRemoved() )
355 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anOwnerObj );
358 aViewer->Erase( aPrs );
363 module()->removeObjectVTKPrs( (size_t)aViewer, anOwnerObj );
364 doEraseScalarBar = true;
369 if ( doEraseScalarBar )
371 EraseScalarBar( theViewerId );
376 HYDROGUI_VTKPrsDriver* HYDROGUI_VTKPrsDisplayer::getDriver( const int theViewId, const Handle(HYDROData_Entity)& theObj )
378 HYDROGUI_VTKPrsDriver* aDriver = NULL;
379 ObjectKind aKind = theObj->GetKind();
380 if( theObj->GetKind() == KIND_BATHYMETRY )
384 myDriver = new HYDROGUI_VTKPrsBathymetryDriver( myScalarBars[ theViewId ] );
390 if ( !myShapeDriver )
392 myShapeDriver = new HYDROGUI_VTKPrsShapeDriver( myScalarBars[ theViewId ] );
394 aDriver = myShapeDriver;
400 QString HYDROGUI_VTKPrsDisplayer::GetType() const
402 return SVTK_Viewer::Type();
405 void HYDROGUI_VTKPrsDisplayer::SetZRange( const int theViewId, double theRange[] )
407 myScalarBars[ theViewId ]->GetLookupTable()->SetRange( theRange );
410 double* HYDROGUI_VTKPrsDisplayer::GetZRange( const int theViewId ) const
412 return myScalarBars[ theViewId ]->GetLookupTable()->GetRange();
415 void HYDROGUI_VTKPrsDisplayer::createScalarBar( const int theViewId )
417 if ( !myScalarBars.contains( theViewId ) )
419 // The invalid value is used to identify the case when the table range is not initialized yet.
420 double anInvalidValue = HYDROGUI_VTKPrs::InvalidZValue();
421 vtkLookupTable* aTable = vtkLookupTable::New();
422 aTable->SetHueRange( HUE_START, HUE_END );
423 aTable->SetSaturationRange( SATURATION_START, SATURATION_END );
424 aTable->SetTableRange( anInvalidValue, anInvalidValue );
425 aTable->SetValueRange( 1.0, 1.0 );
426 aTable->SetAlphaRange( 1.0, 1.0 );
427 aTable->SetNumberOfColors( NB_COLORS );
429 vtkSmartPointer<vtkScalarBarActor> aScalarBar = vtkScalarBarActor::New();
430 aScalarBar->SetLookupTable( aTable );
431 aScalarBar->SetNumberOfLabels( NB_COLORS * 0.75 );
432 aScalarBar->SetWidth( aScalarBar->GetWidth() / 1.5 );
433 aScalarBar->SetTextureGridWidth( aScalarBar->GetTextureGridWidth() * 4. );
434 aScalarBar->SetTitle( " " );
435 // The call of SetTitle() with dummy string is a workaround necessary
436 // to avoid the problem with uninitialized variables in VTK scalar bar actor
437 // which leads to incorrect (very big) size of the VTK scalar bar presentation
439 myScalarBars.insert( theViewId, aScalarBar );