]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/QT/ImageWidget.cxx
Start contour tracer widget debugging: change interactor style.
[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/ImageOutlineActor.h>
7 #include <cpExtensions/Visualization/WindowLevelImageActor.h>
8
9 #include <vtkRenderer.h>
10
11 /* TODO
12    #include <cpExtensions/Visualization/ImageViewerActors.h>
13    #include <cpExtensions/Visualization/LUTImageActor.h>
14    #include <vtkCamera.h>
15    #include <vtkImageData.h>
16    #include <vtkImageProperty.h>
17 */
18
19 // -------------------------------------------------------------------------
20 cpExtensions::QT::ImageWidget::
21 ImageWidget( QWidget* parent, Qt::WindowFlags f )
22   : Superclass( parent, f ),
23     m_ImageName( "" )
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 }
35
36 // -------------------------------------------------------------------------
37 void cpExtensions::QT::ImageWidget::
38 Clear( )
39 {
40   this->RemoveViewProps( );
41   this->m_ImageName = "";
42 }
43
44 // -------------------------------------------------------------------------
45 void cpExtensions::QT::ImageWidget::
46 SetImage( vtkImageData* image, int orientation, const std::string& name )
47 {
48   if( name == "" )
49     return;
50   if( this->m_ImageName != "" )
51     this->Clear( );
52   this->m_ImageName = name;
53
54   this->m_WLActor = vtkSmartPointer< TWLActor >::New( );
55   this->m_WLActor->SetImage( image );
56   this->m_WLActor->SetOrientation( orientation );
57
58   this->m_OLActor = vtkSmartPointer< TOLActor >::New( );
59   this->m_OLActor->SetBounds(
60     orientation,
61     this->m_WLActor->GetBounds( )[ orientation ],
62     this->m_WLActor->GetBounds( )
63     );
64
65   this->AddViewProp( this->m_WLActor, this->m_ImageName );
66   this->AddAuxViewProp( this->m_OLActor, this->m_ImageName );
67   this->ResetCamera( );
68 }
69
70
71 /* TODO
72 // -------------------------------------------------------------------------
73 vtkInteractorStyle* cpExtensions::QT::ImageWidget::
74 GetInteractorStyle( )
75 {
76   return( this->m_ImageSliceStyle );
77 }
78
79 // -------------------------------------------------------------------------
80 const vtkInteractorStyle* cpExtensions::QT::ImageWidget::
81 GetInteractorStyle( ) const
82 {
83   return( this->m_ImageSliceStyle );
84 }
85
86 // -------------------------------------------------------------------------
87 void cpExtensions::QT::ImageWidget::
88 SetImage( vtkImageData* image, int orientation, const std::string& name )
89 {
90   if( this->m_ImageName != "" )
91   {
92     // TODO: Clear visualization
93
94   } // fi
95   this->m_ImageName = name;
96
97   this->GetActiveCamera( )->ParallelProjectionOn( );
98
99   this->m_ImageViewerActors =
100     vtkSmartPointer< cpExtensions::Visualization::ImageViewerActors >::New( );
101   this->m_ImageViewerActors->SetImage( image, orientation );
102
103   this->m_ImageSliceStyle->SetActors( this->m_ImageViewerActors );
104
105   this->AddViewProp(
106     this->m_ImageViewerActors->GetWindowLevelImageActor( ),
107     this->m_ImageName
108     );
109   this->AddAuxViewProps( this->m_ImageViewerActors, this->m_ImageName );
110   this->ResetCamera( );
111 }
112
113 // -------------------------------------------------------------------------
114 void cpExtensions::QT::ImageWidget::
115 Add( vtkDataSet* data, const std::string& name )
116 {
117   auto image = dynamic_cast< vtkImageData* >( data );
118
119   if( image == NULL )
120   {
121        if( this->m_ImageName == "" )
122        this->SetImage( data, name );
123        {
124        } // fi
125     return;
126
127   } // fi
128
129   if( this->m_ImageName == "" )
130     return;
131
132   if( this->m_ImageViewerActors.GetPointer( ) != NULL )
133   {
134     if( this->m_ImageViewerActors->AddLUTImage( image ) > 0 )
135     {
136       this->AddViewProp(
137         this->m_ImageViewerActors->GetLUTImageActor( ),
138         name
139         );
140       this->Render( );
141
142     } // fi
143
144   } // fi
145 }
146
147 // -------------------------------------------------------------------------
148 int cpExtensions::QT::ImageWidget::
149 GetOrientation( ) const
150 {
151   return( this->m_ImageViewerActors->GetOrientation( ) );
152 }
153
154 // -------------------------------------------------------------------------
155 void cpExtensions::QT::ImageWidget::
156 SetSliceNumber( int slice )
157 {
158   this->m_ImageViewerActors->SetSliceNumber( slice );
159   this->Render( );
160 }
161
162 // -------------------------------------------------------------------------
163 void cpExtensions::QT::ImageWidget::
164 ResetCamera( )
165 {
166   if( this->m_ImageViewerActors.GetPointer( ) != NULL )
167   {
168     auto image = this->m_ImageViewerActors->GetImage( );
169     if( image != NULL )
170     {
171       double bounds[ 6 ];
172       image->GetBounds( bounds );
173
174       // Compute camera properties
175       double center[ 3 ];
176       center[ 0 ] = ( bounds[ 1 ] + bounds[ 0 ] ) / double( 2 );
177       center[ 1 ] = ( bounds[ 3 ] + bounds[ 2 ] ) / double( 2 );
178       center[ 2 ] = ( bounds[ 5 ] + bounds[ 4 ] ) / double( 2 );
179
180       int ori = this->m_ImageViewerActors->GetOrientation( );
181       double pos[ 3 ] = { double( 0 ) };
182       pos[ ori ] = double( 1 );
183       pos[ 0 ] += center[ 0 ];
184       pos[ 1 ] += center[ 1 ];
185       pos[ 2 ] += center[ 2 ];
186
187       double up[ 3 ] = { double( 0 ) };
188       if( ori == 0 )
189       {
190         if     ( this->m_Quadrant == 0 ) up[ 2 ] = double( 1 );
191         else if( this->m_Quadrant == 1 ) up[ 2 ] = double( 1 );
192         else if( this->m_Quadrant == 2 ) up[ 2 ] = double( 1 );
193         else if( this->m_Quadrant == 3 ) up[ 2 ] = double( 1 );
194       }
195       else if( ori == 1 )
196       {
197         if     ( this->m_Quadrant == 0 ) up[ 2 ] = double( 1 );
198         else if( this->m_Quadrant == 1 ) up[ 2 ] = double( 1 );
199         else if( this->m_Quadrant == 2 ) up[ 2 ] = double( 1 );
200         else if( this->m_Quadrant == 3 ) up[ 2 ] = double( 1 );
201       }
202       else if( ori == 2 )
203       {
204         if     ( this->m_Quadrant == 0 ) up[ 1 ] = double( 1 );
205         else if( this->m_Quadrant == 1 ) up[ 1 ] = double( 1 );
206         else if( this->m_Quadrant == 2 ) up[ 1 ] = double( 1 );
207         else if( this->m_Quadrant == 3 ) up[ 1 ] = double( 1 );
208         pos[ 2 ] *= double( -1 );
209
210       } // fi
211
212       // Reconfigure camera and return
213       auto camera = this->m_Renderer->GetActiveCamera( );
214       camera->SetFocalPoint( center );
215       camera->SetPosition( pos );
216       camera->SetViewUp( up );
217       this->m_Renderer->ResetCamera( bounds );
218     }
219     else
220       this->Superclass::ResetCamera( );
221   }
222   else
223     this->Superclass::ResetCamera( );
224 }
225
226 // -------------------------------------------------------------------------
227 void cpExtensions::QT::ImageWidget::
228 GetScalarRange( double r[ 2 ] ) const
229 {
230   auto actor = this->m_ImageViewerActors->GetWindowLevelImageActor( );
231   if( actor != NULL )
232     actor->GetRange( r );
233   else
234     r[ 0 ] = r[ 1 ] = double( 0 );
235 }
236
237 // -------------------------------------------------------------------------
238 void cpExtensions::QT::ImageWidget::
239 GetWindowLevel( double wl[ 2 ] ) const
240 {
241   auto actor = this->m_ImageViewerActors->GetWindowLevelImageActor( );
242   if( actor != NULL )
243   {
244     wl[ 0 ] = actor->GetWindow( );
245     wl[ 1 ] = actor->GetLevel( );
246   }
247   else
248     wl[ 0 ] = wl[ 1 ] = double( 0 );
249 }
250
251 // -------------------------------------------------------------------------
252 double cpExtensions::QT::ImageWidget::
253 GetImageOpacity( ) const
254 {
255   auto actor = this->m_ImageViewerActors->GetWindowLevelImageActor( );
256   if( actor != NULL )
257     return( actor->GetProperty( )->GetOpacity( ) );
258   else
259     return( double( 0 ) );
260 }
261
262 // -------------------------------------------------------------------------
263 unsigned char cpExtensions::QT::ImageWidget::
264 GetImageInterpolation( ) const
265 {
266   auto actor = this->m_ImageViewerActors->GetWindowLevelImageActor( );
267   if( actor != NULL )
268   {
269     int int_type = actor->GetProperty( )->GetInterpolationType( );
270     char ret = 0;
271     switch( int_type )
272     {
273     case VTK_NEAREST_INTERPOLATION: ret = 'N'; break;
274     case VTK_LINEAR_INTERPOLATION: ret = 'L'; break;
275     case VTK_CUBIC_INTERPOLATION: ret = 'C'; break;
276     default: ret = 0; break;
277     } // hctiws
278     return( ret );
279   }
280   else
281     return( 0 );
282 }
283
284 // -------------------------------------------------------------------------
285 void cpExtensions::QT::ImageWidget::
286 SetScalarRange( double r[ 2 ] )
287 {
288   auto actor = this->m_ImageViewerActors->GetWindowLevelImageActor( );
289   if( actor != NULL )
290   {
291     actor->ConfigureWindowLevel( r[ 0 ], r[ 1 ] );
292     this->Render( );
293
294   } // fi
295 }
296
297 // -------------------------------------------------------------------------
298 void cpExtensions::QT::ImageWidget::
299 SetWindowLevel( double wl[ 2 ] )
300 {
301   auto actor = this->m_ImageViewerActors->GetWindowLevelImageActor( );
302   if( actor != NULL )
303   {
304     actor->SetWindowLevel( wl[ 0 ], wl[ 1 ] );
305     this->Render( );
306
307   } // fi
308 }
309
310 // -------------------------------------------------------------------------
311 void cpExtensions::QT::ImageWidget::
312 SetImageOpacity( double o )
313 {
314   auto actor = this->m_ImageViewerActors->GetWindowLevelImageActor( );
315   if( actor != NULL )
316   {
317     actor->GetProperty( )->SetOpacity( o );
318     this->Render( );
319
320   } // fi
321 }
322
323 // -------------------------------------------------------------------------
324 void cpExtensions::QT::ImageWidget::
325 SetImageInterpolation( unsigned char i )
326 {
327   auto actor = this->m_ImageViewerActors->GetWindowLevelImageActor( );
328   if( actor != NULL )
329   {
330     int int_type = 0;
331     switch( i )
332     {
333     case 'L': int_type = VTK_LINEAR_INTERPOLATION; break;
334     case 'C': int_type = VTK_CUBIC_INTERPOLATION; break;
335     default : int_type = VTK_NEAREST_INTERPOLATION; break;
336     } // hctiws
337     actor->GetProperty( )->SetInterpolationType( int_type );
338     this->Render( );
339
340   } // fi
341 }
342
343 // -------------------------------------------------------------------------
344 vtkProp* cpExtensions::QT::ImageWidget::
345 GetImageActor( )
346 {
347   return( this->m_ImageViewerActors->GetWindowLevelImageActor( ) );
348 }
349
350 // -------------------------------------------------------------------------
351 const vtkProp* cpExtensions::QT::ImageWidget::
352 GetImageActor( ) const
353 {
354   return( this->m_ImageViewerActors->GetWindowLevelImageActor( ) );
355 }
356 */
357
358 #endif // cpExtensions_QT4
359
360 // eof - $RCSfile$