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 ;
150 Cell_Actor = vtkActor::New();
151 Cell_Actor->PickableOff();
152 Cell_Actor->GetProperty()->SetColor(1,1,0);
153 Cell_Actor->GetProperty()->SetLineWidth(5);
154 Cell_Actor->GetProperty()->SetRepresentationToSurface();
156 Edge_Actor = vtkActor::New();
157 Edge_Actor->PickableOff();
158 Edge_Actor->GetProperty()->SetColor(1,0,0);
159 Edge_Actor->GetProperty()->SetLineWidth(5);
160 Edge_Actor->GetProperty()->SetRepresentationToWireframe();
162 Point_Actor = vtkActor::New();
163 Point_Actor->PickableOff();
164 Point_Actor->GetProperty()->SetColor(1,1,0);
165 Point_Actor->GetProperty()->SetPointSize(5);
166 Point_Actor->GetProperty()->SetRepresentationToPoints();
172 void VTKViewer_RenderWindowInteractor::SetInteractorStyle(vtkInteractorObserver *theInteractor){
173 myInteractorStyle = dynamic_cast<VTKViewer_InteractorStyleSALOME*>(theInteractor);
174 vtkRenderWindowInteractor::SetInteractorStyle(theInteractor);
178 void VTKViewer_RenderWindowInteractor::SetSelectionMode(int mode)
180 Cell_Actor->VisibilityOff();
181 Edge_Actor->VisibilityOff();
182 Point_Actor->VisibilityOff();
184 selectionmode = mode;
186 vtkPointPicker* thepicker = vtkPointPicker::New();
187 // thepicker->SetTolerance(0.001);
188 this->SetPicker(thepicker);
189 } else if ( mode == 2 ) {
190 vtkCellPicker* thepicker = vtkCellPicker::New();
191 thepicker->SetTolerance(0.001);
192 this->SetPicker(thepicker);
193 } else if ( mode == 3 ) {
194 vtkCellPicker* thepicker = vtkCellPicker::New();
195 thepicker->SetTolerance(0.001);
196 this->SetPicker(thepicker);
197 } else if ( mode == 4 ) {
198 vtkPicker* thepicker = vtkPicker::New();
199 thepicker->SetTolerance(0.001);
200 this->SetPicker(thepicker);
205 void VTKViewer_RenderWindowInteractor::Enable() {
207 // Do not need to do anything if already enabled.
209 if( this->Enabled ) {
214 // Attach slots to every useful signal of the render window.
216 this->ConnectSlots() ;
222 void VTKViewer_RenderWindowInteractor::Disable() {
223 if( ! this->Enabled ) {
227 this->DisconnectSlots() ;
232 // ==================================
233 void VTKViewer_RenderWindowInteractor::Start() {
235 // We do not allow this interactor to control the
236 // event loop. Only the QtApplication objects are
237 // allowed to do that.
239 vtkErrorMacro(<<"VTKViewer_RenderWindowInteractor::Start() not allowed to start event loop.") ;
243 void VTKViewer_RenderWindowInteractor::UpdateSize(int w, int h) {
244 // if the size changed send this on to the RenderWindow
245 if ((w != this->Size[0])||(h != this->Size[1])) {
248 this->RenderWindow->SetSize(w,h);
252 int VTKViewer_RenderWindowInteractor::CreateTimer(int vtkNotUsed(timertype)) {
254 // Start a one-shot timer for 10ms.
256 mTimer->start(10, TRUE) ;
260 int VTKViewer_RenderWindowInteractor::DestroyTimer(void) {
262 // :TRICKY: Tue May 2 00:17:32 2000 Pagey
264 // QTimer will automatically expire after 10ms. So
265 // we do not need to do anything here. In fact, we
266 // should not even Stop() the QTimer here because doing
267 // this will skip some of the processing that the TimerFunc()
268 // does and will result in undesirable effects. For
269 // example, this will result in vtkLODActor to leave
270 // the models in low-res mode after the mouse stops
276 void VTKViewer_RenderWindowInteractor::TimerFunc() {
277 if( ! this->Enabled ) {
281 ((vtkInteractorStyle*)this->InteractorStyle)->OnTimer() ;
282 emit RenderWindowModified() ;
285 void VTKViewer_RenderWindowInteractor::ConnectSlots() {
286 ProcessSlotConnections(true) ;
289 void VTKViewer_RenderWindowInteractor::DisconnectSlots() {
290 ProcessSlotConnections(false) ;
293 void VTKViewer_RenderWindowInteractor::ProcessSlotConnections(bool conn) {
295 // We cannot do much unless there is a render window
296 // associated with this interactor.
298 if( ! myGUIWindow ) {
299 vtkErrorMacro(<< "VTKViewer_RenderWindowInteractor::Initialize(): No render window attached!") ;
304 bool (*slot_func) ( const QObject * sender,
306 const QObject * receiver,
307 const char * member ) ;
309 slot_func = &QObject::connect ;
311 slot_func = &QObject::disconnect ;
314 // We cannot hand a render window which is not a VTKViewer_RenderWindow.
315 // One way to force this is to use dynamic_cast and hope that
316 // it works. If the dynamic_cast does not work, we flag an error
317 // and get the hell out.
319 if( ! myGUIWindow ) {
320 vtkErrorMacro(<< "VTKViewer_RenderWindowInteractor::Initialize() can only handle VTKViewer_RenderWindow.") ;
324 slot_func ( myGUIWindow, SIGNAL(ButtonPressed(const QMouseEvent *)),
325 this, SLOT(ButtonPressed(const QMouseEvent *)) ) ;
326 slot_func( myGUIWindow, SIGNAL(ButtonReleased(const QMouseEvent *)),
327 this, SLOT(ButtonReleased(const QMouseEvent *)) ) ;
330 slot_func( myGUIWindow, SIGNAL(LeftButtonPressed(const QMouseEvent *)),
331 this, SLOT(LeftButtonPressed(const QMouseEvent *)) ) ;
332 slot_func( myGUIWindow, SIGNAL(LeftButtonReleased(const QMouseEvent *)),
333 this, SLOT(LeftButtonReleased(const QMouseEvent *)) ) ;
335 slot_func( myGUIWindow, SIGNAL(MiddleButtonPressed(const QMouseEvent *)),
336 this, SLOT(MiddleButtonPressed(const QMouseEvent *)) ) ;
337 slot_func( myGUIWindow, SIGNAL(MiddleButtonReleased(const QMouseEvent *)),
338 this, SLOT(MiddleButtonReleased(const QMouseEvent *)) ) ;
340 slot_func( myGUIWindow, SIGNAL(RightButtonPressed(const QMouseEvent *)),
341 this, SLOT(RightButtonPressed(const QMouseEvent *)) ) ;
342 slot_func( myGUIWindow, SIGNAL(RightButtonReleased(const QMouseEvent *)),
343 this, SLOT(RightButtonReleased(const QMouseEvent *)) ) ;
345 slot_func( myGUIWindow, SIGNAL(MouseMove(const QMouseEvent *)),
346 this, SLOT(MouseMove(const QMouseEvent *)) ) ;
348 slot_func( myGUIWindow, SIGNAL(KeyPressed(QKeyEvent *)),
349 this, SLOT(KeyPressed(QKeyEvent *)) ) ;
351 slot_func( this, SIGNAL(RenderWindowModified()),
352 myGUIWindow, SLOT(update()) ) ;
356 void VTKViewer_RenderWindowInteractor::MouseMove(const QMouseEvent *event) {
357 if( ! this->Enabled ) {
360 VTKViewer_InteractorStyleSALOME* Style = 0;
361 if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
362 Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
364 emit RenderWindowModified() ;
367 Style->OnMouseMove(0, 0, event->x(), event->y()/*this->Size[1] - event->y() - 1*/) ;
368 if (Style->needsRedrawing() )
369 emit RenderWindowModified() ;
372 void VTKViewer_RenderWindowInteractor::LeftButtonPressed(const QMouseEvent *event) {
373 if( ! this->Enabled ) {
376 VTKViewer_InteractorStyleSALOME* Style = 0;
377 if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
378 Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
381 Style->OnLeftButtonDown((event->state() & ControlButton),
382 (event->state() & ShiftButton),
383 event->x(), event->y());
386 void VTKViewer_RenderWindowInteractor::LeftButtonReleased(const QMouseEvent *event) {
387 if( ! this->Enabled ) {
390 VTKViewer_InteractorStyleSALOME* Style = 0;
391 if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
392 Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
395 Style->OnLeftButtonUp( (event->state() & ControlButton),
396 (event->state() & ShiftButton),
397 event->x(), event->y() ) ;
400 void VTKViewer_RenderWindowInteractor::MiddleButtonPressed(const QMouseEvent *event) {
401 if( ! this->Enabled ) {
404 VTKViewer_InteractorStyleSALOME* Style = 0;
405 if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
406 Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
409 Style->OnMiddleButtonDown((event->state() & ControlButton),
410 (event->state() & ShiftButton),
411 event->x(), event->y() ) ;
414 void VTKViewer_RenderWindowInteractor::MiddleButtonReleased(const QMouseEvent *event) {
415 if( ! this->Enabled ) {
418 VTKViewer_InteractorStyleSALOME* Style = 0;
419 if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
420 Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
423 Style->OnMiddleButtonUp( (event->state() & ControlButton),
424 (event->state() & ShiftButton),
425 event->x(), event->y() ) ;
428 void VTKViewer_RenderWindowInteractor::RightButtonPressed(const QMouseEvent *event) {
429 if( ! this->Enabled ) {
432 VTKViewer_InteractorStyleSALOME* Style = 0;
433 if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
434 Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
437 Style->OnRightButtonDown( (event->state() & ControlButton),
438 (event->state() & ShiftButton),
439 event->x(), event->y() ) ;
442 void VTKViewer_RenderWindowInteractor::RightButtonReleased(const QMouseEvent *event) {
443 if( ! this->Enabled ) {
446 VTKViewer_InteractorStyleSALOME* Style = 0;
447 if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
448 Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
451 Style->OnRightButtonUp( (event->state() & ControlButton),
452 (event->state() & ShiftButton),
453 event->x(), event->y() ) ;
456 void VTKViewer_RenderWindowInteractor::ButtonPressed(const QMouseEvent *event) {
460 void VTKViewer_RenderWindowInteractor::ButtonReleased(const QMouseEvent *event) {
464 void VTKViewer_RenderWindowInteractor::ChangeRepresentationToWireframe(vtkActorCollection* ac)
468 for (ac->InitTraversal(); (anActor = ac->GetNextActor()); ) {
469 if ( anActor->IsA("GEOM_Actor") ) {
470 SALOME_Actor* SALOMEactor = SALOME_Actor::SafeDownCast(anActor);
471 // Specific for GEOM actor
472 if ( SALOMEactor->getDisplayMode() == 1 )
473 SALOMEactor->setDisplayMode(0);
475 anActor->GetProperty()->SetRepresentationToWireframe();
478 this->RenderWindow->Render();
479 emit RenderWindowModified() ;
482 int VTKViewer_RenderWindowInteractor::GetDisplayMode() {
486 void VTKViewer_RenderWindowInteractor::SetDisplayMode(int mode) {
487 if(mode==0) ChangeRepresentationToWireframe();
488 else ChangeRepresentationToSurface();
492 void VTKViewer_RenderWindowInteractor::SwitchRepresentation(const Handle(SALOME_InteractiveObject)& IObject,
495 for (this->RenderWindow->GetRenderers()->InitTraversal();
496 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
497 vtkActorCollection* theActors = aren->GetActors();
498 theActors->InitTraversal();
499 vtkActor *ac = theActors->GetNextActor();
501 if ( ac->IsA("SALOME_Actor") ) {
502 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
503 if ( anActor->hasIO() ) {
504 if ( IObject->isSame( anActor->getIO() ) ) {
505 if ( anActor->IsA("GEOM_Actor") ) {
507 if ( anActor->getDisplayMode() == 0 ) anActor->setDisplayMode(1);
508 else anActor->setDisplayMode(0);
511 if(anActor->GetProperty()->GetRepresentation() <= 1)
512 anActor->GetProperty()->SetRepresentationToSurface();
514 anActor->GetProperty()->SetRepresentationToWireframe();
519 ac = theActors->GetNextActor();
523 this->RenderWindow->Render();
524 emit RenderWindowModified() ;
530 void VTKViewer_RenderWindowInteractor::ChangeRepresentationToWireframe()
531 // change all actors to wireframe
533 vtkActorCollection *ac;
534 vtkActor *anActor, *aPart;
536 ac = GetRenderer()->GetActors();
537 ChangeRepresentationToWireframe(ac);
542 void VTKViewer_RenderWindowInteractor::ChangeRepresentationToSurface(vtkActorCollection* ac)
544 vtkActor *anActor, *aPart;
546 for (ac->InitTraversal(); (anActor = ac->GetNextActor()); ) {
547 if ( anActor->IsA("GEOM_Actor") ) {
548 SALOME_Actor* SALOMEactor = SALOME_Actor::SafeDownCast(anActor);
549 // Specific for GEOM actor
550 if ( SALOMEactor->getDisplayMode() == 0 )
551 SALOMEactor->setDisplayMode(1);
553 anActor->GetProperty()->SetRepresentationToSurface();
556 this->RenderWindow->Render();
557 emit RenderWindowModified() ;
560 void VTKViewer_RenderWindowInteractor::ChangeRepresentationToSurface()
561 // change all actors to "surface" or solid
563 vtkActorCollection *ac;
564 vtkActor *anActor, *aPart;
566 ac = GetRenderer()->GetActors();
567 ChangeRepresentationToSurface(ac);
570 vtkRenderer* VTKViewer_RenderWindowInteractor::GetRenderer()
572 vtkRendererCollection * theRenderers = this->RenderWindow->GetRenderers();
573 theRenderers->InitTraversal();
574 return theRenderers->GetNextItem();
577 void VTKViewer_RenderWindowInteractor::EraseAll()
579 vtkActorCollection *ac;
580 vtkActor *anActor, *aPart;
582 ac = GetRenderer()->GetActors();
583 for (ac->InitTraversal(); (anActor = ac->GetNextActor()); ) {
584 if ( anActor->IsA("SALOME_Actor") ) {
585 SALOME_Actor* SActor = SALOME_Actor::SafeDownCast( anActor );
586 SActor->SetVisibility(false);
589 emit RenderWindowModified() ;
592 void VTKViewer_RenderWindowInteractor::DisplayAll()
594 vtkActorCollection *ac;
595 vtkActor *anActor, *aPart;
597 ac = GetRenderer()->GetActors();
598 for (ac->InitTraversal(); (anActor = ac->GetNextActor()); ) {
599 if ( anActor->IsA("SALOME_Actor") ) {
600 SALOME_Actor* SActor = SALOME_Actor::SafeDownCast( anActor );
601 SActor->SetVisibility( true );
604 emit RenderWindowModified() ;
607 void VTKViewer_RenderWindowInteractor::Erase(const Handle(SALOME_InteractiveObject)& IObject, bool update)
610 for (this->RenderWindow->GetRenderers()->InitTraversal();
611 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
612 vtkActorCollection* theActors = aren->GetActors();
613 vtkActorCollection* theChildActors = vtkActorCollection::New();
614 theActors->InitTraversal();
615 vtkActor *ac = theActors->GetNextActor();
617 if ( ac->IsA("SALOME_Actor") ) {
618 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
619 if ( anActor->hasIO() ) {
620 if ( IObject->isSame( anActor->getIO() ) ) {
621 anActor->SetVisibility( false );
622 anActor->GetChildActors(theChildActors);
626 ac = theActors->GetNextActor();
629 // Erase dependent actors
630 theChildActors->InitTraversal();
631 ac = theChildActors->GetNextActor();
633 ac->SetVisibility( false );
634 ac = theChildActors->GetNextActor();
638 emit RenderWindowModified() ;
641 void VTKViewer_RenderWindowInteractor::Remove(const Handle(SALOME_InteractiveObject)& IObject, bool update)
644 for (this->RenderWindow->GetRenderers()->InitTraversal();
645 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
646 vtkActorCollection* theActors = aren->GetActors();
647 theActors->InitTraversal();
648 vtkActor *ac = theActors->GetNextActor();
650 if ( ac->IsA("SALOME_Actor") ) {
651 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
652 if ( anActor->hasIO() ) {
653 if ( IObject->isSame( anActor->getIO() ) ) {
654 aren->RemoveActor(anActor);
658 ac = theActors->GetNextActor();
662 emit RenderWindowModified() ;
665 float VTKViewer_RenderWindowInteractor::GetTransparency(const Handle(SALOME_InteractiveObject)& IObject) {
667 for (this->RenderWindow->GetRenderers()->InitTraversal();
668 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
669 vtkActorCollection* theActors = aren->GetActors();
670 theActors->InitTraversal();
671 vtkActor *ac = theActors->GetNextActor();
673 if ( ac->IsA("SALOME_Actor") ) {
674 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
675 if ( anActor->hasIO() ) {
676 if ( IObject->isSame( anActor->getIO() ) ) {
677 if ( anActor->IsA("GEOM_Actor") ) {
679 return (1-(anActor->GetOpacity()));
681 else return (1-anActor->GetProperty()->GetOpacity());
685 ac = theActors->GetNextActor();
691 void VTKViewer_RenderWindowInteractor::SetTransparency(const Handle(SALOME_InteractiveObject)& IObject, float trans) {
693 for (this->RenderWindow->GetRenderers()->InitTraversal();
694 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
695 vtkActorCollection* theActors = aren->GetActors();
696 theActors->InitTraversal();
697 vtkActor *ac = theActors->GetNextActor();
699 if ( ac->IsA("SALOME_Actor") ) {
700 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
701 if ( anActor->hasIO() ) {
702 if ( IObject->isSame( anActor->getIO() ) ) {
703 if ( anActor->IsA("GEOM_Actor") ) {
705 anActor->SetOpacity(1-trans);
707 else anActor->GetProperty()->SetOpacity(1-trans);
711 ac = theActors->GetNextActor();
716 void VTKViewer_RenderWindowInteractor::Display( SALOME_Actor* SActor, bool update)
719 for (this->RenderWindow->GetRenderers()->InitTraversal();
720 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
721 aren->AddActor( SActor );
722 SActor->SetVisibility( true );
727 emit RenderWindowModified();
731 void VTKViewer_RenderWindowInteractor::Display(const Handle(SALOME_InteractiveObject)& IObject, bool update)
733 Standard_Boolean isalreadydisplayed = false;
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 anActor->SetVisibility( true );
750 ac = theActors->GetNextActor();
754 emit RenderWindowModified() ;
757 void VTKViewer_RenderWindowInteractor::KeyPressed(QKeyEvent *event) {
758 // MESSAGE ( " vtkQGLRenderWindowInteractor::KeyPressed " )
760 // if (!QAD_Application::getDesktop()->getActiveComponent().isEmpty()) {
761 // QAD_Application::getDesktop()->onKeyPress( event );
765 bool VTKViewer_RenderWindowInteractor::highlight( const Handle(SALOME_InteractiveObject)& IObject,
769 VTKViewer_InteractorStyleSALOME* aStyle =
770 VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
771 if(!aStyle) return false;
772 vtkRendererCollection* aRenColl = this->RenderWindow->GetRenderers();
773 aRenColl->InitTraversal();
774 while(vtkRenderer* aRen = this->RenderWindow->GetRenderers()->GetNextItem()){
775 vtkActorCollection* theActors = aRen->GetActors();
776 theActors->InitTraversal();
777 while(vtkActor *anAct = theActors->GetNextActor()) {
778 if(SALOME_Actor* anActor = SALOME_Actor::SafeDownCast(anAct)){
779 if(anActor->hasIO()){
780 if(IObject->isSame(anActor->getIO())){
781 if(anActor->GetMapper() == NULL)
783 //highlight or unhilight actors
784 if(anActor->hasHighlight())
785 anActor->highlight(hilight);
787 if(anActor->GetVisibility() == 1 && hilight)
788 aStyle->HighlightProp(anActor);
790 aStyle->HighlightProp(NULL);
799 emit RenderWindowModified();
804 void VTKViewer_RenderWindowInteractor::Update() {
806 for (this->RenderWindow->GetRenderers()->InitTraversal();
807 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
808 vtkActorCollection* theActors = aren->GetActors();
809 theActors->InitTraversal();
810 vtkActor *ac = theActors->GetNextActor();
813 ac->ApplyProperties();
814 //VSV: No in VTK: ac->Update();
815 ac = theActors->GetNextActor();
818 emit RenderWindowModified() ;
823 bool VTKViewer_RenderWindowInteractor::unHighlightAll(){
824 VTKViewer_InteractorStyleSALOME* aStyle =
825 VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
826 if(aStyle) aStyle->HighlightProp(NULL);
827 vtkRendererCollection* aRenColl = this->RenderWindow->GetRenderers();
828 aRenColl->InitTraversal();
829 while(vtkRenderer* aRen = this->RenderWindow->GetRenderers()->GetNextItem()){
830 vtkActorCollection* theActors = aRen->GetActors();
831 theActors->InitTraversal();
832 if(theActors->IsItemPresent(Point_Actor))
833 aRen->RemoveActor(Point_Actor);
834 if(theActors->IsItemPresent(Edge_Actor))
835 aRen->RemoveActor(Edge_Actor);
836 if(theActors->IsItemPresent(Cell_Actor))
837 aRen->RemoveActor(Cell_Actor);
838 vtkActor *anActor = theActors->GetNextActor();
839 while(vtkActor *anAct = theActors->GetNextActor()) {
840 if(SALOME_Actor* anActor = SALOME_Actor::SafeDownCast(anAct)){
841 if(anActor->hasIO()){
842 //highlight or unhilight actors
843 if(anActor->hasHighlight())
844 anActor->highlight(false);
849 emit RenderWindowModified() ;
857 void VTKViewer_RenderWindowInteractor::SetColor(const Handle(SALOME_InteractiveObject)& IObject,QColor thecolor) {
859 for (this->RenderWindow->GetRenderers()->InitTraversal();
860 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
861 vtkActorCollection* theActors = aren->GetActors();
862 theActors->InitTraversal();
863 vtkActor *ac = theActors->GetNextActor();
865 if ( ac->IsA("SALOME_Actor") ) {
866 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
867 if ( anActor->hasIO() ) {
868 if ( IObject->isSame( anActor->getIO() ) ) {
869 if ( anActor->IsA("GEOM_Actor") ) {
871 anActor->SetColor(float(thecolor.red())/255,float(thecolor.green())/255,float(thecolor.blue())/255);
874 anActor->GetProperty()->SetColor(float(thecolor.red())/255,float(thecolor.green())/255,float(thecolor.blue())/255);
879 ac = theActors->GetNextActor();
884 QColor VTKViewer_RenderWindowInteractor::GetColor(const Handle(SALOME_InteractiveObject)& IObject) {
886 QColor emptycolor(0,0,0);
887 for (this->RenderWindow->GetRenderers()->InitTraversal();
888 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
889 vtkActorCollection* theActors = aren->GetActors();
890 theActors->InitTraversal();
891 vtkActor *ac = theActors->GetNextActor();
893 if ( ac->IsA("SALOME_Actor") ) {
894 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
895 if ( anActor->hasIO() ) {
896 if ( IObject->isSame( anActor->getIO() ) ) {
897 if ( anActor->IsA("GEOM_Actor") ) {
900 anActor->GetColor(r,g,b);
901 return QColor(int(r*255),int(g*255),int(b*255));
905 anActor->GetProperty()->GetColor(color);
906 return QColor(int(color[0]*255),int(color[1]*255),int(color[2]*255));
911 ac = theActors->GetNextActor();
920 bool VTKViewer_RenderWindowInteractor::isInViewer(const Handle(SALOME_InteractiveObject)& IObject)
923 for (this->RenderWindow->GetRenderers()->InitTraversal();
924 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
925 vtkActorCollection* theActors = aren->GetActors();
926 theActors->InitTraversal();
927 vtkActor *ac = theActors->GetNextActor();
929 if ( ac->IsA("SALOME_Actor") ) {
930 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
931 if ( anActor->hasIO() ) {
932 if ( IObject->isSame( anActor->getIO() ) ) {
933 //MESSAGE ( " VTKViewer_RenderWindowInteractor::isInViewer = TRUE" )
938 ac = theActors->GetNextActor();
941 //MESSAGE ( " VTKViewer_RenderWindowInteractor::isInViewer = FALSE" )
945 bool VTKViewer_RenderWindowInteractor::isVisible(const Handle(SALOME_InteractiveObject)& IObject)
948 for (this->RenderWindow->GetRenderers()->InitTraversal();
949 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
950 vtkActorCollection* theActors = aren->GetActors();
951 theActors->InitTraversal();
952 vtkActor *ac = theActors->GetNextActor();
954 if ( ac->IsA("SALOME_Actor") ) {
955 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
956 if ( anActor->hasIO() ) {
957 if ( IObject->isSame( anActor->getIO() ) ) {
958 return anActor->GetVisibility();
962 ac = theActors->GetNextActor();
968 void VTKViewer_RenderWindowInteractor::rename(const Handle(SALOME_InteractiveObject)& IObject, QString newName)
971 for (this->RenderWindow->GetRenderers()->InitTraversal();
972 (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
973 vtkActorCollection* theActors = aren->GetActors();
974 theActors->InitTraversal();
975 vtkActor *ac = theActors->GetNextActor();
977 if ( ac->IsA("SALOME_Actor") ) {
978 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
979 if ( anActor->hasIO() ) {
980 if ( IObject->isSame( anActor->getIO() ) ) {
981 anActor->setName(strdup(newName));
985 ac = theActors->GetNextActor();
989 emit RenderWindowModified() ;
992 bool VTKViewer_RenderWindowInteractor::highlight(const Handle(SALOME_InteractiveObject)& IObject,
993 const TColStd_MapOfInteger& MapIndex,
994 VTKViewer_RenderWindowInteractor::TCreateMapperFun theFun,
995 vtkActor *theActor, bool hilight, bool update )
997 if(MapIndex.Extent() == 0) return false;
999 this->RenderWindow->GetRenderers()->InitTraversal();
1000 for (; aRen = this->RenderWindow->GetRenderers()->GetNextItem(); ) {
1001 vtkActorCollection* anActorColl = aRen->GetActors();
1002 if ( anActorColl->IsItemPresent(theActor) != 0 )
1003 aRen->RemoveActor(theActor);
1004 anActorColl->InitTraversal();
1005 vtkActor *ac = NULL;
1006 for(; (ac = anActorColl->GetNextActor()) != NULL; ){
1007 if(ac->IsA("SALOME_Actor")){
1008 SALOME_Actor* anActor = SALOME_Actor::SafeDownCast(ac);
1009 if(anActor->hasIO()){
1010 if(IObject->isSame(anActor->getIO())){
1011 if(vtkPolyData* aSourcePolyData = anActor->GetPolyDataInput()){
1012 vtkPolyDataMapper* aMapper = vtkPolyDataMapper::New();
1013 (*theFun)(aSourcePolyData,aMapper,MapIndex);
1014 theActor->SetMapper(aMapper);
1016 if(anActorColl->IsItemPresent(theActor) == 0)
1017 aRen->AddActor(theActor);
1019 theActor->VisibilityOn();
1021 theActor->VisibilityOff();
1030 this->RenderWindow->Render();
1031 emit RenderWindowModified() ;
1036 static void CellCreateMapper(vtkPolyData *theSourcePolyData, vtkPolyDataMapper* theMapper,
1037 const TColStd_MapOfInteger& theMapIndex)
1039 vtkPolyData* aPolyData = vtkPolyData::New();
1040 aPolyData->Allocate();
1042 vtkIdList *ptIds = vtkIdList::New();
1043 ptIds->Allocate(theSourcePolyData->GetMaxCellSize());
1044 vtkPolyDataMapper* aMapper = vtkPolyDataMapper::New();
1045 aPolyData->SetPoints(theSourcePolyData->GetPoints());
1046 TColStd_MapIteratorOfMapOfInteger ite(theMapIndex);
1047 int aNbOfParts = theSourcePolyData->GetNumberOfCells();
1048 for(; ite.More(); ite.Next()){
1049 int aPartId = ite.Key();
1050 if(0 > aPartId || aPartId >= aNbOfParts) break;
1051 theSourcePolyData->GetCellPoints(aPartId,ptIds);
1052 vtkCell* aCell = theSourcePolyData->GetCell(aPartId);
1053 aPolyData->InsertNextCell(aCell->GetCellType(),ptIds);
1054 for (int i = 0, iEnd = aCell->GetNumberOfEdges(); i < iEnd; i++){
1055 vtkCell* anEdgeCell = aCell->GetEdge(i);
1056 aPolyData->InsertNextCell(VTK_LINE,anEdgeCell->GetPointIds());
1060 theMapper->SetInput(aPolyData);
1061 aPolyData->Delete();
1064 bool VTKViewer_RenderWindowInteractor::highlightCell(const Handle(SALOME_InteractiveObject)& IObject,
1066 const TColStd_MapOfInteger& MapIndex,
1069 return highlight(IObject,MapIndex,&CellCreateMapper,Cell_Actor,hilight,update);
1073 static void PointCreateMapper(vtkPolyData *theSourcePolyData, vtkPolyDataMapper* theMapper,
1074 const TColStd_MapOfInteger& theMapIndex)
1076 vtkPolyData* aPolyData = vtkPolyData::New();
1077 aPolyData->Allocate();
1079 vtkPoints *aPoints = vtkPoints::New();
1080 vtkMaskPoints* aMaskPoints = vtkMaskPoints::New();
1081 TColStd_MapIteratorOfMapOfInteger ite(theMapIndex);
1082 aPoints->SetNumberOfPoints(theMapIndex.Extent());
1083 int aNbOfParts = theSourcePolyData->GetNumberOfPoints();
1084 for(int i = 0; ite.More(); ite.Next(),i++){
1085 int aPartId = ite.Key();
1086 if(0 > aPartId || aPartId >= aNbOfParts) break;
1087 aPoints->SetPoint(i,theSourcePolyData->GetPoint(aPartId));
1089 aPolyData->SetPoints(aPoints);
1091 aMaskPoints->SetInput(aPolyData);
1092 aMaskPoints->SetGenerateVertices(1);
1093 aMaskPoints->SetOnRatio(1);
1094 theMapper->SetInput(aMaskPoints->GetOutput());
1095 aMaskPoints->Delete();
1098 bool VTKViewer_RenderWindowInteractor::highlightPoint(const Handle(SALOME_InteractiveObject)& IObject,
1099 bool hilight, const TColStd_MapOfInteger& MapIndex,
1102 return highlight(IObject,MapIndex,&PointCreateMapper,Point_Actor,hilight,update);
1106 static void EdgeCreateMapper(vtkPolyData *theSourcePolyData, vtkPolyDataMapper* theMapper,
1107 const TColStd_MapOfInteger& theMapIndex)
1109 vtkPolyData* aPolyData = vtkPolyData::New();
1110 aPolyData->Allocate();
1112 vtkIdList *ptIds = vtkIdList::New();
1113 ptIds->Allocate(theSourcePolyData->GetMaxCellSize());
1114 vtkPolyDataMapper* aMapper = vtkPolyDataMapper::New();
1115 aPolyData->SetPoints(theSourcePolyData->GetPoints());
1116 int aCellId = -1, aCellCounter = 0;
1117 for(TColStd_MapIteratorOfMapOfInteger ite(theMapIndex); ite.More(); ite.Next()){
1119 aCellId = ite.Key();
1123 if(aCellCounter == 1){
1124 vtkCell* aCell = theSourcePolyData->GetCell(aCellId);
1125 if(aCell->GetCellType() <= VTK_LINE){
1126 aPolyData->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
1128 TColStd_MapIteratorOfMapOfInteger ite(theMapIndex);
1129 int aNbOfParts = aCell->GetNumberOfEdges();
1130 for(; ite.More(); ite.Next()){
1132 int aPartId = -ite.Key()-1;
1133 if(0 > aPartId || aPartId >= aNbOfParts) break;
1134 vtkCell* anEdgeCell = aCell->GetEdge(aPartId);
1135 aPolyData->InsertNextCell(VTK_LINE,anEdgeCell->GetPointIds());
1140 int aNbOfParts = theSourcePolyData->GetNumberOfCells();
1141 for(TColStd_MapIteratorOfMapOfInteger ite(theMapIndex); ite.More(); ite.Next()){
1143 int aPartId = ite.Key();
1144 if(0 > aPartId || aPartId >= aNbOfParts) break;
1145 vtkCell* aCell = theSourcePolyData->GetCell(aPartId);
1146 aPolyData->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
1151 theMapper->SetInput(aPolyData);
1152 aPolyData->Delete();
1156 bool VTKViewer_RenderWindowInteractor::highlightEdge( const Handle(SALOME_InteractiveObject)& IObject,
1158 const TColStd_MapOfInteger& MapIndex,
1161 return highlight(IObject,MapIndex,&EdgeCreateMapper,Edge_Actor,hilight,update);