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::EraseAll( const int theViewerId )
95 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
98 aViewer->EraseAll( true );
99 module()->removeViewVTKPrs( (size_t)aViewer );
100 EraseScalarBar( theViewerId );
104 void HYDROGUI_VTKPrsDisplayer::EraseScalarBar( const int theViewerId, const bool theIsDelete )
106 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
109 if ( myScalarBars.contains( (size_t)aViewer ) )
111 SUIT_ViewManager* aViewMgr = dynamic_cast<SUIT_ViewManager*>( aViewer->getViewManager() );
112 if ( aViewMgr && aViewMgr->getViewsCount() > 0 )
114 SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>( aViewMgr->getActiveView() );
117 vtkScalarBarActor* aScalarBar = myScalarBars[ (size_t)aViewer ];
118 if ( aView->getRenderer()->HasViewProp( aScalarBar ) )
120 aView->getRenderer()->RemoveActor2D( aScalarBar );
126 myScalarBars.remove( (size_t)aViewer );
132 void HYDROGUI_VTKPrsDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
133 const int theViewerId )
135 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
138 HYDROGUI_VTKPrs* aPrs;
139 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
141 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
145 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
148 aViewer->Erase( aPrs, true );
150 if ( anObj->IsRemoved() )
152 module()->removeObjectVTKPrs( (size_t)aViewer, anObj );
158 void HYDROGUI_VTKPrsDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
159 const int theViewerId,
160 const bool theIsForced,
161 const bool theDoFitAll)
163 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
166 // Hide colors legend bar
167 SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(
168 aViewer->getViewManager()->getActiveView() );
169 vtkScalarBarActor* aScalarBar = 0;
172 if ( !myScalarBars.contains( (size_t)aViewer ) )
174 createScalarBar( (size_t)aViewer );
176 aScalarBar = myScalarBars[ (size_t)aViewer ];
178 if ( aView->getRenderer()->HasViewProp( aScalarBar ) )
180 aView->getRenderer()->RemoveActor2D( aScalarBar );
184 // Invalidate global Z range
185 double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
186 SetZRange( (size_t)aViewer, anInvalidRange );
188 int anInvalidZ = HYDROGUI_VTKPrs::InvalidZValue();
189 bool isChanged = false;
190 bool isScalarBarNeeded = false;
191 HYDROGUI_VTKPrs* aPrs;
192 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
194 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
195 if( !anObj.IsNull() )
197 bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
198 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
200 bool anIsInserted = ( aPrs != 0 );
201 if( anIsVisible && ( !aPrs || aPrs->getIsToUpdate() || theIsForced ) )
203 if( HYDROGUI_VTKPrsDriver* aDriver = getDriver( (size_t)aViewer, anObj ) )
205 if( aDriver->Update( anObj, aPrs ) && aPrs && !anIsInserted )
207 module()->setObjectVTKPrs( (size_t)aViewer, anObj, aPrs );
216 if ( aPrs->needScalarBar() )
218 // Extend the global Z range if necessary
219 double* aGlobalRange = GetZRange( (size_t)aViewer );
220 double* aRange = aPrs->getInternalZRange();
221 bool anIsUpdate = false;
222 if ( aRange[0] < aGlobalRange[0] || ValuesEquals( aGlobalRange[0], anInvalidZ ) )
224 aGlobalRange[0] = aRange[0];
227 if ( aRange[1] > aGlobalRange[1] || ValuesEquals( aGlobalRange[1], anInvalidZ ) )
229 aGlobalRange[1] = aRange[1];
235 module()->updateVTKZRange( (size_t)aViewer, aGlobalRange );
239 aViewer->Display( aPrs );
240 isScalarBarNeeded = isScalarBarNeeded || aPrs->needScalarBar();
245 aViewer->Erase( aPrs );
254 if ( isChanged && isScalarBarNeeded && aScalarBar )
256 // Show colors legend bar
257 aView->getRenderer()->AddActor2D( aScalarBar );
263 // Repaint is done inside OnFitAll()
264 aView->onAccelAction( SUIT_Accel::ZoomFit );
266 else if ( isChanged )
274 void HYDROGUI_VTKPrsDisplayer::purgeObjects( const int theViewerId )
276 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
279 SALOME_ListIO aListIO;
280 aViewer->GetVisible( aListIO );
282 HYDROGUI_VTKPrs* aPrs;
283 SALOME_ListIteratorOfListIO anIter( aListIO );
284 for( ; anIter.More(); anIter.Next() )
286 Handle(SALOME_InteractiveObject) aPrsObj = anIter.Value();
287 if ( !aPrsObj.IsNull() )
289 Handle(HYDROData_Entity) anOwnerObj =
290 module()->getDataModel()->objectByEntry( aPrsObj->getEntry() );
291 if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
293 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anOwnerObj );
296 aViewer->Erase( aPrs );
298 module()->removeObjectVTKPrs( (size_t)aViewer, anOwnerObj );
305 HYDROGUI_VTKPrsDriver* HYDROGUI_VTKPrsDisplayer::getDriver( const int theViewId, const Handle(HYDROData_Entity)& theObj )
307 HYDROGUI_VTKPrsDriver* aDriver = NULL;
308 ObjectKind aKind = theObj->GetKind();
309 if( theObj->GetKind() == KIND_BATHYMETRY )
313 myDriver = new HYDROGUI_VTKPrsBathymetryDriver( myScalarBars[ theViewId ] );
319 if ( !myShapeDriver )
321 myShapeDriver = new HYDROGUI_VTKPrsShapeDriver( myScalarBars[ theViewId ] );
323 aDriver = myShapeDriver;
329 QString HYDROGUI_VTKPrsDisplayer::GetType() const
331 return SVTK_Viewer::Type();
334 void HYDROGUI_VTKPrsDisplayer::SetZRange( const int theViewId, double theRange[] )
336 myScalarBars[ theViewId ]->GetLookupTable()->SetRange( theRange );
339 double* HYDROGUI_VTKPrsDisplayer::GetZRange( const int theViewId ) const
341 return myScalarBars[ theViewId ]->GetLookupTable()->GetRange();
344 void HYDROGUI_VTKPrsDisplayer::createScalarBar( const int theViewId )
346 if ( !myScalarBars.contains( theViewId ) )
348 // The invalid value is used to identify the case when the table range is not initialized yet.
349 double anInvalidValue = HYDROGUI_VTKPrs::InvalidZValue();
350 vtkLookupTable* aTable = vtkLookupTable::New();
351 aTable->SetHueRange( HUE_START, HUE_END );
352 aTable->SetSaturationRange( SATURATION_START, SATURATION_END );
353 aTable->SetTableRange( anInvalidValue, anInvalidValue );
354 aTable->SetValueRange( 1.0, 1.0 );
355 aTable->SetAlphaRange( 1.0, 1.0 );
356 aTable->SetNumberOfColors( NB_COLORS );
358 vtkSmartPointer<vtkScalarBarActor> aScalarBar = vtkScalarBarActor::New();
359 aScalarBar->SetLookupTable( aTable );
360 aScalarBar->SetNumberOfLabels( NB_COLORS * 0.75 );
361 aScalarBar->SetWidth( aScalarBar->GetWidth() / 1.5 );
362 aScalarBar->SetTextureGridWidth( aScalarBar->GetTextureGridWidth() * 4. );
364 myScalarBars.insert( theViewId, aScalarBar );