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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
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<<"; 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 //myImplicitFunctionWidget(VISU_ImplicitFunctionWidget::New()),
240 myWidgetCtrl(VISU_WidgetCtrl::New()),
241 myOutsideCursorSettings(NULL)
243 if(MYDEBUG) INFOS("VVTK_Renderer1() - "<<this);
245 //myImplicitFunctionWidget->SetPlaceFactor(1.1);
246 myWidgetCtrl->SetPlaceFactor(1.1);
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);
254 myWidgetCtrl->Delete();
260 if(MYDEBUG) INFOS("~VVTK_Renderer1() - "<<this);
261 myWidgetCtrl->SetInteractor(NULL);
264 //----------------------------------------------------------------------------
267 ::AddActor(VTKViewer_Actor* theActor)
269 Superclass::AddActor(theActor);
270 if(VISU_GaussPtsAct1* anActor = dynamic_cast<VISU_GaussPtsAct1*>(theActor)){
271 anActor->SetWidgetCtrl(GetWidgetCtrl());
272 anActor->SetOutsideCursorSettings(myOutsideCursorSettings);
277 //----------------------------------------------------------------------------
280 ::RemoveActor(VTKViewer_Actor* theActor)
282 Superclass::RemoveActor(theActor);
283 if(VISU_GaussPtsAct1* anActor = dynamic_cast<VISU_GaussPtsAct1*>(theActor)){
284 anActor->SetWidgetCtrl(NULL);
285 anActor->SetOutsideCursorSettings(NULL);
289 //----------------------------------------------------------------------------
290 void VVTK_Renderer1::AdjustWidgetCtrl()
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]));
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];
310 pSphereWidget->SetRadius(dXmin);
313 //----------------------------------------------------------------------------
316 ::Initialize(vtkRenderWindowInteractor* theInteractor,
317 SVTK_Selector* theSelector)
319 SVTK_Renderer::Initialize(theInteractor,theSelector);
320 myWidgetCtrl->SetInteractor(theInteractor);
323 //----------------------------------------------------------------------------
326 ::SetOutsideCursorSettings(VISU_OutsideCursorSettings* theOutsideCursorSettings)
328 myOutsideCursorSettings = theOutsideCursorSettings;
331 //----------------------------------------------------------------------------
332 VISU_WidgetCtrl* VVTK_Renderer1::GetWidgetCtrl()
334 return myWidgetCtrl.GetPointer();
337 //----------------------------------------------------------------------------
342 return SVTK_Renderer::OnAdjustActors();
346 //----------------------------------------------------------------------------
347 vtkStandardNewMacro(VVTK_Renderer2);
349 //----------------------------------------------------------------------------
352 myEventCallbackCommand(vtkCallbackCommand::New())
354 if(MYDEBUG) INFOS("VVTK_Renderer2() - "<<this);
355 myEventCallbackCommand->Delete();
358 myEventCallbackCommand->SetClientData(this);
359 myEventCallbackCommand->SetCallback(VVTK_Renderer2::ProcessEvents);
365 if(MYDEBUG) INFOS("~VVTK_Renderer2() - "<<this);
368 //----------------------------------------------------------------------------
369 void VVTK_Renderer2::SetWidgetCtrl(VISU_WidgetCtrl* theWidgetCtrl)
371 theWidgetCtrl->AddObserver(vtkCommand::EndInteractionEvent,
372 myEventCallbackCommand.GetPointer(),
374 theWidgetCtrl->AddObserver(vtkCommand::EnableEvent,
375 myEventCallbackCommand.GetPointer(),
377 theWidgetCtrl->AddObserver(vtkCommand::DisableEvent,
378 myEventCallbackCommand.GetPointer(),
380 myWidgetCtrl = theWidgetCtrl;
385 ::ProcessEvents(vtkObject* vtkNotUsed(theObject),
386 unsigned long theEvent,
388 void* vtkNotUsed(theCallData))
390 VVTK_Renderer2* self = reinterpret_cast<VVTK_Renderer2*>(theClientData);
393 case vtkCommand::EnableEvent:
394 case vtkCommand::EndInteractionEvent:
395 self->OnEndInteractionEvent();
402 ::OnEndInteractionEvent()
405 myInteractor->Render();
409 //----------------------------------------------------------------------------
410 void VVTK_Renderer2::AddActor(VTKViewer_Actor* theActor)
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);
422 //----------------------------------------------------------------------------
425 ::RemoveActor(VTKViewer_Actor* theActor)
427 using namespace VISU;
428 if(VISU_GaussPtsAct2* anActor = dynamic_cast<VISU_GaussPtsAct2*>(theActor)){
429 anActor->SetWidgetCtrl(NULL);
430 Superclass::RemoveActor(theActor);