]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/QT/ImageWidget.cxx
...
[cpPlugins.git] / lib / cpExtensions / QT / ImageWidget.cxx
1 #include <cpExtensions/QT/ImageWidget.h>
2
3 #ifdef cpExtensions_QT4
4
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>
10
11 #include <vtkActor.h>
12 #include <vtkCamera.h>
13 #include <vtkImageData.h>
14 #include <vtkImageProperty.h>
15 #include <vtkProperty.h>
16 #include <vtkRenderer.h>
17
18 // -------------------------------------------------------------------------
19 cpExtensions::QT::ImageWidget::
20 ImageWidget( QWidget* parent, Qt::WindowFlags f )
21   : Superclass( parent, f ),
22     m_ImageName( "" ),
23     m_OutlineActor( NULL )
24 {
25   this->m_Style = vtkSmartPointer< TStyle >::New( );
26   this->m_Style->SetCurrentRenderer( this->m_Renderer );
27   this->SetStyle( this->m_Style );
28 }
29
30 // -------------------------------------------------------------------------
31 cpExtensions::QT::ImageWidget::
32 ~ImageWidget( )
33 {
34   this->Clear( );
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 )
38     delete *a;
39   this->m_Actors.clear( );
40 }
41
42 // -------------------------------------------------------------------------
43 void cpExtensions::QT::ImageWidget::
44 Clear( )
45 {
46   this->RemoveViewProps( );
47   this->m_ImageName = "";
48 }
49
50 // -------------------------------------------------------------------------
51 void cpExtensions::QT::ImageWidget::
52 SetImage( vtkImageData* image, int orientation, const std::string& name )
53 {
54   if( name == "" )
55     return;
56   if( this->m_ImageName != "" )
57     this->Clear( );
58   this->m_ImageName = name;
59
60   this->m_WLActor = vtkSmartPointer< TWLActor >::New( );
61   this->m_WLActor->SetImage( image );
62   this->m_WLActor->SetOrientation( orientation );
63
64   this->m_LUTActor = vtkSmartPointer< TLUTActor >::New( );
65   this->m_LUTActor->SetOrientation( orientation );
66
67   this->m_Outline = vtkSmartPointer< TOutline >::New( );
68   this->m_Outline->SetBounds( image->GetBounds( ) );
69   this->m_Outline->Update( );
70
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( ) );
75
76   double cr = double( 0 );
77   double cg = double( 0 );
78   double cb = double( 0 );
79   switch( this->m_WLActor->GetOrientation( ) )
80   {
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;
85   } // hctiws
86   this->m_OutlineActor->GetActor( )->GetProperty( )->SetColor( cr, cg, cb );
87
88   this->AddViewProp( this->m_WLActor, this->m_ImageName );
89   this->AddAuxViewProp( this->m_OutlineActor->GetActor( ), this->m_ImageName );
90   this->ResetCamera( );
91 }
92
93 // -------------------------------------------------------------------------
94 void cpExtensions::QT::ImageWidget::
95 Add( vtkDataSet* data, const std::string& name )
96 {
97   auto image = dynamic_cast< vtkImageData* >( data );
98   auto pdata = dynamic_cast< vtkPolyData* >( data );
99   if( image != NULL )
100   {
101     this->m_LUTActor->AddImage( image );
102     this->m_LUTActor->GetProperty( )->SetOpacity( 0.5 );
103     this->AddViewProp( this->m_LUTActor, "__LUT_IMAGE__" );
104     this->Render( );
105   }
106   else if( pdata != NULL )
107   {
108     TActor* actor = new TActor( );
109     actor->SetMesh( pdata );
110     this->m_Actors.push_back( actor );
111     this->AddViewProp( actor->GetActor( ), name );
112     this->Render( );
113
114   } // fi
115 }
116
117 // -------------------------------------------------------------------------
118 void cpExtensions::QT::ImageWidget::
119 ResetCamera( )
120 {
121   if( this->m_WLActor.GetPointer( ) != NULL )
122   {
123     auto image = this->m_WLActor->GetImage( );
124     if( image != NULL )
125     {
126       double bounds[ 6 ];
127       image->GetBounds( bounds );
128
129       // Compute camera properties
130       double center[ 3 ];
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 );
134
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 ];
141
142       double up[ 3 ] = { double( 0 ) };
143       if( ori == 0 )
144       {
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 );
149       }
150       else if( ori == 1 )
151       {
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 );
156       }
157       else if( ori == 2 )
158       {
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 );
164
165       } // fi
166
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 );
174     }
175     else
176       this->Superclass::ResetCamera( );
177   }
178   else
179     this->Superclass::ResetCamera( );
180 }
181
182 // -------------------------------------------------------------------------
183 cpExtensions::QT::ImageWidget::
184 TWLActor* cpExtensions::QT::ImageWidget::
185 GetImageActor( )
186 {
187   return( this->m_WLActor );
188 }
189
190 // -------------------------------------------------------------------------
191 const cpExtensions::QT::ImageWidget::
192 TWLActor* cpExtensions::QT::ImageWidget::
193 GetImageActor( ) const
194 {
195   return( this->m_WLActor );
196 }
197
198 // -------------------------------------------------------------------------
199 void cpExtensions::QT::ImageWidget::
200 SetColor( const std::string& name, double r, double g, double b )
201 {
202   auto props = this->GetViewProps( name );
203   for( auto p = props.begin( ); p != props.end( ); ++p )
204   {
205     auto actor = dynamic_cast< vtkActor* >( p->GetPointer( ) );
206     if( actor != NULL )
207       actor->GetProperty( )->SetColor( r, g, b );
208
209   } // rof
210   this->Render( );
211 }
212
213 // -------------------------------------------------------------------------
214 void cpExtensions::QT::ImageWidget::
215 SetLineWidth( const std::string& name, double w )
216 {
217   auto props = this->GetViewProps( name );
218   for( auto p = props.begin( ); p != props.end( ); ++p )
219   {
220     auto actor = dynamic_cast< vtkActor* >( p->GetPointer( ) );
221     if( actor != NULL )
222       actor->GetProperty( )->SetLineWidth( w );
223
224   } // rof
225   this->Render( );
226 }
227
228 #endif // cpExtensions_QT4
229
230 // eof - $RCSfile$