1 // SALOME VTKViewer : build VTK viewer into Salome desktop
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
29 #include "VVTK_Renderer.h"
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"
37 #include <vtkObjectFactory.h>
38 #include <vtkProperty.h>
39 #include <vtkPointPicker.h>
41 #include <vtkRenderWindowInteractor.h>
42 #include <vtkCallbackCommand.h>
43 #include <vtkCommand.h>
46 #include <vtkPropCollection.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>
57 #include <vtkPropCollection.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>
68 #include "utilities.h"
71 static int MYDEBUG = 0;
73 static int MYDEBUG = 0;
76 //======================================================================
77 class VISU_FPSActor : public vtkTextActor
80 vtkTypeMacro( VISU_FPSActor, vtkTextActor);
87 RenderOpaqueGeometry(vtkViewport *theViewport);
90 //======================================================================
91 vtkStandardNewMacro(VISU_FPSActor);
93 //======================================================================
94 // function: RenderOpaqueGeometry
96 //======================================================================
99 ::RenderOpaqueGeometry(vtkViewport *theViewport)
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 )
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();
126 std::ostringstream aStr;
127 float aFPS = 1.0 / aLastRenderTimeInSeconds;
128 aStr<<"FPS: "<<aFPS<<"\n NumberOfCells: "<<aNumberOfCells;
129 std::string anInput = aStr.str();
130 SetInput(anInput.c_str());
131 return Superclass::RenderOpaqueGeometry(theViewport);
137 //----------------------------------------------------------------------------
138 vtkStandardNewMacro(VVTK_Renderer);
140 //----------------------------------------------------------------------------
143 myFPSActor(VISU_FPSActor::New()),
144 myInsideCursorSettings(NULL),
145 myPickingSettings(NULL),
146 myGaussPointPicker(vtkPointPicker::New()),
147 myGaussPreHighlightProperty(vtkProperty::New()),
148 myGaussHighlightProperty(vtkProperty::New())
150 if(MYDEBUG) INFOS("VVTK_Renderer() - "<<this);
152 myFPSActor->Delete();
154 vtkTextMapper* aTextMapper = vtkTextMapper::New();
155 vtkTextProperty *aTextProperty = aTextMapper->GetTextProperty();
156 aTextProperty->SetJustificationToRight();
157 aTextProperty->SetVerticalJustificationToTop();
158 aTextProperty->SetFontSize(10);
160 myFPSActor->SetPickable(false);
161 myFPSActor->ScaledTextOff();
162 myFPSActor->SetAlignmentPoint(8);
163 myFPSActor->SetPosition2 (1., 1.);
164 myFPSActor->SetMapper(aTextMapper);
165 aTextMapper->Delete();
167 //GetDevice()->AddActor2D(myFPSActor.GetPointer());
169 myGaussPointPicker->Delete();
171 myGaussPreHighlightProperty->Delete();
172 myGaussPreHighlightProperty->SetColor(0,1,1);
174 myGaussHighlightProperty->Delete();
175 myGaussHighlightProperty->SetColor(1,1,0);
182 if(MYDEBUG) INFOS("~VVTK_Renderer() - "<<this);
185 //----------------------------------------------------------------------------
188 ::AddActor(VTKViewer_Actor* theActor)
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());
196 anActor->SetInsideCursorSettings(myInsideCursorSettings);
197 anActor->SetPickingSettings(myPickingSettings);
201 //----------------------------------------------------------------------------
204 ::RemoveActor(VTKViewer_Actor* theActor)
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);
212 anActor->SetInsideCursorSettings(NULL);
213 anActor->SetPickingSettings(NULL);
217 //----------------------------------------------------------------------------
220 ::SetInsideCursorSettings(VISU_InsideCursorSettings* theInsideCursorSettings)
222 myInsideCursorSettings = theInsideCursorSettings;
225 //----------------------------------------------------------------------------
228 ::SetPickingSettings(VISU_PickingSettings* thePickingSettings)
230 myPickingSettings = thePickingSettings;
234 //----------------------------------------------------------------------------
235 vtkStandardNewMacro(VVTK_Renderer1);
237 //----------------------------------------------------------------------------
238 VVTK_Renderer1::VVTK_Renderer1():
239 myWidgetCtrl(VISU_WidgetCtrl::New()),
240 myOutsideCursorSettings(NULL)
242 if(MYDEBUG) INFOS("VVTK_Renderer1() - "<<this);
244 myWidgetCtrl->SetPlaceFactor(1.1);
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);
252 myWidgetCtrl->Delete();
258 if(MYDEBUG) INFOS("~VVTK_Renderer1() - "<<this);
259 myWidgetCtrl->SetInteractor(NULL);
262 //----------------------------------------------------------------------------
265 ::AddActor(VTKViewer_Actor* theActor)
267 Superclass::AddActor(theActor);
268 if(VISU_GaussPtsAct1* anActor = dynamic_cast<VISU_GaussPtsAct1*>(theActor)){
269 anActor->SetWidgetCtrl(GetWidgetCtrl());
270 anActor->SetOutsideCursorSettings(myOutsideCursorSettings);
276 //----------------------------------------------------------------------------
279 ::RemoveActor(VTKViewer_Actor* theActor)
281 Superclass::RemoveActor(theActor);
282 if(VISU_GaussPtsAct1* anActor = dynamic_cast<VISU_GaussPtsAct1*>(theActor)){
283 anActor->SetWidgetCtrl(NULL);
284 anActor->SetOutsideCursorSettings(NULL);
290 //----------------------------------------------------------------------------
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]));
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]));
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);
311 aSphereWidget->SetRadius(aMinLength);
314 //----------------------------------------------------------------------------
317 ::Initialize(vtkRenderWindowInteractor* theInteractor,
318 SVTK_Selector* theSelector)
320 SVTK_Renderer::Initialize(theInteractor,theSelector);
321 myWidgetCtrl->SetInteractor(theInteractor);
324 //----------------------------------------------------------------------------
327 ::SetOutsideCursorSettings(VISU_OutsideCursorSettings* theOutsideCursorSettings)
329 myOutsideCursorSettings = theOutsideCursorSettings;
332 //----------------------------------------------------------------------------
337 return myWidgetCtrl.GetPointer();
340 //----------------------------------------------------------------------------
345 return SVTK_Renderer::OnAdjustActors();
349 //----------------------------------------------------------------------------
350 vtkStandardNewMacro(VVTK_Renderer2);
352 //----------------------------------------------------------------------------
355 myEventCallbackCommand(vtkCallbackCommand::New())
357 if(MYDEBUG) INFOS("VVTK_Renderer2() - "<<this);
358 myEventCallbackCommand->Delete();
361 myEventCallbackCommand->SetClientData(this);
362 myEventCallbackCommand->SetCallback(VVTK_Renderer2::ProcessEvents);
368 if(MYDEBUG) INFOS("~VVTK_Renderer2() - "<<this);
371 //----------------------------------------------------------------------------
372 void VVTK_Renderer2::SetWidgetCtrl(VISU_WidgetCtrl* theWidgetCtrl)
374 theWidgetCtrl->AddObserver(vtkCommand::EndInteractionEvent,
375 myEventCallbackCommand.GetPointer(),
377 theWidgetCtrl->AddObserver(vtkCommand::EnableEvent,
378 myEventCallbackCommand.GetPointer(),
380 theWidgetCtrl->AddObserver(vtkCommand::DisableEvent,
381 myEventCallbackCommand.GetPointer(),
383 myWidgetCtrl = theWidgetCtrl;
388 ::ProcessEvents(vtkObject* vtkNotUsed(theObject),
389 unsigned long theEvent,
391 void* vtkNotUsed(theCallData))
393 VVTK_Renderer2* self = reinterpret_cast<VVTK_Renderer2*>(theClientData);
396 case vtkCommand::EnableEvent:
397 case vtkCommand::EndInteractionEvent:
398 self->OnEndInteractionEvent();
405 ::OnEndInteractionEvent()
408 myInteractor->Render();
412 //----------------------------------------------------------------------------
413 void VVTK_Renderer2::AddActor(VTKViewer_Actor* theActor)
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);
425 //----------------------------------------------------------------------------
428 ::RemoveActor(VTKViewer_Actor* theActor)
430 using namespace VISU;
431 if(VISU_GaussPtsAct2* anActor = dynamic_cast<VISU_GaussPtsAct2*>(theActor)){
432 anActor->SetWidgetCtrl(NULL);
433 Superclass::RemoveActor(theActor);