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
24 // File : VTKViewer_RenderWindowInteractor.cxx
25 // Author : Nicolas REJNERI
29 #include "VTKViewer_RenderWindowInteractor.h"
30 #include "VTKViewer_RenderWindow.h"
31 #include "VTKViewer_InteractorStyleSALOME.h"
33 #include "QAD_Application.h"
34 #include "QAD_Desktop.h"
36 //#include "SALOME_Selection.h"
37 #include "SALOME_Actor.h"
45 #include <vtkAssemblyNode.h>
47 #include <vtkInteractorStyle.h>
48 #include <vtkObjectFactory.h>
49 #include <vtkPicker.h>
50 #include <vtkCellPicker.h>
51 #include <vtkPointPicker.h>
52 #include <vtkUnstructuredGrid.h>
53 #include <vtkPolyDataMapper.h>
54 #include <vtkSphereSource.h>
55 #include <vtkDataSet.h>
56 #include <vtkMaskPoints.h>
57 #include <vtkVertex.h>
58 #include <vtkRendererCollection.h>
59 #include <vtkPolyDataWriter.h>
66 VTKViewer_RenderWindowInteractor* VTKViewer_RenderWindowInteractor::New() {
67 vtkObject *ret = vtkObjectFactory::CreateInstance("VTKViewer_RenderWindowInteractor") ;
69 return dynamic_cast<VTKViewer_RenderWindowInteractor *>(ret) ;
71 return new VTKViewer_RenderWindowInteractor;
74 VTKViewer_RenderWindowInteractor::VTKViewer_RenderWindowInteractor() {
76 this->mTimer = new QTimer( this ) ;
77 this->displaymode = 0;
79 vtkPicker* thepicker = vtkPicker::New();
80 thepicker->SetTolerance(0);
81 this->SetPicker(thepicker);
83 connect(mTimer, SIGNAL(timeout()), this, SLOT(TimerFunc())) ;
87 VTKViewer_RenderWindowInteractor::~VTKViewer_RenderWindowInteractor() {
91 void VTKViewer_RenderWindowInteractor::PrintSelf(ostream& os, vtkIndent indent) {
92 vtkRenderWindowInteractor::PrintSelf(os, indent) ;
94 // :NOTE: Fri Apr 21 21:51:05 2000 Pagey
95 // QGL specific stuff goes here. One should add output
96 // lines here if any protected members are added to
102 // We never allow the VTKViewer_RenderWindowInteractor to control
103 // the event loop. The application always has the control.
105 void VTKViewer_RenderWindowInteractor::Initialize() {
107 // We cannot do much unless there is a render window
108 // associated with this interactor.
110 if( ! RenderWindow ) {
111 vtkErrorMacro(<< "VTKViewer_RenderWindowInteractor::Initialize(): No render window attached!") ;
116 // We cannot hand a render window which is not a VTKViewer_RenderWindow.
117 // One way to force this is to use dynamic_cast and hope that
118 // it works. If the dynamic_cast does not work, we flag an error
119 // and get the hell out.
121 vtkRenderWindow *my_render_win = dynamic_cast<vtkRenderWindow *>(RenderWindow) ;
122 if( !my_render_win ) {
123 vtkErrorMacro(<< "VTKViewer_RenderWindowInteractor::Initialize() can only handle VTKViewer_RenderWindow.") ;
128 // If the render window has zero size, then set it to a default
131 int* aSize = my_render_win->GetSize();
132 this->Size[0] = ((aSize[0] > 0) ? aSize[0] : 300);
133 this->Size[1] = ((aSize[1] > 0) ? aSize[1] : 300);
136 // Enable the interactor.
141 // Start the rendering of the window.
143 my_render_win->Start() ;
146 // The interactor has been initialized.
148 this->Initialized = 1 ;
153 Cell_Actor = vtkActor::New();
154 Cell_Actor->PickableOff();
155 Cell_Actor->GetProperty()->SetColor(1,1,0);
156 Cell_Actor->GetProperty()->SetLineWidth(5);
157 Cell_Actor->GetProperty()->SetRepresentationToSurface();
159 Edge_Actor = vtkActor::New();
160 Edge_Actor->PickableOff();
161 Edge_Actor->GetProperty()->SetColor(1,0,0);
162 Edge_Actor->GetProperty()->SetLineWidth(5);
163 Edge_Actor->GetProperty()->SetRepresentationToWireframe();
165 Point_Actor = vtkActor::New();
166 Point_Actor->PickableOff();
167 Point_Actor->GetProperty()->SetColor(1,1,0);
168 Point_Actor->GetProperty()->SetPointSize(5);
169 Point_Actor->GetProperty()->SetRepresentationToPoints();
175 void VTKViewer_RenderWindowInteractor::SetInteractorStyle(vtkInteractorObserver *theInteractor){
176 myInteractorStyle = dynamic_cast<VTKViewer_InteractorStyleSALOME*>(theInteractor);
177 vtkRenderWindowInteractor::SetInteractorStyle(theInteractor);
181 void VTKViewer_RenderWindowInteractor::SetSelectionMode(Selection_Mode mode)
183 Cell_Actor->VisibilityOff();
184 Edge_Actor->VisibilityOff();
185 Point_Actor->VisibilityOff();
187 selectionmode = mode;
188 if ( mode == NodeSelection ) {
189 vtkPointPicker* thepicker = vtkPointPicker::New();
190 thepicker->SetTolerance(myTolNodes);
191 this->SetPicker(thepicker);
192 } else if ( mode == EdgeOfCellSelection ) {
193 vtkCellPicker* thepicker = vtkCellPicker::New();
194 thepicker->SetTolerance(myTolItems);
195 this->SetPicker(thepicker);
196 } else if ( mode == CellSelection || mode == EdgeSelection ||
197 mode == FaceSelection || mode == VolumeSelection ) {
198 vtkCellPicker* thepicker = vtkCellPicker::New();
199 thepicker->SetTolerance(myTolItems);
200 this->SetPicker(thepicker);
201 } else if ( mode == ActorSelection ) {
202 vtkPicker* thepicker = vtkPicker::New();
203 thepicker->SetTolerance(myTolItems);
204 this->SetPicker(thepicker);
207 VTKViewer_InteractorStyleSALOME* Style = 0;
208 if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
209 Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
212 Style->OnSelectionModeChanged();
215 void VTKViewer_RenderWindowInteractor::SetSelectionProp(const double& theRed, const double& theGreen,
216 const double& theBlue, const int& theWidth)
218 Cell_Actor->GetProperty()->SetColor(theRed, theGreen, theBlue);
219 Cell_Actor->GetProperty()->SetLineWidth(theWidth);
221 Point_Actor->GetProperty()->SetColor(theRed, theGreen, theBlue);
222 Point_Actor->GetProperty()->SetPointSize(theWidth);
225 void VTKViewer_RenderWindowInteractor::SetSelectionTolerance(const double& theTolNodes, const double& theTolItems)
227 myTolNodes = theTolNodes;
228 myTolItems = theTolItems;
229 if (this->GetPicker()->IsA("vtkPointPicker")) {
230 vtkPointPicker* picker = vtkPointPicker::SafeDownCast(this->GetPicker());
231 picker->SetTolerance(theTolNodes);
233 else if (this->GetPicker()->IsA("vtkPicker")) {
234 vtkPicker* picker = vtkPicker::SafeDownCast(this->GetPicker());
235 picker->SetTolerance(theTolItems);
239 void VTKViewer_RenderWindowInteractor::Enable() {
241 // Do not need to do anything if already enabled.
243 if( this->Enabled ) {
248 // Attach slots to every useful signal of the render window.
250 this->ConnectSlots() ;
256 void VTKViewer_RenderWindowInteractor::Disable() {
257 if( ! this->Enabled ) {
261 this->DisconnectSlots() ;
266 // ==================================
267 void VTKViewer_RenderWindowInteractor::Start() {
269 // We do not allow this interactor to control the
270 // event loop. Only the QtApplication objects are
271 // allowed to do that.
273 vtkErrorMacro(<<"VTKViewer_RenderWindowInteractor::Start() not allowed to start event loop.") ;
277 void VTKViewer_RenderWindowInteractor::UpdateSize(int w, int h) {
278 // if the size changed send this on to the RenderWindow
279 if ((w != this->Size[0])||(h != this->Size[1])) {
282 this->RenderWindow->SetSize(w,h);
286 int VTKViewer_RenderWindowInteractor::CreateTimer(int vtkNotUsed(timertype)) {
288 // Start a one-shot timer for 10ms.
290 mTimer->start(10, TRUE) ;
294 int VTKViewer_RenderWindowInteractor::DestroyTimer(void) {
296 // :TRICKY: Tue May 2 00:17:32 2000 Pagey
298 // QTimer will automatically expire after 10ms. So
299 // we do not need to do anything here. In fact, we
300 // should not even Stop() the QTimer here because doing
301 // this will skip some of the processing that the TimerFunc()
302 // does and will result in undesirable effects. For
303 // example, this will result in vtkLODActor to leave
304 // the models in low-res mode after the mouse stops
310 void VTKViewer_RenderWindowInteractor::TimerFunc() {
311 if( ! this->Enabled ) {
315 ((vtkInteractorStyle*)this->InteractorStyle)->OnTimer() ;
316 emit RenderWindowModified() ;
319 void VTKViewer_RenderWindowInteractor::ConnectSlots() {
320 ProcessSlotConnections(true) ;
323 void VTKViewer_RenderWindowInteractor::DisconnectSlots() {
324 ProcessSlotConnections(false) ;
327 void VTKViewer_RenderWindowInteractor::ProcessSlotConnections(bool conn) {
329 // We cannot do much unless there is a render window
330 // associated with this interactor.
332 if( ! myGUIWindow ) {
333 vtkErrorMacro(<< "VTKViewer_RenderWindowInteractor::Initialize(): No render window attached!") ;
338 bool (*slot_func) ( const QObject * sender,
340 const QObject * receiver,
341 const char * member ) ;
343 slot_func = &QObject::connect ;
345 slot_func = &QObject::disconnect ;
348 // We cannot hand a render window which is not a VTKViewer_RenderWindow.
349 // One way to force this is to use dynamic_cast and hope that
350 // it works. If the dynamic_cast does not work, we flag an error
351 // and get the hell out.
353 if( ! myGUIWindow ) {
354 vtkErrorMacro(<< "VTKViewer_RenderWindowInteractor::Initialize() can only handle VTKViewer_RenderWindow.") ;
358 slot_func ( myGUIWindow, SIGNAL(ButtonPressed(const QMouseEvent *)),
359 this, SLOT(ButtonPressed(const QMouseEvent *)) ) ;
360 slot_func( myGUIWindow, SIGNAL(ButtonReleased(const QMouseEvent *)),
361 this, SLOT(ButtonReleased(const QMouseEvent *)) ) ;
364 slot_func( myGUIWindow, SIGNAL(LeftButtonPressed(const QMouseEvent *)),
365 this, SLOT(LeftButtonPressed(const QMouseEvent *)) ) ;
366 slot_func( myGUIWindow, SIGNAL(LeftButtonReleased(const QMouseEvent *)),
367 this, SLOT(LeftButtonReleased(const QMouseEvent *)) ) ;
369 slot_func( myGUIWindow, SIGNAL(MiddleButtonPressed(const QMouseEvent *)),
370 this, SLOT(MiddleButtonPressed(const QMouseEvent *)) ) ;
371 slot_func( myGUIWindow, SIGNAL(MiddleButtonReleased(const QMouseEvent *)),
372 this, SLOT(MiddleButtonReleased(const QMouseEvent *)) ) ;
374 slot_func( myGUIWindow, SIGNAL(RightButtonPressed(const QMouseEvent *)),
375 this, SLOT(RightButtonPressed(const QMouseEvent *)) ) ;
376 slot_func( myGUIWindow, SIGNAL(RightButtonReleased(const QMouseEvent *)),
377 this, SLOT(RightButtonReleased(const QMouseEvent *)) ) ;
379 slot_func( myGUIWindow, SIGNAL(MouseMove(const QMouseEvent *)),
380 this, SLOT(MouseMove(const QMouseEvent *)) ) ;
382 slot_func( myGUIWindow, SIGNAL(KeyPressed(QKeyEvent *)),
383 this, SLOT(KeyPressed(QKeyEvent *)) ) ;
385 slot_func( this, SIGNAL(RenderWindowModified()),
386 myGUIWindow, SLOT(update()) ) ;
390 void VTKViewer_RenderWindowInteractor::MouseMove(const QMouseEvent *event) {
391 if( ! this->Enabled ) {
394 VTKViewer_InteractorStyleSALOME* Style = 0;
395 if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
396 Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
398 emit RenderWindowModified() ;
401 Style->OnMouseMove(0, 0, event->x(), event->y()/*this->Size[1] - event->y() - 1*/) ;
402 if (Style->needsRedrawing() )
403 emit RenderWindowModified() ;
406 void VTKViewer_RenderWindowInteractor::LeftButtonPressed(const QMouseEvent *event) {
407 if( ! this->Enabled ) {
410 VTKViewer_InteractorStyleSALOME* Style = 0;
411 if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
412 Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
415 Style->OnLeftButtonDown((event->state() & ControlButton),
416 (event->state() & ShiftButton),
417 event->x(), event->y());
420 void VTKViewer_RenderWindowInteractor::LeftButtonReleased(const QMouseEvent *event) {
421 if( ! this->Enabled ) {
424 VTKViewer_InteractorStyleSALOME* Style = 0;
425 if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
426 Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
429 Style->OnLeftButtonUp( (event->state() & ControlButton),
430 (event->state() & ShiftButton),
431 event->x(), event->y() ) ;
434 void VTKViewer_RenderWindowInteractor::MiddleButtonPressed(const QMouseEvent *event) {
435 if( ! this->Enabled ) {
438 VTKViewer_InteractorStyleSALOME* Style = 0;
439 if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
440 Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
443 Style->OnMiddleButtonDown((event->state() & ControlButton),
444 (event->state() & ShiftButton),
445 event->x(), event->y() ) ;
448 void VTKViewer_RenderWindowInteractor::MiddleButtonReleased(const QMouseEvent *event) {
449 if( ! this->Enabled ) {
452 VTKViewer_InteractorStyleSALOME* Style = 0;
453 if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
454 Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
457 Style->OnMiddleButtonUp( (event->state() & ControlButton),
458 (event->state() & ShiftButton),
459 event->x(), event->y() ) ;
462 void VTKViewer_RenderWindowInteractor::RightButtonPressed(const QMouseEvent *event) {
463 if( ! this->Enabled ) {
466 VTKViewer_InteractorStyleSALOME* Style = 0;
467 if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
468 Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
471 Style->OnRightButtonDown( (event->state() & ControlButton),
472 (event->state() & ShiftButton),
473 event->x(), event->y() ) ;
476 void VTKViewer_RenderWindowInteractor::RightButtonReleased(const QMouseEvent *event) {
477 if( ! this->Enabled ) {
480 VTKViewer_InteractorStyleSALOME* Style = 0;
481 if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
482 Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
485 Style->OnRightButtonUp( (event->state() & ControlButton),
486 (event->state() & ShiftButton),
487 event->x(), event->y() ) ;
490 void VTKViewer_RenderWindowInteractor::ButtonPressed(const QMouseEvent *event) {
494 void VTKViewer_RenderWindowInteractor::ButtonReleased(const QMouseEvent *event) {
498 void VTKViewer_RenderWindowInteractor::ChangeRepresentationToWireframe(vtkActorCollection* ac)
502 for (ac->InitTraversal(); (anActor = ac->GetNextActor()); ) {
503 if ( anActor->IsA("GEOM_Actor") ) {
504 SALOME_Actor* SALOMEactor = SALOME_Actor::SafeDownCast(anActor);
505 // Specific for GEOM actor
506 if ( SALOMEactor->getDisplayMode() == 1 )
507 SALOMEactor->setDisplayMode(0);
509 anActor->GetProperty()->SetRepresentationToWireframe();
512 this->RenderWindow->Render();
513 emit RenderWindowModified() ;
516 int VTKViewer_RenderWindowInteractor::GetDisplayMode() {
520 void VTKViewer_RenderWindowInteractor::SetDisplayMode(int mode) {
521 if(mode==0) ChangeRepresentationToWireframe();
522 else ChangeRepresentationToSurface();
526 void VTKViewer_RenderWindowInteractor::SwitchRepresentation(const Handle(SALOME_InteractiveObject)& IObject,
529 for (this->RenderWindow->GetRenderers()->InitTraversal();
530 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
531 vtkActorCollection* theActors = aren->GetActors();
532 theActors->InitTraversal();
533 vtkActor *ac = theActors->GetNextActor();
535 if ( ac->IsA("SALOME_Actor") ) {
536 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
537 if ( anActor->hasIO() ) {
538 if ( IObject->isSame( anActor->getIO() ) ) {
539 if ( anActor->IsA("GEOM_Actor") ) {
541 if ( anActor->getDisplayMode() == 0 ) anActor->setDisplayMode(1);
542 else anActor->setDisplayMode(0);
545 if(anActor->GetProperty()->GetRepresentation() <= 1)
546 anActor->GetProperty()->SetRepresentationToSurface();
548 anActor->GetProperty()->SetRepresentationToWireframe();
553 ac = theActors->GetNextActor();
557 this->RenderWindow->Render();
558 emit RenderWindowModified() ;
564 void VTKViewer_RenderWindowInteractor::ChangeRepresentationToWireframe()
565 // change all actors to wireframe
567 vtkActorCollection *ac;
568 vtkActor *anActor, *aPart;
570 ac = GetRenderer()->GetActors();
571 ChangeRepresentationToWireframe(ac);
576 void VTKViewer_RenderWindowInteractor::ChangeRepresentationToSurface(vtkActorCollection* ac)
578 vtkActor *anActor, *aPart;
580 for (ac->InitTraversal(); (anActor = ac->GetNextActor()); ) {
581 if ( anActor->IsA("GEOM_Actor") ) {
582 SALOME_Actor* SALOMEactor = SALOME_Actor::SafeDownCast(anActor);
583 // Specific for GEOM actor
584 if ( SALOMEactor->getDisplayMode() == 0 )
585 SALOMEactor->setDisplayMode(1);
587 anActor->GetProperty()->SetRepresentationToSurface();
590 this->RenderWindow->Render();
591 emit RenderWindowModified() ;
594 void VTKViewer_RenderWindowInteractor::ChangeRepresentationToSurface()
595 // change all actors to "surface" or solid
597 vtkActorCollection *ac;
598 vtkActor *anActor, *aPart;
600 ac = GetRenderer()->GetActors();
601 ChangeRepresentationToSurface(ac);
604 vtkRenderer* VTKViewer_RenderWindowInteractor::GetRenderer()
606 vtkRendererCollection * theRenderers = this->RenderWindow->GetRenderers();
607 theRenderers->InitTraversal();
608 return theRenderers->GetNextItem();
611 void VTKViewer_RenderWindowInteractor::EraseAll()
613 vtkActorCollection *ac;
614 vtkActor *anActor, *aPart;
616 ac = GetRenderer()->GetActors();
617 for (ac->InitTraversal(); (anActor = ac->GetNextActor()); ) {
618 if ( anActor->IsA("SALOME_Actor") ) {
619 SALOME_Actor* SActor = SALOME_Actor::SafeDownCast( anActor );
620 SActor->SetVisibility(false);
623 emit RenderWindowModified() ;
626 void VTKViewer_RenderWindowInteractor::DisplayAll()
628 vtkActorCollection *ac;
629 vtkActor *anActor, *aPart;
631 ac = GetRenderer()->GetActors();
632 for (ac->InitTraversal(); (anActor = ac->GetNextActor()); ) {
633 if ( anActor->IsA("SALOME_Actor") ) {
634 SALOME_Actor* SActor = SALOME_Actor::SafeDownCast( anActor );
635 SActor->SetVisibility( true );
638 emit RenderWindowModified() ;
641 void VTKViewer_RenderWindowInteractor::Erase( SALOME_Actor* SActor, bool update)
643 SActor->SetVisibility( false );
645 // Erase dependent actors
646 vtkActorCollection* theChildActors = vtkActorCollection::New();
647 SActor->GetChildActors(theChildActors);
649 theChildActors->InitTraversal();
650 vtkActor *ac = theChildActors->GetNextActor();
652 ac->SetVisibility( false );
653 ac = theChildActors->GetNextActor();
657 emit RenderWindowModified();
660 void VTKViewer_RenderWindowInteractor::Erase(const Handle(SALOME_InteractiveObject)& IObject, bool update)
663 for (this->RenderWindow->GetRenderers()->InitTraversal();
664 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
665 vtkActorCollection* theActors = aren->GetActors();
666 theActors->InitTraversal();
667 vtkActor *ac = theActors->GetNextActor();
669 if ( ac->IsA("SALOME_Actor") ) {
670 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
671 if ( anActor->hasIO() ) {
672 if ( IObject->isSame( anActor->getIO() ) ) {
673 Erase(anActor, false);
677 ac = theActors->GetNextActor();
681 emit RenderWindowModified() ;
684 void VTKViewer_RenderWindowInteractor::Remove(const Handle(SALOME_InteractiveObject)& IObject, bool update)
687 for (this->RenderWindow->GetRenderers()->InitTraversal();
688 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
689 vtkActorCollection* theActors = aren->GetActors();
690 theActors->InitTraversal();
691 vtkActor *ac = theActors->GetNextActor();
693 if ( ac->IsA("SALOME_Actor") ) {
694 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
695 if ( anActor->hasIO() ) {
696 if ( IObject->isSame( anActor->getIO() ) ) {
697 aren->RemoveActor(anActor);
701 ac = theActors->GetNextActor();
705 emit RenderWindowModified() ;
708 float VTKViewer_RenderWindowInteractor::GetTransparency(const Handle(SALOME_InteractiveObject)& IObject) {
710 for (this->RenderWindow->GetRenderers()->InitTraversal();
711 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
712 vtkActorCollection* theActors = aren->GetActors();
713 theActors->InitTraversal();
714 vtkActor *ac = theActors->GetNextActor();
716 if ( ac->IsA("SALOME_Actor") ) {
717 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
718 if ( anActor->hasIO() ) {
719 if ( IObject->isSame( anActor->getIO() ) ) {
720 if ( anActor->IsA("GEOM_Actor") ) {
722 return (1-(anActor->GetOpacity()));
724 else return (1-anActor->GetProperty()->GetOpacity());
728 ac = theActors->GetNextActor();
734 void VTKViewer_RenderWindowInteractor::SetTransparency(const Handle(SALOME_InteractiveObject)& IObject, float trans) {
736 for (this->RenderWindow->GetRenderers()->InitTraversal();
737 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
738 vtkActorCollection* theActors = aren->GetActors();
739 theActors->InitTraversal();
740 vtkActor *ac = theActors->GetNextActor();
742 if ( ac->IsA("SALOME_Actor") ) {
743 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
744 if ( anActor->hasIO() ) {
745 if ( IObject->isSame( anActor->getIO() ) ) {
746 if ( anActor->IsA("GEOM_Actor") ) {
748 anActor->SetOpacity(1-trans);
750 else anActor->GetProperty()->SetOpacity(1-trans);
754 ac = theActors->GetNextActor();
759 void VTKViewer_RenderWindowInteractor::Display( SALOME_Actor* SActor, bool update)
762 for (this->RenderWindow->GetRenderers()->InitTraversal();
763 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
764 aren->AddActor( SActor );
765 SActor->SetVisibility( true );
770 emit RenderWindowModified();
774 void VTKViewer_RenderWindowInteractor::Display(const Handle(SALOME_InteractiveObject)& IObject, bool update)
776 Standard_Boolean isalreadydisplayed = false;
779 for (this->RenderWindow->GetRenderers()->InitTraversal();
780 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
781 vtkActorCollection* theActors = aren->GetActors();
782 theActors->InitTraversal();
783 vtkActor *ac = theActors->GetNextActor();
785 if ( ac->IsA("SALOME_Actor") ) {
786 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
787 if ( anActor->hasIO() ) {
788 if ( IObject->isSame( anActor->getIO() ) ) {
789 anActor->SetVisibility( true );
793 ac = theActors->GetNextActor();
797 emit RenderWindowModified() ;
800 void VTKViewer_RenderWindowInteractor::KeyPressed(QKeyEvent *event) {
801 // MESSAGE ( " vtkQGLRenderWindowInteractor::KeyPressed " )
803 // if (!QAD_Application::getDesktop()->getActiveComponent().isEmpty()) {
804 // QAD_Application::getDesktop()->onKeyPress( event );
808 bool VTKViewer_RenderWindowInteractor::highlight( const Handle(SALOME_InteractiveObject)& IObject,
812 VTKViewer_InteractorStyleSALOME* aStyle =
813 VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
814 if(!aStyle) return false;
815 vtkRendererCollection* aRenColl = this->RenderWindow->GetRenderers();
816 aRenColl->InitTraversal();
817 while(vtkRenderer* aRen = this->RenderWindow->GetRenderers()->GetNextItem()){
818 vtkActorCollection* theActors = aRen->GetActors();
819 theActors->InitTraversal();
820 while(vtkActor *anAct = theActors->GetNextActor()) {
821 if(SALOME_Actor* anActor = SALOME_Actor::SafeDownCast(anAct)){
822 if(anActor->hasIO()){
823 if(IObject->isSame(anActor->getIO())){
824 if(anActor->GetMapper() == NULL)
826 //highlight or unhilight actors
827 if(anActor->hasHighlight())
828 anActor->highlight(hilight);
830 if(anActor->GetVisibility() == 1 && hilight)
831 aStyle->HighlightProp(anActor);
833 aStyle->HighlightProp(NULL);
842 emit RenderWindowModified();
847 void VTKViewer_RenderWindowInteractor::Update() {
849 for (this->RenderWindow->GetRenderers()->InitTraversal();
850 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
851 vtkActorCollection* theActors = aren->GetActors();
852 theActors->InitTraversal();
853 vtkActor *ac = theActors->GetNextActor();
856 ac->ApplyProperties();
857 //VSV: No in VTK: ac->Update();
858 ac = theActors->GetNextActor();
861 emit RenderWindowModified() ;
866 bool VTKViewer_RenderWindowInteractor::unHighlightAll(){
867 VTKViewer_InteractorStyleSALOME* aStyle =
868 VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
869 if(aStyle) aStyle->HighlightProp(NULL);
870 vtkRendererCollection* aRenColl = this->RenderWindow->GetRenderers();
871 aRenColl->InitTraversal();
872 while(vtkRenderer* aRen = this->RenderWindow->GetRenderers()->GetNextItem()){
873 vtkActorCollection* theActors = aRen->GetActors();
874 if(theActors->IsItemPresent(Point_Actor))
875 aRen->RemoveActor(Point_Actor);
876 if(theActors->IsItemPresent(Edge_Actor))
877 aRen->RemoveActor(Edge_Actor);
878 if(theActors->IsItemPresent(Cell_Actor))
879 aRen->RemoveActor(Cell_Actor);
881 theActors->InitTraversal();
882 while(vtkActor *anAct = theActors->GetNextActor()) {
883 if(SALOME_Actor* anActor = SALOME_Actor::SafeDownCast(anAct)){
884 if(anActor->hasIO()){
885 //highlight or unhilight actors
886 if(anActor->hasHighlight())
887 anActor->highlight(false);
892 emit RenderWindowModified() ;
900 void VTKViewer_RenderWindowInteractor::SetColor(const Handle(SALOME_InteractiveObject)& IObject,QColor thecolor) {
902 for (this->RenderWindow->GetRenderers()->InitTraversal();
903 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
904 vtkActorCollection* theActors = aren->GetActors();
905 theActors->InitTraversal();
906 vtkActor *ac = theActors->GetNextActor();
908 if ( ac->IsA("SALOME_Actor") ) {
909 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
910 if ( anActor->hasIO() ) {
911 if ( IObject->isSame( anActor->getIO() ) ) {
912 if ( anActor->IsA("GEOM_Actor") ) {
914 anActor->SetColor(float(thecolor.red())/255,float(thecolor.green())/255,float(thecolor.blue())/255);
917 anActor->GetProperty()->SetColor(float(thecolor.red())/255,float(thecolor.green())/255,float(thecolor.blue())/255);
922 ac = theActors->GetNextActor();
927 QColor VTKViewer_RenderWindowInteractor::GetColor(const Handle(SALOME_InteractiveObject)& IObject) {
929 QColor emptycolor(0,0,0);
930 for (this->RenderWindow->GetRenderers()->InitTraversal();
931 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
932 vtkActorCollection* theActors = aren->GetActors();
933 theActors->InitTraversal();
934 vtkActor *ac = theActors->GetNextActor();
936 if ( ac->IsA("SALOME_Actor") ) {
937 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
938 if ( anActor->hasIO() ) {
939 if ( IObject->isSame( anActor->getIO() ) ) {
940 if ( anActor->IsA("GEOM_Actor") ) {
943 anActor->GetColor(r,g,b);
944 return QColor(int(r*255),int(g*255),int(b*255));
948 anActor->GetProperty()->GetColor(color);
949 return QColor(int(color[0]*255),int(color[1]*255),int(color[2]*255));
954 ac = theActors->GetNextActor();
963 bool VTKViewer_RenderWindowInteractor::isInViewer(const Handle(SALOME_InteractiveObject)& IObject)
966 for (this->RenderWindow->GetRenderers()->InitTraversal();
967 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
968 vtkActorCollection* theActors = aren->GetActors();
969 theActors->InitTraversal();
970 vtkActor *ac = theActors->GetNextActor();
972 if ( ac->IsA("SALOME_Actor") ) {
973 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
974 if ( anActor->hasIO() ) {
975 if ( IObject->isSame( anActor->getIO() ) ) {
976 //MESSAGE ( " VTKViewer_RenderWindowInteractor::isInViewer = TRUE" )
981 ac = theActors->GetNextActor();
984 //MESSAGE ( " VTKViewer_RenderWindowInteractor::isInViewer = FALSE" )
988 bool VTKViewer_RenderWindowInteractor::isVisible(const Handle(SALOME_InteractiveObject)& IObject)
991 for (this->RenderWindow->GetRenderers()->InitTraversal();
992 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
993 vtkActorCollection* theActors = aren->GetActors();
994 theActors->InitTraversal();
995 vtkActor *ac = theActors->GetNextActor();
997 if ( ac->IsA("SALOME_Actor") ) {
998 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
999 if ( anActor->hasIO() ) {
1000 if ( IObject->isSame( anActor->getIO() ) ) {
1001 return anActor->GetVisibility();
1005 ac = theActors->GetNextActor();
1011 void VTKViewer_RenderWindowInteractor::rename(const Handle(SALOME_InteractiveObject)& IObject, QString newName)
1014 for (this->RenderWindow->GetRenderers()->InitTraversal();
1015 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
1016 vtkActorCollection* theActors = aren->GetActors();
1017 theActors->InitTraversal();
1018 vtkActor *ac = theActors->GetNextActor();
1019 while(!(ac==NULL)) {
1020 if ( ac->IsA("SALOME_Actor") ) {
1021 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
1022 if ( anActor->hasIO() ) {
1023 if ( IObject->isSame( anActor->getIO() ) ) {
1024 anActor->setName(strdup(newName));
1028 ac = theActors->GetNextActor();
1032 emit RenderWindowModified() ;
1035 bool VTKViewer_RenderWindowInteractor::highlight(const Handle(SALOME_InteractiveObject)& IObject,
1036 const TColStd_MapOfInteger& MapIndex,
1037 VTKViewer_RenderWindowInteractor::TCreateMapperFun theFun,
1038 vtkActor *theActor, bool hilight, bool update )
1040 if(MapIndex.Extent() == 0) return false;
1041 vtkRenderer* aRen = GetRenderer();
1042 vtkActorCollection* anActorColl = aRen->GetActors();
1043 if ( anActorColl->IsItemPresent(theActor) != 0 )
1044 aRen->RemoveActor(theActor);
1047 setActorData(IObject, MapIndex, theFun, theActor);
1048 aRen->AddActor(theActor);
1049 theActor->VisibilityOn();
1052 theActor->VisibilityOff();
1056 this->RenderWindow->Render();
1057 emit RenderWindowModified() ;
1063 void VTKViewer_RenderWindowInteractor::setActorData(const Handle(SALOME_InteractiveObject)& IObject,
1064 const TColStd_MapOfInteger& MapIndex,
1065 VTKViewer_RenderWindowInteractor::TCreateMapperFun theFun,
1068 vtkActorCollection* anActorColl = GetRenderer()->GetActors();
1069 anActorColl->InitTraversal();
1070 vtkActor *ac = NULL;
1071 while ((ac = anActorColl->GetNextActor()) != NULL) {
1072 if (ac->IsA("SALOME_Actor")){
1073 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast(ac);
1074 if (anActor->hasIO()) {
1075 if (IObject->isSame(anActor->getIO())) {
1076 if (vtkPolyData* aSourcePolyData = anActor->GetPolyDataInput()) {
1077 vtkPolyDataMapper* aMapper = vtkPolyDataMapper::New();
1078 (*theFun)(aSourcePolyData,aMapper,MapIndex);
1079 theActor->SetMapper(aMapper);
1090 static void CellCreateMapper(vtkPolyData *theSourcePolyData, vtkPolyDataMapper* theMapper,
1091 const TColStd_MapOfInteger& theMapIndex)
1093 vtkPolyData* aPolyData = vtkPolyData::New();
1094 aPolyData->Allocate();
1096 vtkIdList *ptIds = vtkIdList::New();
1097 ptIds->Allocate(theSourcePolyData->GetMaxCellSize());
1098 // vtkPolyDataMapper* aMapper = vtkPolyDataMapper::New();
1099 aPolyData->SetPoints(theSourcePolyData->GetPoints());
1100 TColStd_MapIteratorOfMapOfInteger ite(theMapIndex);
1101 int aNbOfParts = theSourcePolyData->GetNumberOfCells();
1102 for(; ite.More(); ite.Next()){
1103 int aPartId = ite.Key();
1104 if(0 > aPartId || aPartId >= aNbOfParts) break;
1105 theSourcePolyData->GetCellPoints(aPartId,ptIds);
1106 vtkCell* aCell = theSourcePolyData->GetCell(aPartId);
1107 aPolyData->InsertNextCell(aCell->GetCellType(),ptIds);
1108 for (int i = 0, iEnd = aCell->GetNumberOfEdges(); i < iEnd; i++){
1109 vtkCell* anEdgeCell = aCell->GetEdge(i);
1110 aPolyData->InsertNextCell(VTK_LINE,anEdgeCell->GetPointIds());
1114 theMapper->SetInput(aPolyData);
1115 aPolyData->Delete();
1118 bool VTKViewer_RenderWindowInteractor::highlightCell(const Handle(SALOME_InteractiveObject)& IObject,
1120 const TColStd_MapOfInteger& MapIndex,
1123 return highlight(IObject,MapIndex,&CellCreateMapper,Cell_Actor,hilight,update);
1126 void VTKViewer_RenderWindowInteractor::setCellData(const Handle(SALOME_InteractiveObject)& IObject,
1127 const int& theIndex, vtkActor* theActor )
1129 TColStd_MapOfInteger MapIndex; MapIndex.Add(theIndex);
1130 return setActorData(IObject,MapIndex,&CellCreateMapper,theActor);
1133 void VTKViewer_RenderWindowInteractor::setCellData(const Handle(SALOME_InteractiveObject)& IObject,
1134 const std::vector<int>& theIndexes, vtkActor* theActor )
1136 TColStd_MapOfInteger MapIndex;
1137 std::vector<int>::const_iterator it;
1138 for (it = theIndexes.begin(); it != theIndexes.end(); ++it)
1140 return setActorData(IObject,MapIndex,&CellCreateMapper,theActor);
1144 static void PointCreateMapper(vtkPolyData *theSourcePolyData, vtkPolyDataMapper* theMapper,
1145 const TColStd_MapOfInteger& theMapIndex)
1147 vtkPolyData* aPolyData = vtkPolyData::New();
1148 aPolyData->Allocate();
1150 vtkPoints *aPoints = vtkPoints::New();
1151 vtkMaskPoints* aMaskPoints = vtkMaskPoints::New();
1152 TColStd_MapIteratorOfMapOfInteger ite(theMapIndex);
1153 aPoints->SetNumberOfPoints(theMapIndex.Extent());
1154 int aNbOfParts = theSourcePolyData->GetNumberOfPoints();
1155 for(int i = 0; ite.More(); ite.Next(),i++){
1156 int aPartId = ite.Key();
1157 if(0 > aPartId || aPartId >= aNbOfParts) break;
1158 aPoints->SetPoint(i,theSourcePolyData->GetPoint(aPartId));
1160 aPolyData->SetPoints(aPoints);
1162 aMaskPoints->SetInput(aPolyData);
1163 aMaskPoints->SetGenerateVertices(1);
1164 aMaskPoints->SetOnRatio(1);
1165 theMapper->SetInput(aMaskPoints->GetOutput());
1166 aMaskPoints->Delete();
1169 bool VTKViewer_RenderWindowInteractor::highlightPoint(const Handle(SALOME_InteractiveObject)& IObject,
1170 bool hilight, const TColStd_MapOfInteger& MapIndex,
1173 return highlight(IObject,MapIndex,&PointCreateMapper,Point_Actor,hilight,update);
1177 void VTKViewer_RenderWindowInteractor::setPointData(const Handle(SALOME_InteractiveObject)& IObject,
1178 const int& theIndex, vtkActor* theActor )
1180 TColStd_MapOfInteger MapIndex; MapIndex.Add(theIndex);
1181 return setActorData(IObject,MapIndex,&PointCreateMapper,theActor);
1185 static void EdgeCreateMapper(vtkPolyData *theSourcePolyData, vtkPolyDataMapper* theMapper,
1186 const TColStd_MapOfInteger& theMapIndex)
1188 vtkPolyData* aPolyData = vtkPolyData::New();
1189 aPolyData->Allocate();
1191 vtkIdList *ptIds = vtkIdList::New();
1192 ptIds->Allocate(theSourcePolyData->GetMaxCellSize());
1193 vtkPolyDataMapper* aMapper = vtkPolyDataMapper::New();
1194 aPolyData->SetPoints(theSourcePolyData->GetPoints());
1195 int aCellId = -1, aCellCounter = 0;
1196 for(TColStd_MapIteratorOfMapOfInteger ite(theMapIndex); ite.More(); ite.Next()){
1198 aCellId = ite.Key();
1202 if(aCellCounter == 1){
1203 vtkCell* aCell = theSourcePolyData->GetCell(aCellId);
1204 if(aCell->GetCellType() <= VTK_LINE){
1205 aPolyData->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
1207 TColStd_MapIteratorOfMapOfInteger ite(theMapIndex);
1208 int aNbOfParts = aCell->GetNumberOfEdges();
1209 for(; ite.More(); ite.Next()){
1211 int aPartId = -ite.Key()-1;
1212 if(0 > aPartId || aPartId >= aNbOfParts) break;
1213 vtkCell* anEdgeCell = aCell->GetEdge(aPartId);
1214 aPolyData->InsertNextCell(VTK_LINE,anEdgeCell->GetPointIds());
1219 int aNbOfParts = theSourcePolyData->GetNumberOfCells();
1220 for(TColStd_MapIteratorOfMapOfInteger ite(theMapIndex); ite.More(); ite.Next()){
1222 int aPartId = ite.Key();
1223 if(0 > aPartId || aPartId >= aNbOfParts) break;
1224 vtkCell* aCell = theSourcePolyData->GetCell(aPartId);
1225 aPolyData->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
1230 theMapper->SetInput(aPolyData);
1231 aPolyData->Delete();
1235 bool VTKViewer_RenderWindowInteractor::highlightEdge( const Handle(SALOME_InteractiveObject)& IObject,
1237 const TColStd_MapOfInteger& MapIndex,
1240 return highlight(IObject,MapIndex,&EdgeCreateMapper,Edge_Actor,hilight,update);
1243 void VTKViewer_RenderWindowInteractor::setEdgeData(const Handle(SALOME_InteractiveObject)& IObject,
1244 const int& theCellIndex, const int& theEdgeIndex,
1245 vtkActor* theActor )
1247 TColStd_MapOfInteger MapIndex; MapIndex.Add(theCellIndex); MapIndex.Add(theEdgeIndex);
1248 return setActorData(IObject,MapIndex,&EdgeCreateMapper,theActor);