Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[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   myWidgetCtrl(VISU_WidgetCtrl::New()),
240   myOutsideCursorSettings(NULL)
241 {
242   if(MYDEBUG) INFOS("VVTK_Renderer1() - "<<this);
243
244   myWidgetCtrl->SetPlaceFactor(1.1);
245   //
246   VISU_PlanesWidget *aPlanesWidget = myWidgetCtrl->GetPlanesWidget();
247   aPlanesWidget->SetOutlineTranslation(false);
248   vtkProperty* aSelectedPlaneProperty = aPlanesWidget->GetSelectedPlaneProperty();
249   vtkProperty* aPlaneProperty = aPlanesWidget->GetPlaneProperty();
250   aPlaneProperty->SetOpacity(aSelectedPlaneProperty->GetOpacity()*1.5);
251   //
252   myWidgetCtrl->Delete();
253 }
254
255 VVTK_Renderer1
256 ::~VVTK_Renderer1()
257 {
258   if(MYDEBUG) INFOS("~VVTK_Renderer1() - "<<this);
259   myWidgetCtrl->SetInteractor(NULL);
260 }
261
262 //----------------------------------------------------------------------------
263 void
264 VVTK_Renderer1
265 ::AddActor(VTKViewer_Actor* theActor)
266 {
267   Superclass::AddActor(theActor);
268   if(VISU_GaussPtsAct1* anActor = dynamic_cast<VISU_GaussPtsAct1*>(theActor)){
269     anActor->SetWidgetCtrl(GetWidgetCtrl());
270     anActor->SetOutsideCursorSettings(myOutsideCursorSettings);
271     AdjustWidgetCtrl();
272   }
273 }
274
275
276 //----------------------------------------------------------------------------
277 void
278 VVTK_Renderer1
279 ::RemoveActor(VTKViewer_Actor* theActor)
280 {
281   Superclass::RemoveActor(theActor);
282   if(VISU_GaussPtsAct1* anActor = dynamic_cast<VISU_GaussPtsAct1*>(theActor)){
283     anActor->SetWidgetCtrl(NULL);
284     anActor->SetOutsideCursorSettings(NULL);
285     AdjustWidgetCtrl();
286   }
287 }
288
289
290 //----------------------------------------------------------------------------
291 void
292 VVTK_Renderer1
293 ::AdjustWidgetCtrl()
294 {
295   VISU_PlanesWidget *aPlanesWidget = myWidgetCtrl->GetPlanesWidget();
296   aPlanesWidget->InitialPlaceWidget(myBndBox);
297   aPlanesWidget->SetOrigin(0.5*(myBndBox[1] + myBndBox[0]),
298                            0.5*(myBndBox[3] + myBndBox[2]),
299                            0.5*(myBndBox[5] + myBndBox[4]));
300   //
301   VISU_SphereWidget *aSphereWidget = myWidgetCtrl->GetSphereWidget();
302   aSphereWidget->SetCenter(0.5*(myBndBox[1] + myBndBox[0]),
303                            0.5*(myBndBox[3] + myBndBox[2]),
304                            0.5*(myBndBox[5] + myBndBox[4]));
305   
306   float aMinLength = VTK_LARGE_FLOAT;
307   for (int i=0; i<3; ++i) {
308     float aLength = myBndBox[2*i+1]-myBndBox[2*i];
309     aMinLength = std::min(aMinLength,aLength);
310   }
311   aSphereWidget->SetRadius(aMinLength);
312 }
313
314 //----------------------------------------------------------------------------
315 void 
316 VVTK_Renderer1
317 ::Initialize(vtkRenderWindowInteractor* theInteractor,
318              SVTK_Selector* theSelector)
319 {
320   SVTK_Renderer::Initialize(theInteractor,theSelector);
321   myWidgetCtrl->SetInteractor(theInteractor);
322 }
323
324 //----------------------------------------------------------------------------
325 void
326 VVTK_Renderer1
327 ::SetOutsideCursorSettings(VISU_OutsideCursorSettings* theOutsideCursorSettings)
328 {
329   myOutsideCursorSettings = theOutsideCursorSettings;
330 }
331
332 //----------------------------------------------------------------------------
333 VISU_WidgetCtrl* 
334 VVTK_Renderer1
335 ::GetWidgetCtrl()
336 {
337   return myWidgetCtrl.GetPointer();
338 }
339
340 //----------------------------------------------------------------------------
341 bool
342 VVTK_Renderer1
343 ::OnAdjustActors()
344 {
345   return SVTK_Renderer::OnAdjustActors();
346 }
347
348
349 //----------------------------------------------------------------------------
350 vtkStandardNewMacro(VVTK_Renderer2);
351
352 //----------------------------------------------------------------------------
353 VVTK_Renderer2
354 ::VVTK_Renderer2():
355   myEventCallbackCommand(vtkCallbackCommand::New())
356 {
357   if(MYDEBUG) INFOS("VVTK_Renderer2() - "<<this);
358   myEventCallbackCommand->Delete();
359
360   myPriority = 0.0;
361   myEventCallbackCommand->SetClientData(this); 
362   myEventCallbackCommand->SetCallback(VVTK_Renderer2::ProcessEvents);
363 }
364
365 VVTK_Renderer2
366 ::~VVTK_Renderer2()
367 {
368   if(MYDEBUG) INFOS("~VVTK_Renderer2() - "<<this);
369 }
370
371 //----------------------------------------------------------------------------
372 void VVTK_Renderer2::SetWidgetCtrl(VISU_WidgetCtrl* theWidgetCtrl)
373 {
374   theWidgetCtrl->AddObserver(vtkCommand::EndInteractionEvent, 
375                              myEventCallbackCommand.GetPointer(), 
376                              myPriority);
377   theWidgetCtrl->AddObserver(vtkCommand::EnableEvent, 
378                              myEventCallbackCommand.GetPointer(), 
379                              myPriority);
380   theWidgetCtrl->AddObserver(vtkCommand::DisableEvent, 
381                              myEventCallbackCommand.GetPointer(), 
382                              myPriority);
383   myWidgetCtrl = theWidgetCtrl;
384 }
385
386 void 
387 VVTK_Renderer2
388 ::ProcessEvents(vtkObject* vtkNotUsed(theObject), 
389                 unsigned long theEvent,
390                 void* theClientData, 
391                 void* vtkNotUsed(theCallData))
392 {
393   VVTK_Renderer2* self = reinterpret_cast<VVTK_Renderer2*>(theClientData);
394
395   switch(theEvent){
396   case vtkCommand::EnableEvent:
397   case vtkCommand::EndInteractionEvent:
398     self->OnEndInteractionEvent();  
399     break;
400   }
401 }
402
403 void
404 VVTK_Renderer2
405 ::OnEndInteractionEvent()
406 {
407   AdjustActors();
408   myInteractor->Render();
409 }
410
411
412 //----------------------------------------------------------------------------
413 void VVTK_Renderer2::AddActor(VTKViewer_Actor* theActor)
414 {
415   if(VISU_GaussPtsAct1* anActor = dynamic_cast<VISU_GaussPtsAct1*>(theActor)){
416     if(VISU::TGaussPtsActorFactory* aFactory = anActor->GetGaussPtsFactory()){
417       if(VISU_GaussPtsAct2* anActor2 = aFactory->CloneActor(anActor)){
418         anActor2->SetWidgetCtrl(myWidgetCtrl);
419         Superclass::AddActor(anActor2);
420       }
421     }
422   }
423 }
424
425 //----------------------------------------------------------------------------
426 void
427 VVTK_Renderer2
428 ::RemoveActor(VTKViewer_Actor* theActor)
429 {
430   using namespace VISU;  
431   if(VISU_GaussPtsAct2* anActor = dynamic_cast<VISU_GaussPtsAct2*>(theActor)){
432     anActor->SetWidgetCtrl(NULL);
433     Superclass::RemoveActor(theActor);
434   }
435 }