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< std::string > cpExtensions::QT::RendererWidget::
351 GetActorsNames( ) const
353 std::set< std::string > names;
355 auto p = this->m_ViewProps.begin( );
356 p != this->m_ViewProps.end( );
359 names.insert( p->first );
363 // -------------------------------------------------------------------------
364 bool cpExtensions::QT::RendererWidget::
365 IsWindowLevelImageActor( const std::string& name ) const
367 auto it = this->m_ViewProps.find( name );
368 if( it != this->m_ViewProps.end( ) )
370 auto a = it->second.begin( );
371 if( a != it->second.end( ) )
372 return( dynamic_cast< TWLActor* >( a->GetPointer( ) ) != NULL );
380 // -------------------------------------------------------------------------
381 bool cpExtensions::QT::RendererWidget::
382 IsLUTImageActor( const std::string& name ) const
384 auto it = this->m_ViewProps.find( name );
385 if( it != this->m_ViewProps.end( ) )
387 auto a = it->second.begin( );
388 if( a != it->second.end( ) )
389 return( dynamic_cast< TLUTActor* >( a->GetPointer( ) ) != NULL );
397 // -------------------------------------------------------------------------
398 bool cpExtensions::QT::RendererWidget::
399 Is3DActor( const std::string& name ) const
401 auto it = this->m_ViewProps.find( name );
402 if( it != this->m_ViewProps.end( ) )
404 auto a = it->second.begin( );
405 if( a != it->second.end( ) )
406 return( dynamic_cast< vtkActor* >( a->GetPointer( ) ) != NULL );
414 // -------------------------------------------------------------------------
415 void cpExtensions::QT::RendererWidget::
416 GetScalarRange( const std::string& name, double r[ 2 ] ) const
418 auto it = this->m_ViewProps.find( name );
419 if( it != this->m_ViewProps.end( ) )
421 auto p = it->second.begin( );
423 while( a == NULL && p != it->second.end( ) )
425 a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
435 // -------------------------------------------------------------------------
436 void cpExtensions::QT::RendererWidget::
437 GetWindowLevel( const std::string& name, double wl[ 2 ] ) const
439 auto it = this->m_ViewProps.find( name );
440 if( it != this->m_ViewProps.end( ) )
442 auto p = it->second.begin( );
444 while( a == NULL && p != it->second.end( ) )
446 a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
451 a->GetWindowLevel( wl );
456 // -------------------------------------------------------------------------
457 double cpExtensions::QT::RendererWidget::
458 GetWindow( const std::string& name ) const
460 auto it = this->m_ViewProps.find( name );
461 if( it != this->m_ViewProps.end( ) )
463 auto p = it->second.begin( );
465 while( a == NULL && p != it->second.end( ) )
467 a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
472 return( a->GetWindow( ) );
480 // -------------------------------------------------------------------------
481 double cpExtensions::QT::RendererWidget::
482 GetLevel( const std::string& name ) const
484 auto it = this->m_ViewProps.find( name );
485 if( it != this->m_ViewProps.end( ) )
487 auto p = it->second.begin( );
489 while( a == NULL && p != it->second.end( ) )
491 a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
496 return( a->GetLevel( ) );
504 // -------------------------------------------------------------------------
505 char cpExtensions::QT::RendererWidget::
506 GetImageInterpolation( const std::string& name ) const
508 auto it = this->m_ViewProps.find( name );
509 if( it != this->m_ViewProps.end( ) )
511 auto p = it->second.begin( );
513 while( a == NULL && p != it->second.end( ) )
515 a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
521 int int_type = a->GetProperty( )->GetInterpolationType( );
525 case VTK_NEAREST_INTERPOLATION: ret = 'N'; break;
526 case VTK_LINEAR_INTERPOLATION: ret = 'L'; break;
527 case VTK_CUBIC_INTERPOLATION: ret = 'C'; break;
528 default: ret = 0; break;
539 // -------------------------------------------------------------------------
540 void cpExtensions::QT::RendererWidget::
541 GetColor( const std::string& name, double& r, double& g, double& b ) const
543 auto it = this->m_ViewProps.find( name );
544 if( it != this->m_ViewProps.end( ) )
546 auto p = it->second.begin( );
548 while( a == NULL && p != it->second.end( ) )
550 a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
555 return( a->GetProperty( )->GetColor( r, g, b ) );
560 // -------------------------------------------------------------------------
561 double cpExtensions::QT::RendererWidget::
562 GetOpacity( const std::string& name ) const
564 auto it = this->m_ViewProps.find( name );
565 if( it != this->m_ViewProps.end( ) )
567 auto p = it->second.begin( );
569 vtkImageSlice* s = NULL;
570 while( a == NULL && s == NULL && p != it->second.end( ) )
572 a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
573 s = dynamic_cast< vtkImageSlice* >( p->GetPointer( ) );
578 return( a->GetProperty( )->GetOpacity( ) );
580 return( s->GetProperty( )->GetOpacity( ) );
587 // -------------------------------------------------------------------------
588 double cpExtensions::QT::RendererWidget::
589 GetPointSize( const std::string& name ) const
591 auto it = this->m_ViewProps.find( name );
592 if( it != this->m_ViewProps.end( ) )
594 auto p = it->second.begin( );
596 while( a == NULL && p != it->second.end( ) )
598 a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
603 return( a->GetProperty( )->GetPointSize( ) );
610 // -------------------------------------------------------------------------
611 double cpExtensions::QT::RendererWidget::
612 GetLineWidth( const std::string& name ) const
614 auto it = this->m_ViewProps.find( name );
615 if( it != this->m_ViewProps.end( ) )
617 auto p = it->second.begin( );
619 while( a == NULL && p != it->second.end( ) )
621 a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
626 return( a->GetProperty( )->GetLineWidth( ) );
633 // -------------------------------------------------------------------------
634 int cpExtensions::QT::RendererWidget::
635 GetRepresentation( const std::string& name ) const
637 auto it = this->m_ViewProps.find( name );
638 if( it != this->m_ViewProps.end( ) )
640 auto p = it->second.begin( );
642 while( a == NULL && p != it->second.end( ) )
644 a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
649 return( a->GetProperty( )->GetRepresentation( ) );
656 // -------------------------------------------------------------------------
657 void cpExtensions::QT::RendererWidget::
658 SetScalarRange( const std::string& name, double r[ 2 ] )
660 this->SetScalarRange( name, r[ 0 ], r[ 1 ] );
663 // -------------------------------------------------------------------------
664 void cpExtensions::QT::RendererWidget::
665 SetScalarRange( const std::string& name, double min, double max )
667 auto it = this->m_ViewProps.find( name );
668 if( it != this->m_ViewProps.end( ) )
671 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
673 TWLActor* a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
677 a->SetRange( min, max );
688 // -------------------------------------------------------------------------
689 void cpExtensions::QT::RendererWidget::
690 SetWindowLevel( const std::string& name, double wl[ 2 ] )
692 this->SetWindowLevel( name, wl[ 0 ], wl[ 1 ] );
695 // -------------------------------------------------------------------------
696 void cpExtensions::QT::RendererWidget::
697 SetWindowLevel( const std::string& name, double w, double l )
699 auto it = this->m_ViewProps.find( name );
700 if( it != this->m_ViewProps.end( ) )
703 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
705 TWLActor* a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
709 a->SetWindowLevel( w, l );
720 // -------------------------------------------------------------------------
721 void cpExtensions::QT::RendererWidget::
722 SetWindow( const std::string& name, double w )
724 auto it = this->m_ViewProps.find( name );
725 if( it != this->m_ViewProps.end( ) )
728 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
730 TWLActor* a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
745 // -------------------------------------------------------------------------
746 void cpExtensions::QT::RendererWidget::
747 SetLevel( const std::string& name, double l )
749 auto it = this->m_ViewProps.find( name );
750 if( it != this->m_ViewProps.end( ) )
753 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
755 TWLActor* a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
770 // -------------------------------------------------------------------------
771 void cpExtensions::QT::RendererWidget::
772 SetImageInterpolation( const std::string& name, char i )
774 auto it = this->m_ViewProps.find( name );
775 if( it != this->m_ViewProps.end( ) )
778 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
780 TWLActor* a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
784 int int_type = VTK_NEAREST_INTERPOLATION;
787 case 'L': int_type = VTK_LINEAR_INTERPOLATION; break;
788 case 'C': int_type = VTK_CUBIC_INTERPOLATION; break;
789 default: int_type = VTK_NEAREST_INTERPOLATION; break;
791 a->GetProperty( )->SetInterpolationType( int_type );
802 // -------------------------------------------------------------------------
803 void cpExtensions::QT::RendererWidget::
804 SetColor( const std::string& name, double r, double g, double b )
806 auto it = this->m_ViewProps.find( name );
807 if( it != this->m_ViewProps.end( ) )
810 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
812 vtkActor* a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
816 a->GetProperty( )->SetColor( r, g, b );
827 // -------------------------------------------------------------------------
828 void cpExtensions::QT::RendererWidget::
829 SetOpacity( const std::string& name, double o )
831 auto it = this->m_ViewProps.find( name );
832 if( it != this->m_ViewProps.end( ) )
835 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
837 vtkActor* a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
838 vtkImageSlice* s = dynamic_cast< vtkImageSlice* >( p->GetPointer( ) );
842 a->GetProperty( )->SetOpacity( o );
847 s->GetProperty( )->SetOpacity( o );
858 // -------------------------------------------------------------------------
859 void cpExtensions::QT::RendererWidget::
860 SetPointSize( const std::string& name, double s )
862 auto it = this->m_ViewProps.find( name );
863 if( it != this->m_ViewProps.end( ) )
866 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
868 vtkActor* a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
872 a->GetProperty( )->SetPointSize( s );
883 // -------------------------------------------------------------------------
884 void cpExtensions::QT::RendererWidget::
885 SetLineWidth( const std::string& name, double w )
887 auto it = this->m_ViewProps.find( name );
888 if( it != this->m_ViewProps.end( ) )
891 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
893 vtkActor* a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
897 a->GetProperty( )->SetLineWidth( w );
908 // -------------------------------------------------------------------------
909 void cpExtensions::QT::RendererWidget::
910 SetRepresentationToPoints( const std::string& name )
912 auto it = this->m_ViewProps.find( name );
913 if( it != this->m_ViewProps.end( ) )
916 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
918 vtkActor* a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
922 a->GetProperty( )->SetRepresentationToPoints( );
933 // -------------------------------------------------------------------------
934 void cpExtensions::QT::RendererWidget::
935 SetRepresentationToSurface( const std::string& name )
937 auto it = this->m_ViewProps.find( name );
938 if( it != this->m_ViewProps.end( ) )
941 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
943 vtkActor* a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
947 a->GetProperty( )->SetRepresentationToSurface( );
958 // -------------------------------------------------------------------------
959 void cpExtensions::QT::RendererWidget::
960 SetRepresentationToWireframe( const std::string& name )
962 auto it = this->m_ViewProps.find( name );
963 if( it != this->m_ViewProps.end( ) )
966 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
968 vtkActor* a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
972 a->GetProperty( )->SetRepresentationToWireframe( );
983 #endif // cpExtensions_QT4