1 #include <cpExtensions/QT/ImageWidget.h>
3 #ifdef cpExtensions_QT4
5 #include <cpExtensions/Interaction/ImageSliceStyle.h>
6 #include <cpExtensions/Visualization/LUTImageActor.h>
7 #include <cpExtensions/Visualization/MeshActor.h>
8 #include <cpExtensions/Visualization/OutlineSource.h>
9 #include <cpExtensions/Visualization/WindowLevelImageActor.h>
12 #include <vtkCamera.h>
13 #include <vtkImageData.h>
14 #include <vtkImageProperty.h>
15 #include <vtkProperty.h>
16 #include <vtkRenderer.h>
19 #include <cpExtensions/Visualization/ImageViewerActors.h>
20 #include <cpExtensions/Visualization/LUTImageActor.h>
21 #include <vtkImageData.h>
22 #include <vtkImageProperty.h>
25 // -------------------------------------------------------------------------
26 cpExtensions::QT::ImageWidget::
27 ImageWidget( QWidget* parent, Qt::WindowFlags f )
28 : Superclass( parent, f ),
30 m_OutlineActor( NULL )
32 this->m_Style = vtkSmartPointer< TStyle >::New( );
33 this->m_Style->SetCurrentRenderer( this->m_Renderer );
34 this->SetStyle( this->m_Style );
37 // -------------------------------------------------------------------------
38 cpExtensions::QT::ImageWidget::
42 if( this->m_OutlineActor != NULL )
43 delete this->m_OutlineActor;
44 for( auto a = this->m_Actors.begin( ); a != this->m_Actors.end( ); ++a )
46 this->m_Actors.clear( );
49 // -------------------------------------------------------------------------
50 void cpExtensions::QT::ImageWidget::
53 this->RemoveViewProps( );
54 this->m_ImageName = "";
57 // -------------------------------------------------------------------------
58 void cpExtensions::QT::ImageWidget::
59 SetImage( vtkImageData* image, int orientation, const std::string& name )
63 if( this->m_ImageName != "" )
65 this->m_ImageName = name;
67 this->m_WLActor = vtkSmartPointer< TWLActor >::New( );
68 this->m_WLActor->SetImage( image );
69 this->m_WLActor->SetOrientation( orientation );
71 this->m_LUTActor = vtkSmartPointer< TLUTActor >::New( );
72 this->m_LUTActor->SetOrientation( orientation );
74 this->m_Outline = vtkSmartPointer< TOutline >::New( );
75 this->m_Outline->SetBounds( image->GetBounds( ) );
76 this->m_Outline->Update( );
78 if( this->m_OutlineActor != NULL )
79 delete this->m_OutlineActor;
80 this->m_OutlineActor = new TActor( );
81 this->m_OutlineActor->SetMesh( this->m_Outline->GetOutput( ) );
83 double cr = double( 0 );
84 double cg = double( 0 );
85 double cb = double( 0 );
86 switch( this->m_WLActor->GetOrientation( ) )
88 case 0: cr = double( 1 ); break;
89 case 1: cg = double( 1 ); break;
90 case 2: cb = double( 1 ); break;
91 default: cr = double( 1 ); break;
93 this->m_OutlineActor->GetActor( )->GetProperty( )->SetColor( cr, cg, cb );
95 this->AddViewProp( this->m_WLActor, this->m_ImageName );
96 this->AddAuxViewProp( this->m_OutlineActor->GetActor( ), this->m_ImageName );
100 // -------------------------------------------------------------------------
101 void cpExtensions::QT::ImageWidget::
102 Add( vtkDataSet* data, const std::string& name )
104 auto image = dynamic_cast< vtkImageData* >( data );
105 auto pdata = dynamic_cast< vtkPolyData* >( data );
108 this->m_LUTActor->AddImage( image );
109 this->m_LUTActor->GetProperty( )->SetOpacity( 0.5 );
110 this->AddViewProp( this->m_LUTActor, "__LUT_IMAGE__" );
113 else if( pdata != NULL )
115 TActor* actor = new TActor( );
116 actor->SetMesh( pdata );
117 this->m_Actors.push_back( actor );
118 this->AddViewProp( actor->GetActor( ), name );
124 // -------------------------------------------------------------------------
125 void cpExtensions::QT::ImageWidget::
128 if( this->m_WLActor.GetPointer( ) != NULL )
130 auto image = this->m_WLActor->GetImage( );
134 image->GetBounds( bounds );
136 // Compute camera properties
138 center[ 0 ] = ( bounds[ 1 ] + bounds[ 0 ] ) / double( 2 );
139 center[ 1 ] = ( bounds[ 3 ] + bounds[ 2 ] ) / double( 2 );
140 center[ 2 ] = ( bounds[ 5 ] + bounds[ 4 ] ) / double( 2 );
142 int ori = this->m_WLActor->GetOrientation( );
143 double pos[ 3 ] = { double( 0 ) };
144 pos[ ori ] = double( 1 );
145 pos[ 0 ] += center[ 0 ];
146 pos[ 1 ] += center[ 1 ];
147 pos[ 2 ] += center[ 2 ];
149 double up[ 3 ] = { double( 0 ) };
152 if ( this->m_Quadrant == 0 ) up[ 2 ] = double( 1 );
153 else if( this->m_Quadrant == 1 ) up[ 2 ] = double( 1 );
154 else if( this->m_Quadrant == 2 ) up[ 2 ] = double( 1 );
155 else if( this->m_Quadrant == 3 ) up[ 2 ] = double( 1 );
159 if ( this->m_Quadrant == 0 ) up[ 2 ] = double( 1 );
160 else if( this->m_Quadrant == 1 ) up[ 2 ] = double( 1 );
161 else if( this->m_Quadrant == 2 ) up[ 2 ] = double( 1 );
162 else if( this->m_Quadrant == 3 ) up[ 2 ] = double( 1 );
166 if ( this->m_Quadrant == 0 ) up[ 1 ] = double( -1 );
167 else if( this->m_Quadrant == 1 ) up[ 1 ] = double( -1 );
168 else if( this->m_Quadrant == 2 ) up[ 1 ] = double( -1 );
169 else if( this->m_Quadrant == 3 ) up[ 1 ] = double( -1 );
170 pos[ 2 ] *= double( -1 );
174 // Reconfigure camera and return
175 auto camera = this->m_Renderer->GetActiveCamera( );
176 camera->ParallelProjectionOn( );
177 camera->SetFocalPoint( center );
178 camera->SetPosition( pos );
179 camera->SetViewUp( up );
180 this->m_Renderer->ResetCamera( bounds );
183 this->Superclass::ResetCamera( );
186 this->Superclass::ResetCamera( );
189 // -------------------------------------------------------------------------
190 cpExtensions::QT::ImageWidget::
191 TWLActor* cpExtensions::QT::ImageWidget::
194 return( this->m_WLActor );
197 // -------------------------------------------------------------------------
198 const cpExtensions::QT::ImageWidget::
199 TWLActor* cpExtensions::QT::ImageWidget::
200 GetImageActor( ) const
202 return( this->m_WLActor );
205 // -------------------------------------------------------------------------
206 void cpExtensions::QT::ImageWidget::
207 SetColor( const std::string& name, double r, double g, double b )
209 auto props = this->GetViewProps( name );
210 for( auto p = props.begin( ); p != props.end( ); ++p )
212 auto actor = dynamic_cast< vtkActor* >( p->GetPointer( ) );
214 actor->GetProperty( )->SetColor( r, g, b );
220 // -------------------------------------------------------------------------
221 void cpExtensions::QT::ImageWidget::
222 SetLineWidth( const std::string& name, double w )
224 auto props = this->GetViewProps( name );
225 for( auto p = props.begin( ); p != props.end( ); ++p )
227 auto actor = dynamic_cast< vtkActor* >( p->GetPointer( ) );
229 actor->GetProperty( )->SetLineWidth( w );
237 // -------------------------------------------------------------------------
238 vtkInteractorStyle* cpExtensions::QT::ImageWidget::
239 GetInteractorStyle( )
241 return( this->m_ImageSliceStyle );
244 // -------------------------------------------------------------------------
245 const vtkInteractorStyle* cpExtensions::QT::ImageWidget::
246 GetInteractorStyle( ) const
248 return( this->m_ImageSliceStyle );
251 // -------------------------------------------------------------------------
252 void cpExtensions::QT::ImageWidget::
253 SetImage( vtkImageData* image, int orientation, const std::string& name )
255 if( this->m_ImageName != "" )
257 // TODO: Clear visualization
260 this->m_ImageName = name;
262 this->GetActiveCamera( )->ParallelProjectionOn( );
264 this->m_ImageViewerActors =
265 vtkSmartPointer< cpExtensions::Visualization::ImageViewerActors >::New( );
266 this->m_ImageViewerActors->SetImage( image, orientation );
268 this->m_ImageSliceStyle->SetActors( this->m_ImageViewerActors );
271 this->m_ImageViewerActors->GetWindowLevelImageActor( ),
274 this->AddAuxViewProps( this->m_ImageViewerActors, this->m_ImageName );
275 this->ResetCamera( );
278 // -------------------------------------------------------------------------
279 void cpExtensions::QT::ImageWidget::
280 Add( vtkDataSet* data, const std::string& name )
282 auto image = dynamic_cast< vtkImageData* >( data );
286 if( this->m_ImageName == "" )
287 this->SetImage( data, name );
294 if( this->m_ImageName == "" )
297 if( this->m_ImageViewerActors.GetPointer( ) != NULL )
299 if( this->m_ImageViewerActors->AddLUTImage( image ) > 0 )
302 this->m_ImageViewerActors->GetLUTImageActor( ),
312 // -------------------------------------------------------------------------
313 int cpExtensions::QT::ImageWidget::
314 GetOrientation( ) const
316 return( this->m_ImageViewerActors->GetOrientation( ) );
319 // -------------------------------------------------------------------------
320 void cpExtensions::QT::ImageWidget::
321 SetSliceNumber( int slice )
323 this->m_ImageViewerActors->SetSliceNumber( slice );
327 // -------------------------------------------------------------------------
328 void cpExtensions::QT::ImageWidget::
329 GetScalarRange( double r[ 2 ] ) const
331 auto actor = this->m_ImageViewerActors->GetWindowLevelImageActor( );
333 actor->GetRange( r );
335 r[ 0 ] = r[ 1 ] = double( 0 );
338 // -------------------------------------------------------------------------
339 void cpExtensions::QT::ImageWidget::
340 GetWindowLevel( double wl[ 2 ] ) const
342 auto actor = this->m_ImageViewerActors->GetWindowLevelImageActor( );
345 wl[ 0 ] = actor->GetWindow( );
346 wl[ 1 ] = actor->GetLevel( );
349 wl[ 0 ] = wl[ 1 ] = double( 0 );
352 // -------------------------------------------------------------------------
353 double cpExtensions::QT::ImageWidget::
354 GetImageOpacity( ) const
356 auto actor = this->m_ImageViewerActors->GetWindowLevelImageActor( );
358 return( actor->GetProperty( )->GetOpacity( ) );
360 return( double( 0 ) );
363 // -------------------------------------------------------------------------
364 unsigned char cpExtensions::QT::ImageWidget::
365 GetImageInterpolation( ) const
367 auto actor = this->m_ImageViewerActors->GetWindowLevelImageActor( );
370 int int_type = actor->GetProperty( )->GetInterpolationType( );
374 case VTK_NEAREST_INTERPOLATION: ret = 'N'; break;
375 case VTK_LINEAR_INTERPOLATION: ret = 'L'; break;
376 case VTK_CUBIC_INTERPOLATION: ret = 'C'; break;
377 default: ret = 0; break;
385 // -------------------------------------------------------------------------
386 void cpExtensions::QT::ImageWidget::
387 SetScalarRange( double r[ 2 ] )
389 auto actor = this->m_ImageViewerActors->GetWindowLevelImageActor( );
392 actor->ConfigureWindowLevel( r[ 0 ], r[ 1 ] );
398 // -------------------------------------------------------------------------
399 void cpExtensions::QT::ImageWidget::
400 SetWindowLevel( double wl[ 2 ] )
402 auto actor = this->m_ImageViewerActors->GetWindowLevelImageActor( );
405 actor->SetWindowLevel( wl[ 0 ], wl[ 1 ] );
411 // -------------------------------------------------------------------------
412 void cpExtensions::QT::ImageWidget::
413 SetImageOpacity( double o )
415 auto actor = this->m_ImageViewerActors->GetWindowLevelImageActor( );
418 actor->GetProperty( )->SetOpacity( o );
424 // -------------------------------------------------------------------------
425 void cpExtensions::QT::ImageWidget::
426 SetImageInterpolation( unsigned char i )
428 auto actor = this->m_ImageViewerActors->GetWindowLevelImageActor( );
434 case 'L': int_type = VTK_LINEAR_INTERPOLATION; break;
435 case 'C': int_type = VTK_CUBIC_INTERPOLATION; break;
436 default : int_type = VTK_NEAREST_INTERPOLATION; break;
438 actor->GetProperty( )->SetInterpolationType( int_type );
444 // -------------------------------------------------------------------------
445 vtkProp* cpExtensions::QT::ImageWidget::
448 return( this->m_ImageViewerActors->GetWindowLevelImageActor( ) );
451 // -------------------------------------------------------------------------
452 const vtkProp* cpExtensions::QT::ImageWidget::
453 GetImageActor( ) const
455 return( this->m_ImageViewerActors->GetWindowLevelImageActor( ) );
459 #endif // cpExtensions_QT4