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>
18 // -------------------------------------------------------------------------
19 cpExtensions::QT::ImageWidget::
20 ImageWidget( QWidget* parent, Qt::WindowFlags f )
21 : Superclass( parent, f ),
23 m_OutlineActor( NULL )
25 this->m_Style = vtkSmartPointer< TStyle >::New( );
26 this->m_Style->SetCurrentRenderer( this->m_Renderer );
27 this->SetStyle( this->m_Style );
30 // -------------------------------------------------------------------------
31 cpExtensions::QT::ImageWidget::
35 if( this->m_OutlineActor != NULL )
36 delete this->m_OutlineActor;
37 for( auto a = this->m_Actors.begin( ); a != this->m_Actors.end( ); ++a )
39 this->m_Actors.clear( );
42 // -------------------------------------------------------------------------
43 void cpExtensions::QT::ImageWidget::
46 this->RemoveViewProps( );
47 this->m_ImageName = "";
50 // -------------------------------------------------------------------------
51 void cpExtensions::QT::ImageWidget::
52 SetImage( vtkImageData* image, int orientation, const std::string& name )
56 if( this->m_ImageName != "" )
58 this->m_ImageName = name;
60 this->m_WLActor = vtkSmartPointer< TWLActor >::New( );
61 this->m_WLActor->SetImage( image );
62 this->m_WLActor->SetOrientation( orientation );
64 this->m_LUTActor = vtkSmartPointer< TLUTActor >::New( );
65 this->m_LUTActor->SetOrientation( orientation );
67 this->m_Outline = vtkSmartPointer< TOutline >::New( );
68 this->m_Outline->SetBounds( image->GetBounds( ) );
69 this->m_Outline->Update( );
71 if( this->m_OutlineActor != NULL )
72 delete this->m_OutlineActor;
73 this->m_OutlineActor = new TActor( );
74 this->m_OutlineActor->SetMesh( this->m_Outline->GetOutput( ) );
76 double cr = double( 0 );
77 double cg = double( 0 );
78 double cb = double( 0 );
79 switch( this->m_WLActor->GetOrientation( ) )
81 case 0: cr = double( 1 ); break;
82 case 1: cg = double( 1 ); break;
83 case 2: cb = double( 1 ); break;
84 default: cr = double( 1 ); break;
86 this->m_OutlineActor->GetActor( )->GetProperty( )->SetColor( cr, cg, cb );
88 this->AddViewProp( this->m_WLActor, this->m_ImageName );
89 this->AddAuxViewProp( this->m_OutlineActor->GetActor( ), this->m_ImageName );
93 // -------------------------------------------------------------------------
94 void cpExtensions::QT::ImageWidget::
95 Add( vtkDataSet* data, const std::string& name )
97 auto image = dynamic_cast< vtkImageData* >( data );
98 auto pdata = dynamic_cast< vtkPolyData* >( data );
101 this->m_LUTActor->AddImage( image );
102 this->m_LUTActor->GetProperty( )->SetOpacity( 0.5 );
103 this->AddViewProp( this->m_LUTActor, "__LUT_IMAGE__" );
106 else if( pdata != NULL )
108 TActor* actor = new TActor( );
109 actor->SetMesh( pdata );
110 this->m_Actors.push_back( actor );
111 this->AddViewProp( actor->GetActor( ), name );
117 // -------------------------------------------------------------------------
118 void cpExtensions::QT::ImageWidget::
121 if( this->m_WLActor.GetPointer( ) != NULL )
123 auto image = this->m_WLActor->GetImage( );
127 image->GetBounds( bounds );
129 // Compute camera properties
131 center[ 0 ] = ( bounds[ 1 ] + bounds[ 0 ] ) / double( 2 );
132 center[ 1 ] = ( bounds[ 3 ] + bounds[ 2 ] ) / double( 2 );
133 center[ 2 ] = ( bounds[ 5 ] + bounds[ 4 ] ) / double( 2 );
135 int ori = this->m_WLActor->GetOrientation( );
136 double pos[ 3 ] = { double( 0 ) };
137 pos[ ori ] = double( 1 );
138 pos[ 0 ] += center[ 0 ];
139 pos[ 1 ] += center[ 1 ];
140 pos[ 2 ] += center[ 2 ];
142 double up[ 3 ] = { double( 0 ) };
145 if ( this->m_Quadrant == 0 ) up[ 2 ] = double( 1 );
146 else if( this->m_Quadrant == 1 ) up[ 2 ] = double( 1 );
147 else if( this->m_Quadrant == 2 ) up[ 2 ] = double( 1 );
148 else if( this->m_Quadrant == 3 ) up[ 2 ] = double( 1 );
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[ 1 ] = double( -1 );
160 else if( this->m_Quadrant == 1 ) up[ 1 ] = double( -1 );
161 else if( this->m_Quadrant == 2 ) up[ 1 ] = double( -1 );
162 else if( this->m_Quadrant == 3 ) up[ 1 ] = double( -1 );
163 pos[ 2 ] *= double( -1 );
167 // Reconfigure camera and return
168 auto camera = this->m_Renderer->GetActiveCamera( );
169 camera->ParallelProjectionOn( );
170 camera->SetFocalPoint( center );
171 camera->SetPosition( pos );
172 camera->SetViewUp( up );
173 this->m_Renderer->ResetCamera( bounds );
176 this->Superclass::ResetCamera( );
179 this->Superclass::ResetCamera( );
182 // -------------------------------------------------------------------------
183 cpExtensions::QT::ImageWidget::
184 TWLActor* cpExtensions::QT::ImageWidget::
187 return( this->m_WLActor );
190 // -------------------------------------------------------------------------
191 const cpExtensions::QT::ImageWidget::
192 TWLActor* cpExtensions::QT::ImageWidget::
193 GetImageActor( ) const
195 return( this->m_WLActor );
198 // -------------------------------------------------------------------------
199 void cpExtensions::QT::ImageWidget::
200 SetColor( const std::string& name, double r, double g, double b )
202 auto props = this->GetViewProps( name );
203 for( auto p = props.begin( ); p != props.end( ); ++p )
205 auto actor = dynamic_cast< vtkActor* >( p->GetPointer( ) );
207 actor->GetProperty( )->SetColor( r, g, b );
213 // -------------------------------------------------------------------------
214 void cpExtensions::QT::ImageWidget::
215 SetLineWidth( const std::string& name, double w )
217 auto props = this->GetViewProps( name );
218 for( auto p = props.begin( ); p != props.end( ); ++p )
220 auto actor = dynamic_cast< vtkActor* >( p->GetPointer( ) );
222 actor->GetProperty( )->SetLineWidth( w );
228 #endif // cpExtensions_QT4