1 #include <cpExtensions/QT/RendererWidget.h>
3 #ifdef cpExtensions_QT4
5 #include <vtkAxesActor.h>
7 #include <vtkImageProperty.h>
8 #include <vtkInteractorStyle.h>
9 #include <vtkOrientationMarkerWidget.h>
10 #include <vtkProperty.h>
11 #include <vtkRenderer.h>
12 #include <vtkRenderWindow.h>
13 #include <cpExtensions/Visualization/LUTImageActor.h>
14 #include <cpExtensions/Visualization/WindowLevelImageActor.h>
16 // -------------------------------------------------------------------------
17 cpExtensions::QT::RendererWidget::
18 RendererWidget( QWidget* parent, Qt::WindowFlags f )
19 : Superclass( parent, f ),
22 this->m_Renderer = vtkSmartPointer< vtkRenderer >::New( );
23 this->GetRenderWindow( )->AddRenderer( this->m_Renderer );
25 vtkAxesActor* axes = vtkAxesActor::New( );
26 axes->AxisLabelsOff( );
27 this->m_Marker = vtkSmartPointer< vtkOrientationMarkerWidget >::New( );
28 this->m_Marker->SetOutlineColor( 1, 1, 1 );
29 this->m_Marker->SetOrientationMarker( axes );
30 this->m_Marker->SetInteractor( this->GetRenderWindow( )->GetInteractor( ) );
31 this->m_Marker->EnabledOn( );
32 this->m_Marker->InteractiveOff( );
34 this->SetQuadrant( 0 );
37 // -------------------------------------------------------------------------
38 cpExtensions::QT::RendererWidget::
43 // -------------------------------------------------------------------------
44 int cpExtensions::QT::RendererWidget::
47 return( this->m_Quadrant );
50 // -------------------------------------------------------------------------
51 void cpExtensions::QT::RendererWidget::
54 this->m_Quadrant = ( q - 1 ) % 4;
55 if( this->m_Quadrant == 0 )
56 this->m_Marker->SetViewport( 0.85, 0.00, 1.00, 0.15 );
57 else if( this->m_Quadrant == 1 )
58 this->m_Marker->SetViewport( 0.00, 0.00, 0.15, 0.15 );
59 else if( this->m_Quadrant == 2 )
60 this->m_Marker->SetViewport( 0.00, 0.85, 0.15, 1.00 );
61 else if( this->m_Quadrant == 3 )
62 this->m_Marker->SetViewport( 0.85, 0.85, 1.00, 1.00 );
65 // -------------------------------------------------------------------------
66 vtkRenderer* cpExtensions::QT::RendererWidget::
69 return( this->m_Renderer );
72 // -------------------------------------------------------------------------
73 const vtkRenderer* cpExtensions::QT::RendererWidget::
76 return( this->m_Renderer );
79 // -------------------------------------------------------------------------
80 vtkInteractorStyle* cpExtensions::QT::RendererWidget::
83 auto iren = this->GetInteractor( );
86 dynamic_cast< vtkInteractorStyle* >( iren->GetInteractorStyle( ) )
92 // -------------------------------------------------------------------------
93 const vtkInteractorStyle* cpExtensions::QT::RendererWidget::
96 // Ugly, but necessary :-(
97 Self* self = const_cast< Self* >( this );
100 auto iren = self->GetInteractor( );
103 dynamic_cast< const vtkInteractorStyle* >(
104 iren->GetInteractorStyle( )
114 // -------------------------------------------------------------------------
115 void cpExtensions::QT::RendererWidget::
116 SetStyle( vtkInteractorStyle* style )
118 this->GetInteractor( )->SetInteractorStyle( style );
121 // -------------------------------------------------------------------------
122 vtkCamera* cpExtensions::QT::RendererWidget::
125 return( this->m_Renderer->GetActiveCamera( ) );
128 // -------------------------------------------------------------------------
129 const vtkCamera* cpExtensions::QT::RendererWidget::
130 GetActiveCamera( ) const
132 return( this->m_Renderer->GetActiveCamera( ) );
135 // -------------------------------------------------------------------------
136 void cpExtensions::QT::RendererWidget::
137 AddViewProp( vtkProp* prop, const std::string& name )
141 auto i = this->m_ViewProps.find( name );
142 if( i == this->m_ViewProps.end( ) )
144 this->m_ViewProps.insert(
145 TPropCollection::value_type( name, TProps( ) )
147 i->second.insert( prop );
148 this->m_Renderer->AddViewProp( prop );
153 // -------------------------------------------------------------------------
154 void cpExtensions::QT::RendererWidget::
155 AddViewProps( vtkPropCollection* props, const std::string& name )
159 auto i = this->m_ViewProps.find( name );
160 if( i == this->m_ViewProps.end( ) )
162 this->m_ViewProps.insert(
163 TPropCollection::value_type( name, TProps( ) )
165 props->InitTraversal( );
166 while( vtkProp* prop = props->GetNextProp( ) )
168 i->second.insert( prop );
169 this->m_Renderer->AddViewProp( prop );
176 // -------------------------------------------------------------------------
177 void cpExtensions::QT::RendererWidget::
178 AddAuxViewProp( vtkProp* prop, const std::string& name )
182 auto i = this->m_AuxViewProps.find( name );
183 if( i == this->m_AuxViewProps.end( ) )
185 this->m_AuxViewProps.insert(
186 TPropCollection::value_type( name, TProps( ) )
188 i->second.insert( prop );
189 this->m_Renderer->AddViewProp( prop );
194 // -------------------------------------------------------------------------
195 void cpExtensions::QT::RendererWidget::
196 AddAuxViewProps( vtkPropCollection* props, const std::string& name )
200 auto i = this->m_AuxViewProps.find( name );
201 if( i == this->m_AuxViewProps.end( ) )
203 this->m_AuxViewProps.insert(
204 TPropCollection::value_type( name, TProps( ) )
206 props->InitTraversal( );
207 while( vtkProp* prop = props->GetNextProp( ) )
209 i->second.insert( prop );
210 this->m_Renderer->AddViewProp( prop );
217 // -------------------------------------------------------------------------
218 cpExtensions::QT::RendererWidget::
219 TProps& cpExtensions::QT::RendererWidget::
220 GetViewProps( const std::string& name )
223 auto i = this->m_ViewProps.find( name );
224 if( i == this->m_ViewProps.end( ) )
233 // -------------------------------------------------------------------------
234 const cpExtensions::QT::RendererWidget::
235 TProps& cpExtensions::QT::RendererWidget::
236 GetViewProps( const std::string& name ) const
238 static const TProps zero;
239 auto i = this->m_ViewProps.find( name );
240 if( i == this->m_ViewProps.end( ) )
246 // -------------------------------------------------------------------------
247 cpExtensions::QT::RendererWidget::
248 TProps& cpExtensions::QT::RendererWidget::
249 GetAuxViewProps( const std::string& name )
252 auto i = this->m_AuxViewProps.find( name );
253 if( i == this->m_AuxViewProps.end( ) )
262 // -------------------------------------------------------------------------
263 const cpExtensions::QT::RendererWidget::
264 TProps& cpExtensions::QT::RendererWidget::
265 GetAuxViewProps( const std::string& name ) const
267 static const TProps zero;
268 auto i = this->m_AuxViewProps.find( name );
269 if( i == this->m_AuxViewProps.end( ) )
275 // -------------------------------------------------------------------------
276 void cpExtensions::QT::RendererWidget::
277 RemoveViewProps( const std::string& name )
279 auto i = this->m_ViewProps.find( name );
280 if( i != this->m_ViewProps.end( ) )
282 for( auto p = i->second.begin( ); p != i->second.end( ); ++p )
283 this->m_Renderer->RemoveViewProp( *p );
284 this->m_ViewProps.erase( i );
288 i = this->m_AuxViewProps.find( name );
289 if( i != this->m_AuxViewProps.end( ) )
291 for( auto p = i->second.begin( ); p != i->second.end( ); ++p )
292 this->m_Renderer->RemoveViewProp( *p );
293 this->m_AuxViewProps.erase( i );
298 // -------------------------------------------------------------------------
299 void cpExtensions::QT::RendererWidget::
302 this->m_Renderer->RemoveAllViewProps( );
303 this->m_ViewProps.clear( );
304 this->m_AuxViewProps.clear( );
307 // -------------------------------------------------------------------------
308 void cpExtensions::QT::RendererWidget::
309 HideViewProps( const std::string& name )
311 auto i = this->m_ViewProps.find( name );
312 if( i != this->m_ViewProps.end( ) )
313 for( auto p = i->second.begin( ); p != i->second.end( ); ++p )
314 ( *p )->VisibilityOff( );
315 i = this->m_AuxViewProps.find( name );
316 if( i != this->m_AuxViewProps.end( ) )
317 for( auto p = i->second.begin( ); p != i->second.end( ); ++p )
318 ( *p )->VisibilityOff( );
321 // -------------------------------------------------------------------------
322 void cpExtensions::QT::RendererWidget::
323 ShowViewProps( const std::string& name )
325 auto i = this->m_ViewProps.find( name );
326 if( i != this->m_ViewProps.end( ) )
327 for( auto p = i->second.begin( ); p != i->second.end( ); ++p )
328 ( *p )->VisibilityOn( );
329 i = this->m_AuxViewProps.find( name );
330 if( i != this->m_AuxViewProps.end( ) )
331 for( auto p = i->second.begin( ); p != i->second.end( ); ++p )
332 ( *p )->VisibilityOn( );
335 // -------------------------------------------------------------------------
336 void cpExtensions::QT::RendererWidget::
339 this->m_Renderer->ResetCamera( );
342 // -------------------------------------------------------------------------
343 void cpExtensions::QT::RendererWidget::
346 this->GetRenderWindow( )->Render( );
349 // -------------------------------------------------------------------------
350 std::set< vtkRenderWindowInteractor* > cpExtensions::QT::RendererWidget::
351 GetInteractors( ) const
353 Self* self = const_cast< Self* >( this );
354 std::set< vtkRenderWindowInteractor* > ret;
355 ret.insert( self->GetRenderWindow( )->GetInteractor( ) );
359 // -------------------------------------------------------------------------
360 std::set< std::string > cpExtensions::QT::RendererWidget::
361 GetActorsNames( ) const
363 std::set< std::string > names;
365 auto p = this->m_ViewProps.begin( );
366 p != this->m_ViewProps.end( );
369 names.insert( p->first );
373 // -------------------------------------------------------------------------
374 bool cpExtensions::QT::RendererWidget::
375 IsWindowLevelImageActor( const std::string& name ) const
377 auto it = this->m_ViewProps.find( name );
378 if( it != this->m_ViewProps.end( ) )
380 auto a = it->second.begin( );
381 if( a != it->second.end( ) )
382 return( dynamic_cast< TWLActor* >( a->GetPointer( ) ) != NULL );
390 // -------------------------------------------------------------------------
391 bool cpExtensions::QT::RendererWidget::
392 IsLUTImageActor( const std::string& name ) const
394 auto it = this->m_ViewProps.find( name );
395 if( it != this->m_ViewProps.end( ) )
397 auto a = it->second.begin( );
398 if( a != it->second.end( ) )
399 return( dynamic_cast< TLUTActor* >( a->GetPointer( ) ) != NULL );
407 // -------------------------------------------------------------------------
408 bool cpExtensions::QT::RendererWidget::
409 Is3DActor( const std::string& name ) const
411 auto it = this->m_ViewProps.find( name );
412 if( it != this->m_ViewProps.end( ) )
414 auto a = it->second.begin( );
415 if( a != it->second.end( ) )
416 return( dynamic_cast< vtkActor* >( a->GetPointer( ) ) != NULL );
424 // -------------------------------------------------------------------------
425 void cpExtensions::QT::RendererWidget::
426 GetScalarRange( const std::string& name, double r[ 2 ] ) const
428 auto it = this->m_ViewProps.find( name );
429 if( it != this->m_ViewProps.end( ) )
431 auto p = it->second.begin( );
433 while( a == NULL && p != it->second.end( ) )
435 a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
445 // -------------------------------------------------------------------------
446 void cpExtensions::QT::RendererWidget::
447 GetWindowLevel( const std::string& name, double wl[ 2 ] ) const
449 auto it = this->m_ViewProps.find( name );
450 if( it != this->m_ViewProps.end( ) )
452 auto p = it->second.begin( );
454 while( a == NULL && p != it->second.end( ) )
456 a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
461 a->GetWindowLevel( wl );
466 // -------------------------------------------------------------------------
467 double cpExtensions::QT::RendererWidget::
468 GetWindow( const std::string& name ) const
470 auto it = this->m_ViewProps.find( name );
471 if( it != this->m_ViewProps.end( ) )
473 auto p = it->second.begin( );
475 while( a == NULL && p != it->second.end( ) )
477 a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
482 return( a->GetWindow( ) );
490 // -------------------------------------------------------------------------
491 double cpExtensions::QT::RendererWidget::
492 GetLevel( const std::string& name ) const
494 auto it = this->m_ViewProps.find( name );
495 if( it != this->m_ViewProps.end( ) )
497 auto p = it->second.begin( );
499 while( a == NULL && p != it->second.end( ) )
501 a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
506 return( a->GetLevel( ) );
514 // -------------------------------------------------------------------------
515 char cpExtensions::QT::RendererWidget::
516 GetImageInterpolation( const std::string& name ) const
518 auto it = this->m_ViewProps.find( name );
519 if( it != this->m_ViewProps.end( ) )
521 auto p = it->second.begin( );
523 while( a == NULL && p != it->second.end( ) )
525 a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
531 int int_type = a->GetProperty( )->GetInterpolationType( );
535 case VTK_NEAREST_INTERPOLATION: ret = 'N'; break;
536 case VTK_LINEAR_INTERPOLATION: ret = 'L'; break;
537 case VTK_CUBIC_INTERPOLATION: ret = 'C'; break;
538 default: ret = 0; break;
549 // -------------------------------------------------------------------------
550 void cpExtensions::QT::RendererWidget::
551 GetColor( const std::string& name, double& r, double& g, double& b ) const
553 auto it = this->m_ViewProps.find( name );
554 if( it != this->m_ViewProps.end( ) )
556 auto p = it->second.begin( );
558 while( a == NULL && p != it->second.end( ) )
560 a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
565 return( a->GetProperty( )->GetColor( r, g, b ) );
570 // -------------------------------------------------------------------------
571 double cpExtensions::QT::RendererWidget::
572 GetOpacity( const std::string& name ) const
574 auto it = this->m_ViewProps.find( name );
575 if( it != this->m_ViewProps.end( ) )
577 auto p = it->second.begin( );
579 vtkImageSlice* s = NULL;
580 while( a == NULL && s == NULL && p != it->second.end( ) )
582 a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
583 s = dynamic_cast< vtkImageSlice* >( p->GetPointer( ) );
588 return( a->GetProperty( )->GetOpacity( ) );
590 return( s->GetProperty( )->GetOpacity( ) );
597 // -------------------------------------------------------------------------
598 double cpExtensions::QT::RendererWidget::
599 GetPointSize( const std::string& name ) const
601 auto it = this->m_ViewProps.find( name );
602 if( it != this->m_ViewProps.end( ) )
604 auto p = it->second.begin( );
606 while( a == NULL && p != it->second.end( ) )
608 a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
613 return( a->GetProperty( )->GetPointSize( ) );
620 // -------------------------------------------------------------------------
621 double cpExtensions::QT::RendererWidget::
622 GetLineWidth( const std::string& name ) const
624 auto it = this->m_ViewProps.find( name );
625 if( it != this->m_ViewProps.end( ) )
627 auto p = it->second.begin( );
629 while( a == NULL && p != it->second.end( ) )
631 a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
636 return( a->GetProperty( )->GetLineWidth( ) );
643 // -------------------------------------------------------------------------
644 int cpExtensions::QT::RendererWidget::
645 GetRepresentation( const std::string& name ) const
647 auto it = this->m_ViewProps.find( name );
648 if( it != this->m_ViewProps.end( ) )
650 auto p = it->second.begin( );
652 while( a == NULL && p != it->second.end( ) )
654 a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
659 return( a->GetProperty( )->GetRepresentation( ) );
666 // -------------------------------------------------------------------------
667 void cpExtensions::QT::RendererWidget::
668 SetScalarRange( const std::string& name, double r[ 2 ] )
670 this->SetScalarRange( name, r[ 0 ], r[ 1 ] );
673 // -------------------------------------------------------------------------
674 void cpExtensions::QT::RendererWidget::
675 SetScalarRange( const std::string& name, double min, double max )
677 auto it = this->m_ViewProps.find( name );
678 if( it != this->m_ViewProps.end( ) )
681 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
683 TWLActor* a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
687 a->SetRange( min, max );
698 // -------------------------------------------------------------------------
699 void cpExtensions::QT::RendererWidget::
700 SetWindowLevel( const std::string& name, double wl[ 2 ] )
702 this->SetWindowLevel( name, wl[ 0 ], wl[ 1 ] );
705 // -------------------------------------------------------------------------
706 void cpExtensions::QT::RendererWidget::
707 SetWindowLevel( const std::string& name, double w, double l )
709 auto it = this->m_ViewProps.find( name );
710 if( it != this->m_ViewProps.end( ) )
713 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
715 TWLActor* a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
719 a->SetWindowLevel( w, l );
730 // -------------------------------------------------------------------------
731 void cpExtensions::QT::RendererWidget::
732 SetWindow( const std::string& name, double w )
734 auto it = this->m_ViewProps.find( name );
735 if( it != this->m_ViewProps.end( ) )
738 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
740 TWLActor* a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
755 // -------------------------------------------------------------------------
756 void cpExtensions::QT::RendererWidget::
757 SetLevel( const std::string& name, double l )
759 auto it = this->m_ViewProps.find( name );
760 if( it != this->m_ViewProps.end( ) )
763 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
765 TWLActor* a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
780 // -------------------------------------------------------------------------
781 void cpExtensions::QT::RendererWidget::
782 SetImageInterpolation( const std::string& name, char i )
784 auto it = this->m_ViewProps.find( name );
785 if( it != this->m_ViewProps.end( ) )
788 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
790 TWLActor* a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
794 int int_type = VTK_NEAREST_INTERPOLATION;
797 case 'L': int_type = VTK_LINEAR_INTERPOLATION; break;
798 case 'C': int_type = VTK_CUBIC_INTERPOLATION; break;
799 default: int_type = VTK_NEAREST_INTERPOLATION; break;
801 a->GetProperty( )->SetInterpolationType( int_type );
812 // -------------------------------------------------------------------------
813 void cpExtensions::QT::RendererWidget::
814 SetColor( const std::string& name, double r, double g, double b )
816 auto it = this->m_ViewProps.find( name );
817 if( it != this->m_ViewProps.end( ) )
820 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
822 vtkActor* a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
826 a->GetProperty( )->SetColor( r, g, b );
837 // -------------------------------------------------------------------------
838 void cpExtensions::QT::RendererWidget::
839 SetOpacity( const std::string& name, double o )
841 auto it = this->m_ViewProps.find( name );
842 if( it != this->m_ViewProps.end( ) )
845 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
847 vtkActor* a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
848 vtkImageSlice* s = dynamic_cast< vtkImageSlice* >( p->GetPointer( ) );
852 a->GetProperty( )->SetOpacity( o );
857 s->GetProperty( )->SetOpacity( o );
868 // -------------------------------------------------------------------------
869 void cpExtensions::QT::RendererWidget::
870 SetPointSize( const std::string& name, double s )
872 auto it = this->m_ViewProps.find( name );
873 if( it != this->m_ViewProps.end( ) )
876 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
878 vtkActor* a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
882 a->GetProperty( )->SetPointSize( s );
893 // -------------------------------------------------------------------------
894 void cpExtensions::QT::RendererWidget::
895 SetLineWidth( const std::string& name, double w )
897 auto it = this->m_ViewProps.find( name );
898 if( it != this->m_ViewProps.end( ) )
901 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
903 vtkActor* a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
907 a->GetProperty( )->SetLineWidth( w );
918 // -------------------------------------------------------------------------
919 void cpExtensions::QT::RendererWidget::
920 SetRepresentationToPoints( const std::string& name )
922 auto it = this->m_ViewProps.find( name );
923 if( it != this->m_ViewProps.end( ) )
926 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
928 vtkActor* a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
932 a->GetProperty( )->SetRepresentationToPoints( );
943 // -------------------------------------------------------------------------
944 void cpExtensions::QT::RendererWidget::
945 SetRepresentationToSurface( const std::string& name )
947 auto it = this->m_ViewProps.find( name );
948 if( it != this->m_ViewProps.end( ) )
951 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
953 vtkActor* a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
957 a->GetProperty( )->SetRepresentationToSurface( );
968 // -------------------------------------------------------------------------
969 void cpExtensions::QT::RendererWidget::
970 SetRepresentationToWireframe( const std::string& name )
972 auto it = this->m_ViewProps.find( name );
973 if( it != this->m_ViewProps.end( ) )
976 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
978 vtkActor* a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
982 a->GetProperty( )->SetRepresentationToWireframe( );
993 #endif // cpExtensions_QT4