]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Visualization/MPRActors.cxx
2f7ce682a97e80585589e6a01a14873b52b3636a
[cpPlugins.git] / lib / cpExtensions / Visualization / MPRActors.cxx
1 #include <cpExtensions/Visualization/MPRActors.h>
2
3 #include <vtkImageData.h>
4 #include <vtkInformation.h>
5 #include <vtkOutlineSource.h>
6 #include <vtkPolyDataMapper.h>
7 #include <vtkProperty.h>
8 #include <vtkRenderer.h>
9 #include <vtkWindowLevelLookupTable.h>
10
11 // -------------------------------------------------------------------------
12 cpExtensions::Visualization::MPRActors*
13 cpExtensions::Visualization::MPRActors::
14 New( )
15 {
16   return( new Self( ) );
17 }
18
19 // -------------------------------------------------------------------------
20 cpExtensions::Visualization::
21 ImageSliceActors* cpExtensions::Visualization::MPRActors::
22 GetSliceActors( const int& i ) const
23 {
24   if( i < 3 )
25     return( this->Slices[ 0 ][ i ] );
26   else
27     return( NULL );
28 }
29
30 // -------------------------------------------------------------------------
31 void cpExtensions::Visualization::MPRActors::
32 SetInputConnection( vtkAlgorithmOutput* aout )
33 {
34   this->ImageMapToColors->SetInputConnection( aout );
35   this->SetLookupTableToWindowLevel( );
36   this->_UpdateSlices( );
37 }
38
39 // -------------------------------------------------------------------------
40 void cpExtensions::Visualization::MPRActors::
41 SetInputData( vtkImageData* image )
42 {
43   this->ImageMapToColors->SetInputData( image );
44   this->SetLookupTableToWindowLevel( );
45   this->_UpdateSlices( );
46 }
47
48 // -------------------------------------------------------------------------
49 void cpExtensions::Visualization::MPRActors::
50 PushDataInto(
51   vtkRenderer* x, vtkRenderer* y, vtkRenderer* z, vtkRenderer* w
52   )
53 {
54   vtkRenderer* rends[] = { x, y, z };
55   for( int i = 0; i < 3; ++i )
56   {
57     if( rends[ i ] != NULL )
58     {
59       rends[ i ]->AddActor( this->Slices[ 0 ][ i ]->GetImageActor( ) );
60       rends[ i ]->AddActor( this->Slices[ 0 ][ i ]->GetTextActor( ) );
61       for( int j = 0; j < 3; ++j )
62         rends[ i ]->AddActor( this->Slices[ 0 ][ j ]->GetPlaneActor( ) );
63
64     } // fi
65     if( w != NULL )
66     {
67       w->AddActor( this->Slices[ 1 ][ i ]->GetImageActor( ) );
68       w->AddActor( this->Slices[ 1 ][ i ]->GetPlaneActor( ) );
69
70     } // fi
71
72   } // rof
73   if( w != NULL )
74     w->AddActor( this->ImageOutlineActor );
75 }
76
77 // -------------------------------------------------------------------------
78 void cpExtensions::Visualization::MPRActors::
79 PopDataFrom( vtkRenderer* x, vtkRenderer* y, vtkRenderer* z, vtkRenderer* w )
80 {
81   vtkRenderer* rends[] = { x, y, z };
82   for( int i = 0; i < 3; ++i )
83   {
84     if( rends[ i ] != NULL )
85     {
86       rends[ i ]->RemoveActor( this->Slices[ 0 ][ i ]->GetImageActor( ) );
87       rends[ i ]->RemoveActor( this->Slices[ 0 ][ i ]->GetTextActor( ) );
88       for( int j = 0; j < 3; ++j )
89         rends[ i ]->RemoveActor( this->Slices[ 0 ][ j ]->GetPlaneActor( ) );
90       if( w != NULL )
91       {
92         w->RemoveActor( this->Slices[ 1 ][ i ]->GetImageActor( ) );
93         w->RemoveActor( this->Slices[ 1 ][ i ]->GetPlaneActor( ) );
94
95       } // fi
96
97     } // fi
98
99   } // rof
100
101   if( w != NULL )
102     w->RemoveActor( this->ImageOutlineActor );
103 }
104
105 // -------------------------------------------------------------------------
106 vtkScalarsToColors* cpExtensions::Visualization::MPRActors::
107 GetLookupTable( ) const
108 {
109   return( this->ImageMapToColors->GetLookupTable( ) );
110 }
111
112 // -------------------------------------------------------------------------
113 void cpExtensions::Visualization::MPRActors::
114 SetLookupTable( vtkScalarsToColors* lut )
115 {
116   this->ImageMapToColors->SetLookupTable( lut );
117   this->ImageMapToColors->Update( );
118   this->Modified( );
119 }
120
121 // -------------------------------------------------------------------------
122 vtkWindowLevelLookupTable* cpExtensions::Visualization::MPRActors::
123 GetLookupTableAsWindowLevel( ) const
124 {
125   return(
126     dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( ) )
127     );
128 }
129
130 // -------------------------------------------------------------------------
131 void cpExtensions::Visualization::MPRActors::
132 SetLookupTableToWindowLevel( )
133 {
134   // Check if the input has been configured
135   vtkImageData* image = this->_InputImage( );
136   if( image == NULL )
137     return;
138
139   double r[ 2 ];
140   image->GetScalarRange( r );
141
142   vtkSmartPointer< vtkWindowLevelLookupTable > lut =
143     vtkSmartPointer< vtkWindowLevelLookupTable >::New( );
144   lut->SetScaleToLinear( );
145   lut->SetTableRange( r );
146   lut->Build( );
147
148   this->SetLookupTable( lut );
149 }
150
151 // -------------------------------------------------------------------------
152 double cpExtensions::Visualization::MPRActors::
153 GetMinWindow( ) const
154 {
155   return( double( 0 ) );
156 }
157
158 // -------------------------------------------------------------------------
159 double cpExtensions::Visualization::MPRActors::
160 GetMaxWindow( ) const
161 {
162   // Check if the input has been configured
163   vtkImageData* image = this->_InputImage( );
164   if( image == NULL )
165     return( double( 0 ) );
166
167   double r[ 2 ];
168   image->GetScalarRange( r );
169   return( r[ 1 ] - r[ 0 ] );
170 }
171
172 // -------------------------------------------------------------------------
173 double cpExtensions::Visualization::MPRActors::
174 GetMinLevel( ) const
175 {
176   // Check if the input has been configured
177   vtkImageData* image = this->_InputImage( );
178   if( image == NULL )
179     return( double( 0 ) );
180
181   double r[ 2 ];
182   image->GetScalarRange( r );
183   return( r[ 0 ] );
184 }
185
186 // -------------------------------------------------------------------------
187 double cpExtensions::Visualization::MPRActors::
188 GetMaxLevel( ) const
189 {
190   // Check if the input has been configured
191   vtkImageData* image = this->_InputImage( );
192   if( image == NULL )
193     return( double( 0 ) );
194
195   double r[ 2 ];
196   image->GetScalarRange( r );
197   return( r[ 1 ] );
198 }
199
200 // -------------------------------------------------------------------------
201 double cpExtensions::Visualization::MPRActors::
202 GetWindow( ) const
203 {
204   vtkWindowLevelLookupTable* lut = this->GetLookupTableAsWindowLevel( );
205   if( lut != NULL )
206     return( lut->GetWindow( ) );
207   else
208     return( double( 0 ) );
209 }
210
211 // -------------------------------------------------------------------------
212 double cpExtensions::Visualization::MPRActors::
213 GetLevel( ) const
214 {
215   vtkWindowLevelLookupTable* lut = this->GetLookupTableAsWindowLevel( );
216   if( lut != NULL )
217     return( lut->GetLevel( ) );
218   else
219     return( double( 0 ) );
220 }
221
222 // -------------------------------------------------------------------------
223 void cpExtensions::Visualization::MPRActors::
224 SetWindow( const double& w )
225 {
226   vtkWindowLevelLookupTable* lut = this->GetLookupTableAsWindowLevel( );
227   if( lut != NULL )
228   {
229     lut->SetWindow( w );
230     lut->Build( );
231     this->ImageMapToColors->Modified( );
232     this->Modified( );
233
234   } // fi
235 }
236
237 // -------------------------------------------------------------------------
238 void cpExtensions::Visualization::MPRActors::
239 SetLevel( const double& l )
240 {
241   vtkWindowLevelLookupTable* lut = this->GetLookupTableAsWindowLevel( );
242   if( lut != NULL )
243   {
244     lut->SetLevel( l );
245     lut->Build( );
246     this->ImageMapToColors->Modified( );
247     this->Modified( );
248
249   } // fi
250 }
251
252 // -------------------------------------------------------------------------
253 void cpExtensions::Visualization::MPRActors::
254 SetWindowLevel( const double& w, const double& l )
255 {
256   vtkWindowLevelLookupTable* lut = this->GetLookupTableAsWindowLevel( );
257   if( lut != NULL )
258   {
259     lut->SetWindow( l );
260     lut->SetLevel( l );
261     lut->Build( );
262     this->ImageMapToColors->Modified( );
263     this->Modified( );
264
265   } // fi
266 }
267
268 // -------------------------------------------------------------------------
269 void cpExtensions::Visualization::MPRActors::
270 ResetWindowLevel( )
271 {
272   vtkImageData* image = this->_InputImage( );
273   vtkWindowLevelLookupTable* lut = this->GetLookupTableAsWindowLevel( );
274   if( image != NULL && lut != NULL )
275   {
276     double r[ 2 ];
277     image->GetScalarRange( r );
278     lut->SetTableRange( r );
279     lut->Build( );
280     this->ImageMapToColors->Modified( );
281     this->Modified( );
282
283   } // fi
284 }
285
286 // -------------------------------------------------------------------------
287 int cpExtensions::Visualization::MPRActors::
288 GetSliceNumberMinValue( const int& axis ) const
289 {
290   return( this->Slices[ 0 ][ axis ]->GetSliceNumberMinValue( ) );
291 }
292
293 // -------------------------------------------------------------------------
294 int cpExtensions::Visualization::MPRActors::
295 GetSliceNumberMaxValue( const int& axis ) const
296 {
297   return( this->Slices[ 0 ][ axis ]->GetSliceNumberMaxValue( ) );
298 }
299
300 // -------------------------------------------------------------------------
301 int cpExtensions::Visualization::MPRActors::
302 GetSlice( const int& axis ) const
303 {
304   return( this->Slices[ 0 ][ axis ]->GetSliceNumber( ) );
305 }
306
307 // -------------------------------------------------------------------------
308 void cpExtensions::Visualization::MPRActors::
309 SetSlice( const int& axis, const int& slice )
310 {
311   vtkImageData* image = this->_InputImage( );
312   if( image == NULL )
313     return;
314
315   // Get image data extent
316   int ext[ 6 ];
317   image->GetExtent( ext );
318
319   // Check if the slice is valid
320   int real = slice;
321   if( slice < ext[ axis << 1 ] )
322     real = ext[ axis << 1 ];
323   if( ext[ ( axis << 1 ) + 1 ] < slice )
324     real = ext[ ( axis << 1 ) + 1 ];
325
326   // Change slice
327   this->Slices[ 0 ][ axis ]->SetSliceNumber( real );
328   this->Slices[ 1 ][ axis ]->SetSliceNumber( real );
329   this->Modified( );
330 }
331
332 // -------------------------------------------------------------------------
333 void cpExtensions::Visualization::MPRActors::
334 SetSlice( const int& axis, const double& slice )
335 {
336   vtkImageData* image = this->_InputImage( );
337   if( image == NULL )
338     return;
339
340   double x[ 3 ] = { double( 0 ) };
341   double pcoords[ 3 ];
342   int ijk[ 3 ];
343
344   x[ axis ] = slice;
345   image->ComputeStructuredCoordinates( x, ijk, pcoords );
346   this->SetSlice( axis, ijk[ axis ] );
347 }
348
349 // -------------------------------------------------------------------------
350 void cpExtensions::Visualization::MPRActors::
351 ResetSlices( )
352 {
353   // TODO
354 }
355
356 // -------------------------------------------------------------------------
357 void cpExtensions::Visualization::MPRActors::
358 GetImageBounds( double bounds[ 6 ] ) const
359 {
360   vtkImageData* image = this->_InputImage( );
361   if( image != NULL )
362     image->GetBounds( bounds );
363 }
364
365 // -------------------------------------------------------------------------
366 cpExtensions::Visualization::MPRActors::
367 MPRActors( )
368   : Superclass( )
369 {
370   this->ImageMapToColors = vtkSmartPointer< vtkImageMapToColors >::New( );
371   this->ImageOutlineActor = vtkSmartPointer< vtkActor >::New( );
372   this->Slices[ 0 ][ 0 ] = vtkSmartPointer< _TSlice >::New( );
373   this->Slices[ 0 ][ 1 ] = vtkSmartPointer< _TSlice >::New( );
374   this->Slices[ 0 ][ 2 ] = vtkSmartPointer< _TSlice >::New( );
375   this->Slices[ 1 ][ 0 ] = vtkSmartPointer< _TSlice >::New( );
376   this->Slices[ 1 ][ 1 ] = vtkSmartPointer< _TSlice >::New( );
377   this->Slices[ 1 ][ 2 ] = vtkSmartPointer< _TSlice >::New( );
378 }
379
380 // -------------------------------------------------------------------------
381 cpExtensions::Visualization::MPRActors::
382 ~MPRActors( )
383 {
384 }
385
386 // -------------------------------------------------------------------------
387 vtkImageData* cpExtensions::Visualization::MPRActors::
388 _InputImage( ) const
389 {
390   vtkAlgorithm* algo = this->ImageMapToColors->GetInputAlgorithm( );
391   vtkInformation* info = algo->GetOutputInformation( 0 );
392   return(
393     vtkImageData::SafeDownCast( info->Get( vtkDataObject::DATA_OBJECT( ) ) )
394     );
395 }
396
397 // -------------------------------------------------------------------------
398 void cpExtensions::Visualization::MPRActors::
399 _UpdateSlices( )
400 {
401   // Check if the input has been configured
402   vtkImageData* image = this->_InputImage( );
403   if( image == NULL )
404     return;
405   this->ImageMapToColors->Update( );
406
407   for( int j = 0; j < 2; ++j )
408   {
409     for( int i = 0; i < 3; ++i )
410     {
411       this->Slices[ j ][ i ]->SetInputConnection(
412         this->ImageMapToColors->GetOutputPort( ), i
413         );
414       this->Slices[ j ][ i ]->UpdateText( );
415
416     } // rof
417
418   } // rof
419
420   // Create 3D outline
421   double bb[ 6 ];
422   image->GetBounds( bb );
423
424   vtkSmartPointer< vtkOutlineSource > img_ol =
425     vtkSmartPointer< vtkOutlineSource >::New( );
426   img_ol->SetBounds( bb );
427
428   vtkSmartPointer< vtkPolyDataMapper > img_ol_mapper =
429     vtkSmartPointer< vtkPolyDataMapper >::New( );
430   img_ol_mapper->SetInputConnection( img_ol->GetOutputPort( ) );
431   this->ImageOutlineActor->SetMapper( img_ol_mapper );
432   this->ImageOutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
433   this->ImageOutlineActor->GetProperty( )->SetLineWidth( 1 );
434
435   this->ImageOutlineActorIndex = this->GetNumberOfItems( );
436   this->AddItem( this->ImageOutlineActor );
437
438   // Cursor radius
439   /*
440     double spac[ 3 ];
441     image->GetSpacing( spac );
442     double radius = spac[ 0 ];
443     radius = ( spac[ 1 ] < radius )? spac[ 1 ]: radius;
444     radius = ( spac[ 2 ] < radius )? spac[ 2 ]: radius;
445     radius *= double( 6 );
446     this->Cursor->SetRadius( radius );
447     this->CursorMapper->Modified( );
448     this->CursorActor->Modified( );
449
450     // Plane actors
451     for( int a = 0; a < 3; ++a )
452     {
453     // Configure actors
454     this->Planes[ a ].Configure( this->ImageToWindowLevel->GetOutputPort( ), a );
455     this->Planes[ a ].ConfigureRegion( this->Region->GetOutputPort( ) );
456     this->Planes[ a ].UpdateText( this->GetWindow( ), this->GetLevel( ) );
457
458     // Add them to renderer
459     vtkRenderer* ren = this->Interactors[ a ]->GetRenderWindow( )->
460     GetRenderers( )->GetFirstRenderer( );
461     if( ren == NULL )
462     vtkErrorMacro( "At least one interactor doesn't have a valid renderer" );
463     ren->AddActor( this->Planes[ a ].ImageActor );
464     ren->AddActor( this->Planes[ a ].TextActor );
465
466     for( int i = 0; i < 3; ++i )
467     this->Interactors[ a ]->GetRenderWindow( )->
468     GetRenderers( )->GetFirstRenderer( )->
469     AddActor( this->Planes[ i ].PlaneActor );
470
471     } // rof
472   */
473   // Keep track into collection
474   /*
475     this->XPlaneIndex = this->GetNumberOfItems( );
476     this->AddItem( this->Planes[ 0 ].ImageActor.GetPointer( ) );
477     this->XTextIndex = this->GetNumberOfItems( );
478     this->AddItem( this->Planes[ 0 ].TextActor.GetPointer( ) );
479     this->XBoundsIndex = this->GetNumberOfItems( );
480     this->AddItem( this->Planes[ 0 ].PlaneActor.GetPointer( ) );
481
482     this->YPlaneIndex = this->GetNumberOfItems( );
483     this->AddItem( this->Planes[ 1 ].ImageActor.GetPointer( ) );
484     this->YTextIndex = this->GetNumberOfItems( );
485     this->AddItem( this->Planes[ 1 ].TextActor.GetPointer( ) );
486     this->YBoundsIndex = this->GetNumberOfItems( );
487     this->AddItem( this->Planes[ 1 ].PlaneActor.GetPointer( ) );
488
489     this->ZPlaneIndex = this->GetNumberOfItems( );
490     this->AddItem( this->Planes[ 2 ].ImageActor.GetPointer( ) );
491     this->ZTextIndex = this->GetNumberOfItems( );
492     this->AddItem( this->Planes[ 2 ].TextActor.GetPointer( ) );
493     this->ZBoundsIndex = this->GetNumberOfItems( );
494     this->AddItem( this->Planes[ 2 ].PlaneActor.GetPointer( ) );
495   */
496   // Initialize slice visualization
497   // this->ResetSlices( );
498
499   /*
500     #error CONTOUR_WIDGET <- ACA VOY
501     static vtkSmartPointer<vtkOrientedGlyphContourRepresentation> contourRep =
502     vtkSmartPointer<vtkOrientedGlyphContourRepresentation>::New();
503     static vtkSmartPointer<vtkContourWidget> contourWidget =
504     vtkSmartPointer<vtkContourWidget>::New();
505     contourWidget->SetInteractor( zi );
506     contourWidget->SetRepresentation( contourRep );
507     contourWidget->On( );
508   */
509 }
510
511 // eof - $RCSfile$