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