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