]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Visualization/MPRActors.cxx
e26651b4dd24098b7df304e741041789419694b0
[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 SetAxesCursor( const int& axis, double* pos )
356 {
357   if( axis < 3 )
358     this->Slices[ 0 ][ axis ]->SetAxesCursor( pos );
359 }
360
361 // -------------------------------------------------------------------------
362 void cpExtensions::Visualization::MPRActors::
363 Render( const int& axis )
364 {
365   if( axis < 3 )
366   {
367     this->Slices[ 0 ][ axis ]->Render( );
368     this->Slices[ 1 ][ axis ]->Render( );
369
370   } // fi
371 }
372
373 // -------------------------------------------------------------------------
374 void cpExtensions::Visualization::MPRActors::
375 Render( )
376 {
377   this->Render( 0 );
378   this->Render( 1 );
379   this->Render( 2 );
380 }
381
382 // -------------------------------------------------------------------------
383 void cpExtensions::Visualization::MPRActors::
384 ResetCamera( const int& axis )
385 {
386   if( axis < 3 )
387     this->Slices[ 0 ][ axis ]->ResetCamera( );
388 }
389
390 // -------------------------------------------------------------------------
391 void cpExtensions::Visualization::MPRActors::
392 ResetCameras( )
393 {
394   this->ResetCamera( 0 );
395   this->ResetCamera( 1 );
396   this->ResetCamera( 2 );
397 }
398
399 // -------------------------------------------------------------------------
400 cpExtensions::Visualization::MPRActors::
401 MPRActors( )
402   : Superclass( )
403 {
404   this->ImageOutlineActor = vtkSmartPointer< vtkActor >::New( );
405   for( unsigned int i = 0; i < 2; ++i )
406     for( unsigned int j = 0; j < 3; ++j )
407       this->Slices[ i ][ j ] = vtkSmartPointer< ImageSliceActors >::New( );
408
409   this->Slices[ 0 ][ 0 ]->AssociateSlice( this->Slices[ 1 ][ 0 ] );
410   this->Slices[ 0 ][ 1 ]->AssociateSlice( this->Slices[ 1 ][ 1 ] );
411   this->Slices[ 0 ][ 2 ]->AssociateSlice( this->Slices[ 1 ][ 2 ] );
412
413   for( unsigned int i = 0; i < 3; ++ i )
414   {
415     this->Slices[ 0 ][ i ]->AddRenderCommand(
416       Self::_RenderCommand, this
417       );
418     this->Slices[ 0 ][ i ]->AddWindowLevelCommand(
419       Self::_WindowLevelCommand, this
420       );
421     this->Slices[ 0 ][ i ]->AddSlicesCommand( Self::_SlicesCommand, this );
422
423   } // rof
424 }
425
426 // -------------------------------------------------------------------------
427 cpExtensions::Visualization::MPRActors::
428 ~MPRActors( )
429 {
430 }
431
432 // -------------------------------------------------------------------------
433 void cpExtensions::Visualization::MPRActors::
434 _CreateBoundingBox( )
435 {
436   vtkImageData* image =
437     dynamic_cast< vtkImageData* >( this->Slices[ 0 ][ 0 ]->GetInputImage( ) );
438   if( image == NULL )
439     return;
440
441   // Create 3D outline
442   double bb[ 6 ];
443   image->GetBounds( bb );
444
445   vtkSmartPointer< vtkOutlineSource > img_ol =
446     vtkSmartPointer< vtkOutlineSource >::New( );
447   img_ol->SetBounds( bb );
448
449   vtkSmartPointer< vtkPolyDataMapper > img_ol_mapper =
450     vtkSmartPointer< vtkPolyDataMapper >::New( );
451   img_ol_mapper->SetInputConnection( img_ol->GetOutputPort( ) );
452   this->ImageOutlineActor->SetMapper( img_ol_mapper );
453   this->ImageOutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
454   this->ImageOutlineActor->GetProperty( )->SetLineWidth( 1 );
455
456   this->AddItem( this->ImageOutlineActor );
457 }
458
459 // -------------------------------------------------------------------------
460 void cpExtensions::Visualization::MPRActors::
461 _SlicesCommand( double* pos, int axis, void* data )
462 {
463   MPRActors* actors = reinterpret_cast< MPRActors* >( data );
464   if( actors == NULL )
465     return;
466   for( unsigned int j = 0; j < 3; ++j )
467   {
468     if( actors->Slices[ 0 ][ j ]->GetAxis( ) != axis )
469     {
470       actors->SetSlice( j, pos );
471       actors->SetAxesCursor( j, pos );
472       actors->Render( j );
473
474     } // fi
475
476   } // rof
477 }
478
479 // -------------------------------------------------------------------------
480 void cpExtensions::Visualization::MPRActors::
481 _WindowLevelCommand( double window, double level, void* data )
482 {
483   MPRActors* actors = reinterpret_cast< MPRActors* >( data );
484   if( actors == NULL )
485     return;
486   actors->SetWindowLevel( window, level );
487   actors->Render( );
488 }
489
490 // -------------------------------------------------------------------------
491 void cpExtensions::Visualization::MPRActors::
492 _RenderCommand( void* data )
493 {
494   MPRActors* actors = reinterpret_cast< MPRActors* >( data );
495   if( actors == NULL )
496     return;
497   actors->Render( );
498 }
499
500 // eof - $RCSfile$