1 #include <cpExtensions/QT/RendererWidget.h>
2 #include <vtkAxesActor.h>
4 #include <vtkImageProperty.h>
5 #include <vtkInteractorStyle.h>
6 #include <vtkOrientationMarkerWidget.h>
7 #include <vtkProperty.h>
8 #include <vtkRenderer.h>
9 #include <vtkRenderWindow.h>
10 #include <cpExtensions/Visualization/LUTImageActor.h>
11 #include <cpExtensions/Visualization/WindowLevelImageActor.h>
13 // -------------------------------------------------------------------------
14 cpExtensions::QT::RendererWidget::
15 RendererWidget( QWidget* parent, Qt::WindowFlags f )
16 : Superclass( parent, f ),
19 this->m_Renderer = vtkSmartPointer< vtkRenderer >::New( );
20 this->GetRenderWindow( )->AddRenderer( this->m_Renderer );
22 vtkAxesActor* axes = vtkAxesActor::New( );
23 axes->AxisLabelsOff( );
24 this->m_Marker = vtkSmartPointer< vtkOrientationMarkerWidget >::New( );
25 this->m_Marker->SetOutlineColor( 1, 1, 1 );
26 this->m_Marker->SetOrientationMarker( axes );
27 this->m_Marker->SetInteractor( this->GetRenderWindow( )->GetInteractor( ) );
28 this->m_Marker->EnabledOn( );
29 this->m_Marker->InteractiveOff( );
31 this->SetQuadrant( 0 );
34 // -------------------------------------------------------------------------
35 cpExtensions::QT::RendererWidget::
40 // -------------------------------------------------------------------------
41 int cpExtensions::QT::RendererWidget::
44 return( this->m_Quadrant );
47 // -------------------------------------------------------------------------
48 void cpExtensions::QT::RendererWidget::
51 this->m_Quadrant = ( q - 1 ) % 4;
52 if( this->m_Quadrant == 0 )
53 this->m_Marker->SetViewport( 0.85, 0.00, 1.00, 0.15 );
54 else if( this->m_Quadrant == 1 )
55 this->m_Marker->SetViewport( 0.00, 0.00, 0.15, 0.15 );
56 else if( this->m_Quadrant == 2 )
57 this->m_Marker->SetViewport( 0.00, 0.85, 0.15, 1.00 );
58 else if( this->m_Quadrant == 3 )
59 this->m_Marker->SetViewport( 0.85, 0.85, 1.00, 1.00 );
62 // -------------------------------------------------------------------------
63 vtkRenderer* cpExtensions::QT::RendererWidget::
66 return( this->m_Renderer );
69 // -------------------------------------------------------------------------
70 const vtkRenderer* cpExtensions::QT::RendererWidget::
73 return( this->m_Renderer );
76 // -------------------------------------------------------------------------
77 vtkInteractorStyle* cpExtensions::QT::RendererWidget::
80 auto iren = this->GetInteractor( );
83 dynamic_cast< vtkInteractorStyle* >( iren->GetInteractorStyle( ) )
89 // -------------------------------------------------------------------------
90 const vtkInteractorStyle* cpExtensions::QT::RendererWidget::
93 // Ugly, but necessary :-(
94 Self* self = const_cast< Self* >( this );
97 auto iren = self->GetInteractor( );
100 dynamic_cast< const vtkInteractorStyle* >(
101 iren->GetInteractorStyle( )
111 // -------------------------------------------------------------------------
112 void cpExtensions::QT::RendererWidget::
113 SetStyle( vtkInteractorStyle* style )
115 this->GetInteractor( )->SetInteractorStyle( style );
118 // -------------------------------------------------------------------------
119 vtkCamera* cpExtensions::QT::RendererWidget::
122 return( this->m_Renderer->GetActiveCamera( ) );
125 // -------------------------------------------------------------------------
126 const vtkCamera* cpExtensions::QT::RendererWidget::
127 GetActiveCamera( ) const
129 return( this->m_Renderer->GetActiveCamera( ) );
132 // -------------------------------------------------------------------------
133 void cpExtensions::QT::RendererWidget::
134 AddViewProp( vtkProp* prop, const std::string& name )
138 auto i = this->m_ViewProps.find( name );
139 if( i == this->m_ViewProps.end( ) )
141 this->m_ViewProps.insert(
142 TPropCollection::value_type( name, TProps( ) )
144 i->second.insert( prop );
145 this->m_Renderer->AddViewProp( prop );
150 // -------------------------------------------------------------------------
151 void cpExtensions::QT::RendererWidget::
152 AddViewProps( vtkPropCollection* props, const std::string& name )
156 auto i = this->m_ViewProps.find( name );
157 if( i == this->m_ViewProps.end( ) )
159 this->m_ViewProps.insert(
160 TPropCollection::value_type( name, TProps( ) )
162 props->InitTraversal( );
163 while( vtkProp* prop = props->GetNextProp( ) )
165 i->second.insert( prop );
166 this->m_Renderer->AddViewProp( prop );
173 // -------------------------------------------------------------------------
174 void cpExtensions::QT::RendererWidget::
175 AddAuxViewProp( vtkProp* prop, const std::string& name )
179 auto i = this->m_AuxViewProps.find( name );
180 if( i == this->m_AuxViewProps.end( ) )
182 this->m_AuxViewProps.insert(
183 TPropCollection::value_type( name, TProps( ) )
185 i->second.insert( prop );
186 this->m_Renderer->AddViewProp( prop );
191 // -------------------------------------------------------------------------
192 void cpExtensions::QT::RendererWidget::
193 AddAuxViewProps( vtkPropCollection* props, const std::string& name )
197 auto i = this->m_AuxViewProps.find( name );
198 if( i == this->m_AuxViewProps.end( ) )
200 this->m_AuxViewProps.insert(
201 TPropCollection::value_type( name, TProps( ) )
203 props->InitTraversal( );
204 while( vtkProp* prop = props->GetNextProp( ) )
206 i->second.insert( prop );
207 this->m_Renderer->AddViewProp( prop );
214 // -------------------------------------------------------------------------
215 cpExtensions::QT::RendererWidget::
216 TProps& cpExtensions::QT::RendererWidget::
217 GetViewProps( const std::string& name )
220 auto i = this->m_ViewProps.find( name );
221 if( i == this->m_ViewProps.end( ) )
230 // -------------------------------------------------------------------------
231 const cpExtensions::QT::RendererWidget::
232 TProps& cpExtensions::QT::RendererWidget::
233 GetViewProps( const std::string& name ) const
235 static const TProps zero;
236 auto i = this->m_ViewProps.find( name );
237 if( i == this->m_ViewProps.end( ) )
243 // -------------------------------------------------------------------------
244 cpExtensions::QT::RendererWidget::
245 TProps& cpExtensions::QT::RendererWidget::
246 GetAuxViewProps( const std::string& name )
249 auto i = this->m_AuxViewProps.find( name );
250 if( i == this->m_AuxViewProps.end( ) )
259 // -------------------------------------------------------------------------
260 const cpExtensions::QT::RendererWidget::
261 TProps& cpExtensions::QT::RendererWidget::
262 GetAuxViewProps( const std::string& name ) const
264 static const TProps zero;
265 auto i = this->m_AuxViewProps.find( name );
266 if( i == this->m_AuxViewProps.end( ) )
272 // -------------------------------------------------------------------------
273 void cpExtensions::QT::RendererWidget::
274 RemoveViewProps( const std::string& name )
276 auto i = this->m_ViewProps.find( name );
277 if( i != this->m_ViewProps.end( ) )
279 for( auto p = i->second.begin( ); p != i->second.end( ); ++p )
280 this->m_Renderer->RemoveViewProp( *p );
281 this->m_ViewProps.erase( i );
285 i = this->m_AuxViewProps.find( name );
286 if( i != this->m_AuxViewProps.end( ) )
288 for( auto p = i->second.begin( ); p != i->second.end( ); ++p )
289 this->m_Renderer->RemoveViewProp( *p );
290 this->m_AuxViewProps.erase( i );
295 // -------------------------------------------------------------------------
296 void cpExtensions::QT::RendererWidget::
299 this->m_Renderer->RemoveAllViewProps( );
300 this->m_ViewProps.clear( );
301 this->m_AuxViewProps.clear( );
304 // -------------------------------------------------------------------------
305 void cpExtensions::QT::RendererWidget::
306 HideViewProps( const std::string& name )
308 auto i = this->m_ViewProps.find( name );
309 if( i != this->m_ViewProps.end( ) )
310 for( auto p = i->second.begin( ); p != i->second.end( ); ++p )
311 ( *p )->VisibilityOff( );
312 i = this->m_AuxViewProps.find( name );
313 if( i != this->m_AuxViewProps.end( ) )
314 for( auto p = i->second.begin( ); p != i->second.end( ); ++p )
315 ( *p )->VisibilityOff( );
318 // -------------------------------------------------------------------------
319 void cpExtensions::QT::RendererWidget::
320 ShowViewProps( const std::string& name )
322 auto i = this->m_ViewProps.find( name );
323 if( i != this->m_ViewProps.end( ) )
324 for( auto p = i->second.begin( ); p != i->second.end( ); ++p )
325 ( *p )->VisibilityOn( );
326 i = this->m_AuxViewProps.find( name );
327 if( i != this->m_AuxViewProps.end( ) )
328 for( auto p = i->second.begin( ); p != i->second.end( ); ++p )
329 ( *p )->VisibilityOn( );
332 // -------------------------------------------------------------------------
333 void cpExtensions::QT::RendererWidget::
336 this->m_Renderer->ResetCamera( );
339 // -------------------------------------------------------------------------
340 void cpExtensions::QT::RendererWidget::
343 this->GetRenderWindow( )->Render( );
346 // -------------------------------------------------------------------------
347 std::set< vtkRenderWindowInteractor* > cpExtensions::QT::RendererWidget::
348 GetInteractors( ) const
350 Self* self = const_cast< Self* >( this );
351 std::set< vtkRenderWindowInteractor* > ret;
352 ret.insert( self->GetRenderWindow( )->GetInteractor( ) );
356 // -------------------------------------------------------------------------
357 std::set< std::string > cpExtensions::QT::RendererWidget::
358 GetActorsNames( ) const
360 std::set< std::string > names;
362 auto p = this->m_ViewProps.begin( );
363 p != this->m_ViewProps.end( );
366 names.insert( p->first );
370 // -------------------------------------------------------------------------
371 bool cpExtensions::QT::RendererWidget::
372 IsWindowLevelImageActor( const std::string& name ) const
374 auto it = this->m_ViewProps.find( name );
375 if( it != this->m_ViewProps.end( ) )
377 auto a = it->second.begin( );
378 if( a != it->second.end( ) )
379 return( dynamic_cast< TWLActor* >( a->GetPointer( ) ) != NULL );
387 // -------------------------------------------------------------------------
388 bool cpExtensions::QT::RendererWidget::
389 IsLUTImageActor( const std::string& name ) const
391 auto it = this->m_ViewProps.find( name );
392 if( it != this->m_ViewProps.end( ) )
394 auto a = it->second.begin( );
395 if( a != it->second.end( ) )
396 return( dynamic_cast< TLUTActor* >( a->GetPointer( ) ) != NULL );
404 // -------------------------------------------------------------------------
405 bool cpExtensions::QT::RendererWidget::
406 Is3DActor( const std::string& name ) const
408 auto it = this->m_ViewProps.find( name );
409 if( it != this->m_ViewProps.end( ) )
411 auto a = it->second.begin( );
412 if( a != it->second.end( ) )
413 return( dynamic_cast< vtkActor* >( a->GetPointer( ) ) != NULL );
421 // -------------------------------------------------------------------------
422 void cpExtensions::QT::RendererWidget::
423 GetScalarRange( const std::string& name, double r[ 2 ] ) const
425 auto it = this->m_ViewProps.find( name );
426 if( it != this->m_ViewProps.end( ) )
428 auto p = it->second.begin( );
430 while( a == NULL && p != it->second.end( ) )
432 a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
442 // -------------------------------------------------------------------------
443 void cpExtensions::QT::RendererWidget::
444 GetWindowLevel( const std::string& name, double wl[ 2 ] ) const
446 auto it = this->m_ViewProps.find( name );
447 if( it != this->m_ViewProps.end( ) )
449 auto p = it->second.begin( );
451 while( a == NULL && p != it->second.end( ) )
453 a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
458 a->GetWindowLevel( wl );
463 // -------------------------------------------------------------------------
464 double cpExtensions::QT::RendererWidget::
465 GetWindow( const std::string& name ) const
467 auto it = this->m_ViewProps.find( name );
468 if( it != this->m_ViewProps.end( ) )
470 auto p = it->second.begin( );
472 while( a == NULL && p != it->second.end( ) )
474 a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
479 return( a->GetWindow( ) );
487 // -------------------------------------------------------------------------
488 double cpExtensions::QT::RendererWidget::
489 GetLevel( const std::string& name ) const
491 auto it = this->m_ViewProps.find( name );
492 if( it != this->m_ViewProps.end( ) )
494 auto p = it->second.begin( );
496 while( a == NULL && p != it->second.end( ) )
498 a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
503 return( a->GetLevel( ) );
511 // -------------------------------------------------------------------------
512 char cpExtensions::QT::RendererWidget::
513 GetImageInterpolation( const std::string& name ) const
515 auto it = this->m_ViewProps.find( name );
516 if( it != this->m_ViewProps.end( ) )
518 auto p = it->second.begin( );
520 while( a == NULL && p != it->second.end( ) )
522 a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
528 int int_type = a->GetProperty( )->GetInterpolationType( );
532 case VTK_NEAREST_INTERPOLATION: ret = 'N'; break;
533 case VTK_LINEAR_INTERPOLATION: ret = 'L'; break;
534 case VTK_CUBIC_INTERPOLATION: ret = 'C'; break;
535 default: ret = 0; break;
546 // -------------------------------------------------------------------------
547 void cpExtensions::QT::RendererWidget::
548 GetColor( const std::string& name, double& r, double& g, double& b ) const
550 auto it = this->m_ViewProps.find( name );
551 if( it != this->m_ViewProps.end( ) )
553 auto p = it->second.begin( );
555 while( a == NULL && p != it->second.end( ) )
557 a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
562 return( a->GetProperty( )->GetColor( r, g, b ) );
567 // -------------------------------------------------------------------------
568 double cpExtensions::QT::RendererWidget::
569 GetOpacity( const std::string& name ) const
571 auto it = this->m_ViewProps.find( name );
572 if( it != this->m_ViewProps.end( ) )
574 auto p = it->second.begin( );
576 vtkImageSlice* s = NULL;
577 while( a == NULL && s == NULL && p != it->second.end( ) )
579 a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
580 s = dynamic_cast< vtkImageSlice* >( p->GetPointer( ) );
585 return( a->GetProperty( )->GetOpacity( ) );
587 return( s->GetProperty( )->GetOpacity( ) );
594 // -------------------------------------------------------------------------
595 double cpExtensions::QT::RendererWidget::
596 GetPointSize( const std::string& name ) const
598 auto it = this->m_ViewProps.find( name );
599 if( it != this->m_ViewProps.end( ) )
601 auto p = it->second.begin( );
603 while( a == NULL && p != it->second.end( ) )
605 a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
610 return( a->GetProperty( )->GetPointSize( ) );
617 // -------------------------------------------------------------------------
618 double cpExtensions::QT::RendererWidget::
619 GetLineWidth( const std::string& name ) const
621 auto it = this->m_ViewProps.find( name );
622 if( it != this->m_ViewProps.end( ) )
624 auto p = it->second.begin( );
626 while( a == NULL && p != it->second.end( ) )
628 a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
633 return( a->GetProperty( )->GetLineWidth( ) );
640 // -------------------------------------------------------------------------
641 int cpExtensions::QT::RendererWidget::
642 GetRepresentation( const std::string& name ) const
644 auto it = this->m_ViewProps.find( name );
645 if( it != this->m_ViewProps.end( ) )
647 auto p = it->second.begin( );
649 while( a == NULL && p != it->second.end( ) )
651 a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
656 return( a->GetProperty( )->GetRepresentation( ) );
663 // -------------------------------------------------------------------------
664 void cpExtensions::QT::RendererWidget::
665 SetScalarRange( const std::string& name, double r[ 2 ] )
667 this->SetScalarRange( name, r[ 0 ], r[ 1 ] );
670 // -------------------------------------------------------------------------
671 void cpExtensions::QT::RendererWidget::
672 SetScalarRange( const std::string& name, double min, double max )
674 auto it = this->m_ViewProps.find( name );
675 if( it != this->m_ViewProps.end( ) )
678 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
680 TWLActor* a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
684 a->SetRange( min, max );
695 // -------------------------------------------------------------------------
696 void cpExtensions::QT::RendererWidget::
697 SetWindowLevel( const std::string& name, double wl[ 2 ] )
699 this->SetWindowLevel( name, wl[ 0 ], wl[ 1 ] );
702 // -------------------------------------------------------------------------
703 void cpExtensions::QT::RendererWidget::
704 SetWindowLevel( const std::string& name, double w, double l )
706 auto it = this->m_ViewProps.find( name );
707 if( it != this->m_ViewProps.end( ) )
710 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
712 TWLActor* a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
716 a->SetWindowLevel( w, l );
727 // -------------------------------------------------------------------------
728 void cpExtensions::QT::RendererWidget::
729 SetWindow( const std::string& name, double w )
731 auto it = this->m_ViewProps.find( name );
732 if( it != this->m_ViewProps.end( ) )
735 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
737 TWLActor* a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
752 // -------------------------------------------------------------------------
753 void cpExtensions::QT::RendererWidget::
754 SetLevel( const std::string& name, double l )
756 auto it = this->m_ViewProps.find( name );
757 if( it != this->m_ViewProps.end( ) )
760 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
762 TWLActor* a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
777 // -------------------------------------------------------------------------
778 void cpExtensions::QT::RendererWidget::
779 SetImageInterpolation( const std::string& name, char i )
781 auto it = this->m_ViewProps.find( name );
782 if( it != this->m_ViewProps.end( ) )
785 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
787 TWLActor* a = dynamic_cast< TWLActor* >( p->GetPointer( ) );
791 int int_type = VTK_NEAREST_INTERPOLATION;
794 case 'L': int_type = VTK_LINEAR_INTERPOLATION; break;
795 case 'C': int_type = VTK_CUBIC_INTERPOLATION; break;
796 default: int_type = VTK_NEAREST_INTERPOLATION; break;
798 a->GetProperty( )->SetInterpolationType( int_type );
809 // -------------------------------------------------------------------------
810 void cpExtensions::QT::RendererWidget::
811 SetColor( const std::string& name, double r, double g, double b )
813 auto it = this->m_ViewProps.find( name );
814 if( it != this->m_ViewProps.end( ) )
817 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
819 vtkActor* a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
823 a->GetProperty( )->SetColor( r, g, b );
834 // -------------------------------------------------------------------------
835 void cpExtensions::QT::RendererWidget::
836 SetOpacity( const std::string& name, double o )
838 auto it = this->m_ViewProps.find( name );
839 if( it != this->m_ViewProps.end( ) )
842 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
844 vtkActor* a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
845 vtkImageSlice* s = dynamic_cast< vtkImageSlice* >( p->GetPointer( ) );
849 a->GetProperty( )->SetOpacity( o );
854 s->GetProperty( )->SetOpacity( o );
865 // -------------------------------------------------------------------------
866 void cpExtensions::QT::RendererWidget::
867 SetPointSize( const std::string& name, double s )
869 auto it = this->m_ViewProps.find( name );
870 if( it != this->m_ViewProps.end( ) )
873 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
875 vtkActor* a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
879 a->GetProperty( )->SetPointSize( s );
890 // -------------------------------------------------------------------------
891 void cpExtensions::QT::RendererWidget::
892 SetLineWidth( const std::string& name, double w )
894 auto it = this->m_ViewProps.find( name );
895 if( it != this->m_ViewProps.end( ) )
898 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
900 vtkActor* a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
904 a->GetProperty( )->SetLineWidth( w );
915 // -------------------------------------------------------------------------
916 void cpExtensions::QT::RendererWidget::
917 SetRepresentationToPoints( const std::string& name )
919 auto it = this->m_ViewProps.find( name );
920 if( it != this->m_ViewProps.end( ) )
923 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
925 vtkActor* a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
929 a->GetProperty( )->SetRepresentationToPoints( );
940 // -------------------------------------------------------------------------
941 void cpExtensions::QT::RendererWidget::
942 SetRepresentationToSurface( const std::string& name )
944 auto it = this->m_ViewProps.find( name );
945 if( it != this->m_ViewProps.end( ) )
948 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
950 vtkActor* a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
954 a->GetProperty( )->SetRepresentationToSurface( );
965 // -------------------------------------------------------------------------
966 void cpExtensions::QT::RendererWidget::
967 SetRepresentationToWireframe( const std::string& name )
969 auto it = this->m_ViewProps.find( name );
970 if( it != this->m_ViewProps.end( ) )
973 for( auto p = it->second.begin( ); p != it->second.end( ); ++p )
975 vtkActor* a = dynamic_cast< vtkActor* >( p->GetPointer( ) );
979 a->GetProperty( )->SetRepresentationToWireframe( );