]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Visualization/MPRActors.cxx
MPR finished
[cpPlugins.git] / lib / cpExtensions / Visualization / MPRActors.cxx
1 #include <cpExtensions/Visualization/MPRActors.h>
2
3 #include <vtkAlgorithmOutput.h>
4 #include <vtkImageData.h>
5 #include <vtkOutlineSource.h>
6 #include <vtkProperty.h>
7 #include <vtkRenderer.h>
8 #include <vtkRendererCollection.h>
9 #include <vtkRenderWindow.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   for( int i = 0; i < 2; ++i )
35   {
36     for( int j = 0; j < 3; ++j )
37     {
38       this->Slices[ i ][ j ]->SetInputConnection( aout );
39       this->Slices[ i ][ j ]->SetAxis( j );
40
41     } // rof
42
43   } // rof
44   this->_CreateBoundingBox( );
45 }
46
47 // -------------------------------------------------------------------------
48 void cpExtensions::Visualization::MPRActors::
49 SetInputImage( vtkImageData* data )
50 {
51   for( int i = 0; i < 2; ++i )
52   {
53     for( int j = 0; j < 3; ++j )
54     {
55       this->Slices[ i ][ j ]->SetInputImage( data );
56       this->Slices[ i ][ j ]->SetAxis( j );
57
58     } // rof
59
60   } // rof
61   this->_CreateBoundingBox( );
62 }
63
64 // -------------------------------------------------------------------------
65 int cpExtensions::Visualization::MPRActors::
66 AddBinaryConnection(
67   vtkAlgorithmOutput* aout,
68   const double& r, const double& g, const double& b
69   )
70 {
71   this->Slices[ 0 ][ 0 ]->AddBinaryConnection( aout, r, g, b );
72   this->Slices[ 0 ][ 1 ]->AddBinaryConnection( aout, r, g, b );
73   this->Slices[ 0 ][ 2 ]->AddBinaryConnection( aout, r, g, b );
74   this->Slices[ 1 ][ 0 ]->AddBinaryConnection( aout, r, g, b );
75   this->Slices[ 1 ][ 1 ]->AddBinaryConnection( aout, r, g, b );
76   return( this->Slices[ 1 ][ 2 ]->AddBinaryConnection( aout, r, g, b ) );
77 }
78
79 // -------------------------------------------------------------------------
80 int cpExtensions::Visualization::MPRActors::
81 AddBinaryImage(
82   vtkImageData* data,
83   const double& r, const double& g, const double& b
84   )
85 {
86   this->Slices[ 0 ][ 0 ]->AddBinaryImage( data, r, g, b );
87   this->Slices[ 0 ][ 1 ]->AddBinaryImage( data, r, g, b );
88   this->Slices[ 0 ][ 2 ]->AddBinaryImage( data, r, g, b );
89   this->Slices[ 1 ][ 0 ]->AddBinaryImage( data, r, g, b );
90   this->Slices[ 1 ][ 1 ]->AddBinaryImage( data, r, g, b );
91   return( this->Slices[ 1 ][ 2 ]->AddBinaryImage( data, r, g, b ) );
92 }
93
94 // -------------------------------------------------------------------------
95 void cpExtensions::Visualization::MPRActors::
96 Clear( )
97 {
98   this->Slices[ 0 ][ 0 ]->Clear( );
99   this->Slices[ 0 ][ 1 ]->Clear( );
100   this->Slices[ 0 ][ 2 ]->Clear( );
101   this->Slices[ 1 ][ 0 ]->Clear( );
102   this->Slices[ 1 ][ 1 ]->Clear( );
103   this->Slices[ 1 ][ 2 ]->Clear( );
104 }
105
106 // -------------------------------------------------------------------------
107 vtkImageData* cpExtensions::Visualization::MPRActors::
108 GetInputImage( )
109 {
110   return( this->Slices[ 0 ][ 0 ]->GetInputImage( ) );
111 }
112
113 // -------------------------------------------------------------------------
114 const vtkImageData* cpExtensions::Visualization::MPRActors::
115 GetInputImage( ) const
116 {
117   return( this->Slices[ 0 ][ 0 ]->GetInputImage( ) );
118 }
119
120 // -------------------------------------------------------------------------
121 unsigned int cpExtensions::Visualization::MPRActors::
122 GetNumberOfImages( ) const
123 {
124   return( this->Slices[ 0 ][ 0 ]->GetNumberOfImages( ) );
125 }
126
127 // -------------------------------------------------------------------------
128 void cpExtensions::Visualization::MPRActors::
129 SetInterpolate( bool v )
130 {
131   this->Slices[ 0 ][ 0 ]->SetInterpolate( v );
132   this->Slices[ 0 ][ 1 ]->SetInterpolate( v );
133   this->Slices[ 0 ][ 2 ]->SetInterpolate( v );
134   this->Slices[ 1 ][ 0 ]->SetInterpolate( v );
135   this->Slices[ 1 ][ 1 ]->SetInterpolate( v );
136   this->Slices[ 1 ][ 2 ]->SetInterpolate( v );
137 }
138
139 // -------------------------------------------------------------------------
140 void cpExtensions::Visualization::MPRActors::
141 InterpolateOn( )
142 {
143   this->SetInterpolate( true );
144 }
145
146 // -------------------------------------------------------------------------
147 void cpExtensions::Visualization::MPRActors::
148 InterpolateOff( )
149 {
150   this->SetInterpolate( false );
151 }
152
153 // -------------------------------------------------------------------------
154 void cpExtensions::Visualization::MPRActors::
155 PushActorsInto(
156   vtkRenderWindow* x,
157   vtkRenderWindow* y,
158   vtkRenderWindow* z,
159   vtkRenderWindow* w
160   )
161 {
162   this->Slices[ 0 ][ 0 ]->PushActorsInto( x, true );
163   this->Slices[ 0 ][ 1 ]->PushActorsInto( y, true );
164   this->Slices[ 0 ][ 2 ]->PushActorsInto( z, true );
165   this->Slices[ 1 ][ 0 ]->PushActorsInto( w, false );
166   this->Slices[ 1 ][ 1 ]->PushActorsInto( w, false );
167   this->Slices[ 1 ][ 2 ]->PushActorsInto( w, false );
168
169   vtkRenderer* wren =
170     ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
171   if( wren != NULL )
172   {
173     wren->AddViewProp( this->ImageOutlineActor );
174     wren->AddViewProp( this->Slices[ 1 ][ 0 ]->GetPlaneActor( ) );
175     wren->AddViewProp( this->Slices[ 1 ][ 1 ]->GetPlaneActor( ) );
176     wren->AddViewProp( this->Slices[ 1 ][ 2 ]->GetPlaneActor( ) );
177
178   } // fi
179 }
180
181 // -------------------------------------------------------------------------
182 void cpExtensions::Visualization::MPRActors::
183 PopActorsFrom(
184   vtkRenderWindow* x,
185   vtkRenderWindow* y,
186   vtkRenderWindow* z,
187   vtkRenderWindow* w
188   )
189 {
190   this->Slices[ 0 ][ 0 ]->PopActorsFrom( x );
191   this->Slices[ 0 ][ 1 ]->PopActorsFrom( y );
192   this->Slices[ 0 ][ 2 ]->PopActorsFrom( z );
193   this->Slices[ 1 ][ 0 ]->PopActorsFrom( w );
194   this->Slices[ 1 ][ 1 ]->PopActorsFrom( w );
195   this->Slices[ 1 ][ 2 ]->PopActorsFrom( w );
196
197   vtkRenderer* wren =
198     ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
199   if( wren != NULL )
200   {
201     wren->RemoveViewProp( this->ImageOutlineActor );
202     wren->RemoveViewProp( this->Slices[ 1 ][ 0 ]->GetPlaneActor( ) );
203     wren->RemoveViewProp( this->Slices[ 1 ][ 1 ]->GetPlaneActor( ) );
204     wren->RemoveViewProp( this->Slices[ 1 ][ 2 ]->GetPlaneActor( ) );
205
206   } // fi
207 }
208
209 // -------------------------------------------------------------------------
210 double cpExtensions::Visualization::MPRActors::
211 GetMinWindow( ) const
212 {
213   return( this->Slices[ 0 ][ 0 ]->GetMinWindow( ) );
214 }
215
216 // -------------------------------------------------------------------------
217 double cpExtensions::Visualization::MPRActors::
218 GetMaxWindow( ) const
219 {
220   return( this->Slices[ 0 ][ 0 ]->GetMaxWindow( ) );
221 }
222
223 // -------------------------------------------------------------------------
224 double cpExtensions::Visualization::MPRActors::
225 GetMinLevel( ) const
226 {
227   return( this->Slices[ 0 ][ 0 ]->GetMinLevel( ) );
228 }
229
230 // -------------------------------------------------------------------------
231 double cpExtensions::Visualization::MPRActors::
232 GetMaxLevel( ) const
233 {
234   return( this->Slices[ 0 ][ 0 ]->GetMaxLevel( ) );
235 }
236
237 // -------------------------------------------------------------------------
238 double cpExtensions::Visualization::MPRActors::
239 GetWindow( )
240 {
241   return( this->Slices[ 0 ][ 0 ]->GetWindow( ) );
242 }
243
244 // -------------------------------------------------------------------------
245 double cpExtensions::Visualization::MPRActors::
246 GetLevel( )
247 {
248   return( this->Slices[ 0 ][ 0 ]->GetLevel( ) );
249 }
250
251 // -------------------------------------------------------------------------
252 void cpExtensions::Visualization::MPRActors::
253 SetWindow( double w )
254 {
255   this->Slices[ 0 ][ 0 ]->SetWindow( w );
256   this->Slices[ 0 ][ 1 ]->SetWindow( w );
257   this->Slices[ 0 ][ 2 ]->SetWindow( w );
258   this->Slices[ 1 ][ 0 ]->SetWindow( w );
259   this->Slices[ 1 ][ 1 ]->SetWindow( w );
260   this->Slices[ 1 ][ 2 ]->SetWindow( w );
261 }
262
263 // -------------------------------------------------------------------------
264 void cpExtensions::Visualization::MPRActors::
265 SetLevel( double l )
266 {
267   this->Slices[ 0 ][ 0 ]->SetLevel( l );
268   this->Slices[ 0 ][ 1 ]->SetLevel( l );
269   this->Slices[ 0 ][ 2 ]->SetLevel( l );
270   this->Slices[ 1 ][ 0 ]->SetLevel( l );
271   this->Slices[ 1 ][ 1 ]->SetLevel( l );
272   this->Slices[ 1 ][ 2 ]->SetLevel( l );
273 }
274
275 // -------------------------------------------------------------------------
276 void cpExtensions::Visualization::MPRActors::
277 SetWindowLevel( double w, double l )
278 {
279   this->Slices[ 0 ][ 0 ]->SetWindowLevel( w, l );
280   this->Slices[ 0 ][ 1 ]->SetWindowLevel( w, l );
281   this->Slices[ 0 ][ 2 ]->SetWindowLevel( w, l );
282   this->Slices[ 1 ][ 0 ]->SetWindowLevel( w, l );
283   this->Slices[ 1 ][ 1 ]->SetWindowLevel( w, l );
284   this->Slices[ 1 ][ 2 ]->SetWindowLevel( w, l );
285 }
286
287 // -------------------------------------------------------------------------
288 void cpExtensions::Visualization::MPRActors::
289 ResetWindowLevel( )
290 {
291   this->Slices[ 0 ][ 0 ]->ResetWindowLevel( );
292   this->Slices[ 0 ][ 1 ]->ResetWindowLevel( );
293   this->Slices[ 0 ][ 2 ]->ResetWindowLevel( );
294   this->Slices[ 1 ][ 0 ]->ResetWindowLevel( );
295   this->Slices[ 1 ][ 1 ]->ResetWindowLevel( );
296   this->Slices[ 1 ][ 2 ]->ResetWindowLevel( );
297 }
298
299 // -------------------------------------------------------------------------
300 int cpExtensions::Visualization::MPRActors::
301 GetSliceNumber( const int& axis ) const
302 {
303   if( axis < 3 )
304     return( this->Slices[ 0 ][ axis ]->GetSliceNumber( ) );
305   else
306     return( -1 );
307 }
308
309 // -------------------------------------------------------------------------
310 int cpExtensions::Visualization::MPRActors::
311 GetSliceNumberMinValue( const int& axis ) const
312 {
313   if( axis < 3 )
314     return( this->Slices[ 0 ][ axis ]->GetSliceNumberMinValue( ) );
315   else
316     return( -1 );
317 }
318
319 // -------------------------------------------------------------------------
320 int cpExtensions::Visualization::MPRActors::
321 GetSliceNumberMaxValue( const int& axis ) const
322 {
323   if( axis < 3 )
324     return( this->Slices[ 0 ][ axis ]->GetSliceNumberMaxValue( ) );
325   else
326     return( -1 );
327 }
328
329 // -------------------------------------------------------------------------
330 void cpExtensions::Visualization::MPRActors::
331 SetSliceNumber( const int& axis, const int& slice )
332 {
333   if( axis < 3 )
334   {
335     this->Slices[ 0 ][ axis ]->SetSliceNumber( slice );
336     this->Slices[ 1 ][ axis ]->SetSliceNumber( slice );
337
338   } // fi
339 }
340
341 // -------------------------------------------------------------------------
342 void cpExtensions::Visualization::MPRActors::
343 SetSlice( const int& axis, double* pos )
344 {
345   if( axis < 3 )
346   {
347     this->Slices[ 0 ][ axis ]->SetSlice( pos );
348     this->Slices[ 1 ][ axis ]->SetSlice( pos );
349
350   } // fi
351 }
352
353 // -------------------------------------------------------------------------
354 void cpExtensions::Visualization::MPRActors::
355 Render( const int& axis )
356 {
357   if( axis < 3 )
358   {
359     this->Slices[ 0 ][ axis ]->Render( );
360     this->Slices[ 1 ][ axis ]->Render( );
361
362   } // fi
363 }
364
365 // -------------------------------------------------------------------------
366 void cpExtensions::Visualization::MPRActors::
367 Render( )
368 {
369   this->Render( 0 );
370   this->Render( 1 );
371   this->Render( 2 );
372 }
373
374 // -------------------------------------------------------------------------
375 void cpExtensions::Visualization::MPRActors::
376 ResetCamera( const int& axis )
377 {
378   if( axis < 3 )
379     this->Slices[ 0 ][ axis ]->ResetCamera( );
380 }
381
382 // -------------------------------------------------------------------------
383 void cpExtensions::Visualization::MPRActors::
384 ResetCameras( )
385 {
386   this->ResetCamera( 0 );
387   this->ResetCamera( 1 );
388   this->ResetCamera( 2 );
389 }
390
391 // -------------------------------------------------------------------------
392 cpExtensions::Visualization::MPRActors::
393 MPRActors( )
394   : Superclass( )
395 {
396   this->ImageOutlineActor = vtkSmartPointer< vtkActor >::New( );
397   for( unsigned int i = 0; i < 2; ++i )
398     for( unsigned int j = 0; j < 3; ++j )
399       this->Slices[ i ][ j ] = vtkSmartPointer< ImageSliceActors >::New( );
400
401   this->Slices[ 0 ][ 0 ]->AssociateSlice( this->Slices[ 1 ][ 0 ] );
402   this->Slices[ 0 ][ 1 ]->AssociateSlice( this->Slices[ 1 ][ 1 ] );
403   this->Slices[ 0 ][ 2 ]->AssociateSlice( this->Slices[ 1 ][ 2 ] );
404
405   for( unsigned int i = 0; i < 3; ++ i )
406   {
407     this->Slices[ 0 ][ i ]->AddRenderCommand(
408       Self::_RenderCommand, this
409       );
410     this->Slices[ 0 ][ i ]->AddWindowLevelCommand(
411       Self::_WindowLevelCommand, this
412       );
413     this->Slices[ 0 ][ i ]->AddSlicesCommand( Self::_SlicesCommand, this );
414
415   } // rof
416 }
417
418 // -------------------------------------------------------------------------
419 cpExtensions::Visualization::MPRActors::
420 ~MPRActors( )
421 {
422 }
423
424 // -------------------------------------------------------------------------
425 void cpExtensions::Visualization::MPRActors::
426 _CreateBoundingBox( )
427 {
428   vtkImageData* image =
429     dynamic_cast< vtkImageData* >( this->Slices[ 0 ][ 0 ]->GetInputImage( ) );
430   if( image == NULL )
431     return;
432
433   // Create 3D outline
434   double bb[ 6 ];
435   image->GetBounds( bb );
436
437   vtkSmartPointer< vtkOutlineSource > img_ol =
438     vtkSmartPointer< vtkOutlineSource >::New( );
439   img_ol->SetBounds( bb );
440
441   vtkSmartPointer< vtkPolyDataMapper > img_ol_mapper =
442     vtkSmartPointer< vtkPolyDataMapper >::New( );
443   img_ol_mapper->SetInputConnection( img_ol->GetOutputPort( ) );
444   this->ImageOutlineActor->SetMapper( img_ol_mapper );
445   this->ImageOutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
446   this->ImageOutlineActor->GetProperty( )->SetLineWidth( 1 );
447
448   this->AddItem( this->ImageOutlineActor );
449 }
450
451 // -------------------------------------------------------------------------
452 void cpExtensions::Visualization::MPRActors::
453 _SlicesCommand( double* pos, int axis, void* data )
454 {
455   MPRActors* actors = reinterpret_cast< MPRActors* >( data );
456   if( actors == NULL )
457     return;
458   for( unsigned int j = 0; j < 3; ++j )
459   {
460     if( actors->Slices[ 0 ][ j ]->GetAxis( ) != axis )
461     {
462       actors->SetSlice( j, pos );
463       actors->Render( j );
464
465     } // fi
466
467   } // rof
468 }
469
470 // -------------------------------------------------------------------------
471 void cpExtensions::Visualization::MPRActors::
472 _WindowLevelCommand( double window, double level, void* data )
473 {
474   MPRActors* actors = reinterpret_cast< MPRActors* >( data );
475   if( actors == NULL )
476     return;
477   actors->SetWindowLevel( window, level );
478   actors->Render( );
479 }
480
481 // -------------------------------------------------------------------------
482 void cpExtensions::Visualization::MPRActors::
483 _RenderCommand( void* data )
484 {
485   MPRActors* actors = reinterpret_cast< MPRActors* >( data );
486   if( actors == NULL )
487     return;
488   actors->Render( );
489 }
490
491 // eof - $RCSfile$