1 #include <cpExtensions/QT/ImageWidget.h>
2 #include <cpExtensions/Interaction/ImageSliceStyle.h>
3 #include <cpExtensions/Visualization/LUTImageActor.h>
4 #include <cpExtensions/Visualization/MeshActor.h>
5 #include <cpExtensions/Visualization/OutlineSource.h>
6 #include <cpExtensions/Visualization/WindowLevelImageActor.h>
10 #include <vtkImageData.h>
11 #include <vtkImageProperty.h>
12 #include <vtkProperty.h>
13 #include <vtkRenderer.h>
15 // -------------------------------------------------------------------------
16 cpExtensions::QT::ImageWidget::
17 ImageWidget( QWidget* parent, Qt::WindowFlags f )
18 : Superclass( parent, f ),
20 m_OutlineActor( NULL )
22 this->m_Style = vtkSmartPointer< TStyle >::New( );
23 this->m_Style->SetCurrentRenderer( this->m_Renderer );
24 this->SetStyle( this->m_Style );
27 // -------------------------------------------------------------------------
28 cpExtensions::QT::ImageWidget::
32 if( this->m_OutlineActor != NULL )
33 delete this->m_OutlineActor;
34 for( auto a = this->m_Actors.begin( ); a != this->m_Actors.end( ); ++a )
36 this->m_Actors.clear( );
39 // -------------------------------------------------------------------------
40 void cpExtensions::QT::ImageWidget::
43 this->RemoveViewProps( );
44 this->m_ImageName = "";
47 // -------------------------------------------------------------------------
48 void cpExtensions::QT::ImageWidget::
49 SetImage( vtkImageData* image, const std::string& name, int orientation )
53 if( this->m_ImageName != "" )
55 this->m_ImageName = name;
57 this->m_WLActor = vtkSmartPointer< TWLActor >::New( );
58 this->m_WLActor->SetImage( image );
59 this->m_WLActor->SetOrientation( orientation );
61 this->m_LUTActor = vtkSmartPointer< TLUTActor >::New( );
62 this->m_LUTActor->SetOrientation( orientation );
64 this->m_Outline = vtkSmartPointer< TOutline >::New( );
65 this->m_Outline->SetBounds( image->GetBounds( ) );
66 this->m_Outline->Update( );
68 if( this->m_OutlineActor != NULL )
69 delete this->m_OutlineActor;
70 this->m_OutlineActor = new TActor( );
71 this->m_OutlineActor->SetMesh( this->m_Outline->GetOutput( ) );
73 double cr = double( 0 );
74 double cg = double( 0 );
75 double cb = double( 0 );
76 switch( this->m_WLActor->GetOrientation( ) )
78 case 0: cr = double( 1 ); break;
79 case 1: cg = double( 1 ); break;
80 case 2: cb = double( 1 ); break;
81 default: cr = double( 1 ); break;
83 this->m_OutlineActor->GetActor( )->GetProperty( )->SetColor( cr, cg, cb );
85 this->AddViewProp( this->m_WLActor, this->m_ImageName );
86 this->AddAuxViewProp( this->m_OutlineActor->GetActor( ), this->m_ImageName );
90 // -------------------------------------------------------------------------
91 void cpExtensions::QT::ImageWidget::
92 Add( vtkDataSet* data, const std::string& name )
94 auto image = dynamic_cast< vtkImageData* >( data );
95 auto pdata = dynamic_cast< vtkPolyData* >( data );
98 this->m_LUTActor->AddImage( image );
99 this->m_LUTActor->GetProperty( )->SetOpacity( 0.5 );
100 this->AddViewProp( this->m_LUTActor, "__LUT_IMAGE__" );
103 else if( pdata != NULL )
105 TActor* actor = new TActor( );
106 actor->SetMesh( pdata );
107 this->m_Actors.push_back( actor );
108 this->AddViewProp( actor->GetActor( ), name );
114 // -------------------------------------------------------------------------
115 void cpExtensions::QT::ImageWidget::
118 if( this->m_WLActor.GetPointer( ) != NULL )
120 auto image = this->m_WLActor->GetImage( );
124 image->GetBounds( bounds );
126 // Compute camera properties
128 center[ 0 ] = ( bounds[ 1 ] + bounds[ 0 ] ) / double( 2 );
129 center[ 1 ] = ( bounds[ 3 ] + bounds[ 2 ] ) / double( 2 );
130 center[ 2 ] = ( bounds[ 5 ] + bounds[ 4 ] ) / double( 2 );
132 int ori = this->m_WLActor->GetOrientation( );
133 double pos[ 3 ] = { double( 0 ) };
134 pos[ ori ] = double( 1 );
135 pos[ 0 ] += center[ 0 ];
136 pos[ 1 ] += center[ 1 ];
137 pos[ 2 ] += center[ 2 ];
139 double up[ 3 ] = { double( 0 ) };
142 if ( this->m_Quadrant == 0 ) up[ 2 ] = double( 1 );
143 else if( this->m_Quadrant == 1 ) up[ 2 ] = double( 1 );
144 else if( this->m_Quadrant == 2 ) up[ 2 ] = double( 1 );
145 else if( this->m_Quadrant == 3 ) up[ 2 ] = double( 1 );
149 if ( this->m_Quadrant == 0 ) up[ 2 ] = double( 1 );
150 else if( this->m_Quadrant == 1 ) up[ 2 ] = double( 1 );
151 else if( this->m_Quadrant == 2 ) up[ 2 ] = double( 1 );
152 else if( this->m_Quadrant == 3 ) up[ 2 ] = double( 1 );
156 if ( this->m_Quadrant == 0 ) up[ 1 ] = double( -1 );
157 else if( this->m_Quadrant == 1 ) up[ 1 ] = double( -1 );
158 else if( this->m_Quadrant == 2 ) up[ 1 ] = double( -1 );
159 else if( this->m_Quadrant == 3 ) up[ 1 ] = double( -1 );
160 pos[ 2 ] *= double( -1 );
164 // Reconfigure camera and return
165 auto camera = this->m_Renderer->GetActiveCamera( );
166 camera->ParallelProjectionOn( );
167 camera->SetFocalPoint( center );
168 camera->SetPosition( pos );
169 camera->SetViewUp( up );
170 this->m_Renderer->ResetCamera( bounds );
173 this->Superclass::ResetCamera( );
176 this->Superclass::ResetCamera( );
179 // -------------------------------------------------------------------------
180 cpExtensions::QT::ImageWidget::
181 TWLActor* cpExtensions::QT::ImageWidget::
184 return( this->m_WLActor );
187 // -------------------------------------------------------------------------
188 const cpExtensions::QT::ImageWidget::
189 TWLActor* cpExtensions::QT::ImageWidget::
190 GetImageActor( ) const
192 return( this->m_WLActor );
195 // -------------------------------------------------------------------------
196 void cpExtensions::QT::ImageWidget::
197 SetColor( const std::string& name, double r, double g, double b )
199 auto props = this->GetViewProps( name );
200 for( auto p = props.begin( ); p != props.end( ); ++p )
202 auto actor = dynamic_cast< vtkActor* >( p->GetPointer( ) );
204 actor->GetProperty( )->SetColor( r, g, b );
210 // -------------------------------------------------------------------------
211 void cpExtensions::QT::ImageWidget::
212 SetLineWidth( const std::string& name, double w )
214 auto props = this->GetViewProps( name );
215 for( auto p = props.begin( ); p != props.end( ); ++p )
217 auto actor = dynamic_cast< vtkActor* >( p->GetPointer( ) );
219 actor->GetProperty( )->SetLineWidth( w );