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 )
65 HYDROGUI_VTKPrsDisplayer::~HYDROGUI_VTKPrsDisplayer()
69 void HYDROGUI_VTKPrsDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
70 const int theViewerId )
72 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
75 HYDROGUI_VTKPrs* anObjShape;
76 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
78 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
81 anObjShape = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
84 anObjShape->setIsToUpdate( true );
91 void HYDROGUI_VTKPrsDisplayer::EraseAll( const int theViewerId )
93 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
96 aViewer->EraseAll( true );
97 module()->removeViewVTKPrs( (size_t)aViewer );
101 void HYDROGUI_VTKPrsDisplayer::DeleteScalarBar( const int theViewerId )
103 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
106 if ( myScalarBars.contains( (size_t)aViewer ) )
108 SUIT_ViewManager* aViewMgr = dynamic_cast<SUIT_ViewManager*>( aViewer->getViewManager() );
109 if ( aViewMgr && aViewMgr->getViewsCount() > 0 )
111 SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>( aViewMgr->getActiveView() );
114 vtkScalarBarActor* aScalarBar = myScalarBars[ (size_t)aViewer ];
115 if ( aView->getRenderer()->HasViewProp( aScalarBar ) )
117 aView->getRenderer()->RemoveActor2D( aScalarBar );
121 myScalarBars.remove( (size_t)aViewer );
126 void HYDROGUI_VTKPrsDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
127 const int theViewerId )
129 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
132 HYDROGUI_VTKPrs* aPrs;
133 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
135 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
139 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
142 aViewer->Erase( aPrs, true );
144 if ( anObj->IsRemoved() )
146 module()->removeObjectVTKPrs( (size_t)aViewer, anObj );
152 void HYDROGUI_VTKPrsDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
153 const int theViewerId,
154 const bool theIsForced,
155 const bool theDoFitAll)
157 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
160 // Hide colors legend bar
161 SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(
162 aViewer->getViewManager()->getActiveView() );
163 vtkScalarBarActor* aScalarBar = 0;
166 if ( !myScalarBars.contains( (size_t)aViewer ) )
168 createScalarBar( (size_t)aViewer );
170 aScalarBar = myScalarBars[ (size_t)aViewer ];
172 if ( aView->getRenderer()->HasViewProp( aScalarBar ) )
174 aView->getRenderer()->RemoveActor2D( aScalarBar );
178 // Invalidate global Z range
179 double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
180 SetZRange( (size_t)aViewer, anInvalidRange );
182 int anInvalidZ = HYDROGUI_VTKPrs::InvalidZValue();
183 bool isChanged = false;
184 HYDROGUI_VTKPrs* aPrs;
185 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
187 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
188 if( !anObj.IsNull() )
190 bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
191 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
193 bool anIsInserted = ( aPrs != 0 );
194 if( anIsVisible && ( !aPrs || aPrs->getIsToUpdate() || theIsForced ) )
196 if( HYDROGUI_VTKPrsDriver* aDriver = getDriver( (size_t)aViewer, anObj ) )
198 if( aDriver->Update( anObj, aPrs ) && aPrs && !anIsInserted )
200 module()->setObjectVTKPrs( (size_t)aViewer, anObj, aPrs );
210 // Extend the global Z range if necessary
211 double* aGlobalRange = GetZRange( (size_t)aViewer );
212 double* aRange = aPrs->getInternalZRange();
213 bool anIsUpdate = false;
214 if ( aRange[0] < aGlobalRange[0] || ValuesEquals( aGlobalRange[0], anInvalidZ ) )
216 aGlobalRange[0] = aRange[0];
219 if ( aRange[1] > aGlobalRange[1] || ValuesEquals( aGlobalRange[1], anInvalidZ ) )
221 aGlobalRange[1] = aRange[1];
227 module()->updateVTKZRange( (size_t)aViewer, aGlobalRange );
230 aViewer->Display( aPrs );
235 aViewer->Erase( aPrs );
244 if ( isChanged && aScalarBar )
246 // Show colors legend bar
247 aView->getRenderer()->AddActor2D( aScalarBar );
253 // Repaint is done inside OnFitAll()
254 aView->onAccelAction( SUIT_Accel::ZoomFit );
256 else if ( isChanged )
264 void HYDROGUI_VTKPrsDisplayer::purgeObjects( const int theViewerId )
266 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
269 SALOME_ListIO aListIO;
270 aViewer->GetVisible( aListIO );
272 HYDROGUI_VTKPrs* aPrs;
273 SALOME_ListIteratorOfListIO anIter( aListIO );
274 for( ; anIter.More(); anIter.Next() )
276 Handle(SALOME_InteractiveObject) aPrsObj = anIter.Value();
277 if ( !aPrsObj.IsNull() )
279 Handle(HYDROData_Entity) anOwnerObj =
280 module()->getDataModel()->objectByEntry( aPrsObj->getEntry() );
281 if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
283 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anOwnerObj );
286 aViewer->Erase( aPrs );
288 module()->removeObjectVTKPrs( (size_t)aViewer, anOwnerObj );
295 HYDROGUI_VTKPrsDriver* HYDROGUI_VTKPrsDisplayer::getDriver( const int theViewId, const Handle(HYDROData_Entity)& theObj )
297 HYDROGUI_VTKPrsDriver* aDriver = NULL;
298 ObjectKind aKind = theObj->GetKind();
299 if( theObj->GetKind() == KIND_BATHYMETRY )
303 myDriver = new HYDROGUI_VTKPrsBathymetryDriver( myScalarBars[ theViewId ] );
311 QString HYDROGUI_VTKPrsDisplayer::GetType() const
313 return SVTK_Viewer::Type();
316 void HYDROGUI_VTKPrsDisplayer::SetZRange( const int theViewId, double theRange[] )
318 myScalarBars[ theViewId ]->GetLookupTable()->SetRange( theRange );
321 double* HYDROGUI_VTKPrsDisplayer::GetZRange( const int theViewId ) const
323 return myScalarBars[ theViewId ]->GetLookupTable()->GetRange();
326 void HYDROGUI_VTKPrsDisplayer::createScalarBar( const int theViewId )
328 if ( !myScalarBars.contains( theViewId ) )
330 // The invalid value is used to identify the case when the table range is not initialized yet.
331 double anInvalidValue = HYDROGUI_VTKPrs::InvalidZValue();
332 vtkLookupTable* aTable = vtkLookupTable::New();
333 aTable->SetHueRange( HUE_START, HUE_END );
334 aTable->SetSaturationRange( SATURATION_START, SATURATION_END );
335 aTable->SetTableRange( anInvalidValue, anInvalidValue );
336 aTable->SetValueRange( 1.0, 1.0 );
337 aTable->SetAlphaRange( 1.0, 1.0 );
338 aTable->SetNumberOfColors( NB_COLORS );
340 vtkSmartPointer<vtkScalarBarActor> aScalarBar = vtkScalarBarActor::New();
341 aScalarBar->SetLookupTable( aTable );
343 myScalarBars.insert( theViewId, aScalarBar );