Salome HOME
Fix for Bug IPAL8945
[modules/visu.git] / src / VVTK / VVTK_Renderer.cxx
1 //  SALOME VTKViewer : build VTK viewer into Salome desktop
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   :
25 //  Author :
26 //  Module :
27 //  $Header$
28
29 #include "VVTK_Renderer.h"
30
31 #include "VISU_GaussPtsAct.h"
32 #include "VISU_GaussPointsPL.hxx"
33 #include "VISU_WidgetCtrl.hxx"
34 #include "VISU_PlanesWidget.hxx"
35 #include "VISU_SphereWidget.hxx"
36
37 #include <vtkObjectFactory.h>
38 #include <vtkProperty.h>
39 #include <vtkPointPicker.h>
40
41 #include <vtkRenderWindowInteractor.h>
42 #include <vtkCallbackCommand.h>
43 #include <vtkCommand.h>
44 #include <vtkPlane.h>
45
46 #include <vtkPropCollection.h>
47 #include <vtkProp.h>
48 #include <vtkActor.h>
49 #include <vtkMapper.h>
50 #include <vtkPolyDataMapper.h>
51 #include <vtkPolyData.h>
52 #include <vtkTextMapper.h>
53 #include <vtkTextActor.h> 
54 #include <vtkTextProperty.h>
55 #include <vtkRenderer.h>
56
57 #include <vtkPropCollection.h>
58 #include <vtkProp.h>
59 #include <vtkActor.h>
60 #include <vtkMapper.h>
61 #include <vtkPolyDataMapper.h>
62 #include <vtkPolyData.h>
63 #include <vtkTextMapper.h>
64 #include <vtkTextActor.h> 
65 #include <vtkTextProperty.h>
66 #include <vtkRenderer.h>
67
68 #include "utilities.h"
69
70 #ifdef _DEBUG_
71 static int MYDEBUG = 0;
72 #else
73 static int MYDEBUG = 0;
74 #endif
75
76 //======================================================================
77 class VISU_FPSActor : public vtkTextActor 
78 {
79 public:
80   vtkTypeMacro( VISU_FPSActor, vtkTextActor);
81   static
82   VISU_FPSActor* 
83   New();
84
85   virtual
86   int
87   RenderOpaqueGeometry(vtkViewport *theViewport);
88 };
89
90 //======================================================================
91 vtkStandardNewMacro(VISU_FPSActor);
92
93 //======================================================================
94 // function: RenderOpaqueGeometry
95 // purpose :
96 //======================================================================
97 int
98 VISU_FPSActor
99 ::RenderOpaqueGeometry(vtkViewport *theViewport) 
100 {
101   // It's impossible to render opaque geometry of text actor
102   // if the size of the viewport is less than 1.0
103   int *size = theViewport->GetSize();
104   if( size[0] <= 1.0 || size[1] <= 1.0 )
105     return 1;
106
107   if(vtkRenderer *aRenderer = dynamic_cast<vtkRenderer*>(theViewport)){
108     static float aTol = 1.e-6;
109     float aLastRenderTimeInSeconds = aRenderer->GetLastRenderTimeInSeconds();
110     if(aLastRenderTimeInSeconds > aTol){
111       size_t aNumberOfCells = 0;
112       if(vtkActorCollection *anActorCollection = aRenderer->GetActors()){
113         anActorCollection->InitTraversal();
114         while(vtkActor *anActor = anActorCollection->GetNextActor()){
115           if(anActor->GetVisibility()){
116             if(SALOME_Actor *aSActor = dynamic_cast<SALOME_Actor*>(anActor)){
117               if(vtkMapper *aMapper = aSActor->GetMapper()){
118                 if(vtkDataSet *aDataSet = aMapper->GetInput()){
119                   aNumberOfCells += aDataSet->GetNumberOfCells();
120                 }
121               }
122             }
123           }
124         }
125       }
126       std::ostringstream aStr;
127       float aFPS = 1.0 / aLastRenderTimeInSeconds;
128       aStr<<"FPS: "<<aFPS<<"; NumberOfCells: "<<aNumberOfCells;
129       std::string anInput = aStr.str();
130       SetInput(anInput.c_str());
131       return Superclass::RenderOpaqueGeometry(theViewport);
132     }
133   }
134   return 1;
135 }
136
137 //----------------------------------------------------------------------------
138 vtkStandardNewMacro(VVTK_Renderer);
139
140 //----------------------------------------------------------------------------
141 VVTK_Renderer
142 ::VVTK_Renderer():
143   myFPSActor(VISU_FPSActor::New()),
144   myInsideCursorSettings(NULL),
145   myPickingSettings(NULL),
146   myGaussPointPicker(vtkPointPicker::New()),
147   myGaussPreHighlightProperty(vtkProperty::New()),
148   myGaussHighlightProperty(vtkProperty::New())
149 {
150   if(MYDEBUG) INFOS("VVTK_Renderer() - "<<this);
151
152   myFPSActor->Delete();
153
154   vtkTextMapper* aTextMapper = vtkTextMapper::New();
155   vtkTextProperty *aTextProperty = aTextMapper->GetTextProperty();
156   aTextProperty->SetJustificationToRight();
157   aTextProperty->SetVerticalJustificationToTop();
158   aTextProperty->SetFontSize(10);
159
160   myFPSActor->SetPickable(false); 
161   myFPSActor->ScaledTextOff();
162   myFPSActor->SetAlignmentPoint(8);
163   myFPSActor->SetPosition2 (1., 1.);
164   myFPSActor->SetMapper(aTextMapper);
165   aTextMapper->Delete();
166
167   GetDevice()->AddActor2D(myFPSActor.GetPointer());
168
169   myGaussPointPicker->Delete();
170
171   myGaussPreHighlightProperty->Delete();
172   myGaussPreHighlightProperty->SetColor(0,1,1);
173
174   myGaussHighlightProperty->Delete();
175   myGaussHighlightProperty->SetColor(1,1,0);
176
177 }
178
179 VVTK_Renderer
180 ::~VVTK_Renderer()
181 {
182   if(MYDEBUG) INFOS("~VVTK_Renderer() - "<<this);
183 }
184
185 //----------------------------------------------------------------------------
186 void
187 VVTK_Renderer
188 ::AddActor(VTKViewer_Actor* theActor)
189 {
190   Superclass::AddActor(theActor);
191   if(VISU_GaussPtsAct* anActor = dynamic_cast<VISU_GaussPtsAct*>(theActor)){
192     anActor->SetPointPicker(myGaussPointPicker.GetPointer());
193     anActor->SetPreHighlightProperty(myGaussPreHighlightProperty.GetPointer());
194     anActor->SetHighlightProperty(myGaussHighlightProperty.GetPointer());
195
196     anActor->SetInsideCursorSettings(myInsideCursorSettings);
197     anActor->SetPickingSettings(myPickingSettings);
198   }
199 }
200
201 //----------------------------------------------------------------------------
202 void
203 VVTK_Renderer
204 ::RemoveActor(VTKViewer_Actor* theActor)
205 {
206   Superclass::RemoveActor(theActor);
207   if(VISU_GaussPtsAct* anActor = dynamic_cast<VISU_GaussPtsAct*>(theActor)){
208     anActor->SetPointPicker(NULL);
209     anActor->SetPreHighlightProperty(NULL);
210     anActor->SetHighlightProperty(NULL);
211
212     anActor->SetInsideCursorSettings(NULL);
213     anActor->SetPickingSettings(NULL);
214   }
215 }
216
217 //----------------------------------------------------------------------------
218 void
219 VVTK_Renderer
220 ::SetInsideCursorSettings(VISU_InsideCursorSettings* theInsideCursorSettings)
221 {
222   myInsideCursorSettings = theInsideCursorSettings;
223 }
224
225 //----------------------------------------------------------------------------
226 void
227 VVTK_Renderer
228 ::SetPickingSettings(VISU_PickingSettings* thePickingSettings)
229 {
230   myPickingSettings = thePickingSettings;
231 }
232
233
234 //----------------------------------------------------------------------------
235 vtkStandardNewMacro(VVTK_Renderer1);
236
237 //----------------------------------------------------------------------------
238 VVTK_Renderer1::VVTK_Renderer1():
239   //myImplicitFunctionWidget(VISU_ImplicitFunctionWidget::New()),
240   myWidgetCtrl(VISU_WidgetCtrl::New()),
241   myOutsideCursorSettings(NULL)
242 {
243   if(MYDEBUG) INFOS("VVTK_Renderer1() - "<<this);
244
245   //myImplicitFunctionWidget->SetPlaceFactor(1.1);
246   myWidgetCtrl->SetPlaceFactor(1.1);
247   //
248   VISU_PlanesWidget *pPlanesWidget=myWidgetCtrl->GetPlanesWidget();
249   pPlanesWidget->SetOutlineTranslation(false);
250   vtkProperty* aSelectedPlaneProperty = pPlanesWidget->GetSelectedPlaneProperty();
251   vtkProperty* aPlaneProperty = pPlanesWidget->GetPlaneProperty();
252   aPlaneProperty->SetOpacity(aSelectedPlaneProperty->GetOpacity()*1.5);
253   //
254   myWidgetCtrl->Delete();
255 }
256
257 VVTK_Renderer1
258 ::~VVTK_Renderer1()
259 {
260   if(MYDEBUG) INFOS("~VVTK_Renderer1() - "<<this);
261   myWidgetCtrl->SetInteractor(NULL);
262 }
263
264 //----------------------------------------------------------------------------
265 void
266 VVTK_Renderer1
267 ::AddActor(VTKViewer_Actor* theActor)
268 {
269   Superclass::AddActor(theActor);
270   if(VISU_GaussPtsAct1* anActor = dynamic_cast<VISU_GaussPtsAct1*>(theActor)){
271     anActor->SetWidgetCtrl(GetWidgetCtrl());
272     anActor->SetOutsideCursorSettings(myOutsideCursorSettings);
273     AdjustWidgetCtrl();
274   }
275 }
276
277 //----------------------------------------------------------------------------
278 void
279 VVTK_Renderer1
280 ::RemoveActor(VTKViewer_Actor* theActor)
281 {
282   Superclass::RemoveActor(theActor);
283   if(VISU_GaussPtsAct1* anActor = dynamic_cast<VISU_GaussPtsAct1*>(theActor)){
284     anActor->SetWidgetCtrl(NULL);
285     anActor->SetOutsideCursorSettings(NULL);
286     AdjustWidgetCtrl();
287   }
288 }
289 //----------------------------------------------------------------------------
290 void VVTK_Renderer1::AdjustWidgetCtrl()
291 {
292   if(SVTK_Renderer::OnAdjustActors()){
293     VISU_PlanesWidget *pPlanesWidget=myWidgetCtrl->GetPlanesWidget();
294     pPlanesWidget->InitialPlaceWidget(myBndBox);
295     pPlanesWidget->SetOrigin(0.5*(myBndBox[1] + myBndBox[0]),
296                              0.5*(myBndBox[3] + myBndBox[2]),
297                              0.5*(myBndBox[5] + myBndBox[4]));
298     //
299     VISU_SphereWidget *pSphereWidget=myWidgetCtrl->GetSphereWidget();
300     pSphereWidget->SetCenter(0.5*(myBndBox[1] + myBndBox[0]),
301                              0.5*(myBndBox[3] + myBndBox[2]),
302                              0.5*(myBndBox[5] + myBndBox[4]));
303     float dX, dXmin=1.e20;
304     for (int i=0; i<3; ++i) {
305       dX=myBndBox[2*i+1]-myBndBox[2*i];
306       if (dX<dXmin){
307         dXmin=dX;
308       }
309     }
310     pSphereWidget->SetRadius(dXmin);
311   }
312 }
313 //----------------------------------------------------------------------------
314 void 
315 VVTK_Renderer1
316 ::Initialize(vtkRenderWindowInteractor* theInteractor,
317              SVTK_Selector* theSelector)
318 {
319   SVTK_Renderer::Initialize(theInteractor,theSelector);
320   myWidgetCtrl->SetInteractor(theInteractor);
321 }
322
323 //----------------------------------------------------------------------------
324 void
325 VVTK_Renderer1
326 ::SetOutsideCursorSettings(VISU_OutsideCursorSettings* theOutsideCursorSettings)
327 {
328   myOutsideCursorSettings = theOutsideCursorSettings;
329 }
330
331 //----------------------------------------------------------------------------
332 VISU_WidgetCtrl* VVTK_Renderer1::GetWidgetCtrl()
333 {
334   return myWidgetCtrl.GetPointer();
335 }
336
337 //----------------------------------------------------------------------------
338 bool
339 VVTK_Renderer1
340 ::OnAdjustActors()
341 {
342   return SVTK_Renderer::OnAdjustActors();
343 }
344
345
346 //----------------------------------------------------------------------------
347 vtkStandardNewMacro(VVTK_Renderer2);
348
349 //----------------------------------------------------------------------------
350 VVTK_Renderer2
351 ::VVTK_Renderer2():
352   myEventCallbackCommand(vtkCallbackCommand::New())
353 {
354   if(MYDEBUG) INFOS("VVTK_Renderer2() - "<<this);
355   myEventCallbackCommand->Delete();
356
357   myPriority = 0.0;
358   myEventCallbackCommand->SetClientData(this); 
359   myEventCallbackCommand->SetCallback(VVTK_Renderer2::ProcessEvents);
360 }
361
362 VVTK_Renderer2
363 ::~VVTK_Renderer2()
364 {
365   if(MYDEBUG) INFOS("~VVTK_Renderer2() - "<<this);
366 }
367
368 //----------------------------------------------------------------------------
369 void VVTK_Renderer2::SetWidgetCtrl(VISU_WidgetCtrl* theWidgetCtrl)
370 {
371   theWidgetCtrl->AddObserver(vtkCommand::EndInteractionEvent, 
372                              myEventCallbackCommand.GetPointer(), 
373                              myPriority);
374   theWidgetCtrl->AddObserver(vtkCommand::EnableEvent, 
375                              myEventCallbackCommand.GetPointer(), 
376                              myPriority);
377   theWidgetCtrl->AddObserver(vtkCommand::DisableEvent, 
378                              myEventCallbackCommand.GetPointer(), 
379                              myPriority);
380   myWidgetCtrl = theWidgetCtrl;
381 }
382
383 void 
384 VVTK_Renderer2
385 ::ProcessEvents(vtkObject* vtkNotUsed(theObject), 
386                 unsigned long theEvent,
387                 void* theClientData, 
388                 void* vtkNotUsed(theCallData))
389 {
390   VVTK_Renderer2* self = reinterpret_cast<VVTK_Renderer2*>(theClientData);
391
392   switch(theEvent){
393   case vtkCommand::EnableEvent:
394   case vtkCommand::EndInteractionEvent:
395     self->OnEndInteractionEvent();  
396     break;
397   }
398 }
399
400 void
401 VVTK_Renderer2
402 ::OnEndInteractionEvent()
403 {
404   AdjustActors();
405   myInteractor->Render();
406 }
407
408
409 //----------------------------------------------------------------------------
410 void VVTK_Renderer2::AddActor(VTKViewer_Actor* theActor)
411 {
412   if(VISU_GaussPtsAct1* anActor = dynamic_cast<VISU_GaussPtsAct1*>(theActor)){
413     if(VISU::TGaussPtsActorFactory* aFactory = anActor->GetGaussPtsFactory()){
414       if(VISU_GaussPtsAct2* anActor2 = aFactory->CloneActor(anActor)){
415         anActor2->SetWidgetCtrl(myWidgetCtrl);
416         Superclass::AddActor(anActor2);
417       }
418     }
419   }
420 }
421
422 //----------------------------------------------------------------------------
423 void
424 VVTK_Renderer2
425 ::RemoveActor(VTKViewer_Actor* theActor)
426 {
427   using namespace VISU;  
428   if(VISU_GaussPtsAct2* anActor = dynamic_cast<VISU_GaussPtsAct2*>(theActor)){
429     anActor->SetWidgetCtrl(NULL);
430     Superclass::RemoveActor(theActor);
431   }
432 }