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 SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(
107 aViewer->getViewManager()->getActiveView() );
110 if ( myScalarBars.contains( (size_t)aViewer ) )
112 vtkScalarBarActor* aScalarBar = myScalarBars[ (size_t)aViewer ];
113 if ( aView->getRenderer()->HasViewProp( aScalarBar ) )
115 aView->getRenderer()->RemoveActor2D( aScalarBar );
122 void HYDROGUI_VTKPrsDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
123 const int theViewerId )
125 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
128 HYDROGUI_VTKPrs* aPrs;
129 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
131 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
135 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
138 aViewer->Erase( aPrs, true );
140 if ( anObj->IsRemoved() )
142 module()->removeObjectVTKPrs( (size_t)aViewer, anObj );
148 void HYDROGUI_VTKPrsDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
149 const int theViewerId,
150 const bool theIsForced,
151 const bool theDoFitAll)
153 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
156 // Hide colors legend bar
157 SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(
158 aViewer->getViewManager()->getActiveView() );
159 vtkScalarBarActor* aScalarBar = 0;
162 if ( !myScalarBars.contains( (size_t)aViewer ) )
164 createScalarBar( (size_t)aViewer );
166 aScalarBar = myScalarBars[ (size_t)aViewer ];
168 if ( aView->getRenderer()->HasViewProp( aScalarBar ) )
170 aView->getRenderer()->RemoveActor2D( aScalarBar );
174 // Invalidate global Z range
175 double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
176 SetZRange( (size_t)aViewer, anInvalidRange );
178 int anInvalidZ = HYDROGUI_VTKPrs::InvalidZValue();
179 bool isChanged = false;
180 HYDROGUI_VTKPrs* aPrs;
181 for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
183 Handle(HYDROData_Entity) anObj = theObjs.Value( i );
184 if( !anObj.IsNull() )
186 bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
187 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
189 bool anIsInserted = ( aPrs != 0 );
190 if( anIsVisible && ( !aPrs || aPrs->getIsToUpdate() || theIsForced ) )
192 if( HYDROGUI_VTKPrsDriver* aDriver = getDriver( (size_t)aViewer, anObj ) )
194 if( aDriver->Update( anObj, aPrs ) && aPrs && !anIsInserted )
196 module()->setObjectVTKPrs( (size_t)aViewer, anObj, aPrs );
206 // Extend the global Z range if necessary
207 double* aGlobalRange = GetZRange( (size_t)aViewer );
208 double* aRange = aPrs->getInternalZRange();
209 bool anIsUpdate = false;
210 if ( aRange[0] < aGlobalRange[0] || ValuesEquals( aGlobalRange[0], anInvalidZ ) )
212 aGlobalRange[0] = aRange[0];
215 if ( aRange[1] > aGlobalRange[1] || ValuesEquals( aGlobalRange[1], anInvalidZ ) )
217 aGlobalRange[1] = aRange[1];
223 module()->updateVTKZRange( (size_t)aViewer, aGlobalRange );
226 aViewer->Display( aPrs );
231 aViewer->Erase( aPrs );
240 if ( isChanged && aScalarBar )
242 // Show colors legend bar
243 aView->getRenderer()->AddActor2D( aScalarBar );
249 // Repaint is done inside OnFitAll()
250 aView->onAccelAction( SUIT_Accel::ZoomFit );
252 else if ( isChanged )
260 void HYDROGUI_VTKPrsDisplayer::purgeObjects( const int theViewerId )
262 SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
265 SALOME_ListIO aListIO;
266 aViewer->GetVisible( aListIO );
268 HYDROGUI_VTKPrs* aPrs;
269 SALOME_ListIteratorOfListIO anIter( aListIO );
270 for( ; anIter.More(); anIter.Next() )
272 Handle(SALOME_InteractiveObject) aPrsObj = anIter.Value();
273 if ( !aPrsObj.IsNull() )
275 Handle(HYDROData_Entity) anOwnerObj =
276 module()->getDataModel()->objectByEntry( aPrsObj->getEntry() );
277 if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
279 aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anOwnerObj );
282 aViewer->Erase( aPrs );
284 module()->removeObjectVTKPrs( (size_t)aViewer, anOwnerObj );
291 HYDROGUI_VTKPrsDriver* HYDROGUI_VTKPrsDisplayer::getDriver( const int theViewId, const Handle(HYDROData_Entity)& theObj )
293 HYDROGUI_VTKPrsDriver* aDriver = NULL;
294 ObjectKind aKind = theObj->GetKind();
295 if( theObj->GetKind() == KIND_BATHYMETRY )
299 myDriver = new HYDROGUI_VTKPrsBathymetryDriver( myScalarBars[ theViewId ] );
307 QString HYDROGUI_VTKPrsDisplayer::GetType() const
309 return SVTK_Viewer::Type();
312 void HYDROGUI_VTKPrsDisplayer::SetZRange( const int theViewId, double theRange[] )
314 myScalarBars[ theViewId ]->GetLookupTable()->SetRange( theRange );
317 double* HYDROGUI_VTKPrsDisplayer::GetZRange( const int theViewId ) const
319 return myScalarBars[ theViewId ]->GetLookupTable()->GetRange();
322 void HYDROGUI_VTKPrsDisplayer::createScalarBar( const int theViewId )
324 if ( !myScalarBars.contains( theViewId ) )
326 // The invalid value is used to identify the case when the table range is not initialized yet.
327 double anInvalidValue = HYDROGUI_VTKPrs::InvalidZValue();
328 vtkLookupTable* aTable = vtkLookupTable::New();
329 aTable->SetHueRange( HUE_START, HUE_END );
330 aTable->SetSaturationRange( SATURATION_START, SATURATION_END );
331 aTable->SetTableRange( anInvalidValue, anInvalidValue );
332 aTable->SetValueRange( 1.0, 1.0 );
333 aTable->SetAlphaRange( 1.0, 1.0 );
334 aTable->SetNumberOfColors( NB_COLORS );
336 vtkSmartPointer<vtkScalarBarActor> aScalarBar = vtkScalarBarActor::New();
337 aScalarBar->SetLookupTable( aTable );
339 myScalarBars.insert( theViewId, aScalarBar );