Salome HOME
portage V8_5_0
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_VTKPrsDisplayer.cxx
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.
6 //
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.
11 //
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
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_VTKPrsDisplayer.h"
20
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"
27
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>
36
37 #include <vtkLookupTable.h>
38 #include <vtkRenderer.h>
39 #include <vtkTextProperty.h>
40 #include <vtkWindow.h>
41 #include <vtkActor2DCollection.h>
42
43 #include <QVector>
44
45 #define NB_COLORS 32
46
47 // Saturation of blue
48 //#define HUE_START 0.69 
49 //#define HUE_END   0.41
50 //#define SATURATION_START 1.0 
51 //#define SATURATION_END   0.4
52
53 #define HUE_START 0.7
54 #define HUE_END   0.0 
55 #define SATURATION_START 1.0 
56 #define SATURATION_END   1.0
57
58 HYDROGUI_VTKPrsDisplayer::HYDROGUI_VTKPrsDisplayer( HYDROGUI_Module* theModule )
59 : HYDROGUI_AbstractDisplayer( theModule ), myDriver( NULL ), myShapeDriver( NULL )
60 {
61 }
62
63 HYDROGUI_VTKPrsDisplayer::~HYDROGUI_VTKPrsDisplayer()
64 {
65 }
66
67 void HYDROGUI_VTKPrsDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theObjs,
68                                       const int theViewerId )
69 {
70   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
71   if( !aViewer )
72   {
73     HYDROGUI_VTKPrs* anObjShape;
74     for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
75     {
76       Handle(HYDROData_Entity) anObj = theObjs.Value( i );
77       if( !anObj.IsNull() )
78       {
79         anObjShape = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
80         if ( anObjShape )
81         {
82           anObjShape->setIsToUpdate( true );
83         }
84       }
85     }
86   }
87 }
88
89 void HYDROGUI_VTKPrsDisplayer::DisplayAll( const int theViewerId,
90                                            const bool theIsForced,
91                                            const bool theDoFitAll )
92 {
93   HYDROGUI_AbstractDisplayer::DisplayAll( theViewerId, theIsForced, theDoFitAll );
94
95   bool isEraseScalarBar = true;
96
97   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
98   if( aViewer )
99   {
100     SALOME_ListIO aListIO;
101     aViewer->GetVisible( aListIO );
102     
103     HYDROGUI_VTKPrs* aPrs;
104     SALOME_ListIteratorOfListIO anIter( aListIO );
105     for( ; anIter.More(); anIter.Next() )
106     {
107       Handle(SALOME_InteractiveObject) aPrsObj = anIter.Value();
108       if ( !aPrsObj.IsNull() )
109       {
110         Handle(HYDROData_Entity) anObj = 
111           module()->getDataModel()->objectByEntry( aPrsObj->getEntry() );
112         aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
113         if( aPrs->needScalarBar() )
114         {
115           isEraseScalarBar = false;
116           break;
117         }
118       }
119     }
120   }
121
122   if( isEraseScalarBar )
123     EraseScalarBar( theViewerId );
124 }
125
126 void HYDROGUI_VTKPrsDisplayer::EraseAll( const int theViewerId )
127 {
128   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
129   if( aViewer )
130   {
131     aViewer->EraseAll( 0, true );
132     module()->removeViewVTKPrs( (size_t)aViewer );
133     EraseScalarBar( theViewerId );
134   }
135 }
136
137 void HYDROGUI_VTKPrsDisplayer::EraseScalarBar( const int theViewerId, const bool theIsDelete )
138 {
139   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
140   if( aViewer )
141   {
142     if ( myScalarBars.contains( (size_t)aViewer ) )
143     {
144       SUIT_ViewManager* aViewMgr = dynamic_cast<SUIT_ViewManager*>( aViewer->getViewManager() );
145       if ( aViewMgr && aViewMgr->getViewsCount() > 0 )
146       {
147         SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>( aViewMgr->getActiveView() );
148         if ( aView )
149         {
150           vtkScalarBarActor* aScalarBar = myScalarBars[ (size_t)aViewer ];
151           if ( aView->getRenderer()->HasViewProp( aScalarBar ) )
152           {
153             aView->getRenderer()->RemoveActor2D( aScalarBar );
154           }
155         }
156       }
157       if ( theIsDelete )
158       {
159         myScalarBars.remove( (size_t)aViewer );
160       }
161     }
162   }
163 }
164
165 void HYDROGUI_VTKPrsDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
166                                 const int theViewerId )
167 {
168   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
169   if( aViewer )
170   {
171     HYDROGUI_VTKPrs* aPrs;
172     for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
173     {
174       Handle(HYDROData_Entity) anObj = theObjs.Value( i );
175       if( anObj.IsNull() )
176         continue;
177
178       aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
179       if ( aPrs )
180       {
181         aViewer->Erase( aPrs, true );
182       }
183       if ( anObj->IsRemoved() )
184       {
185         module()->removeObjectVTKPrs( (size_t)aViewer, anObj );
186       }
187     }
188   }
189 }
190
191 void HYDROGUI_VTKPrsDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
192                                         const int theViewerId,
193                                         const bool theIsForced,
194                                         const bool theDoFitAll)
195 {
196   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
197   if( aViewer )
198   {
199     // Hide colors legend bar
200     SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(
201       aViewer->getViewManager()->getActiveView() );
202     vtkScalarBarActor* aScalarBar = 0;
203     if ( aView )
204     {
205       if ( !myScalarBars.contains( (size_t)aViewer ) )
206       {
207         createScalarBar( (size_t)aViewer );
208       }
209       aScalarBar = myScalarBars[ (size_t)aViewer ];
210
211       if ( aView->getRenderer()->HasViewProp( aScalarBar ) )
212       {
213         aView->getRenderer()->RemoveActor2D( aScalarBar );
214       }
215     }
216
217     // Invalidate global Z range
218     double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
219     SetZRange( (size_t)aViewer, anInvalidRange );
220
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++ )
226     {
227       Handle(HYDROData_Entity) anObj = theObjs.Value( i );
228       if( !anObj.IsNull() )
229       {
230         bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
231         aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
232
233         bool anIsInserted = ( aPrs != 0 );
234         if( anIsVisible && ( !aPrs || aPrs->getIsToUpdate() || theIsForced ) )
235         {
236           // Erase the presentation in the view because of the problem with GEOM_Actor upadting on second SetShape.
237           if ( aPrs )
238           {
239             aViewer->Erase( aPrs, true );
240           }
241           // Update the presentation
242           if( HYDROGUI_VTKPrsDriver* aDriver = getDriver( (size_t)aViewer, anObj ) )
243           {
244             if( aDriver->Update( anObj, aPrs ) && aPrs && !anIsInserted )
245             {
246               module()->setObjectVTKPrs( (size_t)aViewer, anObj, aPrs );
247             }
248           }
249         }
250
251         if( aPrs )
252         {
253           if ( anIsVisible )
254           {
255             if ( aPrs->needScalarBar() )
256             {
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 ) )
262               {
263                 aGlobalRange[0] = aRange[0];
264                 anIsUpdate = true;
265               }
266               if ( aRange[1] > aGlobalRange[1] || ValuesEquals( aGlobalRange[1], anInvalidZ ) )
267               {
268                 aGlobalRange[1] = aRange[1];
269                 anIsUpdate = true;
270               }
271
272               if ( anIsUpdate )
273               {
274                 module()->updateVTKZRange( (size_t)aViewer, aGlobalRange );
275               }
276             }
277
278             aViewer->Display( aPrs );
279             isScalarBarNeeded = isScalarBarNeeded || aPrs->needScalarBar();
280
281           }
282           else
283           {
284             aViewer->Erase( aPrs );
285           }
286           isChanged = true;
287         }
288       }
289     }
290
291     if ( aView ) 
292     {
293       if ( isChanged && isScalarBarNeeded && aScalarBar )
294       {
295         // Show colors legend bar
296           aView->getRenderer()->AddActor2D( aScalarBar );
297       }
298
299       // Refresh the view
300       if ( theDoFitAll )
301       {
302         // Repaint is done inside OnFitAll()
303         aView->onAccelAction( SUIT_Accel::ZoomFit );
304       } 
305       else if ( isChanged )
306       {
307         aView->Repaint( true );
308       }
309     }
310   }
311 }
312
313 void HYDROGUI_VTKPrsDisplayer::purgeObjects( const int theViewerId )
314 {
315   bool doEraseScalarBar = false;
316
317   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
318   if( aViewer )
319   {
320     SALOME_ListIO aListIO;
321     aViewer->GetVisible( aListIO );
322     
323     HYDROGUI_VTKPrs* aPrs;
324     SALOME_ListIteratorOfListIO anIter( aListIO );
325     for( ; anIter.More(); anIter.Next() )
326     {
327       Handle(SALOME_InteractiveObject) aPrsObj = anIter.Value();
328       if ( !aPrsObj.IsNull() )
329       {
330         Handle(HYDROData_Entity) anOwnerObj = 
331           module()->getDataModel()->objectByEntry( aPrsObj->getEntry() );
332         if ( anOwnerObj.IsNull() )
333         {
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 )
338           {
339             QVector<SUIT_ViewWindow*> aViews = aViewMgr->getViews();
340             foreach ( SUIT_ViewWindow* aView, aViews )
341             {
342               SVTK_ViewWindow* aVTKView = dynamic_cast<SVTK_ViewWindow*>( aView );
343               if ( aVTKView )
344               {
345                 aVTKView->Erase( aPrsObj, false );
346               }
347             }
348           }
349           // Remove the object presentation from the module's cache
350           module()->removeObjectVTKPrs( (size_t)aViewer, aPrsObj->getEntry() );
351           doEraseScalarBar = true;
352         }
353         else if ( anOwnerObj->IsRemoved() )
354         {
355           aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anOwnerObj );
356           if ( aPrs )
357           {
358             aViewer->Erase( aPrs );
359           }
360           else
361           {
362           }
363           module()->removeObjectVTKPrs( (size_t)aViewer, anOwnerObj );
364           doEraseScalarBar = true;
365         }
366       }
367     }
368
369     if ( doEraseScalarBar )
370     {
371       EraseScalarBar( theViewerId );
372     }
373   }
374 }
375
376 HYDROGUI_VTKPrsDriver* HYDROGUI_VTKPrsDisplayer::getDriver( const int theViewId, const Handle(HYDROData_Entity)& theObj )
377 {
378   HYDROGUI_VTKPrsDriver* aDriver = NULL;
379   ObjectKind aKind = theObj->GetKind();
380   if( theObj->GetKind() == KIND_BATHYMETRY )
381   {
382     if ( !myDriver )
383     {
384       myDriver = new HYDROGUI_VTKPrsBathymetryDriver( myScalarBars[ theViewId ] );
385     }
386     aDriver = myDriver;
387   }
388   else
389   {
390     if ( !myShapeDriver )
391     {
392       myShapeDriver = new HYDROGUI_VTKPrsShapeDriver( myScalarBars[ theViewId ] );
393     }
394     aDriver = myShapeDriver;
395   }
396
397   return aDriver;
398 }
399
400 QString HYDROGUI_VTKPrsDisplayer::GetType() const
401 {
402   return SVTK_Viewer::Type();
403 }
404
405 void HYDROGUI_VTKPrsDisplayer::SetZRange( const int theViewId, double theRange[] )
406 {
407   myScalarBars[ theViewId ]->GetLookupTable()->SetRange( theRange );
408 }
409
410 double* HYDROGUI_VTKPrsDisplayer::GetZRange( const int theViewId ) const
411 {
412   return myScalarBars[ theViewId ]->GetLookupTable()->GetRange();
413 }
414
415 void HYDROGUI_VTKPrsDisplayer::createScalarBar( const int theViewId )
416 {
417   if ( !myScalarBars.contains( theViewId ) )
418   {
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 );
428     aTable->Build();
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
438     aTable->Delete();
439     myScalarBars.insert( theViewId, aScalarBar );
440   }
441 }