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 "SVTK_Renderer.h"
31 #include "SVTK_Trihedron.h"
32 #include "SVTK_CubeAxesActor2D.h"
33 #include "SVTK_RectPicker.h"
35 #include "SALOME_Actor.h"
36 #include "VTKViewer_Actor.h"
37 #include "VTKViewer_Transform.h"
38 #include "VTKViewer_Utilities.h"
40 #include <vtkCamera.h>
41 #include <vtkRenderer.h>
42 #include <vtkTextProperty.h>
43 #include <vtkObjectFactory.h>
44 #include <vtkCallbackCommand.h>
46 #include <vtkPicker.h>
47 #include <vtkPointPicker.h>
48 #include <vtkCellPicker.h>
50 #include <vtkProperty.h>
52 // undefining min and max because CASCADE's defines them and
53 // it clashes with std::min(), std::max() included in utilities.h
58 vtkStandardNewMacro(SVTK_Renderer);
65 myDevice(vtkRenderer::New()),
68 myEventCallbackCommand(vtkCallbackCommand::New()),
69 myPointPicker(vtkPointPicker::New()),
70 myCellPicker(vtkCellPicker::New()),
71 myPointRectPicker(SVTK_RectPicker::New()),
72 myCellRectPicker(SVTK_RectPicker::New()),
73 myPreHighlightProperty(vtkProperty::New()),
74 myHighlightProperty(vtkProperty::New()),
75 myTransform(VTKViewer_Transform::New()),
76 myCubeAxes(SVTK_CubeAxesActor2D::New()),
77 myTrihedron(SVTK_Trihedron::New()),
79 myIsTrihedronRelative(true)
82 myTransform->Delete();
84 SetSelectionTolerance();
86 myPointPicker->Delete();
87 myCellPicker->Delete();
89 myPointRectPicker->Delete();
90 myPointRectPicker->PickFromListOn();
92 myCellRectPicker->Delete();
93 myCellRectPicker->PickFromListOn();
94 myCellRectPicker->PickPointsOff();
96 //SetPreselectionProp();
97 myPreHighlightProperty->Delete();
98 myPreHighlightProperty->SetColor(0,1,1);
99 myPreHighlightProperty->SetPointSize(SALOME_POINT_SIZE+2);
100 myPreHighlightProperty->SetLineWidth(SALOME_LINE_WIDTH+2);
101 myPreHighlightProperty->SetRepresentationToPoints();
103 //SetSelectionProp();
104 myHighlightProperty->Delete();
105 myHighlightProperty->SetColor(1,1,0);
106 myHighlightProperty->SetPointSize(SALOME_POINT_SIZE+2);
107 myHighlightProperty->SetLineWidth(SALOME_LINE_WIDTH+2);
108 myHighlightProperty->SetRepresentationToPoints();
110 myTrihedron->Delete();
111 myCubeAxes->Delete();
112 myEventCallbackCommand->Delete();
114 myTrihedron->AddToRender(GetDevice());
115 GetDevice()->AddProp(GetCubeAxes());
117 myBndBox[0] = myBndBox[2] = myBndBox[4] = 0;
118 myBndBox[1] = myBndBox[3] = myBndBox[5] = myTrihedron->GetSize();
120 myCubeAxes->SetBounds(myBndBox);
121 myCubeAxes->SetCamera(GetDevice()->GetActiveCamera());
123 myCubeAxes->SetLabelFormat("%6.4g");
124 myCubeAxes->SetFlyModeToOuterEdges(); // ENK remarks: it must bee
125 myCubeAxes->SetFontFactor(0.8);
126 myCubeAxes->SetCornerOffset(0);
127 myCubeAxes->SetScaling(0);
128 myCubeAxes->SetNumberOfLabels(5);
129 myCubeAxes->VisibilityOff();
130 myCubeAxes->SetTransform(GetTransform());
132 vtkTextProperty* aTextProp = vtkTextProperty::New();
133 aTextProp->SetColor(1, 1, 1);
134 aTextProp->ShadowOn();
135 myCubeAxes->SetAxisTitleTextProperty(aTextProp);
136 myCubeAxes->SetAxisLabelTextProperty(aTextProp);
139 GetDevice()->GetActiveCamera()->ParallelProjectionOn();
140 GetDevice()->LightFollowCameraOn();
141 GetDevice()->TwoSidedLightingOn();
143 myEventCallbackCommand->SetClientData(this);
144 myEventCallbackCommand->SetCallback(SVTK_Renderer::ProcessEvents);
145 GetDevice()->AddObserver(vtkCommand::ConfigureEvent,
146 myEventCallbackCommand.GetPointer(),
148 GetDevice()->AddObserver(vtkCommand::ResetCameraEvent,
149 myEventCallbackCommand.GetPointer(),
151 GetDevice()->AddObserver(vtkCommand::ResetCameraClippingRangeEvent,
152 myEventCallbackCommand.GetPointer(),
162 vtkActorCollection* anActors = GetDevice()->GetActors();
163 vtkActorCollection* anActors2 = vtkActorCollection::New();
165 anActors->InitTraversal();
166 while(vtkActor* anAct = anActors->GetNextActor()){
167 if(SALOME_Actor* anActor = dynamic_cast<SALOME_Actor*>(anAct)){
168 anActors2->AddItem(anActor);
172 anActors2->InitTraversal();
173 while(vtkActor* anAct = anActors2->GetNextActor()){
174 if(SALOME_Actor* anActor = dynamic_cast<SALOME_Actor*>(anAct)){
175 RemoveActor(anActor);
184 Main process event method
188 ::ProcessEvents(vtkObject* vtkNotUsed(theObject),
189 unsigned long theEvent,
191 void* vtkNotUsed(theCallData))
193 SVTK_Renderer* self = reinterpret_cast<SVTK_Renderer*>(theClientData);
196 case vtkCommand::ConfigureEvent:
199 case vtkCommand::ResetCameraEvent:
202 case vtkCommand::ResetCameraClippingRangeEvent:
203 self->OnResetClippingRange();
209 \return renderer's device
215 return myDevice.GetPointer();
223 ::Initialize(vtkRenderWindowInteractor* theInteractor,
224 SVTK_Selector* theSelector)
226 myInteractor = theInteractor;
227 mySelector = theSelector;
231 Publishes pointed actor into the renderer
235 ::AddActor(VTKViewer_Actor* theActor)
237 if(SALOME_Actor* anActor = dynamic_cast<SALOME_Actor*>(theActor)){
238 anActor->SetInteractor(myInteractor);
239 anActor->SetTransform(GetTransform());
240 anActor->SetSelector(mySelector.GetPointer());
242 anActor->SetPointPicker(myPointPicker.GetPointer());
243 anActor->SetCellPicker(myCellPicker.GetPointer());
245 anActor->SetPointRectPicker(myPointRectPicker.GetPointer());
246 anActor->SetCellRectPicker(myCellRectPicker.GetPointer());
248 anActor->SetPreHighlightProperty(myPreHighlightProperty.GetPointer());
249 anActor->SetHighlightProperty(myHighlightProperty.GetPointer());
251 anActor->AddToRender(GetDevice());
257 Removes pointed actor from the renderer
261 ::RemoveActor(VTKViewer_Actor* theActor)
263 if(SALOME_Actor* anActor = dynamic_cast<SALOME_Actor*>(theActor)){
264 // Order of the calls are important because VTKViewer_Actor::RemoveFromRender
265 // can leads do destruction of the actor
266 anActor->SetInteractor(NULL);
267 anActor->SetTransform(NULL);
268 anActor->SetSelector(NULL);
270 anActor->SetPointPicker(NULL);
271 anActor->SetCellPicker(NULL);
273 anActor->SetPointRectPicker(NULL);
274 anActor->SetCellRectPicker(NULL);
276 anActor->SetPreHighlightProperty(NULL);
277 anActor->SetHighlightProperty(NULL);
279 anActor->RemoveFromRender(GetDevice());
285 Get special container that keeps scaling of the scene
291 return myTransform.GetPointer();
295 Allows to get a scale that is applied on the whole scene
299 ::GetScale( double theScale[3] )
301 myTransform->GetMatrixScale( theScale );
305 Allows to apply a scale on the whole scene
309 ::SetScale( double theScale[3] )
311 myTransform->SetMatrixScale( theScale[0], theScale[1], theScale[2] );
316 Applies color and size (PointSize and LineWidth) of primitives in selection mode
320 ::SetSelectionProp(const double& theRed,
321 const double& theGreen,
322 const double& theBlue,
325 myHighlightProperty->SetColor( theRed, theGreen, theBlue );
326 myHighlightProperty->SetLineWidth( theWidth );
327 myHighlightProperty->SetPointSize( theWidth );
331 Applies color and size (PointSize and LineWidth) of primitives in preselection mode
335 ::SetPreselectionProp(const double& theRed,
336 const double& theGreen,
337 const double& theBlue,
340 myPreHighlightProperty->SetColor( theRed, theGreen, theBlue );
341 myPreHighlightProperty->SetLineWidth( theWidth );
342 myPreHighlightProperty->SetPointSize( theWidth );
346 Setup requested tolerance for the picking
350 ::SetSelectionTolerance(const double& theTolNodes,
351 const double& theTolCell)
353 myPointPicker->SetTolerance( theTolNodes );
354 myCellPicker->SetTolerance( theTolCell );
356 myPointRectPicker->SetTolerance( theTolNodes );
357 myCellRectPicker->SetTolerance( theTolCell );
361 /*! If parameter theIsForcedUpdate is true, recalculate parameters for
362 * trihedron and cube axes, even if trihedron and cube axes is invisible.
367 CheckBndBox(const vtkFloatingPointType theBounds[6])
369 if(theBounds[0] > -VTK_LARGE_FLOAT && theBounds[1] < VTK_LARGE_FLOAT &&
370 theBounds[2] > -VTK_LARGE_FLOAT && theBounds[3] < VTK_LARGE_FLOAT &&
371 theBounds[4] > -VTK_LARGE_FLOAT && theBounds[5] < VTK_LARGE_FLOAT)
377 Adjusts size of actors
383 bool aTDisplayed = IsTrihedronDisplayed();
384 bool aCDisplayed = IsCubeAxesDisplayed();
386 vtkFloatingPointType aNewBndBox[6];
387 aNewBndBox[ 0 ] = aNewBndBox[ 2 ] = aNewBndBox[ 4 ] = VTK_LARGE_FLOAT;
388 aNewBndBox[ 1 ] = aNewBndBox[ 3 ] = aNewBndBox[ 5 ] = -VTK_LARGE_FLOAT;
390 int aVisibleNum = myTrihedron->GetVisibleActorCount(GetDevice());
393 myTrihedron->VisibilityOff();
396 myCubeAxes->VisibilityOff();
398 // if the new trihedron size have sufficient difference, then apply the value
399 vtkFloatingPointType aSize = myTrihedron->GetSize();
400 if ( IsTrihedronRelative() )
402 ComputeTrihedronSize(GetDevice(),aSize,aSize,myTrihedronSize);
403 myTrihedron->SetSize(aSize);
406 myTrihedron->SetSize( myTrihedronSize );
408 // iterate through displayed objects and set size if necessary
409 vtkActorCollection* anActors = GetDevice()->GetActors();
410 anActors->InitTraversal();
411 while(vtkActor* anAct = anActors->GetNextActor()){
412 if(SALOME_Actor* anActor = dynamic_cast<SALOME_Actor*>(anAct)){
413 if(anActor->IsResizable())
414 anActor->SetSize(0.5*aSize);
415 if(anActor->GetVisibility() && !anActor->IsInfinitive()){
416 vtkFloatingPointType *aBounds = anActor->GetBounds();
417 if(CheckBndBox(aBounds))
418 for(int i = 0; i < 5; i = i + 2){
419 if(aBounds[i] < aNewBndBox[i])
420 aNewBndBox[i] = aBounds[i];
421 if(aBounds[i+1] > aNewBndBox[i+1])
422 aNewBndBox[i+1] = aBounds[i+1];
429 myTrihedron->VisibilityOn();
432 myCubeAxes->VisibilityOn();
435 double aSize = myTrihedron->GetSize();
436 aNewBndBox[0] = aNewBndBox[2] = aNewBndBox[4] = 0;
437 aNewBndBox[1] = aNewBndBox[3] = aNewBndBox[5] = aSize;
440 if(CheckBndBox(aNewBndBox)){
441 for(int i = 0; i < 6; i++)
442 myBndBox[i] = aNewBndBox[i];
443 myCubeAxes->SetBounds(myBndBox);
451 Adjusts size of actors
458 ::ResetCameraClippingRange(GetDevice());
462 Set size of the trihedron
463 \param theSize - new size
464 \param theRelative - if it is true, then size is mesured in percents from bounding box of the scene,
465 otherwise - in viewer units
469 ::SetTrihedronSize(int theSize, const bool theRelative)
471 if(myTrihedronSize != theSize || myIsTrihedronRelative != theRelative){
472 myTrihedronSize = theSize;
473 myIsTrihedronRelative = theRelative;
479 \return size of the trihedron in percents from bounding box of the scene
483 ::GetTrihedronSize() const
485 return myTrihedronSize;
489 \return true if the size of the trihedron is relative
493 ::IsTrihedronRelative() const
495 return myIsTrihedronRelative;
499 \return trihedron control
505 return myTrihedron.GetPointer();
509 \return true if trihedron is displayed
513 ::IsTrihedronDisplayed()
515 return myTrihedron->GetVisibility() == VTKViewer_Trihedron::eOn;
519 Toggle trihedron visibility
525 if(IsTrihedronDisplayed())
526 myTrihedron->VisibilityOff();
528 myTrihedron->VisibilityOn();
532 Adjust size of the trihedron to the bounding box of the scene
536 ::OnAdjustTrihedron()
542 \return graduated rules control
544 SVTK_CubeAxesActor2D*
548 return myCubeAxes.GetPointer();
552 \return true if graduated rules displayed
556 ::IsCubeAxesDisplayed()
558 return myCubeAxes->GetVisibility() == 1;
562 Toggle graduated rules visibility
568 if(IsCubeAxesDisplayed())
569 myCubeAxes->VisibilityOff();
571 myCubeAxes->VisibilityOn();
575 Adjust size of the graduated rules to the bounding box of the scene
585 Sets camera into predefined state
591 int aTrihedronIsVisible = IsTrihedronDisplayed();
592 int aCubeAxesIsVisible = IsCubeAxesDisplayed();
594 myTrihedron->SetVisibility( VTKViewer_Trihedron::eOnlyLineOn );
595 myCubeAxes->SetVisibility(0);
597 ::ResetCamera(GetDevice(),true);
598 vtkCamera* aCamera = GetDevice()->GetActiveCamera();
599 aCamera->SetPosition(1,-1,1);
600 aCamera->SetViewUp(0,0,1);
601 ::ResetCamera(GetDevice(),true);
603 if(aTrihedronIsVisible)
604 myTrihedron->VisibilityOn();
606 myTrihedron->VisibilityOff();
608 if(aCubeAxesIsVisible)
609 myCubeAxes->VisibilityOn();
611 myCubeAxes->VisibilityOff();
613 static vtkFloatingPointType aCoeff = 3.0;
614 aCamera->SetParallelScale(aCoeff*aCamera->GetParallelScale());
618 Fit all presentation in the scene into the window
624 int aTrihedronWasVisible = false;
625 int aCubeAxesWasVisible = false;
627 aTrihedronWasVisible = IsTrihedronDisplayed();
628 if(aTrihedronWasVisible)
629 myTrihedron->VisibilityOff();
631 aCubeAxesWasVisible = IsCubeAxesDisplayed();
632 if(aCubeAxesWasVisible)
633 myCubeAxes->VisibilityOff();
635 if(myTrihedron->GetVisibleActorCount(GetDevice())){
636 myTrihedron->VisibilityOff();
637 myCubeAxes->VisibilityOff();
638 ::ResetCamera(GetDevice());
640 myTrihedron->SetVisibility(VTKViewer_Trihedron::eOnlyLineOn);
641 myCubeAxes->SetVisibility(2);
642 ::ResetCamera(GetDevice(),true);
645 if(aTrihedronWasVisible)
646 myTrihedron->VisibilityOn();
648 myTrihedron->VisibilityOff();
650 if(aCubeAxesWasVisible)
651 myCubeAxes->VisibilityOn();
653 myCubeAxes->VisibilityOff();
655 ::ResetCameraClippingRange(GetDevice());
659 Reset camera clipping range to adjust the range to the bounding box of the scene
663 ::OnResetClippingRange()
666 ::ResetCameraClippingRange(GetDevice());
670 To reset direction of the camera to front view
676 vtkCamera* aCamera = GetDevice()->GetActiveCamera();
677 aCamera->SetPosition(1,0,0);
678 aCamera->SetViewUp(0,0,1);
679 aCamera->SetFocalPoint(0,0,0);
684 To reset direction of the camera to back view
690 vtkCamera* aCamera = GetDevice()->GetActiveCamera();
691 aCamera->SetPosition(-1,0,0);
692 aCamera->SetViewUp(0,0,1);
693 aCamera->SetFocalPoint(0,0,0);
698 To reset direction of the camera to top view
704 vtkCamera* aCamera = GetDevice()->GetActiveCamera();
705 aCamera->SetPosition(0,0,1);
706 aCamera->SetViewUp(0,1,0);
707 aCamera->SetFocalPoint(0,0,0);
712 To reset direction of the camera to bottom view
718 vtkCamera* aCamera = GetDevice()->GetActiveCamera();
719 aCamera->SetPosition(0,0,-1);
720 aCamera->SetViewUp(0,1,0);
721 aCamera->SetFocalPoint(0,0,0);
726 To reset direction of the camera to left view
732 vtkCamera* aCamera = GetDevice()->GetActiveCamera();
733 aCamera->SetPosition(0,-1,0);
734 aCamera->SetViewUp(0,0,1);
735 aCamera->SetFocalPoint(0,0,0);
740 To reset direction of the camera to right view
746 vtkCamera* aCamera = GetDevice()->GetActiveCamera();
747 aCamera->SetPosition(0,1,0);
748 aCamera->SetViewUp(0,0,1);
749 aCamera->SetFocalPoint(0,0,0);