Salome HOME
bug #136 - problems with performance
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_VTKPrsDisplayer.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_VTKPrsDisplayer.h"
24
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"
30
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>
39
40 #include <vtkLookupTable.h>
41 #include <vtkRenderer.h>
42 #include <vtkTextProperty.h>
43 #include <vtkWindow.h>
44 #include <vtkActor2DCollection.h>
45
46 #include <QVector>
47
48 #define NB_COLORS 32
49
50 // Saturation of blue
51 //#define HUE_START 0.69 
52 //#define HUE_END   0.41
53 //#define SATURATION_START 1.0 
54 //#define SATURATION_END   0.4
55
56 #define HUE_START 0.7
57 #define HUE_END   0.0 
58 #define SATURATION_START 1.0 
59 #define SATURATION_END   1.0
60
61 HYDROGUI_VTKPrsDisplayer::HYDROGUI_VTKPrsDisplayer( HYDROGUI_Module* theModule )
62 : HYDROGUI_AbstractDisplayer( theModule ), myDriver( NULL )
63 {
64 }
65
66 HYDROGUI_VTKPrsDisplayer::~HYDROGUI_VTKPrsDisplayer()
67 {
68 }
69
70 void HYDROGUI_VTKPrsDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
71                                       const int theViewerId )
72 {
73   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
74   if( !aViewer )
75   {
76     HYDROGUI_VTKPrs* anObjShape;
77     for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
78     {
79       Handle(HYDROData_Entity) anObj = theObjs.Value( i );
80       if( !anObj.IsNull() )
81       {
82         anObjShape = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
83         if ( anObjShape )
84         {
85           anObjShape->setIsToUpdate( true );
86         }
87       }
88     }
89   }
90 }
91
92 void HYDROGUI_VTKPrsDisplayer::EraseAll( const int theViewerId )
93 {
94   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
95   if( aViewer )
96   {
97     aViewer->EraseAll( true );
98     module()->removeViewVTKPrs( (size_t)aViewer );
99   }
100 }
101
102 void HYDROGUI_VTKPrsDisplayer::DeleteScalarBar( const int theViewerId )
103 {
104   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
105   if( aViewer )
106   {
107     if ( myScalarBars.contains( (size_t)aViewer ) )
108     {
109       SUIT_ViewManager* aViewMgr = dynamic_cast<SUIT_ViewManager*>( aViewer->getViewManager() );
110       if ( aViewMgr && aViewMgr->getViewsCount() > 0 )
111       {
112         SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>( aViewMgr->getActiveView() );
113         if ( aView )
114         {
115           vtkScalarBarActor* aScalarBar = myScalarBars[ (size_t)aViewer ];
116           if ( aView->getRenderer()->HasViewProp( aScalarBar ) )
117           {
118             aView->getRenderer()->RemoveActor2D( aScalarBar );
119           }
120         }
121       }
122       myScalarBars.remove( (size_t)aViewer );
123     }
124   }
125 }
126
127 void HYDROGUI_VTKPrsDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
128                                 const int theViewerId )
129 {
130   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
131   if( aViewer )
132   {
133     HYDROGUI_VTKPrs* aPrs;
134     for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
135     {
136       Handle(HYDROData_Entity) anObj = theObjs.Value( i );
137       if( anObj.IsNull() )
138         continue;
139
140       aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
141       if ( aPrs )
142       {
143         aViewer->Erase( aPrs, true );
144       }
145       if ( anObj->IsRemoved() )
146       {
147         module()->removeObjectVTKPrs( (size_t)aViewer, anObj );
148       }
149     }
150   }
151 }
152
153 void HYDROGUI_VTKPrsDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
154                                         const int theViewerId,
155                                         const bool theIsForced,
156                                         const bool theDoFitAll)
157 {
158   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
159   if( aViewer )
160   {
161     // Hide colors legend bar
162     SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(
163       aViewer->getViewManager()->getActiveView() );
164     vtkScalarBarActor* aScalarBar = 0;
165     if ( aView )
166     {
167       if ( !myScalarBars.contains( (size_t)aViewer ) )
168       {
169         createScalarBar( (size_t)aViewer );
170       }
171       aScalarBar = myScalarBars[ (size_t)aViewer ];
172
173       if ( aView->getRenderer()->HasViewProp( aScalarBar ) )
174       {
175         aView->getRenderer()->RemoveActor2D( aScalarBar );
176       }
177     }
178
179     // Invalidate global Z range
180     double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
181     SetZRange( (size_t)aViewer, anInvalidRange );
182
183     int anInvalidZ = HYDROGUI_VTKPrs::InvalidZValue();
184     bool isChanged = false;
185     HYDROGUI_VTKPrs* aPrs;
186     for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
187     {
188       Handle(HYDROData_Entity) anObj = theObjs.Value( i );
189       if( !anObj.IsNull() )
190       {
191         bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
192         aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
193
194         bool anIsInserted = ( aPrs != 0 );
195         if( anIsVisible && ( !aPrs || aPrs->getIsToUpdate() || theIsForced ) )
196         {
197           if( HYDROGUI_VTKPrsDriver* aDriver = getDriver( (size_t)aViewer, anObj ) )
198           {
199             if( aDriver->Update( anObj, aPrs ) && aPrs && !anIsInserted )
200             {
201               module()->setObjectVTKPrs( (size_t)aViewer, anObj, aPrs );
202             }
203           }
204         }
205
206         if( aPrs )
207         {
208           if ( anIsVisible )
209           {
210
211             // Extend the global Z range if necessary
212             double* aGlobalRange = GetZRange( (size_t)aViewer );
213             double* aRange = aPrs->getInternalZRange();
214             bool anIsUpdate = false;
215             if ( aRange[0] < aGlobalRange[0] || ValuesEquals( aGlobalRange[0], anInvalidZ ) )
216             {
217               aGlobalRange[0] = aRange[0];
218               anIsUpdate = true;
219             }
220             if ( aRange[1] > aGlobalRange[1] || ValuesEquals( aGlobalRange[1], anInvalidZ ) )
221             {
222               aGlobalRange[1] = aRange[1];
223               anIsUpdate = true;
224             }
225
226             if ( anIsUpdate )
227             {
228               module()->updateVTKZRange( (size_t)aViewer, aGlobalRange );
229             }
230
231             aViewer->Display( aPrs );
232
233           }
234           else
235           {
236             aViewer->Erase( aPrs );
237           }
238           isChanged = true;
239         }
240       }
241     }
242
243     if ( aView ) 
244     {
245       if ( isChanged && aScalarBar )
246       {
247         // Show colors legend bar
248           aView->getRenderer()->AddActor2D( aScalarBar );
249       }
250
251       // Refresh the view
252       if ( theDoFitAll )
253       {
254         // Repaint is done inside OnFitAll()
255         aView->onAccelAction( SUIT_Accel::ZoomFit );
256       } 
257       else if ( isChanged )
258       {
259         aView->Repaint();
260       }
261     }
262   }
263 }
264
265 void HYDROGUI_VTKPrsDisplayer::purgeObjects( const int theViewerId )
266 {
267   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
268   if( aViewer )
269   {
270     SALOME_ListIO aListIO;
271     aViewer->GetVisible( aListIO );
272
273     HYDROGUI_VTKPrs* aPrs;
274     SALOME_ListIteratorOfListIO anIter( aListIO );
275     for( ; anIter.More(); anIter.Next() )
276     {
277       Handle(SALOME_InteractiveObject) aPrsObj = anIter.Value();
278       if ( !aPrsObj.IsNull() )
279       {
280         Handle(HYDROData_Entity) anOwnerObj = 
281           module()->getDataModel()->objectByEntry( aPrsObj->getEntry() );
282         if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
283         {
284           aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anOwnerObj );
285           if ( aPrs )
286           {
287             aViewer->Erase( aPrs );
288           }
289           module()->removeObjectVTKPrs( (size_t)aViewer, anOwnerObj );
290         }
291       }
292     }
293   }
294 }
295
296 HYDROGUI_VTKPrsDriver* HYDROGUI_VTKPrsDisplayer::getDriver( const int theViewId, const Handle(HYDROData_Entity)& theObj )
297 {
298   HYDROGUI_VTKPrsDriver* aDriver = NULL;
299   ObjectKind aKind = theObj->GetKind();
300   if( theObj->GetKind() == KIND_BATHYMETRY )
301   {
302     if ( !myDriver )
303     {
304       myDriver = new HYDROGUI_VTKPrsBathymetryDriver( myScalarBars[ theViewId ] );
305     }
306     aDriver = myDriver;
307   }
308
309   return aDriver;
310 }
311
312 QString HYDROGUI_VTKPrsDisplayer::GetType() const
313 {
314   return SVTK_Viewer::Type();
315 }
316
317 void HYDROGUI_VTKPrsDisplayer::SetZRange( const int theViewId, double theRange[] )
318 {
319   myScalarBars[ theViewId ]->GetLookupTable()->SetRange( theRange );
320 }
321
322 double* HYDROGUI_VTKPrsDisplayer::GetZRange( const int theViewId ) const
323 {
324   return myScalarBars[ theViewId ]->GetLookupTable()->GetRange();
325 }
326
327 void HYDROGUI_VTKPrsDisplayer::createScalarBar( const int theViewId )
328 {
329   if ( !myScalarBars.contains( theViewId ) )
330   {
331     // The invalid value is used to identify the case when the table range is not initialized yet.
332     double anInvalidValue = HYDROGUI_VTKPrs::InvalidZValue();
333     vtkLookupTable* aTable = vtkLookupTable::New();
334     aTable->SetHueRange( HUE_START, HUE_END );
335     aTable->SetSaturationRange( SATURATION_START, SATURATION_END );
336     aTable->SetTableRange( anInvalidValue, anInvalidValue );
337     aTable->SetValueRange( 1.0, 1.0 );
338     aTable->SetAlphaRange( 1.0, 1.0 );
339     aTable->SetNumberOfColors( NB_COLORS );
340     aTable->Build();
341     vtkSmartPointer<vtkScalarBarActor> aScalarBar = vtkScalarBarActor::New();
342     aScalarBar->SetLookupTable( aTable );
343     aScalarBar->SetNumberOfLabels( NB_COLORS * 0.75 );
344     aScalarBar->SetWidth( aScalarBar->GetWidth() / 1.5 );
345     aScalarBar->SetTextureGridWidth( aScalarBar->GetTextureGridWidth() * 4. );
346     aTable->Delete();
347     myScalarBars.insert( theViewId, aScalarBar );
348   }
349 }