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