]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_VTKPrsDisplayer.cxx
Salome HOME
22912ce372de41eb7ad5ff7359aa97236bc01e1c
[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     if ( myScalarBars.contains( (size_t)aViewer ) )
107     {
108       SUIT_ViewManager* aViewMgr = dynamic_cast<SUIT_ViewManager*>( aViewer->getViewManager() );
109       if ( aViewMgr && aViewMgr->getViewsCount() > 0 )
110       {
111         SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>( aViewMgr->getActiveView() );
112         if ( aView )
113         {
114           vtkScalarBarActor* aScalarBar = myScalarBars[ (size_t)aViewer ];
115           if ( aView->getRenderer()->HasViewProp( aScalarBar ) )
116           {
117             aView->getRenderer()->RemoveActor2D( aScalarBar );
118           }
119         }
120       }
121       myScalarBars.remove( (size_t)aViewer );
122     }
123   }
124 }
125
126 void HYDROGUI_VTKPrsDisplayer::Erase( const HYDROData_SequenceOfObjects& theObjs,
127                                 const int theViewerId )
128 {
129   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
130   if( aViewer )
131   {
132     HYDROGUI_VTKPrs* aPrs;
133     for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
134     {
135       Handle(HYDROData_Entity) anObj = theObjs.Value( i );
136       if( anObj.IsNull() )
137         continue;
138
139       aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
140       if ( aPrs )
141       {
142         aViewer->Erase( aPrs, true );
143       }
144       if ( anObj->IsRemoved() )
145       {
146         module()->removeObjectVTKPrs( (size_t)aViewer, anObj );
147       }
148     }
149   }
150 }
151
152 void HYDROGUI_VTKPrsDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs,
153                                         const int theViewerId,
154                                         const bool theIsForced,
155                                         const bool theDoFitAll)
156 {
157   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
158   if( aViewer )
159   {
160     // Hide colors legend bar
161     SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(
162       aViewer->getViewManager()->getActiveView() );
163     vtkScalarBarActor* aScalarBar = 0;
164     if ( aView )
165     {
166       if ( !myScalarBars.contains( (size_t)aViewer ) )
167       {
168         createScalarBar( (size_t)aViewer );
169       }
170       aScalarBar = myScalarBars[ (size_t)aViewer ];
171
172       if ( aView->getRenderer()->HasViewProp( aScalarBar ) )
173       {
174         aView->getRenderer()->RemoveActor2D( aScalarBar );
175       }
176     }
177
178     // Invalidate global Z range
179     double anInvalidRange[2] = { HYDROGUI_VTKPrs::InvalidZValue(), HYDROGUI_VTKPrs::InvalidZValue() };
180     SetZRange( (size_t)aViewer, anInvalidRange );
181
182     int anInvalidZ = HYDROGUI_VTKPrs::InvalidZValue();
183     bool isChanged = false;
184     HYDROGUI_VTKPrs* aPrs;
185     for ( int i = 1, n = theObjs.Length(); i <= n; i++ )
186     {
187       Handle(HYDROData_Entity) anObj = theObjs.Value( i );
188       if( !anObj.IsNull() )
189       {
190         bool anIsVisible = module()->isObjectVisible( (size_t)aViewer, anObj );
191         aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anObj );
192
193         bool anIsInserted = ( aPrs != 0 );
194         if( anIsVisible && ( !aPrs || aPrs->getIsToUpdate() || theIsForced ) )
195         {
196           if( HYDROGUI_VTKPrsDriver* aDriver = getDriver( (size_t)aViewer, anObj ) )
197           {
198             if( aDriver->Update( anObj, aPrs ) && aPrs && !anIsInserted )
199             {
200               module()->setObjectVTKPrs( (size_t)aViewer, anObj, aPrs );
201             }
202           }
203         }
204
205         if( aPrs )
206         {
207           if ( anIsVisible )
208           {
209
210             // Extend the global Z range if necessary
211             double* aGlobalRange = GetZRange( (size_t)aViewer );
212             double* aRange = aPrs->getInternalZRange();
213             bool anIsUpdate = false;
214             if ( aRange[0] < aGlobalRange[0] || ValuesEquals( aGlobalRange[0], anInvalidZ ) )
215             {
216               aGlobalRange[0] = aRange[0];
217               anIsUpdate = true;
218             }
219             if ( aRange[1] > aGlobalRange[1] || ValuesEquals( aGlobalRange[1], anInvalidZ ) )
220             {
221               aGlobalRange[1] = aRange[1];
222               anIsUpdate = true;
223             }
224
225             if ( anIsUpdate )
226             {
227               module()->updateVTKZRange( (size_t)aViewer, aGlobalRange );
228             }
229
230             aViewer->Display( aPrs );
231
232           }
233           else
234           {
235             aViewer->Erase( aPrs );
236           }
237           isChanged = true;
238         }
239       }
240     }
241
242     if ( aView ) 
243     {
244       if ( isChanged && aScalarBar )
245       {
246         // Show colors legend bar
247           aView->getRenderer()->AddActor2D( aScalarBar );
248       }
249
250       // Refresh the view
251       if ( theDoFitAll )
252       {
253         // Repaint is done inside OnFitAll()
254         aView->onAccelAction( SUIT_Accel::ZoomFit );
255       } 
256       else if ( isChanged )
257       {
258         aView->Repaint();
259       }
260     }
261   }
262 }
263
264 void HYDROGUI_VTKPrsDisplayer::purgeObjects( const int theViewerId )
265 {
266   SVTK_Viewer* aViewer = module()->getVTKViewer( theViewerId );
267   if( aViewer )
268   {
269     SALOME_ListIO aListIO;
270     aViewer->GetVisible( aListIO );
271
272     HYDROGUI_VTKPrs* aPrs;
273     SALOME_ListIteratorOfListIO anIter( aListIO );
274     for( ; anIter.More(); anIter.Next() )
275     {
276       Handle(SALOME_InteractiveObject) aPrsObj = anIter.Value();
277       if ( !aPrsObj.IsNull() )
278       {
279         Handle(HYDROData_Entity) anOwnerObj = 
280           module()->getDataModel()->objectByEntry( aPrsObj->getEntry() );
281         if ( !anOwnerObj.IsNull() && anOwnerObj->IsRemoved() )
282         {
283           aPrs = module()->getObjectVTKPrs( (size_t)aViewer, anOwnerObj );
284           if ( aPrs )
285           {
286             aViewer->Erase( aPrs );
287           }
288           module()->removeObjectVTKPrs( (size_t)aViewer, anOwnerObj );
289         }
290       }
291     }
292   }
293 }
294
295 HYDROGUI_VTKPrsDriver* HYDROGUI_VTKPrsDisplayer::getDriver( const int theViewId, const Handle(HYDROData_Entity)& theObj )
296 {
297   HYDROGUI_VTKPrsDriver* aDriver = NULL;
298   ObjectKind aKind = theObj->GetKind();
299   if( theObj->GetKind() == KIND_BATHYMETRY )
300   {
301     if ( !myDriver )
302     {
303       myDriver = new HYDROGUI_VTKPrsBathymetryDriver( myScalarBars[ theViewId ] );
304     }
305     aDriver = myDriver;
306   }
307
308   return aDriver;
309 }
310
311 QString HYDROGUI_VTKPrsDisplayer::GetType() const
312 {
313   return SVTK_Viewer::Type();
314 }
315
316 void HYDROGUI_VTKPrsDisplayer::SetZRange( const int theViewId, double theRange[] )
317 {
318   myScalarBars[ theViewId ]->GetLookupTable()->SetRange( theRange );
319 }
320
321 double* HYDROGUI_VTKPrsDisplayer::GetZRange( const int theViewId ) const
322 {
323   return myScalarBars[ theViewId ]->GetLookupTable()->GetRange();
324 }
325
326 void HYDROGUI_VTKPrsDisplayer::createScalarBar( const int theViewId )
327 {
328   if ( !myScalarBars.contains( theViewId ) )
329   {
330     // The invalid value is used to identify the case when the table range is not initialized yet.
331     double anInvalidValue = HYDROGUI_VTKPrs::InvalidZValue();
332     vtkLookupTable* aTable = vtkLookupTable::New();
333     aTable->SetHueRange( HUE_START, HUE_END );
334     aTable->SetSaturationRange( SATURATION_START, SATURATION_END );
335     aTable->SetTableRange( anInvalidValue, anInvalidValue );
336     aTable->SetValueRange( 1.0, 1.0 );
337     aTable->SetAlphaRange( 1.0, 1.0 );
338     aTable->SetNumberOfColors( NB_COLORS );
339     aTable->Build();
340     vtkSmartPointer<vtkScalarBarActor> aScalarBar = vtkScalarBarActor::New();
341     aScalarBar->SetLookupTable( aTable );
342     aTable->Delete();
343     myScalarBars.insert( theViewId, aScalarBar );
344   }
345 }