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