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